-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathtoken.test.js
99 lines (83 loc) · 3.41 KB
/
token.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
/* eslint-disable camelcase */
const jwt = require('jsonwebtoken');
const { hash } = require('@senecacdot/satellite');
const { createToken } = require('../src/token');
const { JWT_AUDIENCE, JWT_ISSUER, JWT_SECRET } = process.env;
const token = () =>
createToken('email', 'first', 'last', 'name', ['seneca', 'telescope'], 'picture');
describe('createToken()', () => {
it('should return a JWT with the expected sub claim', () => {
const { sub } = jwt.verify(token(), JWT_SECRET);
// sub should be the hashed email value
expect(sub).toBe(hash('email'));
});
it('should return a JWT with the expected email claim', () => {
const { email } = jwt.verify(token(), JWT_SECRET);
expect(email).toBe('email');
});
it('should return a JWT with the expected given_name claim', () => {
const { given_name } = jwt.verify(token(), JWT_SECRET);
expect(given_name).toBe('first');
});
it('should return a JWT with the expected family_name claim', () => {
const { family_name } = jwt.verify(token(), JWT_SECRET);
expect(family_name).toBe('last');
});
it('should return a JWT with the expected name claim', () => {
const { name } = jwt.verify(token(), JWT_SECRET);
expect(name).toBe('name');
});
it('should return a JWT with the expected "seneca" roles claim', () => {
const { roles } = jwt.verify(token(), JWT_SECRET);
expect(Array.isArray(roles)).toBe(true);
expect(roles.length).toBe(2);
expect(roles).toEqual(['seneca', 'telescope']);
});
it('should return a JWT with the expected "seneca" and "telescope" and "admin" roles claim', () => {
const { roles } = jwt.verify(
createToken('email', 'first', 'last', 'name', ['seneca', 'telescope', 'admin'], 'picture'),
JWT_SECRET
);
expect(Array.isArray(roles)).toBe(true);
expect(roles.length).toBe(3);
expect(roles).toContain('seneca');
expect(roles).toContain('telescope');
expect(roles).toContain('admin');
});
it('should return a JWT with the expected audience claim', () => {
const { aud } = jwt.verify(token(), JWT_SECRET);
expect(aud).toBe(JWT_AUDIENCE);
});
it('should return a JWT with the expected issuer claim', () => {
const { iss } = jwt.verify(token(), JWT_SECRET);
expect(iss).toBe(JWT_ISSUER);
});
it('should return a JWT with an expiry claim', () => {
const { exp } = jwt.verify(token(), JWT_SECRET);
expect(typeof exp === 'number').toBe(true);
// The expiry time should be in the future
const nowSeconds = Date.now() / 1000;
expect(exp).toBeGreaterThan(nowSeconds);
});
it('should return a JWT with an issued at claim', () => {
const { iat } = jwt.verify(token(), JWT_SECRET);
expect(typeof iat === 'number').toBe(true);
// The issued at time should be in the past (or now)
const nowSeconds = Date.now() / 1000;
expect(iat).toBeLessThanOrEqual(nowSeconds);
});
it('should return a JWT without the picture claim, if picture not defined', () => {
const { picture } = jwt.verify(
createToken('email', 'first', 'last', 'name', ['seneca', 'telescope']),
JWT_SECRET
);
expect(picture).toBe(undefined);
});
it('should return a JWT with the picture claim, if picture defined', () => {
const { picture } = jwt.verify(
createToken('email', 'first', 'last', 'name', ['seneca', 'telescope'], 'https://picture.com'),
JWT_SECRET
);
expect(picture).toEqual('https://picture.com');
});
});