Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -415,17 +415,19 @@ export const fleetGetAgentStatusHttpMock =
id: 'agentStatus',
path: AGENT_API_ROUTES.STATUS_PATTERN,
method: 'get',
handler: () => {
handler: (): GetAgentStatusResponse => {
return {
results: {
total: 50,
active: 50,
inactive: 5,
online: 40,
error: 0,
offline: 5,
updating: 0,
other: 0,
events: 0,
unenrolled: 0,
all: 0,
},
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { cloneDeep } from 'lodash';
import { set } from '@kbn/safer-lodash-set';
import { ProtectionModes } from '../../../../../../common/endpoint/types';
import { waitFor, cleanup } from '@testing-library/react';
import type { GetAgentStatusResponse } from '@kbn/fleet-plugin/common';
import { packagePolicyRouteService, API_VERSIONS } from '@kbn/fleet-plugin/common';
import { getPolicyDataForUpdate } from '../../../../../../common/endpoint/service/policy';
import { getDeferred } from '../../../../mocks/utils';
Expand All @@ -34,8 +35,7 @@ jest.mock('../../../../../common/components/user_privileges');

const useUserPrivilegesMock = _useUserPrivileges as jest.Mock;

// Failing: See https://github.com/elastic/kibana/issues/179984
describe.skip('When rendering PolicySettingsLayout', () => {
describe('When rendering PolicySettingsLayout', () => {
jest.setTimeout(15000);

const testSubj = getPolicySettingsFormTestSubjects();
Expand Down Expand Up @@ -84,6 +84,14 @@ describe.skip('When rendering PolicySettingsLayout', () => {
}
};

/**
* Performs a minimal number of updates to make 'Save' button enabled.
*/
const makeMinimalUpdates = async () => {
const { getByTestId } = renderResult;
await userEvent.click(getByTestId(testSubj.malware.enableDisableSwitch));
};

/**
* Makes updates to the policy form on the UI and return back a new (cloned) `PolicyData`
* with the updates reflected in it
Expand Down Expand Up @@ -114,9 +122,11 @@ describe.skip('When rendering PolicySettingsLayout', () => {
await userEvent.type(getByTestId(testSubj.ransomware.notifyCustomMessage), 'foo message');
set(policySettings, 'windows.popup.ransomware.message', 'foo message');

await userEvent.click(getByTestId(testSubj.advancedSection.showHideButton));
await userEvent.type(getByTestId('linux.advanced.agent.connection_delay'), '1000');
set(policySettings, 'linux.advanced.agent.connection_delay', '1000');
// skipping Advanced Options as changing them takes too long.
// todo: re-enable them with this issue: https://github.com/elastic/security-team/issues/11765
// await userEvent.click(getByTestId(testSubj.advancedSection.showHideButton));
// await userEvent.type(getByTestId('linux.advanced.agent.connection_delay'), '1000');
// set(policySettings, 'linux.advanced.agent.connection_delay', '1000');
Comment on lines +125 to +129

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test cases calling makeUpdates() used to timeout on CI, due to taking more time than 15 sec: #179984

on my machine, any of these test cases took ca. 4.5 sec alone.

by commenting out the lines above, it is reduced to 1.6 sec.

additionally, in test cases that don't need all changes, I've switched to a makeMinimalUpdates(), which further reduced the time of those test cases to 0.5 sec.


return expectedUpdates;
};
Expand All @@ -131,7 +141,7 @@ describe.skip('When rendering PolicySettingsLayout', () => {

it('should render layout with expected content when changes have been made', async () => {
const { getByTestId } = render();
await makeUpdates();
await makeMinimalUpdates();
expect(getByTestId('endpointPolicyForm'));
expect(getByTestId('policyDetailsCancelButton')).not.toBeDisabled();
expect(getByTestId('policyDetailsSaveButton')).not.toBeDisabled();
Expand All @@ -153,7 +163,7 @@ describe.skip('When rendering PolicySettingsLayout', () => {
const deferred = getDeferred();
apiMocks.responseProvider.updateEndpointPolicy.mockDelay.mockReturnValue(deferred.promise);
const { getByTestId } = render();
await makeUpdates();
await makeMinimalUpdates();
await clickSave(true, false);

await waitFor(() => {
Expand All @@ -164,13 +174,11 @@ describe.skip('When rendering PolicySettingsLayout', () => {
expect(
getByTestId('policyDetailsSaveButton').querySelector('.euiLoadingSpinner')
).not.toBeNull();

deferred.resolve();
});

it('should show success toast on update success', async () => {
render();
await makeUpdates();
await makeMinimalUpdates();
await clickSave();

await waitFor(() => {
Expand All @@ -189,7 +197,7 @@ describe.skip('When rendering PolicySettingsLayout', () => {
throw new Error('oh oh!');
});
render();
await makeUpdates();
await makeMinimalUpdates();
await clickSave();

await waitFor(() => {
Expand All @@ -202,6 +210,39 @@ describe.skip('When rendering PolicySettingsLayout', () => {
title: 'Failed!',
});
});

it('should not show warning about endpoints if there are no active endpoints', async () => {
apiMocks.responseProvider.agentStatus.mockReturnValue({
results: { active: 0 },
} as GetAgentStatusResponse);

const { getByTestId, queryByTestId } = render();
await makeMinimalUpdates();

await userEvent.click(getByTestId('policyDetailsSaveButton'));
await waitFor(() => {
expect(getByTestId('confirmModalConfirmButton')).toBeInTheDocument();
});
expect(queryByTestId('policyDetailsWarningCallout')).not.toBeInTheDocument();
});

it('should show warning about endpoints with the number of active endpoints', async () => {
apiMocks.responseProvider.agentStatus.mockReturnValue({
results: { active: 6 },
} as GetAgentStatusResponse);

const { getByTestId } = render();
await makeMinimalUpdates();

await userEvent.click(getByTestId('policyDetailsSaveButton'));
await waitFor(() => {
expect(getByTestId('confirmModalConfirmButton')).toBeInTheDocument();
});

const callout = getByTestId('policyDetailsWarningCallout');
expect(callout).toBeInTheDocument();
expect(callout.textContent).toContain('This action will update 6 endpoints');
});
});

describe('and user has View Only permissions', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const PolicySettingsLayout = memo<PolicySettingsLayoutProps>(
<>
{showConfirm && (
<ConfirmUpdate
endpointCount={agentSummaryData ? agentSummaryData.all : 0}
endpointCount={agentSummaryData ? agentSummaryData.active : 0}
onCancel={handleSaveCancel}
onConfirm={handleSaveConfirmation}
/>
Expand Down