Skip to content

Commit

Permalink
Add Checks to Dashboard Context (#13182)
Browse files Browse the repository at this point in the history
* Fixes #13181 - Check values before adding them to filter

* Adding tests

* Adding check to make sure query is never undefined
  • Loading branch information
simianhacker committed Jul 28, 2017
1 parent be00869 commit 31b3ec2
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import sinon from 'sinon';
import { expect } from 'chai';
import { dashboardContextProvider } from '../dashboard_context';

describe('Dashboard Context', () => {

describe('with query bar', () => {
let Private;
let getAppState;
let getDashboardContext;
beforeEach(() => {
Private = sinon.stub().returns({
getFilters() {
return [];
}
});
});

it('should return an empty must and must not when there are no filters or queries', () => {
getAppState = sinon.stub().returns({
query: {
language: 'lucene',
query: null
}
});
getDashboardContext = dashboardContextProvider(Private, getAppState);
const context = getDashboardContext();
expect(context).to.eql({
bool: {
must: [],
must_not: []
}
});
});

it('should add a valid query to must', () => {
getAppState = sinon.stub().returns({
query: {
language: 'lucene',
query: '*'
}
});
getDashboardContext = dashboardContextProvider(Private, getAppState);
const context = getDashboardContext();
expect(context).to.eql({
bool: {
must: [
{
query_string: {
query: '*'
}
}
],
must_not: []
}
});
});

});

describe('with filter bar', () => {
let Private;
let getAppState;
let getDashboardContext;
beforeEach(() => {
getAppState = sinon.stub().returns({ query: { language: 'something-else' } });
});

afterEach(() => {
getAppState.reset();
});

it('should add a valid filter to must', () => {
Private = sinon.stub().returns({
getFilters() {
return [
{ meta: { negate: false }, term: { foo: 'bar' } }
];
}
});
getDashboardContext = dashboardContextProvider(Private, getAppState);
const context = getDashboardContext();
expect(context).to.eql({
bool: {
must: [{ term: { foo: 'bar' } }],
must_not: []
}
});
});

it('should add a valid filter to must_not', () => {
Private = sinon.stub().returns({
getFilters() {
return [
{ meta: { negate: true }, term: { foo: 'bar' } }
];
}
});
getDashboardContext = dashboardContextProvider(Private, getAppState);
const context = getDashboardContext();
expect(context).to.eql({
bool: {
must: [],
must_not: [{ term: { foo: 'bar' } }]
}
});
});

it('should not add a disabled filter', () => {
Private = sinon.stub().returns({
getFilters() {
return [
{ meta: { negate: true, disabled: true }, term: { foo: 'bar' } }
];
}
});
getDashboardContext = dashboardContextProvider(Private, getAppState);
const context = getDashboardContext();
expect(context).to.eql({
bool: {
must: [],
must_not: []
}
});
});

});

});


7 changes: 4 additions & 3 deletions src/core_plugins/kibana/public/dashboard/dashboard_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function dashboardContextProvider(Private, getAppState) {

if (queryBarQuery.language === 'lucene') {
// Add the query bar filter, its handled differently.
bool.must.push(luceneStringToDsl(queryBarQuery.query));
const query = luceneStringToDsl(queryBarQuery.query);
if (query) bool.must.push(query);
}

// Add each of the filter bar filters
Expand All @@ -26,10 +27,10 @@ export function dashboardContextProvider(Private, getAppState) {
if (filter.meta.disabled) return;
if (filter.meta.negate) {
bool.must_not = bool.must_not || [];
bool.must_not.push(esFilter.query || esFilter);
if (esFilter.query || esFilter) bool.must_not.push(esFilter.query || esFilter);
} else {
bool.must = bool.must || [];
bool.must.push(esFilter.query || esFilter);
if (esFilter.query || esFilter) bool.must.push(esFilter.query || esFilter);
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/ui/public/courier/saved_object/saved_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ export function SavedObjectProvider(Promise, Private, Notifier, confirmModalProm

this.searchSource.set(_.defaults(state, fnProps));

this.searchSource.set('query', migrateLegacyQuery(this.searchSource.getOwn('query')));
if (!_.isUndefined(this.searchSource.getOwn('query'))) {
this.searchSource.set('query', migrateLegacyQuery(this.searchSource.getOwn('query')));
}
};

/**
Expand Down

0 comments on commit 31b3ec2

Please sign in to comment.