Skip to content

Commit

Permalink
Fixed unit tests by mocking jwtTokenVerifier from okta, Updated test …
Browse files Browse the repository at this point in the history
…scirpts to not include actual okta instances
  • Loading branch information
RohitKandimalla committed Apr 17, 2024
1 parent 457104e commit 4166bd3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "ISSUER=\"https://dev-18092578.okta.com/oauth2/default\" CLIENT_ID=\"0oa2fqtaz95fqJqbf5d7\" jest",
"test:watch": "ISSUER=\"https://dev-18092578.okta.com/oauth2/default\" CLIENT_ID=\"0oa2fqtaz95fqJqbf5d7\" jest --watch",
"test:cov": "ISSUER=\"https://dev-18092578.okta.com/oauth2/default\" CLIENT_ID=\"0oa2fqtaz95fqJqbf5d7\" jest --coverage",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
Expand Down
50 changes: 21 additions & 29 deletions src/auth/auth.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,31 @@ import { AuthGuard } from './auth.guard';
import { JwtService } from '@nestjs/jwt';
import { createMock } from '@golevelup/ts-jest';
import { ExecutionContext } from '@nestjs/common';
import { Jwt } from '@okta/jwt-verifier';
import * as process from 'process';

jest.mock('@okta/jwt-verifier', () => {
return jest.fn().mockImplementation(() => {
return {
verifyAccessToken: () =>
new Promise((resolve) =>
resolve({
claims: {
sub: 'a_user',
},
} as unknown as Jwt),
),
};
});
});

describe('AuthGuard', () => {
let guard: AuthGuard;

beforeEach(async () => {
process.env.ISSUER = 'https://test-issuer.com';
process.env.CLIENT_ID = 'test-client-id';

const module: TestingModule = await Test.createTestingModule({
providers: [JwtService],
}).compile();
Expand All @@ -22,33 +42,6 @@ describe('AuthGuard', () => {
it('should have a fully mocked Execution Context with good auth token', async () => {
const mockExecutionContext = createMock<ExecutionContext>();
expect(mockExecutionContext.switchToHttp()).toBeDefined();

mockExecutionContext.switchToHttp = jest.fn().mockResolvedValue({
getRequest: () => ({
originalUrl: '/',
method: 'GET',
params: undefined,
query: undefined,
body: undefined,
headers: '',
}),
getResponse: () => ({
statusCode: 200,
}),
});

jest.mock('@okta/jwt-verifier', () => {
return jest.fn().mockImplementation(() => ({
verifyAccessToken: () => ({
oktaToken: {
claims: {
sub: 'a_user',
},
},
}),
}));
});

jest
.spyOn(mockExecutionContext.switchToHttp(), 'getRequest')
.mockImplementation(() => {
Expand All @@ -59,8 +52,7 @@ describe('AuthGuard', () => {
query: undefined,
body: undefined,
headers: {
authorization:
'Bearer eyJraWQiOiJNNG9CMW9DSmthdC0tYTNENFFXUFA3RWZCbUl3NG9BV05KYWJxdEJhUnM4IiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULl8xc1BEY0hpekhSdm5CSlZsaWQzak5tVXZjMDIzU3FCZDB0UUhnVldkT0EiLCJpc3MiOiJodHRwczovL2Rldi0xODA5MjU3OC5va3RhLmNvbS9vYXV0aDIvZGVmYXVsdCIsImF1ZCI6ImFwaTovL2RlZmF1bHQiLCJpYXQiOjE3MTI5MzcwNjQsImV4cCI6MTcxMjk0MDY2NCwiY2lkIjoiMG9hMmZxdGF6OTVmcUpxYmY1ZDciLCJ1aWQiOiIwMHUzaTNjM3p6WlhLcjkwMTVkNyIsInNjcCI6WyJvcGVuaWQiLCJwcm9maWxlIl0sImF1dGhfdGltZSI6MTcxMjkzMjU5NSwic3ViIjoiY2VjaWxpYS5saXVAc2VtYW50aWNiaXRzLmNvbSJ9.x_vx7uCGXPyme80erURcS87ZUdibdBRKiNB58yg-AcNoF0ZYoro0lOr9up-ev0j32SQvBnhMXRZOARoOy4ALcT_GovwluH2v_sjXhNtjn26GV5UZU1EaWXsdMWfwg_-6eAmlQ9dLkIZerIYsu7Ut8pfwirgbpME4mMKqiJBXEkRWHUkAh5PEnJO4DvaKj6Tis5ERprNLuUKR5M4bWMhBjAMt74fTu5iLiANOi0uqropZscP72HVNEQhkyqM84hvgAvZvzlVYKUTUBuoWQF21eRSOUpYSE0yvDRW5JS5r0NCXUEZwuNfavuvl3gDUae31hcbtOo7kznQ2Q_-GkfVThA',
authorization: 'Bearer asdf',
},
} as unknown as Request;
});
Expand Down

0 comments on commit 4166bd3

Please sign in to comment.