diff --git a/src/app.ts b/src/app.ts index b9ab1fa3..1660ceda 100755 --- a/src/app.ts +++ b/src/app.ts @@ -5,6 +5,7 @@ import logger from "./utils/logger"; import routerDispatch from "./router.dispatch"; import cookieParser from "cookie-parser"; import { authenticationMiddleware } from "./middleware/authentication_middleware"; +import { authenticationMiddlewareForSoleTrader } from "./middleware/authentication_middleware_sole_trader"; import { sessionMiddleware } from "./middleware/session_middleware"; import { @@ -16,7 +17,7 @@ import { PIWIK_URL, PIWIK_SITE_ID } from "./utils/properties"; -import { BASE_URL, HEALTHCHECK, ACCESSIBILITY_STATEMENT } from "./types/pageURL"; +import { BASE_URL, SOLE_TRADER, HEALTHCHECK, ACCESSIBILITY_STATEMENT } from "./types/pageURL"; import { commonTemplateVariablesMiddleware } from "./middleware/common_variables_middleware"; import { getLocalesService, selectLang } from "./utils/localise"; import { ErrorService } from "./services/errorService"; @@ -54,7 +55,8 @@ app.use(express.static(path.join(__dirname, "/../assets/public"))); // Apply middleware app.use(cookieParser()); app.use(`^(?!(${BASE_URL}${HEALTHCHECK}|${BASE_URL}$|${BASE_URL}${ACCESSIBILITY_STATEMENT}))*`, sessionMiddleware); -app.use(`^(?!(${BASE_URL}${HEALTHCHECK}|${BASE_URL}$|${BASE_URL}${ACCESSIBILITY_STATEMENT}))*`, authenticationMiddleware); +app.use(`^(?!(${BASE_URL}${HEALTHCHECK}|${BASE_URL}$|${BASE_URL}${ACCESSIBILITY_STATEMENT})|(${BASE_URL}${SOLE_TRADER}))*`, authenticationMiddleware); +app.use(`^(${BASE_URL}${SOLE_TRADER})*`, authenticationMiddlewareForSoleTrader); app.use(commonTemplateVariablesMiddleware); // Company Auth redirect diff --git a/src/middleware/authentication_middleware.ts b/src/middleware/authentication_middleware.ts index 3b76e84d..00bc0314 100644 --- a/src/middleware/authentication_middleware.ts +++ b/src/middleware/authentication_middleware.ts @@ -1,7 +1,7 @@ import { NextFunction, Request, Response } from "express"; -import { AuthOptions, acspProfileCreateAuthMiddleware } from "@companieshouse/web-security-node"; - -import { CHS_URL } from "../utils/properties"; +import { AuthOptions, authMiddleware, acspProfileCreateAuthMiddleware } from "@companieshouse/web-security-node"; +import { isActiveFeature } from "../utils/feature.flag"; +import { CHS_URL, FEATURE_FLAG_VERIFY_SOLE_TRADER_ONLY } from "../utils/properties"; import { BASE_URL, CHECK_SAVED_APPLICATION } from "../types/pageURL"; export const authenticationMiddleware = (req: Request, res: Response, next: NextFunction) => { @@ -11,5 +11,7 @@ export const authenticationMiddleware = (req: Request, res: Response, next: Next returnUrl: BASE_URL + CHECK_SAVED_APPLICATION }; - return acspProfileCreateAuthMiddleware(authMiddlewareConfig)(req, res, next); + return isActiveFeature(FEATURE_FLAG_VERIFY_SOLE_TRADER_ONLY) + ? authMiddleware(authMiddlewareConfig)(req, res, next) + : acspProfileCreateAuthMiddleware(authMiddlewareConfig)(req, res, next); }; diff --git a/src/middleware/authentication_middleware_sole_trader.ts b/src/middleware/authentication_middleware_sole_trader.ts new file mode 100644 index 00000000..47acd90c --- /dev/null +++ b/src/middleware/authentication_middleware_sole_trader.ts @@ -0,0 +1,16 @@ +import { NextFunction, Request, Response } from "express"; +import { AuthOptions, acspProfileCreateAuthMiddleware } from "@companieshouse/web-security-node"; + +import { CHS_URL } from "../utils/properties"; +import { BASE_URL, SOLE_TRADER_WHAT_IS_YOUR_ROLE } from "../types/pageURL"; + +export const authenticationMiddlewareForSoleTrader = (req: Request, res: Response, next: NextFunction) => { + + const authMiddlewareConfig: AuthOptions = { + chsWebUrl: CHS_URL, + returnUrl: BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_ROLE + }; + + return acspProfileCreateAuthMiddleware(authMiddlewareConfig)(req, res, next); + +}; diff --git a/src/utils/properties.ts b/src/utils/properties.ts index de6a4f4c..1671560c 100644 --- a/src/utils/properties.ts +++ b/src/utils/properties.ts @@ -49,3 +49,5 @@ export const PIWIK_START_GOAL_ID = getEnvironmentValue("PIWIK_START_GOAL_ID", "4 export const FEATURE_FLAG_DISABLE_LIMITED_JOURNEY = getEnvironmentValue("FEATURE_FLAG_DISABLE_LIMITED_JOURNEY", "false"); export const FEATURE_FLAG_DISABLE_PARTNERSHIP_JOURNEY = getEnvironmentValue("FEATURE_FLAG_DISABLE_PARTNERSHIP_JOURNEY", "false"); + +export const FEATURE_FLAG_VERIFY_SOLE_TRADER_ONLY = getEnvironmentValue("FEATURE_FLAG_VERIFY_SOLE_TRADER_ONLY", "false"); diff --git a/test/mocks/all_middleware_mock.ts b/test/mocks/all_middleware_mock.ts index 6235d2e5..08ba6ad0 100644 --- a/test/mocks/all_middleware_mock.ts +++ b/test/mocks/all_middleware_mock.ts @@ -1,10 +1,12 @@ import mockAuthenticationMiddleware from "./authentication_middleware_mock"; +import mockAuthenticationMiddlewareForSoleTrader from "./authentication_middleware_sole_trader_mock"; import mockSessionMiddleware from "./session_middleware_mock"; import mockCompanyAuthenticationMiddleware from "./company_authentication_middleware_mock"; import mockUKAddressesFromPostcode from "./postcode_lookup_service_mock"; export default { mockAuthenticationMiddleware, + mockAuthenticationMiddlewareForSoleTrader, mockSessionMiddleware, mockCompanyAuthenticationMiddleware, mockUKAddressesFromPostcode diff --git a/test/mocks/authentication_middleware_sole_trader_mock.ts b/test/mocks/authentication_middleware_sole_trader_mock.ts new file mode 100644 index 00000000..2f9d02eb --- /dev/null +++ b/test/mocks/authentication_middleware_sole_trader_mock.ts @@ -0,0 +1,11 @@ +import { NextFunction, Request, Response } from "express"; +import { authenticationMiddlewareForSoleTrader } from "../../src/middleware/authentication_middleware_sole_trader"; +jest.mock("../../src/middleware/authentication_middleware_sole_trader"); + +// get handle on mocked function +const mockAuthenticationMiddlewareForSoleTrader = authenticationMiddlewareForSoleTrader as jest.Mock; + +// tell the mock what to return +mockAuthenticationMiddlewareForSoleTrader.mockImplementation((req: Request, res: Response, next: NextFunction) => next()); + +export default mockAuthenticationMiddlewareForSoleTrader; diff --git a/test/src/controllers/soleTrader/correspodanceAddressDetailsController.test.ts b/test/src/controllers/soleTrader/correspodanceAddressDetailsController.test.ts index 34e09ead..a70c0e5d 100644 --- a/test/src/controllers/soleTrader/correspodanceAddressDetailsController.test.ts +++ b/test/src/controllers/soleTrader/correspodanceAddressDetailsController.test.ts @@ -25,7 +25,7 @@ describe("GET" + SOLE_TRADER_AUTO_LOOKUP_ADDRESS_LIST, () => { const res = await router.get(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS_LIST); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("John Doe"); expect(res.text).toContain("Select the correspondence address"); }); @@ -34,7 +34,7 @@ describe("GET" + SOLE_TRADER_AUTO_LOOKUP_ADDRESS_LIST, () => { const res = await router.get(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS_LIST); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should return status 200 when applicantDetails is undefined", async () => { @@ -45,14 +45,14 @@ describe("GET" + SOLE_TRADER_AUTO_LOOKUP_ADDRESS_LIST, () => { const res = await router.get(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS_LIST); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should return status 200", async () => { const res = await router.get(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS_LIST); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should render the error page if an error is thrown in get function", async () => { mockGetAcspRegistration.mockImplementationOnce(() => { throw new Error(); }); diff --git a/test/src/controllers/soleTrader/correspondenceAddressAutoLookupController.test.ts b/test/src/controllers/soleTrader/correspondenceAddressAutoLookupController.test.ts index 02c76d2b..c7c55248 100644 --- a/test/src/controllers/soleTrader/correspondenceAddressAutoLookupController.test.ts +++ b/test/src/controllers/soleTrader/correspondenceAddressAutoLookupController.test.ts @@ -45,7 +45,7 @@ describe("Correspondence address auto look up tests", () => { const res = await router.get(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("What is the correspondence address?"); }); @@ -54,7 +54,7 @@ describe("Correspondence address auto look up tests", () => { const res = await router.get(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("What is the correspondence address?"); }); @@ -67,14 +67,14 @@ describe("Correspondence address auto look up tests", () => { const res = await router.get(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("What is the correspondence address?"); }); it("should return status 500 after calling GET endpoint and failing", async () => { mockGetAcspRegistration.mockRejectedValueOnce(new Error("Error getting data")); const res = await router.get(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(500); expect(res.text).toContain("Sorry we are experiencing technical difficulties"); }); @@ -98,7 +98,7 @@ describe("POST" + SOLE_TRADER_AUTO_LOOKUP_ADDRESS, () => { mockPutAcspRegistration.mockResolvedValueOnce(acspData); expect(res.status).toBe(302); // Expect a redirect status code expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.header.location).toBe(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS_LIST + "?lang=en"); }); @@ -118,7 +118,7 @@ describe("POST" + SOLE_TRADER_AUTO_LOOKUP_ADDRESS, () => { const res = await router.post(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS).send(formData); expect(res.status).toBe(302); // Expect a redirect status code expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.header.location).toBe(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM + "?lang=en"); }); @@ -133,7 +133,7 @@ describe("POST" + SOLE_TRADER_AUTO_LOOKUP_ADDRESS, () => { const res = await router.post(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS).send(formData); expect(res.status).toBe(302); // Expect a redirect status code expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.header.location).toBe(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM + "?lang=en"); }); @@ -260,7 +260,7 @@ describe("POST" + SOLE_TRADER_AUTO_LOOKUP_ADDRESS, () => { const res = await router.post(BASE_URL + SOLE_TRADER_AUTO_LOOKUP_ADDRESS).send(formData); expect(res.status).toBe(302); // Expect a redirect status code expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.header.location).toBe(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM + "?lang=en"); }); }); diff --git a/test/src/controllers/soleTrader/correspondenceAddressConfirmController.test.ts b/test/src/controllers/soleTrader/correspondenceAddressConfirmController.test.ts index f84be56b..4ad08bd5 100644 --- a/test/src/controllers/soleTrader/correspondenceAddressConfirmController.test.ts +++ b/test/src/controllers/soleTrader/correspondenceAddressConfirmController.test.ts @@ -43,7 +43,7 @@ describe("GET" + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM, () => { mockGetAcspRegistration.mockResolvedValueOnce(acspData); const res = await router.get(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(200); expect(res.text).toContain("John Doe"); expect(res.text).toContain("Confirm the correspondence address"); @@ -53,7 +53,7 @@ describe("GET" + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM, () => { it("should return status 200 when acspData is undefined", async () => { const res = await router.get(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(200); }); @@ -65,7 +65,7 @@ describe("GET" + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM, () => { mockGetAcspRegistration.mockResolvedValueOnce(acspDataWithoutApplicantDetails); const res = await router.get(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(200); }); @@ -81,7 +81,7 @@ describe("POST SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM", () => { it("should redirect to /select-aml-supervisor with status 302", async () => { const res = await router.post(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(302); expect(res.header.location).toBe(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_EMAIL + "?lang=en"); }); @@ -157,7 +157,7 @@ describe("POST SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM", () => { createMockSessionMiddleware(acspDataSameAddress); const res = await router.post(BASE_URL + SOLE_TRADER_CORRESPONDENCE_ADDRESS_CONFIRM).send(formData); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(302); expect(res.header.location).toBe(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_EMAIL + "?lang=en"); }); diff --git a/test/src/controllers/soleTrader/correspondenceAddressManualController.test.ts b/test/src/controllers/soleTrader/correspondenceAddressManualController.test.ts index ec3a08bc..f2cac7ac 100644 --- a/test/src/controllers/soleTrader/correspondenceAddressManualController.test.ts +++ b/test/src/controllers/soleTrader/correspondenceAddressManualController.test.ts @@ -24,7 +24,7 @@ describe("GET" + SOLE_TRADER_MANUAL_CORRESPONDENCE_ADDRESS, () => { mockGetAcspRegistration.mockResolvedValueOnce(acspData); const res = await router.get(BASE_URL + SOLE_TRADER_MANUAL_CORRESPONDENCE_ADDRESS); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(200); expect(res.text).toContain("Enter the correspondence address"); }); @@ -32,7 +32,7 @@ describe("GET" + SOLE_TRADER_MANUAL_CORRESPONDENCE_ADDRESS, () => { it("should return status 200 when acspData is undefined", async () => { const res = await router.get(BASE_URL + SOLE_TRADER_MANUAL_CORRESPONDENCE_ADDRESS); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(200); expect(res.text).toContain("Enter the correspondence address"); }); @@ -45,7 +45,7 @@ describe("GET" + SOLE_TRADER_MANUAL_CORRESPONDENCE_ADDRESS, () => { mockGetAcspRegistration.mockResolvedValueOnce(acspDataWithoutApplicantDetails); const res = await router.get(BASE_URL + SOLE_TRADER_MANUAL_CORRESPONDENCE_ADDRESS); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(200); expect(res.text).toContain("Enter the correspondence address"); }); diff --git a/test/src/controllers/soleTrader/dateOfBirthController.test.ts b/test/src/controllers/soleTrader/dateOfBirthController.test.ts index 19663c4f..580cfe8e 100644 --- a/test/src/controllers/soleTrader/dateOfBirthController.test.ts +++ b/test/src/controllers/soleTrader/dateOfBirthController.test.ts @@ -32,7 +32,7 @@ describe("GET" + SOLE_TRADER_DATE_OF_BIRTH, () => { const res = await router.get(BASE_URL + SOLE_TRADER_DATE_OF_BIRTH); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("What is your date of birth?"); }); @@ -41,7 +41,7 @@ describe("GET" + SOLE_TRADER_DATE_OF_BIRTH, () => { const res = await router.get(BASE_URL + SOLE_TRADER_DATE_OF_BIRTH); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("What is your date of birth?"); }); diff --git a/test/src/controllers/soleTrader/nameController.test.ts b/test/src/controllers/soleTrader/nameController.test.ts index c1d89c50..a2f9089a 100644 --- a/test/src/controllers/soleTrader/nameController.test.ts +++ b/test/src/controllers/soleTrader/nameController.test.ts @@ -23,7 +23,7 @@ describe("GET" + SOLE_TRADER_WHAT_IS_YOUR_NAME, () => { const res = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_NAME); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should return status 200", async () => { @@ -31,7 +31,7 @@ describe("GET" + SOLE_TRADER_WHAT_IS_YOUR_NAME, () => { const res = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_NAME); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should return status 200 when applicantDetails is undefined", async () => { @@ -42,7 +42,7 @@ describe("GET" + SOLE_TRADER_WHAT_IS_YOUR_NAME, () => { const res = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_NAME); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("catch error when rendering the page", async () => { @@ -66,7 +66,7 @@ describe("POST" + SOLE_TRADER_WHAT_IS_YOUR_NAME, () => { }); expect(res.status).toBe(302); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); // Test for incorrect form details entered, will return 400. diff --git a/test/src/controllers/soleTrader/nationalityController.test.ts b/test/src/controllers/soleTrader/nationalityController.test.ts index 245dd3d3..071895c6 100644 --- a/test/src/controllers/soleTrader/nationalityController.test.ts +++ b/test/src/controllers/soleTrader/nationalityController.test.ts @@ -35,7 +35,7 @@ describe("GET" + SOLE_TRADER_WHAT_IS_YOUR_NATIONALITY, () => { const res = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_NATIONALITY); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("What is your nationality?"); }); @@ -48,7 +48,7 @@ describe("GET" + SOLE_TRADER_WHAT_IS_YOUR_NATIONALITY, () => { const res = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_NATIONALITY); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("catch error when rendering the page", async () => { diff --git a/test/src/controllers/soleTrader/sectorYouWorkInController.test.ts b/test/src/controllers/soleTrader/sectorYouWorkInController.test.ts index 1d7828cc..1a023ce9 100644 --- a/test/src/controllers/soleTrader/sectorYouWorkInController.test.ts +++ b/test/src/controllers/soleTrader/sectorYouWorkInController.test.ts @@ -25,7 +25,7 @@ describe("GET" + SOLE_TRADER_SECTOR_YOU_WORK_IN, () => { mockGetAcspRegistration.mockResolvedValueOnce(acspData); await router.get(BASE_URL + SOLE_TRADER_SECTOR_YOU_WORK_IN).expect(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should return status 200", async () => { @@ -36,7 +36,7 @@ describe("GET" + SOLE_TRADER_SECTOR_YOU_WORK_IN, () => { mockGetAcspRegistration.mockResolvedValueOnce(acspDataWithoutApplicantDetails); await router.get(BASE_URL + SOLE_TRADER_SECTOR_YOU_WORK_IN).expect(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("catch error when rendering the page", async () => { diff --git a/test/src/controllers/soleTrader/selectAmlSupervisorController.test.ts b/test/src/controllers/soleTrader/selectAmlSupervisorController.test.ts index f802810e..6e77520d 100644 --- a/test/src/controllers/soleTrader/selectAmlSupervisorController.test.ts +++ b/test/src/controllers/soleTrader/selectAmlSupervisorController.test.ts @@ -27,7 +27,7 @@ describe("GET" + SOLE_TRADER_SELECT_AML_SUPERVISOR, () => { const res = await router.get(BASE_URL + SOLE_TRADER_SELECT_AML_SUPERVISOR); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("Which Anti-Money Laundering (AML) supervisory bodies are you registered with?"); }); @@ -40,7 +40,7 @@ describe("GET" + SOLE_TRADER_SELECT_AML_SUPERVISOR, () => { const res = await router.get(BASE_URL + SOLE_TRADER_SELECT_AML_SUPERVISOR); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("Which Anti-Money Laundering (AML) supervisory bodies are you registered with?"); }); @@ -59,7 +59,7 @@ describe("POST" + SOLE_TRADER_SELECT_AML_SUPERVISOR, () => { const res = await router.post(BASE_URL + SOLE_TRADER_SELECT_AML_SUPERVISOR).send({ "AML-supervisory-bodies": "ACCA" }); expect(res.status).toBe(302); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.header.location).toBe(BASE_URL + AML_MEMBERSHIP_NUMBER + "?lang=en"); }); diff --git a/test/src/controllers/soleTrader/whatIsTheBusinessName.test.ts b/test/src/controllers/soleTrader/whatIsTheBusinessName.test.ts index 8d0d98cd..7296491d 100644 --- a/test/src/controllers/soleTrader/whatIsTheBusinessName.test.ts +++ b/test/src/controllers/soleTrader/whatIsTheBusinessName.test.ts @@ -30,7 +30,7 @@ describe("GET" + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME, () => { const res = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should return status 200 when applicantDetails is undefined", async () => { @@ -42,7 +42,7 @@ describe("GET" + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME, () => { const res = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("catch error when rendering the page", async () => { @@ -71,7 +71,7 @@ describe("POST" + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME, () => { expect(response.status).toBe(302); // Expect a redirect status code expect(response.header.location).toBe(BASE_URL + SOLE_TRADER_SECTOR_YOU_WORK_IN + "?lang=en"); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should redirect with status 302 on successful form submission", async () => { @@ -90,7 +90,7 @@ describe("POST" + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME, () => { expect(response.status).toBe(302); // Expect a redirect status code expect(response.header.location).toBe(BASE_URL + SOLE_TRADER_SECTOR_YOU_WORK_IN + "?lang=en"); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should redirect with status 302 on successful form submission", async () => { @@ -104,7 +104,7 @@ describe("POST" + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME, () => { expect(response.status).toBe(302); // Expect a redirect status code expect(response.header.location).toBe(BASE_URL + SOLE_TRADER_SECTOR_YOU_WORK_IN + "?lang=en"); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should return status 400 for incorrect data entered", async () => { @@ -122,7 +122,7 @@ describe("POST" + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME, () => { expect(response.status).toBe(400); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(response.text).toContain("Select business name"); }); @@ -141,7 +141,7 @@ describe("POST" + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME, () => { expect(response.status).toBe(400); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(response.text).toContain("Enter the business name"); }); @@ -160,7 +160,7 @@ describe("POST" + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME, () => { expect(response.status).toBe(400); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(response.text).toContain("Business name must only include letters a to z, and common special characters such as hyphens, spaces and apostrophes"); }); @@ -180,7 +180,7 @@ describe("POST" + SOLE_TRADER_WHAT_IS_THE_BUSINESS_NAME, () => { expect(response.status).toBe(400); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(response.text).toContain("Business name must be 155 characters or less"); }); diff --git a/test/src/controllers/soleTrader/whatIsYourEmailAddressController.test.ts b/test/src/controllers/soleTrader/whatIsYourEmailAddressController.test.ts index 395de0fb..1c2859ac 100644 --- a/test/src/controllers/soleTrader/whatIsYourEmailAddressController.test.ts +++ b/test/src/controllers/soleTrader/whatIsYourEmailAddressController.test.ts @@ -22,7 +22,7 @@ describe("GET" + SOLE_TRADER_WHAT_IS_YOUR_EMAIL, () => { mockGetAcspRegistration.mockResolvedValueOnce(acspData); const res = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_EMAIL); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(200); expect(res.text).toContain("What email address should we use for correspondence?"); }); @@ -31,7 +31,7 @@ describe("GET" + SOLE_TRADER_WHAT_IS_YOUR_EMAIL, () => { mockGetAcspRegistration.mockRejectedValueOnce(new Error("Error getting data")); const res = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_EMAIL); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.status).toBe(500); expect(res.text).toContain("Sorry we are experiencing technical difficulties"); }); diff --git a/test/src/controllers/soleTrader/whatIsYourRoleController.test.ts b/test/src/controllers/soleTrader/whatIsYourRoleController.test.ts index 32b9e5a4..ba8c7ea6 100644 --- a/test/src/controllers/soleTrader/whatIsYourRoleController.test.ts +++ b/test/src/controllers/soleTrader/whatIsYourRoleController.test.ts @@ -32,7 +32,7 @@ describe("Statement Relevant Officer Router", () => { const response = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_ROLE); expect(response.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(response.text).toContain("What is your role in the business?"); }); @@ -40,7 +40,7 @@ describe("Statement Relevant Officer Router", () => { const response = await router.get(BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_ROLE); expect(response.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("catch error when rendering the page", async () => { @@ -78,7 +78,7 @@ describe("POST " + SOLE_TRADER_WHAT_IS_YOUR_ROLE, () => { expect(response.status).toBe(400); expect(response.text).toContain("Select if you are the sole trader or someone else"); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should show the error page if an error occurs during PUT request", async () => { diff --git a/test/src/controllers/soleTrader/whereDoYouLiveController.test.ts b/test/src/controllers/soleTrader/whereDoYouLiveController.test.ts index 2075783c..8975cfec 100644 --- a/test/src/controllers/soleTrader/whereDoYouLiveController.test.ts +++ b/test/src/controllers/soleTrader/whereDoYouLiveController.test.ts @@ -33,7 +33,7 @@ describe("GET" + SOLE_TRADER_WHERE_DO_YOU_LIVE, () => { const res = await router.get(BASE_URL + SOLE_TRADER_WHERE_DO_YOU_LIVE); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); expect(res.text).toContain("Where do you live?"); }); @@ -46,7 +46,7 @@ describe("GET" + SOLE_TRADER_WHERE_DO_YOU_LIVE, () => { const res = await router.get(BASE_URL + SOLE_TRADER_WHERE_DO_YOU_LIVE); expect(res.status).toBe(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("catch error when rendering the page", async () => { diff --git a/test/src/controllers/soleTrader/whichSectorOtherController.test.ts b/test/src/controllers/soleTrader/whichSectorOtherController.test.ts index 5e2c40ee..e78a8670 100644 --- a/test/src/controllers/soleTrader/whichSectorOtherController.test.ts +++ b/test/src/controllers/soleTrader/whichSectorOtherController.test.ts @@ -25,13 +25,13 @@ describe("GET" + SOLE_TRADER_WHICH_SECTOR_OTHER, () => { mockGetAcspRegistration.mockResolvedValueOnce(acspData); await router.get(BASE_URL + SOLE_TRADER_WHICH_SECTOR_OTHER).expect(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should return status 200", async () => { await router.get(BASE_URL + SOLE_TRADER_WHICH_SECTOR_OTHER).expect(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("should return status 200", async () => { @@ -41,7 +41,7 @@ describe("GET" + SOLE_TRADER_WHICH_SECTOR_OTHER, () => { mockGetAcspRegistration.mockResolvedValueOnce(acspDataWithoutApplicantDetails); await router.get(BASE_URL + SOLE_TRADER_WHICH_SECTOR_OTHER).expect(200); expect(mocks.mockSessionMiddleware).toHaveBeenCalled(); - expect(mocks.mockAuthenticationMiddleware).toHaveBeenCalled(); + expect(mocks.mockAuthenticationMiddlewareForSoleTrader).toHaveBeenCalled(); }); it("catch error when rendering the page", async () => { diff --git a/test/src/middleware/authentication_middleware.test.ts b/test/src/middleware/authentication_middleware.test.ts index 6c29986b..66cf3bb3 100644 --- a/test/src/middleware/authentication_middleware.test.ts +++ b/test/src/middleware/authentication_middleware.test.ts @@ -1,18 +1,21 @@ /* eslint-disable import/first */ - jest.mock("@companieshouse/web-security-node"); - -import { acspProfileCreateAuthMiddleware, AuthOptions } from "@companieshouse/web-security-node"; +process.env.FEATURE_FLAG_VERIFY_SOLE_TRADER_ONLY = "false"; +import { acspProfileCreateAuthMiddleware, authMiddleware, AuthOptions } from "@companieshouse/web-security-node"; import { Request, Response } from "express"; import { authenticationMiddleware } from "../../../src/middleware/authentication_middleware"; import { BASE_URL, CHECK_SAVED_APPLICATION } from "../../../src/types/pageURL"; // get handle on mocked function and create mock function to be returned from calling authMiddleware -const mockAuthMiddleware = acspProfileCreateAuthMiddleware as jest.Mock; -const mockAuthReturnedFunction = jest.fn(); +const mockAuthMiddleware = authMiddleware as jest.Mock; +const mockAuthReturnedFunctionAuthMiddleware = jest.fn(); + +const mockAcspProfileCreateAuthMiddleware = acspProfileCreateAuthMiddleware as jest.Mock; +const mockAuthReturnedFunctionAcspProfileCreateAuthMiddleware = jest.fn(); // when the mocked authMiddleware is called, make it return a mocked function so we can verify it gets called -mockAuthMiddleware.mockReturnValue(mockAuthReturnedFunction); +mockAuthMiddleware.mockReturnValue(mockAuthReturnedFunctionAuthMiddleware); +mockAcspProfileCreateAuthMiddleware.mockReturnValue(mockAuthReturnedFunctionAcspProfileCreateAuthMiddleware); const req: Request = {} as Request; const res: Response = {} as Response; @@ -24,9 +27,10 @@ const expectedAuthMiddlewareConfig: AuthOptions = { }; describe("authentication middleware tests", () => { - it("should call CH authentication library", () => { + it("should call CH authentication library", async () => { authenticationMiddleware(req, res, next); - expect(mockAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfig); - expect(mockAuthReturnedFunction).toHaveBeenCalledWith(req, res, next); + expect(mockAcspProfileCreateAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfig); + expect(mockAuthReturnedFunctionAcspProfileCreateAuthMiddleware).toHaveBeenCalledWith(req, res, next); }); + }); diff --git a/test/src/middleware/authentication_middleware_disabled_verification.test.ts b/test/src/middleware/authentication_middleware_disabled_verification.test.ts new file mode 100644 index 00000000..d72b7ec6 --- /dev/null +++ b/test/src/middleware/authentication_middleware_disabled_verification.test.ts @@ -0,0 +1,37 @@ +/* eslint-disable import/first */ +jest.mock("@companieshouse/web-security-node"); +process.env.FEATURE_FLAG_VERIFY_SOLE_TRADER_ONLY = "true"; +import { acspProfileCreateAuthMiddleware, authMiddleware, AuthOptions } from "@companieshouse/web-security-node"; +import { Request, Response } from "express"; +import { BASE_URL, CHECK_SAVED_APPLICATION } from "../../../src/types/pageURL"; +import { authenticationMiddleware } from "../../../src/middleware/authentication_middleware"; + +// get handle on mocked function and create mock function to be returned from calling authMiddleware +const mockAuthMiddleware = authMiddleware as jest.Mock; +const mockAuthReturnedFunctionAuthMiddleware = jest.fn(); + +const mockAcspProfileCreateAuthMiddleware = acspProfileCreateAuthMiddleware as jest.Mock; +const mockAuthReturnedFunctionAcspProfileCreateAuthMiddleware = jest.fn(); + +// when the mocked authMiddleware is called, make it return a mocked function so we can verify it gets called +mockAuthMiddleware.mockReturnValue(mockAuthReturnedFunctionAuthMiddleware); +mockAcspProfileCreateAuthMiddleware.mockReturnValue(mockAuthReturnedFunctionAcspProfileCreateAuthMiddleware); + +const req: Request = {} as Request; +const res: Response = {} as Response; +const next = jest.fn(); + +const expectedAuthMiddlewareConfig: AuthOptions = { + chsWebUrl: "http://chs.local", + returnUrl: BASE_URL + CHECK_SAVED_APPLICATION +}; + +describe("authentication middleware tests", () => { + it("should call CH authentication library", async () => { + authenticationMiddleware(req, res, next); + authenticationMiddleware(req, res, next); + expect(mockAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfig); + expect(mockAuthReturnedFunctionAuthMiddleware).toHaveBeenCalledWith(req, res, next); + }); + +}); diff --git a/test/src/middleware/authentication_middleware_sole_trader.test.ts b/test/src/middleware/authentication_middleware_sole_trader.test.ts new file mode 100644 index 00000000..0c354a72 --- /dev/null +++ b/test/src/middleware/authentication_middleware_sole_trader.test.ts @@ -0,0 +1,32 @@ +/* eslint-disable import/first */ + +jest.mock("@companieshouse/web-security-node"); + +import { acspProfileCreateAuthMiddleware, AuthOptions } from "@companieshouse/web-security-node"; +import { Request, Response } from "express"; +import { authenticationMiddlewareForSoleTrader } from "../../../src/middleware/authentication_middleware_sole_trader"; +import { BASE_URL, SOLE_TRADER_WHAT_IS_YOUR_ROLE } from "../../../src/types/pageURL"; + +// get handle on mocked function and create mock function to be returned from calling authMiddleware +const mockAuthMiddleware = acspProfileCreateAuthMiddleware as jest.Mock; +const mockAuthReturnedFunction = jest.fn(); + +// when the mocked authMiddleware is called, make it return a mocked function so we can verify it gets called +mockAuthMiddleware.mockReturnValue(mockAuthReturnedFunction); + +const req: Request = {} as Request; +const res: Response = {} as Response; +const next = jest.fn(); + +const expectedAuthMiddlewareConfig: AuthOptions = { + chsWebUrl: "http://chs.local", + returnUrl: BASE_URL + SOLE_TRADER_WHAT_IS_YOUR_ROLE +}; + +describe("authentication middleware tests", () => { + it("should call CH authentication library", () => { + authenticationMiddlewareForSoleTrader(req, res, next); + expect(mockAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfig); + expect(mockAuthReturnedFunction).toHaveBeenCalledWith(req, res, next); + }); +});