From 64dd053dce3d4fca1b87229729d16f9f5f2484a3 Mon Sep 17 00:00:00 2001 From: ttbarnes Date: Fri, 17 Jan 2025 10:40:33 +0000 Subject: [PATCH 1/3] chore(EMS-4132): broker - confirm address - data checks --- ...esses-page-skipping-broker-details.spec.js | 4 +- ...irm-address-skipping-address-forms.spec.js | 37 +++++++++++ .../broker-confirm-address/index.test.ts | 61 +++++++++++++++++++ .../policy/broker-confirm-address/index.ts | 18 +++++- 4 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 e2e-tests/insurance/cypress/e2e/journeys/policy/broker-confirm-address/broker-confirm-address-skipping-address-forms.spec.js diff --git a/e2e-tests/insurance/cypress/e2e/journeys/policy/broker-addresses/broker-addresses-page-skipping-broker-details.spec.js b/e2e-tests/insurance/cypress/e2e/journeys/policy/broker-addresses/broker-addresses-page-skipping-broker-details.spec.js index f5af43a080..340500078d 100644 --- a/e2e-tests/insurance/cypress/e2e/journeys/policy/broker-addresses/broker-addresses-page-skipping-broker-details.spec.js +++ b/e2e-tests/insurance/cypress/e2e/journeys/policy/broker-addresses/broker-addresses-page-skipping-broker-details.spec.js @@ -7,7 +7,7 @@ const { const baseUrl = Cypress.config('baseUrl'); -context('Insurance - Policy - Broker addresses page - Visit directly without completing the BROKER_DETAILS form.', () => { +context('Insurance - Policy - Broker addresses page - Visit directly without completing the BROKER_DETAILS form', () => { let referenceNumber; let url; let brokerDetailsUrl; @@ -32,8 +32,6 @@ context('Insurance - Policy - Broker addresses page - Visit directly without com }); it(`should redirect to ${BROKER_DETAILS_ROOT}`, () => { - cy.navigateToUrl(url); - cy.assertUrl(brokerDetailsUrl); }); }); diff --git a/e2e-tests/insurance/cypress/e2e/journeys/policy/broker-confirm-address/broker-confirm-address-skipping-address-forms.spec.js b/e2e-tests/insurance/cypress/e2e/journeys/policy/broker-confirm-address/broker-confirm-address-skipping-address-forms.spec.js new file mode 100644 index 0000000000..4e0f309b76 --- /dev/null +++ b/e2e-tests/insurance/cypress/e2e/journeys/policy/broker-confirm-address/broker-confirm-address-skipping-address-forms.spec.js @@ -0,0 +1,37 @@ +import { INSURANCE_ROUTES } from '../../../../../../constants/routes/insurance'; + +const { + ROOT, + POLICY: { BROKER_CONFIRM_ADDRESS_ROOT, BROKER_DETAILS_ROOT }, +} = INSURANCE_ROUTES; + +const baseUrl = Cypress.config('baseUrl'); + +context('Insurance - Policy - Broker confirm address - Visit directly without completing any address forms', () => { + let referenceNumber; + let url; + let brokerDetailsUrl; + + before(() => { + cy.completeSignInAndGoToApplication({}).then(({ referenceNumber: refNumber }) => { + referenceNumber = refNumber; + + url = `${baseUrl}${ROOT}/${referenceNumber}${BROKER_CONFIRM_ADDRESS_ROOT}`; + brokerDetailsUrl = `${baseUrl}${ROOT}/${referenceNumber}${BROKER_DETAILS_ROOT}`; + }); + }); + + beforeEach(() => { + cy.saveSession(); + + cy.navigateToUrl(url); + }); + + after(() => { + cy.deleteApplication(referenceNumber); + }); + + it(`should redirect to ${BROKER_DETAILS_ROOT}`, () => { + cy.assertUrl(brokerDetailsUrl); + }); +}); diff --git a/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.test.ts b/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.test.ts index c1967f8c87..ff0c8c5f6d 100644 --- a/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.test.ts +++ b/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.test.ts @@ -10,6 +10,7 @@ import { Request, Response } from '../../../../../types'; import { mockReq, mockRes, mockApplication, referenceNumber } from '../../../../test-mocks'; const { + BROKER_DETAILS: { POSTCODE, BUILDING_NUMBER_OR_NAME }, BROKER_MANUAL_ADDRESS: { FULL_ADDRESS: FIELD_ID }, } = POLICY_FIELD_IDS; @@ -60,6 +61,66 @@ describe('controllers/insurance/policy/broker-confirm-address', () => { }); describe('get', () => { + describe(`when application.broker does not have ${POSTCODE}, ${BUILDING_NUMBER_OR_NAME} or ${FIELD_ID}`, () => { + beforeEach(() => { + res.locals.application = { + ...mockApplication, + broker: { + ...mockApplication.broker, + [POSTCODE]: '', + [BUILDING_NUMBER_OR_NAME]: '', + [FIELD_ID]: '', + }, + }; + }); + + it(`should redirect to ${BROKER_DETAILS_ROOT}`, () => { + get(req, res); + + expect(res.redirect).toHaveBeenCalledWith(`${INSURANCE_ROOT}/${referenceNumber}${BROKER_DETAILS_ROOT}`); + }); + }); + + describe(`when application.broker has ${POSTCODE} and ${BUILDING_NUMBER_OR_NAME}, no ${FIELD_ID}`, () => { + beforeEach(() => { + res.locals.application = { + ...mockApplication, + broker: { + ...mockApplication.broker, + [POSTCODE]: 'Mock postcode', + [BUILDING_NUMBER_OR_NAME]: 'Mock building name/number', + [FIELD_ID]: '', + }, + }; + }); + + it(`should NOT redirect to ${BROKER_DETAILS_ROOT}`, () => { + get(req, res); + + expect(res.redirect).toHaveBeenCalledTimes(0); + }); + }); + + describe(`when application.broker has ${FIELD_ID}, no ${POSTCODE} or ${BUILDING_NUMBER_OR_NAME}`, () => { + beforeEach(() => { + res.locals.application = { + ...mockApplication, + broker: { + ...mockApplication.broker, + [POSTCODE]: '', + [BUILDING_NUMBER_OR_NAME]: '', + [FIELD_ID]: 'Mock full address', + }, + }; + }); + + it(`should NOT redirect to ${BROKER_DETAILS_ROOT}`, () => { + get(req, res); + + expect(res.redirect).toHaveBeenCalledTimes(0); + }); + }); + it('should render template', () => { get(req, res); diff --git a/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.ts b/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.ts index 742e71d7ed..b93a2a39fe 100644 --- a/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.ts +++ b/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.ts @@ -47,6 +47,22 @@ export const get = (req: Request, res: Response) => { return res.redirect(PROBLEM_WITH_SERVICE); } + const { broker, referenceNumber } = application; + const { postcode, buildingNumberOrName, fullAddress } = broker; + + /** + * If a user manually navigates to this route, + * without providing previously required address data + * redirect the user back to BROKER_DETAILS, + * where the data can be submitted. + * NOTE: the required data is either address lookup fields, or a manual address entry field. + */ + const hasAddressLookupFields = postcode && buildingNumberOrName; + + if (!hasAddressLookupFields && !fullAddress) { + return res.redirect(`${INSURANCE_ROOT}/${referenceNumber}${BROKER_DETAILS_ROOT}`); + } + /** * Generate a submittedAnswer const * This is required because the address value is a single string with line break characters. @@ -61,7 +77,7 @@ export const get = (req: Request, res: Response) => { PAGE_CONTENT_STRINGS, BACK_LINK: req.headers.referer, }), - ...pageVariables(application.referenceNumber), + ...pageVariables(referenceNumber), userName: getUserNameFromSession(req.session.user), submittedAnswer, }); From 4143b8cc99c908f4419fdadbde5ad9df8e08750d Mon Sep 17 00:00:00 2001 From: ttbarnes Date: Fri, 17 Jan 2025 11:16:57 +0000 Subject: [PATCH 2/3] chore(EMS-4132): minor documentation improvement --- .../controllers/insurance/policy/broker-addresses/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/server/controllers/insurance/policy/broker-addresses/index.ts b/src/ui/server/controllers/insurance/policy/broker-addresses/index.ts index 271311edea..bbd41e80e4 100644 --- a/src/ui/server/controllers/insurance/policy/broker-addresses/index.ts +++ b/src/ui/server/controllers/insurance/policy/broker-addresses/index.ts @@ -87,7 +87,7 @@ export const get = async (req: Request, res: Response) => { /** * If a user manually navigates to this route, - * without providing previously required data, + * without providing previously required address data, * redirect the user back to BROKER_DETAILS, * where the data can be submitted. */ From 3832211a015bf61202fb1ae48fef8ace84c71db9 Mon Sep 17 00:00:00 2001 From: ttbarnes Date: Mon, 20 Jan 2025 10:32:14 +0000 Subject: [PATCH 3/3] chore(EMS-4132): improve unit tests --- .../broker-confirm-address/index.test.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.test.ts b/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.test.ts index ff0c8c5f6d..bbc23c5006 100644 --- a/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.test.ts +++ b/src/ui/server/controllers/insurance/policy/broker-confirm-address/index.test.ts @@ -121,6 +121,46 @@ describe('controllers/insurance/policy/broker-confirm-address', () => { }); }); + describe(`when application.broker has ${FIELD_ID} and ${POSTCODE}, no ${BUILDING_NUMBER_OR_NAME}`, () => { + beforeEach(() => { + res.locals.application = { + ...mockApplication, + broker: { + ...mockApplication.broker, + [POSTCODE]: 'Mock postcode', + [BUILDING_NUMBER_OR_NAME]: '', + [FIELD_ID]: 'Mock full address', + }, + }; + }); + + it(`should NOT redirect to ${BROKER_DETAILS_ROOT}`, () => { + get(req, res); + + expect(res.redirect).toHaveBeenCalledTimes(0); + }); + }); + + describe(`when application.broker has ${FIELD_ID} and ${BUILDING_NUMBER_OR_NAME}, no ${POSTCODE}`, () => { + beforeEach(() => { + res.locals.application = { + ...mockApplication, + broker: { + ...mockApplication.broker, + [POSTCODE]: '', + [BUILDING_NUMBER_OR_NAME]: 'Mock building name/number', + [FIELD_ID]: 'Mock full address', + }, + }; + }); + + it(`should NOT redirect to ${BROKER_DETAILS_ROOT}`, () => { + get(req, res); + + expect(res.redirect).toHaveBeenCalledTimes(0); + }); + }); + it('should render template', () => { get(req, res);