- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名: * session-client.ts ➔ hose-web-session-client.ts (合思 Web 主站会话客户端) * script-engine-client.ts ➔ piper-workflow-client.ts (Piper 工作流引擎客户端) * unity-client.ts ➔ unity-easo-client.ts (Unity / EASO 运营中台与动态集群客户端) * sso-client.ts ➔ sso-auth-client.ts (SSO 与扫码鉴权客户端) - 新增 hose-docs-client.ts 纳为第 7 大通信底座客户端,收口 docs.ekuaibao.com 文档站点通信 - 端正 Piper 工作流引擎定位,完整覆盖工作流创建、保存、发布、108 类节点编排与 Job 流转 - 彻底整体替换全仓 import 引用与测试套件,新增 clients-semantics.test.ts 自动化校验 - 全量自动化测试 150 files 1155 tests 全部通过,类型检查零错误
139 lines
4.2 KiB
TypeScript
139 lines
4.2 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import {
|
|
createSsoClient,
|
|
getSsoQrCode,
|
|
pollSsoQrCodeStatus,
|
|
verifySsoTokenOnUnity,
|
|
} from "./sso-auth-client.js";
|
|
import { AuthenticationError } from "../errors.js";
|
|
|
|
describe("core/http/sso-auth-client", () => {
|
|
it("getSsoQrCode 能够正常解析二维码链接与 Session Cookie", async () => {
|
|
const mockClient = {
|
|
post: async () => ({
|
|
headers: new Headers({
|
|
"set-cookie": "SSO_SESSION_ID=mock-session-1234; Path=/; HttpOnly",
|
|
}),
|
|
json: async () => ({
|
|
code: 0,
|
|
message: "ok",
|
|
data: {
|
|
url: "https://login.dingtalk.com/oauth2/auth?mock=1",
|
|
appId: "12021",
|
|
redirect: "https://unity.ekuaibao.com/platform-charge/",
|
|
},
|
|
}),
|
|
}),
|
|
};
|
|
|
|
const res = await getSsoQrCode(mockClient as any);
|
|
expect(res.dingUrl).toContain("login.dingtalk.com");
|
|
expect(res.ssoSessionId).toBe("mock-session-1234");
|
|
});
|
|
|
|
it("getSsoQrCode 遇到业务错误时抛出 AuthenticationError", async () => {
|
|
const mockClient = {
|
|
post: async () => ({
|
|
headers: new Headers(),
|
|
json: async () => ({
|
|
code: 1,
|
|
message: "类型不支持",
|
|
}),
|
|
}),
|
|
};
|
|
|
|
expect(async () => {
|
|
await getSsoQrCode(mockClient as any);
|
|
}).toThrow("类型不支持");
|
|
});
|
|
|
|
it("pollSsoQrCodeStatus 能够正确传递 Cookie 并解析确认状态", async () => {
|
|
let capturedHeaders: any = null;
|
|
const mockClient = {
|
|
get: (url: string, opts: any) => {
|
|
capturedHeaders = opts.headers;
|
|
return {
|
|
json: async () => ({
|
|
code: 0,
|
|
message: "ok",
|
|
data: {
|
|
token: "mock-jwt-token",
|
|
redirect: "https://unity.ekuaibao.com/platform-charge/?token=mock-token",
|
|
},
|
|
}),
|
|
};
|
|
},
|
|
};
|
|
|
|
const res = await pollSsoQrCodeStatus(mockClient as any, {
|
|
sessionId: "session-abc",
|
|
});
|
|
|
|
expect(capturedHeaders.cookie).toBe("SSO_SESSION_ID=session-abc");
|
|
expect(res.code).toBe(0);
|
|
expect(typeof res.data).toBe("object");
|
|
});
|
|
|
|
it("verifySsoTokenOnUnity 优先从 redirect 提取 token 并完成换票", async () => {
|
|
// 拦截全局 fetch 测试 verifySsoTokenOnUnity
|
|
const originalFetch = globalThis.fetch;
|
|
try {
|
|
globalThis.fetch = (async (url: any, opts: any) => {
|
|
const req = new Request(url, opts);
|
|
const body = (await req.json()) as any;
|
|
if (body.token === "extracted-token") {
|
|
return new Response(
|
|
JSON.stringify({
|
|
value: {
|
|
token: "unity-session-uuid",
|
|
user: { name: "张三", userId: "zhangsan" },
|
|
},
|
|
}),
|
|
{ status: 200, headers: { "content-type": "application/json" } }
|
|
);
|
|
}
|
|
return new Response(JSON.stringify({ errorCode: 400 }), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}) as any;
|
|
|
|
const res = await verifySsoTokenOnUnity(
|
|
"https://unity.ekuaibao.com/platform-charge/?token=extracted-token"
|
|
);
|
|
|
|
expect(res.sessionToken).toBe("unity-session-uuid");
|
|
expect(res.userName).toBe("张三");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
it("verifySsoTokenOnUnity 当 token 包含点时自动 base64 处理", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
try {
|
|
let passedToken = "";
|
|
globalThis.fetch = (async (url: any, opts: any) => {
|
|
const req = new Request(url, opts);
|
|
const body = (await req.json()) as any;
|
|
passedToken = body.token;
|
|
return new Response(
|
|
JSON.stringify({
|
|
value: {
|
|
token: "unity-session-uuid-2",
|
|
user: { name: "李四" },
|
|
},
|
|
}),
|
|
{ status: 200, headers: { "content-type": "application/json" } }
|
|
);
|
|
}) as any;
|
|
|
|
const res = await verifySsoTokenOnUnity("jwt.with.dots");
|
|
expect(passedToken).toBe(Buffer.from("jwt.with.dots").toString("base64"));
|
|
expect(res.sessionToken).toBe("unity-session-uuid-2");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
});
|