-
Notifications
You must be signed in to change notification settings - Fork 20
/
authentication.test.js
66 lines (63 loc) · 2.19 KB
/
authentication.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// @ts-check
import { createJWKSMock } from 'mock-jwks'
import { createApp } from './api.js'
import supertest from 'supertest'
import { describe, expect, test, onTestFinished } from 'vitest'
// This creates the local PKI
const jwksMock = createJWKSMock('https://levino.eu.auth0.com')
const app = createApp({
jwksUri: 'https://levino.eu.auth0.com/.well-known/jwks.json',
})
describe('Some tests for authentication for our api', () => {
test('should not get access without correct token', async () => {
// We start intercepting queries (see below)
onTestFinished(jwksMock.start())
const { status } = await supertest(app).get('/')
expect(status).toEqual(401)
})
test('should get access with mock token when jwksMock is running', async () => {
// Again we start intercepting queries
onTestFinished(jwksMock.start())
const access_token = jwksMock.token({
aud: 'private',
iss: 'master',
})
const { status } = await supertest(app)
.get('/')
.set('Authorization', `Bearer ${access_token}`)
expect(status).toEqual(200)
})
test('should not get access with mock token when jwksMock is not running', async () => {
// Now we do not intercept queries. The queries of the middleware for the JKWS will
// go to the production server and the local key will be invalid.
const access_token = jwksMock.token({
aud: 'private',
iss: 'master',
})
const { status } = await supertest(app)
.get('/')
.set('Authorization', `Bearer ${access_token}`)
expect(status).toEqual(500)
})
})
test('Another example with a non-auth0-style jkwsUri', async () => {
const jwksMock = createJWKSMock(
'https://keycloak.somedomain.com',
'/auth/realm/application/protocol/openid-connect/certs'
)
// We start our app.
const app = createApp({
jwksUri:
'https://keycloak.somedomain.com/auth/realm/application/protocol/openid-connect/certs',
})
const request = supertest(app)
onTestFinished(jwksMock.start())
const access_token = jwksMock.token({
aud: 'private',
iss: 'master',
})
const { status } = await request
.get('/')
.set('Authorization', `Bearer ${access_token}`)
expect(status).toEqual(200)
})