options.spec.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. const fs = require('fs');
  2. const path = require('path');
  3. const request = require('supertest');
  4. const server = require('./server');
  5. const clearUploadsDir = server.clearUploadsDir;
  6. const fileDir = server.fileDir;
  7. const uploadDir = server.uploadDir;
  8. describe('File Upload Options Tests', function() {
  9. afterEach(function(done) {
  10. clearUploadsDir();
  11. done();
  12. });
  13. /**
  14. * Upload the file for testing and verify the expected filename.
  15. * @param {object} options The expressFileUpload options.
  16. * @param {string} actualFileNameToUpload The name of the file to upload.
  17. * @param {string} expectedFileNameOnFileSystem The name of the file after upload.
  18. * @param {function} done The mocha continuation function.
  19. */
  20. function executeFileUploadTestWalk(options,
  21. actualFileNameToUpload,
  22. expectedFileNameOnFileSystem,
  23. done) {
  24. request(server.setup(options))
  25. .post('/upload/single')
  26. .attach('testFile', path.join(fileDir, actualFileNameToUpload))
  27. .expect(200)
  28. .end(function(err) {
  29. if (err) {
  30. return done(err);
  31. }
  32. const uploadedFilePath = path.join(uploadDir, expectedFileNameOnFileSystem);
  33. fs.stat(uploadedFilePath, done);
  34. });
  35. }
  36. describe('Testing [safeFileNames] option to ensure:', function() {
  37. it('Does nothing to your filename when disabled.',
  38. function(done) {
  39. const fileUploadOptions = {safeFileNames: false};
  40. const actualFileName = 'my$Invalid#fileName.png123';
  41. const expectedFileName = 'my$Invalid#fileName.png123';
  42. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  43. });
  44. it('Is disabled by default.',
  45. function(done) {
  46. const fileUploadOptions = null;
  47. const actualFileName = 'my$Invalid#fileName.png123';
  48. const expectedFileName = 'my$Invalid#fileName.png123';
  49. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  50. });
  51. it('Strips away all non-alphanumeric characters (excluding hyphens/underscores) when enabled.',
  52. function(done) {
  53. const fileUploadOptions = {safeFileNames: true};
  54. const actualFileName = 'my$Invalid#fileName.png123';
  55. const expectedFileName = 'myInvalidfileNamepng123';
  56. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  57. });
  58. it('Accepts a regex for stripping (decidedly) "invalid" characters from filename.',
  59. function(done) {
  60. const fileUploadOptions = {safeFileNames: /[$#]/g};
  61. const actualFileName = 'my$Invalid#fileName.png123';
  62. const expectedFileName = 'myInvalidfileName.png123';
  63. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  64. });
  65. });
  66. describe('Testing [preserveExtension] option to ensure:', function() {
  67. it('Does not preserve the extension of your filename when disabled.',
  68. function(done) {
  69. const fileUploadOptions = {safeFileNames: true, preserveExtension: false};
  70. const actualFileName = 'my$Invalid#fileName.png123';
  71. const expectedFileName = 'myInvalidfileNamepng123';
  72. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  73. });
  74. it('Is disabled by default.',
  75. function(done) {
  76. const fileUploadOptions = {safeFileNames: true};
  77. const actualFileName = 'my$Invalid#fileName.png123';
  78. const expectedFileName = 'myInvalidfileNamepng123';
  79. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  80. });
  81. it('Shortens your extension to the default(3) when enabled, if the extension found is larger.',
  82. function(done) {
  83. const fileUploadOptions = {safeFileNames: true, preserveExtension: true};
  84. const actualFileName = 'my$Invalid#fileName.png123';
  85. const expectedFileName = 'myInvalidfileNamepng.123';
  86. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  87. });
  88. it('Leaves your extension alone when enabled, if the extension found is <= default(3) length',
  89. function(done) {
  90. const fileUploadOptions = {safeFileNames: true, preserveExtension: true};
  91. const actualFileName = 'car.png';
  92. const expectedFileName = 'car.png';
  93. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  94. });
  95. it('Can be configured for an extension length > default(3).',
  96. function(done) {
  97. const fileUploadOptions = {safeFileNames: true, preserveExtension: 7};
  98. const actualFileName = 'my$Invalid#fileName.png123';
  99. const expectedFileName = 'myInvalidfileName.png123';
  100. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  101. });
  102. it('Can be configured for an extension length < default(3).',
  103. function(done) {
  104. const fileUploadOptions = {safeFileNames: true, preserveExtension: 2};
  105. const actualFileName = 'my$Invalid#fileName.png123';
  106. const expectedFileName = 'myInvalidfileNamepng1.23';
  107. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  108. });
  109. it('Will use the absolute value of your extension length when negative.',
  110. function(done) {
  111. const fileUploadOptions = {safeFileNames: true, preserveExtension: -5};
  112. const actualFileName = 'my$Invalid#fileName.png123';
  113. const expectedFileName = 'myInvalidfileNamep.ng123';
  114. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  115. });
  116. it('Will leave no extension when the extension length == 0.',
  117. function(done) {
  118. const fileUploadOptions = {safeFileNames: true, preserveExtension: 0};
  119. const actualFileName = 'car.png';
  120. const expectedFileName = 'car';
  121. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  122. });
  123. it('Will accept numbers as strings, if they can be resolved with parseInt.',
  124. function(done) {
  125. const fileUploadOptions = {safeFileNames: true, preserveExtension: '3'};
  126. const actualFileName = 'my$Invalid#fileName.png123';
  127. const expectedFileName = 'myInvalidfileNamepng.123';
  128. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  129. });
  130. it('Will be evaluated for truthy-ness if it cannot be parsed as an int.',
  131. function(done) {
  132. const fileUploadOptions = {safeFileNames: true, preserveExtension: 'not-a-#-but-truthy'};
  133. const actualFileName = 'my$Invalid#fileName.png123';
  134. const expectedFileName = 'myInvalidfileNamepng.123';
  135. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  136. });
  137. it('Will ignore any decimal amount when evaluating for extension length.',
  138. function(done) {
  139. const fileUploadOptions = {safeFileNames: true, preserveExtension: 4.98};
  140. const actualFileName = 'my$Invalid#fileName.png123';
  141. const expectedFileName = 'myInvalidfileNamepn.g123';
  142. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  143. });
  144. it('Only considers the last dotted part as the extension.',
  145. function(done) {
  146. const fileUploadOptions = {safeFileNames: true, preserveExtension: true};
  147. const actualFileName = 'basket.ball.bp';
  148. const expectedFileName = 'basketball.bp';
  149. executeFileUploadTestWalk(fileUploadOptions, actualFileName, expectedFileName, done);
  150. });
  151. });
  152. describe('Testing [parseNested] option to ensure:', function() {
  153. it('When [parseNested] is enabled result are nested', function(done){
  154. const app = server.setup({parseNested: true});
  155. request(app)
  156. .post('/fields/nested')
  157. .field('name', 'John')
  158. .field('hobbies[0]', 'Cinema')
  159. .field('hobbies[1]', 'Bike')
  160. .expect('Content-Type', /json/)
  161. .expect(200, {
  162. name: 'John',
  163. hobbies: ['Cinema', 'Bike']
  164. }, done);
  165. });
  166. it('When [parseNested] is disabled are flattened', function(done){
  167. const app = server.setup({parseNested: false});
  168. request(app)
  169. .post('/fields/flattened')
  170. .field('name', 'John')
  171. .field('hobbies[0]', 'Cinema')
  172. .field('hobbies[1]', 'Bike')
  173. .expect('Content-Type', /json/)
  174. .expect(200, {
  175. name: 'John',
  176. 'hobbies[0]': 'Cinema',
  177. 'hobbies[1]': 'Bike'
  178. }, done);
  179. });
  180. });
  181. });