Skip to content

Commit

Permalink
Merge pull request #530 from companieshouse/IDVA5-1230-fixingDefaultA…
Browse files Browse the repository at this point in the history
…ddressSelection

Fixing the issue with redirection
  • Loading branch information
sampankumar authored Nov 11, 2024
2 parents a0b5fde + 6b71688 commit 2837793
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/middleware/authentication_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@ import { AuthOptions, authMiddleware, acspProfileCreateAuthMiddleware } from "@c
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";
import { SessionKey } from "@companieshouse/node-session-handler/lib/session/keys/SessionKey";
import { SignInInfoKeys } from "@companieshouse/node-session-handler/lib/session/keys/SignInInfoKeys";
import { ISignInInfo } 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<ISignInInfo>(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 isActiveFeature(FEATURE_FLAG_VERIFY_SOLE_TRADER_ONLY)
? authMiddleware(authMiddlewareConfig)(req, res, next)
Expand Down
31 changes: 30 additions & 1 deletion test/src/middleware/authentication_middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ 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";
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 = authMiddleware as jest.Mock;
Expand All @@ -26,11 +29,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", async () => {
authenticationMiddleware(req, res, next);
expect(mockAcspProfileCreateAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfig);
expect(mockAuthReturnedFunctionAcspProfileCreateAuthMiddleware).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(mockAcspProfileCreateAuthMiddleware).toHaveBeenCalledWith(expectedAuthMiddlewareConfigWithWhatisRoleURL);
});

});

function getSessionRequestWithExtraData (value: Boolean): Session {
const session = getSessionRequestWithPermission();

session.setExtraData(USER_DATA, {
companyAuthCodeProvided: value
});
session.setExtraData(COMPANY_NUMBER, "NI038379");
return session;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 2837793

Please sign in to comment.