Skip to content

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

15.2 KB
Raw Blame
import userEvent from "@testing-library/user-event";
import fetchMock from "fetch-mock";

import {
  setupRecentViewsAndSelectionsEndpoints,
  setupSettingEndpoint,
} from "__support__/server-mocks";
import { createMockSettingsState } from "__support__/state";
import { renderWithProviders, screen, within } from "__support__/ui";
import { getEolReachedMessage } from "metabase/admin/settings/components/widgets/VersionUpdateNotice/utils";
import { Route } from "metabase/router";
import type { Advisory, VersionInfo } from "metabase-types/api";
import {
  createMockMajorVersionSupport,
  createMockVersion,
  createMockVersionInfo,
} from "metabase-types/api/mocks";
import { createAdvisory } from "metabase-types/api/mocks/security-center";

import * as notificationHook from "../../hooks/use-notification-config";
import * as advisoriesHook from "../../hooks/use-security-advisories";

import { SecurityCenterPage } from "./SecurityCenterPage";

jest.mock("metabase/admin/components/AdminLayout/AdminSettingsLayout", () => ({
  AdminSettingsLayout: ({ children }: { children: React.ReactNode }) => (
    <div data-testid="admin-settings-layout">{children}</div>
  ),
}));

const mockAcknowledge = jest.fn();
const mockAcknowledgeAll = jest.fn();

const PAST_EOL = "2000-06-01";

function setup(
  advisories: Advisory[] = [],
  {
    lastCheckedAt = null,
    initialRoute = "/",
    isError = false,
    versionInfo,
  }: {
    lastCheckedAt?: string | null;
    initialRoute?: string;
    isError?: boolean;
    versionInfo?: VersionInfo;
  } = {},
) {
  jest.spyOn(advisoriesHook, "useSecurityAdvisories").mockReturnValue({
    data: advisories,
    lastCheckedAt,
    isLoading: false,
    isError,
    acknowledgeAdvisory: mockAcknowledge,
    acknowledgeAdvisories: mockAcknowledgeAll,
  });

  setupRecentViewsAndSelectionsEndpoints([], ["selections"]);
  setupSettingEndpoint({
    settingKey: "version-info",
    settingValue: versionInfo ?? createMockVersionInfo(),
  });
  jest.spyOn(notificationHook, "useNotificationConfigState").mockReturnValue({
    config: {
      email: {
        sendToAllAdmins: true,
        handler: { channel_type: "channel/email", recipients: [] },
      },
      slack: {
        enabled: false,
        handler: { channel_type: "channel/slack", recipients: [] },
      },
    },
    users: [],
    channels: undefined,
    updateEmailHandler: jest.fn(),
    toggleSendToAllAdmins: jest.fn(),
    updateSlackHandler: jest.fn(),
    toggleSlack: jest.fn(),
    save: jest.fn(),
    resetConfig: jest.fn(),
  });

  renderWithProviders(<Route path="*" element={<SecurityCenterPage />} />, {
    withRouter: true,
    initialRoute,
    storeInitialState: {
      settings: createMockSettingsState({
        version: createMockVersion({ tag: "v0.59.3" }),
      }),
    },
  });
}

