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
107 lines
3.9 KiB
TypeScript
107 lines
3.9 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 { PUT } from "./route";
|
|
|
|
function ctx(id: string) {
|
|
return { params: Promise.resolve({ id }) };
|
|
}
|
|
function putReq(body: unknown) {
|
|
return new Request("http://localhost/api/interests/4", {
|
|
method: "PUT",
|
|
body: JSON.stringify(body),
|
|
}) as unknown as Parameters<typeof PUT>[0];
|
|
}
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset();
|
|
});
|
|
|
|
describe("PUT /api/interests/[id]", () => {
|
|
it("returns 401 when there is no session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
const res = await PUT(putReq({ name: "New" }), ctx("4"));
|
|
expect(res.status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("requires a name when updating name", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
const empty = await PUT(putReq({}), ctx("4"));
|
|
expect(empty.status).toBe(400);
|
|
const ws = await PUT(putReq({ name: " " }), ctx("4"));
|
|
expect(ws.status).toBe(400);
|
|
expect(((await ws.json()) as Record<string, unknown>).code).toBe("REQUIRED_FIELD");
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("trims the name before sending", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: 4, name: "Updated" } });
|
|
await PUT(putReq({ name: " Updated " }), ctx("4"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/interests/4", {
|
|
method: "PUT",
|
|
body: { name: "Updated" },
|
|
token: "jwt-abc",
|
|
});
|
|
});
|
|
|
|
it("forwards { name } to PUT /api/admin/interests/{id}", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: 4, name: "New" } });
|
|
await PUT(putReq({ name: "New", active: true, sortOrder: 10 }), ctx("4"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/interests/4", {
|
|
method: "PUT",
|
|
body: { name: "New", active: true, sortOrder: 10 },
|
|
token: "jwt-abc",
|
|
});
|
|
});
|
|
|
|
it("forwards { active: false } without name for deactivation", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: 4, active: false, name: "Old" } });
|
|
await PUT(putReq({ active: false }), ctx("4"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/interests/4", {
|
|
method: "PUT",
|
|
body: { active: false },
|
|
token: "jwt-abc",
|
|
});
|
|
});
|
|
|
|
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 PUT(putReq({ name: "Dup" }), ctx("4"));
|
|
expect(res.status).toBe(409);
|
|
const json = await res.json() as Record<string, unknown>;
|
|
expect(json.error).toBe("Duplicate name");
|
|
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 PUT(putReq({ active: true }), ctx("4"));
|
|
expect(res.status).toBe(403);
|
|
const json = await res.json() as Record<string, unknown>;
|
|
expect(json.error).toBe("Not authorized");
|
|
expect(json.code).toBe("NOT_AUTHORIZED");
|
|
});
|
|
});
|