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
181 lines
6.9 KiB
TypeScript
181 lines
6.9 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
import { callMobileApi } from "./mobileApi";
|
|
|
|
const BASE = "https://mobile.example.test";
|
|
const KEY = "test-api-key";
|
|
|
|
function makeRes(body: string, init: { ok?: boolean; status?: number; headers?: Record<string, string> } = {}) {
|
|
return {
|
|
ok: init.ok ?? true,
|
|
status: init.status ?? 200,
|
|
headers: new Headers(init.headers ?? {}),
|
|
text: async () => body,
|
|
} as Response;
|
|
}
|
|
|
|
let fetchMock: ReturnType<typeof vi.fn>;
|
|
|
|
beforeEach(() => {
|
|
vi.stubEnv("MOBILE_API_BASE_URL", BASE);
|
|
vi.stubEnv("MOBILE_API_KEY", KEY);
|
|
fetchMock = vi.fn();
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
describe("callMobileApi", () => {
|
|
it("targets the base URL and injects the api-key header", async () => {
|
|
fetchMock.mockResolvedValue(makeRes(JSON.stringify({ value: 1 })));
|
|
await callMobileApi("/api/member/profile");
|
|
|
|
const [url, init] = fetchMock.mock.calls[0];
|
|
expect(url).toBe(`${BASE}/api/member/profile`);
|
|
const headers = new Headers(init.headers);
|
|
expect(headers.get("api-key")).toBe(KEY);
|
|
expect(headers.get("authorization")).toBeNull();
|
|
});
|
|
|
|
it("forwards the bearer token when provided", async () => {
|
|
fetchMock.mockResolvedValue(makeRes("{}"));
|
|
await callMobileApi("/api/member/profile", { token: "jwt-123" });
|
|
|
|
const [, init] = fetchMock.mock.calls[0];
|
|
const headers = new Headers(init.headers);
|
|
expect(headers.get("authorization")).toBe("Bearer jwt-123");
|
|
});
|
|
|
|
it('normalizes {result:"false", errorMessage} to an error', async () => {
|
|
fetchMock.mockResolvedValue(
|
|
makeRes(JSON.stringify({ result: "false", errorMessage: "This phone no already registered. " })),
|
|
);
|
|
const r = await callMobileApi("/api/member/registerNewMember", { method: "POST", body: {} });
|
|
expect(r.ok).toBe(false);
|
|
if (!r.ok) expect(r.error).toContain("already registered");
|
|
});
|
|
|
|
it("surfaces multipleEmailDetected via extra", async () => {
|
|
fetchMock.mockResolvedValue(
|
|
makeRes(JSON.stringify({ success: false, multipleEmailDetected: true })),
|
|
);
|
|
const r = await callMobileApi("/api/member/forgetPassword", { method: "POST", body: {} });
|
|
expect(r.ok).toBe(false);
|
|
if (!r.ok) expect(r.extra?.multipleEmailDetected).toBe(true);
|
|
});
|
|
|
|
it("flags an expired token (HTTP 500 Expired Token)", async () => {
|
|
fetchMock.mockResolvedValue(makeRes("Expired Token", { ok: false, status: 500 }));
|
|
const r = await callMobileApi("/api/member/profile", { token: "old" });
|
|
expect(r.ok).toBe(false);
|
|
if (!r.ok) expect(r.expired).toBe(true);
|
|
});
|
|
|
|
it("returns parsed JSON on success", async () => {
|
|
fetchMock.mockResolvedValue(makeRes(JSON.stringify({ memberCode: "73774867" })));
|
|
const r = await callMobileApi<{ memberCode: string }>("/api/member/profile", { token: "jwt" });
|
|
expect(r.ok).toBe(true);
|
|
if (r.ok) expect(r.data.memberCode).toBe("73774867");
|
|
});
|
|
|
|
it("returns a plain-string body as data (e.g. referralLink)", async () => {
|
|
fetchMock.mockResolvedValue(makeRes("https://tastefully/ref/ABC"));
|
|
const r = await callMobileApi<string>("/api/member/referralLink", { token: "jwt" });
|
|
expect(r.ok).toBe(true);
|
|
if (r.ok) expect(r.data).toBe("https://tastefully/ref/ABC");
|
|
});
|
|
|
|
it("passes an abort signal to fetch so requests cannot hang forever", async () => {
|
|
fetchMock.mockResolvedValue(makeRes("{}"));
|
|
await callMobileApi("/api/member/profile", { timeoutMs: 5000 });
|
|
const [, init] = fetchMock.mock.calls[0];
|
|
expect(init.signal).toBeInstanceOf(AbortSignal);
|
|
});
|
|
|
|
it("returns a timeout error (timedOut flag) when the upstream call aborts", async () => {
|
|
fetchMock.mockRejectedValue(new DOMException("The operation timed out.", "TimeoutError"));
|
|
const r = await callMobileApi("/api/member/registerNewMember", { method: "POST", body: {} });
|
|
expect(r.ok).toBe(false);
|
|
if (!r.ok) {
|
|
expect(r.timedOut).toBe(true);
|
|
expect(r.error).toContain("taking too long");
|
|
}
|
|
});
|
|
|
|
it("maps an upstream 403 to a phoneVerificationRequired failure", async () => {
|
|
fetchMock.mockResolvedValue(
|
|
makeRes(JSON.stringify({ status: 403, error: "Forbidden" }), {
|
|
ok: false,
|
|
status: 403,
|
|
headers: { "X-Phone-Verification-Required": "true" },
|
|
}),
|
|
);
|
|
const r = await callMobileApi("/api/member/profile", { token: "jwt" });
|
|
expect(r).toMatchObject({
|
|
ok: false,
|
|
status: 403,
|
|
error: "Phone verification required.",
|
|
phoneVerificationRequired: true,
|
|
});
|
|
});
|
|
|
|
it("403 without the header is not a phone-verification failure", async () => {
|
|
fetchMock.mockResolvedValue(
|
|
makeRes(JSON.stringify({ errorMessage: "Not your transaction" }), { ok: false, status: 403 }),
|
|
);
|
|
const r = await callMobileApi("/api/member/profile", { token: "jwt" });
|
|
expect(r).toMatchObject({ ok: false, status: 403, error: "Not your transaction" });
|
|
expect(r).not.toHaveProperty("phoneVerificationRequired", true);
|
|
});
|
|
|
|
it("does not flag phoneVerificationRequired for a 400", async () => {
|
|
fetchMock.mockResolvedValue(makeRes(JSON.stringify({ error: "bad" }), { ok: false, status: 400 }));
|
|
const r = await callMobileApi("/api/member/profile", { token: "jwt" });
|
|
expect(r.ok).toBe(false);
|
|
expect(r).not.toHaveProperty("phoneVerificationRequired", true);
|
|
});
|
|
});
|
|
|
|
describe("callMobileApi unreadable upstream errors", () => {
|
|
const GENERIC = "Something went wrong. Please try again.";
|
|
|
|
it("uses the generic message for an empty error body", async () => {
|
|
fetchMock.mockResolvedValue(makeRes("", { ok: false, status: 404 }));
|
|
const r = await callMobileApi("/api/event/luckyDraw");
|
|
expect(r).toEqual({ ok: false, error: GENERIC, status: 404 });
|
|
});
|
|
|
|
it("uses the generic message for Spring error JSON without a message", async () => {
|
|
const body = JSON.stringify({
|
|
timestamp: "2026-11-02T10:00:00.000+00:00",
|
|
status: 500,
|
|
error: "Internal Server Error",
|
|
path: "/api/x",
|
|
});
|
|
fetchMock.mockResolvedValue(makeRes(body, { ok: false, status: 500 }));
|
|
const r = await callMobileApi("/api/x");
|
|
expect(r.ok).toBe(false);
|
|
if (r.ok) return;
|
|
expect(r.error).toBe(GENERIC);
|
|
expect(r.error).not.toContain("Internal Server Error");
|
|
});
|
|
|
|
it("keeps a non-empty plain-text error body", async () => {
|
|
fetchMock.mockResolvedValue(makeRes("Bad thing", { ok: false, status: 400 }));
|
|
const r = await callMobileApi("/api/x");
|
|
expect(r.ok).toBe(false);
|
|
if (r.ok) return;
|
|
expect(r.error).toBe("Bad thing");
|
|
});
|
|
|
|
it("keeps a readable upstream message", async () => {
|
|
fetchMock.mockResolvedValue(makeRes(JSON.stringify({ message: "Param 'dob' is invalid." }), { ok: false, status: 400 }));
|
|
const r = await callMobileApi("/api/member/update");
|
|
expect(r.ok).toBe(false);
|
|
if (r.ok) return;
|
|
expect(r.error).toBe("Param 'dob' is invalid.");
|
|
});
|
|
});
|