fixed bug where fleet server instructions were incorrectly shown (#125357)

This commit is contained in:
Julia Bardi 2022-02-11 14:46:06 +01:00 committed by GitHub
parent a81b11e0fb
commit 2d93bcb085
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 3 deletions

View file

@ -13,6 +13,7 @@ jest.mock('../../hooks/use_request', () => {
sendGetFleetStatus: jest.fn(),
sendGetOneAgentPolicy: jest.fn(),
useGetAgents: jest.fn(),
useGetAgentPolicies: jest.fn(),
};
});

View file

@ -21,6 +21,7 @@ import {
sendGetFleetStatus,
sendGetOneAgentPolicy,
useGetAgents,
useGetAgentPolicies,
} from '../../hooks/use_request';
import { FleetStatusProvider, ConfigContext } from '../../hooks';
@ -101,6 +102,10 @@ describe('<AgentEnrollmentFlyout />', () => {
data: { items: [{ policy_id: 'fleet-server-policy' }] },
});
(useGetAgentPolicies as jest.Mock).mockReturnValue?.({
data: { items: [{ id: 'fleet-server-policy' }] },
});
await act(async () => {
testBed = await setup({
agentPolicies: [{ id: 'fleet-server-policy' } as AgentPolicy],
@ -143,6 +148,25 @@ describe('<AgentEnrollmentFlyout />', () => {
});
});
describe('with a specific policy when no agentPolicies set', () => {
beforeEach(async () => {
jest.clearAllMocks();
await act(async () => {
testBed = await setup({
agentPolicy: testAgentPolicy,
onClose: jest.fn(),
});
testBed.component.update();
});
});
it('should not show fleet server instructions', () => {
const { exists } = testBed;
expect(exists('agentEnrollmentFlyout')).toBe(true);
expect(AgentEnrollmentKeySelectionStep).toHaveBeenCalled();
});
});
// Skipped due to implementation details in the step components. See https://github.com/elastic/kibana/issues/103894
describe.skip('"View data" extension point', () => {
it('shows the "View data" step when UI extension is provided', async () => {

View file

@ -11,7 +11,13 @@ import type { EuiContainedStepProps } from '@elastic/eui/src/components/steps/st
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import { useGetOneEnrollmentAPIKey, useLink, useFleetStatus, useGetAgents } from '../../hooks';
import {
useGetOneEnrollmentAPIKey,
useLink,
useFleetStatus,
useGetAgents,
useGetAgentPolicies,
} from '../../hooks';
import { ManualInstructions } from '../../components/enrollment_instructions';
import {
@ -81,14 +87,24 @@ export const ManagedInstructions = React.memo<Props>(
showInactive: false,
});
const { data: agentPoliciesData, isLoading: isLoadingAgentPolicies } = useGetAgentPolicies({
page: 1,
perPage: 1000,
full: true,
});
const fleetServers = useMemo(() => {
const fleetServerAgentPolicies: string[] = (agentPolicies ?? [])
let policies = agentPolicies;
if (!agentPolicies && !isLoadingAgentPolicies) {
policies = agentPoliciesData?.items;
}
const fleetServerAgentPolicies: string[] = (policies ?? [])
.filter((pol) => policyHasFleetServer(pol))
.map((pol) => pol.id);
return (agents?.items ?? []).filter((agent) =>
fleetServerAgentPolicies.includes(agent.policy_id ?? '')
);
}, [agents, agentPolicies]);
}, [agents, agentPolicies, agentPoliciesData, isLoadingAgentPolicies]);
const fleetServerSteps = useMemo(() => {
const {