Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -7,6 +7,7 @@ const getUserInfoMock = vi.fn();
const evaluateFlagForContextMock = vi.fn();
const getTenantUserTypeMock = vi.fn();
const knowledgeBaseArticleBelongsToTenantMock = vi.fn();
const getUserPermissionMock = vi.fn();
vi.mock("~/utils/session.server", () => ({
getUserInfo: getUserInfoMock,
@@ -20,6 +21,9 @@ vi.mock("~/utils/db/tenants.db.server", () => ({
vi.mock("~/modules/knowledgeBase/db/kbArticles.db.server", () => ({
knowledgeBaseArticleBelongsToTenant: knowledgeBaseArticleBelongsToTenantMock,
}));
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({
getUserPermission: getUserPermissionMock,
}));
const TEST_SECRET = "test-collab-secret-please-rotate-min-32";
@@ -28,6 +32,11 @@ async function importAction() {
return mod.action;
}
async function currentRlsContext() {
const mod = await import("@hivegrid/db/rls-context.server");
return mod.getRlsExecutionContext();
}
function buildRequest(body: unknown, method: "POST" | "GET" = "POST"): Request {
return new Request("http://localhost/api/collab/token", {
method,
@@ -43,8 +52,10 @@ describe("POST /api/collab/token", () => {
evaluateFlagForContextMock.mockReset();
getTenantUserTypeMock.mockReset();
knowledgeBaseArticleBelongsToTenantMock.mockReset();
getUserPermissionMock.mockReset();
// Resource-access defaults to allowed so existing flag/membership tests are unaffected.
knowledgeBaseArticleBelongsToTenantMock.mockResolvedValue(true);
getUserPermissionMock.mockResolvedValue({ permission: { id: "p-1" }, userPermission: "admin.kb.update" });
process.env.HOCUSPOCUS_AUTH_SECRET = TEST_SECRET;
});
afterEach(() => {
@@ -58,6 +69,13 @@ describe("POST /api/collab/token", () => {
expect(res.status).toBe(503);
});
it("returns 503 when the signing secret is too short", async () => {
process.env.HOCUSPOCUS_AUTH_SECRET = "too-short";
const action = await importAction();
const res = await action({ request: buildRequest({ tenantId: "t", resourceType: "kb-article", resourceId: "r" }) } as never);
expect(res.status).toBe(503);
});
it("returns 405 for non-POST methods", async () => {
const action = await importAction();
const res = await action({ request: buildRequest(undefined, "GET") } as never);
@@ -111,6 +129,21 @@ describe("POST /api/collab/token", () => {
expect(await res.json()).toEqual({ error: "forbidden", reason: "tenant-membership-required" });
expect(getTenantUserTypeMock).toHaveBeenCalledWith("u_1", "other_tenant");
expect(evaluateFlagForContextMock).not.toHaveBeenCalled();
expect(getUserPermissionMock).not.toHaveBeenCalled();
});
it("returns 403 before feature and resource checks when the user cannot update knowledge-base content", async () => {
getUserInfoMock.mockResolvedValue({ userId: "u_1" });
getTenantUserTypeMock.mockResolvedValue({ status: 2, type: 1 });
getUserPermissionMock.mockResolvedValue({ permission: { id: "p-1" }, userPermission: undefined });
const action = await importAction();
const res = await action({ request: buildRequest({ tenantId: "tenant_abc", resourceType: "kb-article", resourceId: "art_1" }) } as never);
expect(res.status).toBe(403);
expect(await res.json()).toEqual({ error: "forbidden", reason: "resource-write-permission-required" });
expect(getUserPermissionMock).toHaveBeenCalledWith({ userId: "u_1", permissionName: "admin.kb.update" });
expect(evaluateFlagForContextMock).not.toHaveBeenCalled();
expect(knowledgeBaseArticleBelongsToTenantMock).not.toHaveBeenCalled();
});
it("returns 403 when the feature-flag is disabled for the tenant", async () => {
@@ -141,12 +174,44 @@ describe("POST /api/collab/token", () => {
resourceId: string;
exp: number;
iat: number;
iss: string;
aud: string;
sub: string;
jti: string;
};
expect(decoded.userId).toBe("u_42");
expect(decoded.tenantId).toBe("tenant_abc");
expect(decoded.resourceType).toBe("kb-article");
expect(decoded.resourceId).toBe("art_xyz");
expect(decoded.exp - decoded.iat).toBe(900);
expect(decoded.iss).toBe("hivegrid-web");
expect(decoded.aud).toBe("hivegrid-hocuspocus");
expect(decoded.sub).toBe("u_42");
expect(decoded.jti).toMatch(/^[0-9a-f-]{36}$/u);
});
it("binds feature, resource and token work to the verified tenant membership without leaking context", async () => {
getUserInfoMock.mockResolvedValue({ userId: "u_scoped" });
getTenantUserTypeMock.mockImplementation(async () => {
expect(await currentRlsContext()).toBeNull();
return { status: 2, type: 1 };
});
evaluateFlagForContextMock.mockImplementation(async () => {
expect(await currentRlsContext()).toMatchObject({ tenantId: "tenant_scoped", userId: "u_scoped" });
return { enabled: true, reason: "matched" };
});
knowledgeBaseArticleBelongsToTenantMock.mockImplementation(async () => {
expect(await currentRlsContext()).toMatchObject({ tenantId: "tenant_scoped", userId: "u_scoped" });
return true;
});
const action = await importAction();
const response = await action({
request: buildRequest({ tenantId: "tenant_scoped", resourceType: "kb-article", resourceId: "art_1" }),
} as never);
expect(response.status).toBe(200);
expect(await currentRlsContext()).toBeNull();
});
it("returns 403 for a kb-article that does not belong to the tenant", async () => {
@@ -157,6 +222,8 @@ describe("POST /api/collab/token", () => {
const action = await importAction();
const res = await action({ request: buildRequest({ tenantId: "tenant_abc", resourceType: "kb-article", resourceId: "art_foreign" }) } as never);
expect(res.status).toBe(403);
expect((await res.json()) as { token?: string }).not.toHaveProperty("token");
expect(await currentRlsContext()).toBeNull();
});
it("returns 403 for an unknown resource type (fail closed)", async () => {