Skip to content

Commit

Permalink
AUT-2790: Update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
VladGavrilet committed Jul 11, 2024
1 parent de2733b commit 2d49656
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/components/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const ERROR_CODES = {

export const ERROR_CODE_MAPPING: { [p: string]: string } = {
[ERROR_CODES.ACCOUNT_LOCKED]: pathWithQueryParam(
PATH_NAMES["ACCOUNT_LOCKED"]
PATH_NAMES.SIGNED_OUT.concat("?error=login_required")
),
[ERROR_CODES.INVALID_PASSWORD_MAX_ATTEMPTS_REACHED]: pathWithQueryParam(
PATH_NAMES["ACCOUNT_LOCKED"]
Expand Down
15 changes: 9 additions & 6 deletions src/components/enter-email/enter-email-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ export function enterEmailPost(
const email = req.body.email;
const { sessionId, clientSessionId, persistentSessionId } = res.locals;
req.session.user.email = email.toLowerCase();
const sub = req.session.user.reauthenticate;
const reauthenticateJourney = req.session.user.reauthenticate;

if (supportReauthentication() && sub) {
if (supportReauthentication() && reauthenticateJourney) {
const checkReauth = await checkReauthService.checkReauthUsers(
sessionId,
email,
sub,
reauthenticateJourney,
clientSessionId,
persistentSessionId,
req
Expand All @@ -96,6 +96,11 @@ export function enterEmailPost(
checkReauth.data.code ===
ERROR_CODES.RE_AUTH_SIGN_IN_DETAILS_ENTERED_EXCEEDED
) {
if (reauthenticateJourney) {
return res.redirect(
PATH_NAMES["SIGNED_OUT"].concat("?error=login_required")
);
}
return handleSessionBlocked(req, res);
}

Expand Down Expand Up @@ -237,9 +242,7 @@ function handleSessionBlocked(req: Request, res: Response) {
req.session.user.wrongEmailEnteredLock = timestampNMinutesFromNow(
getEmailEnteredWrongBlockDurationInMinutes()
);
return supportReauthentication()
? res.redirect(PATH_NAMES["SIGNED_OUT"].concat("?error=login_required"))
: res.render(BLOCKED_TEMPLATE);
return res.render(BLOCKED_TEMPLATE);
}

function handleBadRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,31 +184,6 @@ describe("Integration::enter email (create account)", () => {
.expect(302, done);
});

it("should redirect to /signed-out with login_required error when user fails re-auth", (done) => {
process.env.SUPPORT_REAUTHENTICATION = "1";

nock(baseApi)
.post(API_ENDPOINTS.CHECK_REAUTH_USER)
.once()
.reply(HTTP_STATUS_CODES.BAD_REQUEST, {
code: ERROR_CODES.RE_AUTH_SIGN_IN_DETAILS_ENTERED_EXCEEDED,
});

request(app)
.post(PATH_NAMES.ENTER_EMAIL_SIGN_IN)
.type("form")
.set("Cookie", cookies)
.send({
_csrf: token,
email: "[email protected]",
})
.expect(
"Location",
PATH_NAMES["SIGNED_OUT"].concat("?error=login_required")
)
.expect(302, done);
});

it("should return internal server error when /user-exists API call response is 500", (done) => {
nock(baseApi).post(API_ENDPOINTS.USER_EXISTS).once().reply(500, {
message: "Internal Server error",
Expand Down
54 changes: 33 additions & 21 deletions src/components/enter-email/tests/enter-email-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import {
HTTP_STATUS_CODES,
PATH_NAMES,
} from "../../../app.constants";
import { CheckReauthServiceInterface } from "../../check-reauth-users/types";
import { AxiosResponse } from "axios";
import { createApiResponse } from "../../../utils/http";
import { DefaultApiResponse } from "../../../types";
import nock = require("nock");
import { ERROR_CODES } from "../../common/constants";

describe("Integration::enter email", () => {
let token: string | string[];
Expand All @@ -24,7 +21,6 @@ describe("Integration::enter email", () => {
decache("../../../app");
decache("../../../middleware/session-middleware");
const sessionMiddleware = require("../../../middleware/session-middleware");
const checkReauthUsersService = require("../../check-reauth-users/check-reauth-users-service");

sinon
.stub(sessionMiddleware, "validateSessionMiddleware")
Expand All @@ -42,26 +38,12 @@ describe("Integration::enter email", () => {
next();
});

sinon
.stub(checkReauthUsersService, "checkReauthUsersService")
.callsFake((): CheckReauthServiceInterface => {
async function checkReauthUsers() {
const fakeAxiosResponse: AxiosResponse = {
status: HTTP_STATUS_CODES.OK,
} as AxiosResponse;

return createApiResponse<DefaultApiResponse>(fakeAxiosResponse);
}

return { checkReauthUsers };
});

app = await require("../../../app").createApp();
baseApi = process.env.FRONTEND_API_BASE_URL;

request(app)
await request(app)
.get(PATH_NAMES.ENTER_EMAIL_SIGN_IN)
.end((err, res) => {
.then((res) => {
const $ = cheerio.load(res.text);
token = $("[name=_csrf]").val();
cookies = res.headers["set-cookie"];
Expand Down Expand Up @@ -247,4 +229,34 @@ describe("Integration::enter email", () => {
.expect("Location", PATH_NAMES.ENTER_PASSWORD)
.expect(302, done);
});

it("should redirect to /signed-out with login_required error when user fails re-auth", async () => {
process.env.SUPPORT_REAUTHENTICATION = "1";

nock(baseApi)
.post(API_ENDPOINTS.CHECK_REAUTH_USER)
.once()
.reply(HTTP_STATUS_CODES.BAD_REQUEST, {
code: ERROR_CODES.RE_AUTH_SIGN_IN_DETAILS_ENTERED_EXCEEDED,
});

nock(baseApi)
.post(API_ENDPOINTS.USER_EXISTS)
.once()
.reply(HTTP_STATUS_CODES.OK, {
email: "[email protected]",
doesUserExist: true,
});

await request(app)
.post(PATH_NAMES.ENTER_EMAIL_SIGN_IN)
.type("form")
.set("Cookie", cookies)
.send({
_csrf: token,
email: "[email protected]",
})
.expect("Location", PATH_NAMES.SIGNED_OUT.concat("?error=login_required"))
.expect(302);
});
});

0 comments on commit 2d49656

Please sign in to comment.