53 lines
2.2 KiB
TypeScript
53 lines
2.2 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import { canRefreshCorpAccessToken, resolveLinkExpiresAt } from "./login.js";
|
|
import { createEkbClient } from "./ekb.js";
|
|
import type { CorpConfig } from "../config/types.js";
|
|
|
|
function corp(overrides: Partial<CorpConfig> = {}): CorpConfig {
|
|
return {
|
|
id: "corp-1",
|
|
name: "测试企业",
|
|
source: "link",
|
|
credentials: { accessToken: "token", corpId: "corp-1", staffId: "staff-1" },
|
|
...overrides,
|
|
baseUrl: overrides.baseUrl ?? "https://app.ekuaibao.com",
|
|
};
|
|
}
|
|
|
|
describe("core/auth/ekb refresh policy", () => {
|
|
it("only allows account credentials with an explicit account binding to refresh", () => {
|
|
expect(canRefreshCorpAccessToken(corp())).toBe(false);
|
|
expect(canRefreshCorpAccessToken(corp({ source: "browser", accountId: "86-1" }))).toBe(false);
|
|
expect(canRefreshCorpAccessToken(corp({ source: "account" }))).toBe(false);
|
|
expect(canRefreshCorpAccessToken(corp({ source: "account", accountId: "86-1" }))).toBe(true);
|
|
expect(canRefreshCorpAccessToken(corp({ source: "account", accountId: "86-1", session: { isShortTermToken: true } }))).toBe(false);
|
|
});
|
|
|
|
it("fails expired temporary credentials without retrying or refreshing", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
let calls = 0;
|
|
globalThis.fetch = (async () => {
|
|
calls += 1;
|
|
return new Response("expired", { status: 401 });
|
|
}) as unknown as typeof fetch;
|
|
|
|
try {
|
|
const client = await createEkbClient(corp());
|
|
await expect(client.get("api/test")).rejects.toThrow("临时凭证已过期,请使用 hose-use corps add-link 重新导入");
|
|
expect(calls).toBe(1);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("core/auth/login link expiry", () => {
|
|
it("parses explicit ttl and expires durations", () => {
|
|
const base = 1_700_000_000_000;
|
|
expect(resolveLinkExpiresAt({ ttl: "72h" }, base)).toBe(base + 72 * 60 * 60 * 1000);
|
|
expect(resolveLinkExpiresAt({ ttl: "3d" }, base)).toBe(base + 3 * 24 * 60 * 60 * 1000);
|
|
expect(resolveLinkExpiresAt({ expires: "3" }, base)).toBe(base + 3 * 24 * 60 * 60 * 1000);
|
|
expect(resolveLinkExpiresAt({}, base)).toBeUndefined();
|
|
});
|
|
});
|