From c7edfa00a311241874192c3b35c503e38a88104d Mon Sep 17 00:00:00 2001 From: Ashokaditya <1849116+ashokaditya@users.noreply.github.com> Date: Wed, 1 Feb 2023 10:00:09 +0100 Subject: [PATCH] [Security Solution][Endpoint] Flx and unskip flaky test (#149839) ## Summary Fixes flaky test elastic/kibana/issues/145635 flaky test runners: - https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/1816 x 50 (successful on all 50) - https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/1828 x 150 (successful on all 150) - https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/1829 x 200 (failed on a single run for an [unrelated test](https://github.com/elastic/kibana/blob/92cb000a2f5116fc7408f52794cea06ad40de4bb/x-pack/test/security_solution_endpoint/apps/endpoint/responder.ts#L113)) - https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/1832 x 100 (successful on all 100) ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios (cherry picked from commit 511aaf3004aab84ec2f9ae452e3f739a58876dd8) --- .../response_actions_log.test.tsx | 89 +++++++++---------- 1 file changed, 41 insertions(+), 48 deletions(-) diff --git a/x-pack/plugins/security_solution/public/management/components/endpoint_response_actions_list/response_actions_log.test.tsx b/x-pack/plugins/security_solution/public/management/components/endpoint_response_actions_list/response_actions_log.test.tsx index 9f54eeadaf804..16c85c8c84dc2 100644 --- a/x-pack/plugins/security_solution/public/management/components/endpoint_response_actions_list/response_actions_log.test.tsx +++ b/x-pack/plugins/security_solution/public/management/components/endpoint_response_actions_list/response_actions_log.test.tsx @@ -27,7 +27,7 @@ import { RESPONSE_ACTION_API_COMMANDS_NAMES } from '../../../../common/endpoint/ import { useUserPrivileges as _useUserPrivileges } from '../../../common/components/user_privileges'; import { responseActionsHttpMocks } from '../../mocks/response_actions_http_mocks'; import { waitFor } from '@testing-library/react'; -import { getUserPrivilegesMockDefaultValue } from '../../../common/components/user_privileges/__mocks__'; +import { getEndpointAuthzInitialStateMock } from '../../../../common/endpoint/service/authz/mocks'; let mockUseGetEndpointActionList: { isFetched?: boolean; @@ -123,6 +123,7 @@ jest.mock('../../hooks/endpoint/use_get_endpoints_list'); jest.mock('../../../common/experimental_features_service'); jest.mock('../../../common/components/user_privileges'); +const useUserPrivilegesMock = _useUserPrivileges as jest.Mock; let mockUseGetFileInfo: { isFetching?: boolean; @@ -139,12 +140,13 @@ jest.mock('../../hooks/response_actions/use_get_file_info', () => { const mockUseGetEndpointsList = useGetEndpointsList as jest.Mock; -// FLAKY https://github.com/elastic/kibana/issues/145635 -describe.skip('Response actions history', () => { - const useUserPrivilegesMock = _useUserPrivileges as jest.Mock< - ReturnType - >; - +const getBaseMockedActionList = () => ({ + isFetched: true, + isFetching: false, + error: null, + refetch: jest.fn(), +}); +describe('Response actions history', () => { const testPrefix = 'response-actions-list'; let render: ( @@ -155,14 +157,6 @@ describe.skip('Response actions history', () => { let mockedContext: AppContextTestRender; let apiMocks: ReturnType; - const refetchFunction = jest.fn(); - const baseMockedActionList = { - isFetched: true, - isFetching: false, - error: null, - refetch: refetchFunction, - }; - beforeEach(async () => { mockedContext = createAppRootMockRenderer(); ({ history } = mockedContext); @@ -173,7 +167,7 @@ describe.skip('Response actions history', () => { }); mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 13 }), }; @@ -189,20 +183,20 @@ describe.skip('Response actions history', () => { pageSize: 50, total: 50, }); + useUserPrivilegesMock.mockReturnValue({ + endpointPrivileges: getEndpointAuthzInitialStateMock(), + }); }); afterEach(() => { - mockUseGetEndpointActionList = { - ...baseMockedActionList, - }; - jest.clearAllMocks(); - useUserPrivilegesMock.mockImplementation(getUserPrivilegesMockDefaultValue); + mockUseGetEndpointActionList = getBaseMockedActionList(); + useUserPrivilegesMock.mockReset(); }); describe('When index does not exist yet', () => { it('should show global loader when waiting for response', () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), isFetched: false, isFetching: true, }; @@ -211,7 +205,7 @@ describe.skip('Response actions history', () => { }); it('should show empty page when there is no index', () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), error: { body: { statusCode: 404, message: 'index_not_found_exception' }, }, @@ -234,7 +228,7 @@ describe.skip('Response actions history', () => { it('should show empty state when there is no data', async () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 0 }), }; render(); @@ -295,7 +289,7 @@ describe.skip('Response actions history', () => { }; mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data, }; render({ showHostNames: true }); @@ -316,7 +310,7 @@ describe.skip('Response actions history', () => { }; mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data, }; render({ showHostNames: true }); @@ -339,7 +333,7 @@ describe.skip('Response actions history', () => { }; mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data, }; render({ showHostNames: true }); @@ -367,7 +361,7 @@ describe.skip('Response actions history', () => { it('should update per page rows on the table', async () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 33 }), }; @@ -398,7 +392,7 @@ describe.skip('Response actions history', () => { it('should show 1-1 record label when only 1 record', async () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 1 }), }; render(); @@ -446,7 +440,7 @@ describe.skip('Response actions history', () => { it('should contain download link in expanded row for `get-file` action WITH file operation permission', async () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 1, commands: ['get-file'] }), }; @@ -475,7 +469,7 @@ describe.skip('Response actions history', () => { it('should show file unavailable for download for `get-file` action WITH file operation permission when file is deleted', async () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 1, commands: ['get-file'] }), }; @@ -507,20 +501,14 @@ describe.skip('Response actions history', () => { }); it('should not contain download link in expanded row for `get-file` action when NO file operation permission', async () => { - const privileges = useUserPrivilegesMock(); - - useUserPrivilegesMock.mockImplementationOnce(() => { - return { - ...privileges, - endpointPrivileges: { - ...privileges.endpointPrivileges, - canWriteFileOperations: false, - }, - }; + useUserPrivilegesMock.mockReturnValue({ + endpointPrivileges: getEndpointAuthzInitialStateMock({ + canWriteFileOperations: false, + }), }); mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 1, commands: ['get-file'] }), }; @@ -536,6 +524,7 @@ describe.skip('Response actions history', () => { }); it('should refresh data when autoRefresh is toggled on', async () => { + mockUseGetEndpointActionList = getBaseMockedActionList(); render(); const { getByTestId } = renderResult; @@ -550,16 +539,19 @@ describe.skip('Response actions history', () => { reactTestingLibrary.fireEvent.change(intervalInput, { target: { value: 1 } }); await reactTestingLibrary.waitFor(() => { - expect(refetchFunction).toHaveBeenCalledTimes(3); + expect(mockUseGetEndpointActionList.refetch).toHaveBeenCalledTimes(3); }); }); it('should refresh data when super date picker refresh button is clicked', async () => { + mockUseGetEndpointActionList = getBaseMockedActionList(); render(); const superRefreshButton = renderResult.getByTestId(`${testPrefix}-super-refresh-button`); userEvent.click(superRefreshButton); - expect(refetchFunction).toHaveBeenCalledTimes(1); + await waitFor(() => { + expect(mockUseGetEndpointActionList.refetch).toHaveBeenCalled(); + }); }); it('should set date picker with relative dates', async () => { @@ -592,7 +584,7 @@ describe.skip('Response actions history', () => { it('shows completed status badge for successfully completed actions', async () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 2 }), }; render(); @@ -609,7 +601,7 @@ describe.skip('Response actions history', () => { it('shows Failed status badge for failed actions', async () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 2, wasSuccessful: false, status: 'failed' }), }; render(); @@ -623,7 +615,7 @@ describe.skip('Response actions history', () => { it('shows Failed status badge for expired actions', async () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 2, isCompleted: false, @@ -645,7 +637,7 @@ describe.skip('Response actions history', () => { it('shows Pending status badge for pending actions', async () => { mockUseGetEndpointActionList = { - ...baseMockedActionList, + ...getBaseMockedActionList(), data: await getActionListMock({ actionCount: 2, isCompleted: false, status: 'pending' }), }; render(); @@ -693,6 +685,7 @@ describe.skip('Response actions history', () => { 'suspend-process', 'processes', 'get-file', + 'execute', ]); });