99 lines
3.0 KiB
TypeScript
99 lines
3.0 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test";
|
|
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { CorpConfig, HoseSealSource } from "../config/types.js";
|
|
import { defaultSealUrl, getHoseEnterpriseUrl, normalizeBaseUrl } from "./hose.js";
|
|
|
|
const ORIGINAL_ENV = process.env.SEAL_HOME_TOKEN_STORE;
|
|
const tempDirs: string[] = [];
|
|
|
|
function setupTokenStore(data: Record<string, unknown>): string {
|
|
const dir = mkdtempSync(join(tmpdir(), "hose-auth-test-"));
|
|
tempDirs.push(dir);
|
|
const storePath = join(dir, "tokens.json");
|
|
process.env.SEAL_HOME_TOKEN_STORE = storePath;
|
|
writeFileSync(storePath, JSON.stringify(data, null, 2));
|
|
return storePath;
|
|
}
|
|
|
|
afterEach(() => {
|
|
if (ORIGINAL_ENV === undefined) {
|
|
delete process.env.SEAL_HOME_TOKEN_STORE;
|
|
} else {
|
|
process.env.SEAL_HOME_TOKEN_STORE = ORIGINAL_ENV;
|
|
}
|
|
|
|
for (const dir of tempDirs.splice(0)) {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
describe("hose auth helpers", () => {
|
|
test("normalizeBaseUrl strips trailing slashes", () => {
|
|
expect(normalizeBaseUrl("https://app.ekuaibao.com///")).toBe("https://app.ekuaibao.com");
|
|
expect(normalizeBaseUrl("https://app.ekuaibao.com")).toBe("https://app.ekuaibao.com");
|
|
});
|
|
|
|
test("defaultSealUrl resolves corp.seal.url, source.sealUrl, or fallback subdomain", () => {
|
|
const source: HoseSealSource = {
|
|
type: "hose",
|
|
domain: "https://app.ekuaibao.com",
|
|
appKey: "key",
|
|
appSecurity: "sec",
|
|
staffId: "user1",
|
|
corpId: "CORP123"
|
|
};
|
|
|
|
const corpWithSealUrl: CorpConfig = {
|
|
id: "corp-1",
|
|
name: "Corp 1",
|
|
seal: { url: "https://custom.sealai.cc", endpoints: { approvalStylePreferences: "" } },
|
|
source,
|
|
auth: { refreshTtl: 300 }
|
|
};
|
|
expect(defaultSealUrl(corpWithSealUrl, source)).toBe("https://custom.sealai.cc");
|
|
|
|
const corpWithoutSealUrl: CorpConfig = {
|
|
id: "corp-1",
|
|
name: "Corp 1",
|
|
seal: { endpoints: { approvalStylePreferences: "" } },
|
|
source,
|
|
auth: { refreshTtl: 300 }
|
|
};
|
|
expect(defaultSealUrl(corpWithoutSealUrl, source)).toBe("https://corp123.sealai.cc");
|
|
});
|
|
|
|
test("getHoseEnterpriseUrl retrieves cached provisional auth URL", async () => {
|
|
setupTokenStore({
|
|
"corp-cached": {
|
|
hose: {
|
|
closeapi: {
|
|
token: "close-token",
|
|
url: "https://app.ekuaibao.com/web/home.html?accessToken=close-token",
|
|
expiresAt: Date.now() + 3600_000
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
const corp: CorpConfig = {
|
|
id: "corp-cached",
|
|
name: "Corp Cached",
|
|
seal: { endpoints: { approvalStylePreferences: "" } },
|
|
source: {
|
|
type: "hose",
|
|
domain: "https://app.ekuaibao.com",
|
|
appKey: "key",
|
|
appSecurity: "sec",
|
|
staffId: "staff",
|
|
corpId: "corp"
|
|
},
|
|
auth: { refreshTtl: 300 }
|
|
};
|
|
|
|
const url = await getHoseEnterpriseUrl(corp);
|
|
expect(url).toBe("https://app.ekuaibao.com/web/home.html?accessToken=close-token");
|
|
});
|
|
});
|