forked from mozilla/send
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata-tests.js
62 lines (54 loc) · 1.35 KB
/
metadata-tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
const storage = {
ttl: sinon.stub(),
length: sinon.stub()
};
function request(id, meta = {}) {
return {
params: { id },
meta
};
}
function response() {
return {
sendStatus: sinon.stub(),
send: sinon.stub()
};
}
const metadataRoute = proxyquire('../../server/routes/metadata', {
'../storage': storage
});
describe('/api/metadata', function() {
afterEach(function() {
storage.ttl.reset();
storage.length.reset();
});
it('calls storage.ttl with the id parameter', async function() {
const req = request('x');
const res = response();
await metadataRoute(req, res);
sinon.assert.calledWith(storage.ttl, 'x');
});
it('sends a 404 on failure', async function() {
storage.ttl.returns(Promise.reject(new Error()));
const res = response();
await metadataRoute(request('x'), res);
sinon.assert.calledWith(res.sendStatus, 404);
});
it('returns a json object', async function() {
storage.ttl.returns(Promise.resolve(123));
const meta = {
dlimit: 1,
dlToken: 0,
metadata: 'foo'
};
const res = response();
await metadataRoute(request('x', meta), res);
sinon.assert.calledWithMatch(res.send, {
metadata: 'foo',
finalDownload: true,
ttl: 123
});
});
});