Skip to content

Commit 7eb70a1

Browse files
committed
add tests for option validation
1 parent 5dbb8a8 commit 7eb70a1

File tree

5 files changed

+121
-10
lines changed

5 files changed

+121
-10
lines changed

test/cache.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const test = require('ava')
2-
const { getServer } = require('./_helpers')
2+
const helpers = require('./_helpers')
33
const cache = require('../src/cache')
44

55
test.afterEach('reset cache', () => {
66
cache.reset()
77
})
88

99
test.cb.serial('just create one instance – object', (t) => {
10-
getServer(false, (server) => {
10+
helpers.getServer(false, (server) => {
1111
const cch1 = cache.init(server, { segment: 'foo' })
1212
const cch2 = cache.init(server, { segment: 'foo' })
1313

@@ -19,7 +19,7 @@ test.cb.serial('just create one instance – object', (t) => {
1919
})
2020

2121
test.cb.serial('just create one instance – boolean', (t) => {
22-
getServer(false, (server) => {
22+
helpers.getServer(false, (server) => {
2323
const cch1 = cache.init(server, false)
2424
const cch2 = cache.init(server, false)
2525

@@ -31,7 +31,7 @@ test.cb.serial('just create one instance – boolean', (t) => {
3131
})
3232

3333
test.cb.serial('set and get value', (t) => {
34-
getServer(false, (server) => {
34+
helpers.getServer(false, (server) => {
3535
cache.init(server, { segment: 'foo' })
3636
cache.set('bar', 42, 10000)
3737

@@ -44,7 +44,7 @@ test.cb.serial('set and get value', (t) => {
4444
})
4545

4646
test.cb.serial('set and get value – no cache', (t) => {
47-
getServer(false, (server) => {
47+
helpers.getServer(false, (server) => {
4848
cache.init(server, false)
4949
cache.set('bar', 42, 10000)
5050

@@ -57,7 +57,7 @@ test.cb.serial('set and get value – no cache', (t) => {
5757
})
5858

5959
test.cb.serial('set and get value – expired', (t) => {
60-
getServer(false, (server) => {
60+
helpers.getServer(false, (server) => {
6161
cache.init(server, { segment: 'foo' })
6262
cache.set('bar', 42, 100)
6363

test/fixtures/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ const introspectPath = '/protocol/openid-connect/token/introspect'
55
const realmUrl = `${baseUrl}${realmPath}`
66
const clientId = 'foobar'
77
const secret = '1234-bar-4321-foo'
8+
const publicKey = `-----BEGIN RSA PUBLIC KEY-----
9+
MIICCgKCAgEAur96MoQa/blg5eJFqmN//V4oQjKaBJl6KEvWSGAVgRm2PsnFKzCJ
10+
K9aJrUjETS473/x6fAHXyF5QKun6avNxpWs2VzwlO4t8Bi2EpSW2w0led2OzR/MF
11+
CYUX1Bg00OXLmdh0kvemABHXSOL4Zs4nN95rr77JsdG6ntylWmtnZlofySwLjFrX
12+
B2JFpunl4n7eF7y8vD4Zyycvi+xl+OuMA6qomLQmfIDF0qJh0QMt/xh6CB4ooK5a
13+
Fy6VHORU5gS6MoSgJ78ZPhqmKayRd6U0rhDOq7bWiVykktTPdB1X/YlMbQHonyqZ
14+
0z2sh7e7olroUdE1FsDi0SdxV2Zzvff8IIPSGT74gQuaU4buoVUpMjSdXDfJw9m4
15+
i/mhsZwX1/MUU8Oq1AsBHdgTkNVaVwFc6Z7AqJSGW/mxMKQqspOr+/BAM8pnw5BC
16+
R/RnZBJfMjAYiSh6rVYuEWUBBLgZWIRNAMyRwS8OtWxADfOBtVwn8fmw8schro9X
17+
D17VcqTdjlS1Mkr73ZoD1GagWZvuSOaz2P0PhnfapRUF1KkbjjnbyzLpfT8Mgovv
18+
xBQJDIcRYL5oetXu//V5rNAr0na4zMBkPOi3ArpFp+Z4YKulYmSEG//216rLmzjl
19+
WKEkH1OK39jddDrMTidlxDKY+THheyQBPZ6pFfbKEM5281glYjkeQpECAwEAAQ==
20+
-----END RSA PUBLIC KEY-----`
821

922
/**
1023
* @type Object
@@ -25,6 +38,7 @@ const clientConfig = {
2538
* Common attributes
2639
*/
2740
const common = Object.assign({}, clientConfig, {
41+
publicKey,
2842
baseUrl,
2943
token,
3044
realmPath,

test/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const test = require('ava')
21
const nock = require('nock')
3-
const cache = require('../src/cache')
2+
const test = require('ava')
43
const helpers = require('./_helpers')
54
const fixtures = require('./fixtures')
5+
const cache = require('../src/cache')
66

77
test.afterEach('reset instances and prototypes', () => {
88
cache.reset()

test/token.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const test = require('ava')
2-
const token = require('../src/token')
32
const fixtures = require('./fixtures')
3+
const token = require('../src/token')
44

55
test('enable multiple instances', (t) => {
66
const tkn1 = token('bearer a.b.c')

test/utils.spec.js

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const test = require('ava')
21
const boom = require('boom')
2+
const test = require('ava')
33
const helpers = require('./_helpers')
4+
const fixtures = require('./fixtures')
45
const utils = require('../src/utils')
56

67
test('get boom error with default message', (t) => {
@@ -113,12 +114,81 @@ test('throw error if options are invalid – secret', (t) => {
113114
})
114115
})
115116

117+
test('throw error if options are invalid – publicKey', (t) => {
118+
const invalids = [
119+
null,
120+
NaN,
121+
'',
122+
'foobar',
123+
fixtures.common.baseUrl,
124+
42,
125+
true,
126+
false,
127+
[],
128+
new RegExp(),
129+
{}
130+
]
131+
132+
t.plan(invalids.length)
133+
134+
invalids.forEach((invalid) => {
135+
t.throws(() => utils.verify(helpers.getOptions({
136+
secret: undefined,
137+
publicKey: invalid
138+
})), Error, helpers.log('publicKey', invalid))
139+
})
140+
})
141+
142+
test('throw error if options are invalid – verifyOpts', (t) => {
143+
const invalids = [
144+
null,
145+
NaN,
146+
'',
147+
'foobar',
148+
fixtures.common.baseUrl,
149+
42,
150+
true,
151+
false,
152+
[],
153+
{ ignoreExpiration: true },
154+
{ ignoreNotBefore: true }
155+
]
156+
157+
t.plan(invalids.length)
158+
159+
invalids.forEach((invalid) => {
160+
t.throws(() => utils.verify(helpers.getOptions({
161+
secret: undefined,
162+
publicKey: fixtures.common.publicKey,
163+
verifyOpts: invalid
164+
})), Error, helpers.log('publicKey', invalid))
165+
})
166+
})
167+
168+
test('throw error if options are invalid – publicKey/secret conflict', (t) => {
169+
t.throws(() => utils.verify(helpers.getOptions({
170+
secret: undefined,
171+
publicKey: undefined
172+
})), Error, 'publicKey/secret: both undefined')
173+
174+
t.throws(() => utils.verify(helpers.getOptions({
175+
publicKey: fixtures.common.publicKey
176+
})), Error, 'publicKey/secret: both defined')
177+
})
178+
179+
test('throw error if options are invalid – verifyOpts/secret conflict', (t) => {
180+
t.throws(() => utils.verify(helpers.getOptions({
181+
verifyOpts: {}
182+
})), Error, 'verifyOpts/secret: both defined')
183+
})
184+
116185
test('throw error if options are invalid – cache', (t) => {
117186
const invalids = [
118187
null,
119188
NaN,
120189
'',
121190
'foobar',
191+
fixtures.common.baseUrl,
122192
42,
123193
[]
124194
]
@@ -138,6 +208,7 @@ test('throw error if options are invalid – userInfo', (t) => {
138208
NaN,
139209
'',
140210
'foobar',
211+
fixtures.common.baseUrl,
141212
42,
142213
true,
143214
false,
@@ -183,3 +254,29 @@ test('throw no error if options are valid', (t) => {
183254
Error, helpers.log('valid', valid))
184255
})
185256
})
257+
258+
test('throw no error if options are valid', (t) => {
259+
const valids = [
260+
{},
261+
{ verifyOpts: undefined },
262+
{ verifyOpts: {} },
263+
{ verifyOpts: { audience: 'foobar' } },
264+
{ cache: {} },
265+
{ cache: { segment: 'foobar' } },
266+
{ cache: true },
267+
{ cache: false },
268+
{ userInfo: [] },
269+
{ userInfo: ['string'] }
270+
]
271+
272+
t.plan(valids.length)
273+
274+
valids.forEach((valid) => {
275+
t.notThrows(
276+
() => utils.verify(helpers.getOptions(Object.assign({
277+
secret: undefined,
278+
publicKey: fixtures.common.publicKey
279+
}, valid))),
280+
Error, helpers.log('valid with publicKey', valid))
281+
})
282+
})

0 commit comments

Comments
 (0)