|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "vitest"; |
| 2 | +import nock from "nock"; |
| 3 | +import { Base44Error, createClient } from "../../src/index.ts"; |
| 4 | + |
| 5 | +describe("App module", () => { |
| 6 | + const appId = "test-app-id"; |
| 7 | + const serverUrl = "https://base44.app"; |
| 8 | + const token = "user-token-456"; |
| 9 | + const publicSettingsPath = `/api/apps/public/prod/public-settings/by-id/${appId}`; |
| 10 | + let base44: ReturnType<typeof createClient>; |
| 11 | + let scope: nock.Scope; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + base44 = createClient({ serverUrl, appId, token }); |
| 15 | + scope = nock(serverUrl); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + nock.cleanAll(); |
| 20 | + }); |
| 21 | + |
| 22 | + test("getPublicSettings returns the app id and its access policy", async () => { |
| 23 | + scope |
| 24 | + .get(publicSettingsPath) |
| 25 | + .reply(200, { id: appId, public_settings: "public_without_login" }); |
| 26 | + |
| 27 | + const settings = await base44.app.getPublicSettings(); |
| 28 | + |
| 29 | + expect(settings).toEqual({ |
| 30 | + id: appId, |
| 31 | + public_settings: "public_without_login", |
| 32 | + }); |
| 33 | + expect(scope.isDone()).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + test("getPublicSettings authenticates with the client's token, so callers never handle it", async () => { |
| 37 | + scope |
| 38 | + .get(publicSettingsPath) |
| 39 | + .matchHeader("Authorization", `Bearer ${token}`) |
| 40 | + .reply(200, { id: appId, public_settings: "private_with_login" }); |
| 41 | + |
| 42 | + await base44.app.getPublicSettings(); |
| 43 | + |
| 44 | + expect(scope.isDone()).toBe(true); |
| 45 | + }); |
| 46 | + |
| 47 | + test("getPublicSettings sends no Authorization header for an anonymous client", async () => { |
| 48 | + const anonymous = createClient({ serverUrl, appId }); |
| 49 | + |
| 50 | + scope |
| 51 | + .get(publicSettingsPath) |
| 52 | + .matchHeader("Authorization", (value) => value === undefined) |
| 53 | + .reply(200, { id: appId, public_settings: "public_without_login" }); |
| 54 | + |
| 55 | + await anonymous.app.getPublicSettings(); |
| 56 | + |
| 57 | + expect(scope.isDone()).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + test.each([ |
| 61 | + ["auth_required", "the visitor must sign in"], |
| 62 | + ["user_not_registered", "the visitor has no access to this app"], |
| 63 | + ])( |
| 64 | + "getPublicSettings surfaces a 403 %s as a Base44Error carrying the reason", |
| 65 | + async (reason) => { |
| 66 | + scope |
| 67 | + .get(publicSettingsPath) |
| 68 | + .reply(403, { extra_data: { app_id: appId, reason } }); |
| 69 | + |
| 70 | + const error = await base44.app |
| 71 | + .getPublicSettings() |
| 72 | + .catch((rejection) => rejection); |
| 73 | + |
| 74 | + expect(error).toBeInstanceOf(Base44Error); |
| 75 | + expect(error.status).toBe(403); |
| 76 | + expect(error.data.extra_data.reason).toBe(reason); |
| 77 | + } |
| 78 | + ); |
| 79 | +}); |
0 commit comments