Files

86 lines
3.0 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
tx: {
pipeline: { findFirst: vi.fn() },
pipelineStage: { findFirst: vi.fn() },
acceptanceFormTemplate: { findFirst: vi.fn() },
auftragPipelineStageConfig: { upsert: vi.fn() },
projectTemplate: { findFirst: vi.fn(), update: vi.fn(), upsert: vi.fn() },
},
}));
vi.mock("~/utils/db/.server/tenantRls.server", () => ({
withTenantRLS: async (_tenantId: string, callback: (tx: typeof mocks.tx) => Promise<unknown>) => callback(mocks.tx),
}));
vi.mock("@hivegrid/activity", () => ({ logActivity: vi.fn() }));
import { upsertAuftragPipelineStageConfig, upsertProjectTemplate } from "./auftragService.server";
describe("order/project template parent ownership boundaries", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.tx.pipeline.findFirst.mockResolvedValue({ id: "pipeline-a" });
mocks.tx.pipelineStage.findFirst.mockResolvedValue({ id: "stage-a" });
mocks.tx.acceptanceFormTemplate.findFirst.mockResolvedValue({ id: "acceptance-a" });
mocks.tx.projectTemplate.findFirst.mockResolvedValue({ id: "template-a" });
mocks.tx.projectTemplate.update.mockResolvedValue({ id: "template-a" });
});
it("rejects a cross-tenant acceptance template on stage-config upsert", async () => {
mocks.tx.acceptanceFormTemplate.findFirst.mockResolvedValueOnce(null);
await expect(
upsertAuftragPipelineStageConfig({
tenantId: "tenant-a",
pipelineId: "pipeline-a",
stageId: "stage-a",
acceptanceFormTemplateId: "acceptance-from-tenant-b",
}),
).rejects.toThrow();
expect(mocks.tx.auftragPipelineStageConfig.upsert).not.toHaveBeenCalled();
});
it("rejects a project template stage outside its tenant pipeline", async () => {
mocks.tx.pipelineStage.findFirst.mockResolvedValueOnce(null);
await expect(
upsertProjectTemplate({
tenantId: "tenant-a",
name: "Foreign defaults",
defaultPipelineId: "pipeline-a",
defaultStageId: "stage-from-tenant-b",
}),
).rejects.toThrow();
expect(mocks.tx.projectTemplate.upsert).not.toHaveBeenCalled();
});
it("rejects a foreign template target and tenant-scopes a valid update", async () => {
mocks.tx.projectTemplate.findFirst.mockResolvedValueOnce(null);
await expect(
upsertProjectTemplate({
tenantId: "tenant-a",
templateId: "template-from-tenant-b",
name: "Foreign target",
}),
).rejects.toThrow();
expect(mocks.tx.projectTemplate.update).not.toHaveBeenCalled();
await upsertProjectTemplate({ tenantId: "tenant-a", templateId: "template-a", name: "Safe update" });
expect(mocks.tx.projectTemplate.update).toHaveBeenCalledWith({
where: { id: "template-a", tenantId: "tenant-a" },
data: {
name: "Safe update",
defaultPipelineId: null,
defaultStageId: null,
defaultMembers: [],
defaultTasks: [],
customFields: {},
archivedAt: null,
},
});
});
});