refactor: auth jwt

This commit is contained in:
Innei
2022-06-10 23:28:38 +08:00
committed by
parent 0d67b0be4a
commit c1282412c6
20 changed files with 113 additions and 200 deletions

View File

@@ -1,11 +1,8 @@
import { JwtModule } from '@nestjs/jwt'
import { PassportModule } from '@nestjs/passport'
import { Test } from '@nestjs/testing'
import { getModelToken } from '~/transformers/model.transformer'
import { SECURITY } from '~/app.config'
import { AuthService } from '~/modules/auth/auth.service'
import { JwtStrategy } from '~/modules/auth/jwt.strategy'
import { UserModel } from '~/modules/user/user.model'
import { getModelToken } from '~/transformers/model.transformer'
describe('Test AuthService', () => {
let service: AuthService
@@ -18,24 +15,8 @@ describe('Test AuthService', () => {
authCode: 'authCode',
}
beforeAll(async () => {
const __secret: any = SECURITY.jwtSecret || 'asjhczxiucipoiopiqm2376'
const jwtModule = JwtModule.registerAsync({
useFactory() {
return {
secret: __secret,
signOptions: {
expiresIn: SECURITY.jwtExpire,
algorithm: 'HS256',
},
}
},
})
const moduleRef = Test.createTestingModule({
imports: [jwtModule, PassportModule],
providers: [
JwtStrategy,
AuthService,
{
provide: getModelToken(UserModel.name),
@@ -55,17 +36,8 @@ describe('Test AuthService', () => {
service = app.get(AuthService)
})
it('should sign token', async () => {
const _token = await service.signToken('1')
it('should sign token', () => {
const _token = service.jwtServicePublic.sign('1')
expect(_token).toBeDefined()
})
it('should verifyied', async () => {
const user = await service.verifyPayload({
_id: '1',
authCode: 'authCode',
})
expect(user).toBeDefined()
expect(user).toEqual(mockUser)
})
})