import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; const mocks = vi.hoisted(() => ({ getToken: vi.fn() })); vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken })); import { GET as refundPhoto } from "@/app/api/refunds/[uuid]/photos/[index]/route"; import { GET as warrantyPhoto } from "@/app/api/warranty-claims/[uuid]/photos/[index]/route"; import { GET as reviewImage } from "@/app/api/product-reviews/[refno]/images/[index]/route"; import { GET as webOrderInvoice } from "@/app/api/web-orders/[uuid]/invoice/route"; import { NextRequest } from "next/server"; // portal-path-segment-encoding: these routes fetch mobile-api directly (bytes out), so they do not get // callMobileApi's dot-segment guard for free. encodeURIComponent("..") is still "..", which the URL parser // would climb, so a dot-segment id must be refused before any upstream call. const req = new Request("http://localhost") as never; const ROUTES = [ ["refunds photo", (id: string) => refundPhoto(req, { params: Promise.resolve({ uuid: id, index: "0" }) })], ["warranty photo", (id: string) => warrantyPhoto(req, { params: Promise.resolve({ uuid: id, index: "0" }) })], ["review image", (id: string) => reviewImage(req, { params: Promise.resolve({ refno: id, index: "0" }) })], [ "web-order invoice", (id: string) => webOrderInvoice(new NextRequest("http://localhost/api/web-orders/x/invoice"), { params: Promise.resolve({ uuid: id }) }), ], ] as const; const fetchMock = vi.fn(); beforeEach(() => { mocks.getToken.mockReset().mockResolvedValue("jwt"); fetchMock.mockReset(); vi.stubGlobal("fetch", fetchMock); process.env.MOBILE_API_BASE_URL = "http://mobile"; process.env.MOBILE_API_KEY = "k"; }); afterEach(() => vi.unstubAllGlobals()); describe.each(ROUTES)("%s refuses a dot-segment id", (_name, call) => { it.each([".", "..", "..;x"])("refuses %s without calling upstream", async id => { expect((await call(id)).status).toBe(400); expect(fetchMock).not.toHaveBeenCalled(); }); });