2024-01-11 09:40:55 +03:00
|
|
|
import { INestApplication, ModuleMetadata } from '@nestjs/common';
|
2024-03-12 13:00:09 +03:00
|
|
|
import { APP_GUARD } from '@nestjs/core';
|
2024-01-11 09:40:55 +03:00
|
|
|
import { Query, Resolver } from '@nestjs/graphql';
|
|
|
|
import { Test, TestingModuleBuilder } from '@nestjs/testing';
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
2024-03-12 13:00:09 +03:00
|
|
|
import cookieParser from 'cookie-parser';
|
2024-01-11 09:40:55 +03:00
|
|
|
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
|
2024-04-26 12:43:35 +03:00
|
|
|
import type { Response } from 'supertest';
|
2024-06-20 15:25:10 +03:00
|
|
|
import supertest from 'supertest';
|
2023-12-14 12:50:51 +03:00
|
|
|
|
2024-01-14 08:47:56 +03:00
|
|
|
import { AppModule, FunctionalityModules } from '../../src/app.module';
|
2024-03-12 13:00:09 +03:00
|
|
|
import { AuthGuard, AuthModule } from '../../src/core/auth';
|
2024-01-11 09:40:55 +03:00
|
|
|
import { UserFeaturesInit1698652531198 } from '../../src/data/migrations/1698652531198-user-features-init';
|
2024-07-23 13:39:33 +03:00
|
|
|
import { Config, GlobalExceptionFilter } from '../../src/fundamentals';
|
2024-01-12 07:18:39 +03:00
|
|
|
import { GqlModule } from '../../src/fundamentals/graphql';
|
2023-12-14 12:50:51 +03:00
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
async function flushDB(client: PrismaClient) {
|
2023-12-14 12:50:51 +03:00
|
|
|
const result: { tablename: string }[] =
|
|
|
|
await client.$queryRaw`SELECT tablename
|
|
|
|
FROM pg_catalog.pg_tables
|
|
|
|
WHERE schemaname != 'pg_catalog'
|
|
|
|
AND schemaname != 'information_schema'`;
|
|
|
|
|
|
|
|
// remove all table data
|
|
|
|
await client.$executeRawUnsafe(
|
|
|
|
`TRUNCATE TABLE ${result
|
|
|
|
.map(({ tablename }) => tablename)
|
|
|
|
.filter(name => !name.includes('migrations'))
|
|
|
|
.join(', ')}`
|
|
|
|
);
|
2024-01-11 09:40:55 +03:00
|
|
|
}
|
2023-12-14 12:50:51 +03:00
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
async function initFeatureConfigs(db: PrismaClient) {
|
|
|
|
await UserFeaturesInit1698652531198.up(db);
|
2023-12-14 12:50:51 +03:00
|
|
|
}
|
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
export async function initTestingDB(db: PrismaClient) {
|
|
|
|
await flushDB(db);
|
|
|
|
await initFeatureConfigs(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TestingModuleMeatdata extends ModuleMetadata {
|
|
|
|
tapModule?(m: TestingModuleBuilder): void;
|
|
|
|
tapApp?(app: INestApplication): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dedupeModules(modules: NonNullable<ModuleMetadata['imports']>) {
|
|
|
|
const map = new Map();
|
2023-12-14 12:50:51 +03:00
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
modules.forEach(m => {
|
|
|
|
if ('module' in m) {
|
|
|
|
map.set(m.module, m);
|
|
|
|
} else {
|
|
|
|
map.set(m, m);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Array.from(map.values());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Resolver(() => String)
|
|
|
|
class MockResolver {
|
|
|
|
@Query(() => String)
|
|
|
|
hello() {
|
|
|
|
return 'hello world';
|
2023-12-14 12:50:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
export async function createTestingModule(
|
2024-07-23 13:39:33 +03:00
|
|
|
moduleDef: TestingModuleMeatdata = {},
|
|
|
|
init = true
|
2024-01-11 09:40:55 +03:00
|
|
|
) {
|
|
|
|
// setting up
|
|
|
|
let imports = moduleDef.imports ?? [];
|
|
|
|
imports =
|
|
|
|
imports[0] === AppModule
|
|
|
|
? [AppModule]
|
2024-01-22 10:40:28 +03:00
|
|
|
: dedupeModules([
|
|
|
|
...FunctionalityModules,
|
|
|
|
AuthModule,
|
|
|
|
GqlModule,
|
|
|
|
...imports,
|
|
|
|
]);
|
2024-01-11 09:40:55 +03:00
|
|
|
|
|
|
|
const builder = Test.createTestingModule({
|
|
|
|
imports,
|
2024-03-12 13:00:09 +03:00
|
|
|
providers: [
|
|
|
|
{
|
|
|
|
provide: APP_GUARD,
|
|
|
|
useClass: AuthGuard,
|
|
|
|
},
|
|
|
|
MockResolver,
|
|
|
|
...(moduleDef.providers ?? []),
|
|
|
|
],
|
2024-01-11 09:40:55 +03:00
|
|
|
controllers: moduleDef.controllers,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (moduleDef.tapModule) {
|
|
|
|
moduleDef.tapModule(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
const m = await builder.compile();
|
|
|
|
|
|
|
|
const prisma = m.get(PrismaClient);
|
|
|
|
if (prisma instanceof PrismaClient) {
|
2024-05-28 09:43:53 +03:00
|
|
|
await initTestingDB(prisma);
|
2024-01-11 09:40:55 +03:00
|
|
|
}
|
|
|
|
|
2024-07-23 13:39:33 +03:00
|
|
|
if (init) {
|
|
|
|
await m.init();
|
|
|
|
|
|
|
|
const config = m.get(Config);
|
|
|
|
// by pass password min length validation
|
|
|
|
await config.runtime.set('auth/password.min', 1);
|
|
|
|
}
|
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createTestingApp(moduleDef: TestingModuleMeatdata = {}) {
|
2024-07-23 13:39:33 +03:00
|
|
|
const m = await createTestingModule(moduleDef, false);
|
2024-01-11 09:40:55 +03:00
|
|
|
|
|
|
|
const app = m.createNestApplication({
|
|
|
|
cors: true,
|
|
|
|
bodyParser: true,
|
|
|
|
rawBody: true,
|
2024-03-26 05:24:17 +03:00
|
|
|
logger: ['warn'],
|
2024-01-11 09:40:55 +03:00
|
|
|
});
|
|
|
|
|
2024-06-17 06:30:58 +03:00
|
|
|
app.useGlobalFilters(new GlobalExceptionFilter(app.getHttpAdapter()));
|
2024-01-11 09:40:55 +03:00
|
|
|
app.use(
|
|
|
|
graphqlUploadExpress({
|
|
|
|
maxFileSize: 10 * 1024 * 1024,
|
|
|
|
maxFiles: 5,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2024-03-12 13:00:09 +03:00
|
|
|
app.use(cookieParser());
|
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
if (moduleDef.tapApp) {
|
|
|
|
moduleDef.tapApp(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
await app.init();
|
|
|
|
|
2024-07-23 13:39:33 +03:00
|
|
|
const config = app.get(Config);
|
|
|
|
// by pass password min length validation
|
|
|
|
await config.runtime.set('auth/password.min', 1);
|
|
|
|
|
2024-01-11 09:40:55 +03:00
|
|
|
return {
|
|
|
|
module: m,
|
|
|
|
app,
|
|
|
|
};
|
2023-12-14 12:50:51 +03:00
|
|
|
}
|
2024-04-26 12:43:35 +03:00
|
|
|
|
|
|
|
export function handleGraphQLError(resp: Response) {
|
|
|
|
const { errors } = resp.body;
|
|
|
|
if (errors) {
|
|
|
|
const cause = errors[0];
|
|
|
|
const stacktrace = cause.extensions?.stacktrace;
|
2024-07-03 07:49:19 +03:00
|
|
|
throw new Error(
|
|
|
|
stacktrace
|
|
|
|
? Array.isArray(stacktrace)
|
|
|
|
? stacktrace.join('\n')
|
|
|
|
: String(stacktrace)
|
|
|
|
: cause.message,
|
|
|
|
cause
|
|
|
|
);
|
2024-04-26 12:43:35 +03:00
|
|
|
}
|
|
|
|
}
|
2024-06-20 15:25:10 +03:00
|
|
|
|
|
|
|
export function gql(app: INestApplication, query?: string) {
|
|
|
|
const req = supertest(app.getHttpServer())
|
|
|
|
.post('/graphql')
|
|
|
|
.set({ 'x-request-id': 'test', 'x-operation-name': 'test' });
|
|
|
|
|
|
|
|
if (query) {
|
|
|
|
return req.send({ query });
|
|
|
|
}
|
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
2024-07-18 06:08:47 +03:00
|
|
|
|
|
|
|
export async function sleep(ms: number) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|