Skip to content

Commit

Permalink
BC-6453 - extend unit tests for jwt and cookie handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bergatco committed Jun 11, 2024
1 parent 7d7f51c commit a7453b9
Showing 1 changed file with 53 additions and 4 deletions.
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';
Expand All @@ -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
.
The hard-coded value "someJwtToken" is used as
authorization header
.
The hard-coded value "someJwtToken" is used as
authorization header
.
The hard-coded value "someJwtToken" is used as
authorization header
.
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;
Expand All @@ -31,7 +32,7 @@ describe(AuthorizationClientAdapter.name, () => {
provide: REQUEST,
useValue: createMock<Request>({
headers: {
authorization: jwtToken,
authorization: `Bearer ${jwtToken}`,
},
}),
},
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand Down

0 comments on commit a7453b9

Please sign in to comment.