[Security Solution] Fix signals index initialization bug (#123087)

This commit is contained in:
Steph Milovic 2022-01-17 09:22:32 -07:00 committed by GitHub
parent 74ef3238fc
commit fef96e99e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 1 deletions

View file

@ -41,6 +41,10 @@ jest.mock('../../../common/containers/source', () => ({
useFetchIndex: () => [false, { indicesExist: true, indexPatterns: mockIndexPattern }],
}));
jest.mock('../../../common/containers/sourcerer/use_signal_helpers', () => ({
useSignalHelpers: () => ({ signalIndexNeedsInit: false }),
}));
jest.mock('react-reverse-portal', () => ({
InPortal: ({ children }: { children: React.ReactNode }) => <>{children}</>,
OutPortal: ({ children }: { children: React.ReactNode }) => <>{children}</>,

View file

@ -21,10 +21,12 @@ import { createStore } from '../../store';
import { EuiSuperSelectOption } from '@elastic/eui/src/components/form/super_select/super_select_control';
import { waitFor } from '@testing-library/dom';
import { useSourcererDataView } from '../../containers/sourcerer';
import { useSignalHelpers } from '../../containers/sourcerer/use_signal_helpers';
const mockDispatch = jest.fn();
jest.mock('../../containers/sourcerer');
jest.mock('../../containers/sourcerer/use_signal_helpers');
const mockUseUpdateDataView = jest.fn().mockReturnValue(() => true);
jest.mock('./use_update_data_view', () => ({
useUpdateDataView: () => mockUseUpdateDataView,
@ -81,10 +83,12 @@ const sourcererDataView = {
describe('Sourcerer component', () => {
const { storage } = createSecuritySolutionStorageMock();
const pollForSignalIndexMock = jest.fn();
beforeEach(() => {
store = createStore(mockGlobalState, SUB_PLUGINS_REDUCER, kibanaObservable, storage);
(useSourcererDataView as jest.Mock).mockReturnValue(sourcererDataView);
(useSignalHelpers as jest.Mock).mockReturnValue({ signalIndexNeedsInit: false });
jest.clearAllMocks();
});
@ -570,6 +574,63 @@ describe('Sourcerer component', () => {
.exists()
).toBeFalsy();
});
it('does not poll for signals index if pollForSignalIndex is not defined', () => {
(useSignalHelpers as jest.Mock).mockReturnValue({
signalIndexNeedsInit: false,
});
mount(
<TestProviders store={store}>
<Sourcerer scope={sourcererModel.SourcererScopeName.timeline} />
</TestProviders>
);
expect(pollForSignalIndexMock).toHaveBeenCalledTimes(0);
});
it('does not poll for signals index if it does not exist and scope is default', () => {
(useSignalHelpers as jest.Mock).mockReturnValue({
pollForSignalIndex: pollForSignalIndexMock,
signalIndexNeedsInit: false,
});
mount(
<TestProviders store={store}>
<Sourcerer scope={sourcererModel.SourcererScopeName.default} />
</TestProviders>
);
expect(pollForSignalIndexMock).toHaveBeenCalledTimes(0);
});
it('polls for signals index if it does not exist and scope is timeline', () => {
(useSignalHelpers as jest.Mock).mockReturnValue({
pollForSignalIndex: pollForSignalIndexMock,
signalIndexNeedsInit: false,
});
mount(
<TestProviders store={store}>
<Sourcerer scope={sourcererModel.SourcererScopeName.timeline} />
</TestProviders>
);
expect(pollForSignalIndexMock).toHaveBeenCalledTimes(1);
});
it('polls for signals index if it does not exist and scope is detections', () => {
(useSignalHelpers as jest.Mock).mockReturnValue({
pollForSignalIndex: pollForSignalIndexMock,
signalIndexNeedsInit: false,
});
mount(
<TestProviders store={store}>
<Sourcerer scope={sourcererModel.SourcererScopeName.detections} />
</TestProviders>
);
expect(pollForSignalIndexMock).toHaveBeenCalledTimes(1);
});
});
describe('sourcerer on alerts page or rules details page', () => {

View file

@ -28,6 +28,7 @@ import { useSourcererDataView } from '../../containers/sourcerer';
import { useUpdateDataView } from './use_update_data_view';
import { Trigger } from './trigger';
import { AlertsCheckbox, SaveButtons, SourcererCallout } from './sub_components';
import { useSignalHelpers } from '../../containers/sourcerer/use_signal_helpers';
export interface SourcererComponentProps {
scope: sourcererModel.SourcererScopeName;
@ -50,6 +51,14 @@ export const Sourcerer = React.memo<SourcererComponentProps>(({ scope: scopeId }
},
} = useDeepEqualSelector((state) => sourcererScopeSelector(state, scopeId));
const { pollForSignalIndex } = useSignalHelpers();
useEffect(() => {
if (pollForSignalIndex != null && (isTimelineSourcerer || isDetectionsSourcerer)) {
pollForSignalIndex();
}
}, [isDetectionsSourcerer, isTimelineSourcerer, pollForSignalIndex]);
const { activePatterns, indicesExist, loading } = useSourcererDataView(scopeId);
const [missingPatterns, setMissingPatterns] = useState<string[]>(
activePatterns && activePatterns.length > 0

View file

@ -35,6 +35,9 @@ jest.mock('../body/events/index', () => ({
}));
jest.mock('../../../../common/containers/sourcerer');
jest.mock('../../../../common/containers/sourcerer/use_signal_helpers', () => ({
useSignalHelpers: () => ({ signalIndexNeedsInit: false }),
}));
const mockUseResizeObserver: jest.Mock = useResizeObserver as jest.Mock;
jest.mock('use-resize-observer/polyfilled');

View file

@ -38,6 +38,9 @@ jest.mock('../body/events/index', () => ({
}));
jest.mock('../../../../common/containers/sourcerer');
jest.mock('../../../../common/containers/sourcerer/use_signal_helpers', () => ({
useSignalHelpers: () => ({ signalIndexNeedsInit: false }),
}));
const mockUseResizeObserver: jest.Mock = useResizeObserver as jest.Mock;
jest.mock('use-resize-observer/polyfilled');