From f02a4f05f47dd1c3558fe047fc6d6a75fb3f3619 Mon Sep 17 00:00:00 2001 From: Jack Foltz Date: Sun, 29 Jul 2018 20:09:09 -0400 Subject: [PATCH] Update tests for upload rewrite --- test/api.js | 40 ++++++++++++++++++++++++++++------------ test/testUtil.js | 7 +++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/test/api.js b/test/api.js index 8305389..278a1ad 100644 --- a/test/api.js +++ b/test/api.js @@ -245,6 +245,8 @@ describe('Authentication', function() { }); describe('Uploading', () => { + after(async () => util.clearDirectory(config.get('Upload.path'))); + describe('/POST upload', () => { async function verifySuccessfulUpload(file, key) { // Get file stats beforehand @@ -340,7 +342,7 @@ describe('Uploading', () => { it('SHOULD NOT accept an unauthenticated request', async () => { await util.createTestFile(2048, 'test.bin'); - await verifyFailedUpload(null, 401, 'Unauthorized.'); + await verifyFailedUpload('test.bin', 401, 'Unauthorized.'); return util.deleteFile('test.bin'); }); @@ -373,28 +375,42 @@ describe('Uploading', () => { }); describe('3 Invalid File', () => { + before(() => util.createTestFile(config.get('Upload.maxSize') + 1024, 'large.bin')); + after(() => util.deleteFile('large.bin')); + it('SHOULD NOT accept a too large file', async () => { - await Promise.all([ - util.createTestSession(agent), - util.createTestFile(config.get('Upload.maxSize') + 1, 'large.bin') - ]); - + await util.createTestSession(agent); await verifyFailedUpload('large.bin', 413, 'File too large.'); - - return Promise.all([ - util.logout(agent), - util.deleteFile('large.bin') - ]); + return util.logout(agent); }); }); describe('4 Malformed Request', () => { it('SHOULD NOT accept a request with no file attached', async () => { await util.createTestSession(agent); - await verifyFailedUpload(null, 400, 'No file specified.'); + await verifyFailedUpload(null, 400, 'Bad request.'); return util.logout(agent); }); + + it('SHOULD NOT accept a request with multiple files attached', async () => { + await Promise.all([ + util.createTestFile(2048, 'test1.bin'), + util.createTestFile(2048, 'test2.bin'), + util.createTestSession(agent) + ]); + + const res = await agent.post('/api/upload') + .attach('file', 'test1.bin', 'test1.bin') + .attach('file1', 'test2.bin', 'test2.bin'); + + util.verifyResponse(res, 400, 'Bad request.'); + + return Promise.all([ + util.deleteFile('test1.bin'), + util.deleteFile('test2.bin') + ]); + }) }) }); }); diff --git a/test/testUtil.js b/test/testUtil.js index 42bd778..726e366 100644 --- a/test/testUtil.js +++ b/test/testUtil.js @@ -13,6 +13,7 @@ const Buffer = require('buffer').Buffer; const crypto = require('crypto'); const fs = require('fs'); const fsPromises = fs.promises; +const path = require('path'); //---------------- RESPONSE VERIFICATION ----------------// @@ -117,6 +118,12 @@ exports.fileHash = file => exports.directoryFileCount = async dir => (await fsPromises.readdir(dir)).length; +exports.clearDirectory = async dir => { + const files = await fsPromises.readdir(dir); + const promises = files.map(file => fsPromises.unlink(path.join(dir, file))); + return Promise.all(promises); +}; + //---------------- UPLOADS ----------------// exports.upload = (file, agent, key) => {