Skip to content
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

chore(EMS-4132): broker - confirm address - data checks #3487

Merged
merged 5 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
abhi-markan marked this conversation as resolved.
Show resolved Hide resolved

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);
abhi-markan marked this conversation as resolved.
Show resolved Hide resolved
});

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(() => {
abhi-markan marked this conversation as resolved.
Show resolved Hide resolved
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
Loading