Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security Solution] Fix signals index initialization bug #123087

Merged
merged 4 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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