This repository was archived by the owner on Jan 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathconstructor.spec.js
More file actions
307 lines (276 loc) · 9.34 KB
/
Copy pathconstructor.spec.js
File metadata and controls
307 lines (276 loc) · 9.34 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const OpenIdClient = require('openid-client');
const passport = require('passport');
const nock = require('nock');
const os = require('os');
const path = require('path');
const rpt = require ('read-package-tree');
const { ExpressOIDC } = require('../../index.js');
const pkg = require('../../package.json');
const modulesRoot = path.resolve(__dirname, '../../');
const Issuer = OpenIdClient.Issuer;
const custom = OpenIdClient.custom;
describe('new ExpressOIDC()', () => {
const findDomainMessage = 'You can copy your domain from the Okta Developer ' +
'Console. Follow these instructions to find it: https://bit.ly/finding-okta-domain';
const findCredentialsMessage = 'You can copy it from the Okta Developer Console ' +
'in the details for the Application you created. Follow these instructions to ' +
'find it: https://bit.ly/finding-okta-app-credentials';
const minimumConfig = {
client_id: 'foo',
client_secret: 'foo',
issuer: 'https://foo',
appBaseUrl: 'https://app.foo'
};
function mockWellKnown(issuer) {
issuer = issuer || 'https://foo'
nock(issuer)
.get('/.well-known/openid-configuration')
.reply(200, {
issuer
})
}
afterEach(function() {
if(!nock.isDone()) {
nock.cleanAll();
throw new Error('Not all nock interceptors were used!');
}
});
it('should throw if no issuer is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
issuer: undefined
});
}
const errorMsg = `Your Okta URL is missing. ${findDomainMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should throw if an issuer that does not contain https is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
issuer: 'http://foo'
});
}
const errorMsg = `Your Okta URL must start with https. Current value: http://foo. ${findDomainMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should not throw if https issuer validation is skipped', done => {
jest.spyOn(console, 'warn').mockImplementation(() => {}); // silence for testing
mockWellKnown('http://foo');
new ExpressOIDC({
...minimumConfig,
issuer: 'http://foo',
testing: {
disableHttpsCheck: true
}
})
.on('error', () => {
expect(false).toBe(true);
})
.on('ready', () => {
expect(console.warn).toBeCalledWith('Warning: HTTPS check is disabled. This allows for insecure configurations and is NOT recommended for production use.');
done();
});
});
it('should throw if an issuer matching {yourOktaDomain} is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
issuer: 'https://{yourOktaDomain}'
});
}
const errorMsg = `Replace {yourOktaDomain} with your Okta domain. ${findDomainMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should throw if an issuer matching -admin.okta.com is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
issuer: 'https://foo-admin.okta.com'
});
}
const errorMsg = 'Your Okta domain should not contain -admin. Current value: ' +
`https://foo-admin.okta.com. ${findDomainMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should throw if an issuer matching -admin.oktapreview.com is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
issuer: 'https://foo-admin.oktapreview.com'
});
}
const errorMsg = 'Your Okta domain should not contain -admin. Current value: ' +
`https://foo-admin.oktapreview.com. ${findDomainMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should throw if an issuer matching -admin.okta-emea.com is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
issuer: 'https://foo-admin.okta-emea.com'
});
}
const errorMsg = 'Your Okta domain should not contain -admin. Current value: ' +
`https://foo-admin.okta-emea.com. ${findDomainMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should throw if the client_id is not provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
client_id: undefined
});
}
const errorMsg = `Your client ID is missing. ${findCredentialsMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should throw if the client_secret is not provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
client_secret: undefined
});
}
const errorMsg = `Your client secret is missing. ${findCredentialsMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should throw if a client_id matching {clientId} is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
client_id: '{clientId}'
});
}
const errorMsg = `Replace {clientId} with the client ID of your Application. ${findCredentialsMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should throw if a client_secret matching {clientSecret} is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
client_secret: '{clientSecret}',
});
}
const errorMsg = `Replace {clientSecret} with the client secret of your Application. ${findCredentialsMessage}`;
expect(createInstance).toThrow(errorMsg);
});
it('should throw if the appBaseUrl is not provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
appBaseUrl: undefined
});
}
const errorMsg = 'Your appBaseUrl is missing.';
expect(createInstance).toThrow(errorMsg);
});
it('should throw if a appBaseUrl matching {appBaseUrl} is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
appBaseUrl: '{appBaseUrl}'
});
}
const errorMsg = 'Replace {appBaseUrl} with the base URL of your Application.'
expect(createInstance).toThrow(errorMsg);
});
it('should throw if an appBaseUrl without a protocol is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
appBaseUrl: 'foo.example.com'
});
}
const errorMsg = 'Your appBaseUrl must contain a protocol (e.g. https://). Current value: foo.example.com.';
expect(createInstance).toThrow(errorMsg);
});
it('should throw if an appBaseUrl ending in a slash is provided', () => {
function createInstance() {
new ExpressOIDC({
...minimumConfig,
appBaseUrl: 'https://foo.example.com/'
});
}
const errorMsg = `Your appBaseUrl must not end in a '/'. Current value: https://foo.example.com/.`;
expect(createInstance).toThrow(errorMsg);
});
it('should set the HTTP timeout to 10 seconds', done => {
mockWellKnown();
new ExpressOIDC({
...minimumConfig
})
.on('ready', () => {
expect(Issuer[custom.http_options]().timeout).toBe(10000);
done();
});
});
it('should allow me to change the HTTP timeout', done => {
mockWellKnown();
new ExpressOIDC({
...minimumConfig,
timeout: 2000
})
.on('ready', () => {
expect(Issuer[custom.http_options]().timeout).toBe(2000);
done();
});
});
// eslint-disable-next-line jest/no-test-callback
it('should throw ETIMEOUT if the timeout is reached', (done) => {
nock('https://foo')
.get('/.well-known/openid-configuration')
.delay(1000)
.reply(200, function cb() {
// dont reply, we want to timeout
});
new ExpressOIDC({
...minimumConfig,
timeout: 1
}).on('error', (e) => {
nock.abortPendingRequests();
expect(e.code).toBe('ETIMEDOUT');
done();
});
});
// eslint-disable-next-line jest/no-test-callback
it('should set the correct User-Agent string', (done) => {
rpt(modulesRoot, function (node, kidName) {
return kidName.includes('openid');
}, function (er, data) {
const openIdPkg = data.children[0].package;
const expectedAgent = `${pkg.name}/${pkg.version} ${openIdPkg.name}/${openIdPkg.version} node/${process.versions.node} ${os.platform()}/${os.release()}`;
let userAgent;
nock('https://foo')
.get('/.well-known/openid-configuration')
.reply(200, function cb() {
userAgent = this.req.headers['user-agent'];
return JSON.stringify({ issuer: 'https://foo' });
});
new ExpressOIDC({
...minimumConfig
})
.on('ready', () => {
expect(userAgent).toBe(expectedAgent);
done();
})
});
});
it('should set token_endpoint_auth_method', (done) => {
mockWellKnown();
const passportStrategySetter = jest.spyOn(passport, 'use').mockImplementation(() => {});
new ExpressOIDC({
...minimumConfig,
oidcClientOptions: {
token_endpoint_auth_method: 'client_secret_post',
}
})
.on('ready', () => {
const passportStrategy = passportStrategySetter.mock.calls[0][1];
const oidcClient = passportStrategy._client;
expect(oidcClient.token_endpoint_auth_method).toBe('client_secret_post');
done();
});
});
});