Skip to content

Commit d973824

Browse files
author
Max Marttinen
committed
add tests for hapi auth modes
1 parent 43c28b5 commit d973824

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

test/_helpers.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,32 @@ function registerRoutes (server) {
184184
}
185185
}
186186
}
187+
},
188+
{
189+
method: 'GET',
190+
path: '/mode-optional',
191+
options: {
192+
auth: { strategy: 'keycloak-jwt', mode: 'optional' },
193+
handler (req) {
194+
return {
195+
headers: req.headers,
196+
query: req.query
197+
}
198+
}
199+
}
200+
},
201+
{
202+
method: 'GET',
203+
path: '/mode-try',
204+
options: {
205+
auth: { strategy: 'keycloak-jwt', mode: 'try' },
206+
handler (req) {
207+
return {
208+
headers: req.headers,
209+
query: req.query
210+
}
211+
}
212+
}
187213
}
188214
])
189215
}

test/index.hapi-modes.spec.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const nock = require('nock')
2+
const test = require('ava')
3+
const helpers = require('./_helpers')
4+
const fixtures = require('./fixtures')
5+
6+
const cfg = helpers.getOptions({ secret: fixtures.common.secret })
7+
8+
test.afterEach.always('reset instances and prototypes', () => {
9+
nock.cleanAll()
10+
})
11+
12+
test('server method – authentication supports mode: "optional" - will succeed without auth', async (t) => {
13+
const server = await helpers.getServer(cfg)
14+
15+
const res = await server.inject({
16+
method: 'GET',
17+
url: '/mode-optional',
18+
})
19+
20+
t.truthy(res)
21+
t.is(res.statusCode, 200)
22+
})
23+
24+
test('server method – authentication supports mode: "optional" - will fail with invalid auth', async (t) => {
25+
const server = await helpers.getServer(cfg)
26+
27+
const res = await server.inject({
28+
method: 'GET',
29+
url: '/mode-optional',
30+
headers: {
31+
authorization: `bearer invalid-token`
32+
}
33+
})
34+
35+
t.truthy(res)
36+
t.is(res.statusCode, 401)
37+
t.is(err.output.headers['WWW-Authenticate'], 'Bearer strategy="keycloak-jwt", error="Invalid credentials"')
38+
})
39+
40+
test('server method – authentication supports mode: "try" - will succeed with invalid auth', async (t) => {
41+
const server = await helpers.getServer(cfg)
42+
43+
const res = await server.inject({
44+
method: 'GET',
45+
url: '/mode-try',
46+
headers: {
47+
authorization: `bearer ${fixtures.composeJwt('expired')}`
48+
}
49+
})
50+
51+
t.truthy(res)
52+
t.is(res.statusCode, 200)
53+
})

0 commit comments

Comments
 (0)