-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.js
More file actions
143 lines (125 loc) · 5.02 KB
/
test.js
File metadata and controls
143 lines (125 loc) · 5.02 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const { promisify } = require('util');
const { decode, verify: jwtVerify } = require('jsonwebtoken');
const lib = require('./lib');
const jwtVerifyP = promisify(jwtVerify);
const {
accountToken, projectToken, generateToken, verify,
} = lib;
const accountJWT = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBUElfS0VZIiwiaXN0IjoiYWNjb3VudCIsImlhdCI6MTUzOTA5MywiZXhwIjo0MTMxMDkzfQ.b5BvdeNtuJPzoZjqMNe-wQPqJOEnLTPkqwwgsK6KtpI';
const projectJWT = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBUElfS0VZIiwiaXN0IjoicHJvamVjdCIsImlhdCI6MTUzOTA5MywiZXhwIjo0MTMxMDkzfQ.Mb_mwsvs5wW0Bf-BCFDGdmkOXYpQvvD14EXOWlJ3r7k';
const accountJWTCustomExp = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBUElfS0VZIiwiaXN0IjoiYWNjb3VudCIsImlhdCI6MTUzOTA5MywiZXhwIjoxNjI1NDkzfQ.mG533LCwrOPBkgCR0EgQtSOM1L31JavQRLv_266kDnM';
const projectJWTCustomExp = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBUElfS0VZIiwiaXN0IjoicHJvamVjdCIsImlhdCI6MTUzOTA5MywiZXhwIjoxNjI1NDkzfQ.iIV-84cmarQtbT4RYW9DIEXPhhAwnr8S3Znisv-LnUw';
const apiKey = 'API_KEY';
const secret = 'SECRET';
const timestamp = 1539093828;
const currentTime = Math.floor(timestamp / 1000);
const customExpiry = currentTime + (24 * 60 * 60);
const defaultExpiry = currentTime + (30 * 24 * 60 * 60);
const RealDate = Date;
const mockDate = (isoDate) => {
global.Date = class extends RealDate {
constructor() {
return new RealDate(isoDate);
}
};
};
describe('lib', () => {
describe('verify', () => {
it('should decode token', async (done) => {
const currentTimestamp = new Date();
mockDate(currentTimestamp);
const exp = Math.floor(currentTimestamp / 1000) + (30 * 24 * 60 * 60);
const token = accountToken(apiKey, secret);
const decoded = await verify(token, secret);
expect(decoded.iss).toEqual(apiKey);
expect(decoded.exp).toEqual(exp);
done();
});
it('should pass errors on from jwt library', async (done) => {
let jwtError;
let opentokJwtError;
try {
await jwtVerifyP(accountJWT, secret);
} catch (err) {
jwtError = err;
}
try {
await jwtVerifyP(accountJWT, secret);
} catch (err) {
opentokJwtError = err;
}
expect(opentokJwtError).toEqual(jwtError);
done();
});
});
describe('regression tests with mocked date', () => {
beforeAll(() => {
mockDate(timestamp);
});
afterAll(() => {
global.Date = RealDate;
});
describe('accountToken', () => {
it('should return account jwt token', async (done) => {
const token = accountToken(apiKey, secret);
expect(token).toEqual(accountJWT);
const decoded = await decode(token, secret);
expect(decoded.exp).toEqual(defaultExpiry);
done();
});
it('should return account jwt token with custom expire', async (done) => {
const token = accountToken(apiKey, secret, customExpiry);
expect(token).toEqual(accountJWTCustomExp);
const decoded = await decode(token, secret);
expect(decoded.exp).toEqual(customExpiry);
done();
});
});
describe('projectToken', () => {
it('should return project jwt token', async (done) => {
const token = projectToken(apiKey, secret);
expect(token).toEqual(projectJWT);
const decoded = await decode(token, secret);
expect(decoded.exp).toEqual(defaultExpiry);
done();
});
it('should return project jwt token with custom expire', async (done) => {
const token = projectToken(apiKey, secret, customExpiry);
expect(token).toEqual(projectJWTCustomExp);
const decoded = await decode(token, secret);
expect(decoded.exp).toEqual(customExpiry);
done();
});
});
describe('generateToken', () => {
it('should return project jwt token', async (done) => {
const token = generateToken(apiKey, secret, 'project');
expect(token).toEqual(projectJWT);
const decoded = await decode(token, secret);
expect(decoded.exp).toEqual(defaultExpiry);
done();
});
it('should return project jwt token with custom expire', async (done) => {
const token = generateToken(apiKey, secret, 'project', customExpiry);
expect(token).toEqual(projectJWTCustomExp);
const decoded = await decode(token, secret);
expect(decoded.exp).toEqual(customExpiry);
done();
});
it('should return account jwt token', async (done) => {
const token = generateToken(apiKey, secret, 'account');
expect(token).toEqual(accountJWT);
const decoded = await decode(token, secret);
expect(decoded.exp).toEqual(defaultExpiry);
done();
});
it('should return account jwt token with custom expire', async (done) => {
const token = generateToken(apiKey, secret, 'account', customExpiry);
expect(token).toEqual(accountJWTCustomExp);
const decoded = await decode(token, secret);
expect(decoded.exp).toEqual(customExpiry);
done();
});
});
});
});