Skip to content

Commit

Permalink
chore(EMS-4132): broker - confirm address - data checks (#3487)
Browse files Browse the repository at this point in the history
* chore(EMS-4132): broker - confirm address - data checks

* chore(EMS-4132): minor documentation improvement

* chore(EMS-4132): improve unit tests
  • Loading branch information
ttbarnes authored Jan 20, 2025
1 parent f32ebd8 commit b958d11
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
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';
import generateBrokerAddressInsetTextHtml from '../../../../helpers/generate-broker-address-inset-text-html';
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,
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,30 @@ 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, {
...insuranceCorePageVariables({
PAGE_CONTENT_STRINGS,
BACK_LINK: req.headers.referer,
}),
...pageVariables(application.referenceNumber),
...pageVariables(referenceNumber),
userName: getUserNameFromSession(req.session.user),
submittedAnswer,
});
Expand Down

0 comments on commit b958d11

Please sign in to comment.