From ac76d7dbe1462746ce617a85b0be5930068c9dcf Mon Sep 17 00:00:00 2001 From: ttbarnes Date: Mon, 22 Aug 2022 10:13:22 +0100 Subject: [PATCH] fix(EMS-95): add 'problem with service' page to list of routes that do not require session data checks --- .../middleware/required-data-provided.test.ts | 22 ++++++++++++++----- .../middleware/required-data-provided.ts | 13 ++++++++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/ui/server/middleware/required-data-provided.test.ts b/src/ui/server/middleware/required-data-provided.test.ts index 3132956be4..4398e783fe 100644 --- a/src/ui/server/middleware/required-data-provided.test.ts +++ b/src/ui/server/middleware/required-data-provided.test.ts @@ -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, @@ -34,6 +34,7 @@ describe('middleware/required-data-provided', () => { const expected = Object.values({ ROOT, COOKIES, + PROBLEM_WITH_SERVICE, ...QUOTE, }); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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'; diff --git a/src/ui/server/middleware/required-data-provided.ts b/src/ui/server/middleware/required-data-provided.ts index d7a0e7edbd..30cd20f551 100644 --- a/src/ui/server/middleware/required-data-provided.ts +++ b/src/ui/server/middleware/required-data-provided.ts @@ -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, @@ -31,6 +31,7 @@ export const getRoutesAsArray = (): Array => { const routes = { ROOT, COOKIES, + PROBLEM_WITH_SERVICE, ...QUOTE, }; @@ -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(); }