Files

62 lines
2.8 KiB
TypeScript

// WF-P8 S02-N — the note reader gate.
//
// This tests `noteReadScopeWhere` as a pure predicate rather than mocking the
// database: a mock that answers every query would hide exactly the bug this
// gate exists to prevent (AGENTS.md §RLS — "Tests, die den DB-Client mocken,
// beweisen nichts"). Here we evaluate the produced `where` fragment against
// concrete note rows, which is the same decision Postgres makes.
import { describe, expect, it } from "vitest";
import { noteReadScopeWhere, type ProjektNotizContext } from "./projektNotizService.server";
type NoteRow = { sichtbarkeit: "alle" | "innendienst" | "nur_ich"; autorUserId: string | null };
/** Evaluate the OR-fragment the service hands to Prisma against one row. */
function matches(where: ReturnType<typeof noteReadScopeWhere>, row: NoteRow): boolean {
return where.OR.some((clause) =>
Object.entries(clause).every(([key, value]) => row[key as keyof NoteRow] === value)
);
}
function ctx(overrides: Partial<ProjektNotizContext> = {}): ProjektNotizContext {
return { userId: "usr_a", permissions: [], canReadInternal: false, ...overrides } as ProjektNotizContext;
}
const publicNote: NoteRow = { sichtbarkeit: "alle", autorUserId: "usr_b" };
const internalNote: NoteRow = { sichtbarkeit: "innendienst", autorUserId: "usr_b" };
const ownPrivateNote: NoteRow = { sichtbarkeit: "nur_ich", autorUserId: "usr_a" };
const foreignPrivateNote: NoteRow = { sichtbarkeit: "nur_ich", autorUserId: "usr_b" };
describe("noteReadScopeWhere", () => {
it("lets anyone with project access read a public note", () => {
expect(matches(noteReadScopeWhere(ctx()), publicNote)).toBe(true);
});
it("hides an internal note without projects.read_internal_notes", () => {
expect(matches(noteReadScopeWhere(ctx()), internalNote)).toBe(false);
});
it("shows an internal note with the permission", () => {
expect(matches(noteReadScopeWhere(ctx({ canReadInternal: true })), internalNote)).toBe(true);
});
it("shows a private note only to its author", () => {
const where = noteReadScopeWhere(ctx());
expect(matches(where, ownPrivateNote)).toBe(true);
expect(matches(where, foreignPrivateNote)).toBe(false);
});
it("does not open foreign private notes to someone holding the internal permission", () => {
// "Nur ich" outranks the internal-notes permission; it is not a hierarchy.
expect(matches(noteReadScopeWhere(ctx({ canReadInternal: true })), foreignPrivateNote)).toBe(false);
});
it("matches no private note at all for an anonymous context", () => {
const where = noteReadScopeWhere(ctx({ userId: null as unknown as string }));
expect(matches(where, ownPrivateNote)).toBe(false);
expect(matches(where, foreignPrivateNote)).toBe(false);
// …but a public note stays readable.
expect(matches(where, publicNote)).toBe(true);
});
});