1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-14 08:49:51 -05:00

Update tests for upload rewrite

This commit is contained in:
Jack Foltz 2018-07-29 20:09:09 -04:00
parent 50f37fd1a2
commit f02a4f05f4
Signed by: foltik
GPG Key ID: 303F88F996E95541
2 changed files with 35 additions and 12 deletions

View File

@ -245,6 +245,8 @@ describe('Authentication', function() {
}); });
describe('Uploading', () => { describe('Uploading', () => {
after(async () => util.clearDirectory(config.get('Upload.path')));
describe('/POST upload', () => { describe('/POST upload', () => {
async function verifySuccessfulUpload(file, key) { async function verifySuccessfulUpload(file, key) {
// Get file stats beforehand // Get file stats beforehand
@ -340,7 +342,7 @@ describe('Uploading', () => {
it('SHOULD NOT accept an unauthenticated request', async () => { it('SHOULD NOT accept an unauthenticated request', async () => {
await util.createTestFile(2048, 'test.bin'); await util.createTestFile(2048, 'test.bin');
await verifyFailedUpload(null, 401, 'Unauthorized.'); await verifyFailedUpload('test.bin', 401, 'Unauthorized.');
return util.deleteFile('test.bin'); return util.deleteFile('test.bin');
}); });
@ -373,28 +375,42 @@ describe('Uploading', () => {
}); });
describe('3 Invalid File', () => { 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 () => { it('SHOULD NOT accept a too large file', async () => {
await Promise.all([ await util.createTestSession(agent);
util.createTestSession(agent),
util.createTestFile(config.get('Upload.maxSize') + 1, 'large.bin')
]);
await verifyFailedUpload('large.bin', 413, 'File too large.'); await verifyFailedUpload('large.bin', 413, 'File too large.');
return util.logout(agent);
return Promise.all([
util.logout(agent),
util.deleteFile('large.bin')
]);
}); });
}); });
describe('4 Malformed Request', () => { describe('4 Malformed Request', () => {
it('SHOULD NOT accept a request with no file attached', async () => { it('SHOULD NOT accept a request with no file attached', async () => {
await util.createTestSession(agent); await util.createTestSession(agent);
await verifyFailedUpload(null, 400, 'No file specified.'); await verifyFailedUpload(null, 400, 'Bad request.');
return util.logout(agent); 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')
]);
})
}) })
}); });
}); });

View File

@ -13,6 +13,7 @@ const Buffer = require('buffer').Buffer;
const crypto = require('crypto'); const crypto = require('crypto');
const fs = require('fs'); const fs = require('fs');
const fsPromises = fs.promises; const fsPromises = fs.promises;
const path = require('path');
//---------------- RESPONSE VERIFICATION ----------------// //---------------- RESPONSE VERIFICATION ----------------//
@ -117,6 +118,12 @@ exports.fileHash = file =>
exports.directoryFileCount = async dir => exports.directoryFileCount = async dir =>
(await fsPromises.readdir(dir)).length; (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 ----------------// //---------------- UPLOADS ----------------//
exports.upload = (file, agent, key) => { exports.upload = (file, agent, key) => {