-
Notifications
You must be signed in to change notification settings - Fork 20
/
customMSWServer.test.js
64 lines (56 loc) · 1.75 KB
/
customMSWServer.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
// @ts-check
/**
* @typedef
*/
import createJWKSMock from 'mock-jwks'
import { createApp } from './api.js'
import supertest from 'supertest'
import { beforeAll, beforeEach, describe, expect, test } from 'vitest'
import { setupServer } from 'msw/node'
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', () => {
/** @type {import('msw/node').SetupServerApi} */
let mswServer
beforeAll(() => {
mswServer = setupServer()
mswServer.listen({
onUnhandledRequest: 'bypass', // We silence the warnings of msw for unhandled requests. Not necessary for things to work.
})
return () => mswServer.close()
})
beforeEach(() => {
mswServer.resetHandlers()
})
test('Can get access with mock token when handler is attached to msw', async () => {
// arrange
mswServer.use(jwksMock.mswHandler)
const access_token = jwksMock.token({
aud: 'private',
iss: 'master',
})
// act
const { status } = await supertest(app)
.get('/')
.set('Authorization', `Bearer ${access_token}`)
// assert
expect(status).toEqual(200)
})
test('Cannot get access with mock token when handler is not attached to msw', 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.
// arrange
const access_token = jwksMock.token({
aud: 'private',
iss: 'master',
})
// act
const { status } = await supertest(app)
.get('/')
.set('Authorization', `Bearer ${access_token}`)
// assert
expect(status).toEqual(500)
})
})