Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/modules/knowledgeBase/services/publicKnowledgeBaseRls.server.test.ts
T

52 lines
2.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const { resolvePublicKnowledgeBaseRlsIdentity, listPublicKnowledgeBaseRlsIdentities, runWithIsolatedPublicKnowledgeBaseRlsContext } = vi.hoisted(() => ({
resolvePublicKnowledgeBaseRlsIdentity: vi.fn(),
listPublicKnowledgeBaseRlsIdentities: vi.fn(),
runWithIsolatedPublicKnowledgeBaseRlsContext: vi.fn(async (_resourceId: string, operation: () => Promise<unknown>) => operation()),
}));
vi.mock("@hivegrid/db/rls-resolvers.server", () => ({
resolvePublicKnowledgeBaseRlsIdentity,
listPublicKnowledgeBaseRlsIdentities,
}));
vi.mock("@hivegrid/db/rls-context.server", () => ({
runWithIsolatedPublicKnowledgeBaseRlsContext,
}));
import { mapPublicKnowledgeBasesWithRlsContext, withPublicKnowledgeBaseRlsContext } from "./publicKnowledgeBaseRls.server";
describe("public knowledge-base RLS context", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("fails closed when the slug resolver does not expose an enabled global knowledge base", async () => {
resolvePublicKnowledgeBaseRlsIdentity.mockResolvedValue(null);
const operation = vi.fn();
await expect(withPublicKnowledgeBaseRlsContext("disabled", operation)).resolves.toBeNull();
expect(operation).not.toHaveBeenCalled();
expect(runWithIsolatedPublicKnowledgeBaseRlsContext).not.toHaveBeenCalled();
});
it("binds the resolved id instead of trusting the public slug", async () => {
resolvePublicKnowledgeBaseRlsIdentity.mockResolvedValue({ resourceId: "kb-public" });
const operation = vi.fn().mockResolvedValue("visible");
await expect(withPublicKnowledgeBaseRlsContext("docs", operation)).resolves.toBe("visible");
expect(runWithIsolatedPublicKnowledgeBaseRlsContext).toHaveBeenCalledWith("kb-public", operation);
});
it("runs sitemap operations once per resolver-approved public knowledge base", async () => {
listPublicKnowledgeBaseRlsIdentities.mockResolvedValue([{ resourceId: "kb-a" }, { resourceId: "kb-b" }]);
const operation = vi.fn(async (knowledgeBaseId: string) => (knowledgeBaseId === "kb-a" ? { id: knowledgeBaseId } : null));
await expect(mapPublicKnowledgeBasesWithRlsContext(operation)).resolves.toEqual([{ id: "kb-a" }]);
expect(operation).toHaveBeenCalledTimes(2);
expect(runWithIsolatedPublicKnowledgeBaseRlsContext).toHaveBeenNthCalledWith(1, "kb-a", expect.any(Function));
expect(runWithIsolatedPublicKnowledgeBaseRlsContext).toHaveBeenNthCalledWith(2, "kb-b", expect.any(Function));
});
});