This repository was archived by the owner on Jan 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathheader-auth-middleware.sinon-test.js
More file actions
55 lines (46 loc) · 1.54 KB
/
header-auth-middleware.sinon-test.js
File metadata and controls
55 lines (46 loc) · 1.54 KB
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
const test = require('ava');
const sinon = require('sinon');
const mockRequest = (authHeader, sessionData) => ({
get(name) {
if (name === 'authorization') return authHeader
return null
},
session: { data: sessionData }
});
const mockResponse = () => {
const res = {};
res.status = sinon.stub().returns(res);
res.json = sinon.stub().returns(res);
return res;
};
const headerAuthMiddleware = require('./header-auth-middleware');
test('should set req.session.data if API key is in authorization and is valid', async (t) => {
const req = mockRequest('76b1e728-1c14-43f9-aa06-6de5cbc064c2');
const res = mockResponse();
await headerAuthMiddleware(req, res, () => {});
t.deepEqual(
req.session.data,
{ username: 'hugo' }
);
});
test('should not do anything if req.session.data is already set', async (t) => {
const req = mockRequest('76b1e728-1c14-43f9-aa06-6de5cbc064c2', { username: 'guest' });
const res = mockResponse();
await headerAuthMiddleware(req, res, () => {});
t.deepEqual(
req.session.data,
{ username: 'guest' }
);
});
test('should not do anything if authorization header is not present', async (t) => {
const req = mockRequest(undefined);
const res = mockResponse();
await headerAuthMiddleware(req, res, () => {});
t.is(req.session.data, undefined);
});
test('should not do anything if api key is invalid', async (t) => {
const req = mockRequest('invalid-api-key');
const res = mockResponse();
await headerAuthMiddleware(req, res, () => {});
t.is(req.session.data, undefined);
});