import { describe, it, expect, vi } from "vitest"; import { verifyCaptchaToken } from "./verify.js"; const base = { token: "pow-token", sitekey: "site-key", secret: "account-secret", instanceUrl: "https://captcha.example.test", }; function jsonResponse(body: unknown, ok = true, status = 200): Response { return new Response(JSON.stringify(body), { status: ok ? status : status || 500, headers: { "content-type": "application/json" }, }); } describe("verifyCaptchaToken", () => { it("returns true when mCaptcha reports valid", async () => { let calledUrl = ""; let calledBody: unknown = null; let calledInit: RequestInit | undefined; const fetchImpl = (async (url: string, init?: RequestInit) => { calledUrl = url; calledBody = JSON.parse(String(init?.body)); calledInit = init; return jsonResponse({ valid: true }); }) as unknown as typeof fetch; const ok = await verifyCaptchaToken({ ...base, fetchImpl }); expect(ok).toBe(true); expect(calledUrl).toBe("https://captcha.example.test/api/v1/pow/siteverify"); expect(calledBody).toEqual({ token: "pow-token", key: "site-key", secret: "account-secret" }); expect(calledInit?.redirect).toBe("manual"); }); it("returns true when Cap reports success", async () => { let calledUrl = ""; let calledBody: unknown = null; const fetchImpl = (async (url: string, init?: RequestInit) => { calledUrl = url; calledBody = JSON.parse(String(init?.body)); return jsonResponse({ success: true }); }) as unknown as typeof fetch; const ok = await verifyCaptchaToken({ ...base, provider: "cap", fetchImpl }); expect(ok).toBe(true); expect(calledUrl).toBe("https://captcha.example.test/site-key/siteverify"); expect(calledBody).toEqual({ response: "pow-token", secret: "account-secret" }); }); it("returns false when Cap reports failure", async () => { const fetchImpl = (async () => jsonResponse({ success: false })) as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, provider: "cap", fetchImpl })).toBe(false); }); it("returns false when mCaptcha reports invalid", async () => { const fetchImpl = (async () => jsonResponse({ valid: false })) as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, fetchImpl })).toBe(false); }); it("returns false on missing token even when configured (hard failure)", async () => { const fetchImpl = (async () => jsonResponse({ valid: true })) as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, token: "", fetchImpl })).toBe(false); }); it("fails closed (false) on network error by default", async () => { const fetchImpl = (async () => { throw new Error("ECONNREFUSED"); }) as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, fetchImpl })).toBe(false); }); it("fails closed (false) on network error when failOpen=false", async () => { const fetchImpl = (async () => { throw new Error("ECONNREFUSED"); }) as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, failOpen: false, fetchImpl })).toBe(false); }); it("fails closed on non-OK HTTP status by default", async () => { const fetchImpl = (async () => jsonResponse({}, false, 502)) as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, fetchImpl })).toBe(false); }); it("applies fail policy when misconfigured (no secret)", async () => { const fetchImpl = (async () => jsonResponse({ valid: true })) as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, secret: undefined, fetchImpl })).toBe(false); expect(await verifyCaptchaToken({ ...base, secret: undefined, failOpen: true, fetchImpl })).toBe(true); expect( await verifyCaptchaToken({ ...base, secret: undefined, failOpen: false, fetchImpl }), ).toBe(false); }); it("rejects non-HTTPS non-loopback instance URLs before fetch", async () => { const fetchImpl = vi.fn() as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, instanceUrl: "http://captcha.example.test", fetchImpl })).toBe(false); expect(fetchImpl).not.toHaveBeenCalled(); }); it("allows HTTP only for a loopback development instance", async () => { const fetchImpl = vi.fn(async () => jsonResponse({ valid: true })) as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, instanceUrl: "http://127.0.0.1:7000", fetchImpl })).toBe(true); expect(fetchImpl).toHaveBeenCalledWith("http://127.0.0.1:7000/api/v1/pow/siteverify", expect.objectContaining({ redirect: "manual" })); }); it("fails closed when the provider response exceeds the hard limit", async () => { const fetchImpl = (async () => jsonResponse({ valid: true, padding: "x".repeat(70 * 1024) })) as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, fetchImpl })).toBe(false); }); it("rejects oversized challenge tokens before fetch", async () => { const fetchImpl = vi.fn() as unknown as typeof fetch; expect(await verifyCaptchaToken({ ...base, token: "x".repeat(9 * 1024), fetchImpl })).toBe(false); expect(fetchImpl).not.toHaveBeenCalled(); }); });