-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add integration test for passport module
- Loading branch information
Showing
12 changed files
with
7,097 additions
and
2,087 deletions.
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 |
---|---|---|
|
@@ -12,7 +12,6 @@ npm-debug.log | |
.DS_Store | ||
|
||
# tests | ||
/test | ||
/coverage | ||
/.nyc_output | ||
|
||
|
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,10 @@ | ||
module.exports = { | ||
moduleFileExtensions: ['js', 'json', 'ts'], | ||
rootDir: '.', | ||
testMatch: ['<rootDir>/test/*.e2e-spec.ts'], | ||
transform: { | ||
'^.+\\.ts$': 'ts-jest' | ||
}, | ||
testEnvironment: 'node', | ||
collectCoverage: true | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
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,28 @@ | ||
import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common'; | ||
import { AuthGuard } from '../lib'; | ||
|
||
import { AppService } from './app.service'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
@Get() | ||
getHello() { | ||
return this.appService.getHello(); | ||
} | ||
|
||
@UseGuards(AuthGuard('jwt')) | ||
@Get('private') | ||
getPrivateHello() { | ||
return this.appService.getPrivateMessage(); | ||
} | ||
|
||
@UseGuards(AuthGuard('local')) | ||
@Post('login') | ||
login(@Req() req: any, @Body() body: Record<string, string>) { | ||
return this.appService.getToken({ | ||
username: body.username, | ||
id: req.user.id | ||
}); | ||
} | ||
} |
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,50 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { spec, request } from 'pactum'; | ||
import { AppModule } from './app.module'; | ||
|
||
describe('Passport Module', () => { | ||
let app: INestApplication; | ||
|
||
beforeAll(async () => { | ||
const modRef = await Test.createTestingModule({ | ||
imports: [AppModule] | ||
}).compile(); | ||
app = modRef.createNestApplication(); | ||
await app.listen(0); | ||
const url = (await app.getUrl()).replace('[::1]', 'localhost'); | ||
request.setBaseUrl(url); | ||
}); | ||
|
||
describe('Authenticated flow', () => { | ||
it('should be able to log in and get a jwt, then hit the secret route', async () => { | ||
await spec() | ||
.post('/login') | ||
.withBody({ username: 'test1', password: 'test' }) | ||
.expectStatus(201) | ||
.stores('token', 'token'); | ||
await spec() | ||
.get('/private') | ||
.withHeaders('Authorization', 'Bearer $S{token}') | ||
.expectBody({ message: 'Hello secure world!' }); | ||
}); | ||
}); | ||
describe('UnauthenticatedFlow', () => { | ||
it('should return a 401 for an invalid login', async () => { | ||
await spec() | ||
.post('/login') | ||
.withBody({ username: 'test1', password: 'not the right password' }) | ||
.expectStatus(401); | ||
}); | ||
it('should return a 401 for an invalid JWT', async () => { | ||
await spec() | ||
.get('/private') | ||
.withHeaders('Authorization', 'Bearer not-a-jwt') | ||
.expectStatus(401); | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
}); |
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,19 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { PassportModule } from '../lib'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { JwtStrategy } from './jwt.strategy'; | ||
import { LocalStrategy } from './local.strategy'; | ||
|
||
@Module({ | ||
controllers: [AppController], | ||
imports: [ | ||
JwtModule.register({ | ||
secret: 's3cr3t' | ||
}), | ||
PassportModule.register({}) | ||
], | ||
providers: [AppService, LocalStrategy, JwtStrategy] | ||
}) | ||
export class AppModule {} |
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,47 @@ | ||
import { | ||
ForbiddenException, | ||
Injectable, | ||
UnauthorizedException | ||
} from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
private users = [ | ||
{ | ||
id: '1', | ||
username: 'test1', | ||
password: 'test' | ||
}, | ||
{ | ||
id: '2', | ||
username: 'nottest1', | ||
password: 'secret' | ||
} | ||
]; | ||
constructor(private readonly jwtService: JwtService) {} | ||
getHello() { | ||
return { message: 'Hello open world!' }; | ||
} | ||
|
||
getPrivateMessage() { | ||
return { message: 'Hello secure world!' }; | ||
} | ||
|
||
getToken({ username, id }: { username: string; id: string }): { | ||
token: string; | ||
} { | ||
return { token: this.jwtService.sign({ username, id }) }; | ||
} | ||
|
||
findUser({ username, password }: { username: string; password: string }): { | ||
id: string; | ||
username: string; | ||
} { | ||
const user = this.users.find((u) => u.username === username); | ||
if (!user || user.password !== password) { | ||
return null; | ||
} | ||
return user; | ||
} | ||
} |
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,17 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { PassportStrategy } from '../lib'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor() { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: 's3cr3t' | ||
}); | ||
} | ||
|
||
validate(payload) { | ||
return { id: payload.id, email: payload.email }; | ||
} | ||
} |
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,14 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Strategy } from 'passport-local'; | ||
import { PassportStrategy } from '../lib'; | ||
import { AppService } from './app.service'; | ||
|
||
@Injectable() | ||
export class LocalStrategy extends PassportStrategy(Strategy) { | ||
constructor(private readonly appService: AppService) { | ||
super(); | ||
} | ||
validate(username: string, password: string) { | ||
return this.appService.findUser({ username, password }); | ||
} | ||
} |
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,24 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": true, | ||
"noImplicitAny": false, | ||
"removeComments": true, | ||
"noLib": false, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"target": "es6", | ||
"sourceMap": false, | ||
"outDir": "./dist", | ||
"rootDir": "./lib", | ||
"skipLibCheck": true | ||
}, | ||
"include": [ | ||
"lib/**/*", | ||
"../index.ts" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"**/*.spec.ts" | ||
] | ||
} |
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