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

@@ -0,0 +1,66 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { getRlsExecutionContext, runWithRlsContext } from "@hivegrid/db/rls-context.server";
const mocks = vi.hoisted(() => ({
findScopes: vi.fn(),
deleteForTenant: vi.fn(),
}));
vi.mock("@hivegrid/db/accounts.server", () => ({
adminGetAllLegacyTenantSummaries: vi.fn(async () => []),
}));
vi.mock("@hivegrid/db/api-keys.server", () => ({
countLegacyApiKeyLogs: vi.fn(),
deleteTenantLegacyApiKeyLogsByIds: mocks.deleteForTenant,
findLegacyApiKeyLogTenantScopesByIds: mocks.findScopes,
getLegacyApiKeyLogAdminDetails: vi.fn(),
groupLegacyApiKeyLogs: vi.fn(),
tenantLegacyApiKeyLogScope: (tenantId: string) => ({ tenantId }),
}));
import ApiKeyLogService from "./ApiKeyLogService";
describe("ApiKeyLogService admin RLS deletion", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.deleteForTenant.mockImplementation(async ({ tenantId }: { tenantId: string }) => {
expect(getRlsExecutionContext()).toMatchObject({ userId: "operator_1", tenantId });
return { count: 1 };
});
});
it("deletes each selected log only inside its owning tenant context", async () => {
mocks.findScopes.mockResolvedValue([
{ id: "log_a", tenantId: "tenant_a" },
{ id: "log_b", tenantId: "tenant_b" },
]);
const result = await runWithRlsContext({ userId: "operator_1" }, () => ApiKeyLogService.adminDeleteMany(["log_a", "log_b"]));
expect(result).toEqual({ deletedCount: 2, rejectedTenantlessIds: [] });
expect(mocks.deleteForTenant).toHaveBeenCalledTimes(2);
expect(mocks.deleteForTenant).toHaveBeenCalledWith({ ids: ["log_a"], tenantId: "tenant_a" });
expect(mocks.deleteForTenant).toHaveBeenCalledWith({ ids: ["log_b"], tenantId: "tenant_b" });
});
it("rejects the whole batch when it contains a tenantless platform log", async () => {
mocks.findScopes.mockResolvedValue([
{ id: "log_a", tenantId: "tenant_a" },
{ id: "log_platform", tenantId: null },
]);
const result = await runWithRlsContext({ userId: "operator_1" }, () => ApiKeyLogService.adminDeleteMany(["log_a", "log_platform"]));
expect(result).toEqual({ deletedCount: 0, rejectedTenantlessIds: ["log_platform"] });
expect(mocks.deleteForTenant).not.toHaveBeenCalled();
});
it("fails closed outside an unscoped authenticated operator request", async () => {
await expect(ApiKeyLogService.adminDeleteMany(["log_a"])).rejects.toThrow("unscoped authenticated operator context");
await expect(runWithRlsContext({ userId: "operator_1", tenantId: "tenant_a" }, () => ApiKeyLogService.adminDeleteMany(["log_a"]))).rejects.toThrow(
"unscoped authenticated operator context"
);
expect(mocks.findScopes).not.toHaveBeenCalled();
});
});
@@ -1,14 +1,15 @@
import { adminGetAllLegacyTenantSummaries } from "@hivegrid/db/accounts.server";
import {
adminDeleteLegacyApiKeyLogsByIds,
countLegacyApiKeyLogs,
deleteTenantLegacyApiKeyLogsByIds,
findLegacyApiKeyLogTenantScopesByIds,
getLegacyApiKeyLogAdminDetails,
groupLegacyApiKeyLogs,
tenantLegacyApiKeyLogScope,
type LegacyApiKeyLogOrderByInput,
type LegacyApiKeyLogWhereInput,
} from "@hivegrid/db/api-keys.server";
import { getRlsExecutionContext, runWithRlsContext } from "@hivegrid/db/rls-context.server";
import { Prisma } from "@hivegrid/db/prisma-browser";
import { Params } from "react-router";
import { FilterablePropertyDto } from "~/application/dtos/data/FilterablePropertyDto";
@@ -63,7 +64,7 @@ async function getDetails({ request, params }: { request: Request; params: Param
const totalItems = await countLegacyApiKeyLogs(whereFilters);
return {
items: items as ApiKeyLogDto[],
items: items.map(toApiKeyLogDto),
filterableProperties,
pagination: {
page: pagination.page,
@@ -170,12 +171,55 @@ async function getFilters({ request, params }: { request: Request; params: Param
return { filterableProperties, whereFilters, allTenants };
}
async function adminDeleteMany(ids: string[]) {
return await adminDeleteLegacyApiKeyLogsByIds(ids);
async function adminDeleteMany(ids: string[]): Promise<{ deletedCount: number; rejectedTenantlessIds: string[] }> {
const context = getRlsExecutionContext();
if (!context?.userId || context.tenantId) {
throw new Error("Admin API log deletion requires an unscoped authenticated operator context");
}
const normalizedIds = [...new Set(ids.map((id) => id.trim()).filter(Boolean))];
if (normalizedIds.length === 0) return { deletedCount: 0, rejectedTenantlessIds: [] };
if (normalizedIds.length > 500) {
throw new Error("At most 500 API log entries can be deleted at once");
}
const scopes = await findLegacyApiKeyLogTenantScopesByIds(normalizedIds);
const rejectedTenantlessIds = scopes.filter((scope) => !scope.tenantId).map((scope) => scope.id);
if (rejectedTenantlessIds.length > 0) {
return { deletedCount: 0, rejectedTenantlessIds };
}
const byTenant = new Map<string, string[]>();
for (const scope of scopes) {
if (!scope.tenantId) continue;
const tenantIds = byTenant.get(scope.tenantId) ?? [];
tenantIds.push(scope.id);
byTenant.set(scope.tenantId, tenantIds);
}
await Promise.all(
[...byTenant].map(([tenantId, tenantIds]) => runWithRlsContext({ tenantId }, () => deleteTenantLegacyApiKeyLogsByIds({ ids: tenantIds, tenantId })))
);
return { deletedCount: scopes.length, rejectedTenantlessIds: [] };
}
async function deleteManyForTenant(ids: string[], tenantId: string | null) {
return await deleteTenantLegacyApiKeyLogsByIds({ ids, tenantId });
return tenantId
? runWithRlsContext({ tenantId }, () => deleteTenantLegacyApiKeyLogsByIds({ ids, tenantId }))
: deleteTenantLegacyApiKeyLogsByIds({ ids, tenantId });
}
function toApiKeyLogDto(item: Awaited<ReturnType<typeof getLegacyApiKeyLogAdminDetails>>[number]): ApiKeyLogDto {
if (item.apiKey || !item.apiKeyId || !item.alias || !item.tenantId) return item as ApiKeyLogDto;
return {
...item,
apiKey: {
alias: item.alias,
tenant: {
id: item.tenantId,
name: item.tenant?.name ?? item.tenantName ?? item.tenantId,
},
},
} as ApiKeyLogDto;
}
export default {