74 lines
2.4 KiB
TypeScript
74 lines
2.4 KiB
TypeScript
import 'reflect-metadata';
|
|
import { testDatabase } from './test-database';
|
|
import { Test } from '@nestjs/testing';
|
|
import request from 'supertest';
|
|
import { AppModule } from '../../src/app.module';
|
|
import { ENVIRONMENT } from '../../src/config/environment.module';
|
|
import { parseEnvironment } from '../../src/config/environment';
|
|
import { DatabaseService } from '../../src/database/database.service';
|
|
import { BootstrapService } from '../../src/identity/bootstrap.service';
|
|
import { RecoveryMailer } from '../../src/identity/recovery-mailer';
|
|
import { configureApp } from '../../src/configure-app';
|
|
import { configureSwagger } from '../../src/documentation/configure-swagger';
|
|
|
|
export const ownerPassword = 'correct horse battery staple';
|
|
export async function identityApp(swagger = false) {
|
|
const database = await testDatabase();
|
|
const env = parseEnvironment({
|
|
DATABASE_URL: database.connectionUrl,
|
|
NODE_ENV: 'test',
|
|
DATABASE_POOL_SIZE: process.env.TEST_DATABASE_URL ? '10' : '1',
|
|
});
|
|
const mailer = {
|
|
assertConfigured: jest.fn(),
|
|
send: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
const module = await Test.createTestingModule({ imports: [AppModule] })
|
|
.overrideProvider(ENVIRONMENT)
|
|
.useValue(env)
|
|
.overrideProvider(RecoveryMailer)
|
|
.useValue(mailer)
|
|
.compile();
|
|
const app = module.createNestApplication();
|
|
app.useLogger(false);
|
|
configureApp(app, env);
|
|
if (swagger) configureSwagger(app, { ...env, SWAGGER_ENABLED: true });
|
|
await app.init();
|
|
const db = app.get(DatabaseService);
|
|
const owner = await app.get(BootstrapService).createOwner('Mani Candles', {
|
|
email: 'owner@example.com',
|
|
name: 'Owner',
|
|
password: ownerPassword,
|
|
});
|
|
const api = () => request(app.getHttpServer());
|
|
const login = async (
|
|
email = 'owner@example.com',
|
|
password = ownerPassword,
|
|
organizationId = owner.organizationId,
|
|
) =>
|
|
api().post('/api/v1/auth/login').send({ organizationId, email, password });
|
|
const response = await login();
|
|
if (response.status !== 200)
|
|
throw new Error(`Fixture login failed: ${response.status}`);
|
|
const token = response.body.accessToken as string;
|
|
return {
|
|
app,
|
|
db,
|
|
|
|
executeSql: database.execute,
|
|
owner,
|
|
token,
|
|
api,
|
|
login,
|
|
mailer,
|
|
async clearLimits() {
|
|
await db.rateLimit.deleteMany();
|
|
},
|
|
async close() {
|
|
await app.close();
|
|
await database.close();
|
|
},
|
|
};
|
|
}
|
|
export type IdentityApp = Awaited<ReturnType<typeof identityApp>>;
|