Skip to content

Commit

Permalink
[Security Solution] expandable flyout - fix infinite loop in correlat…
Browse files Browse the repository at this point in the history
…ions (#163450)
  • Loading branch information
PhilippeOberti authored Aug 23, 2023
1 parent 5c39cc3 commit a95f4f8
Show file tree
Hide file tree
Showing 67 changed files with 2,110 additions and 1,610 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,47 @@
*/

import React from 'react';
import { render, screen } from '@testing-library/react';
import { useTimelineEventsDetails } from '../../../timelines/containers/details';
import { useSourcererDataView } from '../../../common/containers/sourcerer';
import { useCorrelations, type UseCorrelationsResult } from '../../shared/hooks/use_correlations';
import { render } from '@testing-library/react';
import { CorrelationsDetails } from './correlations_details';
import { type GetRelatedCasesByAlertResponse } from '@kbn/cases-plugin/common';
import type { SelectedDataView } from '../../../common/store/sourcerer/model';
import { TestProviders } from '../../../common/mock';
import { LeftPanelContext } from '../context';
import { CaseStatuses } from '@kbn/cases-components';

jest.mock('../../../timelines/containers/details');
jest.mock('../../../common/containers/sourcerer');
jest.mock('../../shared/hooks/use_correlations');

const mockCasesByAlertId: GetRelatedCasesByAlertResponse = [
{
id: '123',
title: 'Mock Case',
description: 'This is a mock case for testing purposes',
status: CaseStatuses.open,
createdAt: '2021-10-01T12:00:00Z',
totals: {
alerts: 5,
userComments: 2,
},
},
];

const mockUseCorrelationsResult: UseCorrelationsResult = {
loading: false,
error: false,
data: [],
dataCount: 0,
alertsBySessionIds: ['alert1', 'alert2', 'alert3'],
sameSourceAlertsIds: ['alert1', 'alert2'],
ancestryAlertsIds: ['alert3'],
cases: mockCasesByAlertId,
};

const contextValue: LeftPanelContext = {
indexName: 'index',
eventId: 'event',
getFieldsData: () => null,
dataFormattedForFieldBrowser: [],
dataAsNestedObject: null,
scopeId: '',
browserFields: null,
searchHit: undefined,
investigationFields: [],
};
import { useShowRelatedAlertsByAncestry } from '../../shared/hooks/use_show_related_alerts_by_ancestry';
import { useShowRelatedAlertsBySameSourceEvent } from '../../shared/hooks/use_show_related_alerts_by_same_source_event';
import { useShowRelatedAlertsBySession } from '../../shared/hooks/use_show_related_alerts_by_session';
import { useShowRelatedCases } from '../../shared/hooks/use_show_related_cases';
import {
CORRELATIONS_DETAILS_BY_ANCESTRY_SECTION_TABLE_TEST_ID,
CORRELATIONS_DETAILS_BY_ANCESTRY_SECTION_TEST_ID,
CORRELATIONS_DETAILS_BY_SESSION_SECTION_TABLE_TEST_ID,
CORRELATIONS_DETAILS_BY_SESSION_SECTION_TEST_ID,
CORRELATIONS_DETAILS_BY_SOURCE_SECTION_TABLE_TEST_ID,
CORRELATIONS_DETAILS_BY_SOURCE_SECTION_TEST_ID,
CORRELATIONS_DETAILS_CASES_SECTION_TABLE_TEST_ID,
CORRELATIONS_DETAILS_CASES_SECTION_TEST_ID,
} from './test_ids';
import { useFetchRelatedAlertsBySession } from '../../shared/hooks/use_fetch_related_alerts_by_session';
import { useFetchRelatedAlertsByAncestry } from '../../shared/hooks/use_fetch_related_alerts_by_ancestry';
import { useFetchRelatedAlertsBySameSourceEvent } from '../../shared/hooks/use_fetch_related_alerts_by_same_source_event';
import { useFetchRelatedCases } from '../../shared/hooks/use_fetch_related_cases';
import { mockContextValue } from '../mocks/mock_context';

jest.mock('react-router-dom', () => {
const actual = jest.requireActual('react-router-dom');
return { ...actual, useLocation: jest.fn().mockReturnValue({ pathname: '' }) };
});
jest.mock('../../shared/hooks/use_show_related_alerts_by_ancestry');
jest.mock('../../shared/hooks/use_show_related_alerts_by_same_source_event');
jest.mock('../../shared/hooks/use_show_related_alerts_by_session');
jest.mock('../../shared/hooks/use_show_related_cases');
jest.mock('../../shared/hooks/use_fetch_related_alerts_by_session');
jest.mock('../../shared/hooks/use_fetch_related_alerts_by_ancestry');
jest.mock('../../shared/hooks/use_fetch_related_alerts_by_same_source_event');
jest.mock('../../shared/hooks/use_fetch_related_cases');

const renderCorrelationDetails = () => {
return render(
<TestProviders>
<LeftPanelContext.Provider value={contextValue}>
<LeftPanelContext.Provider value={mockContextValue}>
<CorrelationsDetails />
</LeftPanelContext.Provider>
</TestProviders>
Expand All @@ -71,46 +56,84 @@ const renderCorrelationDetails = () => {
describe('CorrelationsDetails', () => {
beforeEach(() => {
jest.clearAllMocks();

jest
.mocked(useSourcererDataView)
.mockReturnValue({ runtimeMappings: {} } as unknown as SelectedDataView);
});

it('renders loading spinner when data is loading', () => {
it('renders all sections', () => {
jest
.mocked(useTimelineEventsDetails)
.mockReturnValue([true, null, undefined, null, async () => {}]);
jest.mocked(useCorrelations).mockReturnValue(mockUseCorrelationsResult);

renderCorrelationDetails();

expect(screen.getByRole('progressbar')).toBeInTheDocument();
.mocked(useShowRelatedAlertsByAncestry)
.mockReturnValue({ show: true, documentId: 'documentId', indices: ['index1'] });
jest
.mocked(useShowRelatedAlertsBySameSourceEvent)
.mockReturnValue({ show: true, originalEventId: 'originalEventId' });
jest
.mocked(useShowRelatedAlertsBySession)
.mockReturnValue({ show: true, entityId: 'entityId' });
jest.mocked(useShowRelatedCases).mockReturnValue(true);

(useFetchRelatedAlertsByAncestry as jest.Mock).mockReturnValue({
loading: false,
error: false,
data: [],
dataCount: 1,
});
(useFetchRelatedAlertsBySameSourceEvent as jest.Mock).mockReturnValue({
loading: false,
error: false,
data: [],
dataCount: 1,
});
(useFetchRelatedAlertsBySession as jest.Mock).mockReturnValue({
loading: false,
error: false,
data: [],
dataCount: 1,
});
(useFetchRelatedCases as jest.Mock).mockReturnValue({
loading: false,
error: false,
data: [],
dataCount: 1,
});

const { getByTestId } = renderCorrelationDetails();

expect(getByTestId(CORRELATIONS_DETAILS_BY_ANCESTRY_SECTION_TABLE_TEST_ID)).toBeInTheDocument();
expect(getByTestId(CORRELATIONS_DETAILS_BY_SOURCE_SECTION_TABLE_TEST_ID)).toBeInTheDocument();
expect(getByTestId(CORRELATIONS_DETAILS_BY_SESSION_SECTION_TABLE_TEST_ID)).toBeInTheDocument();
expect(getByTestId(CORRELATIONS_DETAILS_CASES_SECTION_TABLE_TEST_ID)).toBeInTheDocument();
});

it('renders error message when there is an error', () => {
it('should render no section if show values are false', () => {
jest
.mocked(useShowRelatedAlertsByAncestry)
.mockReturnValue({ show: false, documentId: 'documentId', indices: ['index1'] });
jest
.mocked(useTimelineEventsDetails)
.mockReturnValue([false, null, undefined, null, async () => {}]);
jest.mocked(useCorrelations).mockReturnValue({ ...mockUseCorrelationsResult, error: true });
.mocked(useShowRelatedAlertsBySameSourceEvent)
.mockReturnValue({ show: false, originalEventId: 'originalEventId' });
jest
.mocked(useShowRelatedAlertsBySession)
.mockReturnValue({ show: false, entityId: 'entityId' });
jest.mocked(useShowRelatedCases).mockReturnValue(false);

renderCorrelationDetails();
const { queryByTestId } = renderCorrelationDetails();

expect(
screen.getByText('There was an error displaying Correlation Details view')
).toBeInTheDocument();
expect(queryByTestId(CORRELATIONS_DETAILS_BY_ANCESTRY_SECTION_TEST_ID)).not.toBeInTheDocument();
expect(queryByTestId(CORRELATIONS_DETAILS_BY_SOURCE_SECTION_TEST_ID)).not.toBeInTheDocument();
expect(queryByTestId(CORRELATIONS_DETAILS_BY_SESSION_SECTION_TEST_ID)).not.toBeInTheDocument();
expect(queryByTestId(CORRELATIONS_DETAILS_CASES_SECTION_TEST_ID)).not.toBeInTheDocument();
});

it('renders alerts tables when data is loaded', () => {
jest
.mocked(useTimelineEventsDetails)
.mockReturnValue([false, null, undefined, null, async () => {}]);
jest.mocked(useCorrelations).mockReturnValue(mockUseCorrelationsResult);
it('should render no section if values are null', () => {
jest.mocked(useShowRelatedAlertsByAncestry).mockReturnValue({ show: true });
jest.mocked(useShowRelatedAlertsBySameSourceEvent).mockReturnValue({ show: true });
jest.mocked(useShowRelatedAlertsBySession).mockReturnValue({ show: true });
jest.mocked(useShowRelatedCases).mockReturnValue(false);

renderCorrelationDetails();
const { queryByTestId } = renderCorrelationDetails();

expect(screen.getByText('1 alert related by ancestry')).toBeInTheDocument();
expect(screen.getByText('2 alerts related by source event')).toBeInTheDocument();
expect(screen.getByText('3 alerts related by session')).toBeInTheDocument();
expect(queryByTestId(CORRELATIONS_DETAILS_BY_ANCESTRY_SECTION_TEST_ID)).not.toBeInTheDocument();
expect(queryByTestId(CORRELATIONS_DETAILS_BY_SOURCE_SECTION_TEST_ID)).not.toBeInTheDocument();
expect(queryByTestId(CORRELATIONS_DETAILS_BY_SESSION_SECTION_TEST_ID)).not.toBeInTheDocument();
expect(queryByTestId(CORRELATIONS_DETAILS_CASES_SECTION_TEST_ID)).not.toBeInTheDocument();
});
});
Loading

0 comments on commit a95f4f8

Please sign in to comment.