-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
20 changed files
with
494 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { render, waitFor } from '@testing-library/react'; | ||
import SessionProvider, { Session } from '../../../providers/sessionProvider/SessionProvider'; | ||
import * as ReactRouter from 'react-router'; | ||
import { History, createMemoryHistory } from 'history'; | ||
import { buildUserAuth } from '../../../helpers/test/testBuilders'; | ||
import AuthGuard from './AuthGuard'; | ||
import { routes } from '../../../types/generic/routes'; | ||
|
||
const guardPage = '/profile'; | ||
describe('AuthGuard', () => { | ||
beforeEach(() => { | ||
sessionStorage.setItem('UserSession', ''); | ||
|
||
process.env.REACT_APP_ENVIRONMENT = 'jest'; | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('navigates user to unauthorised when user is not logged in', async () => { | ||
const auth: Session = { | ||
auth: null, | ||
isLoggedIn: false, | ||
}; | ||
const history = createMemoryHistory({ | ||
initialEntries: [guardPage], | ||
initialIndex: 0, | ||
}); | ||
expect(history.location.pathname).toBe(guardPage); | ||
renderAuthGuard(auth, history); | ||
|
||
await waitFor(async () => { | ||
expect(history.location.pathname).toBe(routes.UNAUTHORISED); | ||
}); | ||
}); | ||
|
||
it('navigates user to correct page when user is logged in', async () => { | ||
const auth: Session = { | ||
auth: buildUserAuth(), | ||
isLoggedIn: true, | ||
}; | ||
const history = createMemoryHistory({ | ||
initialEntries: [guardPage], | ||
initialIndex: 0, | ||
}); | ||
expect(history.location.pathname).toBe(guardPage); | ||
renderAuthGuard(auth, history); | ||
|
||
await waitFor(async () => { | ||
expect(history.location.pathname).toBe(guardPage); | ||
}); | ||
}); | ||
}); | ||
|
||
const renderAuthGuard = (session: Session, history: History) => { | ||
render( | ||
<SessionProvider sessionOverride={session}> | ||
<ReactRouter.Router navigator={history} location={history.location}> | ||
<AuthGuard> | ||
<div>User is logged in</div> | ||
</AuthGuard> | ||
</ReactRouter.Router> | ||
, | ||
</SessionProvider>, | ||
); | ||
}; |
59 changes: 59 additions & 0 deletions
59
app/src/components/blocks/patientGuard/PatientGuard.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 PatientGuard from './PatientGuard'; | ||
import { PatientDetails } from '../../../types/generic/patientDetails'; | ||
import PatientDetailsProvider from '../../../providers/patientProvider/PatientProvider'; | ||
import { buildPatientDetails } from '../../../helpers/test/testBuilders'; | ||
|
||
const guardPage = '/profile'; | ||
describe('AuthGuard', () => { | ||
beforeEach(() => { | ||
process.env.REACT_APP_ENVIRONMENT = 'jest'; | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('navigates user to unauthorised when no patient is searched', async () => { | ||
const patientDetails = null; | ||
const history = createMemoryHistory({ | ||
initialEntries: [guardPage], | ||
initialIndex: 0, | ||
}); | ||
expect(history.location.pathname).toBe(guardPage); | ||
renderAuthGuard(patientDetails, history); | ||
|
||
await waitFor(async () => { | ||
expect(history.location.pathname).toBe(routes.UNAUTHORISED); | ||
}); | ||
}); | ||
|
||
it('navigates user to correct page when patient is searched', async () => { | ||
const patientDetails = buildPatientDetails(); | ||
const history = createMemoryHistory({ | ||
initialEntries: [guardPage], | ||
initialIndex: 0, | ||
}); | ||
expect(history.location.pathname).toBe(guardPage); | ||
renderAuthGuard(patientDetails, history); | ||
|
||
await waitFor(async () => { | ||
expect(history.location.pathname).toBe(guardPage); | ||
}); | ||
}); | ||
}); | ||
|
||
const renderAuthGuard = (patient: PatientDetails | null, history: History) => { | ||
return render( | ||
<PatientDetailsProvider patientDetails={patient}> | ||
<ReactRouter.Router navigator={history} location={history.location}> | ||
<PatientGuard> | ||
<div>patient number: {patient?.nhsNumber}</div> | ||
<div>patient postcode: {patient?.postalCode}</div> | ||
</PatientGuard> | ||
</ReactRouter.Router> | ||
, | ||
</PatientDetailsProvider>, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
app/src/pages/authCallbackPage/AuthCallbackPage.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import AuthCallbackPage from './AuthCallbackPage'; | ||
import SessionProvider from '../../providers/sessionProvider/SessionProvider'; | ||
import * as ReactRouter from 'react-router'; | ||
import { History, createMemoryHistory } from 'history'; | ||
import axios from 'axios'; | ||
import { buildUserAuth } from '../../helpers/test/testBuilders'; | ||
import { routes } from '../../types/generic/routes'; | ||
jest.mock('axios'); | ||
|
||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
const params = { | ||
code: 'cis2-code', | ||
state: 'cis2-state', | ||
id: 'cis2-id', | ||
}; | ||
|
||
const codeAndStateQueryParams = `code=${params.code}&state=${params.state}`; | ||
const allQueryParams = `?${codeAndStateQueryParams}&client_id=${params.id}`; | ||
const baseUiUrl = 'http://localhost:3000' + allQueryParams; | ||
const originalWindowLocation = window.location; | ||
|
||
const currentPage = '/example'; | ||
|
||
describe('AuthCallbackPage', () => { | ||
beforeEach(() => { | ||
process.env.REACT_APP_ENVIRONMENT = 'jest'; | ||
|
||
Object.defineProperty(window, 'location', { | ||
configurable: true, | ||
enumerable: true, | ||
value: new URL(baseUiUrl), | ||
}); | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
Object.defineProperty(window, 'location', { | ||
configurable: true, | ||
enumerable: true, | ||
value: originalWindowLocation, | ||
}); | ||
}); | ||
|
||
it('returns a loading state until redirection to token request handler', async () => { | ||
const history = createMemoryHistory({ | ||
initialEntries: [currentPage], | ||
initialIndex: 0, | ||
}); | ||
|
||
mockedAxios.get.mockImplementation(() => Promise.resolve({ data: buildUserAuth() })); | ||
renderCallbackPage(history); | ||
expect(screen.getByRole('status')).toBeInTheDocument(); | ||
expect(screen.getByText('Logging in...')).toBeInTheDocument(); | ||
await waitFor(() => { | ||
expect(history.location.pathname).toBe(routes.SELECT_ORG); | ||
}); | ||
}); | ||
|
||
it('navigates to the select role page when callback token request is successful', async () => { | ||
const history = createMemoryHistory({ | ||
initialEntries: [currentPage], | ||
initialIndex: 0, | ||
}); | ||
|
||
mockedAxios.get.mockImplementation(() => Promise.resolve({ data: buildUserAuth() })); | ||
renderCallbackPage(history); | ||
|
||
expect(screen.getByRole('status')).toBeInTheDocument(); | ||
expect(screen.getByText('Logging in...')).toBeInTheDocument(); | ||
expect(history.location.pathname).toBe(currentPage); | ||
|
||
await waitFor(() => { | ||
expect(history.location.pathname).toBe(routes.SELECT_ORG); | ||
}); | ||
}); | ||
|
||
it('navigates to auth error page when callback token request is unsuccessful', async () => { | ||
const errorResponse = { | ||
response: { | ||
status: 400, | ||
message: '400 Bad Request', | ||
}, | ||
}; | ||
const history = createMemoryHistory({ | ||
initialEntries: [currentPage], | ||
initialIndex: 0, | ||
}); | ||
|
||
mockedAxios.get.mockImplementation(() => Promise.reject(errorResponse)); | ||
renderCallbackPage(history); | ||
|
||
expect(screen.getByRole('status')).toBeInTheDocument(); | ||
expect(screen.getByText('Logging in...')).toBeInTheDocument(); | ||
expect(history.location.pathname).toBe(currentPage); | ||
|
||
await waitFor(() => { | ||
expect(history.location.pathname).toBe(routes.AUTH_ERROR); | ||
}); | ||
}); | ||
}); | ||
|
||
const renderCallbackPage = ( | ||
history: History = createMemoryHistory({ | ||
initialEntries: [currentPage], | ||
initialIndex: 1, | ||
}), | ||
) => { | ||
render( | ||
<SessionProvider> | ||
<ReactRouter.Router navigator={history} location={history.location}> | ||
<AuthCallbackPage />, | ||
</ReactRouter.Router> | ||
, | ||
</SessionProvider>, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import * as ReactRouter from 'react-router'; | ||
import { createMemoryHistory } from 'history'; | ||
import AuthErrorPage from './AuthErrorPage'; | ||
|
||
describe('AuthErrorPage', () => { | ||
it('renders unauthorised message', () => { | ||
const history = createMemoryHistory({ | ||
initialEntries: ['/'], | ||
initialIndex: 0, | ||
}); | ||
render( | ||
<ReactRouter.Router navigator={history} location={history.location}> | ||
<AuthErrorPage /> | ||
</ReactRouter.Router>, | ||
); | ||
expect(screen.getByText('You have been logged out')).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.