81 lines
4.0 KiB
TypeScript
81 lines
4.0 KiB
TypeScript
import { afterEach, describe, expect, it, spyOn } from "bun:test";
|
|
import { getCorpProvisionalAuthLink } from "./openapi-onboard.js";
|
|
import * as openapi from "./openapi-v1.js";
|
|
import * as loader from "../config/loader.js";
|
|
import * as links from "./link-import.js";
|
|
import type { CorpConfig } from "../config/types.js";
|
|
|
|
describe("provisional link identity isolation", () => {
|
|
const spies: Array<{ mockRestore(): void }> = [];
|
|
afterEach(() => { while (spies.length) spies.pop()!.mockRestore(); });
|
|
function setup(uid = "corp:B") {
|
|
const corp: CorpConfig = {
|
|
id: "corp", name: "Mock", source: "openapi", baseUrl: "https://example.test",
|
|
staffId: "corp:A", staffName: "A", session: { staffName: "A" },
|
|
credentials: { accessToken: "token-A", corpId: "corp", staffId: "corp:A", appKey: "key", appSecurity: "security" },
|
|
};
|
|
const token = spyOn(openapi, "fetchHoseOpenApiAccessToken").mockResolvedValue("openapi");
|
|
const sign = spyOn(openapi, "fetchHoseProvisionalAuth").mockResolvedValue({ uid, token: "token-B", url: "https://example.test/link", expiresIn: 7200, expiresAt: Date.now() + 7200000 });
|
|
const save = spyOn(loader, "saveCorpConfig").mockReturnValue("/mock/corp.json");
|
|
const invalidate = spyOn(links, "invalidateCorpSubdomainTokenCaches").mockImplementation(() => {});
|
|
spies.push(token, sign, save, invalidate);
|
|
return { corp, save, sign, invalidate };
|
|
}
|
|
|
|
it("does not mutate or save the default identity for a temporary staff link", async () => {
|
|
const { corp, save, invalidate } = setup();
|
|
const before = structuredClone(corp);
|
|
const result = await getCorpProvisionalAuthLink(corp, { staffId: "corp:B", staffName: "B" });
|
|
expect(result.staffId).toBe("corp:B");
|
|
expect(result.staffName).toBe("B");
|
|
expect(corp).toEqual(before);
|
|
expect(save).not.toHaveBeenCalled();
|
|
expect(invalidate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("persists token and normalized identity together and clears the old staff name", async () => {
|
|
const { corp, save } = setup();
|
|
const result = await getCorpProvisionalAuthLink(corp, { staffId: "B", persistStaff: true });
|
|
expect(result.staffName).toBeUndefined();
|
|
expect(corp.credentials.accessToken).toBe("token-B");
|
|
expect(corp.credentials.staffId).toBe("corp:B");
|
|
expect(corp.staffId).toBe("corp:B");
|
|
expect(corp.staffName).toBeUndefined();
|
|
expect(corp.session?.staffName).toBeUndefined();
|
|
expect(save.mock.calls[0]?.[0].credentials.staffId).toBe("corp:B");
|
|
});
|
|
|
|
it("keeps ordinary renewal of the same staff working", async () => {
|
|
const { corp, save } = setup("corp:A");
|
|
await getCorpProvisionalAuthLink(corp);
|
|
expect(corp.credentials.accessToken).toBe("token-B");
|
|
expect(corp.credentials.staffId).toBe("corp:A");
|
|
expect(save).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("does not partly mutate memory when saving fails", async () => {
|
|
const { corp, save } = setup();
|
|
const before = structuredClone(corp);
|
|
save.mockImplementation(() => { throw new Error("disk unavailable"); });
|
|
await expect(getCorpProvisionalAuthLink(corp, { staffId: "corp:B", persistStaff: true })).rejects.toThrow("disk unavailable");
|
|
expect(corp).toEqual(before);
|
|
});
|
|
|
|
it("rejects a mismatched signing identity before persisting", async () => {
|
|
const { corp, save } = setup("corp:C");
|
|
const before = structuredClone(corp);
|
|
await expect(getCorpProvisionalAuthLink(corp, { staffId: "corp:B", persistStaff: true })).rejects.toThrow("身份与目标员工不一致");
|
|
expect(corp).toEqual(before);
|
|
expect(save).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not disguise an existing session as another staff without OpenAPI keys", async () => {
|
|
const { corp, sign, save } = setup();
|
|
delete corp.credentials.appKey;
|
|
delete corp.credentials.appSecurity;
|
|
await expect(getCorpProvisionalAuthLink(corp, { staffId: "corp:B" })).rejects.toThrow("需要已配置的 OpenAPI");
|
|
expect(sign).not.toHaveBeenCalled();
|
|
expect(save).not.toHaveBeenCalled();
|
|
});
|
|
});
|