|
| 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