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-addresses/index.ts b/src/ui/server/controllers/insurance/policy/broker-addresses/index.ts index 8ea2cb85c3..34c629cb83 100644 --- a/src/ui/server/controllers/insurance/policy/broker-addresses/index.ts +++ b/src/ui/server/controllers/insurance/policy/broker-addresses/index.ts @@ -88,7 +88,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. */ 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 ab4cfc30b5..c9da64551e 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 @@ -1,6 +1,7 @@ import { pageVariables, PAGE_CONTENT_STRINGS, TEMPLATE, get, post } from '.'; import { PAGES } from '../../../../content-strings'; import { TEMPLATES } from '../../../../constants'; +import { POLICY as POLICY_FIELD_IDS } from '../../../../constants/field-ids/insurance/policy'; import { INSURANCE_ROUTES } from '../../../../constants/routes/insurance'; import insuranceCorePageVariables from '../../../../helpers/page-variables/core/insurance'; import getUserNameFromSession from '../../../../helpers/get-user-name-from-session'; @@ -8,6 +9,11 @@ import generateBrokerAddressInsetTextHtml from '../../../../helpers/generate-bro 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; + const { INSURANCE_ROOT, ALL_SECTIONS, @@ -55,6 +61,106 @@ 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); + }); + }); + + 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); 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 4856011800..3f5259ce39 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 @@ -42,6 +42,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}`); + } + const submittedAnswer = generateBrokerAddressInsetTextHtml(application.broker); return res.render(TEMPLATE, { @@ -49,7 +65,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, });