2023-06-29 04:45:45 +03:00
|
|
|
/// <reference types="../global.d.ts" />
|
2023-08-29 13:07:05 +03:00
|
|
|
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';
|
2023-08-29 13:07:05 +03:00
|
|
|
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;
|
2023-08-29 13:07:05 +03:00
|
|
|
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 () => {
|
2023-08-29 13:07:05 +03:00
|
|
|
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,
|
2023-08-29 13:07:05 +03:00
|
|
|
MetricsModule,
|
2023-08-31 15:29:25 +03:00
|
|
|
RateLimiterModule,
|
2023-04-28 06:49:44 +03:00
|
|
|
],
|
|
|
|
}).compile();
|
|
|
|
auth = module.get(AuthService);
|
|
|
|
});
|
|
|
|
|
2023-09-11 12:30:39 +03:00
|
|
|
test.afterEach.always(async () => {
|
2023-08-29 13:07:05 +03:00
|
|
|
await module.close();
|
|
|
|
});
|
|
|
|
|
2023-09-01 22:41:29 +03:00
|
|
|
test('should be able to register and signIn', async t => {
|
2023-08-29 13:07:05 +03:00
|
|
|
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 => {
|
2023-08-29 13:07:05 +03:00
|
|
|
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-08-29 13:07:05 +03:00
|
|
|
const date = new Date();
|
|
|
|
|
2023-04-28 06:49:44 +03:00
|
|
|
const user = {
|
|
|
|
id: '1',
|
|
|
|
name: 'Alex Yang',
|
|
|
|
email: 'alexyang@example.org',
|
2023-08-29 13:07:05 +03:00
|
|
|
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);
|
2023-09-05 11:01:45 +03:00
|
|
|
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);
|
2023-09-05 11:01:45 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|