You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.7 KiB

  1. const fs = require('fs');
  2. const md5 = require('md5');
  3. const path = require('path');
  4. const request = require('supertest');
  5. const server = require('./server');
  6. const clearUploadsDir =
  7. server.clearUploadsDir;
  8. const fileDir =
  9. server.fileDir;
  10. const uploadDir =
  11. server.uploadDir;
  12. describe('File Upload Options Tests', function() {
  13. afterEach(function(done) {
  14. clearUploadsDir();
  15. done();
  16. });
  17. /**
  18. * Upload the file for testing and verify the expected filename.
  19. * @param {object} options The expressFileUpload options.
  20. * @param {string} actualFileNameToUpload The name of the file to upload.
  21. * @param {string} expectedFileNameOnFileSystem The name of the file after upload.
  22. * @param {function} done The mocha continuation function.
  23. */
  24. function executeFileUploadTestWalk(
  25. options,
  26. actualFileNameToUpload,
  27. expectedFileNameOnFileSystem,
  28. done
  29. ) {
  30. let filePath = path.join(fileDir, actualFileNameToUpload);
  31. let fileBuffer = fs.readFileSync(filePath);
  32. let fileHash = md5(fileBuffer);
  33. let fileStat = fs.statSync(filePath);
  34. let uploadedFilePath = path.join(uploadDir, expectedFileNameOnFileSystem);
  35. request(
  36. server.setup(options)
  37. )
  38. .post('/upload/single')
  39. .attach('testFile', filePath)
  40. .expect((res)=>{
  41. res.body.uploadDir = '';
  42. res.body.uploadPath = '';
  43. })
  44. .expect(200, {
  45. name: expectedFileNameOnFileSystem,
  46. md5: fileHash,
  47. size: fileStat.size,
  48. uploadDir: '',
  49. uploadPath: ''
  50. })
  51. .end(function(err) {
  52. if (err) {
  53. return done(err);
  54. }
  55. fs.stat(uploadedFilePath, done);
  56. });
  57. }
  58. describe('Testing [safeFileNames with useTempFiles] option to ensure:', function() {
  59. it('Does nothing to your filename when disabled.', function(done) {
  60. const fileUploadOptions = {
  61. safeFileNames: false,
  62. useTempFiles: true,
  63. tempFileDir: '/tmp/'
  64. };
  65. const actualFileName =
  66. 'my$Invalid#fileName.png123';
  67. const expectedFileName =
  68. 'my$Invalid#fileName.png123';
  69. executeFileUploadTestWalk(
  70. fileUploadOptions,
  71. actualFileName,
  72. expectedFileName,
  73. done
  74. );
  75. });
  76. it('Is disabled by default.', function(done) {
  77. const fileUploadOptions = {
  78. useTempFiles: true,
  79. tempFileDir: '/tmp/'
  80. };
  81. const actualFileName =
  82. 'my$Invalid#fileName.png123';
  83. const expectedFileName =
  84. 'my$Invalid#fileName.png123';
  85. executeFileUploadTestWalk(
  86. fileUploadOptions,
  87. actualFileName,
  88. expectedFileName,
  89. done
  90. );
  91. });
  92. it(
  93. 'Strips away all non-alphanumeric characters (excluding hyphens/underscores) when enabled.',
  94. function(done) {
  95. const fileUploadOptions = {
  96. safeFileNames: true,
  97. useTempFiles: true,
  98. tempFileDir: '/tmp/'
  99. };
  100. const actualFileName = 'my$Invalid#fileName.png123';
  101. const expectedFileName = 'myInvalidfileNamepng123';
  102. executeFileUploadTestWalk(
  103. fileUploadOptions,
  104. actualFileName,
  105. expectedFileName,
  106. done
  107. );
  108. });
  109. it(
  110. 'Accepts a regex for stripping (decidedly) "invalid" characters from filename.',
  111. function(done) {
  112. const fileUploadOptions = {
  113. safeFileNames: /[$#]/g,
  114. useTempFiles: true,
  115. tempFileDir: '/tmp/'
  116. };
  117. const actualFileName = 'my$Invalid#fileName.png123';
  118. const expectedFileName = 'myInvalidfileName.png123';
  119. executeFileUploadTestWalk(
  120. fileUploadOptions,
  121. actualFileName,
  122. expectedFileName,
  123. done
  124. );
  125. });
  126. });
  127. });