|
| 1 | +/** |
| 2 | + * Tests for GET/POST /api/team. |
| 3 | + * |
| 4 | + * Covers: |
| 5 | + * - Mock fallback list/add in non-production when no backend is configured |
| 6 | + * - Proxies to the backend when NEXT_PUBLIC_API_URL is set |
| 7 | + * - Never falls back to mock team data in a production build — fails |
| 8 | + * loudly with 503 instead, matching the pattern in |
| 9 | + * src/app/api/wallets/route.ts / isMockFallbackAllowed(). |
| 10 | + */ |
| 11 | + |
| 12 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 13 | +import { GET, POST } from "./route"; |
| 14 | + |
| 15 | +function postRequest(body: unknown): Request { |
| 16 | + return new Request("http://localhost/api/team", { |
| 17 | + method: "POST", |
| 18 | + headers: { "content-type": "application/json" }, |
| 19 | + body: JSON.stringify(body), |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +describe("/api/team", () => { |
| 24 | + afterEach(() => { |
| 25 | + vi.unstubAllEnvs(); |
| 26 | + vi.unstubAllGlobals(); |
| 27 | + }); |
| 28 | + |
| 29 | + describe("mock fallback (no NEXT_PUBLIC_API_URL, non-production)", () => { |
| 30 | + it("lists mock team members", async () => { |
| 31 | + vi.stubEnv("NEXT_PUBLIC_API_URL", ""); |
| 32 | + vi.stubEnv("NODE_ENV", "test"); |
| 33 | + |
| 34 | + const res = await GET(); |
| 35 | + expect(res.status).toBe(200); |
| 36 | + const body = await res.json(); |
| 37 | + expect(Array.isArray(body.data)).toBe(true); |
| 38 | + expect(body.data.length).toBeGreaterThan(0); |
| 39 | + }); |
| 40 | + |
| 41 | + it("rejects an invalid role", async () => { |
| 42 | + vi.stubEnv("NEXT_PUBLIC_API_URL", ""); |
| 43 | + vi.stubEnv("NODE_ENV", "test"); |
| 44 | + |
| 45 | + const res = await POST( |
| 46 | + postRequest({ name: "Test", email: "t@example.com", role: "owner" }), |
| 47 | + ); |
| 48 | + expect(res.status).toBe(400); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("backend proxy (NEXT_PUBLIC_API_URL is set)", () => { |
| 53 | + it("proxies GET to the backend", async () => { |
| 54 | + vi.stubEnv("NEXT_PUBLIC_API_URL", "https://api.example.com"); |
| 55 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 56 | + ok: true, |
| 57 | + json: () => Promise.resolve([{ id: "m1", role: "admin" }]), |
| 58 | + }); |
| 59 | + vi.stubGlobal("fetch", fetchMock); |
| 60 | + |
| 61 | + const res = await GET(); |
| 62 | + expect(res.status).toBe(200); |
| 63 | + expect(fetchMock).toHaveBeenCalledWith( |
| 64 | + "https://api.example.com/team", |
| 65 | + expect.any(Object), |
| 66 | + ); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe("production without a configured backend", () => { |
| 71 | + it("returns 503 instead of silently serving mock team members", async () => { |
| 72 | + vi.stubEnv("NEXT_PUBLIC_API_URL", ""); |
| 73 | + vi.stubEnv("NEXT_PUBLIC_MUX_API_URL", ""); |
| 74 | + vi.stubEnv("NEXT_PUBLIC_API_BASE", ""); |
| 75 | + vi.stubEnv("NODE_ENV", "production"); |
| 76 | + |
| 77 | + const res = await GET(); |
| 78 | + expect(res.status).toBe(503); |
| 79 | + const body = await res.json(); |
| 80 | + expect(body.error).toBe("backend_unavailable"); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments