Files
streamline/control-server/test/openapi_cli_sso.integration.test.ts
2026-11-16 17:26:44 -08:00

366 lines
24 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import "./setup_env";
import { expect, test } from "bun:test";
import { createHash, randomBytes } from "node:crypto";
/**
* 开放平台命令行登录(openapi_cli_sso.ts)+ 会话在自助接口上的权限 + 查任务接口。
* 只连显式给出的本地库:TEST_DATABASE_URL 必须指向 127.0.0.1 / localhost。
*/
const url = process.env.TEST_DATABASE_URL;
const local = !!url && ["127.0.0.1", "localhost"].includes(new URL(url).hostname);
const { callbackUrl, authorizeInput } = await import("../src/openapi_cli_sso");
const { CHANGE_RUN_OPEN } = await import("../src/routes_cli");
function pkce(port = 49152) {
const verifier = randomBytes(32).toString("base64url");
return {
verifier,
body: {
state: randomBytes(32).toString("base64url"), code_challenge: createHash("sha256").update(verifier).digest("base64url"),
code_challenge_method: "S256", redirect_uri: `http://127.0.0.1:${port}/streamline/sso/callback`,
},
};
}
test("回调只认本机 IPv4 临时端口上的命令行路径;PKCE 和 state 必填", () => {
for (const uri of [
"https://evil.example/streamline/sso/callback", "http://localhost:49152/streamline/sso/callback",
"http://127.0.0.1:80/streamline/sso/callback", "http://127.0.0.1:49152/eddy/sso/callback",
"http://127.0.0.1:49152/streamline/sso/callback?next=evil", "http://user@127.0.0.1:49152/streamline/sso/callback",
]) expect(() => callbackUrl(uri)).toThrow();
expect(callbackUrl("http://127.0.0.1:49152/streamline/sso/callback")).toBe("http://127.0.0.1:49152/streamline/sso/callback");
expect(() => authorizeInput({ ...pkce().body, code_challenge_method: "plain" })).toThrow();
});
test.skipIf(!local)("命令行会话:一次性授权码、只认 /cli/v1、退出即失效;Key 能查自己建的单、查不到别人的", async () => {
const { api } = await import("../src/routes");
const { createOpenPlatformKey, revokeOpenPlatformKey } = await import("../src/openplatform_keys");
const realFetch = globalThis.fetch;
globalThis.fetch = (async (input: any, init?: RequestInit) => {
const u = String(input);
if (u.includes("/invoke/getUserInfo")) {
const s = new Headers(init?.headers).get("x-tt-session-v2");
const email = s === "fixture-employee" ? "cli-employee@bytedance.com" : s === "fixture-admin" ? "jiaopengwen@bytedance.com" : null;
return Response.json({ data: email ? { email } : null });
}
// 登记机器人时向飞书核实凭据:本地一律当作有效。
if (u.includes("/auth/v3/tenant_access_token/internal")) return Response.json({ code: 0, tenant_access_token: "t-fixture", expire: 7200 });
if (u.includes("/bot/v3/info")) return Response.json({ code: 0, bot: { app_name: "Fixture Bot", avatar_url: "" } });
throw new Error(`本地测试不允许外部请求:${u}`);
}) as typeof fetch;
const req = (path: string, init: { method?: string; body?: unknown; headers?: Record<string, string> } = {}) =>
api.request(path, {
method: init.method ?? (init.body === undefined ? "GET" : "POST"),
headers: { ...(init.body === undefined ? {} : { "content-type": "application/json" }), ...init.headers },
body: init.body === undefined ? undefined : JSON.stringify(init.body),
});
async function login(who: "employee" | "admin") {
const p = pkce();
const a = await req("/openapi-cli/auth/authorize", { body: p.body, headers: { "x-tt-session-v2": `fixture-${who}` } });
expect(a.status).toBe(200);
const code = new URL((await a.json() as any).redirect).searchParams.get("code")!;
const t = await req("/openapi-cli/auth/token", { body: { code, code_verifier: p.verifier, redirect_uri: p.body.redirect_uri } });
expect(t.status).toBe(200);
const tok = await t.json() as any;
// 同一个授权码不能兑换第二次。
expect((await req("/openapi-cli/auth/token", { body: { code, code_verifier: p.verifier, redirect_uri: p.body.redirect_uri } })).status).toBe(400);
return { Authorization: `Bearer ${tok.access_token}`, token: tok.access_token as string, email: tok.email as string };
}
const keyIds: string[] = [];
try {
// 没有浏览器 SSO 不能授权;只带 cookie 也不行(要显式的 SDK 凭据头)。
expect((await req("/openapi-cli/auth/authorize", { body: pkce().body })).status).toBe(401);
expect((await req("/openapi-cli/auth/authorize", { body: pkce().body, headers: { cookie: "x=1" } })).status).toBe(401);
const emp = await login("employee");
expect(emp.token.startsWith("slcli_")).toBe(true);
expect(emp.email).toBe("cli-employee@bytedance.com");
const auth = { Authorization: emp.Authorization };
expect((await (await req("/openapi-cli/auth/session", { headers: auth })).json() as any).scope).toBe("openapi:self");
// 会话只认 /cli/v1/*:Key 申请、机器人登记这些自助接口只认面板 SSO(命令行不再做系统接入)。
expect((await req("/openapi-key-requests", { body: { label: "CLI fixture", source: "cli-test" }, headers: auth })).status).toBe(401);
expect((await req("/openapi-key-requests/mine", { headers: auth })).status).toBe(401);
expect((await req("/openapi-lark-bots/mine", { headers: auth })).status).toBe(401);
expect((await (await req("/cli/v1/me", { headers: auth })).json() as any).email).toBe("cli-employee@bytedance.com");
const mineKey = await createOpenPlatformKey({ label: "CLI fixture", source: "cli-test", createdBy: "cli-employee@bytedance.com" });
keyIds.push(mineKey.key.id);
const secret = mineKey.secret;
// 会话本身不能建单。
expect((await req("/openapi/v1/tasks", { body: { title: "x", description: "y" }, headers: auth })).status).toBe(401);
// Key 建单 → 查单 / 列表 / 按 externalId 查。
const key = { Authorization: `Bearer ${secret}` };
const externalId = `cli-${crypto.randomUUID()}`;
const made = await req("/openapi/v1/tasks", { body: { title: "CLI fixture bug", description: "local only", platform: "android", externalId }, headers: key });
expect(made.status).toBe(200);
const taskId = (await made.json() as any).taskId as string;
const got = await req(`/openapi/v1/tasks/${taskId}`, { headers: key });
expect(got.status).toBe(200);
const view = await got.json() as any;
expect(view).toMatchObject({ taskId, externalId, title: "CLI fixture bug", platform: "android" });
expect(typeof view.statusLabel).toBe("string");
expect(view.failureReason).toBeUndefined();
expect((await (await req("/openapi/v1/tasks?limit=5", { headers: key })).json() as any).tasks.map((t: any) => t.taskId)).toContain(taskId);
expect((await (await req(`/openapi/v1/tasks?externalId=${externalId}`, { headers: key })).json() as any).tasks).toHaveLength(1);
expect((await req("/openapi/v1/tasks/does-not-exist", { headers: key })).status).toBe(404);
// 别人的 Key 看不到这条单的状态。
const other = await createOpenPlatformKey({ label: "other", source: "other", createdBy: "someone-else@bytedance.com" });
keyIds.push(other.key.id);
const foreign = await req(`/openapi/v1/tasks/${taskId}`, { headers: { Authorization: `Bearer ${other.secret}` } });
expect(foreign.status).toBe(403);
expect((await foreign.json() as any).status).toBeUndefined();
// Eddy 的会话不能冒充命令行会话。
expect((await req("/cli/v1/me", { headers: { Authorization: "Bearer eddy_sso_" + "a".repeat(43) } })).status).toBe(401);
// 退出后会话失效。
expect((await req("/openapi-cli/auth/logout", { body: {}, headers: auth })).status).toBe(200);
expect((await req("/cli/v1/me", { headers: auth })).status).toBe(401);
expect((await req("/openapi-cli/auth/session", { headers: auth })).status).toBe(401);
} finally {
for (const id of keyIds) await revokeOpenPlatformKey(id);
globalThis.fetch = realFetch;
}
}, 60_000);
test.skipIf(!local)("本人身份:会话建单 / 导入记在本人名下、只能确认自己的单;调用记入使用痕迹,管理台统计读得到", async () => {
// 每次运行用新的邮箱:本地库会留着上一轮的数据,按人统计的断言要只看这一轮。
const run = crypto.randomUUID().slice(0, 8);
const ALICE = `cli-alice-${run}@bytedance.com`, BOB = `cli-bob-${run}@bytedance.com`;
const { api } = await import("../src/routes");
const { Hono } = await import("hono");
// 和生产 index.ts 一样挂在 /api 下:使用痕迹的路由模板曾经只在直连时能取到,挂载后整批丢失。
const app = new Hono();
app.route("/api", api);
const { db } = await import("../src/db_client");
const s = await import("../src/schema");
const { eq } = await import("drizzle-orm");
const realFetch = globalThis.fetch;
globalThis.fetch = (async (input: any, init?: RequestInit) => {
const u = String(input);
if (u.includes("/invoke/getUserInfo")) {
const h = new Headers(init?.headers).get("x-tt-session-v2");
const email = h === "fixture-alice" ? ALICE : h === "fixture-bob" ? BOB
: h === "fixture-admin" ? "jiaopengwen@bytedance.com" : null;
return Response.json({ data: email ? { email } : null });
}
throw new Error(`本地测试不允许外部请求:${u}`);
}) as typeof fetch;
const req = (path: string, init: { method?: string; body?: unknown; headers?: Record<string, string> } = {}) =>
app.request("/api" + path, {
method: init.method ?? (init.body === undefined ? "GET" : "POST"),
headers: { ...(init.body === undefined ? {} : { "content-type": "application/json" }), ...init.headers },
body: init.body === undefined ? undefined : JSON.stringify(init.body),
});
async function login(who: string) {
const p = pkce();
const a = await req("/openapi-cli/auth/authorize", { body: p.body, headers: { "x-tt-session-v2": `fixture-${who}` } });
const code = new URL((await a.json() as any).redirect).searchParams.get("code")!;
const t = await (await req("/openapi-cli/auth/token", { body: { code, code_verifier: p.verifier, redirect_uri: p.body.redirect_uri } })).json() as any;
return { Authorization: `Bearer ${t.access_token}`, "X-Streamline-Client": "streamline-cli/0.2.0; agent=codex" };
}
try {
const alice = await login("alice");
const bob = await login("bob");
expect((await req("/cli/v1/tasks", { body: { title: "x", description: "y" } })).status).toBe(401);
expect((await (await req("/cli/v1/me", { headers: alice })).json() as any).email).toBe(ALICE);
const made = await req("/cli/v1/tasks", { body: { title: "Skill fixture", description: "local only", platform: "ios" }, headers: alice });
expect(made.status).toBe(200);
const taskId = (await made.json() as any).taskId as string;
const [row] = await db.select({ ch: s.tasks.createdChannel, by: s.tasks.creatorEmail, key: s.tasks.createdViaKeyId })
.from(s.tasks).where(eq(s.tasks.id, taskId));
expect(row).toEqual({ ch: "cli", by: ALICE, key: null });
// 列的是本人建的单;查单任何人都能查,并告诉是不是自己建的。
expect((await (await req("/cli/v1/tasks?limit=5", { headers: alice })).json() as any).tasks.map((t: any) => t.taskId)).toContain(taskId);
expect((await (await req("/cli/v1/tasks?limit=5", { headers: bob })).json() as any).tasks.map((t: any) => t.taskId)).not.toContain(taskId);
expect((await (await req(`/cli/v1/tasks/${taskId}`, { headers: bob })).json() as any)).toMatchObject({ taskId, createdByMe: false });
// 确认执行只能确认自己的单:别人 403;本人在评估中 409。
expect((await req(`/cli/v1/tasks/${taskId}/execute`, { body: {}, headers: bob })).status).toBe(403);
expect((await req(`/cli/v1/tasks/${taskId}/execute`, { body: {}, headers: alice })).status).toBe(409);
// 使用痕迹:带了路由模板、Agent、版本、关联任务;未登录的那次不记。
const events = await db.select().from(s.openapi_usage_events).where(eq(s.openapi_usage_events.actor, ALICE));
const create = events.find((e) => e.route === "POST /cli/v1/tasks");
expect(create).toMatchObject({ channel: "cli", status: 200, agent: "codex", clientVersion: "0.2.0", taskId });
expect(events.find((e) => e.route === "POST /cli/v1/tasks/:taskId/execute")).toMatchObject({ status: 409, taskId });
// 接力:现象进 description,线索加说明后进 remark;不完整的请求不建单。
expect((await req("/cli/v1/handoff", { body: { title: "t", description: "d" }, headers: alice })).status).toBe(400);
const handed = await req("/cli/v1/handoff", { headers: { ...alice, "X-Streamline-Client": "streamline-cli/0.3.0; agent=cursor" }, body: {
title: "暗色头像描边", description: "暗色下头像描边仍是白色", platform: "android", reproSteps: "1. 设暗色",
notes: "## 怀疑点\n- ProfileHeader.applyStroke 写死白色(把握:高)",
context: { repo: "git@code.byted.org:tiktok/android.git", branch: "fix/x", commit: "abcdef1", evil: "ignored" },
} });
expect(handed.status).toBe(200);
const handoffId = (await handed.json() as any).taskId as string;
const [h] = await db.select({ d: s.tasks.description, r: s.tasks.remark, ch: s.tasks.createdChannel, by: s.tasks.creatorEmail })
.from(s.tasks).where(eq(s.tasks.id, handoffId));
expect(h).toMatchObject({ ch: "cli", by: ALICE });
expect(h.d).toContain("暗色下头像描边仍是白色");
expect(h.r).toContain("不是已验证的事实");
expect(h.r).toContain("cursor");
expect(h.r).toContain("提交 abcdef1");
expect(h.r).toContain("ProfileHeader.applyStroke");
// 管理台统计:只有管理员能看;本人身份建的单算 Skill 入口。
expect((await req("/admin/openapi-usage?days=7", { headers: { "x-tt-session-v2": "fixture-alice" } })).status).toBe(403);
const stats = await (await req("/admin/openapi-usage?days=7", { headers: { "x-tt-session-v2": "fixture-admin" } })).json() as any;
expect(stats.summary.tasks.cli).toBeGreaterThanOrEqual(1);
expect(stats.summary.activeCliUsers).toBeGreaterThanOrEqual(1);
expect(stats.actors.find((a: any) => a.id === ALICE)).toMatchObject({ channel: "cli", tasks: 2, agents: ["codex", "cursor"] });
expect(stats.outcomes.find((o: any) => o.channel === "handoff").total).toBeGreaterThanOrEqual(1);
expect(stats.agents.find((g: any) => g.agent === "codex")?.users).toBeGreaterThanOrEqual(2);
const [sess] = await db.select().from(s.openapi_cli_sessions).where(eq(s.openapi_cli_sessions.email, ALICE));
expect(typeof sess.createdAt).toBe("number");
} finally {
globalThis.fetch = realFetch;
}
}, 60_000);
test.skipIf(!local || CHANGE_RUN_OPEN)("改动验证暂时下线:建单、复验一律 403,不落库", async () => {
const run = crypto.randomUUID().slice(0, 8);
const ME = `cli-paused-${run}@bytedance.com`;
const { api } = await import("../src/routes");
const { Hono } = await import("hono");
const app = new Hono();
app.route("/api", api);
const realFetch = globalThis.fetch;
globalThis.fetch = (async (input: any, init?: RequestInit) => {
const u = String(input);
if (u.includes("/invoke/getUserInfo"))
return Response.json({ data: new Headers(init?.headers).get("x-tt-session-v2") === "fixture-me" ? { email: ME } : null });
throw new Error(`本地测试不允许外部请求:${u}`);
}) as typeof fetch;
const req = (path: string, init: { body?: unknown; headers?: Record<string, string> } = {}) =>
app.request("/api" + path, {
method: init.body === undefined ? "GET" : "POST",
headers: { ...(init.body === undefined ? {} : { "content-type": "application/json" }), ...init.headers },
body: init.body === undefined ? undefined : JSON.stringify(init.body),
});
try {
const p = pkce();
const a = await req("/openapi-cli/auth/authorize", { body: p.body, headers: { "x-tt-session-v2": "fixture-me" } });
const code = new URL((await a.json() as any).redirect).searchParams.get("code")!;
const tok = await (await req("/openapi-cli/auth/token", { body: { code, code_verifier: p.verifier, redirect_uri: p.body.redirect_uri } })).json() as any;
const me = { Authorization: `Bearer ${tok.access_token}` };
const made = await req("/cli/v1/verifications", {
body: { title: "x", platform: "ios", cases: [{ title: "x", checkpoints: ["y"] }], change: { commit: "a".repeat(40), branch: "b" } }, headers: me,
});
expect(made.status).toBe(403);
expect(await made.json()).toMatchObject({ code: "change_run_paused" });
const rerun = await req("/cli/v1/verifications/whatever/rerun", { body: { change: { commit: "b".repeat(40) } }, headers: me });
expect(rerun.status).toBe(403);
const { listActiveVerificationsByCreator } = await import("../src/db");
expect(await listActiveVerificationsByCreator(ME)).toEqual([]);
} finally {
globalThis.fetch = realFetch;
}
}, 60_000);
test.skipIf(!local || !CHANGE_RUN_OPEN)("改动验证:命令行建单落成 change-run 规约;能被老卫星闸门认出;查询只给状态;复验换提交新建一条", async () => {
const run = crypto.randomUUID().slice(0, 8);
const ME = `cli-verify-${run}@bytedance.com`;
const { api } = await import("../src/routes");
const { Hono } = await import("hono");
const app = new Hono();
app.route("/api", api);
const { saveVerificationAuditReport, getVerification } = await import("../src/db");
const { parseVerifySpec } = await import("@bbbug/shared/verify_prompt");
const realFetch = globalThis.fetch;
globalThis.fetch = (async (input: any, init?: RequestInit) => {
const u = String(input);
if (u.includes("/invoke/getUserInfo"))
return Response.json({ data: new Headers(init?.headers).get("x-tt-session-v2") === "fixture-me" ? { email: ME } : null });
throw new Error(`本地测试不允许外部请求:${u}`);
}) as typeof fetch;
const req = (path: string, init: { body?: unknown; headers?: Record<string, string> } = {}) =>
app.request("/api" + path, {
method: init.body === undefined ? "GET" : "POST",
headers: { ...(init.body === undefined ? {} : { "content-type": "application/json" }), ...init.headers },
body: init.body === undefined ? undefined : JSON.stringify(init.body),
});
try {
const p = pkce();
const a = await req("/openapi-cli/auth/authorize", { body: p.body, headers: { "x-tt-session-v2": "fixture-me" } });
const code = new URL((await a.json() as any).redirect).searchParams.get("code")!;
const tok = await (await req("/openapi-cli/auth/token", { body: { code, code_verifier: p.verifier, redirect_uri: p.body.redirect_uri } })).json() as any;
const me = { Authorization: `Bearer ${tok.access_token}`, "X-Streamline-Client": "streamline-cli/0.4.0; agent=claude-code" };
const sha = "abcdef0123456789abcdef0123456789abcdef01";
const body = {
title: "暗色头像描边", platform: "android", background: "修了暗色模式头像描边是白色",
cases: [{ title: "个人页", pre: "暗色", step: "打开 我", checkpoints: ["描边是深色", "切回浅色恢复白色"] }, { title: "他人主页", checkpoints: ["描边正常"] }],
change: { repo: "git@code.byted.org:x/y.git", branch: "fix/stroke", commit: sha },
};
expect((await req("/cli/v1/verifications", { body: { ...body, change: { commit: "nope" } }, headers: me })).status).toBe(400);
expect((await req("/cli/v1/verifications", { body: { ...body, cases: [{ title: "x", checkpoints: [] }] }, headers: me })).status).toBe(400);
const made = await req("/cli/v1/verifications", { body, headers: me });
expect(made.status).toBe(200);
const id = (await made.json() as any).taskId as string;
const v = (await getVerification(id))!;
const spec = parseVerifySpec(v.verifySpec)!;
expect(spec.format).toBe("change-run");
expect(spec.change).toMatchObject({ commit: sha, branch: "fix/stroke", submittedBy: ME, agent: "claude-code" });
expect(spec.cases!.cases.map((c) => [c.id, c.checkpoints.map((x) => x.no)])).toEqual([["C01", [1, 2]], ["C02", [3]]]);
expect(spec.cases!.source).toMatchObject({ kind: "agent-session", submittedBy: ME });
// 通用建单接口不收改动验证:被测提交只能经命令行那条路进来。
const generic = await req("/verifications", { body: { description: "x", platform: "android", verifySpec: v.verifySpec } });
expect(generic.status).toBe(400);
// 老卫星不派:认领时靠 verify_spec 的 LIKE 认出改动验证(claimAnyFixForNode)。这里钉住落库的规约
// 确实能被这个模式认出来 —— 序列化方式一变,老卫星就会把它当 UI 走查跑。
// (认领本身在 pglite 上测不了:事务里又用另一条连接查节点集群,单线程的 pglite 会互等。)
const { db } = await import("../src/db_client");
const { verifications } = await import("../src/schema");
const { and, eq, like } = await import("drizzle-orm");
const { CHANGE_RUN_SPEC_LIKE } = await import("../src/db");
expect((await db.select({ id: verifications.id }).from(verifications)
.where(and(eq(verifications.id, id), like(verifications.verifySpec, CHANGE_RUN_SPEC_LIKE)))).length).toBe(1);
await (await import("../src/db")).updateVerification(id, { status: "verifying" });
// 查询只给状态、失败原因和面板链接;报告就绪后也不回传报告内容(回平台验收)。
expect((await (await req(`/cli/v1/verifications/${id}`, { headers: me })).json() as any)).toMatchObject({ taskId: id, statusLabel: "编译 / 执行中", failureReason: null });
await saveVerificationAuditReport(id, JSON.stringify({ format: "case-run", cases: [] }));
expect((await (await req(`/cli/v1/verifications/${id}`, { headers: me })).json() as any).report).toBeUndefined();
// 同提交去重:这个提交的验证还在跑,再建一次回的就是它,不新建。
const dup = await (await req("/cli/v1/verifications", { body, headers: me })).json() as any;
expect(dup).toMatchObject({ taskId: id, status: "exists" });
// 复验:同一份用例换新提交,建一条新的;同一个提交拒绝(那是平台上打回的事);只能复验自己的。
const same = await req(`/cli/v1/verifications/${id}/rerun`, { body: { change: { commit: sha } }, headers: me });
expect(same.status).toBe(409);
const sha2 = "1111111111111111111111111111111111111111";
const rr = await req(`/cli/v1/verifications/${id}/rerun`, { body: { change: { commit: sha2, branch: "fix/stroke" } }, headers: me });
expect(rr.status).toBe(200);
const rerun = await rr.json() as any;
expect(rerun.rerunOf).toBe(id);
const v2 = (await getVerification(rerun.taskId))!;
expect(v2.title).toBe("暗色头像描边(复验)");
expect(v2.description).toContain(id);
const spec2 = parseVerifySpec(v2.verifySpec)!;
expect(spec2.change!.commit).toBe(sha2);
expect(spec2.cases!.cases.map((c) => c.id)).toEqual(["C01", "C02"]);
expect(spec2.scope).toBe("修了暗色模式头像描边是白色");
// 并发上限:现在有 2 条在跑(原单 verifying + 复验 awaiting),第 3 个提交被拒,并列出进行中的。
const third = await req("/cli/v1/verifications", { body: { ...body, change: { ...body.change, commit: "3333333333333333333333333333333333333333" } }, headers: me });
expect(third.status).toBe(429);
expect(((await third.json()) as any).active.map((v: any) => v.taskId).sort()).toEqual([id, rerun.taskId].sort());
// 跑完一条(报告就绪)就又能提交了。
await (await import("../src/db")).updateVerification(id, { status: "report_ready" });
// 复验的复验不会叠成「(复验)(复验)」。
const rr2 = await (await req(`/cli/v1/verifications/${rerun.taskId}/rerun`, { body: { change: { commit: "2222222222222222222222222222222222222222" } }, headers: me })).json() as any;
expect((await getVerification(rr2.taskId))!.title).toBe("暗色头像描边(复验)");
} finally {
globalThis.fetch = realFetch;
}
}, 60_000);