describe("SecurityCenterPage", () => {
  afterEach(() => {
    jest.restoreAllMocks();
    mockAcknowledge.mockClear();
    mockAcknowledgeAll.mockClear();
  });

  it("renders the title and current version", async () => {
    setup();

    expect(screen.getByText("Security Center")).toBeInTheDocument();
    expect(screen.getByTestId("current-version")).toHaveTextContent("v0.59.3");
  });

  it("renders the error state within the admin settings layout (GDGT-2748)", async () => {
    setup([], { isError: true });

    expect(
      screen.getByText("Something went wrong loading security advisories."),
    ).toBeInTheDocument();
    expect(screen.getByTestId("admin-settings-layout")).toBeInTheDocument();
  });

  it("renders the empty state when there are no advisories", async () => {
    setup([]);

    expect(
      screen.getByText(
        /No known security issues that impact your Metabase version/,
      ),
    ).toBeInTheDocument();
  });

  it("renders advisory cards in the correct order (affected first, then by severity)", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "1",
        title: "Low not affected",
        severity: "low",
        match_status: "not_affected",
      }),
      createAdvisory({
        advisory_id: "2",
        title: "Critical affected",
        severity: "critical",
        match_status: "active",
      }),
      createAdvisory({
        advisory_id: "3",
        title: "High affected",
        severity: "high",
        match_status: "active",
      }),
    ];

    setup(advisories);

    const cards = screen.getAllByTestId("advisory-card");
    expect(cards).toHaveLength(3);

    expect(within(cards[0]).getByText("Critical affected")).toBeInTheDocument();
    expect(within(cards[1]).getByText("High affected")).toBeInTheDocument();
    expect(within(cards[2]).getByText("Low not affected")).toBeInTheDocument();
  });

  it("shows 'not affected' badge on non-affecting cards", async () => {
    const advisories = [
      createAdvisory({ advisory_id: "1", match_status: "active" }),
      createAdvisory({ advisory_id: "2", match_status: "not_affected" }),
    ];

    setup(advisories);

    expect(
      screen.getByText("Your instance is not affected"),
    ).toBeInTheDocument();
  });

  it("renders external links with correct targets", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "1",
        advisory_url: "https://example.com/advisory/1",
      }),
    ];

    setup(advisories);

    const advisoryLink = screen.getByText("View advisory");
    expect(advisoryLink).toHaveAttribute(
      "href",
      "https://example.com/advisory/1",
    );
    expect(advisoryLink).toHaveAttribute("target", "_blank");
  });

  it("renders a single download button for the fix matching the instance's major version", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "1",
        match_status: "active",
        affected_versions: [
          { min: "0.58.0", fixed: "0.58.11" },
          { min: "0.59.0", fixed: "0.59.11" },
        ],
        download_jar_urls: [
          { version: "0.58.11", url: "https://downloads.example.com/58.jar" },
          { version: "0.59.11", url: "https://downloads.example.com/59.jar" },
        ],
      }),
    ];

    setup(advisories);

    expect(screen.queryByText("Download v0.58.11")).not.toBeInTheDocument();
    const downloadButton = screen.getByRole("link", {
      name: /Download v0.59.11/,
    });
    expect(downloadButton).toHaveAttribute(
      "href",
      "https://downloads.example.com/59.jar",
    );
    expect(downloadButton).toHaveAttribute("target", "_blank");
  });

  it("does not render a download button when there is no matching download url", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "1",
        match_status: "active",
        affected_versions: [{ min: "0.59.0", fixed: "0.59.11" }],
        download_jar_urls: [],
      }),
    ];

    setup(advisories);

    expect(screen.queryByText("Download v0.59.11")).not.toBeInTheDocument();
  });

  it("does not render a download button when the instance is not affected", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "1",
        match_status: "not_affected",
        affected_versions: [{ min: "0.59.0", fixed: "0.59.11" }],
        download_jar_urls: [
          { version: "0.59.11", url: "https://downloads.example.com/59.jar" },
        ],
      }),
    ];

    setup(advisories);

    expect(screen.queryByText("Download v0.59.11")).not.toBeInTheDocument();
  });

  it("calls acknowledgeAdvisory when dismiss button is clicked", async () => {
    const advisories = [
      createAdvisory({ advisory_id: "SA-001", acknowledged_at: null }),
    ];

    setup(advisories);

    await userEvent.click(screen.getByTestId("acknowledge-button"));
    expect(mockAcknowledge).toHaveBeenCalledWith("SA-001");
  });

  it("calls acknowledgeAdvisories with only undismissed non-affecting advisory ids when 'Dismiss all' is clicked", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "SA-001",
        match_status: "active",
        severity: "critical",
      }),
      createAdvisory({
        advisory_id: "SA-002",
        match_status: "not_affected",
        severity: "medium",
      }),
      createAdvisory({
        advisory_id: "SA-003",
        match_status: "not_affected",
        severity: "low",
        acknowledged_at: "2026-03-01T00:00:00Z",
      }),
    ];

    setup(advisories);

    await userEvent.click(screen.getByText("Dismiss all"));
    expect(mockAcknowledgeAll).toHaveBeenCalledWith(["SA-002"]);
  });

  it("does not show 'Dismiss all' when there are no non-affecting advisories", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "SA-001",
        match_status: "active",
        severity: "critical",
      }),
    ];

    setup(advisories);

    expect(screen.queryByText("Dismiss all")).not.toBeInTheDocument();
  });

  it("does not show 'Dismiss all' when all non-affecting advisories are already dismissed", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "SA-001",
        match_status: "not_affected",
        acknowledged_at: "2026-03-01T00:00:00Z",
      }),
      createAdvisory({
        advisory_id: "SA-002",
        match_status: "not_affected",
        acknowledged_at: "2026-03-01T00:00:00Z",
      }),
    ];

    setup(advisories);

    // Enable showing dismissed advisories so they appear in the list
    await userEvent.click(screen.getByTestId("show-acknowledged-filter"));

    expect(screen.queryByText("Dismiss all")).not.toBeInTheDocument();
  });

  it("does not show dismiss button for already dismissed advisories", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "SA-001",
        acknowledged_at: "2026-03-01T00:00:00Z",
      }),
    ];

    setup(advisories);

    // Dismissed advisories are hidden by default — enable the checkbox first
    await userEvent.click(screen.getByTestId("show-acknowledged-filter"));

    const button = screen.getByTestId("acknowledge-button");
    expect(button).toBeDisabled();
    expect(button).toHaveTextContent("Dismissed");
  });

  it("hides dismissed advisories by default", async () => {
    const advisories = [
      createAdvisory({
        advisory_id: "1",
        title: "Visible",
        acknowledged_at: null,
      }),
      createAdvisory({
        advisory_id: "2",
        title: "Hidden",
        acknowledged_at: "2026-03-01T00:00:00Z",
      }),
    ];

    setup(advisories);

    expect(screen.getByText("Visible")).toBeInTheDocument();
    expect(screen.queryByText("Hidden")).not.toBeInTheDocument();
  });

  it("renders the filter bar", async () => {
    setup([]);

    expect(screen.getByTestId("advisory-filter-bar")).toBeInTheDocument();
  });

  describe("upgrade banner", () => {
    it("shows the upgrade banner when there are active advisories", async () => {
      const advisories = [
        createAdvisory({
          advisory_id: "1",
          match_status: "active",
          affected_versions: [{ min: "0.58.0", fixed: "0.59.4" }],
        }),
      ];

      setup(advisories);

      const banner = screen.getByTestId("upgrade-banner");
      expect(banner).toBeInTheDocument();
      expect(banner).toHaveTextContent("0.59.4");
      expect(banner).toHaveTextContent("A security update is available");
    });

    it("does not show the upgrade banner when there are no active advisories", async () => {
      const advisories = [
        createAdvisory({
          advisory_id: "1",
          match_status: "not_affected",
        }),
      ];

      setup(advisories);

      expect(screen.queryByTestId("upgrade-banner")).not.toBeInTheDocument();
    });

    it("shows the highest fixed version across multiple active advisories", async () => {
      const advisories = [
        createAdvisory({
          advisory_id: "1",
          match_status: "active",
          affected_versions: [{ min: "0.58.0", fixed: "0.59.2" }],
        }),
        createAdvisory({
          advisory_id: "2",
          match_status: "active",
          affected_versions: [{ min: "0.58.0", fixed: "0.59.5" }],
        }),
      ];

      setup(advisories);

      const banner = screen.getByTestId("upgrade-banner");
      expect(banner).toHaveTextContent("0.59.5");
    });

    it("includes a link to upgrade instructions", async () => {
      const advisories = [
        createAdvisory({
          advisory_id: "1",
          match_status: "active",
          affected_versions: [{ min: "0.58.0", fixed: "0.59.4" }],
        }),
      ];

      setup(advisories);

      const link = screen.getByText("View upgrade instructions");
      expect(link).toHaveAttribute(
        "href",
        "https://www.metabase.com/docs/latest/installation-and-operation/upgrading-metabase",
      );
      expect(link).toHaveAttribute("target", "_blank");
    });

    it("does not show the upgrade banner when there are no advisories", async () => {
      setup([]);

      expect(screen.queryByTestId("upgrade-banner")).not.toBeInTheDocument();
    });
  });

  describe("eol notice", () => {
    const pastEolVersionInfo = createMockVersionInfo({
      major_version_support: [
        createMockMajorVersionSupport({ major: 59, eol: PAST_EOL }),
      ],
    });

    it("shows the eol notice when the version is past eol and there is no upgrade target", async () => {
      setup([], { versionInfo: pastEolVersionInfo });

      const banner = await screen.findByTestId("upgrade-banner");
      expect(banner).toHaveTextContent(getEolReachedMessage());
    });

    it("does not show the eol notice when an upgrade target is available", () => {
      setup(
        [
          createAdvisory({
            advisory_id: "1",
            match_status: "active",
            affected_versions: [{ min: "0.58.0", fixed: "0.59.4" }],
          }),
        ],
        { versionInfo: pastEolVersionInfo },
      );

      expect(screen.getByTestId("upgrade-banner")).toBeInTheDocument();
      expect(
        screen.queryByText(getEolReachedMessage()),
      ).not.toBeInTheDocument();
    });
  });

  describe("notification settings modal", () => {
    it("opens the modal when the 'open=notifications' query param is set", async () => {
      setup([], { initialRoute: "/?open=notifications" });

      expect(
        await screen.findByRole("dialog", { name: "Notification settings" }),
      ).toBeInTheDocument();
    });

    it("does not open the modal when the query param is not set", async () => {
      setup();

      expect(
        screen.queryByRole("dialog", { name: "Notification settings" }),
      ).not.toBeInTheDocument();
    });
  });

  describe("sync polling", () => {
    it("disables the sync button while syncing", async () => {
      fetchMock.post("path:/api/ee/security-center/sync", 200);
      setup();

      await userEvent.click(screen.getByTestId("sync-advisories"));

      expect(screen.getByTestId("sync-advisories")).toBeDisabled();
    });

    it("passes isPolling to useSecurityAdvisories after sync is triggered", async () => {
      fetchMock.post("path:/api/ee/security-center/sync", 200);
      const spy = jest.spyOn(advisoriesHook, "useSecurityAdvisories");
      setup();

      // Before sync, the hook should be called without polling
      expect(spy).toHaveBeenCalledWith(false);

      await userEvent.click(screen.getByTestId("sync-advisories"));

      // After sync, the hook should be called with polling enabled
      expect(spy).toHaveBeenCalledWith(true);
    });
  });
});