Skip to content

Commit ed3e1de

Browse files
committed
add validation tests
1 parent 1968876 commit ed3e1de

File tree

2 files changed

+99
-8
lines changed

2 files changed

+99
-8
lines changed

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const scheme = joi.object({
1515
cache: joi.alternatives().try(joi.object({
1616
segment: joi.string().default('keycloakJwt')
1717
}), joi.boolean().invalid(true)).default(false),
18-
userInfo: joi.array().items(joi.string())
18+
userInfo: joi.array().items(joi.string().min(1))
1919
}).unknown(true).required()
2020

2121
/**

test/utils.spec.js

Lines changed: 98 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,56 +84,75 @@ test('throw error if options are invalid – client', (t) => {
8484
})
8585

8686
test('throw error if options are invalid – client.realmUrl', (t) => {
87+
t.throws(() => utils.verify(), Error)
88+
t.throws(() => utils.verify({}), Error)
89+
8790
t.throws(() => utils.verify({
8891
client: {
92+
clientId: 'foobar',
8993
realmUrl: null
9094
}
9195
}), Error)
9296

9397
t.throws(() => utils.verify({
9498
client: {
99+
clientId: 'foobar',
95100
realmUrl: undefined
96101
}
97102
}), Error)
98103

99104
t.throws(() => utils.verify({
100105
client: {
106+
clientId: 'foobar',
101107
realmUrl: NaN
102108
}
103109
}), Error)
104110

105111
t.throws(() => utils.verify({
106112
client: {
113+
clientId: 'foobar',
114+
realmUrl: ''
115+
}
116+
}), Error)
117+
118+
t.throws(() => utils.verify({
119+
client: {
120+
clientId: 'foobar',
107121
realmUrl: 'foobar'
108122
}
109123
}), Error)
110124

111125
t.throws(() => utils.verify({
112126
client: {
127+
clientId: 'foobar',
113128
realmUrl: 42
114129
}
115130
}), Error)
116131

117132
t.throws(() => utils.verify({
118133
client: {
134+
clientId: 'foobar',
119135
realmUrl: true
120136
}
121137
}), Error)
122138

123139
t.throws(() => utils.verify({
124140
client: {
141+
clientId: 'foobar',
125142
realmUrl: []
126143
}
127144
}), Error)
128145

129146
t.throws(() => utils.verify({
130147
client: {
148+
clientId: 'foobar',
131149
realmUrl: new RegExp()
132150
}
133151
}), Error)
134152

135153
t.throws(() => utils.verify({
136154
client: {
155+
clientId: 'foobar',
137156
realmUrl: {}
138157
}
139158
}), Error)
@@ -142,48 +161,56 @@ test('throw error if options are invalid – client.realmUrl', (t) => {
142161
test('throw error if options are invalid – client.clientId', (t) => {
143162
t.throws(() => utils.verify({
144163
client: {
164+
realmUrl: 'http://foobar.com',
145165
clientId: null
146166
}
147167
}), Error)
148168

149169
t.throws(() => utils.verify({
150170
client: {
171+
realmUrl: 'http://foobar.com',
151172
clientId: undefined
152173
}
153174
}), Error)
154175

155176
t.throws(() => utils.verify({
156177
client: {
178+
realmUrl: 'http://foobar.com',
157179
clientId: NaN
158180
}
159181
}), Error)
160182

161183
t.throws(() => utils.verify({
162184
client: {
185+
realmUrl: 'http://foobar.com',
163186
clientId: 42
164187
}
165188
}), Error)
166189

167190
t.throws(() => utils.verify({
168191
client: {
192+
realmUrl: 'http://foobar.com',
169193
clientId: true
170194
}
171195
}), Error)
172196

173197
t.throws(() => utils.verify({
174198
client: {
199+
realmUrl: 'http://foobar.com',
175200
clientId: []
176201
}
177202
}), Error)
178203

179204
t.throws(() => utils.verify({
180205
client: {
206+
realmUrl: 'http://foobar.com',
181207
clientId: new RegExp()
182208
}
183209
}), Error)
184210

185211
t.throws(() => utils.verify({
186212
client: {
213+
realmUrl: 'http://foobar.com',
187214
clientId: {}
188215
}
189216
}), Error)
@@ -253,55 +280,119 @@ test('throw error if options are invalid – userInfo', (t) => {
253280
clientId: 'foobar',
254281
realmUrl: 'http://foobar.com'
255282
},
256-
cache: null
283+
userInfo: null
257284
}), Error)
258285

259286
t.throws(() => utils.verify({
260287
client: {
261288
clientId: 'foobar',
262289
realmUrl: 'http://foobar.com'
263290
},
264-
cache: NaN
291+
userInfo: NaN
265292
}), Error)
266293

267294
t.throws(() => utils.verify({
268295
client: {
269296
clientId: 'foobar',
270297
realmUrl: 'http://foobar.com'
271298
},
272-
cache: ''
299+
userInfo: ''
273300
}), Error)
274301

275302
t.throws(() => utils.verify({
276303
client: {
277304
clientId: 'foobar',
278305
realmUrl: 'http://foobar.com'
279306
},
280-
cache: 'foobar'
307+
userInfo: 'foobar'
281308
}), Error)
282309

283310
t.throws(() => utils.verify({
284311
client: {
285312
clientId: 'foobar',
286313
realmUrl: 'http://foobar.com'
287314
},
288-
cache: 42
315+
userInfo: 42
289316
}), Error)
290317

291318
t.throws(() => utils.verify({
292319
client: {
293320
clientId: 'foobar',
294321
realmUrl: 'http://foobar.com'
295322
},
296-
cache: true
323+
userInfo: true
324+
}), Error)
325+
326+
t.throws(() => utils.verify({
327+
client: {
328+
clientId: 'foobar',
329+
realmUrl: 'http://foobar.com'
330+
},
331+
userInfo: [null]
332+
}), Error)
333+
334+
t.throws(() => utils.verify({
335+
client: {
336+
clientId: 'foobar',
337+
realmUrl: 'http://foobar.com'
338+
},
339+
userInfo: [undefined]
340+
}), Error)
341+
342+
t.throws(() => utils.verify({
343+
client: {
344+
clientId: 'foobar',
345+
realmUrl: 'http://foobar.com'
346+
},
347+
userInfo: [NaN]
348+
}), Error)
349+
350+
t.throws(() => utils.verify({
351+
client: {
352+
clientId: 'foobar',
353+
realmUrl: 'http://foobar.com'
354+
},
355+
userInfo: [42]
356+
}), Error)
357+
358+
t.throws(() => utils.verify({
359+
client: {
360+
clientId: 'foobar',
361+
realmUrl: 'http://foobar.com'
362+
},
363+
userInfo: [true]
364+
}), Error)
365+
366+
t.throws(() => utils.verify({
367+
client: {
368+
clientId: 'foobar',
369+
realmUrl: 'http://foobar.com'
370+
},
371+
userInfo: ['']
372+
}), Error)
373+
374+
t.throws(() => utils.verify({
375+
client: {
376+
clientId: 'foobar',
377+
realmUrl: 'http://foobar.com'
378+
},
379+
userInfo: [{}]
380+
}), Error)
381+
382+
t.throws(() => utils.verify({
383+
client: {
384+
clientId: 'foobar',
385+
realmUrl: 'http://foobar.com'
386+
},
387+
userInfo: [[]]
297388
}), Error)
298389

299390
t.throws(() => utils.verify({
300391
client: {
301392
clientId: 'foobar',
302393
realmUrl: 'http://foobar.com'
303394
},
304-
cache: [42]
395+
userInfo: [new RegExp()]
305396
}), Error)
306397
})
307398

0 commit comments

Comments
 (0)