|
| 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