Lists, adds, renames, orders and activates/deactivates interests, gated by BO_INTERESTS. API errors map to field messages under Name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
106 lines
4.0 KiB
TypeScript
106 lines
4.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
|
|
|
import { GET, POST } from "./route";
|
|
|
|
function getReq() {
|
|
return new Request("http://localhost/api/interests") as unknown as Parameters<typeof GET>[0];
|
|
}
|
|
function postReq(body: unknown) {
|
|
return new Request("http://localhost/api/interests", {
|
|
method: "POST",
|
|
body: JSON.stringify(body),
|
|
}) as unknown as Parameters<typeof POST>[0];
|
|
}
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset();
|
|
});
|
|
|
|
describe("GET /api/interests", () => {
|
|
it("returns 401 when there is no session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
const res = await GET(getReq());
|
|
expect(res.status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("forwards to GET /api/admin/interests", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: [] });
|
|
await GET(getReq());
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/interests", { token: "jwt-abc" });
|
|
});
|
|
});
|
|
|
|
describe("POST /api/interests", () => {
|
|
it("returns 401 when there is no session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
const res = await POST(postReq({ name: "Test" }));
|
|
expect(res.status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("requires a name, blocks empty/whitespace", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
const empty = await POST(postReq({}));
|
|
expect(empty.status).toBe(400);
|
|
const ws = await POST(postReq({ name: " " }));
|
|
expect(ws.status).toBe(400);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("trims the name before sending", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: 1, name: "Workshop" } });
|
|
await POST(postReq({ name: " Workshop " }));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/interests", {
|
|
method: "POST",
|
|
body: { name: "Workshop" },
|
|
token: "jwt-abc",
|
|
});
|
|
});
|
|
|
|
it("forwards name, active, sortOrder to POST /api/admin/interests", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: 1, name: "Test" } });
|
|
await POST(postReq({ name: "Test", active: true, sortOrder: 5 }));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/interests", {
|
|
method: "POST",
|
|
body: { name: "Test", active: true, sortOrder: 5 },
|
|
token: "jwt-abc",
|
|
});
|
|
});
|
|
|
|
it("returns REQUIRED_FIELD code for a blank name", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
const res = await POST(postReq({ name: " " }));
|
|
expect(res.status).toBe(400);
|
|
expect(((await res.json()) as Record<string, unknown>).code).toBe("REQUIRED_FIELD");
|
|
});
|
|
|
|
it("passes upstream 409 with code and details to the client", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false, error: "Duplicate name", status: 409, code: "DUPLICATE_NAME", details: { field: "name" },
|
|
});
|
|
const res = await POST(postReq({ name: "Dup" }));
|
|
expect(res.status).toBe(409);
|
|
const json = (await res.json()) as Record<string, unknown>;
|
|
expect(json.code).toBe("DUPLICATE_NAME");
|
|
expect(json.details).toEqual({ field: "name" });
|
|
});
|
|
|
|
it("passes upstream 403 with code to the client", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Not authorized", status: 403, code: "NOT_AUTHORIZED" });
|
|
const res = await POST(postReq({ name: "X" }));
|
|
expect(res.status).toBe(403);
|
|
expect(((await res.json()) as Record<string, unknown>).code).toBe("NOT_AUTHORIZED");
|
|
});
|
|
});
|