-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# THIS ENV FILE EXAMPLE ONLY FOR DOCKER COMPOSE | ||
# SEE https://docs.docker.com/compose/environment-variables/#the-env-file | ||
JWT_SECRET=asffasgvxczfqreqw213 | ||
ALLOWED_ORIGINS=https://innei.ren,https://www.innei.ren |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { JwtModule } from '@nestjs/jwt' | ||
import { PassportModule } from '@nestjs/passport' | ||
import { Test } from '@nestjs/testing' | ||
import { getModelToken } from 'nestjs-typegoose' | ||
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' | ||
|
||
describe('Test AuthService', () => { | ||
let service: AuthService | ||
|
||
const mockUser = { | ||
_id: '1', | ||
id: '1', | ||
username: 'test-user', | ||
email: '[email protected]', | ||
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), | ||
useValue: { | ||
findById: jest.fn().mockReturnValue({ | ||
select: jest.fn().mockResolvedValue({ | ||
...mockUser, | ||
}), | ||
}), | ||
}, | ||
}, | ||
], | ||
}) | ||
|
||
const app = await moduleRef.compile() | ||
await app.init() | ||
service = app.get(AuthService) | ||
}) | ||
|
||
it('should sign token', async () => { | ||
const _token = await service.signToken('1') | ||
expect(_token).toBeDefined() | ||
}) | ||
|
||
it('should verifyied', async () => { | ||
const user = await service.verifyPayload({ | ||
_id: '1', | ||
authCode: 'authCode', | ||
}) | ||
expect(user).toBeDefined() | ||
expect(user).toEqual(mockUser) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { NestFastifyApplication } from '@nestjs/platform-fastify' | ||
import { Test } from '@nestjs/testing' | ||
import { hashSync } from 'bcrypt' | ||
import { getModelToken } from 'nestjs-typegoose' | ||
import { fastifyApp } from '~/common/adapt/fastify' | ||
import { AuthService } from '~/modules/auth/auth.service' | ||
import { UserController } from '~/modules/user/user.controller' | ||
import { UserModel } from '~/modules/user/user.model' | ||
import { UserService } from '~/modules/user/user.service' | ||
|
||
describe('AppController (e2e)', () => { | ||
let app: NestFastifyApplication | ||
|
||
beforeAll(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
controllers: [UserController], | ||
providers: [ | ||
{ | ||
provide: AuthService, | ||
useValue: { | ||
signToken() { | ||
return '' | ||
}, | ||
}, | ||
}, | ||
{ | ||
provide: UserService, | ||
useValue: { | ||
login() { | ||
return { | ||
username: 'test', | ||
avatar: '', | ||
email: '[email protected]', | ||
} | ||
}, | ||
recordFootstep() { | ||
return {} | ||
}, | ||
}, | ||
}, | ||
{ | ||
provide: getModelToken(UserModel.name), | ||
useValue: { | ||
findOne: jest.fn().mockImplementationOnce(() => ({ | ||
select: jest.fn().mockResolvedValueOnce({ | ||
username: 'test', | ||
password: hashSync('pwd', 2), | ||
}), | ||
})), | ||
}, | ||
}, | ||
], | ||
}).compile() | ||
|
||
app = moduleRef.createNestApplication<NestFastifyApplication>(fastifyApp) | ||
await app.init() | ||
await app.getHttpAdapter().getInstance().ready() | ||
}) | ||
|
||
it('GET /master/login', () => { | ||
return app | ||
.inject({ | ||
method: 'POST', | ||
url: '/master/login', | ||
payload: { | ||
username: 'test', | ||
password: 'pwd', | ||
}, | ||
}) | ||
.then((res) => { | ||
expect(res.statusCode).toBe(200) | ||
}) | ||
}) | ||
}) |