Skip to content

Commit

Permalink
test: FORMS-1265 consistency and coverage (bcgov#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterMoar authored May 23, 2024
1 parent 82180e1 commit cc49b3e
Show file tree
Hide file tree
Showing 23 changed files with 763 additions and 679 deletions.
4 changes: 4 additions & 0 deletions app/src/forms/auth/middleware/userAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ const hasRoleModifyPermissions = async (req, _res, next) => {

if (userRoles.includes(Roles.OWNER)) {
// Can't remove a different user's owner role unless you are an owner.
//
// TODO: Remove this if statement and just throw the exception. It's not
// possible for userId === currentUser.id since we're in an if that we
// are !isOwner but also that userRoles.includes(Roles.OWNER).
if (userId !== currentUser.id) {
throw new Problem(401, {
detail: "You can't update an owner's roles.",
Expand Down
16 changes: 8 additions & 8 deletions app/tests/unit/components/idpService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,19 @@ describe('idpService', () => {
it('should return a user search', async () => {
const s = await idpService.userSearch({ idpCode: 'idir', email: '[email protected]' });
expect(s).toBeFalsy();
expect(MockModel.query).toHaveBeenCalledTimes(1);
expect(MockModel.modify).toHaveBeenCalledTimes(9);
expect(MockModel.modify).toHaveBeenCalledWith('filterIdpCode', 'idir');
expect(MockModel.modify).toHaveBeenCalledWith('filterEmail', '[email protected]', false);
expect(MockModel.query).toBeCalledTimes(1);
expect(MockModel.modify).toBeCalledTimes(9);
expect(MockModel.modify).toBeCalledWith('filterIdpCode', 'idir');
expect(MockModel.modify).toBeCalledWith('filterEmail', '[email protected]', false);
});

it('should return a customized user search', async () => {
const s = await idpService.userSearch({ idpCode: 'bceid-business', email: '[email protected]' });
expect(s).toBeFalsy();
expect(MockModel.query).toHaveBeenCalledTimes(1);
expect(MockModel.modify).toHaveBeenCalledWith('filterIdpCode', 'bceid-business');
expect(MockModel.modify).toHaveBeenCalledWith('filterEmail', '[email protected]', true);
expect(MockModel.modify).toHaveBeenCalledTimes(9);
expect(MockModel.query).toBeCalledTimes(1);
expect(MockModel.modify).toBeCalledWith('filterIdpCode', 'bceid-business');
expect(MockModel.modify).toBeCalledWith('filterEmail', '[email protected]', true);
expect(MockModel.modify).toBeCalledTimes(9);
});

it('should throw error when customized user search fails validation', async () => {
Expand Down
30 changes: 15 additions & 15 deletions app/tests/unit/components/jwtService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ describe('jwtService', () => {
const req = getMockReq({ headers: { authorization: 'Bearer JWT' } });
const r = await jwtService.getTokenPayload(req);
expect(r).toBe(payload);
expect(jwtService.getBearerToken).toHaveBeenCalledTimes(1);
expect(jwtService._verify).toHaveBeenCalledTimes(1);
expect(jwtService.getBearerToken).toBeCalledTimes(1);
expect(jwtService._verify).toBeCalledTimes(1);
});

it('should error if token not valid', async () => {
Expand All @@ -59,8 +59,8 @@ describe('jwtService', () => {
expect(e).toBeInstanceOf(jose.errors.JWTClaimValidationFailed);
expect(payload).toBe(undefined);
}
expect(jwtService.getBearerToken).toHaveBeenCalledTimes(1);
expect(jwtService._verify).toHaveBeenCalledTimes(1);
expect(jwtService.getBearerToken).toBeCalledTimes(1);
expect(jwtService._verify).toBeCalledTimes(1);
});

it('should validate access token on good jwt', async () => {
Expand All @@ -71,7 +71,7 @@ describe('jwtService', () => {
const req = getMockReq({ headers: { authorization: 'Bearer JWT' } });
const r = await jwtService.validateAccessToken(req);
expect(r).toBeTruthy();
expect(jwtService._verify).toHaveBeenCalledTimes(1);
expect(jwtService._verify).toBeCalledTimes(1);
});

it('should not validate access token on jwt error', async () => {
Expand All @@ -84,7 +84,7 @@ describe('jwtService', () => {
const r = await jwtService.validateAccessToken(req);
expect(r).toBeFalsy();

expect(jwtService._verify).toHaveBeenCalledTimes(1);
expect(jwtService._verify).toBeCalledTimes(1);
});

it('should throw problem when validate access token catches (non-jwt) error)', async () => {
Expand All @@ -104,7 +104,7 @@ describe('jwtService', () => {

expect(e).toBeInstanceOf(Problem);
expect(r).toBe(undefined);
expect(jwtService._verify).toHaveBeenCalledTimes(1);
expect(jwtService._verify).toBeCalledTimes(1);
});

it('should pass middleware protect with valid jwt)', async () => {
Expand All @@ -122,8 +122,8 @@ describe('jwtService', () => {
const middleware = jwtService.protect();

await middleware(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith();
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith();
});

it('should fail middleware protect with invalid jwt', async () => {
Expand All @@ -142,8 +142,8 @@ describe('jwtService', () => {
const middleware = jwtService.protect();

await middleware(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith(expect.objectContaining({ status: 401 }));
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith(expect.objectContaining({ status: 401 }));
});

it('should pass middleware protect with valid jwt and role', async () => {
Expand All @@ -161,8 +161,8 @@ describe('jwtService', () => {
const middleware = jwtService.protect('admin');

await middleware(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith();
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith();
});

it('should fail middleware protect with valid jwt and but no role', async () => {
Expand All @@ -180,7 +180,7 @@ describe('jwtService', () => {
const middleware = jwtService.protect('admin');

await middleware(req, res, next);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith(expect.objectContaining({ status: 401 }));
expect(next).toBeCalledTimes(1);
expect(next).toBeCalledWith(expect.objectContaining({ status: 401 }));
});
});
6 changes: 3 additions & 3 deletions app/tests/unit/forms/admin/controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('form controller', () => {
service.createFormComponentsProactiveHelp = jest.fn().mockReturnValue(formComponentsProactiveHelp);

await controller.createFormComponentsProactiveHelp(req, {}, jest.fn());
expect(service.createFormComponentsProactiveHelp).toHaveBeenCalledTimes(1);
expect(service.createFormComponentsProactiveHelp).toBeCalledTimes(1);
});

it('should update proactive help component publish status', async () => {
Expand All @@ -43,13 +43,13 @@ describe('form controller', () => {
service.updateFormComponentsProactiveHelp = jest.fn().mockReturnValue(formComponentsProactiveHelp);

await controller.updateFormComponentsProactiveHelp(req, {}, jest.fn());
expect(service.updateFormComponentsProactiveHelp).toHaveBeenCalledTimes(1);
expect(service.updateFormComponentsProactiveHelp).toBeCalledTimes(1);
});

it('should get list of all proactive help components', async () => {
service.listFormComponentsProactiveHelp = jest.fn().mockReturnValue({});

await controller.listFormComponentsProactiveHelp(req, {}, jest.fn());
expect(service.listFormComponentsProactiveHelp).toHaveBeenCalledTimes(1);
expect(service.listFormComponentsProactiveHelp).toBeCalledTimes(1);
});
});
8 changes: 4 additions & 4 deletions app/tests/unit/forms/auth/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ describe('login', () => {
service.getUserId = jest.fn().mockReturnValue({ user: 'me' });
const token = 'token';
const result = await service.login(token);
expect(idpService.parseToken).toHaveBeenCalledTimes(1);
expect(idpService.parseToken).toHaveBeenCalledWith(token);
expect(service.getUserId).toHaveBeenCalledTimes(1);
expect(service.getUserId).toHaveBeenCalledWith({ idp: 'fake' });
expect(idpService.parseToken).toBeCalledTimes(1);
expect(idpService.parseToken).toBeCalledWith(token);
expect(service.getUserId).toBeCalledTimes(1);
expect(service.getUserId).toBeCalledWith({ idp: 'fake' });
expect(result).toBeTruthy();
expect(result).toEqual(resultSample);
});
Expand Down
Loading

0 comments on commit cc49b3e

Please sign in to comment.