import { expect, test } from "bun:test"; import { createAccountWorkflow, type Account, type Principal, } from "../scripts/openobserve-accounts"; const accounts = () => ({ ingest: { user: "vp7-ingest@example.invalid", token: "SyntheticIngestToken-123", }, query: { user: "vp7-query@example.invalid", token: "SyntheticQueryToken-123", }, }); function ports(positive = 200, deny = 403) { const events: string[] = []; return { events, positive: async (name: Principal) => { events.push("positive:" + name); return positive; }, deny: async (name: Principal) => { events.push("deny:" + name); return deny; }, publish: async (_accounts: Record) => { events.push("publish"); }, }; } test("saved native200 and403 accounts publish only after both account controls", async () => { const p = ports(); await createAccountWorkflow(p)(accounts()); expect(p.events).toEqual([ "positive:ingest", "deny:ingest", "positive:query", "deny:query", "publish", ]); }); for (const status of [401, 503, 201]) test(`positive HTTP${status} cannot publish derived credentials`, async () => { const p = ports(status); await expect(createAccountWorkflow(p)(accounts())).rejects.toThrow( `OPENOBSERVE_CREDENTIAL_INGEST_POSITIVE_HTTP_${status}`, ); expect(p.events).not.toContain("publish"); }); for (const status of [200, 503]) test(`unexpected deny HTTP${status} cannot publish derived credentials`, async () => { const p = ports(200, status); await expect(createAccountWorkflow(p)(accounts())).rejects.toThrow( `OPENOBSERVE_CREDENTIAL_INGEST_DENY_HTTP_${status}`, ); expect(p.events).not.toContain("publish"); }); for (const patch of [ { user: "vp7-owner@example.invalid" }, { user: "foreign@example.invalid" }, { token: "" }, { token: "bad\nvalue" }, ]) test(`invalid saved query shape ${Object.keys(patch)[0]} cannot publish or probe`, async () => { const p = ports(), saved = accounts(); saved.query = { ...saved.query, ...patch }; await expect(createAccountWorkflow(p)(saved)).rejects.toThrow( "OPENOBSERVE_ACCOUNT_INVALID_RESPONSE", ); expect(p.events).toEqual([]); }); test("extra owner slot never reaches credential publication", async () => { const p = ports(); await expect( createAccountWorkflow(p)({ ...accounts(), owner: { user: "vp7-owner@example.invalid", token: "owner-slot" }, } as any), ).rejects.toThrow("OPENOBSERVE_ACCOUNT_SLOTS_INVALID"); expect(p.events).toEqual([]); }); test("workflow requires explicit native and publishing ports", () => { expect(() => createAccountWorkflow({ publish: ports().publish } as any), ).toThrow("OPENOBSERVE_ACCOUNT_WORKFLOW_PORTS_REQUIRED"); }); test("invalid query token after a healthy ingest control still cannot publish any derived file", async () => { const p = ports(); p.positive = async (name: Principal) => { p.events.push("positive:" + name); return name === "query" ? 401 : 200; }; await expect(createAccountWorkflow(p)(accounts())).rejects.toThrow( "OPENOBSERVE_CREDENTIAL_QUERY_POSITIVE_HTTP_401", ); expect(p.events).not.toContain("publish"); });