Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
68 lines
2.6 KiB
TypeScript
68 lines
2.6 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, PUT } from "./route";
|
|
|
|
function ctx(uuid: string) {
|
|
return { params: Promise.resolve({ uuid }) };
|
|
}
|
|
function getReq() {
|
|
return new Request("http://localhost/api/events/abc-123") as unknown as Parameters<typeof GET>[0];
|
|
}
|
|
function putReq(body: unknown) {
|
|
return new Request("http://localhost/api/events/abc-123", {
|
|
method: "PUT",
|
|
body: JSON.stringify(body),
|
|
}) as unknown as Parameters<typeof PUT>[0];
|
|
}
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset();
|
|
});
|
|
|
|
describe("GET /api/events/[uuid]", () => {
|
|
it("returns 401 when there is no session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
const res = await GET(getReq(), ctx("abc-123"));
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it("forwards to GET /api/admin/events/{uuid}", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { uuid: "abc-123" } });
|
|
await GET(getReq(), ctx("abc-123"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/events/abc-123", { token: "jwt-abc" });
|
|
});
|
|
|
|
it("encodes special characters in uuid", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { uuid: "encoded" } });
|
|
await GET(getReq(), ctx("some:uuid/with-slashes"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/events/some%3Auuid%2Fwith-slashes", { token: "jwt-abc" });
|
|
});
|
|
});
|
|
|
|
describe("PUT /api/events/[uuid]", () => {
|
|
it("returns 401 when there is no session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
const res = await PUT(putReq({ name: "Updated" }), ctx("abc-123"));
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it("forwards the body to PUT /api/admin/events/{uuid}", async () => {
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
const body = { name: "Updated", location: "New Place", startDate: "2025-02-01", endDate: "2025-02-02", enabled: true, salespersonIds: [3] };
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { uuid: "abc-123", ...body } });
|
|
await PUT(putReq(body), ctx("abc-123"));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/events/abc-123", {
|
|
method: "PUT",
|
|
body,
|
|
token: "jwt-abc",
|
|
});
|
|
});
|
|
});
|