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
99 lines
4.3 KiB
TypeScript
99 lines
4.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
// add-cms-forms Task 8: the portal's form route handlers forward the admin JWT to mobile-api's
|
|
// /api/admin/forms/**, encode ids, 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 forms from "./route";
|
|
import * as form from "./[id]/route";
|
|
import * as remove from "./[id]/delete/route";
|
|
import * as submissions from "./[id]/submissions/route";
|
|
import * as submission from "./submissions/[sid]/route";
|
|
import * as removeSubmission from "./submissions/[sid]/delete/route";
|
|
|
|
type Req = Parameters<typeof forms.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 }) });
|
|
const sctx = (sid: string) => ({ params: Promise.resolve({ sid }) });
|
|
|
|
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([
|
|
forms.GET(req("/api/forms")),
|
|
forms.POST(json("/api/forms", {})),
|
|
form.GET(req("/api/forms/3"), ctx("3")),
|
|
remove.POST(json("/api/forms/3/delete", {}), ctx("3")),
|
|
submissions.GET(req("/api/forms/3/submissions"), ctx("3")),
|
|
submission.GET(req("/api/forms/submissions/12"), sctx("12")),
|
|
removeSubmission.POST(json("/api/forms/submissions/12/delete", {}), sctx("12")),
|
|
]);
|
|
expect(results.map((r) => r.status)).toEqual([401, 401, 401, 401, 401, 401, 401]);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("forwarding", () => {
|
|
it("GETs only read", async () => {
|
|
await forms.GET(req("/api/forms"));
|
|
await form.GET(req("/api/forms/a%2Fb"), ctx("a/b"));
|
|
await submissions.GET(req("/api/forms/3/submissions?page=2&size=5000"), ctx("3"));
|
|
await submissions.GET(req("/api/forms/3/submissions"), ctx("3"));
|
|
await submission.GET(req("/api/forms/submissions/12"), sctx("12"));
|
|
|
|
expect(mocks.callMobileApi.mock.calls).toEqual([
|
|
["/api/admin/forms", { token: "jwt-abc" }],
|
|
["/api/admin/forms/a%2Fb", { token: "jwt-abc" }],
|
|
["/api/admin/forms/3/submissions?page=2&size=5000", { token: "jwt-abc" }],
|
|
["/api/admin/forms/3/submissions?page=0&size=50", { token: "jwt-abc" }],
|
|
["/api/admin/forms/submissions/12", { token: "jwt-abc" }],
|
|
]);
|
|
});
|
|
|
|
it("drops a non-numeric page or size back to the defaults", async () => {
|
|
await submissions.GET(req("/api/forms/3/submissions?page=x&size=1e9junk"), ctx("3"));
|
|
expect(mocks.callMobileApi.mock.calls[0][0]).toBe("/api/admin/forms/3/submissions?page=0&size=50");
|
|
});
|
|
|
|
it("POSTs save and delete", async () => {
|
|
await forms.POST(json("/api/forms", { name: "Contact us" }));
|
|
await remove.POST(json("/api/forms/3/delete", {}), ctx("3"));
|
|
await removeSubmission.POST(json("/api/forms/submissions/12/delete", {}), sctx("12"));
|
|
|
|
expect(mocks.callMobileApi.mock.calls).toEqual([
|
|
["/api/admin/forms", { method: "POST", body: { name: "Contact us" }, token: "jwt-abc" }],
|
|
["/api/admin/forms/3/delete", { method: "POST", token: "jwt-abc" }],
|
|
["/api/admin/forms/submissions/12/delete", { method: "POST", token: "jwt-abc" }],
|
|
]);
|
|
});
|
|
|
|
it("passes a 409 refusal through with its message", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
status: 409,
|
|
error: "This form has submissions. Hide it instead, or delete its submissions first.",
|
|
});
|
|
|
|
const res = await remove.POST(json("/api/forms/3/delete", {}), ctx("3"));
|
|
|
|
expect(res.status).toBe(409);
|
|
expect(JSON.stringify(await res.json())).toContain("This form has submissions.");
|
|
});
|
|
|
|
it("delete routes are POST only", () => {
|
|
expect("GET" in remove).toBe(false);
|
|
expect("GET" in removeSubmission).toBe(false);
|
|
});
|
|
});
|