Skip to content

Commit

Permalink
[Fleet] Do not display add agent for managed policy in integration de…
Browse files Browse the repository at this point in the history
…tails (elastic#123160)
  • Loading branch information
nchaulet committed Jan 18, 2022
1 parent b606054 commit 52d4475
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}) {
Expand All @@ -24,7 +25,7 @@ function renderCell({
return renderer.render(
<PackagePolicyAgentsCell
agentCount={agentCount}
agentPolicyId={agentPolicyId}
agentPolicy={agentPolicy}
onAddAgent={onAddAgent}
hasHelpPopover={hasHelpPopover}
/>
Expand All @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => (
<EuiButton iconType="plusInCircle" data-test-subj="addAgentButton" onClick={onAddAgent} size="s">
Expand Down Expand Up @@ -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 (
<LinkedAgentCount
count={agentCount}
agentPolicyId={agentPolicyId}
agentPolicyId={agentPolicy.id}
className="eui-textTruncate"
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export const PackagePoliciesPage = ({ name, version }: PackagePoliciesPanelProps
render({ agentPolicy, packagePolicy }: InMemoryPackagePolicyAndAgentPolicy) {
return (
<PackagePolicyAgentsCell
agentPolicyId={agentPolicy.id}
agentPolicy={agentPolicy}
agentCount={agentPolicy.agents}
onAddAgent={() => setFlyoutOpenForPolicyId(agentPolicy.id)}
hasHelpPopover={showAddAgentHelpForPackagePolicyId === packagePolicy.id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const PackagePolicyActionsMenu: React.FunctionComponent<{
// defaultMessage="View integration"
// />
// </EuiContextMenuItem>,
...(showAddAgent
...(showAddAgent && !agentPolicy.is_managed
? [
<EuiContextMenuItem
icon="plusInCircle"
Expand Down

0 comments on commit 52d4475

Please sign in to comment.