@@ -146,3 +146,282 @@ test('hook accepts correct header with extra padding', (t) => {
146
146
t . pass ( )
147
147
} )
148
148
} )
149
+
150
+ test ( 'hook accepts correct header with auth function (promise)' , ( t ) => {
151
+ t . plan ( 2 )
152
+ const auth = function ( val ) {
153
+ t . equal ( val , key , 'wrong argument' )
154
+ return Promise . resolve ( true )
155
+ }
156
+ const request = {
157
+ log : { error : noop } ,
158
+ req : {
159
+ headers : { authorization : `bearer ${ key } ` }
160
+ }
161
+ }
162
+ const response = {
163
+ code : ( ) => response ,
164
+ send : send
165
+ }
166
+
167
+ function send ( body ) {
168
+ t . fail ( 'should not happen' )
169
+ }
170
+
171
+ const hook = plugin ( { auth } )
172
+ hook ( request , response , ( ) => {
173
+ t . pass ( )
174
+ } )
175
+ } )
176
+
177
+ test ( 'hook accepts correct header with auth function (non-promise)' , ( t ) => {
178
+ t . plan ( 2 )
179
+ const auth = function ( val ) {
180
+ t . equal ( val , key , 'wrong argument' )
181
+ return true
182
+ }
183
+ const request = {
184
+ log : { error : noop } ,
185
+ req : {
186
+ headers : { authorization : `bearer ${ key } ` }
187
+ }
188
+ }
189
+ const response = {
190
+ code : ( ) => response ,
191
+ send : send
192
+ }
193
+
194
+ function send ( body ) {
195
+ t . fail ( 'should not happen' )
196
+ }
197
+
198
+ const hook = plugin ( { auth } )
199
+ hook ( request , response , ( ) => {
200
+ t . pass ( )
201
+ } )
202
+ } )
203
+
204
+ test ( 'hook rejects wrong token with keys' , ( t ) => {
205
+ t . plan ( 2 )
206
+
207
+ const request = {
208
+ log : { error : noop } ,
209
+ req : {
210
+ headers : { authorization : `bearer abcdedfg` }
211
+ }
212
+ }
213
+ const response = {
214
+ code : ( ) => response ,
215
+ send : send
216
+ }
217
+
218
+ function send ( body ) {
219
+ t . ok ( body . error )
220
+ t . match ( body . error , / i n v a l i d a u t h o r i z a t i o n h e a d e r / )
221
+ }
222
+
223
+ const hook = plugin ( keys )
224
+ hook ( request , response , ( ) => {
225
+ t . fail ( 'should not accept' )
226
+ } )
227
+ } )
228
+
229
+ test ( 'hook rejects wrong token with auth function' , ( t ) => {
230
+ t . plan ( 5 )
231
+
232
+ const request = {
233
+ log : { error : noop } ,
234
+ req : {
235
+ headers : { authorization : `bearer abcdefg` }
236
+ }
237
+ }
238
+
239
+ const auth = function ( val , req ) {
240
+ t . equal ( req , request )
241
+ t . equal ( val , 'abcdefg' , 'wrong argument' )
242
+ return false
243
+ }
244
+
245
+ const response = {
246
+ code : ( status ) => {
247
+ t . equal ( 401 , status )
248
+ return response
249
+ } ,
250
+ send : send
251
+ }
252
+
253
+ function send ( body ) {
254
+ t . ok ( body . error )
255
+ t . match ( body . error , / i n v a l i d a u t h o r i z a t i o n h e a d e r / )
256
+ }
257
+
258
+ const hook = plugin ( { auth } )
259
+ hook ( request , response , ( ) => {
260
+ t . fail ( 'should not accept' )
261
+ } )
262
+ } )
263
+
264
+ test ( 'hook rejects wrong token with function (resolved promise)' , ( t ) => {
265
+ t . plan ( 4 )
266
+
267
+ const auth = function ( val ) {
268
+ t . equal ( val , 'abcdefg' , 'wrong argument' )
269
+ return Promise . resolve ( false )
270
+ }
271
+
272
+ const request = {
273
+ log : { error : noop } ,
274
+ req : {
275
+ headers : { authorization : `bearer abcdefg` }
276
+ }
277
+ }
278
+ const response = {
279
+ code : ( status ) => {
280
+ t . equal ( 401 , status )
281
+ return response
282
+ } ,
283
+ send : send
284
+ }
285
+
286
+ function send ( body ) {
287
+ t . ok ( body . error )
288
+ t . match ( body . error , / i n v a l i d a u t h o r i z a t i o n h e a d e r / )
289
+ }
290
+
291
+ const hook = plugin ( { auth } )
292
+ hook ( request , response , ( ) => {
293
+ t . fail ( 'should not accept' )
294
+ } )
295
+ } )
296
+
297
+ test ( 'hook rejects with 500 when functions fails' , ( t ) => {
298
+ t . plan ( 4 )
299
+
300
+ const auth = function ( val ) {
301
+ t . equal ( val , 'abcdefg' , 'wrong argument' )
302
+ throw Error ( 'failing' )
303
+ }
304
+
305
+ const request = {
306
+ log : { error : noop } ,
307
+ req : {
308
+ headers : { authorization : `bearer abcdefg` }
309
+ }
310
+ }
311
+ const response = {
312
+ code : ( status ) => {
313
+ t . equal ( 500 , status )
314
+ return response
315
+ } ,
316
+ send : send
317
+ }
318
+
319
+ function send ( body ) {
320
+ t . ok ( body . error )
321
+ t . match ( body . error , / f a i l i n g / )
322
+ }
323
+
324
+ const hook = plugin ( { auth } )
325
+ hook ( request , response , ( ) => {
326
+ t . fail ( 'should not accept' )
327
+ } )
328
+ } )
329
+
330
+ test ( 'hook rejects with 500 when promise rejects' , ( t ) => {
331
+ t . plan ( 4 )
332
+
333
+ const auth = function ( val ) {
334
+ t . equal ( val , 'abcdefg' , 'wrong argument' )
335
+ return Promise . reject ( Error ( 'failing' ) )
336
+ }
337
+
338
+ const request = {
339
+ log : { error : noop } ,
340
+ req : {
341
+ headers : { authorization : `bearer abcdefg` }
342
+ }
343
+ }
344
+ const response = {
345
+ code : ( status ) => {
346
+ t . equal ( 500 , status )
347
+ return response
348
+ } ,
349
+ send : send
350
+ }
351
+
352
+ function send ( body ) {
353
+ t . ok ( body . error )
354
+ t . match ( body . error , / f a i l i n g / )
355
+ }
356
+
357
+ const hook = plugin ( { auth } )
358
+ hook ( request , response , ( ) => {
359
+ t . fail ( 'should not accept' )
360
+ } )
361
+ } )
362
+
363
+ test ( 'hook rejects with 500 when functions returns non-boolean' , ( t ) => {
364
+ t . plan ( 4 )
365
+
366
+ const auth = function ( val ) {
367
+ t . equal ( val , 'abcdefg' , 'wrong argument' )
368
+ return 'foobar'
369
+ }
370
+
371
+ const request = {
372
+ log : { error : noop } ,
373
+ req : {
374
+ headers : { authorization : `bearer abcdefg` }
375
+ }
376
+ }
377
+ const response = {
378
+ code : ( status ) => {
379
+ t . equal ( 500 , status )
380
+ return response
381
+ } ,
382
+ send : send
383
+ }
384
+
385
+ function send ( body ) {
386
+ t . ok ( body . error )
387
+ t . match ( body . error , / i n t e r n a l s e r v e r e r r o r / )
388
+ }
389
+
390
+ const hook = plugin ( { auth } )
391
+ hook ( request , response , ( ) => {
392
+ t . fail ( 'should not accept' )
393
+ } )
394
+ } )
395
+
396
+ test ( 'hook rejects with 500 when promise resolves to non-boolean' , ( t ) => {
397
+ t . plan ( 4 )
398
+
399
+ const auth = function ( val ) {
400
+ t . equal ( val , 'abcdefg' , 'wrong argument' )
401
+ return Promise . resolve ( 'abcde' )
402
+ }
403
+
404
+ const request = {
405
+ log : { error : noop } ,
406
+ req : {
407
+ headers : { authorization : `bearer abcdefg` }
408
+ }
409
+ }
410
+ const response = {
411
+ code : ( status ) => {
412
+ t . equal ( 500 , status )
413
+ return response
414
+ } ,
415
+ send : send
416
+ }
417
+
418
+ function send ( body ) {
419
+ t . ok ( body . error )
420
+ t . match ( body . error , / i n t e r n a l s e r v e r e r r o r / )
421
+ }
422
+
423
+ const hook = plugin ( { auth } )
424
+ hook ( request , response , ( ) => {
425
+ t . fail ( 'should not accept' )
426
+ } )
427
+ } )
0 commit comments