180 lines
6.0 KiB
TypeScript
180 lines
6.0 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
tx: {
|
|
projekt: { findFirst: vi.fn() },
|
|
projektMitglied: { findFirst: vi.fn() },
|
|
fileEntityLink: { findMany: vi.fn() },
|
|
file: { findFirst: vi.fn() },
|
|
projektChecklist: { findFirst: vi.fn() },
|
|
projektChecklistItem: { findFirst: vi.fn(), create: vi.fn(), update: vi.fn() },
|
|
acceptanceFormTemplate: { findFirst: vi.fn(), update: vi.fn() },
|
|
tenantUser: { findFirst: vi.fn() },
|
|
rechnung: { create: vi.fn() },
|
|
hiveNumberSeq: { update: vi.fn() },
|
|
$executeRaw: vi.fn(),
|
|
$queryRaw: vi.fn(),
|
|
},
|
|
logActivity: 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: mocks.logActivity }));
|
|
|
|
import { createProjectInvoice, upsertAcceptanceFormTemplate, upsertProjectChecklistItem, type AuftragVisibilityContext } from "./auftragService.server";
|
|
|
|
const TENANT_ID = "tenant-a";
|
|
const PROJECT_ID = "project-a";
|
|
|
|
function context(extraPermissions: readonly string[] = []): AuftragVisibilityContext {
|
|
return {
|
|
userId: "actor-a",
|
|
permissions: ["projects.read_all", ...extraPermissions],
|
|
roleIds: ["role-a"],
|
|
groupIds: ["group-a"],
|
|
};
|
|
}
|
|
|
|
describe("order file-reference authorization", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.tx.projekt.findFirst.mockResolvedValue({
|
|
id: PROJECT_ID,
|
|
tenantId: TENANT_ID,
|
|
projektleiterId: null,
|
|
zustaendigePersonId: null,
|
|
});
|
|
mocks.tx.fileEntityLink.findMany.mockResolvedValue([]);
|
|
mocks.tx.projektChecklist.findFirst.mockResolvedValue({ id: "checklist-a", tenantId: TENANT_ID, projektId: PROJECT_ID });
|
|
mocks.tx.projektChecklistItem.create.mockResolvedValue({ id: "item-a", tenantId: TENANT_ID, checklistId: "checklist-a" });
|
|
});
|
|
|
|
it("rejects an invoice file id when the actor has no Files read permission", async () => {
|
|
mocks.tx.file.findFirst.mockResolvedValueOnce(null);
|
|
|
|
await expect(
|
|
createProjectInvoice({
|
|
tenantId: TENANT_ID,
|
|
projectId: PROJECT_ID,
|
|
ctx: context(),
|
|
createdByUserId: "actor-a",
|
|
betrag: "1250.00",
|
|
dateiId: "guessed-file",
|
|
}),
|
|
).rejects.toMatchObject({ status: 404 });
|
|
|
|
expect(mocks.tx.file.findFirst).toHaveBeenCalledWith({
|
|
where: { id: { in: [] }, tenantId: TENANT_ID, archivedAt: null },
|
|
select: { id: true },
|
|
});
|
|
expect(mocks.tx.$executeRaw).not.toHaveBeenCalled();
|
|
expect(mocks.tx.rechnung.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps invoice file lookup tenant-scoped for a tenant-wide Files reader", async () => {
|
|
mocks.tx.file.findFirst.mockResolvedValueOnce(null);
|
|
|
|
await expect(
|
|
createProjectInvoice({
|
|
tenantId: TENANT_ID,
|
|
projectId: PROJECT_ID,
|
|
ctx: context(["files.read_all"]),
|
|
createdByUserId: "actor-a",
|
|
betrag: "1250.00",
|
|
dateiId: "file-from-tenant-b",
|
|
}),
|
|
).rejects.toMatchObject({ status: 404 });
|
|
|
|
expect(mocks.tx.file.findFirst).toHaveBeenCalledWith({
|
|
where: {
|
|
id: "file-from-tenant-b",
|
|
tenantId: TENANT_ID,
|
|
archivedAt: null,
|
|
links: { none: { tenantId: TENANT_ID, entityType: "user", entityId: { not: "actor-a" } } },
|
|
},
|
|
select: { id: true },
|
|
});
|
|
expect(mocks.tx.rechnung.create).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects a checklist photo id outside the actor's authoritative Files scope", async () => {
|
|
mocks.tx.file.findFirst.mockResolvedValueOnce(null);
|
|
|
|
await expect(
|
|
upsertProjectChecklistItem({
|
|
tenantId: TENANT_ID,
|
|
checklistId: "checklist-a",
|
|
ctx: context(),
|
|
titel: "Photo evidence",
|
|
fotoFileId: "guessed-photo",
|
|
}),
|
|
).rejects.toMatchObject({ status: 404 });
|
|
|
|
expect(mocks.tx.file.findFirst).toHaveBeenCalledWith({
|
|
where: { id: { in: [] }, tenantId: TENANT_ID, archivedAt: null },
|
|
select: { id: true },
|
|
});
|
|
expect(mocks.tx.projektChecklistItem.create).not.toHaveBeenCalled();
|
|
expect(mocks.tx.projektChecklistItem.update).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("accepts a checklist photo only through the current actor's own-file scope", async () => {
|
|
mocks.tx.file.findFirst.mockResolvedValueOnce({ id: "own-photo" });
|
|
|
|
await upsertProjectChecklistItem({
|
|
tenantId: TENANT_ID,
|
|
checklistId: "checklist-a",
|
|
ctx: context(["files.read_own"]),
|
|
titel: "Own photo evidence",
|
|
fotoFileId: "own-photo",
|
|
});
|
|
|
|
expect(mocks.tx.file.findFirst).toHaveBeenCalledWith({
|
|
where: {
|
|
id: "own-photo",
|
|
tenantId: TENANT_ID,
|
|
archivedAt: null,
|
|
AND: [
|
|
{
|
|
OR: [
|
|
{ createdByUserId: "actor-a" },
|
|
{ assignedToUserId: "actor-a" },
|
|
{ links: { some: { tenantId: TENANT_ID, entityType: "user", entityId: "actor-a" } } },
|
|
],
|
|
},
|
|
{ links: { none: { tenantId: TENANT_ID, entityType: "user", entityId: { not: "actor-a" } } } },
|
|
],
|
|
},
|
|
select: { id: true },
|
|
});
|
|
expect(mocks.tx.projektChecklistItem.create).toHaveBeenCalledWith({
|
|
data: expect.objectContaining({
|
|
tenantId: TENANT_ID,
|
|
checklistId: "checklist-a",
|
|
fotoFileId: "own-photo",
|
|
}),
|
|
});
|
|
});
|
|
|
|
it("rejects an acceptance template id outside the current tenant before updating", async () => {
|
|
mocks.tx.acceptanceFormTemplate.findFirst.mockResolvedValueOnce(null);
|
|
|
|
await expect(
|
|
upsertAcceptanceFormTemplate({
|
|
tenantId: TENANT_ID,
|
|
templateId: "template-from-tenant-b",
|
|
name: "Hidden template",
|
|
}),
|
|
).rejects.toMatchObject({ status: 404 });
|
|
|
|
expect(mocks.tx.acceptanceFormTemplate.findFirst).toHaveBeenCalledWith({
|
|
where: { id: "template-from-tenant-b", tenantId: TENANT_ID, archivedAt: null },
|
|
select: { id: true },
|
|
});
|
|
expect(mocks.tx.acceptanceFormTemplate.update).not.toHaveBeenCalled();
|
|
});
|
|
});
|