Skip to content

Commit

Permalink
[Security Solution] Fix signals index initialization bug (elastic#123087
Browse files Browse the repository at this point in the history
)
  • Loading branch information
stephmilovic authored Jan 17, 2022
1 parent 74ef323 commit fef96e9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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}</>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit fef96e9

Please sign in to comment.