Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/captcha/src/verify.test.ts
T
2026-07-08 16:15:37 +02:00

92 lines
3.5 KiB
TypeScript

import { describe, it, expect } 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 {
ok,
status,
json: async () => body,
} as unknown as Response;
}
describe("verifyCaptchaToken", () => {
it("returns true when mCaptcha reports valid", async () => {
let calledUrl = "";
let calledBody: unknown = null;
const fetchImpl = (async (url: string, init?: RequestInit) => {
calledUrl = url;
calledBody = JSON.parse(String(init?.body));
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" });
});
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 open (true) on network error by default", async () => {
const fetchImpl = (async () => {
throw new Error("ECONNREFUSED");
}) as unknown as typeof fetch;
expect(await verifyCaptchaToken({ ...base, fetchImpl })).toBe(true);
});
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 open on non-OK HTTP status", async () => {
const fetchImpl = (async () => jsonResponse({}, false, 502)) as unknown as typeof fetch;
expect(await verifyCaptchaToken({ ...base, fetchImpl })).toBe(true);
});
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(true);
expect(
await verifyCaptchaToken({ ...base, secret: undefined, failOpen: false, fetchImpl }),
).toBe(false);
});
});