test: add attche token to header test

This commit is contained in:
Innei
2021-11-19 22:44:05 +08:00
parent 9a1b20ea6b
commit 9b614c0854
3 changed files with 61 additions and 120 deletions

View File

@@ -0,0 +1,59 @@
import {
Controller,
Get,
MiddlewareConsumer,
Module,
NestModule,
Req,
UseGuards,
} from '@nestjs/common'
import { AuthGuard } from '@nestjs/passport'
import { NestFastifyApplication } from '@nestjs/platform-fastify'
import { Test } from '@nestjs/testing'
import { fastifyApp } from '~/common/adapters/fastify.adapter'
import { RolesGuard } from '~/common/guard/roles.guard'
import { AttachHeaderTokenMiddleware } from '~/common/middlewares/attach-auth.middleware'
@Controller('/')
@UseGuards(RolesGuard)
class TestContoller {
@Get('/')
get(@Req() req: any) {
return req.headers.authorization
}
}
@Module({ controllers: [TestContoller] })
class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AttachHeaderTokenMiddleware).forRoutes('(.*)')
}
}
describe('AuthMiddleware (e2e)', () => {
let app: NestFastifyApplication
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(AuthGuard)
.useValue({ canActivate: () => true })
.compile()
app = moduleRef.createNestApplication<NestFastifyApplication>(fastifyApp)
await app.init()
await app.getHttpAdapter().getInstance().ready()
})
it('get / ', async () => {
const token = 'fake token'
app
.inject({
method: 'GET',
url: '/?token=Bearer ' + token,
})
.then(async (res) => {
expect(res.statusCode).toBe(200)
expect(res.body).toBe('Bearer ' + token)
})
})
})