Переглянути джерело

Add tests for view.js route

production
Jack Foltz 5 роки тому
джерело
коміт
289a2e0c16
Підписано: foltik <jack@foltz.io> Ідентифікатор GPG ключа: 303F88F996E95541
2 змінених файлів з 67 додано та 1 видалено
  1. +44
    -0
      test/api.js
  2. +23
    -1
      test/testUtil.js

+ 44
- 0
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) {


+ 23
- 1
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();

Завантаження…
Відмінити
Зберегти