mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-22 20:46:38 +08:00
chore(server): data mocking and seeding (#10864)
This commit is contained in:
@@ -3,7 +3,7 @@ import { randomUUID } from 'node:crypto';
|
||||
import { INestApplication, ModuleMetadata } from '@nestjs/common';
|
||||
import type { NestExpressApplication } from '@nestjs/platform-express';
|
||||
import { TestingModuleBuilder } from '@nestjs/testing';
|
||||
import { User } from '@prisma/client';
|
||||
import { PrismaClient, User } from '@prisma/client';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
|
||||
import supertest from 'supertest';
|
||||
@@ -11,6 +11,7 @@ import supertest from 'supertest';
|
||||
import { AFFiNELogger, ApplyType, GlobalExceptionFilter } from '../../base';
|
||||
import { AuthService } from '../../core/auth';
|
||||
import { UserModel } from '../../models';
|
||||
import { createFactory, MockedUser, MockUser, MockUserInput } from '../mocks';
|
||||
import { createTestingModule } from './testing-module';
|
||||
import { initTestingDB, TEST_LOG_LEVEL } from './utils';
|
||||
|
||||
@@ -80,6 +81,8 @@ export class TestingApp extends ApplyType<INestApplication>() {
|
||||
private currentUserCookie: string | null = null;
|
||||
private readonly userCookies: Set<string> = new Set();
|
||||
|
||||
readonly create!: ReturnType<typeof createFactory>;
|
||||
|
||||
[Symbol.asyncDispose](): Promise<void> {
|
||||
return this.close();
|
||||
}
|
||||
@@ -188,6 +191,9 @@ export class TestingApp extends ApplyType<INestApplication>() {
|
||||
return `test-${randomUUID()}@affine.pro`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use `create(MockUser)`
|
||||
*/
|
||||
async createUser(
|
||||
email?: string,
|
||||
override?: Partial<User>
|
||||
@@ -209,13 +215,22 @@ export class TestingApp extends ApplyType<INestApplication>() {
|
||||
return user as Omit<User, 'password'> & { password: string };
|
||||
}
|
||||
|
||||
async signup(email?: string, override?: Partial<User>) {
|
||||
/**
|
||||
* @deprecated use `signup`
|
||||
*/
|
||||
async signupV1(email?: string, override?: Partial<User>) {
|
||||
const user = await this.createUser(email ?? this.randomEmail(), override);
|
||||
await this.login(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
async login(user: TestUser) {
|
||||
async signup(overrides?: Partial<MockUserInput>) {
|
||||
const user = await this.create(MockUser, overrides);
|
||||
await this.login(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
async login(user: MockedUser) {
|
||||
await this.POST('/api/auth/sign-in')
|
||||
.send({
|
||||
email: user.email,
|
||||
@@ -263,6 +278,9 @@ export class TestingApp extends ApplyType<INestApplication>() {
|
||||
function makeTestingApp(app: INestApplication): TestingApp {
|
||||
const testingApp = new TestingApp();
|
||||
|
||||
// @ts-expect-error allow
|
||||
testingApp.create = createFactory(app.get(PrismaClient, { strict: false }));
|
||||
|
||||
return new Proxy(testingApp, {
|
||||
get(target, prop) {
|
||||
// @ts-expect-error override
|
||||
|
||||
@@ -6,12 +6,14 @@ import {
|
||||
TestingModule as BaseTestingModule,
|
||||
TestingModuleBuilder,
|
||||
} from '@nestjs/testing';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
import { AppModule, FunctionalityModules } from '../../app.module';
|
||||
import { AFFiNELogger, Runtime } from '../../base';
|
||||
import { GqlModule } from '../../base/graphql';
|
||||
import { AuthGuard, AuthModule } from '../../core/auth';
|
||||
import { ModelsModule } from '../../models';
|
||||
import { createFactory } from '../mocks';
|
||||
import { initTestingDB, TEST_LOG_LEVEL } from './utils';
|
||||
|
||||
interface TestingModuleMeatdata extends ModuleMetadata {
|
||||
@@ -20,6 +22,7 @@ interface TestingModuleMeatdata extends ModuleMetadata {
|
||||
|
||||
export interface TestingModule extends BaseTestingModule {
|
||||
initTestingDB(): Promise<void>;
|
||||
create: ReturnType<typeof createFactory>;
|
||||
[Symbol.asyncDispose](): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -91,6 +94,10 @@ export async function createTestingModule(
|
||||
await runtime.set('auth/password.min', 1);
|
||||
};
|
||||
|
||||
testingModule.create = createFactory(
|
||||
module.get(PrismaClient, { strict: false })
|
||||
);
|
||||
|
||||
testingModule[Symbol.asyncDispose] = async () => {
|
||||
await module.close();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user