返回 CodeWhale
public-api-security.test.ts
根目录 / web / lib / public-api-security.test.ts
1 import { readFileSync } from "node:fs";
2 import { describe, expect, it } from "vitest";
3
4 function routeSource(path: string): string {
5 return readFileSync(new URL(`../app/api/${path}/route.ts`, import.meta.url), "utf8");
6 }
7
8 describe("public API security contracts", () => {
9 it("validates the draft namespace before admin discard can reach KV", () => {
10 const source = routeSource("admin/post");
11 expect(source).toContain("parseDraftKey(draftKey)");
12 expect(source.indexOf("parseDraftKey(draftKey)")).toBeLessThan(
13 source.indexOf("getDraft(env.CURATED_KV, draftKey)"),
14 );
15 expect(source.indexOf("getDraft(env.CURATED_KV, draftKey)")).toBeLessThan(
16 source.indexOf("deleteDraft(env.CURATED_KV, draftKey)"),
17 );
18 });
19
20 it("bounds the public login body before comparing the maintainer token", () => {
21 const source = routeSource("admin/login");
22 expect(source).toContain("readBoundedUrlEncodedForm(req, MAX_LOGIN_BODY_BYTES)");
23 expect(source).not.toContain("req.formData()");
24 expect(source.indexOf("readBoundedUrlEncodedForm(req, MAX_LOGIN_BODY_BYTES)")).toBeLessThan(
25 source.indexOf("safeEqual(submitted, env.MAINTAINER_TOKEN)"),
26 );
27 });
28
29 it("digest post: never returns ok:true for a no-op (happy path returns real url/number)", () => {
30 const source = routeSource("admin/post");
31 // The old false-success sentinel must be gone
32 expect(source).not.toContain("digest-skipped");
33 expect(source).not.toContain("Digest pages are not posted as comments");
34 // Happy path must surface the real GitHub Issue outcome
35 expect(source).toContain("number: issue.number");
36 expect(source).toContain("url: issue.html_url");
37 // The draft must be marked posted and stored back on success
38 const digestBlock = source.slice(source.indexOf('draft.type === "digest"'));
39 expect(digestBlock.indexOf("draft.posted = true")).toBeLessThan(
40 digestBlock.indexOf('action: "posted"'),
41 );
42 });
43
44 // "digest post: GitHub API failure surfaces a 502" is covered behaviorally
45 // in community-agent-review-state.test.ts.
46 });
47
47 lines TYPESCRIPT