From 289a2e0c16fc4e257c5657020716069637869f2f Mon Sep 17 00:00:00 2001 From: Jack Foltz Date: Wed, 1 Aug 2018 20:25:27 -0400 Subject: [PATCH] Add tests for view.js route --- test/api.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ test/testUtil.js | 24 +++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/test/api.js b/test/api.js index e3530da..c2f8aeb 100644 --- a/test/api.js +++ b/test/api.js @@ -359,6 +359,50 @@ describe('Uploading', () => { }); }); +describe('Viewing', () => { + async function verifyView(file, id, disposition) { + const viewsBefore = (await Upload.findOne({id: id})).views; + + const res = await util.view(id, agent) + .parse(util.binaryFileParser); + + res.should.have.status(200); + res.should.have.header('content-disposition', disposition); + + const [uploadHash, downloadHash] = await Promise.all([ + util.fileHash(file), + util.bufferHash(res.body) + ]); + downloadHash.should.equal(uploadHash, 'Uploaded file and downloaded hash should match'); + + const viewsAfter = (await Upload.findOne({id: id})).views; + viewsAfter.should.equal(viewsBefore + 1, 'The files views should be incremented.'); + } + + it('must return an uploaded binary file', async () => { + await Promise.all([ + util.createTestSession(agent), + util.createTestFile(2048, 'test.bin') + ]); + const upload = await util.upload('test.bin', agent); + return verifyView('test.bin', upload.body.id, 'attachment; filename="test.bin"'); + }); + + it('must return an uploaded image file inline', async () => { + await Promise.all([ + util.createTestSession(agent), + util.createTestFile(2048, 'test.jpg') + ]); + const upload = await util.upload('test.jpg', agent); + return verifyView('test.jpg', upload.body.id, 'inline'); + }); + + it('must return an error when file not found', async () => { + const res = await util.view('abcdef', agent); + util.verifyResponse(res, 404, 'File not found.'); + }); +}); + describe('Invites', () => { describe('/POST create', () => { async function verifyCreatedInvite(invite) { diff --git a/test/testUtil.js b/test/testUtil.js index e201cb9..aa2f0a0 100644 --- a/test/testUtil.js +++ b/test/testUtil.js @@ -115,6 +115,11 @@ exports.fileHash = file => .on('end', () => resolve(hash.digest('hex'))); }); +exports.bufferHash = buffer => + crypto.createHash('MD5') + .update(buffer) + .digest('hex'); + exports.directoryFileCount = async dir => (await fsPromises.readdir(dir)).length; @@ -149,7 +154,7 @@ exports.getInvites = (query, agent) => agent.get('/api/invites/get') .send(query); -//---------------- Invites ----------------// +//---------------- Keys ----------------// exports.createKey = (key, agent) => agent.post('/api/keys/create') @@ -162,3 +167,20 @@ exports.deleteKey = (key, agent) => exports.getKeys = (query, agent) => agent.get('/api/keys/get') .send(query); + +//---------------- Viewing ----------------// + +exports.binaryFileParser = (res, cb) => { + res.setEncoding('binary'); + res.data = ''; + res.on("data", function (chunk) { + res.data += chunk; + }); + res.on('end', function () { + cb(null, Buffer.from(res.data, 'binary')); + }); +}; + +exports.view = (id, agent) => + agent.get('/v/' + id) + .buffer(); \ No newline at end of file