UNPKG

1.69 kBJavaScriptView Raw
1const boom = require('@hapi/boom')
2const test = require('ava')
3const utils = require('../src/utils')
4
5test('get boom error with default message', (t) => {
6 const result = utils.raiseUnauthorized()
7 t.truthy(result)
8 t.deepEqual(result, boom.unauthorized(undefined, 'Bearer', {
9 strategy: 'keycloak-jwt'
10 }))
11})
12
13test('get boom error with reason', (t) => {
14 const result = utils.raiseUnauthorized(null, 'foobar')
15 t.truthy(result)
16 t.deepEqual(result, boom.unauthorized(undefined, 'Bearer', {
17 strategy: 'keycloak-jwt',
18 reason: 'foobar'
19 }))
20})
21
22test('get boom error with custom scheme', (t) => {
23 const result = utils.raiseUnauthorized(null, null, 'custom')
24 t.truthy(result)
25 t.deepEqual(result, boom.unauthorized(undefined, 'custom', {
26 strategy: 'keycloak-jwt'
27 }))
28})
29
30test('get boom error with error message', (t) => {
31 const result = utils.raiseUnauthorized('foobar')
32 t.truthy(result)
33 t.deepEqual(result, boom.unauthorized('foobar', 'Bearer', {
34 strategy: 'keycloak-jwt'
35 }))
36})
37
38test('decorate callback function with `authenticated`', (t) => {
39 const mockFn = function () {}
40 const fake = utils.fakeToolkit(mockFn)
41
42 t.truthy(mockFn.authenticated)
43 t.truthy(fake.authenticated)
44 t.deepEqual(mockFn, fake)
45})
46
47test('ignore callback function with existing `authenticated`', (t) => {
48 const mockFn = function () {}
49 mockFn.authenticated = 'foo'
50 const fake = utils.fakeToolkit(mockFn)
51
52 t.truthy(fake.authenticated)
53 t.is(fake.authenticated, 'foo')
54 t.deepEqual(mockFn, fake)
55})
56
57test('ignore callback function when not a function', (t) => {
58 const mockFn = {}
59 const fake = utils.fakeToolkit(mockFn)
60
61 t.falsy(fake.authenticated)
62})