mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-11 20:08:37 +00:00
refactor(server): import prisma from @prisma/client (#5863)
This commit is contained in:
@@ -1,126 +1,11 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
import type { INestApplication } from '@nestjs/common';
|
||||
import { hashSync } from '@node-rs/argon2';
|
||||
import { type User } from '@prisma/client';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import ava, { type TestFn } from 'ava';
|
||||
import Sinon from 'sinon';
|
||||
|
||||
import { AppModule } from '../src/app.module';
|
||||
import { FeatureKind, FeatureManagementService } from '../src/core/features';
|
||||
import { Quotas } from '../src/core/quota';
|
||||
import { FeatureManagementService } from '../src/core/features';
|
||||
import { MailService } from '../src/fundamentals/mailer';
|
||||
import {
|
||||
createTestingApp,
|
||||
createWorkspace,
|
||||
getInviteInfo,
|
||||
inviteUser,
|
||||
signUp,
|
||||
} from './utils';
|
||||
|
||||
const FakePrisma = {
|
||||
fakeUser: {
|
||||
id: randomUUID(),
|
||||
name: 'Alex Yang',
|
||||
avatarUrl: '',
|
||||
email: 'alex.yang@example.org',
|
||||
password: hashSync('123456'),
|
||||
emailVerified: new Date(),
|
||||
createdAt: new Date(),
|
||||
} satisfies User,
|
||||
|
||||
get user() {
|
||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||
const prisma = this;
|
||||
return {
|
||||
async findFirst() {
|
||||
return null;
|
||||
},
|
||||
async create({ data }: any) {
|
||||
return {
|
||||
...prisma.fakeUser,
|
||||
...data,
|
||||
};
|
||||
},
|
||||
async findUnique() {
|
||||
return prisma.fakeUser;
|
||||
},
|
||||
};
|
||||
},
|
||||
get workspace() {
|
||||
return {
|
||||
id: randomUUID(),
|
||||
async create({ data }: any) {
|
||||
return {
|
||||
id: this.id,
|
||||
public: data.public ?? false,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
snapshot: {
|
||||
id: randomUUID(),
|
||||
async create() {},
|
||||
async findFirstOrThrow() {
|
||||
return { id: this.id, blob: Buffer.from([0, 0]) };
|
||||
},
|
||||
},
|
||||
get workspaceUserPermission() {
|
||||
return {
|
||||
id: randomUUID(),
|
||||
prisma: this,
|
||||
async count() {
|
||||
return 1;
|
||||
},
|
||||
async create() {
|
||||
return { id: this.id };
|
||||
},
|
||||
async findUniqueOrThrow() {
|
||||
return { id: this.id, workspaceId: this.prisma.workspace.id };
|
||||
},
|
||||
async findFirst() {
|
||||
return { id: this.id };
|
||||
},
|
||||
async findFirstOrThrow() {
|
||||
return { id: this.id, user: this.prisma.fakeUser };
|
||||
},
|
||||
async workspaceUserPermission() {
|
||||
return {
|
||||
id: randomUUID(),
|
||||
createdAt: new Date(),
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
get features() {
|
||||
return {
|
||||
async findFirst() {
|
||||
return {
|
||||
id: 1,
|
||||
type: FeatureKind.Quota,
|
||||
feature: Quotas[0].feature,
|
||||
configs: Quotas[0].configs,
|
||||
version: Quotas[0].version,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
get userFeatures() {
|
||||
return {
|
||||
async findFirst() {
|
||||
return {
|
||||
createdAt: new Date(),
|
||||
featureId: 1,
|
||||
reason: '',
|
||||
expiredAt: null,
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
import { createTestingApp, createWorkspace, inviteUser, signUp } from './utils';
|
||||
const test = ava as TestFn<{
|
||||
app: INestApplication;
|
||||
mail: MailService;
|
||||
@@ -130,15 +15,11 @@ test.beforeEach(async t => {
|
||||
const { module, app } = await createTestingApp({
|
||||
imports: [AppModule],
|
||||
tapModule: module => {
|
||||
module
|
||||
.overrideProvider(PrismaClient)
|
||||
.useValue(FakePrisma)
|
||||
.overrideProvider(FeatureManagementService)
|
||||
.useValue({
|
||||
hasWorkspaceFeature() {
|
||||
return false;
|
||||
},
|
||||
});
|
||||
module.overrideProvider(FeatureManagementService).useValue({
|
||||
hasWorkspaceFeature() {
|
||||
return false;
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -153,38 +34,34 @@ test.afterEach.always(async t => {
|
||||
|
||||
test('should send invite email', async t => {
|
||||
const { mail, app } = t.context;
|
||||
|
||||
if (mail.hasConfigured()) {
|
||||
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
|
||||
const u2 = await signUp(app, 'u2', 'u2@affine.pro', '1');
|
||||
|
||||
const workspace = await createWorkspace(app, u1.token.token);
|
||||
const inviteId = await inviteUser(
|
||||
|
||||
const stub = Sinon.stub(mail, 'sendMail');
|
||||
|
||||
await inviteUser(
|
||||
app,
|
||||
u1.token.token,
|
||||
workspace.id,
|
||||
u2.email,
|
||||
'Admin'
|
||||
'Admin',
|
||||
true
|
||||
);
|
||||
|
||||
const inviteInfo = await getInviteInfo(app, u1.token.token, inviteId);
|
||||
t.true(stub.calledOnce);
|
||||
|
||||
const resp = await mail.sendInviteEmail(
|
||||
'production@toeverything.info',
|
||||
inviteId,
|
||||
{
|
||||
workspace: {
|
||||
id: inviteInfo.workspace.id,
|
||||
name: inviteInfo.workspace.name,
|
||||
avatar: '',
|
||||
},
|
||||
user: {
|
||||
avatar: inviteInfo.user?.avatarUrl || '',
|
||||
name: inviteInfo.user?.name || '',
|
||||
},
|
||||
}
|
||||
const args = stub.args[0][0];
|
||||
|
||||
t.is(args.to, u2.email);
|
||||
t.true(
|
||||
args.subject!.startsWith(
|
||||
`${u1.name} invited you to join` /* we don't know the name of mocked workspace */
|
||||
)
|
||||
);
|
||||
|
||||
t.is(resp.accepted.length, 1, 'failed to send invite email');
|
||||
}
|
||||
t.pass();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user