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
89 lines
3.8 KiB
TypeScript
89 lines
3.8 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
// add-cms-pages: the portal's page route handlers forward the admin JWT to mobile-api's
|
|
// /api/admin/pages/**, encode the id, pass 4xx through, and keep every GET free of side effects.
|
|
|
|
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 pages from "./route";
|
|
import * as page from "./[id]/route";
|
|
import * as remove from "./[id]/delete/route";
|
|
import * as image from "./image/route";
|
|
|
|
type Req = Parameters<typeof pages.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([
|
|
pages.GET(req("/api/pages")),
|
|
pages.POST(json("/api/pages", {})),
|
|
page.GET(req("/api/pages/5"), ctx("5")),
|
|
remove.POST(json("/api/pages/5/delete", {}), ctx("5")),
|
|
image.POST(json("/api/pages/image", { fileName: "a.png", fileType: "image/png", base: "x" })),
|
|
]);
|
|
expect(results.map((r) => r.status)).toEqual([401, 401, 401, 401, 401]);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("forwarding", () => {
|
|
it("GET lists and reads with a single read each", async () => {
|
|
await pages.GET(req("/api/pages"));
|
|
await page.GET(req("/api/pages/a%2Fb"), ctx("a/b"));
|
|
|
|
expect(mocks.callMobileApi.mock.calls).toEqual([
|
|
["/api/admin/pages", { token: "jwt-abc" }],
|
|
["/api/admin/pages/a%2Fb", { token: "jwt-abc" }],
|
|
]);
|
|
});
|
|
|
|
it("POST saves, deletes and uploads", async () => {
|
|
await pages.POST(json("/api/pages", { slug: "about-us" }));
|
|
await remove.POST(json("/api/pages/7/delete", {}), ctx("7"));
|
|
await image.POST(json("/api/pages/image", { fileName: "a.png", fileType: "image/png", base: "QQ==" }));
|
|
|
|
expect(mocks.callMobileApi.mock.calls).toEqual([
|
|
["/api/admin/pages", { method: "POST", body: { slug: "about-us" }, token: "jwt-abc" }],
|
|
["/api/admin/pages/7/delete", { method: "POST", token: "jwt-abc" }],
|
|
["/api/admin/pages/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/pages/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: "blocks[0].buttonUrl: must start with / or https://" });
|
|
|
|
const res = await pages.POST(json("/api/pages", {}));
|
|
|
|
expect(res.status).toBe(422);
|
|
expect((await res.json()).error).toBe("blocks[0].buttonUrl: must start with / or https://");
|
|
});
|
|
|
|
it("a successful delete answers JSON so the client can parse it", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: null });
|
|
|
|
const res = await remove.POST(json("/api/pages/7/delete", {}), ctx("7"));
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({ ok: true });
|
|
});
|
|
});
|