The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
GDGT-2617 Hide Security Center setup prompt from non-admins (#75690)
Stas Gavryloveacd80a882d5
diff --git a/enterprise/frontend/src/metabase-enterprise/security_center/components/SecurityCenterPromoCard/SecurityCenterPromoCard.tsx b/enterprise/frontend/src/metabase-enterprise/security_center/components/SecurityCenterPromoCard/SecurityCenterPromoCard.tsxindex 72d2b0f7add..a0e06a1f266 100644--- a/enterprise/frontend/src/metabase-enterprise/security_center/components/SecurityCenterPromoCard/SecurityCenterPromoCard.tsx+++ b/enterprise/frontend/src/metabase-enterprise/security_center/components/SecurityCenterPromoCard/SecurityCenterPromoCard.tsx@@ -1,3 +1,4 @@+import { skipToken } from "@reduxjs/toolkit/query/react";import { useCallback, useState } from "react";import { t } from "ttag";@@ -8,6 +9,8 @@ import {import { useSetting } from "metabase/common/hooks";import { getPlan } from "metabase/common/utils/plan";import { NavbarPromoCard } from "metabase/nav/components/NavbarPromoCard";+import { useSelector } from "metabase/redux";+import { getUserIsAdmin } from "metabase/selectors/user";import { Icon } from "metabase/ui";import { isAffected } from "../../utils";@@ -28,14 +31,20 @@ function useDismissed() {}export function SecurityCenterPromoCard() {+ const isAdmin = useSelector(getUserIsAdmin);const tokenFeatures = useSetting("token-features");const plan = getPlan(tokenFeatures);const { data: channelInfo, isLoading: isChannelInfoLoading } =- useGetChannelInfoQuery();+ useGetChannelInfoQuery(isAdmin ? undefined : skipToken);const { data: advisoriesResponse, isLoading: isAdvisoriesLoading } =- useListSecurityAdvisoriesQuery();+ useListSecurityAdvisoriesQuery(isAdmin ? undefined : skipToken);const { dismissed, dismiss } = useDismissed();+ // The promo links to /admin/security-center, so only admins should see it.+ if (!isAdmin) {+ return null;+ }+if (plan !== "pro-self-hosted") {return null;}diff --git a/enterprise/frontend/src/metabase-enterprise/security_center/components/SecurityCenterPromoCard/SecurityCenterPromoCard.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/security_center/components/SecurityCenterPromoCard/SecurityCenterPromoCard.unit.spec.tsxindex aa5ca05dbc8..4a0056406fc 100644--- a/enterprise/frontend/src/metabase-enterprise/security_center/components/SecurityCenterPromoCard/SecurityCenterPromoCard.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/security_center/components/SecurityCenterPromoCard/SecurityCenterPromoCard.unit.spec.tsx@@ -1,9 +1,11 @@+import userEvent from "@testing-library/user-event";import fetchMock from "fetch-mock";import { Route } from "react-router";import { setupNotificationChannelsEndpoints } from "__support__/server-mocks";import { mockSettings } from "__support__/settings";import { renderWithProviders, screen, waitFor } from "__support__/ui";+import { securityCenterApi, subscriptionApi } from "metabase/api";import { createMockState } from "metabase/redux/store/mocks";import type { Advisory } from "metabase-types/api";import {@@ -17,6 +19,7 @@ import { SecurityCenterPromoCard } from "./SecurityCenterPromoCard";const DISMISSED_KEY = "security-center-promo-dismissed";interface SetupOpts {+ isAdmin?: boolean;isProSelfHosted?: boolean;emailConfigured?: boolean;slackConfigured?: boolean;@@ -24,6 +27,7 @@ interface SetupOpts {}function setup({+ isAdmin = true,isProSelfHosted = true,emailConfigured = false,slackConfigured = false,@@ -36,8 +40,8 @@ function setup({);setupNotificationChannelsEndpoints({- email: { configured: emailConfigured } as any,- slack: { configured: slackConfigured } as any,+ email: { configured: emailConfigured },+ slack: { configured: slackConfigured },});fetchMock.get("path:/api/ee/security-center", {@@ -46,19 +50,50 @@ function setup({});const state = createMockState({- currentUser: createMockUser({ is_superuser: true }),+ currentUser: createMockUser({ is_superuser: isAdmin }),settings: mockSettings({"token-features": tokenFeatures,}),});- renderWithProviders(<Route path="*" component={SecurityCenterPromoCard} />, {- initialRoute: "/",- storeInitialState: state,- withRouter: true,+ return renderWithProviders(+ <Route path="*" component={SecurityCenterPromoCard} />,+ {+ initialRoute: "/",+ storeInitialState: state,+ withRouter: true,+ },+ );+}++type SetupResult = ReturnType<typeof setup>;++async function waitForAdminQueriesToFinish({ store }: SetupResult) {+ await waitFor(() => {+ expect(+ subscriptionApi.endpoints.getChannelInfo.select()(store.getState())+ .isSuccess,+ ).toBe(true);+ expect(+ securityCenterApi.endpoints.listSecurityAdvisories.select()(+ store.getState(),+ ).isSuccess,+ ).toBe(true);});}+function expectAdminQueriesToBeSkipped({ store }: SetupResult) {+ expect(+ subscriptionApi.endpoints.getChannelInfo.select()(store.getState())+ .isUninitialized,+ ).toBe(true);+ expect(+ securityCenterApi.endpoints.listSecurityAdvisories.select()(+ store.getState(),+ ).isUninitialized,+ ).toBe(true);+}+describe("SecurityCenterPromoCard", () => {afterEach(() => {localStorage.removeItem(DISMISSED_KEY);@@ -76,38 +111,52 @@ describe("SecurityCenterPromoCard", () => {});it("does not render when email is configured", async () => {- setup({ emailConfigured: true });+ const view = setup({ emailConfigured: true });- await screen.findByText(() => false).catch(() => {});+ await waitForAdminQueriesToFinish(view);expect(screen.queryByText(/Stay safe with security alerts/),).not.toBeInTheDocument();});it("does not render when slack is configured", async () => {- setup({ slackConfigured: true });+ const view = setup({ slackConfigured: true });- await screen.findByText(() => false).catch(() => {});+ await waitForAdminQueriesToFinish(view);expect(screen.queryByText(/Stay safe with security alerts/),).not.toBeInTheDocument();});+ it("does not render or fire admin-only requests for non-admin users", () => {+ const view = setup({ isAdmin: false });++ expectAdminQueriesToBeSkipped(view);+ expect(+ screen.queryByText(/Stay safe with security alerts/),+ ).not.toBeInTheDocument();++ // Non-admins must not trigger admin-only endpoints.+ expect(fetchMock.callHistory.called("path:/api/ee/security-center")).toBe(+ false,+ );+ });+it("does not render for non-pro-self-hosted plans", async () => {- setup({ isProSelfHosted: false });+ const view = setup({ isProSelfHosted: false });- await screen.findByText(() => false).catch(() => {});+ await waitForAdminQueriesToFinish(view);expect(screen.queryByText(/Stay safe with security alerts/),).not.toBeInTheDocument();});it("does not render when there is an active advisory (red banner takes over)", async () => {- setup({+ const view = setup({advisories: [createAdvisory({ match_status: "active" })],});- await screen.findByText(() => false).catch(() => {});+ await waitForAdminQueriesToFinish(view);expect(screen.queryByText(/Stay safe with security alerts/),).not.toBeInTheDocument();@@ -118,7 +167,7 @@ describe("SecurityCenterPromoCard", () => {await screen.findByText(/Stay safe with security alerts/);const close = screen.getByRole("button", { name: /close/i });- close.click();+ await userEvent.click(close);await waitFor(() => {expect(@@ -131,9 +180,9 @@ describe("SecurityCenterPromoCard", () => {it("stays hidden after dismissal", async () => {localStorage.setItem(DISMISSED_KEY, "true");- setup();+ const view = setup();- await screen.findByText(() => false).catch(() => {});+ await waitForAdminQueriesToFinish(view);expect(screen.queryByText(/Stay safe with security alerts/),).not.toBeInTheDocument();