Skip to content

Commit

Permalink
Add role guard test
Browse files Browse the repository at this point in the history
  • Loading branch information
RioKnightleyNHS committed Nov 20, 2023
1 parent 273887f commit 05366e9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
11 changes: 7 additions & 4 deletions app/src/router/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,37 @@ export const routeMap: Routes = {
[DOWNLOAD_SEARCH]: {
page: <PatientSearchPage />,
type: ROUTE_TYPE.PRIVATE,
unauthorized: [REPOSITORY_ROLE.PCSE],
unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL],
},
[UPLOAD_SEARCH]: {
page: <PatientSearchPage />,
type: ROUTE_TYPE.PRIVATE,
unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL],
unauthorized: [REPOSITORY_ROLE.PCSE],
},
[DOWNLOAD_VERIFY]: {
page: <PatientResultPage />,
type: ROUTE_TYPE.PATIENT,
unauthorized: [REPOSITORY_ROLE.PCSE],
unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL],
},
[UPLOAD_VERIFY]: {
page: <PatientResultPage />,
type: ROUTE_TYPE.PATIENT,
unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL],
unauthorized: [REPOSITORY_ROLE.PCSE],
},
[UPLOAD_DOCUMENTS]: {
page: <UploadDocumentsPage />,
type: ROUTE_TYPE.PATIENT,
unauthorized: [REPOSITORY_ROLE.PCSE],
},
[DOWNLOAD_DOCUMENTS]: {
page: <DocumentSearchResultsPage />,
type: ROUTE_TYPE.PATIENT,
unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL],
},
[LLOYD_GEORGE]: {
page: <LloydGeorgeRecordPage />,
type: ROUTE_TYPE.PATIENT,
unauthorized: [REPOSITORY_ROLE.PCSE],
},
};

Expand Down
59 changes: 59 additions & 0 deletions app/src/router/guards/roleGuard/RoleGuard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { render, waitFor } from '@testing-library/react';
import * as ReactRouter from 'react-router';
import { History, createMemoryHistory } from 'history';
import { routes } from '../../../types/generic/routes';
import RoleGuard from './RoleGuard';
import useRole from '../../../helpers/hooks/useRole';
import { REPOSITORY_ROLE } from '../../../types/generic/authRole';

jest.mock('../../../helpers/hooks/useRole');
const mockedUseRole = useRole as jest.Mock;

const guardPage = routes.LLOYD_GEORGE;
describe('RoleGuard', () => {
beforeEach(() => {
process.env.REACT_APP_ENVIRONMENT = 'jest';
});
afterEach(() => {
jest.clearAllMocks();
});
it('navigates user to unauthorised when role is not accepted', async () => {
const history = createMemoryHistory({
initialEntries: [guardPage],
initialIndex: 0,
});

mockedUseRole.mockReturnValue(REPOSITORY_ROLE.PCSE);
expect(history.location.pathname).toBe(guardPage);
renderAuthGuard(history);

await waitFor(async () => {
expect(history.location.pathname).toBe(routes.UNAUTHORISED);
});
});

it('navigates user to correct page when role is accepted', async () => {
const history = createMemoryHistory({
initialEntries: [guardPage],
initialIndex: 0,
});

mockedUseRole.mockReturnValue(REPOSITORY_ROLE.GP_ADMIN);
expect(history.location.pathname).toBe(guardPage);
renderAuthGuard(history);

await waitFor(async () => {
expect(history.location.pathname).toBe(guardPage);
});
});
});

const renderAuthGuard = (history: History) => {
return render(
<ReactRouter.Router navigator={history} location={history.location}>
<RoleGuard>
<div />
</RoleGuard>
</ReactRouter.Router>,
);
};
9 changes: 4 additions & 5 deletions app/src/router/guards/roleGuard/RoleGuard.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import { useEffect, type ReactNode } from 'react';
import { REPOSITORY_ROLE } from '../../../types/generic/authRole';
import { useNavigate } from 'react-router';
import { useLocation } from 'react-router-dom';
import { routes } from '../../../types/generic/routes';
import { routeMap } from '../../AppRouter';
import useRole from '../../../helpers/hooks/useRole';

type Props = {
children: ReactNode;
};

function RoleGuard({ children }: Props) {
const role = REPOSITORY_ROLE.PCSE;
const role = useRole();
const navigate = useNavigate();
const location = useLocation();
useEffect(() => {
const routeKey = location.pathname as keyof typeof routeMap;
const { unauthorized } = routeMap[routeKey];
const denyResource = Array.isArray(unauthorized) && unauthorized.includes(role);
console.log(unauthorized);
console.log('DENY RESOURCE?:', denyResource);
const denyResource = Array.isArray(unauthorized) && role && unauthorized.includes(role);

if (denyResource) {
navigate(routes.UNAUTHORISED);
}
Expand Down

0 comments on commit 05366e9

Please sign in to comment.