From 276850e13e12531aa9b6d937f8e6fe78a51994cd Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 18 Jan 2022 10:46:35 -0500 Subject: [PATCH] [Fleet] Do not display add agent for managed policy in integration details (#123160) (#123232) (cherry picked from commit 52d4475430a0bc2815cc3f2d4686edd9b522d9c0) Co-authored-by: Nicolas Chaulet --- .../package_policy_agents_cell.test.tsx | 17 +++++++++++++++-- .../components/package_policy_agents_cell.tsx | 9 +++++---- .../detail/policies/package_policies.tsx | 2 +- .../package_policy_actions_menu.test.tsx | 18 ++++++++++++++++++ .../components/package_policy_actions_menu.tsx | 2 +- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/components/package_policy_agents_cell.test.tsx b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/components/package_policy_agents_cell.test.tsx index 6cb9aab005e777..ce1de40f1d59c7 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/components/package_policy_agents_cell.test.tsx +++ b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/components/package_policy_agents_cell.test.tsx @@ -10,12 +10,13 @@ import React from 'react'; import { act } from '@testing-library/react'; import { createIntegrationsTestRendererMock } from '../../../../../../../../mock'; +import type { AgentPolicy } from '../../../../../../types'; import { PackagePolicyAgentsCell } from './package_policy_agents_cell'; function renderCell({ agentCount = 0, - agentPolicyId = '123', + agentPolicy = {} as AgentPolicy, onAddAgent = () => {}, hasHelpPopover = false, }) { @@ -24,7 +25,7 @@ function renderCell({ return renderer.render( @@ -39,6 +40,18 @@ describe('PackagePolicyAgentsCell', () => { }); }); + test('it should not display add agent if policy is managed', async () => { + const utils = renderCell({ + agentCount: 0, + agentPolicy: { + is_managed: true, + } as AgentPolicy, + }); + await act(async () => { + expect(utils.queryByText('Add agent')).not.toBeInTheDocument(); + }); + }); + test('it should display only count if count > 0', async () => { const utils = renderCell({ agentCount: 9999 }); await act(async () => { diff --git a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/components/package_policy_agents_cell.tsx b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/components/package_policy_agents_cell.tsx index fc3007b174ced5..cecd007a7b84ee 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/components/package_policy_agents_cell.tsx +++ b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/components/package_policy_agents_cell.tsx @@ -11,6 +11,7 @@ import { EuiButton } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { LinkedAgentCount, AddAgentHelpPopover } from '../../../../../../components'; +import type { AgentPolicy } from '../../../../../../types'; const AddAgentButton = ({ onAddAgent }: { onAddAgent: () => void }) => ( @@ -38,21 +39,21 @@ const AddAgentButtonWithPopover = ({ onAddAgent }: { onAddAgent: () => void }) = }; export const PackagePolicyAgentsCell = ({ - agentPolicyId, + agentPolicy, agentCount = 0, onAddAgent, hasHelpPopover = false, }: { - agentPolicyId: string; + agentPolicy: AgentPolicy; agentCount?: number; hasHelpPopover?: boolean; onAddAgent: () => void; }) => { - if (agentCount > 0) { + if (agentCount > 0 || agentPolicy.is_managed) { return ( ); diff --git a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/package_policies.tsx b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/package_policies.tsx index 9895016641f9a5..2bc28382399c92 100644 --- a/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/package_policies.tsx +++ b/x-pack/plugins/fleet/public/applications/integrations/sections/epm/screens/detail/policies/package_policies.tsx @@ -298,7 +298,7 @@ export const PackagePoliciesPage = ({ name, version }: PackagePoliciesPanelProps render({ agentPolicy, packagePolicy }: InMemoryPackagePolicyAndAgentPolicy) { return ( setFlyoutOpenForPolicyId(agentPolicy.id)} hasHelpPopover={showAddAgentHelpForPackagePolicyId === packagePolicy.id} diff --git a/x-pack/plugins/fleet/public/components/package_policy_actions_menu.test.tsx b/x-pack/plugins/fleet/public/components/package_policy_actions_menu.test.tsx index 63a3a93b7e8943..220e5ddb8f547b 100644 --- a/x-pack/plugins/fleet/public/components/package_policy_actions_menu.test.tsx +++ b/x-pack/plugins/fleet/public/components/package_policy_actions_menu.test.tsx @@ -119,3 +119,21 @@ test('Should be able to delete integration from a non-managed policy', async () expect(utils.queryByText('Delete integration')).not.toBeNull(); }); }); + +test('Should show add button if the policy is not managed and showAddAgent=true', async () => { + const agentPolicy = createMockAgentPolicy(); + const packagePolicy = createMockPackagePolicy({ hasUpgrade: true }); + const { utils } = renderMenu({ agentPolicy, packagePolicy, showAddAgent: true }); + await act(async () => { + expect(utils.queryByText('Add agent')).not.toBeNull(); + }); +}); + +test('Should not show add button if the policy is managed and showAddAgent=true', async () => { + const agentPolicy = createMockAgentPolicy({ is_managed: true }); + const packagePolicy = createMockPackagePolicy({ hasUpgrade: true }); + const { utils } = renderMenu({ agentPolicy, packagePolicy, showAddAgent: true }); + await act(async () => { + expect(utils.queryByText('Add agent')).toBeNull(); + }); +}); diff --git a/x-pack/plugins/fleet/public/components/package_policy_actions_menu.tsx b/x-pack/plugins/fleet/public/components/package_policy_actions_menu.tsx index 754a3fb1c76b56..8cbfd40bbbf556 100644 --- a/x-pack/plugins/fleet/public/components/package_policy_actions_menu.tsx +++ b/x-pack/plugins/fleet/public/components/package_policy_actions_menu.tsx @@ -57,7 +57,7 @@ export const PackagePolicyActionsMenu: React.FunctionComponent<{ // defaultMessage="View integration" // /> // , - ...(showAddAgent + ...(showAddAgent && !agentPolicy.is_managed ? [