refactor: auth jwt
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user