forked from mozilla/send
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth-tests.js
105 lines (93 loc) · 2.82 KB
/
auth-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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const assert = require('assert');
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
const storage = {
metadata: sinon.stub(),
setField: sinon.stub()
};
function request(id, auth) {
return {
params: { id },
header: sinon.stub().returns(auth)
};
}
function response() {
return {
sendStatus: sinon.stub(),
set: sinon.stub()
};
}
const next = sinon.stub();
const storedMeta = {
auth:
'r9uFxEs9GEVaQR9CJJ0uTKFGhFSOTRjOY2FCLFlCIZ0Cr-VGTVpMGlXDbNR8RMT55trMpSrzWtBVKq1LffOT2g',
nonce: 'FL4oxA7IE1PW8shwFN9qZw=='
};
const authMiddleware = proxyquire('../../server/middleware/auth', {
'../storage': storage
}).hmac;
describe('Owner Middleware', function() {
afterEach(function() {
storage.metadata.reset();
storage.setField.reset();
next.reset();
});
it('sends a 401 when no auth header is set', async function() {
const req = request('x');
const res = response();
await authMiddleware(req, res, next);
sinon.assert.calledWith(res.sendStatus, 401);
sinon.assert.notCalled(next);
});
it('sends a 404 when metadata is not found', async function() {
const req = request('x', 'y');
const res = response();
await authMiddleware(req, res, next);
sinon.assert.calledWith(res.sendStatus, 404);
sinon.assert.notCalled(next);
});
it('sends a 401 when the auth header is invalid base64', async function() {
storage.metadata.returns(Promise.resolve(storedMeta));
const req = request('x', '1');
const res = response();
await authMiddleware(req, res, next);
sinon.assert.calledWith(res.sendStatus, 401);
sinon.assert.notCalled(next);
});
it('authenticates when the hashes match', async function() {
storage.metadata.returns(Promise.resolve(storedMeta));
const req = request(
'x',
'send-v1 R7nZk14qJqZXtxpnAtw2uDIRQTRnO1qSO1Q0PiwcNA8'
);
const res = response();
await authMiddleware(req, res, next);
sinon.assert.calledOnce(next);
sinon.assert.calledWith(storage.setField, 'x', 'nonce', req.nonce);
sinon.assert.calledWith(
res.set,
'WWW-Authenticate',
`send-v1 ${req.nonce}`
);
sinon.assert.notCalled(res.sendStatus);
assert.equal(req.authorized, true);
assert.equal(req.meta, storedMeta);
assert.notEqual(req.nonce, storedMeta.nonce);
});
it('sends a 401 when the hashes do not match', async function() {
storage.metadata.returns(Promise.resolve(storedMeta));
const req = request(
'x',
'send-v1 R8nZk14qJqZXtxpnAtw2uDIRQTRnO1qSO1Q0PiwcNA8'
);
const res = response();
await authMiddleware(req, res, next);
sinon.assert.calledWith(res.sendStatus, 401);
sinon.assert.calledWith(
res.set,
'WWW-Authenticate',
`send-v1 ${storedMeta.nonce}`
);
sinon.assert.notCalled(next);
});
});