|
| 1 | +import { INestApplication } from '@nestjs/common'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import type { Identity } from '@ory/client'; |
| 4 | +import { execSync } from 'node:child_process'; |
| 5 | +import { randomBytes } from 'node:crypto'; |
| 6 | +import { join, resolve } from 'node:path'; |
| 7 | +import request from 'supertest'; |
| 8 | + |
| 9 | +import { OryFrontendModule, OryFrontendService } from '../src/lib/ory-frontend'; |
| 10 | +import { |
| 11 | + OryIdentitiesModule, |
| 12 | + OryIdentitiesService, |
| 13 | +} from '../src/lib/ory-identities'; |
| 14 | +import { ExampleController } from './app.controller.mock'; |
| 15 | +import { ExampleService } from './app.service.mock'; |
| 16 | + |
| 17 | +describe('Kratos client wrapper E2E', () => { |
| 18 | + let app: INestApplication; |
| 19 | + let oryFrontendService: OryFrontendService; |
| 20 | + let oryIdentityService: OryIdentitiesService; |
| 21 | + const dockerComposeFile = resolve(join(__dirname, 'docker-compose.yaml')); |
| 22 | + const route = '/Example'; |
| 23 | + |
| 24 | + const register = async ( |
| 25 | + email: string, |
| 26 | + password: string |
| 27 | + ): Promise<Identity> => { |
| 28 | + const { data: registrationFlow } = |
| 29 | + await oryFrontendService.createNativeRegistrationFlow(); |
| 30 | + |
| 31 | + const { data } = await oryFrontendService.updateRegistrationFlow({ |
| 32 | + flow: registrationFlow.id, |
| 33 | + updateRegistrationFlowBody: { |
| 34 | + traits: { email }, |
| 35 | + password, |
| 36 | + method: 'password', |
| 37 | + }, |
| 38 | + }); |
| 39 | + return data.identity; |
| 40 | + }; |
| 41 | + |
| 42 | + const login = async (email: string, password: string): Promise<string> => { |
| 43 | + const { data: loginFlow } = |
| 44 | + await oryFrontendService.createNativeLoginFlow(); |
| 45 | + |
| 46 | + const { data } = await oryFrontendService.updateLoginFlow({ |
| 47 | + flow: loginFlow.id, |
| 48 | + updateLoginFlowBody: { |
| 49 | + password, |
| 50 | + identifier: email, |
| 51 | + method: 'password', |
| 52 | + }, |
| 53 | + }); |
| 54 | + return data.session_token as string; |
| 55 | + }; |
| 56 | + |
| 57 | + const createOryUser = async ( |
| 58 | + email: string, |
| 59 | + password: string |
| 60 | + ): Promise<{ identity: Identity; sessionToken: string }> => { |
| 61 | + const identity = await register(email, password); |
| 62 | + const response = await oryIdentityService.getIdentity({ |
| 63 | + id: identity.id, |
| 64 | + }); |
| 65 | + expect(response.data.id).toEqual(identity.id); |
| 66 | + const sessionToken = await login(email, password); |
| 67 | + return { identity, sessionToken }; |
| 68 | + }; |
| 69 | + |
| 70 | + beforeAll(() => { |
| 71 | + execSync(`docker-compose -f ${dockerComposeFile} up -d --wait`, { |
| 72 | + stdio: 'ignore', |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + afterAll(() => { |
| 77 | + execSync(`docker-compose -f ${dockerComposeFile} down`, { |
| 78 | + stdio: 'ignore', |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + beforeEach(async () => { |
| 83 | + const module: TestingModule = await Test.createTestingModule({ |
| 84 | + imports: [ |
| 85 | + OryFrontendModule.forRoot({ |
| 86 | + basePath: 'http://localhost:44330', |
| 87 | + }), |
| 88 | + OryIdentitiesModule.forRootAsync({ |
| 89 | + useFactory: () => ({ |
| 90 | + basePath: 'http://localhost:44340', |
| 91 | + accessToken: '', |
| 92 | + }), |
| 93 | + }), |
| 94 | + ], |
| 95 | + providers: [ExampleService], |
| 96 | + controllers: [ExampleController], |
| 97 | + }).compile(); |
| 98 | + |
| 99 | + oryFrontendService = module.get(OryFrontendService); |
| 100 | + oryIdentityService = module.get(OryIdentitiesService); |
| 101 | + |
| 102 | + app = module.createNestApplication(); |
| 103 | + await app.init(); |
| 104 | + }); |
| 105 | + |
| 106 | + afterEach(() => { |
| 107 | + return app?.close(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should successfully authenticate user when it is registered in Ory Kratos', async () => { |
| 111 | + const password = randomBytes(8).toString('hex'); |
| 112 | + const email = `${randomBytes(8).toString('hex')}@example.com`; |
| 113 | + const { sessionToken } = await createOryUser(email, password); |
| 114 | + |
| 115 | + const { body } = await request(app.getHttpServer()) |
| 116 | + .get(route) |
| 117 | + .set({ |
| 118 | + Authorization: `Bearer ${sessionToken}`, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(body).toEqual({ message: 'OK' }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should fail to authenticate user when it is not registered in Ory Kratos', async () => { |
| 125 | + const { body } = await request(app.getHttpServer()) |
| 126 | + .get(route) |
| 127 | + .set({ |
| 128 | + Authorization: `Bearer ory_st_${randomBytes(8).toString('hex')}`, |
| 129 | + }); |
| 130 | + expect(body).toEqual({ |
| 131 | + error: 'Forbidden', |
| 132 | + message: 'Forbidden resource', |
| 133 | + statusCode: 403, |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments