Skip to content

Commit 41ec30d

Browse files
committed
BAU: refactor send mfa code error handling to helper function
1 parent 08245d3 commit 41ec30d

4 files changed

Lines changed: 111 additions & 47 deletions

File tree

src/components/enter-password/enter-password-controller.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { supportAccountInterventions } from "../../config.js";
2828
import { getJourneyTypeFromUserSession } from "../common/journey/journey.js";
2929
import { accountInterventionService } from "../account-intervention/account-intervention-service.js";
3030
import type { AccountInterventionsInterface } from "../account-intervention/types.js";
31+
import { handleSendMfaCodeError } from "../../utils/send-mfa-code-error-helper.js";
3132

3233
const ENTER_PASSWORD_TEMPLATE = "enter-password/index.njk";
3334
const ENTER_PASSWORD_VALIDATION_KEY =
@@ -210,27 +211,7 @@ export function enterPasswordPost(
210211
);
211212

212213
if (!result.success) {
213-
if (result.data.code === ERROR_CODES.MFA_CODE_REQUESTS_BLOCKED) {
214-
return res.render("security-code-error/index-wait.njk");
215-
}
216-
217-
if (result.data.code === ERROR_CODES.ENTERED_INVALID_MFA_MAX_TIMES) {
218-
return res.render(
219-
"security-code-error/index-security-code-entered-exceeded.njk",
220-
{
221-
show2HrScreen: true,
222-
contentId: "727a0395-cc00-48eb-a411-bfe9d8ac5fc8",
223-
}
224-
);
225-
}
226-
227-
const path = getErrorPathByCode(result.data.code);
228-
229-
if (path) {
230-
return res.redirect(path);
231-
}
232-
233-
throw new BadRequestError(result.data.message, result.data.code);
214+
return handleSendMfaCodeError(result, res);
234215
}
235216
}
236217
return res.redirect(

src/components/how-do-you-want-security-codes/how-do-you-want-security-codes-controller.ts

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import type { Request, Response } from "express";
22
import { MFA_METHOD_TYPE, PATH_NAMES } from "../../app.constants.js";
33
import type { ExpressRouteFunc, MfaMethod } from "../../types.js";
4-
import {
5-
ERROR_CODES,
6-
getErrorPathByCode,
7-
getNextPathAndUpdateJourney,
8-
} from "../common/constants.js";
4+
import { getNextPathAndUpdateJourney } from "../common/constants.js";
95
import { USER_JOURNEY_EVENTS } from "../common/state-machine/state-machine.js";
106
import type { MfaServiceInterface } from "../common/mfa/types.js";
117
import { mfaService } from "../common/mfa/mfa-service.js";
128
import xss from "xss";
139
import { getJourneyTypeFromUserSession } from "../common/journey/journey.js";
1410
import { BadRequestError } from "../../utils/error.js";
11+
import { handleSendMfaCodeError } from "../../utils/send-mfa-code-error-helper.js";
1512

1613
export function sortMfaMethodsBackupFirst(
1714
mfaMethods: MfaMethod[]
@@ -65,27 +62,7 @@ export function howDoYouWantSecurityCodesPost(
6562
);
6663

6764
if (!result.success) {
68-
if (result.data.code === ERROR_CODES.MFA_CODE_REQUESTS_BLOCKED) {
69-
return res.render("security-code-error/index-wait.njk");
70-
}
71-
72-
if (result.data.code === ERROR_CODES.ENTERED_INVALID_MFA_MAX_TIMES) {
73-
return res.render(
74-
"security-code-error/index-security-code-entered-exceeded.njk",
75-
{
76-
show2HrScreen: true,
77-
contentId: "727a0395-cc00-48eb-a411-bfe9d8ac5fc8",
78-
}
79-
);
80-
}
81-
82-
const path = getErrorPathByCode(result.data.code);
83-
84-
if (path) {
85-
return res.redirect(path);
86-
}
87-
88-
throw new BadRequestError(result.data.message, result.data.code);
65+
return handleSendMfaCodeError(result, res);
8966
}
9067
}
9168

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { ApiResponseResult, DefaultApiResponse } from "../types.js";
2+
import type { Response } from "express";
3+
import {
4+
ERROR_CODES,
5+
getErrorPathByCode,
6+
} from "../components/common/constants.js";
7+
import { BadRequestError } from "./error.js";
8+
9+
export const handleSendMfaCodeError = (
10+
result: ApiResponseResult<DefaultApiResponse>,
11+
res: Response
12+
) => {
13+
if (result.data.code === ERROR_CODES.MFA_CODE_REQUESTS_BLOCKED) {
14+
return res.render("security-code-error/index-wait.njk");
15+
}
16+
17+
if (result.data.code === ERROR_CODES.ENTERED_INVALID_MFA_MAX_TIMES) {
18+
return res.render(
19+
"security-code-error/index-security-code-entered-exceeded.njk",
20+
{
21+
show2HrScreen: true,
22+
contentId: "727a0395-cc00-48eb-a411-bfe9d8ac5fc8",
23+
}
24+
);
25+
}
26+
27+
const path = getErrorPathByCode(result.data.code);
28+
29+
if (path) {
30+
return res.redirect(path);
31+
}
32+
33+
throw new BadRequestError(result.data.message, result.data.code);
34+
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe } from "mocha";
2+
import { mockResponse } from "mock-req-res";
3+
import type { Response } from "express";
4+
import type {
5+
ApiResponseResult,
6+
DefaultApiResponse,
7+
} from "../../../src/types.js";
8+
import { ERROR_CODES } from "../../../src/components/common/constants.js";
9+
import { handleSendMfaCodeError } from "../../../src/utils/send-mfa-code-error-helper.js";
10+
import { expect } from "../../utils/test-utils.js";
11+
import { createApiResponse } from "../../../src/utils/http.js";
12+
import { BadRequestError } from "../../../src/utils/error.js";
13+
14+
describe("send mfa code error helper", () => {
15+
const res: Response = mockResponse();
16+
let result: ApiResponseResult<DefaultApiResponse>;
17+
18+
it("should return render security-code-error/index-wait", () => {
19+
result = createApiResponse({
20+
config: undefined,
21+
headers: undefined,
22+
status: 0,
23+
statusText: "",
24+
data: { code: ERROR_CODES.MFA_CODE_REQUESTS_BLOCKED },
25+
});
26+
handleSendMfaCodeError(result, res);
27+
expect(res.render).to.have.calledWith("security-code-error/index-wait.njk");
28+
});
29+
30+
it("should return render security-code-error/index-security-code-entered-exceeded", () => {
31+
result = createApiResponse({
32+
config: undefined,
33+
headers: undefined,
34+
status: 0,
35+
statusText: "",
36+
data: { code: ERROR_CODES.ENTERED_INVALID_MFA_MAX_TIMES },
37+
});
38+
handleSendMfaCodeError(result, res);
39+
expect(res.render).to.have.calledWith(
40+
"security-code-error/index-security-code-entered-exceeded.njk"
41+
);
42+
});
43+
44+
it("should redirect to error path associated with a code", () => {
45+
result = createApiResponse({
46+
config: undefined,
47+
headers: undefined,
48+
status: 0,
49+
statusText: "",
50+
data: {
51+
code: ERROR_CODES.VERIFY_CHANGE_HOW_GET_SECURITY_CODES_MAX_CODES_SENT,
52+
},
53+
});
54+
handleSendMfaCodeError(result, res);
55+
expect(res.redirect).to.have.calledWith(
56+
"/security-code-requested-too-many-times?actionType=changeSecurityCodesEmailMaxCodesSent"
57+
);
58+
});
59+
60+
it("should throw bad request error", () => {
61+
result = createApiResponse({
62+
config: undefined,
63+
headers: undefined,
64+
status: 0,
65+
statusText: "",
66+
data: { code: 123456789 },
67+
});
68+
expect(() => {
69+
handleSendMfaCodeError(result, res);
70+
}).to.throw(BadRequestError);
71+
});
72+
});

0 commit comments

Comments
 (0)