This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
idamUserDetails.test.js
110 lines (86 loc) · 2.86 KB
/
idamUserDetails.test.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
106
107
108
109
110
const sinon = require('sinon');
const { expect } = require('chai');
const config = require('../config');
const idamWrapper = require('../wrapper');
const middleware = require('./idamUserDetails');
const sinonStubPromise = require('sinon-stub-promise');
sinonStubPromise(sinon);
let req = null;
let res = null;
let next = null;
const idamArgs = {};
const userDetails = {
id: 'idam.user.id',
email: '[email protected]'
};
describe('idamUserDetails', () => {
it('should return a middleware handler', () => {
const handler = middleware();
expect(handler).to.be.a('function');
});
describe('middleware', () => {
beforeEach(() => {
req = {
cookies: {},
query: []
};
res = {
redirect: sinon.stub(),
cookie: sinon.stub(),
clearCookie: sinon.stub()
};
next = sinon.stub();
});
{
let idamExpressProtect = null;
let getUserDetails = null;
beforeEach(() => {
getUserDetails = sinon.stub().returnsPromise();
sinon.stub(idamWrapper, 'setup').returns({ getUserDetails });
idamExpressProtect = middleware(idamArgs);
});
afterEach(() => {
idamWrapper.setup.restore();
});
it('calls next on successful auth', () => {
req.cookies[config.tokenCookieName] = 'token';
getUserDetails.resolves(userDetails);
idamExpressProtect(req, res, next);
expect(getUserDetails.callCount).to.equal(1);
expect(next.callCount).to.equal(1);
});
it('should set idam userDetails', () => {
req.cookies[config.tokenCookieName] = 'token';
getUserDetails.resolves(userDetails);
idamExpressProtect(req, res, next);
expect(getUserDetails.callCount).to.equal(1);
expect(next.callCount).to.equal(1);
expect(req.idam.userDetails).to.equal(userDetails);
});
it('calls next if getUserDetails rejects', () => {
req.cookies[config.tokenCookieName] = 'token';
getUserDetails.rejects();
idamExpressProtect(req, res, next);
expect(getUserDetails.callCount).to.equal(1);
expect(next.callCount).to.equal(1);
});
it('cookie to be removed if getUserDetails rejects', () => {
req.cookies[config.tokenCookieName] = 'token';
getUserDetails.rejects();
idamExpressProtect(req, res, next);
expect(res.clearCookie.callCount).to.equal(1);
});
it('cookie to be removed if getUserDetails rejects', () => {
req.cookies[config.tokenCookieName] = 'token';
getUserDetails.rejects();
idamExpressProtect(req, res, next);
expect(res.clearCookie.callCount).to.equal(1);
});
}
it('calls next if no token cookie', () => {
const handler = middleware(idamArgs);
handler(req, res, next);
expect(next.callCount).to.equal(1);
});
});
});