Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit bd456db

Browse files
committed
add tests for a middleware
1 parent 4be8673 commit bd456db

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const mockRequest = (authHeader, sessionData) => ({
2+
get(name) {
3+
if (name === 'authorization') return authHeader
4+
return null
5+
},
6+
session: { data: sessionData }
7+
});
8+
9+
const mockResponse = () => {
10+
const res = {};
11+
res.status = jest.fn().mockReturnValue(res);
12+
res.json = jest.fn().mockReturnValue(res);
13+
return res;
14+
};
15+
16+
const headerAuthMiddleware = require('./header-auth-middleware');
17+
18+
describe('headerAuthMiddleware', () => {
19+
test('should set req.session.data if API key is in authorization and is valid', async () => {
20+
const req = mockRequest('76b1e728-1c14-43f9-aa06-6de5cbc064c2');
21+
const res = mockResponse();
22+
await headerAuthMiddleware(req, res, () => {});
23+
24+
expect(req.session.data).toEqual({ username: 'hugo' });
25+
});
26+
test('should not do anything if req.session.data is already set', async () => {
27+
const req = mockRequest('76b1e728-1c14-43f9-aa06-6de5cbc064c2', { username: 'guest' });
28+
const res = mockResponse();
29+
await headerAuthMiddleware(req, res, () => {});
30+
31+
expect(req.session.data).toEqual({ username: 'guest' });
32+
});
33+
test('should not do anything if authorization header is not present', async () => {
34+
const req = mockRequest(undefined);
35+
const res = mockResponse();
36+
await headerAuthMiddleware(req, res, () => {});
37+
38+
expect(req.session.data).toBeUndefined();
39+
});
40+
test('should not do anything if api key is invalid', async () => {
41+
const req = mockRequest('invalid-api-key');
42+
const res = mockResponse();
43+
await headerAuthMiddleware(req, res, () => {});
44+
45+
expect(req.session.data).toBeUndefined();
46+
});
47+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const test = require('ava');
2+
const sinon = require('sinon');
3+
4+
const mockRequest = (authHeader, sessionData) => ({
5+
get(name) {
6+
if (name === 'authorization') return authHeader
7+
return null
8+
},
9+
session: { data: sessionData }
10+
});
11+
12+
const mockResponse = () => {
13+
const res = {};
14+
res.status = sinon.stub().returns(res);
15+
res.json = sinon.stub().returns(res);
16+
return res;
17+
};
18+
19+
const headerAuthMiddleware = require('./header-auth-middleware');
20+
21+
22+
test('should set req.session.data if API key is in authorization and is valid', async (t) => {
23+
const req = mockRequest('76b1e728-1c14-43f9-aa06-6de5cbc064c2');
24+
const res = mockResponse();
25+
await headerAuthMiddleware(req, res, () => {});
26+
27+
t.deepEqual(
28+
req.session.data,
29+
{ username: 'hugo' }
30+
);
31+
});
32+
test('should not do anything if req.session.data is already set', async (t) => {
33+
const req = mockRequest('76b1e728-1c14-43f9-aa06-6de5cbc064c2', { username: 'guest' });
34+
const res = mockResponse();
35+
await headerAuthMiddleware(req, res, () => {});
36+
37+
t.deepEqual(
38+
req.session.data,
39+
{ username: 'guest' }
40+
);
41+
});
42+
test('should not do anything if authorization header is not present', async (t) => {
43+
const req = mockRequest(undefined);
44+
const res = mockResponse();
45+
await headerAuthMiddleware(req, res, () => {});
46+
47+
t.is(req.session.data, undefined);
48+
});
49+
test('should not do anything if api key is invalid', async (t) => {
50+
const req = mockRequest('invalid-api-key');
51+
const res = mockResponse();
52+
await headerAuthMiddleware(req, res, () => {});
53+
54+
t.is(req.session.data, undefined);
55+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"ava": {
2626
"files": [
27-
"./express-handlers.sinon-test.js"
27+
"./*.sinon-test.js"
2828
]
2929
},
3030
"jest": {

0 commit comments

Comments
 (0)