-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BC-6453 - extend unit tests for jwt and cookie handling
- Loading branch information
Showing
1 changed file
with
53 additions
and
4 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { createMock, DeepMocked } from '@golevelup/ts-jest'; | ||
import { UnauthorizedException } from '@nestjs/common'; | ||
import { REQUEST } from '@nestjs/core'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AxiosResponse } from 'axios'; | ||
|
@@ -12,7 +13,7 @@ import { | |
import { AuthorizationClientAdapter } from './authorization-client.adapter'; | ||
import { AuthorizationErrorLoggableException, AuthorizationForbiddenLoggableException } from './error'; | ||
|
||
const jwtToken = 'Bearer someJwtToken'; | ||
const jwtToken = 'someJwtToken'; | ||
Check failure Code scanning / CodeQL Hard-coded credentials Critical test
The hard-coded value "someJwtToken" is used as
authorization header Error loading related location Loading The hard-coded value "someJwtToken" is used as authorization header Error loading related location Loading The hard-coded value "someJwtToken" is used as authorization header Error loading related location Loading The hard-coded value "someJwtToken" is used as authorization header Error loading related location Loading The hard-coded value "someJwtToken" is used as authorization header. The hard-coded value "someJwtToken" is used as authorization header. |
||
|
||
describe(AuthorizationClientAdapter.name, () => { | ||
let module: TestingModule; | ||
|
@@ -31,7 +32,7 @@ describe(AuthorizationClientAdapter.name, () => { | |
provide: REQUEST, | ||
useValue: createMock<Request>({ | ||
headers: { | ||
authorization: jwtToken, | ||
authorization: `Bearer ${jwtToken}`, | ||
}, | ||
}), | ||
}, | ||
|
@@ -75,7 +76,7 @@ describe(AuthorizationClientAdapter.name, () => { | |
referenceType: AuthorizationBodyParamsReferenceType.COURSES, | ||
referenceId: 'someReferenceId', | ||
}; | ||
const expectedOptions = { headers: { authorization: jwtToken } }; | ||
const expectedOptions = { headers: { authorization: `Bearer ${jwtToken}` } }; | ||
|
||
await service.checkPermissionByReferences(params); | ||
|
||
|
@@ -156,7 +157,7 @@ describe(AuthorizationClientAdapter.name, () => { | |
referenceType: AuthorizationBodyParamsReferenceType.COURSES, | ||
referenceId: 'someReferenceId', | ||
}; | ||
const expectedOptions = { headers: { authorization: jwtToken } }; | ||
const expectedOptions = { headers: { authorization: `Bearer ${jwtToken}` } }; | ||
|
||
await service.hasPermissionByReferences(params); | ||
|
||
|
@@ -166,6 +167,54 @@ describe(AuthorizationClientAdapter.name, () => { | |
); | ||
}); | ||
|
||
it('should forward the JWT token from the "jwt" cookie', async () => { | ||
setup(); | ||
|
||
const request = createMock<Request>({ | ||
headers: { | ||
cookie: `jwt=${jwtToken}`, | ||
}, | ||
}); | ||
|
||
const adapter = new AuthorizationClientAdapter(authorizationApi, request); | ||
|
||
const params = { | ||
context: { | ||
action: Action.READ, | ||
requiredPermissions: [], | ||
}, | ||
referenceType: AuthorizationBodyParamsReferenceType.COURSES, | ||
referenceId: 'someReferenceId', | ||
}; | ||
const expectedOptions = { headers: { authorization: `Bearer ${jwtToken}` } }; | ||
|
||
await adapter.hasPermissionByReferences(params); | ||
|
||
expect(authorizationApi.authorizationReferenceControllerAuthorizeByReference).toHaveBeenCalledWith( | ||
params, | ||
expectedOptions | ||
); | ||
}); | ||
|
||
it('should throw an UnauthorizedException if no JWT token is found', async () => { | ||
const request = createMock<Request>({ | ||
headers: {}, | ||
}); | ||
|
||
const adapter = new AuthorizationClientAdapter(authorizationApi, request); | ||
|
||
const params = { | ||
context: { | ||
action: Action.READ, | ||
requiredPermissions: [], | ||
}, | ||
referenceType: AuthorizationBodyParamsReferenceType.COURSES, | ||
referenceId: 'someReferenceId', | ||
}; | ||
|
||
await expect(adapter.hasPermissionByReferences(params)).rejects.toThrowError(UnauthorizedException); | ||
}); | ||
|
||
describe('when client returns response', () => { | ||
it('should return isAuthorized', async () => { | ||
const { response } = setup(); | ||
|