Skip to content

Commit

Permalink
fix(EMS-95): add 'problem with service' page to list of routes that d…
Browse files Browse the repository at this point in the history
…o not require session data checks
  • Loading branch information
ttbarnes committed Aug 22, 2022
1 parent 1b2e007 commit ac76d7d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/ui/server/middleware/required-data-provided.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FIELD_IDS, FIELD_VALUES, ROUTES } from '../constants';
import { mockReq, mockRes, mockSession } from '../test-mocks';
import { Request, Response } from '../../types';

const { ROOT, COOKIES, QUOTE } = ROUTES;
const { ROOT, COOKIES, PROBLEM_WITH_SERVICE, QUOTE } = ROUTES;

const {
BUYER_COUNTRY,
Expand Down Expand Up @@ -34,6 +34,7 @@ describe('middleware/required-data-provided', () => {
const expected = Object.values({
ROOT,
COOKIES,
PROBLEM_WITH_SERVICE,
...QUOTE,
});

Expand Down Expand Up @@ -206,7 +207,7 @@ describe('middleware/required-data-provided', () => {
});
});

describe(`when req.originalUrl is root ${BUYER_COUNTRY}`, () => {
describe(`when req.originalUrl is ${BUYER_COUNTRY}`, () => {
it('should call req.next', () => {
req.originalUrl = BUYER_COUNTRY;
requiredDataProvided(req, res, nextSpy);
Expand All @@ -215,7 +216,7 @@ describe('middleware/required-data-provided', () => {
});
});

describe(`when req.originalUrl is root ${NEED_TO_START_AGAIN}`, () => {
describe(`when req.originalUrl is ${NEED_TO_START_AGAIN}`, () => {
it('should call req.next', () => {
req.originalUrl = NEED_TO_START_AGAIN;
requiredDataProvided(req, res, nextSpy);
Expand All @@ -224,7 +225,7 @@ describe('middleware/required-data-provided', () => {
});
});

describe(`when req.originalUrl is root ${CANNOT_OBTAIN_COVER}`, () => {
describe(`when req.originalUrl is ${CANNOT_OBTAIN_COVER}`, () => {
it('should call req.next', () => {
req.originalUrl = CANNOT_OBTAIN_COVER;
requiredDataProvided(req, res, nextSpy);
Expand All @@ -233,7 +234,7 @@ describe('middleware/required-data-provided', () => {
});
});

describe(`when req.originalUrl is root ${GET_A_QUOTE_BY_EMAIL}`, () => {
describe(`when req.originalUrl is ${GET_A_QUOTE_BY_EMAIL}`, () => {
it('should call req.next', () => {
req.originalUrl = GET_A_QUOTE_BY_EMAIL;
requiredDataProvided(req, res, nextSpy);
Expand All @@ -242,7 +243,7 @@ describe('middleware/required-data-provided', () => {
});
});

describe(`when req.originalUrl is root ${COOKIES}`, () => {
describe(`when req.originalUrl is ${COOKIES}`, () => {
it('should call req.next', () => {
req.originalUrl = COOKIES;
requiredDataProvided(req, res, nextSpy);
Expand All @@ -251,6 +252,15 @@ describe('middleware/required-data-provided', () => {
});
});

describe(`when req.originalUrl is ${PROBLEM_WITH_SERVICE}`, () => {
it('should call req.next', () => {
req.originalUrl = PROBLEM_WITH_SERVICE;
requiredDataProvided(req, res, nextSpy);

expect(nextSpy).toHaveBeenCalled();
});
});

describe('when req.originalUrl contains `assets`', () => {
it('should call req.next', () => {
req.originalUrl = '/assets/styles.css';
Expand Down
13 changes: 10 additions & 3 deletions src/ui/server/middleware/required-data-provided.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FIELD_IDS, ROUTES } from '../constants';
import { Request, RequiredDataState, Response, SubmittedData } from '../../types';
import { isSinglePolicyType, isMultiPolicyType } from '../helpers/policy-type';

const { ROOT, COOKIES, QUOTE } = ROUTES;
const { ROOT, COOKIES, PROBLEM_WITH_SERVICE, QUOTE } = ROUTES;

const {
BUYER_COUNTRY,
Expand Down Expand Up @@ -31,6 +31,7 @@ export const getRoutesAsArray = (): Array<string> => {
const routes = {
ROOT,
COOKIES,
PROBLEM_WITH_SERVICE,
...QUOTE,
};

Expand Down Expand Up @@ -148,13 +149,19 @@ export const hasRequiredData = (route: string, submittedData: SubmittedData) =>
export const requiredDataProvided = (req: Request, res: Response, next: () => void) => {
const { originalUrl: url, method } = req;

// This business logic is irrelevant for these routes, assets and any request that is not a GET request.
// get all defined routes as an array
const routesArray = getRoutesAsArray();

const irrelevantRoutes = [ROOT, BUYER_COUNTRY, CANNOT_OBTAIN_COVER, GET_A_QUOTE_BY_EMAIL, COOKIES, NEED_TO_START_AGAIN];
// array of routes that do not require any data checks.
const irrelevantRoutes = [ROOT, BUYER_COUNTRY, CANNOT_OBTAIN_COVER, GET_A_QUOTE_BY_EMAIL, COOKIES, NEED_TO_START_AGAIN, PROBLEM_WITH_SERVICE];

const isIrrelevantRoute = (route: string) => irrelevantRoutes.includes(route);

// do not run any data checks if the requested route is one of the following:
// is a route that does nout require any data checks
// is assets
// is 404 page or 'problem with service' page
// or the request is not a GET request.
if (isIrrelevantRoute(url) || url.includes('/assets') || !routeIsKnown(routesArray, url) || method !== 'GET') {
return next();
}
Expand Down

0 comments on commit ac76d7d

Please sign in to comment.