Files
vfxreview/lib/review-auth.test.ts
T
twotalesanimation 28e8c47907 feat(client-portal): let a reviewer edit and delete their own notes
Notes left through a review link were write-once. A client who mistyped a
timecode or changed their mind had no way to correct it and had to add a second
note contradicting the first, which the studio then had to reconcile by hand.

PATCH and DELETE on comment/[commentId] cover both. Only the reviewer's own
notes are touchable: the studio's comments and those of every other review
session are read-only through a link, whatever the holder sends. Ownership is
re-checked server-side on every write, so the canEdit flag now returned by the
versions route only decides whether to offer the controls and grants nothing.

Refusals are 404 rather than 403 throughout, since a link holder has no business
learning which comment ids exist outside their own notes.

Only the text is editable. The frame a note is pinned to is what the studio
works from, so moving it would silently repoint feedback someone may already
have actioned; a note about a different frame is a new note. Deleting a drawing
note also removes its strokes, mirroring deleteComment in actions/comments.ts,
and the page reloads the player afterwards so the frame does not keep showing
marks that no longer exist. Delete goes through a confirmation that shows the
note and says plainly that it disappears for the studio too.

Worth knowing before this reaches a show: everyone holding the same review link
is the same author. A ReviewSession with no email resolves to a token-derived
guest user, so one link sent to three people gives all three one identity and
any of them can now edit or delete the others' notes. That model is not new,
which is why their comments already showed as one name, but edit and delete is
the first feature where it grants real power rather than muddling attribution.
Per-reviewer links with email set are the fix if that is not wanted, and that is
a change to how links are issued rather than to this code. The scope is
documented on reviewClientEmail so the next reader meets it before it surprises
them.

getOrCreateClientUser was copy-pasted into the comment and approve routes with
the fallback-email expression spelled out in three places. It is one set of
helpers in review-auth.ts now, including findReviewUser, a read-only variant so
that merely opening a review link does not conjure a User row just to answer
"is this theirs?", and ownsComment, the single rule shared by the route that
offers the controls and the route that enforces them.

Nine tests cover that rule directly: internal users' notes stay read-only, a
session that has never posted owns nothing, and an authorless comment — one
whose User was deleted, since Comment.authorId is SetNull — cannot be claimed by
anyone. The routes themselves are not covered, so exercise edit and delete
against a real review link before handing one to a client.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-31 18:17:40 +02:00

67 lines
2.3 KiB
TypeScript

import { describe, it, expect, vi } from "vitest";
// Mock Prisma so the pure helpers can be tested without a database.
vi.mock("@/lib/db", () => ({ db: {} }));
import { reviewClientEmail, ownsComment } from "./review-auth";
import type { ReviewSession } from "@prisma/client";
const session = (email: string | null): ReviewSession =>
({ email, label: null } as ReviewSession);
describe("reviewClientEmail", () => {
it("uses the session's own email when it has one", () => {
expect(reviewClientEmail(session("dailies@client.com"), "tok123456789")).toBe(
"dailies@client.com"
);
});
it("falls back to a token-derived address when it has none", () => {
expect(reviewClientEmail(session(null), "tok123456789")).toBe(
"client+tok12345@review.external"
);
});
// Identity has to be stable across requests, or a reviewer would stop owning
// the notes they wrote a minute ago.
it("is stable for the same token", () => {
const a = reviewClientEmail(session(null), "tok123456789");
const b = reviewClientEmail(session(null), "tok123456789");
expect(a).toBe(b);
});
it("separates two different review links", () => {
expect(reviewClientEmail(session(null), "aaaaaaaaaaaa")).not.toBe(
reviewClientEmail(session(null), "bbbbbbbbbbbb")
);
});
});
describe("ownsComment", () => {
it("lets a reviewer edit their own note", () => {
expect(ownsComment({ authorId: "u1" }, "u1")).toBe(true);
});
it("refuses another author's note", () => {
expect(ownsComment({ authorId: "u2" }, "u1")).toBe(false);
});
// The studio's notes reach the portal in the same list; they must stay
// read-only there.
it("refuses a note written by an internal user", () => {
expect(ownsComment({ authorId: "supervisor-id" }, "client-id")).toBe(false);
});
it("refuses when the reviewer has never posted, so has no identity yet", () => {
expect(ownsComment({ authorId: "u1" }, undefined)).toBe(false);
expect(ownsComment({ authorId: "u1" }, null)).toBe(false);
});
// An authorless comment (its User row was deleted — Comment.authorId is
// SetNull) belongs to nobody and must not become claimable.
it("refuses an authorless note rather than letting it be claimed", () => {
expect(ownsComment({ authorId: null }, "u1")).toBe(false);
expect(ownsComment({ authorId: null }, null)).toBe(false);
});
});