Skip to content

Commit

Permalink
filters are back (#54218) (#54255)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
XavierM and elasticmachine authored Jan 8, 2020
1 parent fa01538 commit 8ab8402
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 7 deletions.
60 changes: 59 additions & 1 deletion x-pack/legacy/plugins/siem/public/pages/hosts/hosts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import { Router } from 'react-router-dom';
import { MockedProvider } from 'react-apollo/test-utils';
import { ActionCreator } from 'typescript-fsa';

import { esFilters } from '../../../../../../../src/plugins/data/common/es_query';
import '../../mock/match_media';
import { mocksSource } from '../../containers/source/mock';
import { wait } from '../../lib/helpers';
import { TestProviders } from '../../mock';
import { apolloClientObservable, TestProviders, mockGlobalState } from '../../mock';
import { InputsModelId } from '../../store/inputs/constants';
import { SiemNavigation } from '../../components/navigation';
import { inputsActions } from '../../store/inputs';
import { State, createStore } from '../../store';
import { HostsComponentProps } from './types';
import { Hosts } from './hosts';
import { HostsTabs } from './hosts_tabs';

// Test will fail because we will to need to mock some core services to make the test work
// For now let's forget about SiemSearchBar and QueryBar
Expand Down Expand Up @@ -136,4 +140,58 @@ describe('Hosts - rendering', () => {
wrapper.update();
expect(wrapper.find(SiemNavigation).exists()).toBe(true);
});

test('it should add the new filters after init', async () => {
const newFilters: esFilters.Filter[] = [
{
query: {
bool: {
filter: [
{
bool: {
should: [
{
match_phrase: {
'host.name': 'ItRocks',
},
},
],
minimum_should_match: 1,
},
},
],
},
},
meta: {
alias: '',
disabled: false,
key: 'bool',
negate: false,
type: 'custom',
value:
'{"query": {"bool": {"filter": [{"bool": {"should": [{"match_phrase": {"host.name": "ItRocks"}}],"minimum_should_match": 1}}]}}}',
},
},
];
localSource[0].result.data.source.status.indicesExist = true;
const myState: State = mockGlobalState;
const myStore = createStore(myState, apolloClientObservable);
const wrapper = mount(
<TestProviders store={myStore}>
<MockedProvider mocks={localSource} addTypename={false}>
<Router history={mockHistory}>
<Hosts {...hostProps} />
</Router>
</MockedProvider>
</TestProviders>
);
await wait();
wrapper.update();

myStore.dispatch(inputsActions.setSearchBarFilter({ id: 'global', filters: newFilters }));
wrapper.update();
expect(wrapper.find(HostsTabs).props().filterQuery).toEqual(
'{"bool":{"must":[],"filter":[{"match_all":{}},{"bool":{"filter":[{"bool":{"should":[{"match_phrase":{"host.name":"ItRocks"}}],"minimum_should_match":1}}]}}],"should":[],"must_not":[]}}'
);
});
});
5 changes: 2 additions & 3 deletions x-pack/legacy/plugins/siem/public/pages/hosts/hosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { HostsTableType } from '../../store/hosts/model';

const KpiHostsComponentManage = manageQuery(KpiHostsComponent);

const HostsComponent = React.memo<HostsComponentProps>(
export const HostsComponent = React.memo<HostsComponentProps>(
({
deleteQuery,
isInitializing,
Expand All @@ -56,13 +56,12 @@ const HostsComponent = React.memo<HostsComponentProps>(
const capabilities = React.useContext(MlCapabilitiesContext);
const kibana = useKibana();
const { tabName } = useParams();

const hostsFilters = React.useMemo(() => {
if (tabName === HostsTableType.alerts) {
return filters.length > 0 ? [...filters, ...filterAlertsHosts] : filterAlertsHosts;
}
return filters;
}, [tabName]);
}, [tabName, filters]);
const narrowDateRange = useCallback(
(min: number, max: number) => {
setAbsoluteRangeDatePicker({ id: 'global', from: min, to: max });
Expand Down
61 changes: 59 additions & 2 deletions x-pack/legacy/plugins/siem/public/pages/network/network.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import { Router } from 'react-router-dom';
import { MockedProvider } from 'react-apollo/test-utils';

import '../../mock/match_media';

import { esFilters } from '../../../../../../../src/plugins/data/common/es_query';
import { mocksSource } from '../../containers/source/mock';
import { TestProviders } from '../../mock';
import { TestProviders, mockGlobalState, apolloClientObservable } from '../../mock';
import { State, createStore } from '../../store';
import { inputsActions } from '../../store/inputs';
import { Network } from './network';
import { NetworkRoutes } from './navigation';

// Test will fail because we will to need to mock some core services to make the test work
// For now let's forget about SiemSearchBar and QueryBar
Expand Down Expand Up @@ -111,4 +114,58 @@ describe('rendering - rendering', () => {
wrapper.update();
expect(wrapper.find('[data-test-subj="empty-page"]').exists()).toBe(false);
});

test('it should add the new filters after init', async () => {
const newFilters: esFilters.Filter[] = [
{
query: {
bool: {
filter: [
{
bool: {
should: [
{
match_phrase: {
'host.name': 'ItRocks',
},
},
],
minimum_should_match: 1,
},
},
],
},
},
meta: {
alias: '',
disabled: false,
key: 'bool',
negate: false,
type: 'custom',
value:
'{"query": {"bool": {"filter": [{"bool": {"should": [{"match_phrase": {"host.name": "ItRocks"}}],"minimum_should_match": 1}}]}}}',
},
},
];
localSource[0].result.data.source.status.indicesExist = true;
const myState: State = mockGlobalState;
const myStore = createStore(myState, apolloClientObservable);
const wrapper = mount(
<TestProviders store={myStore}>
<MockedProvider mocks={localSource} addTypename={false}>
<Router history={mockHistory}>
<Network {...getMockProps()} />
</Router>
</MockedProvider>
</TestProviders>
);
await new Promise(resolve => setTimeout(resolve));
wrapper.update();

myStore.dispatch(inputsActions.setSearchBarFilter({ id: 'global', filters: newFilters }));
wrapper.update();
expect(wrapper.find(NetworkRoutes).props().filterQuery).toEqual(
'{"bool":{"must":[],"filter":[{"match_all":{}},{"bool":{"filter":[{"bool":{"should":[{"match_phrase":{"host.name":"ItRocks"}}],"minimum_should_match":1}}]}}],"should":[],"must_not":[]}}'
);
});
});
3 changes: 2 additions & 1 deletion x-pack/legacy/plugins/siem/public/pages/network/network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const NetworkComponent = React.memo<NetworkComponentProps>(
return filters.length > 0 ? [...filters, ...filterAlertsNetwork] : filterAlertsNetwork;
}
return filters;
}, [tabName]);
}, [tabName, filters]);

const narrowDateRange = useCallback(
(min: number, max: number) => {
setAbsoluteRangeDatePicker({ id: 'global', from: min, to: max });
Expand Down

0 comments on commit 8ab8402

Please sign in to comment.