From e628979d5ceed151e727770d0a9ca09afff99916 Mon Sep 17 00:00:00 2001 From: Volodymyr Borysenko Date: Wed, 7 Oct 2026 09:42:03 -0700 Subject: [PATCH] fix: check startup and search ownership when saving a person Fixes #40 --- src/app/api/people/save/route.ts | 11 +++++ tests/people-save.test.ts | 71 ++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tests/people-save.test.ts diff --git a/src/app/api/people/save/route.ts b/src/app/api/people/save/route.ts index ead3158..bde2f3b 100644 --- a/src/app/api/people/save/route.ts +++ b/src/app/api/people/save/route.ts @@ -26,6 +26,17 @@ export async function POST(request: Request) { select: { id: true, fullName: true }, }); if (!person) return badRequest("Person not found."); + const { startupId, searchRunId, searchResultId } = body.data; + const [startup, searchRun, searchResult] = await Promise.all([ + startupId ? prisma.startupProfile.findFirst({ where: { id: startupId, userId: user.id }, select: { id: true } }) : null, + searchRunId ? prisma.peopleSearchRun.findFirst({ where: { id: searchRunId, userId: user.id }, select: { id: true } }) : null, + searchResultId + ? prisma.peopleSearchResult.findFirst({ where: { id: searchResultId, userId: user.id }, select: { id: true } }) + : null, + ]); + if (startupId && !startup) return badRequest("Startup not found."); + if (searchRunId && !searchRun) return badRequest("Search run not found."); + if (searchResultId && !searchResult) return badRequest("Search result not found."); const list = body.data.listId ? await prisma.savedPeopleList.findFirst({ where: { id: body.data.listId, userId: user.id } }) diff --git a/tests/people-save.test.ts b/tests/people-save.test.ts new file mode 100644 index 0000000..cc71374 --- /dev/null +++ b/tests/people-save.test.ts @@ -0,0 +1,71 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const auth = vi.hoisted(() => ({ requireCurrentUser: vi.fn() })); +const db = vi.hoisted(() => ({ prisma: {} as Record })); + +vi.mock("@/lib/auth/current-user", () => auth); +vi.mock("@/lib/prisma", () => db); + +const user = { id: "user-1", email: "partner@example.com", name: "Partner", imageUrl: null, role: "partner" }; + +/** findFirst that only returns rows owned by user-1, like a userId-scoped query would. */ +function ownedBy(ids: string[]) { + return vi.fn(async ({ where }: { where: { id: string; userId: string } }) => + where.userId === "user-1" && ids.includes(where.id) ? { id: where.id } : null, + ); +} + +const listUpsert = vi.fn(); +const savedUpsert = vi.fn(); + +beforeEach(() => { + auth.requireCurrentUser.mockReset(); + auth.requireCurrentUser.mockResolvedValue(user); + listUpsert.mockReset().mockResolvedValue({ id: "list-1" }); + savedUpsert.mockReset().mockResolvedValue({ id: "saved-1" }); + for (const key of Object.keys(db.prisma)) delete db.prisma[key]; + Object.assign(db.prisma, { + discoveredPerson: { findFirst: vi.fn().mockResolvedValue({ id: "person-1", fullName: "Dana Lee" }) }, + startupProfile: { findFirst: ownedBy(["startup-1"]) }, + peopleSearchRun: { findFirst: ownedBy(["run-1"]) }, + peopleSearchResult: { findFirst: ownedBy(["result-1"]) }, + savedPeopleList: { upsert: listUpsert, findFirst: vi.fn() }, + savedPerson: { upsert: savedUpsert }, + auditEvent: { create: vi.fn().mockResolvedValue({}) }, + }); +}); + +async function save(body: Record) { + const { POST } = await import("@/app/api/people/save/route"); + return POST( + new Request("http://localhost/api/people/save", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ personId: "person-1", ...body }), + }), + ); +} + +describe("people save route", () => { + it.each([ + [{ startupId: "other-users-startup" }], + [{ searchRunId: "other-users-run" }], + [{ searchResultId: "other-users-result" }], + ])("rejects ids the user does not own: %o", async (body) => { + const response = await save(body); + + expect(response.status).toBe(400); + expect(listUpsert).not.toHaveBeenCalled(); + expect(savedUpsert).not.toHaveBeenCalled(); + }); + + it("saves the person when every linked id is owned", async () => { + const response = await save({ startupId: "startup-1", searchRunId: "run-1", searchResultId: "result-1" }); + + expect(response.status).toBe(200); + expect(listUpsert).toHaveBeenCalledWith(expect.objectContaining({ create: expect.objectContaining({ startupId: "startup-1" }) })); + expect(savedUpsert).toHaveBeenCalledWith( + expect.objectContaining({ create: expect.objectContaining({ searchRunId: "run-1", searchResultId: "result-1" }) }), + ); + }); +});