Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,71 @@
|
||||
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 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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user