132 lines
4.0 KiB
TypeScript
132 lines
4.0 KiB
TypeScript
import { afterEach, describe, expect, test } from "bun:test";
|
|
import { chmodSync, mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import {
|
|
clearCorpTokenCache,
|
|
clearSealSession,
|
|
getCorpTokenCache,
|
|
getHoseAuthCache,
|
|
getSealSession,
|
|
setHoseAuthCache,
|
|
setSealSession
|
|
} from "./token-store.js";
|
|
import { SealSession } from "./types.js";
|
|
|
|
const ORIGINAL_USE_ENV = process.env.SEAL_USE_TOKEN_STORE;
|
|
const ORIGINAL_HOME_ENV = process.env.SEAL_HOME_TOKEN_STORE;
|
|
const tempDirs: string[] = [];
|
|
|
|
function makeTempStore(useLegacy = false): string {
|
|
const dir = mkdtempSync(join(tmpdir(), "seal-token-store-test-"));
|
|
tempDirs.push(dir);
|
|
const filePath = join(dir, "tokens.json");
|
|
if (useLegacy) {
|
|
process.env.SEAL_HOME_TOKEN_STORE = filePath;
|
|
} else {
|
|
process.env.SEAL_USE_TOKEN_STORE = filePath;
|
|
}
|
|
return filePath;
|
|
}
|
|
|
|
afterEach(() => {
|
|
if (ORIGINAL_USE_ENV === undefined) {
|
|
delete process.env.SEAL_USE_TOKEN_STORE;
|
|
} else {
|
|
process.env.SEAL_USE_TOKEN_STORE = ORIGINAL_USE_ENV;
|
|
}
|
|
if (ORIGINAL_HOME_ENV === undefined) {
|
|
delete process.env.SEAL_HOME_TOKEN_STORE;
|
|
} else {
|
|
process.env.SEAL_HOME_TOKEN_STORE = ORIGINAL_HOME_ENV;
|
|
}
|
|
|
|
for (const dir of tempDirs.splice(0)) {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
describe("token-store", () => {
|
|
test("returns empty cache when store file does not exist", () => {
|
|
makeTempStore();
|
|
expect(getSealSession("corp-test")).toBeUndefined();
|
|
expect(getCorpTokenCache("corp-test")).toEqual({});
|
|
expect(getHoseAuthCache("corp-test")).toEqual({});
|
|
});
|
|
|
|
test("handles corrupted JSON gracefully", () => {
|
|
const storePath = makeTempStore();
|
|
writeFileSync(storePath, "{ invalid json", "utf-8");
|
|
expect(getSealSession("corp-test")).toBeUndefined();
|
|
});
|
|
|
|
test("sets, retrieves, and clears seal session", () => {
|
|
makeTempStore();
|
|
const session: SealSession = {
|
|
token: "test-token-123",
|
|
expiresAt: Date.now() + 3600_000,
|
|
enterprise: {
|
|
provider: "seal",
|
|
corpId: "corp-1",
|
|
staffId: "staff-1",
|
|
sealUrl: "https://seal.example.com"
|
|
}
|
|
};
|
|
|
|
setSealSession("corp-1", session);
|
|
expect(getSealSession("corp-1")).toEqual(session);
|
|
|
|
clearSealSession("corp-1");
|
|
expect(getSealSession("corp-1")).toBeUndefined();
|
|
});
|
|
|
|
test.skipIf(process.platform === "win32")("tightens an existing token store without losing other corp caches", () => {
|
|
const path = makeTempStore();
|
|
const cache = { openapi: { token: "fixture-token", expiresAt: Date.now() + 3600000 } };
|
|
setHoseAuthCache("other", cache);
|
|
chmodSync(path, 0o644);
|
|
setHoseAuthCache("target", cache);
|
|
expect(statSync(path).mode & 0o777).toBe(0o600);
|
|
expect(getHoseAuthCache("other")).toEqual(cache);
|
|
expect(getHoseAuthCache("target")).toEqual(cache);
|
|
});
|
|
|
|
test("sets, retrieves, and clears hose auth cache", () => {
|
|
makeTempStore();
|
|
const hoseCache = {
|
|
openapi: {
|
|
token: "openapi-token",
|
|
expiresAt: Date.now() + 3600_000
|
|
},
|
|
closeapi: {
|
|
token: "closeapi-token",
|
|
url: "https://app.ekuaibao.com/web/home.html",
|
|
expiresAt: Date.now() + 7200_000
|
|
}
|
|
};
|
|
|
|
setHoseAuthCache("corp-hose", hoseCache);
|
|
expect(getHoseAuthCache("corp-hose")).toEqual(hoseCache);
|
|
|
|
clearCorpTokenCache("corp-hose");
|
|
expect(getHoseAuthCache("corp-hose")).toEqual({});
|
|
expect(getCorpTokenCache("corp-hose")).toEqual({});
|
|
});
|
|
|
|
test("falls back to reading legacy token store when primary does not exist", () => {
|
|
const legacyPath = makeTempStore(true);
|
|
const session: SealSession = {
|
|
token: "legacy-token-456",
|
|
expiresAt: Date.now() + 3600_000,
|
|
enterprise: {
|
|
provider: "seal",
|
|
corpId: "corp-legacy",
|
|
staffId: "staff-legacy",
|
|
sealUrl: "https://seal.example.com"
|
|
}
|
|
};
|
|
writeFileSync(legacyPath, JSON.stringify({ "corp-legacy": { seal: session } }));
|
|
expect(getSealSession("corp-legacy")).toEqual(session);
|
|
});
|
|
});
|