[Security Solution] Fix unit tests in document details flyout (#170364)

## Summary

This PR fixed some console errors while running unit tests in
`flyout/document_details` folder

### 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
This commit is contained in:
christineweng 2023-11-02 12:00:43 -05:00 committed by GitHub
parent b6be6e0804
commit 5cfd05a2fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 183 additions and 1 deletions

View file

@ -14,6 +14,14 @@ import { EntitiesDetails } from './entities_details';
import { ENTITIES_DETAILS_TEST_ID, HOST_DETAILS_TEST_ID, USER_DETAILS_TEST_ID } from './test_ids';
import { mockContextValue } from '../mocks/mock_context';
import { EXPANDABLE_PANEL_CONTENT_TEST_ID } from '../../../shared/components/test_ids';
import type { Anomalies } from '../../../../common/components/ml/types';
import { useMlCapabilities } from '../../../../common/components/ml/hooks/use_ml_capabilities';
import { useRiskScore } from '../../../../explore/containers/risk_score';
import { mockAnomalies } from '../../../../common/components/ml/mock';
import { useHostDetails } from '../../../../explore/hosts/containers/hosts/details';
import { useHostRelatedUsers } from '../../../../common/containers/related_entities/related_users';
import { useObservedUserDetails } from '../../../../explore/users/containers/users/observed_details';
import { useUserRelatedHosts } from '../../../../common/containers/related_entities/related_hosts';
jest.mock('react-router-dom', () => {
const actual = jest.requireActual('react-router-dom');
@ -32,6 +40,61 @@ jest.mock('react-redux', () => {
};
});
const from = '2022-07-28T08:20:18.966Z';
const to = '2022-07-28T08:20:18.966Z';
jest.mock('../../../../common/containers/use_global_time', () => {
const actual = jest.requireActual('../../../../common/containers/use_global_time');
return {
...actual,
useGlobalTime: jest
.fn()
.mockReturnValue({ from, to, setQuery: jest.fn(), deleteQuery: jest.fn() }),
};
});
jest.mock('uuid', () => ({
v4: jest.fn().mockReturnValue('uuid'),
}));
jest.mock('../../../../common/components/ml/hooks/use_ml_capabilities');
const mockUseMlUserPermissions = useMlCapabilities as jest.Mock;
const mockUseHasSecurityCapability = jest.fn().mockReturnValue(false);
jest.mock('../../../../helper_hooks', () => ({
useHasSecurityCapability: () => mockUseHasSecurityCapability(),
}));
jest.mock('../../../../common/containers/sourcerer', () => ({
useSourcererDataView: jest.fn().mockReturnValue({ selectedPatterns: ['index'] }),
}));
jest.mock('../../../../common/components/ml/anomaly/anomaly_table_provider', () => ({
AnomalyTableProvider: ({
children,
}: {
children: (args: {
anomaliesData: Anomalies;
isLoadingAnomaliesData: boolean;
jobNameById: Record<string, string | undefined>;
}) => React.ReactNode;
}) => children({ anomaliesData: mockAnomalies, isLoadingAnomaliesData: false, jobNameById: {} }),
}));
jest.mock('../../../../explore/hosts/containers/hosts/details');
const mockUseHostDetails = useHostDetails as jest.Mock;
jest.mock('../../../../common/containers/related_entities/related_users');
const mockUseHostsRelatedUsers = useHostRelatedUsers as jest.Mock;
jest.mock('../../../../explore/containers/risk_score');
const mockUseRiskScore = useRiskScore as jest.Mock;
jest.mock('../../../../explore/users/containers/users/observed_details');
const mockUseObservedUserDetails = useObservedUserDetails as jest.Mock;
jest.mock('../../../../common/containers/related_entities/related_hosts');
const mockUseUsersRelatedHosts = useUserRelatedHosts as jest.Mock;
const USER_TEST_ID = EXPANDABLE_PANEL_CONTENT_TEST_ID(USER_DETAILS_TEST_ID);
const HOST_TEST_ID = EXPANDABLE_PANEL_CONTENT_TEST_ID(HOST_DETAILS_TEST_ID);
@ -47,6 +110,26 @@ const renderEntitiesDetails = (contextValue: LeftPanelContext) =>
);
describe('<EntitiesDetails />', () => {
beforeEach(() => {
mockUseMlUserPermissions.mockReturnValue({ isPlatinumOrTrialLicense: false, capabilities: {} });
mockUseHasSecurityCapability.mockReturnValue(false);
mockUseHostDetails.mockReturnValue([false, {}]);
mockUseRiskScore.mockReturnValue({ data: [], isAuthorized: false });
mockUseHostsRelatedUsers.mockReturnValue({
inspect: jest.fn(),
refetch: jest.fn(),
relatedUsers: [],
loading: false,
});
mockUseObservedUserDetails.mockReturnValue([false, {}]);
mockUseUsersRelatedHosts.mockReturnValue({
inspect: jest.fn(),
refetch: jest.fn(),
relatedHosts: [],
loading: false,
});
});
it('renders entities details correctly', () => {
const { getByTestId, queryByText } = renderEntitiesDetails(mockContextValue);
expect(getByTestId(ENTITIES_DETAILS_TEST_ID)).toBeInTheDocument();

View file

@ -72,6 +72,7 @@ const NO_DATA_MESSAGE = 'There was an error displaying data.';
describe('<RulePreview />', () => {
afterEach(() => {
jest.clearAllMocks();
mockUseGetSavedQuery.mockReturnValue({ isSavedQueryLoading: false, savedQueryBar: null });
});
it('should render rule preview and its sub sections', async () => {
@ -84,7 +85,6 @@ describe('<RulePreview />', () => {
scheduleRuleData: mockScheduleStepRule(),
ruleActionsData: { actions: ['action'] },
});
mockUseGetSavedQuery.mockReturnValue({ isSavedQueryLoading: false, savedQueryBar: null });
const { getByTestId } = renderRulePreview();
@ -124,6 +124,7 @@ describe('<RulePreview />', () => {
it('should render loading spinner when rule is loading', async () => {
mockUseRuleWithFallback.mockReturnValue({ loading: true, rule: null });
mockGetStepsData.mockReturnValue({});
const { getByTestId } = renderRulePreview();
await act(async () => {
expect(getByTestId(RULE_PREVIEW_LOADING_TEST_ID)).toBeInTheDocument();
@ -132,6 +133,7 @@ describe('<RulePreview />', () => {
it('should not render rule preview when rule is null', async () => {
mockUseRuleWithFallback.mockReturnValue({});
mockGetStepsData.mockReturnValue({});
const { queryByTestId, getByText } = renderRulePreview();
await act(async () => {
expect(queryByTestId(RULE_PREVIEW_BODY_TEST_ID)).not.toBeInTheDocument();

View file

@ -15,6 +15,10 @@ import {
} from './test_ids';
import { EntitiesOverview } from './entities_overview';
import { TestProviders } from '../../../../common/mock';
import { useRiskScore } from '../../../../explore/containers/risk_score';
import { useFirstLastSeen } from '../../../../common/containers/use_first_last_seen';
import { useObservedUserDetails } from '../../../../explore/users/containers/users/observed_details';
import { useHostDetails } from '../../../../explore/hosts/containers/hosts/details';
import { mockGetFieldsData } from '../../shared/mocks/mock_get_fields_data';
import {
EXPANDABLE_PANEL_HEADER_TITLE_ICON_TEST_ID,
@ -23,6 +27,36 @@ import {
EXPANDABLE_PANEL_TOGGLE_ICON_TEST_ID,
} from '../../../shared/components/test_ids';
const from = '2022-04-05T12:00:00.000Z';
const to = '2022-04-08T12:00:00.;000Z';
const selectedPatterns = 'alerts';
const mockUseGlobalTime = jest.fn().mockReturnValue({ from, to });
jest.mock('../../../../common/containers/use_global_time', () => {
return {
useGlobalTime: (...props: unknown[]) => mockUseGlobalTime(...props),
};
});
const mockUseSourcererDataView = jest.fn().mockReturnValue({ selectedPatterns });
jest.mock('../../../../common/containers/sourcerer', () => {
return {
useSourcererDataView: (...props: unknown[]) => mockUseSourcererDataView(...props),
};
});
const mockUseUserDetails = useObservedUserDetails as jest.Mock;
jest.mock('../../../../explore/users/containers/users/observed_details');
const mockUseRiskScore = useRiskScore as jest.Mock;
jest.mock('../../../../explore/containers/risk_score');
const mockUseFirstLastSeen = useFirstLastSeen as jest.Mock;
jest.mock('../../../../common/containers/use_first_last_seen');
const mockUseHostDetails = useHostDetails as jest.Mock;
jest.mock('../../../../explore/hosts/containers/hosts/details');
const TOGGLE_ICON_TEST_ID = EXPANDABLE_PANEL_TOGGLE_ICON_TEST_ID(INSIGHTS_ENTITIES_TEST_ID);
const TITLE_LINK_TEST_ID = EXPANDABLE_PANEL_HEADER_TITLE_LINK_TEST_ID(INSIGHTS_ENTITIES_TEST_ID);
const TITLE_ICON_TEST_ID = EXPANDABLE_PANEL_HEADER_TITLE_ICON_TEST_ID(INSIGHTS_ENTITIES_TEST_ID);
@ -47,6 +81,12 @@ const renderEntitiesOverview = (contextValue: RightPanelContext) =>
const NO_DATA_MESSAGE = 'Host and user information are unavailable for this alert.';
describe('<EntitiesOverview />', () => {
beforeEach(() => {
mockUseUserDetails.mockReturnValue([false, { userDetails: null }]);
mockUseRiskScore.mockReturnValue({ data: null, isAuthorized: false });
mockUseHostDetails.mockReturnValue([false, { hostDetails: null }]);
mockUseFirstLastSeen.mockReturnValue([false, { lastSeen: null }]);
});
it('should render wrapper component', () => {
const { getByTestId, queryByTestId } = renderEntitiesOverview(mockContextValue);

View file

@ -10,6 +10,12 @@ import { render } from '@testing-library/react';
import { RightPanelContext } from '../context';
import { INSIGHTS_HEADER_TEST_ID } from './test_ids';
import { TestProviders } from '../../../../common/mock';
import { useRiskScore } from '../../../../explore/containers/risk_score';
import { useFirstLastSeen } from '../../../../common/containers/use_first_last_seen';
import { useObservedUserDetails } from '../../../../explore/users/containers/users/observed_details';
import { useHostDetails } from '../../../../explore/hosts/containers/hosts/details';
import { useFetchThreatIntelligence } from '../hooks/use_fetch_threat_intelligence';
import { usePrevalence } from '../../shared/hooks/use_prevalence';
import { mockGetFieldsData } from '../../shared/mocks/mock_get_fields_data';
import { mockDataFormattedForFieldBrowser } from '../../shared/mocks/mock_data_formatted_for_field_browser';
import { InsightsSection } from './insights_section';
@ -41,6 +47,40 @@ jest.mock('react-router-dom', () => {
alertIds: [],
});
const from = '2022-04-05T12:00:00.000Z';
const to = '2022-04-08T12:00:00.;000Z';
const selectedPatterns = 'alerts';
const mockUseGlobalTime = jest.fn().mockReturnValue({ from, to });
jest.mock('../../../../common/containers/use_global_time', () => {
return {
useGlobalTime: (...props: unknown[]) => mockUseGlobalTime(...props),
};
});
const mockUseSourcererDataView = jest.fn().mockReturnValue({ selectedPatterns });
jest.mock('../../../../common/containers/sourcerer', () => {
return {
useSourcererDataView: (...props: unknown[]) => mockUseSourcererDataView(...props),
};
});
const mockUseUserDetails = useObservedUserDetails as jest.Mock;
jest.mock('../../../../explore/users/containers/users/observed_details');
const mockUseRiskScore = useRiskScore as jest.Mock;
jest.mock('../../../../explore/containers/risk_score');
const mockUseFirstLastSeen = useFirstLastSeen as jest.Mock;
jest.mock('../../../../common/containers/use_first_last_seen');
const mockUseHostDetails = useHostDetails as jest.Mock;
jest.mock('../../../../explore/hosts/containers/hosts/details');
jest.mock('../hooks/use_fetch_threat_intelligence');
jest.mock('../../shared/hooks/use_prevalence');
const renderInsightsSection = (contextValue: RightPanelContext, expanded: boolean) =>
render(
<TestProviders>
@ -51,6 +91,23 @@ const renderInsightsSection = (contextValue: RightPanelContext, expanded: boolea
);
describe('<InsightsSection />', () => {
beforeEach(() => {
mockUseUserDetails.mockReturnValue([false, { userDetails: null }]);
mockUseRiskScore.mockReturnValue({ data: null, isAuthorized: false });
mockUseHostDetails.mockReturnValue([false, { hostDetails: null }]);
mockUseFirstLastSeen.mockReturnValue([false, { lastSeen: null }]);
(useFetchThreatIntelligence as jest.Mock).mockReturnValue({
loading: false,
threatMatchesCount: 2,
threatEnrichmentsCount: 2,
});
(usePrevalence as jest.Mock).mockReturnValue({
loading: false,
error: false,
data: [],
});
});
it('should render insights component', () => {
const contextValue = {
eventId: 'some_Id',