AFFiNE/apps/server/src/tests/auth.spec.ts

87 lines
2.4 KiB
TypeScript
Raw Normal View History

/// <reference types="../global.d.ts" />
import { Test, TestingModule } from '@nestjs/testing';
2023-04-28 06:49:44 +03:00
import { PrismaClient } from '@prisma/client';
2023-09-01 22:41:29 +03:00
import test from 'ava';
2023-04-28 06:49:44 +03:00
2023-04-28 08:41:06 +03:00
import { ConfigModule } from '../config';
2023-04-28 06:49:44 +03:00
import { GqlModule } from '../graphql.module';
import { MetricsModule } from '../metrics';
2023-04-28 06:49:44 +03:00
import { AuthModule } from '../modules/auth';
import { AuthService } from '../modules/auth/service';
import { PrismaModule } from '../prisma';
2023-08-31 15:29:25 +03:00
import { RateLimiterModule } from '../throttler';
2023-04-28 06:49:44 +03:00
let auth: AuthService;
let module: TestingModule;
2023-04-28 06:49:44 +03:00
// cleanup database before each test
2023-09-01 22:41:29 +03:00
test.beforeEach(async () => {
2023-04-28 06:49:44 +03:00
const client = new PrismaClient();
await client.$connect();
await client.user.deleteMany({});
2023-09-07 23:55:04 +03:00
await client.$disconnect();
2023-04-28 06:49:44 +03:00
});
2023-09-01 22:41:29 +03:00
test.beforeEach(async () => {
module = await Test.createTestingModule({
2023-04-28 06:49:44 +03:00
imports: [
ConfigModule.forRoot({
auth: {
2023-06-21 09:08:32 +03:00
accessTokenExpiresIn: 1,
refreshTokenExpiresIn: 1,
leeway: 1,
2023-04-28 06:49:44 +03:00
},
}),
PrismaModule,
GqlModule,
AuthModule,
MetricsModule,
2023-08-31 15:29:25 +03:00
RateLimiterModule,
2023-04-28 06:49:44 +03:00
],
}).compile();
auth = module.get(AuthService);
});
test.afterEach.always(async () => {
await module.close();
});
2023-09-01 22:41:29 +03:00
test('should be able to register and signIn', async t => {
await auth.signUp('Alex Yang', 'alexyang@example.org', '123456');
2023-04-28 06:49:44 +03:00
await auth.signIn('alexyang@example.org', '123456');
2023-09-01 22:41:29 +03:00
t.pass();
2023-04-28 06:49:44 +03:00
});
2023-09-01 22:41:29 +03:00
test('should be able to verify', async t => {
await auth.signUp('Alex Yang', 'alexyang@example.org', '123456');
2023-04-28 06:49:44 +03:00
await auth.signIn('alexyang@example.org', '123456');
const date = new Date();
2023-04-28 06:49:44 +03:00
const user = {
id: '1',
name: 'Alex Yang',
email: 'alexyang@example.org',
emailVerified: date,
createdAt: date,
avatarUrl: '',
2023-04-28 06:49:44 +03:00
};
{
2023-06-21 09:08:32 +03:00
const token = await auth.sign(user);
const claim = await auth.verify(token);
t.is(claim.id, '1');
t.is(claim.name, 'Alex Yang');
t.is(claim.email, 'alexyang@example.org');
t.is(claim.emailVerified?.toISOString(), date.toISOString());
t.is(claim.createdAt.toISOString(), date.toISOString());
2023-04-28 06:49:44 +03:00
}
{
2023-06-21 09:08:32 +03:00
const token = await auth.refresh(user);
const claim = await auth.verify(token);
t.is(claim.id, '1');
t.is(claim.name, 'Alex Yang');
t.is(claim.email, 'alexyang@example.org');
t.is(claim.emailVerified?.toISOString(), date.toISOString());
t.is(claim.createdAt.toISOString(), date.toISOString());
2023-04-28 06:49:44 +03:00
}
});