Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { verifyCaptchaToken } from "./verify.js";
|
||||
|
||||
const base = {
|
||||
@@ -9,20 +9,21 @@ const base = {
|
||||
};
|
||||
|
||||
function jsonResponse(body: unknown, ok = true, status = 200): Response {
|
||||
return {
|
||||
ok,
|
||||
status,
|
||||
json: async () => body,
|
||||
} as unknown as 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;
|
||||
|
||||
@@ -30,6 +31,7 @@ describe("verifyCaptchaToken", () => {
|
||||
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 () => {
|
||||
@@ -62,11 +64,11 @@ describe("verifyCaptchaToken", () => {
|
||||
expect(await verifyCaptchaToken({ ...base, token: "", fetchImpl })).toBe(false);
|
||||
});
|
||||
|
||||
it("fails open (true) on network error by default", async () => {
|
||||
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(true);
|
||||
expect(await verifyCaptchaToken({ ...base, fetchImpl })).toBe(false);
|
||||
});
|
||||
|
||||
it("fails closed (false) on network error when failOpen=false", async () => {
|
||||
@@ -76,16 +78,40 @@ describe("verifyCaptchaToken", () => {
|
||||
expect(await verifyCaptchaToken({ ...base, failOpen: false, fetchImpl })).toBe(false);
|
||||
});
|
||||
|
||||
it("fails open on non-OK HTTP status", async () => {
|
||||
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(true);
|
||||
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(true);
|
||||
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();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user