|
| 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('SafeFileNames', function() { |
| 10 | + it(`Does nothing to your filename when disabled.`, function(done) { |
| 11 | + const app = server.setup({safeFileNames: false}); |
| 12 | + |
| 13 | + request(app) |
| 14 | + .post('/upload/single') |
| 15 | + .attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) |
| 16 | + .expect(200) |
| 17 | + .end(function(err, res) { |
| 18 | + if (err) |
| 19 | + return done(err); |
| 20 | + |
| 21 | + let uploadedFilePath = path.join(uploadDir, 'my$Invalid#fileName.png123'); |
| 22 | + |
| 23 | + fs.stat(uploadedFilePath, done); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it(`Strips away all illegal characters (including spaces) when enabled.`, function(done) { |
| 28 | + const app = server.setup({safeFileNames: true}); |
| 29 | + |
| 30 | + request(app) |
| 31 | + .post('/upload/single') |
| 32 | + .attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) |
| 33 | + .expect(200) |
| 34 | + .end(function(err, res) { |
| 35 | + if (err) |
| 36 | + return done(err); |
| 37 | + |
| 38 | + let uploadedFilePath = path.join(uploadDir, 'myInvalidfileNamepng123'); |
| 39 | + |
| 40 | + fs.stat(uploadedFilePath, done); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it(`Respects a regex for stripping 'invalid' characters from filename.`, function(done) { |
| 45 | + const app = server.setup({safeFileNames: /[\$#]/g}); |
| 46 | + |
| 47 | + request(app) |
| 48 | + .post('/upload/single') |
| 49 | + .attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) |
| 50 | + .expect(200) |
| 51 | + .end(function(err, res) { |
| 52 | + if (err) |
| 53 | + return done(err); |
| 54 | + |
| 55 | + let uploadedFilePath = path.join(uploadDir, 'myInvalidfileName.png123'); |
| 56 | + |
| 57 | + fs.stat(uploadedFilePath, done); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe(`preserveExtension`, function() { |
| 63 | + it(`Does nothing to your filename when disabled.`, function(done) { |
| 64 | + const app = server.setup({safeFileNames: true, preserveExtension: false}); |
| 65 | + |
| 66 | + request(app) |
| 67 | + .post('/upload/single') |
| 68 | + .attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) |
| 69 | + .expect(200) |
| 70 | + .end(function(err, res) { |
| 71 | + if (err) |
| 72 | + return done(err); |
| 73 | + |
| 74 | + let uploadedFilePath = path.join(uploadDir, 'myInvalidfileNamepng123'); |
| 75 | + |
| 76 | + fs.stat(uploadedFilePath, done); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it(`Shortens your extension to the default(3) when enabled, if the extension found is larger`, |
| 81 | + function(done) { |
| 82 | + const app = server.setup({safeFileNames: true, preserveExtension: true}); |
| 83 | + |
| 84 | + request(app) |
| 85 | + .post('/upload/single') |
| 86 | + .attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) |
| 87 | + .expect(200) |
| 88 | + .end(function(err, res) { |
| 89 | + if (err) |
| 90 | + return done(err); |
| 91 | + |
| 92 | + let uploadedFilePath = path.join(uploadDir, 'myInvalidfileNamepng.123'); |
| 93 | + |
| 94 | + fs.stat(uploadedFilePath, done); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it(`Leaves your extension alone when enabled, if the extension found is <= default(3) length`, |
| 99 | + function(done) { |
| 100 | + const app = server.setup({safeFileNames: true, preserveExtension: true}); |
| 101 | + |
| 102 | + request(app) |
| 103 | + .post('/upload/single') |
| 104 | + .attach('testFile', path.join(fileDir, 'car.png')) |
| 105 | + .expect(200) |
| 106 | + .end(function(err, res) { |
| 107 | + if (err) |
| 108 | + return done(err); |
| 109 | + |
| 110 | + let uploadedFilePath = path.join(uploadDir, 'car.png'); |
| 111 | + |
| 112 | + fs.stat(uploadedFilePath, done); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it(`Leaves your extension alone when set to a number >= the extension length.`, |
| 117 | + function(done) { |
| 118 | + const app = server.setup({safeFileNames: true, preserveExtension: 7}); |
| 119 | + |
| 120 | + request(app) |
| 121 | + .post('/upload/single') |
| 122 | + .attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) |
| 123 | + .expect(200) |
| 124 | + .end(function(err, res) { |
| 125 | + if (err) |
| 126 | + return done(err); |
| 127 | + |
| 128 | + let uploadedFilePath = path.join(uploadDir, 'myInvalidfileName.png123'); |
| 129 | + |
| 130 | + fs.stat(uploadedFilePath, done); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it(`Only considers the last dotted part the extension.`, |
| 135 | + function(done) { |
| 136 | + const app = server.setup({safeFileNames: true, preserveExtension: true}); |
| 137 | + |
| 138 | + request(app) |
| 139 | + .post('/upload/single') |
| 140 | + .attach('testFile', path.join(fileDir, 'basket.ball.bp')) |
| 141 | + .expect(200) |
| 142 | + .end(function(err, res) { |
| 143 | + if (err) |
| 144 | + return done(err); |
| 145 | + |
| 146 | + let uploadedFilePath = path.join(uploadDir, 'basketball.bp'); |
| 147 | + |
| 148 | + fs.stat(uploadedFilePath, done); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments