release: WRNexusJS 0.4.0
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { createCaptchaEngine, MemoryCaptchaStore, type CaptchaChallengeGenerator } from "@wrnexus/captcha/server";
|
||||
import {
|
||||
createCaptchaEngine,
|
||||
MemoryCaptchaStore,
|
||||
type CaptchaChallengeGenerator,
|
||||
} from "@wrnexus/captcha/server";
|
||||
import { ManagedCaptchaService } from "../src/service.ts";
|
||||
|
||||
let seed = 11;
|
||||
@@ -30,45 +34,68 @@ describe("managed CAPTCHA service", () => {
|
||||
adminToken: "a-long-admin-token-for-managed-captcha-tests",
|
||||
baseUrl: "https://captcha.test",
|
||||
randomBytes,
|
||||
engineFactory: (project) => createCaptchaEngine({
|
||||
secret: project.engineSecret,
|
||||
store: new MemoryCaptchaStore(),
|
||||
generators: [generator],
|
||||
defaultType: "number",
|
||||
minCompletionMs: 0,
|
||||
randomBytes,
|
||||
}),
|
||||
engineFactory: (project) =>
|
||||
createCaptchaEngine({
|
||||
secret: project.engineSecret,
|
||||
store: new MemoryCaptchaStore(),
|
||||
generators: [generator],
|
||||
defaultType: "number",
|
||||
minCompletionMs: 0,
|
||||
randomBytes,
|
||||
}),
|
||||
});
|
||||
const project = await service.createProject({ name: "Test", allowedHostnames: ["app.test"] });
|
||||
const created = await service.handle(new Request("https://captcha.test/v1/challenges", {
|
||||
method: "POST",
|
||||
headers: { origin: "https://app.test", "content-type": "application/json" },
|
||||
body: JSON.stringify({ siteKey: project.siteKey, action: "signup", type: "number" }),
|
||||
}));
|
||||
const created = await service.handle(
|
||||
new Request("https://captcha.test/v1/challenges", {
|
||||
method: "POST",
|
||||
headers: { origin: "https://app.test", "content-type": "application/json" },
|
||||
body: JSON.stringify({ siteKey: project.siteKey, action: "signup", type: "number" }),
|
||||
}),
|
||||
);
|
||||
expect(created.status).toBe(201);
|
||||
const challenge = await created.json() as { id: string; provider: string };
|
||||
const challenge = (await created.json()) as { id: string; provider: string };
|
||||
expect(challenge.provider).toBe("wrnexus-managed");
|
||||
|
||||
const solved = await service.handle(new Request("https://captcha.test/v1/solve", {
|
||||
method: "POST",
|
||||
headers: { origin: "https://app.test", "content-type": "application/json" },
|
||||
body: JSON.stringify({ challengeId: challenge.id, action: "signup", answer: "7" }),
|
||||
}));
|
||||
const solved = await service.handle(
|
||||
new Request("https://captcha.test/v1/solve", {
|
||||
method: "POST",
|
||||
headers: { origin: "https://app.test", "content-type": "application/json" },
|
||||
body: JSON.stringify({ challengeId: challenge.id, action: "signup", answer: "7" }),
|
||||
}),
|
||||
);
|
||||
expect(solved.status).toBe(200);
|
||||
const solution = await solved.json() as { responseToken: string };
|
||||
const solution = (await solved.json()) as { responseToken: string };
|
||||
|
||||
const verified = await service.handle(new Request("https://captcha.test/v1/verify", {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${project.secretKey}`, "content-type": "application/json" },
|
||||
body: JSON.stringify({ responseToken: solution.responseToken, action: "signup", hostname: "app.test" }),
|
||||
}));
|
||||
const verified = await service.handle(
|
||||
new Request("https://captcha.test/v1/verify", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
authorization: `Bearer ${project.secretKey}`,
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
responseToken: solution.responseToken,
|
||||
action: "signup",
|
||||
hostname: "app.test",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(verified.status).toBe(200);
|
||||
|
||||
const replay = await service.handle(new Request("https://captcha.test/v1/verify", {
|
||||
method: "POST",
|
||||
headers: { authorization: `Bearer ${project.secretKey}`, "content-type": "application/json" },
|
||||
body: JSON.stringify({ responseToken: solution.responseToken, action: "signup", hostname: "app.test" }),
|
||||
}));
|
||||
const replay = await service.handle(
|
||||
new Request("https://captcha.test/v1/verify", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
authorization: `Bearer ${project.secretKey}`,
|
||||
"content-type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
responseToken: solution.responseToken,
|
||||
action: "signup",
|
||||
hostname: "app.test",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(replay.status).toBe(400);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user