Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

4.7 KB
Raw Blame
import { createDataAppSandbox } from "metabase-enterprise/data-apps/sandbox/sandbox";

import {
  DataAppBundleError,
  fetchDataAppBundleCode,
  instantiateDataAppBundle,
} from "./loader";

jest.mock("metabase-enterprise/data-apps/sandbox/sandbox", () => ({
  ...jest.requireActual("metabase-enterprise/data-apps/sandbox/sandbox"),
  createDataAppSandbox: jest.fn(),
}));

const ALLOWED_HOSTS_HEADER = "X-Metabase-Data-App-Allowed-Hosts";

const mockedCreateSandbox = jest.mocked(createDataAppSandbox);

describe("fetchDataAppBundleCode", () => {
  const originalFetch = global.fetch;

  const setup = (response: Response | Error) => {
    const fetchMock = jest.fn(
      (_input: RequestInfo | URL, _init?: RequestInit) =>
        response instanceof Error
          ? Promise.reject(response)
          : Promise.resolve(response),
    );

    global.fetch = fetchMock;

    return { fetchMock };
  };

  afterEach(() => {
    global.fetch = originalFetch;
    jest.clearAllMocks();
  });

  it("requests the bundle endpoint with cache disabled and the name encoded", async () => {
    const { fetchMock } = setup(new Response("CODE"));

    await fetchDataAppBundleCode("my app");

    const [url, init] = fetchMock.mock.calls[0];

    expect(url).toContain("/api/ee/apps/my%20app/bundle");
    expect(init).toEqual({ cache: "no-store" });
  });

  it.each<[string, string | undefined, string[]]>([
    [
      "a JSON array",
      JSON.stringify(["https://api.example.com", "https://*.acme.com"]),
      ["https://api.example.com", "https://*.acme.com"],
    ],
    ["absent", undefined, []],
    ["invalid JSON", "{", []],
    ["a non-array value", JSON.stringify({ nope: true }), []],
    [
      "mixed entries (non-strings dropped)",
      JSON.stringify(["https://ok.com", 42, null]),
      ["https://ok.com"],
    ],
  ])(
    "returns the code and parses allowed_hosts when the header is %s",
    async (_desc, header, expected) => {
      setup(
        new Response(
          "CODE",
          header === undefined
            ? undefined
            : { headers: { [ALLOWED_HOSTS_HEADER]: header } },
        ),
      );

      await expect(fetchDataAppBundleCode("demo")).resolves.toEqual({
        code: "CODE",
        allowedHosts: expected,
      });
    },
  );

  it("throws a DataAppBundleError carrying the HTTP status on a non-2xx response", async () => {
    setup(new Response("nope", { status: 404 }));

    await expect(fetchDataAppBundleCode("demo")).rejects.toMatchObject({
      name: "DataAppBundleError",
      status: 404,
    });
  });

  it("throws a DataAppBundleError with no status when the request never reaches the server", async () => {
    setup(new Error("offline"));

    const error = await fetchDataAppBundleCode("demo").catch((e) => e);
    expect(error).toBeInstanceOf(DataAppBundleError);
    expect(error.status).toBeUndefined();
    expect(error.message).toMatch(/Failed to reach the server/);
  });
});

describe("instantiateDataAppBundle", () => {
  const setup = (factoryResult: unknown) => {
    // Sandbox creation is async — Near-Membrane defers setup to the realm
    // iframe's `load` event once `iframeSrc` is set.
    mockedCreateSandbox.mockResolvedValue({
      // @ts-expect-error - the factory result is deliberately untyped: these tests
      // feed the loader malformed values (`null`, a non-function `component`).
      evaluate: () => () => factoryResult,
    });

    return {
      instantiate: () => instantiateDataAppBundle("code", "demo", window, []),
    };
  };

  afterEach(() => jest.clearAllMocks());

  it("points the sandbox at the realm-host document", async () => {
    const { instantiate } = setup({ component: () => null });

    await instantiate();

    expect(mockedCreateSandbox).toHaveBeenCalledWith(
      expect.objectContaining({ realmHostUrl: "/api/ee/apps/sandbox-host" }),
    );
  });

  it("returns the component and narrows providerProps to the allowed keys", async () => {
    const component = () => null;
    const { instantiate } = setup({
      component,
      providerProps: {
        theme: { colors: {} },
        allowedCustomVisualizations: ["funnel"],
        // Not an allowed key — must be dropped.
        authConfig: { metabaseInstanceUrl: "https://evil" },
      },
    });

    const loaded = await instantiate();

    expect(loaded.component).toBe(component);
    expect(loaded.providerProps).toEqual({
      theme: { colors: {} },
      allowedCustomVisualizations: ["funnel"],
    });
  });

  it.each<unknown>([{ component: "not a function" }, null])(
    "throws when the factory returns %p",
    async (factoryResult) => {
      const { instantiate } = setup(factoryResult);

      await expect(instantiate()).rejects.toThrow(
        /missing a `component` function/,
      );
    },
  );
});