mirror of
https://github.com/Foltik/Shimapan
synced 2025-01-05 15:58:03 -05:00
Add tests for view.js route
This commit is contained in:
parent
a395485f09
commit
289a2e0c16
44
test/api.js
44
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) {
|
||||
|
@ -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();
|
Loading…
Reference in New Issue
Block a user