Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/db/src/privacy-gdpr.server.test.ts
T
2026-07-08 16:15:37 +02:00

319 lines
9.2 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import {
GDPR_EXPORT_URL_TTL_HOURS,
GDPR_TENANT_DELETION_GRACE_DAYS,
acceptAvv,
cancelTenantDeletionRequest,
createGdprExportJob,
createTenantDeletionRequest,
listDueTenantDeletionRequests,
markGdprExportDownloaded,
markGdprExportFailed,
markGdprExportProcessing,
markGdprExportReady,
type PrivacyGdprClient,
} from "./privacy-gdpr.server.js";
function createClient(overrides: {
exportCount?: number;
deletionCount?: number;
updateCount?: number;
} = {}): PrivacyGdprClient {
return {
gdprExportJob: {
count: vi.fn().mockResolvedValue(overrides.exportCount ?? 0),
create: vi.fn(async ({ data }) => ({
id: "gdpr_export_1",
createdAt: new Date("2026-07-02T09:00:00.000Z"),
updatedAt: new Date("2026-07-02T09:00:00.000Z"),
resultFileId: null,
resultBucket: null,
resultKey: null,
resultSizeBytes: null,
resultExpiresAt: null,
downloadCount: 0,
...data,
})),
findMany: vi.fn().mockResolvedValue([]),
updateMany: vi.fn().mockResolvedValue({ count: overrides.updateCount ?? 1 }),
},
tenantDeletionRequest: {
count: vi.fn().mockResolvedValue(overrides.deletionCount ?? 0),
create: vi.fn(async ({ data }) => ({
id: "tenant_delete_1",
createdAt: new Date("2026-07-02T09:00:00.000Z"),
updatedAt: new Date("2026-07-02T09:00:00.000Z"),
cancelledAt: null,
cancelledByUserId: null,
hardDeleteStartedAt: null,
hardDeleteEndedAt: null,
errorMessage: null,
...data,
})),
findMany: vi.fn().mockResolvedValue([]),
updateMany: vi.fn().mockResolvedValue({ count: overrides.updateCount ?? 1 }),
},
avvAcceptance: {
create: vi.fn(async ({ data }) => ({
id: "avv_acceptance_1",
createdAt: new Date("2026-07-02T09:00:00.000Z"),
...data,
})),
findMany: vi.fn().mockResolvedValue([]),
},
subprocessor: {
findMany: vi.fn().mockResolvedValue([]),
},
};
}
describe("GDPR export jobs", () => {
it("creates user self exports with a 24h rate-limit query", async () => {
const client = createClient();
const now = new Date("2026-07-02T12:00:00.000Z");
await createGdprExportJob(
{
tenantId: "tenant_1",
requestedByUserId: "user_1",
targetUserId: "user_1",
scope: "user_self",
now,
},
client,
);
expect(client.gdprExportJob.count).toHaveBeenCalledWith({
where: {
tenantId: "tenant_1",
scope: "user_self",
targetUserId: "user_1",
createdAt: { gte: new Date("2026-07-01T12:00:00.000Z") },
},
});
expect(client.gdprExportJob.create).toHaveBeenCalledWith({
data: {
tenantId: "tenant_1",
requestedByUserId: "user_1",
targetUserId: "user_1",
scope: "user_self",
status: "queued",
progressPercent: 0,
},
});
});
it("rejects more than three user self exports per 24 hours", async () => {
const client = createClient({ exportCount: 3 });
await expect(
createGdprExportJob(
{
tenantId: "tenant_1",
requestedByUserId: "user_1",
scope: "user_self",
},
client,
),
).rejects.toThrow("GDPR user self export limit reached.");
expect(client.gdprExportJob.create).not.toHaveBeenCalled();
});
it("allows only one active tenant-full export", async () => {
const client = createClient({ exportCount: 1 });
await expect(
createGdprExportJob(
{
tenantId: "tenant_1",
requestedByUserId: "admin_1",
scope: "tenant_full",
},
client,
),
).rejects.toThrow("A tenant GDPR export is already active.");
expect(client.gdprExportJob.count).toHaveBeenCalledWith({
where: {
tenantId: "tenant_1",
scope: "tenant_full",
status: { in: ["queued", "processing"] },
},
});
});
it("marks exports ready with 72h artifact expiry", async () => {
const client = createClient();
const now = new Date("2026-07-02T12:00:00.000Z");
await markGdprExportReady(
{
id: "gdpr_export_1",
tenantId: "tenant_1",
resultBucket: "exports",
resultKey: "tenant_1/gdpr.zip",
resultSizeBytes: 1234,
now,
},
client,
);
expect(client.gdprExportJob.updateMany).toHaveBeenCalledWith({
where: { id: "gdpr_export_1", tenantId: "tenant_1" },
data: {
status: "ready",
progressPercent: 100,
resultFileId: null,
resultBucket: "exports",
resultKey: "tenant_1/gdpr.zip",
resultSizeBytes: 1234,
resultExpiresAt: new Date(now.getTime() + GDPR_EXPORT_URL_TTL_HOURS * 60 * 60 * 1_000),
processingFinishedAt: now,
errorMessage: null,
},
});
});
it("uses tenant-scoped updateMany for lifecycle transitions", async () => {
const client = createClient();
await markGdprExportProcessing({ id: "gdpr_export_1", tenantId: "tenant_1", now: new Date("2026-07-02T12:00:00.000Z") }, client);
await markGdprExportFailed({ id: "gdpr_export_1", tenantId: "tenant_1", errorMessage: "token=secret-value" }, client);
await markGdprExportDownloaded({ id: "gdpr_export_1", tenantId: "tenant_1", now: new Date("2026-07-02T13:00:00.000Z") }, client);
const calls = vi.mocked(client.gdprExportJob.updateMany).mock.calls;
expect(calls[0]?.[0].where).toEqual({ id: "gdpr_export_1", tenantId: "tenant_1" });
expect(calls[1]?.[0].data.errorMessage).toBe("token=[REDACTED]");
expect(calls[2]?.[0]).toEqual({
where: {
id: "gdpr_export_1",
tenantId: "tenant_1",
status: "ready",
resultExpiresAt: { gt: new Date("2026-07-02T13:00:00.000Z") },
},
data: {
downloadCount: { increment: 1 },
lastDownloadedAt: new Date("2026-07-02T13:00:00.000Z"),
},
});
});
});
describe("tenant deletion and AVV", () => {
it("creates tenant deletion requests with slug confirmation and 30-day grace", async () => {
const client = createClient();
const now = new Date("2026-07-02T12:00:00.000Z");
await createTenantDeletionRequest(
{
tenantId: "tenant_1",
requestedByUserId: "admin_1",
tenantSlug: "acme",
slugConfirmPhrase: "acme",
reason: "voluntary",
now,
},
client,
);
expect(client.tenantDeletionRequest.create).toHaveBeenCalledWith({
data: {
tenantId: "tenant_1",
requestedByUserId: "admin_1",
reason: "voluntary",
reasonText: null,
slugConfirmPhrase: "acme",
status: "pending",
gracePeriodEndsAt: new Date(now.getTime() + GDPR_TENANT_DELETION_GRACE_DAYS * 24 * 60 * 60 * 1_000),
},
});
});
it("rejects tenant deletion when the confirmation phrase differs from slug", async () => {
const client = createClient();
await expect(
createTenantDeletionRequest(
{
tenantId: "tenant_1",
requestedByUserId: "admin_1",
tenantSlug: "acme",
slugConfirmPhrase: "wrong",
reason: "voluntary",
},
client,
),
).rejects.toThrow("Tenant deletion confirmation phrase does not match the tenant slug.");
expect(client.tenantDeletionRequest.create).not.toHaveBeenCalled();
});
it("cancels only pending tenant deletion requests", async () => {
const client = createClient();
const now = new Date("2026-07-03T12:00:00.000Z");
await cancelTenantDeletionRequest({ id: "tenant_delete_1", tenantId: "tenant_1", cancelledByUserId: "admin_1", now }, client);
expect(client.tenantDeletionRequest.updateMany).toHaveBeenCalledWith({
where: {
id: "tenant_delete_1",
tenantId: "tenant_1",
status: "pending",
},
data: {
status: "cancelled",
cancelledAt: now,
cancelledByUserId: "admin_1",
},
});
});
it("lists due deletion requests for the hard-delete cron", async () => {
const client = createClient();
const now = new Date("2026-08-02T12:00:00.000Z");
await listDueTenantDeletionRequests({ now, take: 25 }, client);
expect(client.tenantDeletionRequest.findMany).toHaveBeenCalledWith({
where: {
status: "pending",
gracePeriodEndsAt: { lte: now },
},
orderBy: [{ gracePeriodEndsAt: "asc" }, { id: "asc" }],
take: 25,
});
});
it("records AVV acceptance history with request metadata", async () => {
const client = createClient();
await acceptAvv(
{
tenantId: "tenant_1",
acceptedByUserId: "admin_1",
avvVersion: "2026-07-02",
signerName: "Ada Lovelace",
signerRole: "Owner",
acceptedIp: "203.0.113.5",
acceptedUserAgent: "Test browser",
},
client,
);
expect(client.avvAcceptance.create).toHaveBeenCalledWith({
data: {
tenantId: "tenant_1",
acceptedByUserId: "admin_1",
acceptedIp: "203.0.113.5",
acceptedUserAgent: "Test browser",
avvVersion: "2026-07-02",
pdfFileId: null,
signerName: "Ada Lovelace",
signerRole: "Owner",
},
});
});
});