2023-06-29 04:45:45 +03:00
|
|
|
/// <reference types="../global.d.ts" />
|
2023-06-21 09:08:32 +03:00
|
|
|
import { ok } from 'node:assert';
|
2023-04-28 06:49:44 +03:00
|
|
|
import { beforeEach, test } from 'node:test';
|
|
|
|
|
|
|
|
import { Test } from '@nestjs/testing';
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
|
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 { AuthModule } from '../modules/auth';
|
|
|
|
import { AuthService } from '../modules/auth/service';
|
|
|
|
import { PrismaModule } from '../prisma';
|
|
|
|
|
|
|
|
let auth: AuthService;
|
|
|
|
|
|
|
|
// cleanup database before each test
|
|
|
|
beforeEach(async () => {
|
|
|
|
const client = new PrismaClient();
|
|
|
|
await client.$connect();
|
|
|
|
await client.user.deleteMany({});
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
const module = await Test.createTestingModule({
|
|
|
|
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,
|
|
|
|
],
|
|
|
|
}).compile();
|
|
|
|
auth = module.get(AuthService);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to register and signIn', async () => {
|
|
|
|
await auth.register('Alex Yang', 'alexyang@example.org', '123456');
|
|
|
|
await auth.signIn('alexyang@example.org', '123456');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to verify', async () => {
|
|
|
|
await auth.register('Alex Yang', 'alexyang@example.org', '123456');
|
|
|
|
await auth.signIn('alexyang@example.org', '123456');
|
|
|
|
const user = {
|
|
|
|
id: '1',
|
|
|
|
name: 'Alex Yang',
|
|
|
|
email: 'alexyang@example.org',
|
2023-06-21 09:08:32 +03:00
|
|
|
createdAt: new Date(),
|
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);
|
|
|
|
ok(claim.id === '1');
|
|
|
|
ok(claim.name === 'Alex Yang');
|
|
|
|
ok(claim.email === 'alexyang@example.org');
|
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);
|
|
|
|
ok(claim.id === '1');
|
|
|
|
ok(claim.name === 'Alex Yang');
|
|
|
|
ok(claim.email === 'alexyang@example.org');
|
2023-04-28 06:49:44 +03:00
|
|
|
}
|
|
|
|
});
|