generated from ministryofjustice/hmpps-template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VB-3733, VB-3792 User login and booker homepage - tests and code improvements #51
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
acad934
Tidy some redundant files
tpmcgowan cde4d63
Exclude code coverage output from TypeScript
tpmcgowan e75ac02
Add tests for BookerService
tpmcgowan e1ac5db
Update Auth error page to not use OL header
tpmcgowan fbe06ef
Tweak populateCurrenBooker() and add tests
tpmcgowan b88f2d1
Add tests for home page
tpmcgowan b1722aa
Fix and tidy integration log in tests
tpmcgowan a010256
Add first steps of booking journey integration tests
tpmcgowan 79b8c9e
Add page header tests (logged in & auth error)
tpmcgowan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import TestData from '../../server/routes/testutils/testData' | ||
import HomePage from '../pages/home' | ||
import Page from '../pages/page' | ||
|
||
context('Booking journey', () => { | ||
beforeEach(() => { | ||
cy.task('reset') | ||
cy.task('stubSignIn') | ||
cy.task('stubHmppsAuthToken') | ||
}) | ||
|
||
it('should complete the booking journey', () => { | ||
cy.task('stubGetBookerReference') | ||
cy.task('stubGetPrisoners', { prisoners: [TestData.prisonerInfoDto()] }) | ||
cy.signIn() | ||
|
||
const homePage = Page.verifyOnPage(HomePage) | ||
homePage.prisonerName().contains('John Smith') | ||
homePage.startButton().contains('Start') | ||
|
||
// TODO add to this test as booking journey implemented | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,21 +1,5 @@ | ||
import jwt from 'jsonwebtoken' | ||
|
||
import { stubFor } from './wiremock' | ||
|
||
const createToken = () => { | ||
const payload = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing unnecessary complexity in this stub |
||
sub: 'system_client_id', | ||
grant_type: 'client_credentials', | ||
scope: ['read', 'write'], | ||
auth_source: 'none', | ||
iss: 'http://localhost:9091/auth/auth/issuer', | ||
authorities: ['ROLE_VISIT_SCHEDULER'], | ||
jti: 'NBmv9IH_xw89YFE_tFoBwI1zo9Y', | ||
client_id: 'system_client_id', | ||
} | ||
return jwt.sign(payload, 'secret', { expiresIn: '1h' }) | ||
} | ||
|
||
const stubHmppsAuthToken = () => | ||
stubFor({ | ||
request: { | ||
|
@@ -26,13 +10,12 @@ const stubHmppsAuthToken = () => | |
status: 200, | ||
headers: { | ||
'Content-Type': 'application/json;charset=UTF-8', | ||
Location: 'http://localhost:3007/sign-in/callback?code=codexxxx&state=stateyyyy', | ||
}, | ||
jsonBody: { | ||
access_token: createToken(), | ||
access_token: 'hmpps-auth-token', | ||
token_type: 'bearer', | ||
expires_in: 599, | ||
scope: 'read', | ||
scope: 'read write', | ||
sub: 'system_client_id', | ||
auth_source: 'none', | ||
jti: 'NBmv9IH_xw89YFE_tFoBwI1zo9Y', | ||
|
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
6 changes: 3 additions & 3 deletions
6
integration_tests/pages/index.ts → integration_tests/pages/home.ts
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import Page, { PageElement } from './page' | ||
|
||
export default class IndexPage extends Page { | ||
export default class HomePage extends Page { | ||
constructor() { | ||
super('Book a visit') | ||
} | ||
|
||
headerPhaseBanner = (): PageElement => cy.get('[data-qa=header-phase-banner]') | ||
|
||
prisonerName = (): PageElement => cy.get('[data-test=prisoner-name]') | ||
|
||
startButton = (): PageElement => cy.get('[data-test="start-booking"]') | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,99 @@ | ||
import type { Request, Response } from 'express' | ||
import { BadRequest, NotFound } from 'http-errors' | ||
import { createMockBookerService } from '../services/testutils/mocks' | ||
import TestData from '../routes/testutils/testData' | ||
import populateCurrentBooker from './populateCurrentBooker' | ||
import logger from '../../logger' | ||
|
||
jest.mock('../../logger') | ||
|
||
describe('populateCurrentBooker', () => { | ||
let req: Request | ||
let res: Response | ||
const next = jest.fn() | ||
|
||
const bookerService = createMockBookerService() | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
|
||
req = { session: {} } as unknown as Request | ||
|
||
res = { | ||
locals: { | ||
user: { | ||
sub: 'user1', | ||
email: 'user1@example.com', | ||
phone_number: '+440123456789', | ||
}, | ||
}, | ||
redirect: jest.fn(), | ||
} as unknown as Response | ||
}) | ||
|
||
it('should get booker reference and add to session if it is not already set', async () => { | ||
const bookerReference = TestData.bookerReference().value | ||
bookerService.getBookerReference.mockResolvedValue(bookerReference) | ||
|
||
await populateCurrentBooker(bookerService)(req, res, next) | ||
|
||
expect(bookerService.getBookerReference).toHaveBeenCalledWith({ | ||
oneLoginSub: res.locals.user.sub, | ||
email: res.locals.user.email, | ||
phoneNumber: res.locals.user.phone_number, | ||
}) | ||
expect(req.session).toStrictEqual({ booker: { reference: bookerReference } }) | ||
expect(res.redirect).not.toHaveBeenCalled() | ||
expect(next).toHaveBeenCalled() | ||
}) | ||
|
||
it('should not get booker reference if it is already set in session', async () => { | ||
req.session.booker = { reference: TestData.bookerReference().value } | ||
|
||
await populateCurrentBooker(bookerService)(req, res, next) | ||
|
||
expect(bookerService.getBookerReference).not.toHaveBeenCalled() | ||
expect(res.redirect).not.toHaveBeenCalled() | ||
expect(next).toHaveBeenCalled() | ||
}) | ||
|
||
it('should redirect to /sign-out if user details are not present in res.locals', async () => { | ||
delete res.locals.user | ||
await populateCurrentBooker(bookerService)(req, res, next) | ||
|
||
expect(bookerService.getBookerReference).not.toHaveBeenCalled() | ||
expect(res.redirect).toHaveBeenCalledWith('/sign-out') | ||
expect(next).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should handle the booker not being found (404 error) by logging and redirecting to /autherror', async () => { | ||
bookerService.getBookerReference.mockRejectedValue(new NotFound()) | ||
|
||
await populateCurrentBooker(bookerService)(req, res, next) | ||
|
||
expect(bookerService.getBookerReference).toHaveBeenCalledWith({ | ||
oneLoginSub: res.locals.user.sub, | ||
email: res.locals.user.email, | ||
phoneNumber: res.locals.user.phone_number, | ||
}) | ||
expect(res.redirect).toHaveBeenCalledWith('/autherror') | ||
expect(next).not.toHaveBeenCalled() | ||
expect(logger.info).toHaveBeenCalledWith('Failed to retrieve booker reference for: user1') | ||
}) | ||
|
||
it('should propagate any other errors', async () => { | ||
const error = new BadRequest('Oops!') | ||
bookerService.getBookerReference.mockRejectedValue(error) | ||
|
||
await populateCurrentBooker(bookerService)(req, res, next) | ||
|
||
expect(bookerService.getBookerReference).toHaveBeenCalledWith({ | ||
oneLoginSub: res.locals.user.sub, | ||
email: res.locals.user.email, | ||
phoneNumber: res.locals.user.phone_number, | ||
}) | ||
expect(res.redirect).not.toHaveBeenCalled() | ||
expect(logger.info).not.toHaveBeenCalled() | ||
expect(next).toHaveBeenCalledWith(error) | ||
}) | ||
}) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renaming for consistency