fileFactory.spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. const assert = require('assert');
  3. const fs = require('fs');
  4. const md5 = require('md5');
  5. const path = require('path');
  6. const fileFactory = require('../lib').fileFactory;
  7. const {isFunc} = require('../lib/utilities.js');
  8. const server = require('./server');
  9. const mockFile = path.join(server.fileDir, 'basketball.png');
  10. const mockBuffer = fs.readFileSync(mockFile);
  11. describe('Test of the fileFactory factory', function() {
  12. beforeEach(function() {
  13. server.clearUploadsDir();
  14. });
  15. it('return a file object', function() {
  16. assert.ok(fileFactory({
  17. name: 'basketball.png',
  18. buffer: mockBuffer
  19. }));
  20. });
  21. it('return void if buffer is empty and useTempFiles is false.', function() {
  22. assert.equal(fileFactory({
  23. name: 'basketball.png',
  24. buffer: Buffer.concat([])
  25. },{
  26. useTempFiles: false
  27. }), null);
  28. });
  29. describe('Properties', function() {
  30. it('contains the name property', function() {
  31. assert.equal(fileFactory({
  32. name: 'basketball.png',
  33. buffer: mockBuffer
  34. }).name, 'basketball.png');
  35. });
  36. it('contains the data property', function() {
  37. assert.ok(fileFactory({
  38. name: 'basketball.png',
  39. buffer: mockBuffer
  40. }).data);
  41. });
  42. it('contains the encoding property', function() {
  43. assert.equal(fileFactory({
  44. name: 'basketball.png',
  45. buffer: mockBuffer,
  46. encoding: 'utf-8'
  47. }).encoding, 'utf-8');
  48. });
  49. it('contains the mimetype property', function() {
  50. assert.equal(fileFactory({
  51. name: 'basketball.png',
  52. buffer: mockBuffer,
  53. mimetype: 'image/png'
  54. }).mimetype, 'image/png');
  55. });
  56. it('contains the md5 property', function() {
  57. const mockMd5 = md5(mockBuffer);
  58. assert.equal(fileFactory({
  59. name: 'basketball.png',
  60. buffer: mockBuffer,
  61. hash: mockMd5
  62. }).md5, mockMd5);
  63. });
  64. it('contains the mv method', function() {
  65. assert.equal(isFunc(fileFactory({
  66. name: 'basketball.png',
  67. buffer: mockBuffer
  68. }).mv), true);
  69. });
  70. });
  71. describe('File object behavior for in memory upload', function() {
  72. const file = fileFactory({
  73. name: 'basketball.png',
  74. buffer: mockBuffer
  75. });
  76. it('move the file to the specified folder', function(done) {
  77. file.mv(path.join(server.uploadDir, 'basketball.png'), function(err) {
  78. assert.ifError(err);
  79. done();
  80. });
  81. });
  82. it('reject the mv if the destination does not exists', function(done) {
  83. file.mv(path.join(server.uploadDir, 'unknown', 'basketball.png'), function(err) {
  84. assert.ok(err);
  85. done();
  86. });
  87. });
  88. });
  89. describe('File object behavior for upload into temporary file', function() {
  90. const file = fileFactory({
  91. name: 'basketball.png',
  92. buffer: mockBuffer,
  93. tempFilePath: mockFile
  94. });
  95. it('move the file to the specified folder', function(done) {
  96. file.mv(path.join(server.uploadDir, 'basketball.png'), function(err) {
  97. if (!err){
  98. //Place back moved file
  99. fs.renameSync(path.join(server.uploadDir, 'basketball.png'), mockFile);
  100. }
  101. assert.ifError(err);
  102. done();
  103. });
  104. });
  105. it('reject the mv if the destination does not exists', function(done) {
  106. file.mv(path.join(server.uploadDir, 'unknown', 'basketball.png'), function(err) {
  107. assert.ok(err);
  108. done();
  109. });
  110. });
  111. });
  112. });