// @vitest-environment node import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router"; import { describe, expect, it } from "vitest"; import { action, loader } from "./mfa-challenge"; async function captureRedirect(operation: Promise): Promise { try { await operation; throw new Error("Expected a redirect response."); } catch (error) { if (!(error instanceof Response)) throw error; return error; } } describe("legacy admin MFA compatibility route", () => { it("sends the loader to Authentik and preserves a safe return path", async () => { const response = await captureRedirect( loader({ request: new Request("https://admin.example.test/mfa-challenge?redirectTo=%2Fsettings%2Fsecurity"), params: {}, context: {}, unstable_pattern: "admin/mfa-challenge", } as LoaderFunctionArgs) ); expect(response.status).toBe(302); expect(response.headers.get("Location")).toBe("/auth/admin/login?returnTo=%2Fsettings%2Fsecurity"); }); it("rejects external return targets and never verifies HiveGrid TOTP", async () => { const response = await captureRedirect( loader({ request: new Request("https://admin.example.test/mfa-challenge?redirectTo=https%3A%2F%2Fevil.example"), params: {}, context: {}, unstable_pattern: "admin/mfa-challenge", } as LoaderFunctionArgs) ); expect(response.status).toBe(302); expect(response.headers.get("Location")).toBe("/auth/admin/login?returnTo=%2Fdashboard"); }); it("redirects legacy form posts to Authentik without mutating session state", async () => { const response = await captureRedirect( action({ request: new Request("https://admin.example.test/mfa-challenge", { method: "POST", body: new URLSearchParams({ redirectTo: "/settings/admin-users" }), }), params: {}, context: {}, unstable_pattern: "admin/mfa-challenge", } as ActionFunctionArgs) ); expect(response.status).toBe(302); expect(response.headers.get("Location")).toBe("/auth/admin/login?returnTo=%2Fsettings%2Fadmin-users"); }); });