diff --git a/src/components/common/state-machine/tests/state-machine.test.ts b/src/components/common/state-machine/tests/state-machine.test.ts index 4e4cb540c8..fdf30f62d3 100644 --- a/src/components/common/state-machine/tests/state-machine.test.ts +++ b/src/components/common/state-machine/tests/state-machine.test.ts @@ -18,6 +18,53 @@ describe("state-machine", () => { expect(nextState.value).to.equal(PATH_NAMES.PROVE_IDENTITY_WELCOME); }); + + it(`should move from ${PATH_NAMES.AUTHORIZE} to ${PATH_NAMES.PROVE_IDENTITY}`, () => { + const nextState = getNextState( + PATH_NAMES.AUTHORIZE, + USER_JOURNEY_EVENTS.EXISTING_SESSION, + { + isIdentityRequired: true, + proveIdentityWelcomeEnabled: false, + } + ); + + expect(nextState.value).to.equal(PATH_NAMES.PROVE_IDENTITY); + }); + it(`should stay on ${PATH_NAMES.AUTHORIZE}`, () => { + const nextState = getNextState( + PATH_NAMES.AUTHORIZE, + USER_JOURNEY_EVENTS.EXISTING_SESSION, + { isIdentityRequired: false, proveIdentityWelcomeEnabled: false } + ); + expect(nextState.value).to.equal(PATH_NAMES.AUTHORIZE); + }); + it(`should move from ${PATH_NAMES.AUTHORIZE} to ${PATH_NAMES.ENTER_EMAIL_SIGN_IN}`, () => { + const nextState = getNextState( + PATH_NAMES.AUTHORIZE, + USER_JOURNEY_EVENTS.EXISTING_SESSION, + { + isIdentityRequired: true, + proveIdentityWelcomeEnabled: false, + isAuthenticated: true, + isReauthenticationRequired: true, + } + ); + expect(nextState.value).to.equal(PATH_NAMES.ENTER_EMAIL_SIGN_IN); + }); + it(`should move from ${PATH_NAMES.AUTHORIZE} to ${PATH_NAMES.PROVE_IDENTITY}`, () => { + const nextState = getNextState( + PATH_NAMES.AUTHORIZE, + USER_JOURNEY_EVENTS.EXISTING_SESSION, + { + isIdentityRequired: true, + isAuthenticated: true, + isReauthenticationRequired: false, + proveIdentityWelcomeEnabled: false, + } + ); + expect(nextState.value).to.equal(PATH_NAMES.PROVE_IDENTITY); + }); }); }); @@ -89,6 +136,7 @@ describe("state-machine", () => { expect(nextState.value).to.equal(PATH_NAMES.AUTH_CODE); }); }); + describe("getNextState - login journey (non 2fa)", () => { it("should move from initial state to sign or create when user event is landing", () => { const nextState = getNextState(PATH_NAMES.ROOT, USER_JOURNEY_EVENTS.ROOT); diff --git a/src/components/prove-identity-welcome/tests/prove-identity-welcome-controller.test.ts b/src/components/prove-identity-welcome/tests/prove-identity-welcome-controller.test.ts index b40bc72ef8..e4899ea03a 100644 --- a/src/components/prove-identity-welcome/tests/prove-identity-welcome-controller.test.ts +++ b/src/components/prove-identity-welcome/tests/prove-identity-welcome-controller.test.ts @@ -12,76 +12,81 @@ import { } from "../prove-identity-welcome-controller"; import { createMockRequest } from "../../../../test/helpers/mock-request-helper"; import { proveIdentityWelcomeEnabled } from "../../../config"; -if (proveIdentityWelcomeEnabled()) { - describe("prove your identity welcome controller", () => { - let req: RequestOutput; - let res: ResponseOutput; - - const STATE = "ndhd7d7d"; - - beforeEach(() => { - req = createMockRequest(PATH_NAMES.PROVE_IDENTITY_WELCOME); - req.session.client = { - redirectUri: "http://someservice.com/auth", - state: STATE, - }; - res = mockResponse(); - }); - afterEach(() => { - sinon.restore(); - }); +describe("prove your identity welcome controller", () => { + let req: RequestOutput; + let res: ResponseOutput; - describe("proveIdentityWelcomeGet", () => { - it("should render prove your identity welcome page", async () => { - proveIdentityWelcomeGet(req as Request, res as Response); + const STATE = "ndhd7d7d"; + beforeEach(() => { + req = createMockRequest(PATH_NAMES.PROVE_IDENTITY_WELCOME); + req.session.client = { + redirectUri: "http://someservice.com/auth", + state: STATE, + }; + res = mockResponse(); + }); + + afterEach(() => { + sinon.restore(); + }); + + describe("proveIdentityWelcomeGet", () => { + it("should render prove your identity welcome page or redirect to sign-in-or-create", async () => { + proveIdentityWelcomeGet(req as Request, res as Response); + if (proveIdentityWelcomeEnabled()) { expect(res.render).to.have.been.calledWith( "prove-identity-welcome/index.njk" ); - }); - - it("should render prove identity welcome page for user that already has an active session", async () => { - req.session.user.isAuthenticated = true; - proveIdentityWelcomeGet(req as Request, res as Response); + } else { + expect(res.redirect).to.have.been.calledWith( + PATH_NAMES.SIGN_IN_OR_CREATE + ); + } + }); + it("should render prove identity welcome page for user that already has an active session", async () => { + req.session.user.isAuthenticated = true; + proveIdentityWelcomeGet(req as Request, res as Response); + if (proveIdentityWelcomeEnabled()) { expect(res.render).to.have.been.calledWith( "prove-identity-welcome/index-existing-session.njk" ); - }); + } }); + }); - describe("proveIdentityWelcomePost", () => { - it("should redirect to sign in or create when user not authenticated", async () => { - await proveIdentityWelcomePost(req as Request, res as Response); + describe("proveIdentityWelcomePost", () => { + it("should redirect to sign in or create when user not authenticated", async () => { + await proveIdentityWelcomePost(req as Request, res as Response); - expect(res.redirect).to.have.been.calledWith( - PATH_NAMES.SIGN_IN_OR_CREATE - ); - }); + expect(res.redirect).to.have.been.calledWith( + PATH_NAMES.SIGN_IN_OR_CREATE + ); + }); - it("should redirect to prove your identity when user is authenticated", async () => { - req.session.user.isAuthenticated = true; - await proveIdentityWelcomePost(req as Request, res as Response); + it("should redirect to prove your identity when user is authenticated", async () => { + req.session.user.isAuthenticated = true; + await proveIdentityWelcomePost(req as Request, res as Response); - expect(res.redirect).to.have.been.calledWith(PATH_NAMES.PROVE_IDENTITY); - }); + expect(res.redirect).to.have.been.calledWith(PATH_NAMES.PROVE_IDENTITY); + }); - it("should redirect to uplift journey when user is required to step up auth", async () => { - req.session.user.isAuthenticated = true; - req.session.user.isUpliftRequired = true; - await proveIdentityWelcomePost(req as Request, res as Response); + it("should redirect to uplift journey when user is required to step up auth", async () => { + req.session.user.isAuthenticated = true; + req.session.user.isUpliftRequired = true; + await proveIdentityWelcomePost(req as Request, res as Response); - expect(res.redirect).to.have.been.calledWith(PATH_NAMES.UPLIFT_JOURNEY); - }); + expect(res.redirect).to.have.been.calledWith(PATH_NAMES.UPLIFT_JOURNEY); + }); - it("should redirect to enter password when user is required to login (prompt=LOGIN)", async () => { - req.session.user.isAuthenticated = true; - req.session.client.prompt = OIDC_PROMPT.LOGIN; - await proveIdentityWelcomePost(req as Request, res as Response); + it("should redirect to enter password when user is required to login (prompt=LOGIN)", async () => { + req.session.user.isAuthenticated = true; + req.session.client.prompt = OIDC_PROMPT.LOGIN; + await proveIdentityWelcomePost(req as Request, res as Response); - expect(res.redirect).to.have.been.calledWith(PATH_NAMES.ENTER_PASSWORD); - }); + expect(res.redirect).to.have.been.calledWith(PATH_NAMES.ENTER_PASSWORD); }); }); -} +});