Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,190 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
type DsgvoCliMocks = {
|
||||
db: {
|
||||
user: {
|
||||
findUnique: ReturnType<typeof vi.fn>;
|
||||
update: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
};
|
||||
createAuditEvent: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
function getDsgvoCliMocks(): DsgvoCliMocks {
|
||||
const global = globalThis as typeof globalThis & { __dsgvoCliMocks__?: DsgvoCliMocks };
|
||||
global.__dsgvoCliMocks__ ??= {
|
||||
db: {
|
||||
user: {
|
||||
findUnique: vi.fn(),
|
||||
update: vi.fn(),
|
||||
},
|
||||
},
|
||||
createAuditEvent: vi.fn(),
|
||||
};
|
||||
return global.__dsgvoCliMocks__;
|
||||
}
|
||||
|
||||
vi.mock("@toolctrl/db/client.server", () => ({
|
||||
getDbClient: () => getDsgvoCliMocks().db,
|
||||
}));
|
||||
vi.mock("@toolctrl/db/audit-events.server", () => ({
|
||||
createAuditEvent: getDsgvoCliMocks().createAuditEvent,
|
||||
}));
|
||||
vi.mock("consola", () => ({
|
||||
default: {
|
||||
error: vi.fn(),
|
||||
success: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
import consola from "consola";
|
||||
import { dsgvoDeleteCommand } from "./delete.js";
|
||||
import { dsgvoReactivateCommand } from "./reactivate.js";
|
||||
|
||||
const mocks = getDsgvoCliMocks();
|
||||
const consoleMock = vi.mocked(consola);
|
||||
const now = new Date("2026-05-25T12:00:00.000Z");
|
||||
|
||||
describe("dsgvo CLI commands", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(now);
|
||||
vi.clearAllMocks();
|
||||
process.exitCode = undefined;
|
||||
mocks.db.user.update.mockResolvedValue({ id: "user_1" });
|
||||
mocks.createAuditEvent.mockResolvedValue({ id: "audit_1" });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
process.exitCode = undefined;
|
||||
});
|
||||
|
||||
it("rejects immediate deletion without a sufficiently descriptive reason before DB writes", async () => {
|
||||
await dsgvoDeleteCommand.run?.({
|
||||
args: { user: "user_1", immediate: true, reason: "short" },
|
||||
} as never);
|
||||
|
||||
expect(consoleMock.error).toHaveBeenCalledWith("--reason is required (>=10 chars) when --immediate is used.");
|
||||
expect(process.exitCode).toBe(2);
|
||||
expect(mocks.db.user.update).not.toHaveBeenCalled();
|
||||
expect(mocks.createAuditEvent).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("schedules deletion with grace period, tenant, actor, and audit metadata", async () => {
|
||||
await dsgvoDeleteCommand.run?.({
|
||||
args: { user: "user_1", tenant: "tenant_1", actor: "admin_1" },
|
||||
} as never);
|
||||
|
||||
const scheduledDeletionAt = new Date("2026-06-24T12:00:00.000Z");
|
||||
expect(mocks.db.user.update).toHaveBeenCalledWith({
|
||||
where: { id: "user_1" },
|
||||
data: {
|
||||
pendingDeletion: true,
|
||||
deletionRequestedAt: now,
|
||||
scheduledDeletionAt,
|
||||
deletionRequestId: expect.any(String),
|
||||
},
|
||||
});
|
||||
const requestId = mocks.db.user.update.mock.calls[0]?.[0].data.deletionRequestId;
|
||||
expect(mocks.createAuditEvent).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
tenantId: "tenant_1",
|
||||
actorId: "admin_1",
|
||||
action: "REQUEST_USER_DELETION",
|
||||
targetId: "user_1",
|
||||
metadata: expect.objectContaining({
|
||||
requestId,
|
||||
immediate: false,
|
||||
reason: null,
|
||||
scheduledDeletionAt: scheduledDeletionAt.toISOString(),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(consoleMock.success).toHaveBeenCalledWith(
|
||||
`User user_1 scheduled for deletion on ${scheduledDeletionAt.toISOString()}. requestId=${requestId}`,
|
||||
);
|
||||
});
|
||||
|
||||
it("marks immediate deletion using default actor and nullable tenant audit fields", async () => {
|
||||
await dsgvoDeleteCommand.run?.({
|
||||
args: { user: "user_1", immediate: true, reason: "legal erasure request" },
|
||||
} as never);
|
||||
|
||||
expect(mocks.db.user.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
scheduledDeletionAt: now,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(mocks.createAuditEvent).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
tenantId: null,
|
||||
actorId: "cli-operator",
|
||||
metadata: expect.objectContaining({
|
||||
immediate: true,
|
||||
reason: "legal erasure request",
|
||||
scheduledDeletionAt: now.toISOString(),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(consoleMock.success).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/^User user_1 marked for immediate deletion \(next cron pass\)\. requestId=.+$/u),
|
||||
);
|
||||
});
|
||||
|
||||
it("reports missing or non-pending users during reactivation without writing updates", async () => {
|
||||
mocks.db.user.findUnique.mockResolvedValueOnce(null);
|
||||
|
||||
await dsgvoReactivateCommand.run?.({ args: { user: "missing" } } as never);
|
||||
|
||||
expect(consoleMock.error).toHaveBeenCalledWith("User missing not found.");
|
||||
expect(process.exitCode).toBe(1);
|
||||
expect(mocks.db.user.update).not.toHaveBeenCalled();
|
||||
|
||||
process.exitCode = undefined;
|
||||
mocks.db.user.findUnique.mockResolvedValueOnce({ pendingDeletion: false });
|
||||
|
||||
await dsgvoReactivateCommand.run?.({ args: { user: "user_1" } } as never);
|
||||
|
||||
expect(consoleMock.warn).toHaveBeenCalledWith("User user_1 is not pendingDeletion; nothing to do.");
|
||||
expect(process.exitCode).toBe(1);
|
||||
expect(mocks.db.user.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reactivates a pending user and records cancellation audit metadata", async () => {
|
||||
mocks.db.user.findUnique.mockResolvedValueOnce({ pendingDeletion: true });
|
||||
|
||||
await dsgvoReactivateCommand.run?.({
|
||||
args: { user: "user_1", tenant: "tenant_1", actor: "admin_1" },
|
||||
} as never);
|
||||
|
||||
expect(mocks.db.user.findUnique).toHaveBeenCalledWith({
|
||||
where: { id: "user_1" },
|
||||
select: { pendingDeletion: true },
|
||||
});
|
||||
expect(mocks.db.user.update).toHaveBeenCalledWith({
|
||||
where: { id: "user_1" },
|
||||
data: {
|
||||
pendingDeletion: false,
|
||||
deletionRequestedAt: null,
|
||||
scheduledDeletionAt: null,
|
||||
deletionRequestId: null,
|
||||
},
|
||||
});
|
||||
expect(mocks.createAuditEvent).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
tenantId: "tenant_1",
|
||||
actorId: "admin_1",
|
||||
action: "CANCEL_USER_DELETION",
|
||||
targetId: "user_1",
|
||||
metadata: { requestId: expect.any(String), intent: "reactivate", source: "cli" },
|
||||
}),
|
||||
);
|
||||
expect(consoleMock.success).toHaveBeenCalledWith(
|
||||
expect.stringMatching(/^User user_1 reactivated\. requestId=.+$/u),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user