import { NextRequest } from "next/server"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; vi.mock("@/shared/config", () => ({ CLAIM_NAME: "custom-auth-cookie", AUTH_USER_ID: "token-id" })); import { POST as fileAuthorityPOST } from "./route"; import { POST as embedPOST } from "../../flask-service/embed-documents/jobs/route"; const validJwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyLTEifQ.signature"; const forgedJwt = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJmb3JnZWQifQ.signature"; const file = { fileSn: "file-1", projectId: "root-1", clsfSystmSn: "leaf-1", useYn: "Y" }; const categories = [{ clsfSystmSn: "root-1", clsfSystmNm: "자료관", upClsfSystmSn: "0", children: [{ clsfSystmSn: "leaf-1", clsfSystmNm: "물 재이용", upClsfSystmSn: "root-1" }], }]; beforeEach(() => { vi.stubEnv("DC_SERVICE_API_URL", "https://dc-service.invalid"); vi.stubEnv("FLASK_API_URL", "https://dc-rag.invalid"); }); afterEach(() => { vi.unstubAllEnvs(); vi.unstubAllGlobals(); }); describe("fixed Authorization protocol with a configurable browser cookie", () => { it("uses callback Authorization and ignores a contradictory cookie", async () => { const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { expect(new Headers(init?.headers).get("Authorization")).toBe(validJwt); return Response.json(String(input).endsWith("/clsf") ? categories : [file]); }); vi.stubGlobal("fetch", fetchMock); const response = await fileAuthorityPOST(new NextRequest("https://dc.invalid/api/rag-authority/file", { method: "POST", headers: { Authorization: `Bearer ${validJwt}`, Cookie: `custom-auth-cookie=${forgedJwt}`, }, body: JSON.stringify({ fileSn: "file-1" }), })); expect(response.status).toBe(200); expect(fetchMock).toHaveBeenCalledTimes(2); }); it("does not treat an anonymous dc-service response as proof for a forged JWT", async () => { const fetchMock = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => { // A real dc-service authentication filter rejects an invalid Authorization JWT. // It would return public data if this exact header were accidentally omitted. return new Headers(init?.headers).get("Authorization") === forgedJwt ? Response.json({ error: "invalid token" }, { status: 401 }) : Response.json([file]); }); vi.stubGlobal("fetch", fetchMock); const response = await fileAuthorityPOST(new NextRequest("https://dc.invalid/api/rag-authority/file", { method: "POST", headers: { Authorization: `Bearer ${forgedJwt}` }, body: JSON.stringify({ fileSn: "file-1" }), })); expect(response.status).toBe(401); expect(fetchMock).toHaveBeenCalledTimes(1); }); it("reads the configured browser cookie but forwards its token as Authorization", async () => { const form = new FormData(); form.set("file_sn", "file-1"); form.set("project_id", "root-1"); form.set("clsf_systm_sn", "leaf-1"); form.set("pdf_path", "./output_pdf/file-1.pdf"); form.set("original_filename", "자료.pdf"); const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { expect(new Headers(init?.headers).get("Authorization")).toBe(validJwt); const url = String(input); if (url.includes("/fileprcs")) return Response.json([file]); if (url.endsWith("/clsf")) return Response.json(categories); return Response.json({ job_id: "123e4567-e89b-42d3-a456-426614174000", completion_attestation: "opaque-rag-capability-with-more-than-32-characters", }); }); vi.stubGlobal("fetch", fetchMock); const response = await embedPOST(new NextRequest("https://dc.invalid/api/flask-service/embed-documents/jobs", { method: "POST", headers: { Cookie: `custom-auth-cookie=${validJwt}` }, body: form, })); expect(response.status).toBe(200); expect(fetchMock).toHaveBeenCalledTimes(3); }); });