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