feat(server): use user model (#9710)

This commit is contained in:
forehalo
2025-01-17 07:06:11 +00:00
parent a2d16f4b78
commit 44de4474c3
25 changed files with 179 additions and 521 deletions
@@ -8,12 +8,11 @@ import { MailService } from '../../base';
import { AuthModule, CurrentUser } from '../../core/auth';
import { AuthService } from '../../core/auth/service';
import { FeatureModule } from '../../core/features';
import { UserModule, UserService } from '../../core/user';
import { UserModule } from '../../core/user';
import { createTestingApp, getSession, sessionCookie } from '../utils';
const test = ava as TestFn<{
auth: AuthService;
user: UserService;
u1: CurrentUser;
db: PrismaClient;
mailer: Sinon.SinonStubbedInstance<MailService>;
@@ -31,7 +30,6 @@ test.before(async t => {
});
t.context.auth = app.get(AuthService);
t.context.user = app.get(UserService);
t.context.db = app.get(PrismaClient);
t.context.mailer = app.get(MailService);
t.context.app = app;
@@ -6,12 +6,13 @@ import { CurrentUser } from '../../core/auth';
import { AuthService } from '../../core/auth/service';
import { FeatureModule } from '../../core/features';
import { QuotaModule } from '../../core/quota';
import { UserModule, UserService } from '../../core/user';
import { UserModule } from '../../core/user';
import { Models } from '../../models';
import { createTestingModule, initTestingDB } from '../utils';
const test = ava as TestFn<{
auth: AuthService;
user: UserService;
models: Models;
u1: CurrentUser;
db: PrismaClient;
m: TestingModule;
@@ -24,7 +25,7 @@ test.before(async t => {
});
t.context.auth = m.get(AuthService);
t.context.user = m.get(UserService);
t.context.models = m.get(Models);
t.context.db = m.get(PrismaClient);
t.context.m = m;
});
@@ -55,9 +56,9 @@ test('should throw if user not found', async t => {
});
test('should throw if password not set', async t => {
const { user, auth } = t.context;
const { models, auth } = t.context;
await user.createUser({
await models.user.create({
email: 'u2@affine.pro',
name: 'u2',
});
@@ -11,7 +11,7 @@ import { URLHelper } from '../../base';
import { ConfigModule } from '../../base/config';
import { CurrentUser } from '../../core/auth';
import { AuthService } from '../../core/auth/service';
import { UserService } from '../../core/user';
import { Models } from '../../models';
import { OAuthProviderName } from '../../plugins/oauth/config';
import { GoogleOAuthProvider } from '../../plugins/oauth/providers/google';
import { OAuthService } from '../../plugins/oauth/service';
@@ -20,7 +20,7 @@ import { createTestingApp, getSession, initTestingDB } from '../utils';
const test = ava as TestFn<{
auth: AuthService;
oauth: OAuthService;
user: UserService;
models: Models;
u1: CurrentUser;
db: PrismaClient;
app: INestApplication;
@@ -47,7 +47,7 @@ test.before(async t => {
t.context.auth = app.get(AuthService);
t.context.oauth = app.get(OAuthService);
t.context.user = app.get(UserService);
t.context.models = app.get(Models);
t.context.db = app.get(PrismaClient);
t.context.app = app;
});
@@ -309,9 +309,9 @@ test('should not throw if account registered', async t => {
});
test('should be able to fullfil user with oauth sign in', async t => {
const { app, user, db } = t.context;
const { app, models, db } = t.context;
const u3 = await user.createUser({
const u3 = await models.user.create({
name: 'u3',
email: 'u3@affine.pro',
registered: false,
@@ -234,11 +234,7 @@ test.before(async t => {
test.beforeEach(async t => {
const { db, app, stripe } = t.context;
Sinon.reset();
await initTestingDB(db);
t.context.runtime.fetch
.withArgs('plugins.payment/showLifetimePrice')
.resolves(true);
t.context.u1 = await app.get(AuthService).signUp('u1@affine.pro', '1');
await db.workspace.create({
@@ -254,7 +250,13 @@ test.beforeEach(async t => {
},
});
Sinon.reset();
// default stubs
t.context.runtime.fetch
.withArgs('plugins.payment/showLifetimePrice')
.resolves(true);
// @ts-expect-error stub
stripe.prices.list.callsFake((params: Stripe.PriceListParams) => {
if (params.lookup_keys) {
@@ -7,7 +7,8 @@ import {
type CurrentUser,
} from '../../core/auth';
import { sessionUser } from '../../core/auth/service';
import { UserService, type UserType } from '../../core/user';
import { UserType } from '../../core/user';
import { Models } from '../../models';
import { gql } from './common';
export type UserAuthedType = UserType & { token: ClientTokenType };
@@ -52,7 +53,7 @@ export async function signUp(
password: string,
autoVerifyEmail = true
): Promise<UserAuthedType> {
const user = await app.get(UserService).createUser({
const user = await app.get(Models).user.create({
name,
email,
password,
@@ -13,7 +13,7 @@ import { GlobalExceptionFilter, Runtime } from '../../base';
import { GqlModule } from '../../base/graphql';
import { AuthGuard, AuthModule } from '../../core/auth';
import { UserFeaturesInit1698652531198 } from '../../data/migrations/1698652531198-user-features-init';
import { ModelModules } from '../../models';
import { ModelsModule } from '../../models';
export type PermissionEnum = 'Owner' | 'Admin' | 'Write' | 'Read';
@@ -80,7 +80,7 @@ export async function createTestingModule(
? [AppModule]
: dedupeModules([
...FunctionalityModules,
ModelModules,
ModelsModule,
AuthModule,
GqlModule,
...imports,
@@ -10,7 +10,7 @@ import ava from 'ava';
import { AppModule } from '../app.module';
import { MailService } from '../base/mailer';
import { AuthService } from '../core/auth/service';
import { UserService } from '../core/user';
import { Models } from '../models';
import {
acceptInviteById,
createTestingApp,
@@ -27,7 +27,7 @@ const test = ava as TestFn<{
client: PrismaClient;
auth: AuthService;
mail: MailService;
user: UserService;
models: Models;
}>;
test.beforeEach(async t => {
@@ -38,7 +38,7 @@ test.beforeEach(async t => {
t.context.client = app.get(PrismaClient);
t.context.auth = app.get(AuthService);
t.context.mail = app.get(MailService);
t.context.user = app.get(UserService);
t.context.models = app.get(Models);
});
test.afterEach.always(async t => {
@@ -87,14 +87,14 @@ test('should revoke a user', async t => {
});
test('should create user if not exist', async t => {
const { app, user } = t.context;
const { app, models } = t.context;
const u1 = await signUp(app, 'u1', 'u1@affine.pro', '1');
const workspace = await createWorkspace(app, u1.token.token);
await inviteUser(app, u1.token.token, workspace.id, 'u2@affine.pro');
const u2 = await user.findUserByEmail('u2@affine.pro');
const u2 = await models.user.getUserByEmail('u2@affine.pro');
t.not(u2, undefined, 'failed to create user');
t.is(u2?.name, 'u2', 'failed to create user');
});