refactor: move mailer test (#4328)

This commit is contained in:
Alex Yang
2023-09-12 22:11:19 -07:00
committed by GitHub
parent bdc2695caf
commit 74faee887e
8 changed files with 29 additions and 42 deletions
+1
View File
@@ -74,6 +74,7 @@
"yjs": "^13.6.7" "yjs": "^13.6.7"
}, },
"devDependencies": { "devDependencies": {
"@affine-test/kit": "workspace:*",
"@affine/storage": "workspace:*", "@affine/storage": "workspace:*",
"@napi-rs/image": "^1.6.1", "@napi-rs/image": "^1.6.1",
"@nestjs/testing": "^10.2.4", "@nestjs/testing": "^10.2.4",
+4 -1
View File
@@ -2,6 +2,10 @@
// This test case is for testing the mailer service. // This test case is for testing the mailer service.
// Please use local SMTP server for testing. // Please use local SMTP server for testing.
// See: https://github.com/mailhog/MailHog // See: https://github.com/mailhog/MailHog
import {
getCurrentMailMessageCount,
getLatestMailMessage,
} from '@affine-test/kit/utils/cloud';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import test from 'ava'; import test from 'ava';
@@ -13,7 +17,6 @@ import { AuthModule } from '../modules/auth';
import { AuthService } from '../modules/auth/service'; import { AuthService } from '../modules/auth/service';
import { PrismaModule } from '../prisma'; import { PrismaModule } from '../prisma';
import { RateLimiterModule } from '../throttler'; import { RateLimiterModule } from '../throttler';
import { getCurrentMailMessageCount, getLatestMailMessage } from './utils';
let auth: AuthService; let auth: AuthService;
let module: TestingModule; let module: TestingModule;
+3 -33
View File
@@ -1,32 +1,16 @@
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
import type { INestApplication } from '@nestjs/common'; import type { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { hashSync } from '@node-rs/argon2'; import { hashSync } from '@node-rs/argon2';
import { PrismaClient, User } from '@prisma/client'; import { PrismaClient, User } from '@prisma/client';
// @ts-expect-error graphql-upload is not typed
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.mjs';
import request from 'supertest'; import request from 'supertest';
import { AppModule } from '../app';
import type { TokenType } from '../modules/auth'; import type { TokenType } from '../modules/auth';
import type { UserType } from '../modules/users'; import type { UserType } from '../modules/users';
import type { InvitationType, WorkspaceType } from '../modules/workspaces'; import type { InvitationType, WorkspaceType } from '../modules/workspaces';
const gql = '/graphql'; const gql = '/graphql';
export async function getCurrentMailMessageCount() {
const response = await fetch('http://localhost:8025/api/v2/messages');
const data = await response.json();
return data.total;
}
export async function getLatestMailMessage() {
const response = await fetch('http://localhost:8025/api/v2/messages');
const data = await response.json();
return data.items[0];
}
async function signUp( async function signUp(
app: INestApplication, app: INestApplication,
name: string, name: string,
@@ -444,7 +428,8 @@ async function flushDB() {
const result: { tablename: string }[] = const result: { tablename: string }[] =
await client.$queryRaw`SELECT tablename await client.$queryRaw`SELECT tablename
FROM pg_catalog.pg_tables FROM pg_catalog.pg_tables
WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'`; WHERE schemaname != 'pg_catalog'
AND schemaname != 'information_schema'`;
// remove all table data // remove all table data
await client.$executeRawUnsafe( await client.$executeRawUnsafe(
@@ -457,21 +442,6 @@ async function flushDB() {
await client.$disconnect(); await client.$disconnect();
} }
async function createTestApp() {
const module = await Test.createTestingModule({
imports: [AppModule],
}).compile();
const app = module.createNestApplication();
app.use(
graphqlUploadExpress({
maxFileSize: 10 * 1024 * 1024,
maxFiles: 5,
})
);
await app.init();
return app;
}
async function getInviteInfo( async function getInviteInfo(
app: INestApplication, app: INestApplication,
token: string, token: string,
@@ -513,6 +483,7 @@ export class FakePrisma {
emailVerified: new Date(), emailVerified: new Date(),
createdAt: new Date(), createdAt: new Date(),
}; };
get user() { get user() {
// eslint-disable-next-line @typescript-eslint/no-this-alias // eslint-disable-next-line @typescript-eslint/no-this-alias
const prisma = this; const prisma = this;
@@ -536,7 +507,6 @@ export {
checkBlobSize, checkBlobSize,
collectAllBlobSizes, collectAllBlobSizes,
collectBlobSizes, collectBlobSizes,
createTestApp,
createWorkspace, createWorkspace,
currentUser, currentUser,
flushDB, flushDB,
@@ -1,3 +1,7 @@
import {
getCurrentMailMessageCount,
getLatestMailMessage,
} from '@affine-test/kit/utils/cloud';
import type { INestApplication } from '@nestjs/common'; import type { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing'; import { Test } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
@@ -12,8 +16,6 @@ import {
acceptInvite, acceptInvite,
acceptInviteById, acceptInviteById,
createWorkspace, createWorkspace,
getCurrentMailMessageCount,
getLatestMailMessage,
getWorkspace, getWorkspace,
inviteUser, inviteUser,
leaveWorkspace, leaveWorkspace,
+3
View File
@@ -13,6 +13,9 @@
"references": [ "references": [
{ {
"path": "../../tests/fixtures" "path": "../../tests/fixtures"
},
{
"path": "../../tests/kit"
} }
], ],
"include": ["scripts", "package.json"] "include": ["scripts", "package.json"]
+1 -6
View File
@@ -5,10 +5,5 @@
"noEmit": false, "noEmit": false,
"outDir": "lib" "outDir": "lib"
}, },
"include": ["./*.ts", "utils"], "include": ["./*.ts", "utils"]
"references": [
{
"path": "../../apps/server"
}
]
} }
+12
View File
@@ -12,6 +12,18 @@ import { hash } from '@node-rs/argon2';
import type { BrowserContext, Cookie, Page } from '@playwright/test'; import type { BrowserContext, Cookie, Page } from '@playwright/test';
import { z } from 'zod'; import { z } from 'zod';
export async function getCurrentMailMessageCount() {
const response = await fetch('http://localhost:8025/api/v2/messages');
const data = await response.json();
return data.total;
}
export async function getLatestMailMessage() {
const response = await fetch('http://localhost:8025/api/v2/messages');
const data = await response.json();
return data.items[0];
}
export async function getLoginCookie( export async function getLoginCookie(
context: BrowserContext context: BrowserContext
): Promise<Cookie | undefined> { ): Promise<Cookie | undefined> {
+1
View File
@@ -653,6 +653,7 @@ __metadata:
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "@affine/server@workspace:apps/server" resolution: "@affine/server@workspace:apps/server"
dependencies: dependencies:
"@affine-test/kit": "workspace:*"
"@affine/storage": "workspace:*" "@affine/storage": "workspace:*"
"@apollo/server": ^4.9.3 "@apollo/server": ^4.9.3
"@auth/prisma-adapter": ^1.0.1 "@auth/prisma-adapter": ^1.0.1