forked from mozilla/send
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathowner-tests.js
81 lines (71 loc) · 2.35 KB
/
owner-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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const assert = require('assert');
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
const storage = {
metadata: sinon.stub()
};
function request(id, owner_token) {
return {
params: { id },
body: { owner_token }
};
}
function response() {
return {
sendStatus: sinon.stub()
};
}
const ownerMiddleware = proxyquire('../../server/middleware/auth', {
'../storage': storage
}).owner;
describe('Owner Middleware', function() {
afterEach(function() {
storage.metadata.reset();
});
it('sends a 404 when the id is not found', async function() {
const next = sinon.stub();
storage.metadata.returns(Promise.resolve(null));
const res = response();
await ownerMiddleware(request('a', 'y'), res, next);
sinon.assert.notCalled(next);
sinon.assert.calledWith(res.sendStatus, 404);
});
it('sends a 401 when the owner_token is missing', async function() {
const next = sinon.stub();
const meta = { owner: 'y' };
storage.metadata.returns(Promise.resolve(meta));
const res = response();
await ownerMiddleware(request('b', null), res, next);
sinon.assert.notCalled(next);
sinon.assert.calledWith(res.sendStatus, 401);
});
it('sends a 401 when the owner_token does not match', async function() {
const next = sinon.stub();
const meta = { owner: 'y' };
storage.metadata.returns(Promise.resolve(meta));
const res = response();
await ownerMiddleware(request('c', 'z'), res, next);
sinon.assert.notCalled(next);
sinon.assert.calledWith(res.sendStatus, 401);
});
it('sends a 401 if the metadata call fails', async function() {
const next = sinon.stub();
storage.metadata.returns(Promise.reject(new Error()));
const res = response();
await ownerMiddleware(request('d', 'y'), res, next);
sinon.assert.notCalled(next);
sinon.assert.calledWith(res.sendStatus, 401);
});
it('sets req.meta and req.authorized on successful auth', async function() {
const next = sinon.stub();
const meta = { owner: 'y' };
storage.metadata.returns(Promise.resolve(meta));
const req = request('e', 'y');
const res = response();
await ownerMiddleware(req, res, next);
assert.equal(req.meta, meta);
assert.equal(req.authorized, true);
sinon.assert.notCalled(res.sendStatus);
sinon.assert.calledOnce(next);
});
});