-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed authguard test, added backend workflow test and created a globa…
…l test
- Loading branch information
Showing
6 changed files
with
78 additions
and
51 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
on: | ||
push: | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
jest-test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Working Directory | ||
run: cd Backend | ||
- name: Install dependencies | ||
run: npm ci | ||
- name: Run tests | ||
run: npm test |
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,18 @@ | ||
{ | ||
"globalSetup": "./env.spec.ts", | ||
"moduleFileExtensions": [ | ||
"js", | ||
"json", | ||
"ts" | ||
], | ||
"rootDir": "src", | ||
"testRegex": ".*\\.spec\\.ts$", | ||
"transform": { | ||
"^.+\\.(t|j)s$": "ts-jest" | ||
}, | ||
"collectCoverageFrom": [ | ||
"**/*.(t|j)s" | ||
], | ||
"coverageDirectory": "../coverage", | ||
"testEnvironment": "node" | ||
} |
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 |
---|---|---|
@@ -1,32 +1,42 @@ | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { AuthGuard } from './auth.guard'; | ||
|
||
describe('AuthGuard', () => { | ||
const payload = 'complexUserNamePayload'; | ||
let service: JwtService; | ||
let jwtToken: string; | ||
let auth: AuthGuard; | ||
let headers: Record<string, string>; | ||
|
||
beforeAll(async () => { | ||
service = new JwtService({ | ||
secret: process.env.JWT_SECRET, | ||
secret: process.env.JWT_SECRET ?? 'keytest', | ||
}); | ||
jwtToken = await service.signAsync(payload); | ||
|
||
auth = new AuthGuard(service); | ||
headers = tokenToHeaders(await service.signAsync(payload)); | ||
}); | ||
|
||
it('Should be defined', () => { | ||
expect(service).toBeDefined(); | ||
expect(jwtToken).toBeDefined(); | ||
expect(auth).toBeDefined(); | ||
expect(headers).toBeDefined(); | ||
}); | ||
|
||
it('Get Payload from token', () => { | ||
const user = service.verify(jwtToken); | ||
const user = auth.checkToken(headers); | ||
expect(user).toBe(payload); | ||
}); | ||
|
||
it('Should fail JwtVerify of another token', () => { | ||
expect(() => | ||
service.verify( | ||
'eyJhbGciOiJIUzI1NiJ9.QmFzaWM.MTnCJYESf5QRL9N8gqn5Di5PEZX8eZB5sN8W4TJTDKF', | ||
auth.checkToken( | ||
tokenToHeaders( | ||
'eyJhbGciOiJIUzI1NiJ9.QmFzaWM.MTnCJYESf5QRL9N8gqn5Di5PEZX8eZB5sN8W4TJTDKF', | ||
), | ||
), | ||
).toThrow(); | ||
}); | ||
|
||
function tokenToHeaders(token: string) { | ||
return { authorization: `Bearer ${token}` }; | ||
} | ||
}); |
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