fileFactory.spec.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. const fs = require('fs');
  3. const md5 = require('md5');
  4. const path = require('path');
  5. const assert = require('assert');
  6. const server = require('./server');
  7. const {isFunc} = require('../lib/utilities');
  8. const fileFactory = require('../lib/fileFactory');
  9. const mockFileName = 'basketball.png';
  10. const mockFile = path.join(server.fileDir, mockFileName);
  11. const mockBuffer = fs.readFileSync(mockFile);
  12. const mockMd5 = md5(mockBuffer);
  13. const mockFileOpts = {
  14. name: mockFileName,
  15. buffer: mockBuffer,
  16. encoding: 'utf-8',
  17. mimetype: 'image/png',
  18. hash: mockMd5,
  19. tempFilePath: mockFile
  20. };
  21. describe('Test of the fileFactory factory', function() {
  22. beforeEach(() => server.clearUploadsDir());
  23. it('return a file object', () => assert.ok(fileFactory(mockFileOpts)));
  24. describe('Properties', function() {
  25. it('contains the name property', () => {
  26. assert.equal(fileFactory(mockFileOpts).name, mockFileName);
  27. });
  28. it('contains the data property', () => assert.ok(fileFactory(mockFileOpts).data));
  29. it('contains the encoding property', () => {
  30. assert.equal(fileFactory(mockFileOpts).encoding, 'utf-8');
  31. });
  32. it('contains the mimetype property', () => {
  33. assert.equal(fileFactory(mockFileOpts).mimetype, 'image/png');
  34. });
  35. it('contains the md5 property', () => assert.equal(fileFactory(mockFileOpts).md5, mockMd5));
  36. it('contains the mv method', () => assert.equal(isFunc(fileFactory(mockFileOpts).mv), true));
  37. });
  38. describe('File object behavior for in memory upload', function() {
  39. const file = fileFactory(mockFileOpts);
  40. it('move the file to the specified folder', (done) => {
  41. file.mv(path.join(server.uploadDir, mockFileName), (err) => {
  42. assert.ifError(err);
  43. done();
  44. });
  45. });
  46. it('reject the mv if the destination does not exists', (done) => {
  47. file.mv(path.join(server.uploadDir, 'unknown', mockFileName), (err) => {
  48. assert.ok(err);
  49. done();
  50. });
  51. });
  52. });
  53. describe('File object behavior for upload into temporary file', function() {
  54. const file = fileFactory(mockFileOpts, { useTempFiles: true });
  55. it('move the file to the specified folder', (done) => {
  56. file.mv(path.join(server.uploadDir, mockFileName), (err) => {
  57. assert.ifError(err);
  58. // Place back moved file.
  59. fs.renameSync(path.join(server.uploadDir, mockFileName), mockFile);
  60. done();
  61. });
  62. });
  63. it('reject the mv if the destination does not exists', (done) => {
  64. file.mv(path.join(server.uploadDir, 'unknown', mockFileName), (err) => {
  65. assert.ok(err);
  66. done();
  67. });
  68. });
  69. });
  70. });