From c68ce2ecd6e50e442cea567f5204144493431928 Mon Sep 17 00:00:00 2001 From: sampankumar Date: Fri, 8 Nov 2024 15:21:28 +0000 Subject: [PATCH 1/3] Fixing the issue with redirection --- src/middleware/authentication_middleware.ts | 23 ++++++++++--- .../authentication_middleware.test.ts | 32 ++++++++++++++++++- .../company_authentication_middleware.test.ts | 3 +- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/middleware/authentication_middleware.ts b/src/middleware/authentication_middleware.ts index 3b76e84d..7b0bfe37 100644 --- a/src/middleware/authentication_middleware.ts +++ b/src/middleware/authentication_middleware.ts @@ -3,13 +3,28 @@ import { AuthOptions, acspProfileCreateAuthMiddleware } from "@companieshouse/we import { CHS_URL } from "../utils/properties"; import { BASE_URL, CHECK_SAVED_APPLICATION } from "../types/pageURL"; +import { SessionKey } from "@companieshouse/node-session-handler/lib/session/keys/SessionKey"; +import { SignInInfoKeys } from "@companieshouse/node-session-handler/lib/session/keys/SignInInfoKeys"; +import { ISignInInfo, IUserProfile } from "@companieshouse/node-session-handler/lib/session/model/SessionInterfaces"; export const authenticationMiddleware = (req: Request, res: Response, next: NextFunction) => { - const authMiddlewareConfig: AuthOptions = { - chsWebUrl: CHS_URL, - returnUrl: BASE_URL + CHECK_SAVED_APPLICATION - }; + const signInInfo: ISignInInfo = req.session?.get(SessionKey.SignInInfo) || {}; + const signedIn: boolean = signInInfo![SignInInfoKeys.SignedIn] === 1; + + let authMiddlewareConfig: AuthOptions; + + if (!signedIn) { + authMiddlewareConfig = { + chsWebUrl: CHS_URL, + returnUrl: BASE_URL + CHECK_SAVED_APPLICATION + }; + } else { + authMiddlewareConfig = { + chsWebUrl: CHS_URL, + returnUrl: req.originalUrl + }; + } return acspProfileCreateAuthMiddleware(authMiddlewareConfig)(req, res, next); }; diff --git a/test/src/middleware/authentication_middleware.test.ts b/test/src/middleware/authentication_middleware.test.ts index 6c29986b..04f2c106 100644 --- a/test/src/middleware/authentication_middleware.test.ts +++ b/test/src/middleware/authentication_middleware.test.ts @@ -5,7 +5,10 @@ jest.mock("@companieshouse/web-security-node"); import { acspProfileCreateAuthMiddleware, 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"; +import { BASE_URL, CHECK_SAVED_APPLICATION, LIMITED_WHAT_IS_YOUR_ROLE } from "../../../src/types/pageURL"; +import { getSessionRequestWithPermission } from "../../mocks/session.mock"; +import { USER_DATA, COMPANY_NUMBER } from "../../../src/common/__utils/constants"; +import { Session } from "@companieshouse/node-session-handler"; // get handle on mocked function and create mock function to be returned from calling authMiddleware const mockAuthMiddleware = acspProfileCreateAuthMiddleware as jest.Mock; @@ -23,10 +26,37 @@ const expectedAuthMiddlewareConfig: AuthOptions = { returnUrl: BASE_URL + CHECK_SAVED_APPLICATION }; +const expectedAuthMiddlewareConfigWithWhatisRoleURL: AuthOptions = { + chsWebUrl: "http://chs.local", + returnUrl: BASE_URL + LIMITED_WHAT_IS_YOUR_ROLE +}; + describe("authentication middleware tests", () => { it("should call CH authentication library", () => { authenticationMiddleware(req, res, next); expect(mockAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfig); expect(mockAuthReturnedFunction).toHaveBeenCalledWith(req, res, next); }); + + it("should call CH authentication library with Limited URL when session is available ", () => { + let request = {} as Request; + const Url = BASE_URL + LIMITED_WHAT_IS_YOUR_ROLE; + request = { + session: getSessionRequestWithExtraData(true), + originalUrl: Url + } as unknown as Request; + authenticationMiddleware(request, res, next); + expect(mockAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfigWithWhatisRoleURL); + }); + }); + +function getSessionRequestWithExtraData (value: Boolean): Session { + const session = getSessionRequestWithPermission(); + + session.setExtraData(USER_DATA, { + companyAuthCodeProvided: value + }); + session.setExtraData(COMPANY_NUMBER, "NI038379"); + return session; +} diff --git a/test/src/middleware/company_authentication_middleware.test.ts b/test/src/middleware/company_authentication_middleware.test.ts index e6f9f759..d457881f 100644 --- a/test/src/middleware/company_authentication_middleware.test.ts +++ b/test/src/middleware/company_authentication_middleware.test.ts @@ -2,7 +2,8 @@ import { Request, Response, NextFunction } from "express"; import { companyAuthenticationMiddleware } from "../../../src/middleware/company_authentication_middleware"; import { getSessionRequestWithPermission } from "../../mocks/session.mock"; import { USER_DATA, COMPANY_NUMBER } from "../../../src/common/__utils/constants"; -import { Session } from "@companieshouse/node-session-handler"; import { BASE_URL, LIMITED_WHAT_IS_YOUR_ROLE } from "../../../src/types/pageURL"; +import { Session } from "@companieshouse/node-session-handler"; +import { BASE_URL, LIMITED_WHAT_IS_YOUR_ROLE } from "../../../src/types/pageURL"; jest.mock("ioredis"); jest.mock("@companieshouse/web-security-node"); From c7596aec63c6c95fc0d95ef911541d38c4770b52 Mon Sep 17 00:00:00 2001 From: sampankumar Date: Fri, 8 Nov 2024 15:38:29 +0000 Subject: [PATCH 2/3] Fixing test --- test/src/middleware/authentication_middleware.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/src/middleware/authentication_middleware.test.ts b/test/src/middleware/authentication_middleware.test.ts index 792673cb..11c037f8 100644 --- a/test/src/middleware/authentication_middleware.test.ts +++ b/test/src/middleware/authentication_middleware.test.ts @@ -49,10 +49,9 @@ describe("authentication middleware tests", () => { originalUrl: Url } as unknown as Request; authenticationMiddleware(request, res, next); - expect(mockAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfigWithWhatisRoleURL); + expect(mockAcspProfileCreateAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfigWithWhatisRoleURL); }); - }); function getSessionRequestWithExtraData (value: Boolean): Session { From 6b7168809beed0e551f55bcf97d156053d61cb55 Mon Sep 17 00:00:00 2001 From: sampankumar Date: Fri, 8 Nov 2024 16:32:42 +0000 Subject: [PATCH 3/3] Removing code smells --- src/middleware/authentication_middleware.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/middleware/authentication_middleware.ts b/src/middleware/authentication_middleware.ts index bb909db4..59f48fb6 100644 --- a/src/middleware/authentication_middleware.ts +++ b/src/middleware/authentication_middleware.ts @@ -5,12 +5,12 @@ import { CHS_URL, FEATURE_FLAG_VERIFY_SOLE_TRADER_ONLY } from "../utils/properti import { BASE_URL, CHECK_SAVED_APPLICATION } from "../types/pageURL"; import { SessionKey } from "@companieshouse/node-session-handler/lib/session/keys/SessionKey"; import { SignInInfoKeys } from "@companieshouse/node-session-handler/lib/session/keys/SignInInfoKeys"; -import { ISignInInfo, IUserProfile } from "@companieshouse/node-session-handler/lib/session/model/SessionInterfaces"; +import { ISignInInfo } from "@companieshouse/node-session-handler/lib/session/model/SessionInterfaces"; export const authenticationMiddleware = (req: Request, res: Response, next: NextFunction) => { const signInInfo: ISignInInfo = req.session?.get(SessionKey.SignInInfo) || {}; - const signedIn: boolean = signInInfo![SignInInfoKeys.SignedIn] === 1; + const signedIn: boolean = signInInfo[SignInInfoKeys.SignedIn] === 1; let authMiddlewareConfig: AuthOptions;