diff --git a/app/src/router/AppRouter.tsx b/app/src/router/AppRouter.tsx index 8886904a4..200c28608 100644 --- a/app/src/router/AppRouter.tsx +++ b/app/src/router/AppRouter.tsx @@ -70,34 +70,37 @@ export const routeMap: Routes = { [DOWNLOAD_SEARCH]: { page: , type: ROUTE_TYPE.PRIVATE, - unauthorized: [REPOSITORY_ROLE.PCSE], + unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL], }, [UPLOAD_SEARCH]: { page: , type: ROUTE_TYPE.PRIVATE, - unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL], + unauthorized: [REPOSITORY_ROLE.PCSE], }, [DOWNLOAD_VERIFY]: { page: , type: ROUTE_TYPE.PATIENT, - unauthorized: [REPOSITORY_ROLE.PCSE], + unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL], }, [UPLOAD_VERIFY]: { page: , type: ROUTE_TYPE.PATIENT, - unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL], + unauthorized: [REPOSITORY_ROLE.PCSE], }, [UPLOAD_DOCUMENTS]: { page: , type: ROUTE_TYPE.PATIENT, + unauthorized: [REPOSITORY_ROLE.PCSE], }, [DOWNLOAD_DOCUMENTS]: { page: , type: ROUTE_TYPE.PATIENT, + unauthorized: [REPOSITORY_ROLE.GP_ADMIN, REPOSITORY_ROLE.GP_CLINICAL], }, [LLOYD_GEORGE]: { page: , type: ROUTE_TYPE.PATIENT, + unauthorized: [REPOSITORY_ROLE.PCSE], }, }; diff --git a/app/src/router/guards/roleGuard/RoleGuard.test.tsx b/app/src/router/guards/roleGuard/RoleGuard.test.tsx new file mode 100644 index 000000000..805f25904 --- /dev/null +++ b/app/src/router/guards/roleGuard/RoleGuard.test.tsx @@ -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( + + +
+ + , + ); +}; diff --git a/app/src/router/guards/roleGuard/RoleGuard.tsx b/app/src/router/guards/roleGuard/RoleGuard.tsx index c57dc8521..21dcaf20d 100644 --- a/app/src/router/guards/roleGuard/RoleGuard.tsx +++ b/app/src/router/guards/roleGuard/RoleGuard.tsx @@ -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); }