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

2
.gitignore vendored
View File

@@ -46,3 +46,5 @@ assets
scripts/workflow/docker-compose.yml
scripts/workflow/data
schema.gql

View File

@@ -1,120 +0,0 @@
# ------------------------------------------------------
# THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
# ------------------------------------------------------
type Paginator {
total: Float!
size: Float!
currentPage: Float!
totalPage: Float!
hasNextPage: Boolean!
hasPrevPage: Boolean!
}
type Image {
width: Float
height: Float
accent: String
type: String
src: String!
}
type CountMixed {
read: Float
like: Float
}
type Coordinate {
latitude: Float!
longitude: Float!
}
type NoteMusic {
type: String!
id: String!
}
type NoteModel {
created: DateTime
id: ID
commentsIndex: Float
allowComment: Boolean!
images: Image
modified: DateTime
title: String!
text: String!
coordinates: Coordinate
count: CountMixed
music: [NoteMusic!]
nid: Float!
hide: Boolean!
password: String
secret: DateTime
mood: String
weather: String
hasMemory: Boolean
location: String
}
"""
A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.
"""
scalar DateTime
type NoteItemAggregateModel {
data: NoteModel!
prev: NoteModel
next: NoteModel
}
type NotePaginatorModel {
data: [NoteModel!]!
pagination: Paginator!
}
type CategoryModel {
created: DateTime
id: ID
name: String!
type: CategoryType
slug: String!
}
enum CategoryType {
Category
Tag
}
type PostModel {
created: DateTime
id: ID
commentsIndex: Float
allowComment: Boolean!
images: Image
modified: DateTime
title: String!
text: String!
categoryId: String!
category: CategoryModel
count: CountMixed
slug: String!
summary: String
hide: Boolean
copyright: Boolean
tags: [String!]
}
type PostPaginatorModel {
data: [PostModel!]!
pagination: Paginator!
}
type Query {
sayHello: String!
getPostById(id: ID!): PostModel!
getPostList(size: Int, page: Int, select: String, year: Float, state: Float, sortOrder: Int, sortBy: String): PostPaginatorModel!
getByCateAndSlug(category: String!, slug: String!): PostModel!
getNoteById(password: String, nid: Int, id: ID): NoteItemAggregateModel!
getLastestNote: NoteItemAggregateModel!
getNotesWithPager(size: Int, page: Int, select: String, year: Float, state: Float, sortOrder: Int, sortBy: String): NotePaginatorModel!
}

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)
})
})
})