import { NextRequest } from "next/server"; import { beforeEach, describe, expect, it, vi } from "vitest"; const findUniqueMock = vi.fn(); const updateMock = vi.fn(); const upsertMock = vi.fn(); const getSessionUserMock = vi.fn(); const createSessionMock = vi.fn(); const setCookieMock = vi.fn(); vi.mock("../../lib/server/db", () => ({ prisma: { ruleStakeholder: { findUnique: (...args: unknown[]) => findUniqueMock(...args), update: (...args: unknown[]) => updateMock(...args), }, user: { upsert: (...args: unknown[]) => upsertMock(...args), }, }, })); vi.mock("../../lib/server/env", () => ({ isDatabaseConfigured: () => true, getSessionPepper: () => "pepper", })); vi.mock("../../lib/server/hash", () => ({ hashSessionToken: (t: string) => `h-${t}`, })); vi.mock("../../lib/server/session", () => ({ getSessionUser: () => getSessionUserMock(), createSessionForUser: (...args: unknown[]) => createSessionMock(...args), setSessionCookie: (...args: unknown[]) => setCookieMock(...args), })); import { GET, POST } from "../../app/api/invites/rule-stakeholder/verify/route"; beforeEach(() => { findUniqueMock.mockReset(); updateMock.mockReset(); upsertMock.mockReset(); getSessionUserMock.mockReset(); createSessionMock.mockReset(); setCookieMock.mockReset(); }); describe("GET /api/invites/rule-stakeholder/verify", () => { it("redirects a valid token to the confirm page without spending it", async () => { getSessionUserMock.mockResolvedValue(null); findUniqueMock.mockResolvedValue({ id: "st1", email: "inv@example.com", ruleId: "rule-1", inviteExpiresAt: new Date(Date.now() + 60_000), acceptedAt: null, }); const token = "x".repeat(12); const res = await GET( new NextRequest( `https://x.test/api/invites/rule-stakeholder/verify?token=${token}`, ), ); expect(res.status).toBe(307); expect(res.headers.get("location")).toContain("/invite/confirm"); expect(res.headers.get("location")).toContain(`token=${token}`); expect(upsertMock).not.toHaveBeenCalled(); expect(updateMock).not.toHaveBeenCalled(); expect(setCookieMock).not.toHaveBeenCalled(); }); it("redirects to login when another user is already signed in", async () => { getSessionUserMock.mockResolvedValue({ id: "u-other", email: "other@example.com", }); findUniqueMock.mockResolvedValue({ id: "st1", email: "inv@example.com", ruleId: "rule-1", inviteExpiresAt: new Date(Date.now() + 60_000), acceptedAt: null, }); const res = await GET( new NextRequest( `https://x.test/api/invites/rule-stakeholder/verify?token=${"y".repeat(12)}`, ), ); expect(res.headers.get("location")).toContain( "error=stakeholder_wrong_account", ); expect(upsertMock).not.toHaveBeenCalled(); }); it("redirects expired tokens to the expired invitation page", async () => { getSessionUserMock.mockResolvedValue(null); findUniqueMock.mockResolvedValue({ id: "st1", email: "inv@example.com", ruleId: "rule-1", inviteExpiresAt: new Date(Date.now() - 60_000), acceptedAt: null, }); const token = "z".repeat(12); const res = await GET( new NextRequest( `https://x.test/api/invites/rule-stakeholder/verify?token=${token}`, ), ); expect(res.headers.get("location")).toContain("/invite/expired"); expect(res.headers.get("location")).toContain(`token=${token}`); expect(upsertMock).not.toHaveBeenCalled(); }); it("keeps copy=1 on the confirm page after a valid token", async () => { getSessionUserMock.mockResolvedValue(null); findUniqueMock.mockResolvedValue({ id: "st1", email: "inv@example.com", ruleId: "rule-1", inviteExpiresAt: new Date(Date.now() + 60_000), acceptedAt: null, }); const res = await GET( new NextRequest( `https://x.test/api/invites/rule-stakeholder/verify?token=${"x".repeat(12)}©=1`, ), ); expect(res.headers.get("location")).toContain("/invite/confirm"); expect(res.headers.get("location")).toContain("copy=1"); expect(setCookieMock).not.toHaveBeenCalled(); }); it("opens the Rule when a used link is opened by the signed-in invitee", async () => { getSessionUserMock.mockResolvedValue({ id: "u1", email: "inv@example.com", }); findUniqueMock.mockResolvedValue({ id: "st1", email: "inv@example.com", ruleId: "rule-1", inviteExpiresAt: null, acceptedAt: new Date(), }); const res = await GET( new NextRequest( `https://x.test/api/invites/rule-stakeholder/verify?token=${"x".repeat(12)}`, ), ); expect(res.headers.get("location")).toContain("/invite/rule-1"); expect(upsertMock).not.toHaveBeenCalled(); expect(setCookieMock).not.toHaveBeenCalled(); }); it("sends a used link to the expired screen when the invitee is signed out", async () => { getSessionUserMock.mockResolvedValue(null); findUniqueMock.mockResolvedValue({ id: "st1", email: "inv@example.com", ruleId: "rule-1", inviteExpiresAt: null, acceptedAt: new Date(), }); const token = "z".repeat(12); const res = await GET( new NextRequest( `https://x.test/api/invites/rule-stakeholder/verify?token=${token}`, ), ); expect(res.headers.get("location")).toContain("/invite/expired"); expect(res.headers.get("location")).toContain(`token=${token}`); expect(upsertMock).not.toHaveBeenCalled(); }); }); describe("POST /api/invites/rule-stakeholder/verify", () => { it("spends a valid token, keeps the hash, and redirects to the Rule", async () => { getSessionUserMock.mockResolvedValue(null); findUniqueMock.mockResolvedValue({ id: "st1", email: "inv@example.com", ruleId: "rule-1", inviteExpiresAt: new Date(Date.now() + 60_000), acceptedAt: null, }); upsertMock.mockResolvedValue({ id: "u1", email: "inv@example.com" }); updateMock.mockResolvedValue({}); createSessionMock.mockResolvedValue({ token: "sess", expiresAt: new Date(), }); const res = await POST( new NextRequest( `https://x.test/api/invites/rule-stakeholder/verify?token=${"x".repeat(12)}`, { method: "POST" }, ), ); expect(res.status).toBe(303); expect(res.headers.get("location")).toContain("/invite/rule-1"); expect(setCookieMock).toHaveBeenCalled(); expect(updateMock).toHaveBeenCalledWith({ where: { id: "st1" }, data: { userId: "u1", acceptedAt: expect.any(Date), }, }); }); it("keeps copy=1 on the invited view after confirm", async () => { getSessionUserMock.mockResolvedValue(null); findUniqueMock.mockResolvedValue({ id: "st1", email: "inv@example.com", ruleId: "rule-1", inviteExpiresAt: new Date(Date.now() + 60_000), acceptedAt: null, }); upsertMock.mockResolvedValue({ id: "u1", email: "inv@example.com" }); updateMock.mockResolvedValue({}); createSessionMock.mockResolvedValue({ token: "sess", expiresAt: new Date(), }); const res = await POST( new NextRequest( `https://x.test/api/invites/rule-stakeholder/verify?token=${"x".repeat(12)}©=1`, { method: "POST" }, ), ); expect(res.headers.get("location")).toContain("/invite/rule-1"); expect(res.headers.get("location")).toContain("copy=1"); }); });