|
| 1 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 2 | +import { createClient } from "../../src/index.ts"; |
| 3 | + |
| 4 | +const appId = "test-app-id"; |
| 5 | +const origin = "https://my-app.base44.app"; |
| 6 | + |
| 7 | +function makeLocalStorage(initial: Record<string, string> = {}) { |
| 8 | + const store = new Map<string, string>(Object.entries(initial)); |
| 9 | + return { |
| 10 | + getItem: (k: string) => (store.has(k) ? store.get(k)! : null), |
| 11 | + setItem: (k: string, v: string) => { |
| 12 | + store.set(k, v); |
| 13 | + }, |
| 14 | + removeItem: (k: string) => { |
| 15 | + store.delete(k); |
| 16 | + }, |
| 17 | + clear: () => store.clear(), |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +function stubBrowser(storage = makeLocalStorage()) { |
| 22 | + vi.stubGlobal("document", { referrer: "", visibilityState: "visible" }); |
| 23 | + vi.stubGlobal("window", { |
| 24 | + addEventListener: vi.fn(), |
| 25 | + removeEventListener: vi.fn(), |
| 26 | + history: { replaceState: vi.fn() }, |
| 27 | + location: { |
| 28 | + href: `${origin}/dashboard`, |
| 29 | + origin, |
| 30 | + pathname: "/dashboard", |
| 31 | + search: "", |
| 32 | + }, |
| 33 | + localStorage: storage, |
| 34 | + }); |
| 35 | + vi.stubGlobal("localStorage", storage); |
| 36 | +} |
| 37 | + |
| 38 | +const createTestClient = (token?: string) => |
| 39 | + createClient({ |
| 40 | + serverUrl: "", |
| 41 | + appId, |
| 42 | + token, |
| 43 | + analytics: { enabled: false }, |
| 44 | + }); |
| 45 | + |
| 46 | +let fetchMock: ReturnType<typeof vi.fn>; |
| 47 | + |
| 48 | +beforeEach(() => { |
| 49 | + fetchMock = vi.fn().mockResolvedValue(new Response("{}")); |
| 50 | + vi.stubGlobal("fetch", fetchMock); |
| 51 | +}); |
| 52 | + |
| 53 | +afterEach(() => { |
| 54 | + vi.unstubAllGlobals(); |
| 55 | + vi.clearAllMocks(); |
| 56 | +}); |
| 57 | + |
| 58 | +const lastCall = () => { |
| 59 | + const [url, init] = fetchMock.mock.calls[0]; |
| 60 | + return { url, init, headers: new Headers(init.headers) }; |
| 61 | +}; |
| 62 | + |
| 63 | +describe("fetchWithAuth", () => { |
| 64 | + test("attaches the user's token to a same-origin path", async () => { |
| 65 | + stubBrowser(); |
| 66 | + const base44 = createTestClient("user-token"); |
| 67 | + |
| 68 | + await base44.fetchWithAuth("/api/orders"); |
| 69 | + |
| 70 | + const { url, headers } = lastCall(); |
| 71 | + expect(url).toBe(`${origin}/api/orders`); |
| 72 | + expect(headers.get("Authorization")).toBe("Bearer user-token"); |
| 73 | + }); |
| 74 | + |
| 75 | + test("reads the token from local storage when the client was created without one", async () => { |
| 76 | + stubBrowser(makeLocalStorage({ base44_access_token: "stored-token" })); |
| 77 | + const base44 = createTestClient(); |
| 78 | + |
| 79 | + await base44.fetchWithAuth("/api/orders"); |
| 80 | + |
| 81 | + expect(lastCall().headers.get("Authorization")).toBe("Bearer stored-token"); |
| 82 | + }); |
| 83 | + |
| 84 | + test("uses the token set after login", async () => { |
| 85 | + stubBrowser(); |
| 86 | + const base44 = createTestClient("old-token"); |
| 87 | + |
| 88 | + base44.setToken("new-token"); |
| 89 | + await base44.fetchWithAuth("/api/orders"); |
| 90 | + |
| 91 | + expect(lastCall().headers.get("Authorization")).toBe("Bearer new-token"); |
| 92 | + }); |
| 93 | + |
| 94 | + test("sends no auth header after logout", async () => { |
| 95 | + stubBrowser(); |
| 96 | + const base44 = createTestClient("user-token"); |
| 97 | + |
| 98 | + base44.auth.logout(); |
| 99 | + // logout() navigates the page; the test keeps the stubbed location usable. |
| 100 | + (globalThis as any).window.location.href = `${origin}/dashboard`; |
| 101 | + await base44.fetchWithAuth("/api/orders"); |
| 102 | + |
| 103 | + expect(lastCall().headers.get("Authorization")).toBeNull(); |
| 104 | + }); |
| 105 | + |
| 106 | + test("sends no auth header when no user is signed in", async () => { |
| 107 | + stubBrowser(); |
| 108 | + const base44 = createTestClient(); |
| 109 | + |
| 110 | + await base44.fetchWithAuth("/api/public"); |
| 111 | + |
| 112 | + expect(lastCall().headers.get("Authorization")).toBeNull(); |
| 113 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 114 | + }); |
| 115 | + |
| 116 | + test("forwards init options and keeps a caller-set Authorization header", async () => { |
| 117 | + stubBrowser(); |
| 118 | + const base44 = createTestClient("user-token"); |
| 119 | + |
| 120 | + await base44.fetchWithAuth("/api/orders", { |
| 121 | + method: "POST", |
| 122 | + headers: { |
| 123 | + "Content-Type": "application/json", |
| 124 | + Authorization: "Bearer caller-token", |
| 125 | + }, |
| 126 | + body: JSON.stringify({ productId: "abc" }), |
| 127 | + }); |
| 128 | + |
| 129 | + const { init, headers } = lastCall(); |
| 130 | + expect(init.method).toBe("POST"); |
| 131 | + expect(init.body).toBe(JSON.stringify({ productId: "abc" })); |
| 132 | + expect(headers.get("Content-Type")).toBe("application/json"); |
| 133 | + expect(headers.get("Authorization")).toBe("Bearer caller-token"); |
| 134 | + }); |
| 135 | + |
| 136 | + test("resolves a path relative to the current page", async () => { |
| 137 | + stubBrowser(); |
| 138 | + const base44 = createTestClient("user-token"); |
| 139 | + |
| 140 | + await base44.fetchWithAuth("api/orders"); |
| 141 | + |
| 142 | + expect(lastCall().url).toBe(`${origin}/api/orders`); |
| 143 | + }); |
| 144 | + |
| 145 | + test.each([ |
| 146 | + ["an absolute URL", "https://evil.example/steal"], |
| 147 | + ["a protocol-relative path", "//evil.example/steal"], |
| 148 | + ["a backslash-prefixed path", "/\\evil.example/steal"], |
| 149 | + ["an absolute URL on another port", `${origin}:8443/api/orders`], |
| 150 | + ])("rejects %s", async (_label, path) => { |
| 151 | + stubBrowser(); |
| 152 | + const base44 = createTestClient("user-token"); |
| 153 | + |
| 154 | + await expect(base44.fetchWithAuth(path)).rejects.toThrow( |
| 155 | + /only sends requests to your app's own origin/ |
| 156 | + ); |
| 157 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 158 | + }); |
| 159 | + |
| 160 | + test("rejects an empty path", async () => { |
| 161 | + stubBrowser(); |
| 162 | + const base44 = createTestClient("user-token"); |
| 163 | + |
| 164 | + await expect(base44.fetchWithAuth("")).rejects.toThrow(/requires a path/); |
| 165 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 166 | + }); |
| 167 | + |
| 168 | + test("throws outside the browser", async () => { |
| 169 | + const base44 = createTestClient("user-token"); |
| 170 | + |
| 171 | + await expect(base44.fetchWithAuth("/api/orders")).rejects.toThrow( |
| 172 | + /only available in the browser/ |
| 173 | + ); |
| 174 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 175 | + }); |
| 176 | +}); |
0 commit comments