Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
84 lines
5.2 KiB
TypeScript
84 lines
5.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), callMobileApiBinary: vi.fn(), getToken: vi.fn() }));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi, callMobileApiBinary: mocks.callMobileApiBinary }));
|
|
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
|
vi.mock("@/app/lib/server/clientContext", () => ({ clientIp: () => "203.0.113.9" }));
|
|
|
|
import * as list from "./route";
|
|
import * as one from "./[id]/route";
|
|
import * as images from "./images/route";
|
|
import * as publish from "./[id]/publish/route";
|
|
import * as discard from "./[id]/discard/route";
|
|
import * as form from "../public/pages/[company]/[slug]/form/[block]/route";
|
|
import * as image from "../public/page-images/[id]/route";
|
|
import * as branding from "../companies/[id]/branding/route";
|
|
|
|
type Req = Parameters<typeof list.GET>[0];
|
|
const req = (url: string, init: RequestInit = {}) => new Request(`http://localhost${url}`, init) as unknown as Req;
|
|
const ctx = <T,>(p: T) => ({ params: Promise.resolve(p) });
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.getToken.mockResolvedValue("jwt");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: {} });
|
|
});
|
|
|
|
describe("admin page routes", () => {
|
|
it("refuse without a session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
expect((await list.GET(req("/api/pages"))).status).toBe(401);
|
|
expect((await one.PUT(req("/api/pages/1", { method: "PUT", body: "{}" }), ctx({ id: "1" }))).status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("list forwards only the allowed query values", async () => {
|
|
await list.GET(req("/api/pages?q=a%26b&page=2&size=50&companyId=x&evil=1"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/pages?q=a%26b&page=2&size=50", { token: "jwt" });
|
|
});
|
|
|
|
it("encodes the id and passes create, update and delete", async () => {
|
|
await one.GET(req("/api/pages/x"), ctx({ id: "1/../2" }));
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages/1%2F..%2F2", { token: "jwt" });
|
|
await one.PUT(req("/api/pages/1", { method: "PUT", body: JSON.stringify({ title: "T" }) }), ctx({ id: "1" }));
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages/1", { method: "PUT", body: { title: "T" }, token: "jwt" });
|
|
await one.DELETE(req("/api/pages/1", { method: "DELETE" }), ctx({ id: "1" }));
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages/1", { method: "DELETE", token: "jwt" });
|
|
await publish.POST(req("/api/pages/1/publish", { method: "POST" }), ctx({ id: "1/x" }));
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages/1%2Fx/publish", { method: "POST", token: "jwt" });
|
|
await discard.POST(req("/api/pages/1/discard", { method: "POST" }), ctx({ id: "1" }));
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages/1/discard", { method: "POST", token: "jwt" });
|
|
await list.POST(req("/api/pages", { method: "POST", body: JSON.stringify({ title: "T" }) }));
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/pages", { method: "POST", body: { title: "T" }, token: "jwt" });
|
|
});
|
|
|
|
it("uploads an image and saves branding", async () => {
|
|
await images.POST(req("/api/pages/images", { method: "POST", body: JSON.stringify({ companyId: 2, data: "aGk=" }) }));
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/page-images", { method: "POST", body: { companyId: 2, data: "aGk=" }, token: "jwt" });
|
|
await branding.PUT(req("/api/companies/2/branding", { method: "PUT", body: JSON.stringify({ accentColor: "#112233" }) }), ctx({ id: "2" }));
|
|
expect(mocks.callMobileApi).toHaveBeenLastCalledWith("/api/admin/companies/2/branding", { method: "PUT", body: { accentColor: "#112233" }, token: "jwt" });
|
|
});
|
|
});
|
|
|
|
describe("public page routes", () => {
|
|
it("form submit is forwarded with no session, encoded, with the visitor's address", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
await form.POST(req("/x", { method: "POST", body: JSON.stringify({ name: "A" }) }), ctx({ company: "INS", slug: "a/b", block: "2" }));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/pages/INS/a%2Fb/form/2",
|
|
{ method: "POST", body: { name: "A" }, headers: { "X-Forwarded-For": "203.0.113.9" } });
|
|
});
|
|
|
|
it("image is served with its type and a long cache, and a miss is 404", async () => {
|
|
mocks.callMobileApiBinary.mockResolvedValueOnce({ ok: true, data: { body: new Uint8Array([1, 2]).buffer, contentType: "image/png", contentDisposition: null } });
|
|
const res = await image.GET(req("/api/public/page-images/7"), ctx({ id: "7" }));
|
|
expect(mocks.callMobileApiBinary).toHaveBeenCalledWith("/api/public/page-images/7");
|
|
expect(res.headers.get("Content-Type")).toBe("image/png");
|
|
expect(res.headers.get("Cache-Control")).toContain("max-age=31536000");
|
|
expect(new Uint8Array(await res.arrayBuffer())).toEqual(new Uint8Array([1, 2]));
|
|
|
|
expect((await image.GET(req("/x"), ctx({ id: "abc" }))).status).toBe(404);
|
|
mocks.callMobileApiBinary.mockResolvedValueOnce({ ok: false, status: 404, error: "x" });
|
|
expect((await image.GET(req("/x"), ctx({ id: "8" }))).status).toBe(404);
|
|
});
|
|
});
|