Files
2026-11-16 17:26:44 -08:00

125 lines
5.1 KiB
TypeScript

// MUST be first — pins env before anything that reads it at module load.
import "./setup_env";
import { test, expect, describe, afterAll } from "bun:test";
import { mkdtempSync, mkdirSync, rmSync, existsSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
const ROOT = mkdtempSync(path.join(tmpdir(), "lark-users-"));
process.env.LARK_USER_STORE_DIR = ROOT;
const mod = await import("../src/lark_user_auth");
const { isValidPrefix, larkUserSlot, larkUserAuthStatus, forgetLarkUserAuth } = mod;
afterAll(() => rmSync(ROOT, { recursive: true, force: true }));
/**
* 这一组守的是「按人隔离的飞书身份」最容易出事的两件事:
*
* 1. prefix 会变成路径段。它同时来自 SSO 邮箱**和任务的 commitAuthor** —— 后者是
* 数据不是登录态,所以必须校验而不是信任:`..` 一旦进了路径段,「每人一个 store」
* 就变成「往任意位置写」。
* 2. 没授权的人必须干脆地报「没授权」,因为这个返回值就是面板的前置探测:已授权的
* 人不该再被弹授权框,没授权的人也不该被静默降级。
*/
describe("按人隔离的 store 路径", () => {
test("正常 prefix 落到自己的目录里", () => {
expect(larkUserSlot("kongzy")).toBe(path.join(ROOT, "kongzy"));
});
test("大小写归一,免得同一个人有两份身份", () => {
expect(larkUserSlot("KongZY")).toBe(path.join(ROOT, "kongzy"));
});
test("路径穿越一律拒绝", () => {
for (const bad of ["..", "../../etc", "a/b", "/abs", "", " ", "a b"]) {
expect(larkUserSlot(bad)).toBeNull();
}
});
test("isValidPrefix 只放行合理的用户名形状", () => {
expect(isValidPrefix("kongzy")).toBe(true);
expect(isValidPrefix("jiao.peng-wen_1")).toBe(true);
expect(isValidPrefix(".hidden")).toBe(false);
expect(isValidPrefix("../x")).toBe(false);
expect(isValidPrefix("x".repeat(65))).toBe(false);
});
});
describe("前置探测", () => {
test("没授权过的人:authorized=false,并说明原因", async () => {
const state = await larkUserAuthStatus("nobody-here");
expect(state.authorized).toBe(false);
expect(state.reason).toBeTruthy();
});
test("非法 prefix 不去碰磁盘,直接判无效", async () => {
const state = await larkUserAuthStatus("../../etc");
expect(state.authorized).toBe(false);
expect(state.reason).toContain("invalid");
});
test("解除授权是幂等的,并且真的把目录删掉", () => {
const slot = larkUserSlot("tempuser")!;
mkdirSync(slot, { recursive: true });
expect(existsSync(slot)).toBe(true);
expect(forgetLarkUserAuth("tempuser")).toBe(true);
expect(existsSync(slot)).toBe(false);
expect(forgetLarkUserAuth("tempuser")).toBe(true);
});
test("解除一个非法 prefix 不会误删别人的目录", () => {
const victim = larkUserSlot("victim")!;
mkdirSync(victim, { recursive: true });
expect(forgetLarkUserAuth("../victim")).toBe(false);
expect(existsSync(victim)).toBe(true);
});
});
/**
* agent 读飞书用谁的身份:建单人 → watcher 逐个,只认本人在面板上授权过的。
* 主机上不再有一个兜底的「操作员」身份 —— 谁都没授权就是没有 token,
* 跟建单时 chip 读到的结果一致。
*/
describe("按任务挑读文档的身份", () => {
const { larkTokenForCandidates, larkUserPrefixes } = mod;
const ok = (p: string) => ({ ok: true as const, accessToken: `uat-${p}`, appId: "cli_x", expiresAt: 0, brand: "feishu" });
test("取第一个本人授权过的;没 store 的人直接跳过,不去问 lark-cli", async () => {
mkdirSync(larkUserSlot("watcher-a")!, { recursive: true });
mkdirSync(larkUserSlot("watcher-b")!, { recursive: true });
const asked: string[] = [];
const r = await larkTokenForCandidates(["creator-none", "watcher-a", "watcher-b"], async (p) => {
asked.push(p);
return ok(p);
});
expect(r.ok && r.accessToken).toBe("uat-watcher-a");
expect(r.usedOwner).toBe("watcher-a");
expect(asked).toEqual(["watcher-a"]);
});
test("store 在但续不上(比如 7 天没续过)就换下一位", async () => {
const r = await larkTokenForCandidates(["watcher-a", "watcher-b"], async (p) =>
p === "watcher-a" ? { ok: false, reason: "refresh token expired" } : ok(p));
expect(r.usedOwner).toBe("watcher-b");
});
test("谁都没有可用身份:不兜底,报第一位的失败原因", async () => {
const r = await larkTokenForCandidates(["watcher-a", "nobody", null, "WATCHER-A"], async () =>
({ ok: false, reason: "refresh token expired" }));
expect(r.ok).toBe(false);
expect(!r.ok && r.reason).toBe("refresh token expired");
const none = await larkTokenForCandidates(["nobody"], async (p) => ok(p));
expect(none.ok).toBe(false);
});
test("续期循环覆盖每个人的 store,非法目录名不算", () => {
mkdirSync(path.join(ROOT, ".hidden"), { recursive: true });
const ps = larkUserPrefixes();
expect(ps).toContain("watcher-a");
expect(ps).toContain("watcher-b");
expect(ps).not.toContain(".hidden");
});
});