30 lines
1020 B
TypeScript
30 lines
1020 B
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { evaluateCaptchaRisk, shouldRequireCaptcha, validCaptchaGrant } from "../src/policy.ts";
|
|
|
|
describe("adaptive CAPTCHA policy", () => {
|
|
test("raises risk from automation signals", () => {
|
|
const result = evaluateCaptchaRisk({
|
|
failedAttempts: 3,
|
|
completionMs: 200,
|
|
suspiciousHeaders: true,
|
|
});
|
|
expect(result.challenge).toBe(true);
|
|
expect(result.reasons).toContain("too-fast");
|
|
});
|
|
|
|
test("honours explicit policy actions", () => {
|
|
expect(shouldRequireCaptcha("signup", { alwaysForActions: ["signup"] }).challenge).toBe(true);
|
|
expect(shouldRequireCaptcha("health", { neverForActions: ["health"] }).challenge).toBe(false);
|
|
});
|
|
|
|
test("accepts an unexpired route grant", () => {
|
|
const grant = validCaptchaGrant(
|
|
[{ action: "page", routeGroup: "/reports", provider: "self-hosted", expiresAt: 200 }],
|
|
"page",
|
|
100,
|
|
"/reports",
|
|
);
|
|
expect(grant?.provider).toBe("self-hosted");
|
|
});
|
|
});
|