51 lines
2.1 KiB
TypeScript
51 lines
2.1 KiB
TypeScript
// @vitest-environment node
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("~/utils/db/users.db.server", () => ({ getUserTotpStatus: vi.fn() }));
|
|
vi.mock("~/utils/session.server", () => ({ commitPendingMfaLoginCookie: vi.fn() }));
|
|
|
|
import { getUserTotpStatus } from "~/utils/db/users.db.server";
|
|
import { commitPendingMfaLoginCookie } from "~/utils/session.server";
|
|
import { beginAlternativeLoginMfa } from "./alternative-login-mfa.server";
|
|
|
|
const request = new Request("https://app.example.test/auth/callback");
|
|
|
|
describe("alternative login MFA transition", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(commitPendingMfaLoginCookie).mockResolvedValue("hivegrid_session=pending; HttpOnly");
|
|
});
|
|
|
|
it("creates pending state whenever TOTP is enabled", async () => {
|
|
vi.mocked(getUserTotpStatus).mockResolvedValue({ enabled: true } as never);
|
|
|
|
await expect(
|
|
beginAlternativeLoginMfa({ request, userId: "user_1", redirectTo: "/acme/dashboard", primaryMethod: "magic-link" }),
|
|
).resolves.toEqual({ required: true, setCookie: "hivegrid_session=pending; HttpOnly" });
|
|
|
|
expect(commitPendingMfaLoginCookie).toHaveBeenCalledWith(request, {
|
|
userId: "user_1",
|
|
redirectTo: "/acme/dashboard",
|
|
primaryMethod: "magic-link",
|
|
});
|
|
});
|
|
|
|
it("allows the primary flow to finish only when TOTP is explicitly disabled", async () => {
|
|
vi.mocked(getUserTotpStatus).mockResolvedValue({ enabled: false } as never);
|
|
|
|
await expect(
|
|
beginAlternativeLoginMfa({ request, userId: "user_1", redirectTo: "/", primaryMethod: "admin-sso" }),
|
|
).resolves.toEqual({ required: false });
|
|
expect(commitPendingMfaLoginCookie).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("fails closed when MFA status cannot be resolved", async () => {
|
|
vi.mocked(getUserTotpStatus).mockResolvedValue(null);
|
|
|
|
await expect(
|
|
beginAlternativeLoginMfa({ request, userId: "user_1", redirectTo: "/", primaryMethod: "invitation" }),
|
|
).rejects.toThrow("MFA status is unavailable");
|
|
expect(commitPendingMfaLoginCookie).not.toHaveBeenCalled();
|
|
});
|
|
});
|