Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
72 lines
3.3 KiB
TypeScript
72 lines
3.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
// add-storefront-menus Task 6: the menu route handler forwards the admin JWT to mobile-api's
|
|
// /api/admin/menus/{handle}, encodes the handle, passes 4xx through, and its GET only reads.
|
|
|
|
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 * as menu from "./[handle]/route";
|
|
|
|
type Req = Parameters<typeof menu.GET>[0];
|
|
const req = (url: string, init?: RequestInit) => new Request(`http://localhost${url}`, init) as unknown as Req;
|
|
const ctx = (handle: string) => ({ params: Promise.resolve({ handle }) });
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset();
|
|
mocks.getToken.mockResolvedValue("jwt-abc");
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { handle: "main", items: [] } });
|
|
});
|
|
|
|
describe("/api/menus/[handle]", () => {
|
|
it("answers 401 without a session and calls nothing", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
const results = await Promise.all([
|
|
menu.GET(req("/api/menus/main"), ctx("main")),
|
|
menu.POST(req("/api/menus/main", { method: "POST", body: "{}" }), ctx("main")),
|
|
]);
|
|
expect(results.map((r) => r.status)).toEqual([401, 401]);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("reads and saves through mobile-api with the token", async () => {
|
|
await menu.GET(req("/api/menus/main"), ctx("main"));
|
|
await menu.POST(req("/api/menus/footer", { method: "POST", body: JSON.stringify({ items: [] }) }), ctx("footer"));
|
|
|
|
expect(mocks.callMobileApi.mock.calls).toEqual([
|
|
["/api/admin/menus/main", { token: "jwt-abc" }],
|
|
["/api/admin/menus/footer", { method: "POST", body: { items: [] }, token: "jwt-abc" }],
|
|
]);
|
|
});
|
|
|
|
// Task 6 review: ".." is not changed by encodeURIComponent, and fetch would resolve
|
|
// /api/admin/menus/.. to /api/admin/. Only the two menus exist, so anything else is a 404.
|
|
it.each(["..", ".", "a/b", "other"])("answers 404 for the handle %j and calls nothing", async (handle) => {
|
|
const results = await Promise.all([
|
|
menu.GET(req("/api/menus/x"), ctx(handle)),
|
|
menu.POST(req("/api/menus/x", { method: "POST", body: "{}" }), ctx(handle)),
|
|
]);
|
|
expect(results.map((r) => r.status)).toEqual([404, 404]);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
// Task 6 review: a body that is not JSON must not be forwarded as {} (that saves an empty menu).
|
|
it("answers 400 for a body that is not JSON and saves nothing", async () => {
|
|
const res = await menu.POST(req("/api/menus/main", { method: "POST", body: "{not json" }), ctx("main"));
|
|
|
|
expect(res.status).toBe(400);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("passes a 422 through with its message", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 422, error: "items[0].url: must start with a single / or https://" });
|
|
|
|
const res = await menu.POST(req("/api/menus/main", { method: "POST", body: "{}" }), ctx("main"));
|
|
|
|
expect(res.status).toBe(422);
|
|
expect(JSON.stringify(await res.json())).toContain("items[0].url");
|
|
});
|
|
});
|