Skip to content

Commit

Permalink
Add tests for fileupload options
Browse files Browse the repository at this point in the history
Refactor server.js to allow for different options to be provided during testing

Bumped the version number to 0.1.3
  • Loading branch information
pronein committed Apr 28, 2017
1 parent 26e009b commit 5bbf645
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 112 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Pass in non-Busboy options directly to the middleware. These are express-fileupl
Option | Acceptable Values | Details
--- | --- | ---
safeFileNames | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li><li>regex</li></ul> | Strips characters from the upload's filename. You can use custom regex to determine what to strip. If set to `true`, non-alphanumeric characters _except_ dashes and underscores will be stripped. This option is off by default.<br /><br />**Example #1 (strip slashes from file names):** `app.use(fileUpload({ safeFileNames: /\\/g }))`<br />**Example #2:** `app.use(fileUpload({ safeFileNames: true }))`
preserveExtension | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li><li><code>*Number*</code></li></ul> | Preserves filename extension when using <code>safeFileNames</code> option. If set to <code>true</code>, will default to an extension length of 3. If set to <code>*Number*</code>, this will be the max allowable extension length. If an extension is smaller than the extension length, it remains untouched. If the extension is longer, it is shifted.<br /><br />**Example #1 (true):**<br /><code>app.use(fileUpload({ safeFileNames: true, preserveExtension: true }));</code><br />*myFileName.ext* --> *myFileName.ext*<br /><br />**Example #2 (max extension length 2, extension truncated):**<br /><code>app.use(fileUpload({ safeFileNames: true, preserveExtension: 2 }));</code><br />*myFileName.ext* --> *myFileNamee.xt*
preserveExtension | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li><li><code>*Number*</code></li></ul> | Preserves filename extension when using <code>safeFileNames</code> option. If set to <code>true</code>, will default to an extension length of 3. If set to <code>*Number*</code>, this will be the max allowable extension length. If an extension is smaller than the extension length, it remains untouched. If the extension is longer, it is shifted.<br /><br />**Example #1 (true):**<br /><code>app.use(fileUpload({ safeFileNames: true, preserveExtension: true }));</code><br />*myFileName.ext* --> *myFileName.ext*<br /><br />**Example #2 (max extension length 2, extension shifted):**<br /><code>app.use(fileUpload({ safeFileNames: true, preserveExtension: 2 }));</code><br />*myFileName.ext* --> *myFileNamee.xt*

# Help Wanted
Pull Requests are welcomed!
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-fileupload",
"version": "0.1.2",
"version": "0.1.3",
"author": "Richard Girges <[email protected]>",
"description": "Simple express file upload middleware that wraps around Busboy",
"main": "./lib/index",
Expand Down
Binary file added test/files/basket.ball.bp
Binary file not shown.
Binary file added test/files/my$Invalid#fileName.png123
Binary file not shown.
2 changes: 1 addition & 1 deletion test/multipartFields.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const request = require('supertest');
const server = require('./server');
const app = server.app;
const app = server.setup();

let mockUser = {
firstName: 'Joe',
Expand Down
4 changes: 2 additions & 2 deletions test/multipartUploads.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs');
const path = require('path');
const request = require('supertest');
const server = require('./server');
const app = server.app;
const app = server.setup();
const clearUploadsDir = server.clearUploadsDir;
const fileDir = server.fileDir;
const uploadDir = server.uploadDir;
Expand Down Expand Up @@ -212,4 +212,4 @@ describe('Test Upload With Fields', function() {
});
});
}
});
});
151 changes: 151 additions & 0 deletions test/options.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
const fs = require('fs');
const path = require('path');
const request = require('supertest');
const server = require('./server');
// const clearUploadsDir = server.clearUploadsDir;
const fileDir = server.fileDir;
const uploadDir = server.uploadDir;

describe('SafeFileNames', function() {
it(`Does nothing to your filename when disabled.`, function(done) {
const app = server.setup({safeFileNames: false});

request(app)
.post('/upload/single')
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123'))
.expect(200)
.end(function(err, res) {
if (err)
return done(err);

let uploadedFilePath = path.join(uploadDir, 'my$Invalid#fileName.png123');

fs.stat(uploadedFilePath, done);
});
});

it(`Strips away all illegal characters (including spaces) when enabled.`, function(done) {
const app = server.setup({safeFileNames: true});

request(app)
.post('/upload/single')
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123'))
.expect(200)
.end(function(err, res) {
if (err)
return done(err);

let uploadedFilePath = path.join(uploadDir, 'myInvalidfileNamepng123');

fs.stat(uploadedFilePath, done);
});
});

it(`Respects a regex for stripping 'invalid' characters from filename.`, function(done) {
const app = server.setup({safeFileNames: /[\$#]/g});

request(app)
.post('/upload/single')
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123'))
.expect(200)
.end(function(err, res) {
if (err)
return done(err);

let uploadedFilePath = path.join(uploadDir, 'myInvalidfileName.png123');

fs.stat(uploadedFilePath, done);
});
});
});

describe(`preserveExtension`, function() {
it(`Does nothing to your filename when disabled.`, function(done) {
const app = server.setup({safeFileNames: true, preserveExtension: false});

request(app)
.post('/upload/single')
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123'))
.expect(200)
.end(function(err, res) {
if (err)
return done(err);

let uploadedFilePath = path.join(uploadDir, 'myInvalidfileNamepng123');

fs.stat(uploadedFilePath, done);
});
});

it(`Shortens your extension to the default(3) when enabled, if the extension found is larger`,
function(done) {
const app = server.setup({safeFileNames: true, preserveExtension: true});

request(app)
.post('/upload/single')
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123'))
.expect(200)
.end(function(err, res) {
if (err)
return done(err);

let uploadedFilePath = path.join(uploadDir, 'myInvalidfileNamepng.123');

fs.stat(uploadedFilePath, done);
});
});

it(`Leaves your extension alone when enabled, if the extension found is <= default(3) length`,
function(done) {
const app = server.setup({safeFileNames: true, preserveExtension: true});

request(app)
.post('/upload/single')
.attach('testFile', path.join(fileDir, 'car.png'))
.expect(200)
.end(function(err, res) {
if (err)
return done(err);

let uploadedFilePath = path.join(uploadDir, 'car.png');

fs.stat(uploadedFilePath, done);
});
});

it(`Leaves your extension alone when set to a number >= the extension length.`,
function(done) {
const app = server.setup({safeFileNames: true, preserveExtension: 7});

request(app)
.post('/upload/single')
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123'))
.expect(200)
.end(function(err, res) {
if (err)
return done(err);

let uploadedFilePath = path.join(uploadDir, 'myInvalidfileName.png123');

fs.stat(uploadedFilePath, done);
});
});

it(`Only considers the last dotted part the extension.`,
function(done) {
const app = server.setup({safeFileNames: true, preserveExtension: true});

request(app)
.post('/upload/single')
.attach('testFile', path.join(fileDir, 'basket.ball.bp'))
.expect(200)
.end(function(err, res) {
if (err)
return done(err);

let uploadedFilePath = path.join(uploadDir, 'basketball.bp');

fs.stat(uploadedFilePath, done);
});
});
});
Loading

0 comments on commit 5bbf645

Please sign in to comment.