Files
co-lite/solvway-admin-portal/app/api/collections/routes.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

94 lines
4.3 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
// add-storefront-collections: the portal's collection route handlers forward the admin JWT to
// mobile-api's /api/admin/collections/**, encode the id, pass 4xx through, and keep every GET free
// of side effects (a Next.js prefetch is a real GET).
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 collections from "./route";
import * as collection from "./[id]/route";
import * as remove from "./[id]/delete/route";
import * as preview from "./preview/route";
import * as image from "./image/route";
type Req = Parameters<typeof collections.GET>[0];
const req = (url: string, init?: RequestInit) => new Request(`http://localhost${url}`, init) as unknown as Req;
const json = (url: string, body: unknown) => req(url, { method: "POST", body: JSON.stringify(body) });
const ctx = (id: string) => ({ params: Promise.resolve({ id }) });
beforeEach(() => {
mocks.callMobileApi.mockReset();
mocks.getToken.mockReset();
mocks.getToken.mockResolvedValue("jwt-abc");
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { ok: 1 } });
});
describe("no session", () => {
it("every handler answers 401 without calling mobile-api", async () => {
mocks.getToken.mockResolvedValue(null);
const results = await Promise.all([
collections.GET(req("/api/collections")),
collections.POST(json("/api/collections", {})),
collection.GET(req("/api/collections/5"), ctx("5")),
remove.POST(json("/api/collections/5/delete", {}), ctx("5")),
preview.POST(json("/api/collections/preview", {})),
image.POST(json("/api/collections/image", { fileName: "a.png", fileType: "image/png", base: "x" })),
]);
expect(results.map((r) => r.status)).toEqual([401, 401, 401, 401, 401, 401]);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
});
describe("forwarding", () => {
it("GET lists and reads with a single read each", async () => {
await collections.GET(req("/api/collections"));
await collection.GET(req("/api/collections/a%2Fb"), ctx("a/b"));
expect(mocks.callMobileApi.mock.calls).toEqual([
["/api/admin/collections", { token: "jwt-abc" }],
["/api/admin/collections/a%2Fb", { token: "jwt-abc" }],
]);
});
it("POST saves, deletes, previews and uploads", async () => {
await collections.POST(json("/api/collections", { slug: "newborn" }));
await remove.POST(json("/api/collections/7/delete", {}), ctx("7"));
await preview.POST(json("/api/collections/preview", { type: "AUTOMATIC" }));
await image.POST(json("/api/collections/image", { fileName: "a.png", fileType: "image/png", base: "QQ==" }));
expect(mocks.callMobileApi.mock.calls).toEqual([
["/api/admin/collections", { method: "POST", body: { slug: "newborn" }, token: "jwt-abc" }],
["/api/admin/collections/7/delete", { method: "POST", token: "jwt-abc" }],
["/api/admin/collections/preview", { method: "POST", body: { type: "AUTOMATIC" }, token: "jwt-abc" }],
["/api/admin/collections/image", { method: "POST", body: { fileName: "a.png", fileType: "image/png", base: "QQ==" }, token: "jwt-abc" }],
]);
});
it("an image without file data is a 400 and never uploads", async () => {
const res = await image.POST(json("/api/collections/image", { fileName: "a.png" }));
expect(res.status).toBe(400);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("a 422 from mobile-api passes through with its message", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 422, error: "slug: another collection already uses newborn" });
const res = await collections.POST(json("/api/collections", {}));
expect(res.status).toBe(422);
expect((await res.json()).error).toBe("slug: another collection already uses newborn");
});
it("a successful delete answers 204-free JSON so the client can parse it", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: null });
const res = await remove.POST(json("/api/collections/7/delete", {}), ctx("7"));
expect(res.status).toBe(200);
expect(await res.json()).toEqual({ ok: true });
});
});