1
0
mirror of https://github.com/Foltik/Shimapan synced 2025-01-07 08:42:49 -05:00

Fix extraneous upload logic and test

This commit is contained in:
Jack Foltz 2018-08-14 08:30:46 -04:00
parent 323a045465
commit 93b62ee703
Signed by: foltik
GPG Key ID: 303F88F996E95541
2 changed files with 17 additions and 8 deletions

View File

@ -58,13 +58,12 @@ const uploadMultipart = wrap(async (req, res, next) => {
req.body[fieldName] = value; req.body[fieldName] = value;
}); });
let fileCount = 0; let fileReceived = false;
let file;
busboy.on('file', async (fieldName, stream, name, encoding, mime) => { busboy.on('file', async (fieldName, stream, name, encoding, mime) => {
// Only process one file // Only process one file, discard everything after that
fileCount++; if (fileReceived)
if (fileCount > 1) return req.unpipe(busboy);
return res.status(400).json({message: 'Bad request.'}); fileReceived = true;
// If a key was encountered and we are not authenticated, try to authenticate with it before the final check // If a key was encountered and we are not authenticated, try to authenticate with it before the final check
if (req.body.key && !authStatus.authenticated) if (req.body.key && !authStatus.authenticated)

View File

@ -411,18 +411,28 @@ describe('Uploading', () => {
return util.logout(agent); return util.logout(agent);
}); });
it('SHOULD NOT accept a request with multiple files attached', async () => { it('must only accept one file from a request with multiple files attached', async () => {
await Promise.all([ await Promise.all([
util.createTestFile(2048, 'test1.bin'), util.createTestFile(2048, 'test1.bin'),
util.createTestFile(2048, 'test2.bin'), util.createTestFile(2048, 'test2.bin'),
util.createTestSession(agent) util.createTestSession(agent)
]); ]);
const fileCountBefore = await util.directoryFileCount(config.get('Upload.path'));
const uploadCountBefore = await Upload.countDocuments({});
const res = await agent.post('/api/upload') const res = await agent.post('/api/upload')
.attach('file', 'test1.bin', 'test1.bin') .attach('file', 'test1.bin', 'test1.bin')
.attach('file1', 'test2.bin', 'test2.bin'); .attach('file1', 'test2.bin', 'test2.bin');
util.verifyResponse(res, 400, 'Bad request.'); util.verifyResponse(res, 200, 'File uploaded.');
const fileCountAfter = await util.directoryFileCount(config.get('Upload.path'));
fileCountAfter.should.equal(fileCountBefore + 1, 'Only one file should be written to the disk');
const uploadCountAfter = await Upload.countDocuments({});
uploadCountAfter.should.equal(uploadCountBefore + 1, 'Only one upload should be written to the database');
return Promise.all([ return Promise.all([
util.deleteFile('test1.bin'), util.deleteFile('test1.bin'),