106 lines
5.0 KiB
TypeScript
106 lines
5.0 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { GET } from "./route";
|
|
|
|
const jwt = "fixture.user.token";
|
|
const dcBase = "https://dc-service.invalid";
|
|
const file = {
|
|
fileSn: "file-1", useYn: "Y", projectId: "library", clsfSystmSn: "library",
|
|
filePath: "./uploads/연구 & 계획.pdf", fileTitle: "연구 & 계획", fileExtn: "pdf",
|
|
};
|
|
const categories = [{ clsfSystmSn: "library", clsfSystmNm: "자료관" }];
|
|
const disposition = "attachment; filename*=UTF-8''" + encodeURIComponent("연구 & 계획.pdf");
|
|
|
|
function request(query = "fileSn=file-1", authenticated = true) {
|
|
return new NextRequest("https://frontend.invalid/api/registered-original?" + query, {
|
|
headers: authenticated ? { Cookie: `Authorization=${jwt}` } : {},
|
|
});
|
|
}
|
|
|
|
const fetchMock = vi.fn<typeof fetch>();
|
|
function registryReads(row = file) {
|
|
fetchMock.mockResolvedValueOnce(Response.json({ data: [row] }))
|
|
.mockResolvedValueOnce(Response.json(categories));
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.stubEnv("NEXT_PUBLIC_DC_SERVICE_API_URL", dcBase);
|
|
vi.stubEnv("RAG_V2_INGESTION_INTERNAL_API_KEY", "");
|
|
vi.stubGlobal("fetch", fetchMock.mockReset());
|
|
});
|
|
afterEach(() => { vi.unstubAllEnvs(); vi.unstubAllGlobals(); });
|
|
|
|
describe("registered original download", () => {
|
|
it.each(["연구 & 계획", "연구 & 계획.pdf"])("passes the attested path and registered filename to DC for %s", async (fileTitle) => {
|
|
registryReads({ ...file, fileTitle });
|
|
const bytes = new Uint8Array([0, 255, 2, 66]);
|
|
fetchMock.mockResolvedValueOnce(new Response(bytes, { headers: { "Content-Disposition": disposition } }));
|
|
const req = request();
|
|
const response = await GET(req);
|
|
expect(response.status).toBe(200);
|
|
const urls = fetchMock.mock.calls.map(([input]) => new URL(String(input)));
|
|
expect(urls.map(url => url.origin + url.pathname)).toEqual(
|
|
["fileprcs", "clsf", "fileDownload"].map(path => dcBase + "/dc-service/api/v1/" + path));
|
|
expect(Object.fromEntries(urls[0]!.searchParams)).toEqual({ fileSn: file.fileSn });
|
|
expect(Object.fromEntries(urls[2]!.searchParams)).toEqual({ filePath: file.filePath, fileName: "연구 & 계획.pdf" });
|
|
for (const [, init] of fetchMock.mock.calls) {
|
|
expect(init).toMatchObject({ cache: "no-store", redirect: "manual", signal: req.signal });
|
|
expect(new Headers(init?.headers).get("Authorization")).toBe(jwt);
|
|
}
|
|
expect(response.headers.get("Content-Disposition")).toBe(disposition);
|
|
expect(response.headers.get("Cache-Control")).toBe("no-store");
|
|
expect(new Uint8Array(await response.arrayBuffer())).toEqual(bytes);
|
|
});
|
|
|
|
it.each(["", "filePath=other", "fileSn=file-1&filePath=other", "fileSn=file-1&fileName=other", "fileSn=file-1&fileSn=file-2"])(
|
|
"requires a single file ID (%s)", async query => {
|
|
expect((await GET(request(query))).status).toBe(400);
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("requires a browser session before registry access", async () => {
|
|
expect((await GET(request("fileSn=file-1", false))).status).toBe(401);
|
|
expect(fetchMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it.each([401, 403])("propagates registry authentication failure (%i) before download", async status => {
|
|
fetchMock.mockResolvedValueOnce(new Response(null, { status }));
|
|
const response = await GET(request());
|
|
expect(response.status).toBe(status);
|
|
expect(response.headers.has("Content-Disposition")).toBe(false);
|
|
expect(fetchMock).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it.each([
|
|
{ ...file, fileSn: "other" },
|
|
{ ...file, useYn: "N" },
|
|
{ ...file, clsfSystmSn: "other" },
|
|
{ ...file, projectId: "other" },
|
|
])("enforces active file and classification before download", async row => {
|
|
registryReads(row);
|
|
expect((await GET(request())).status).toBe(409);
|
|
expect(fetchMock.mock.calls.some(([input]) => String(input).includes("fileDownload"))).toBe(false);
|
|
});
|
|
|
|
it("declines a missing registered path without calling fileDownload", async () => {
|
|
registryReads({ ...file, filePath: " " });
|
|
expect((await GET(request())).status).toBe(404);
|
|
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it.each([[200, 502], [302, 502], [401, 401], [403, 403], [404, 404], [504, 504]])(
|
|
"does not return a gateway page or upstream error as a file (%i)", async (upstream, expected) => {
|
|
registryReads();
|
|
fetchMock.mockResolvedValueOnce(new Response("UPSTREAM_PRIVATE_MESSAGE", {
|
|
status: upstream, headers: { Location: "https://login.invalid", "Content-Type": "text/html" },
|
|
}));
|
|
const response = await GET(request());
|
|
expect(response.status).toBe(expected);
|
|
expect(response.headers.get("Content-Type")).toBe("text/plain; charset=utf-8");
|
|
expect(response.headers.has("Content-Disposition")).toBe(false);
|
|
expect(response.headers.has("Location")).toBe(false);
|
|
expect(await response.text()).not.toContain("UPSTREAM_PRIVATE_MESSAGE");
|
|
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
});
|
|
});
|