it('queries with size until all 500 docs returned', async function () { const search = new MockSource(); const indexPattern = search.get('index'); sinon.stub(indexPattern, 'toDetailedIndexList').returns(Promise.resolve([ { index: 'one', min: 0, max: 1 }, { index: 'two', min: 0, max: 1 }, { index: 'three', min: 0, max: 1 }, { index: 'four', min: 0, max: 1 }, { index: 'five', min: 0, max: 1 }, ])); const req = new SegmentedReq(search); req._handle.setDirection('desc'); req._handle.setSortFn(new HitSortFn('desc')); req._handle.setSize(500); await req.start(); // first 200 expect((await req.getFetchParams()).body.size).to.be(500); await req.handleResponse({ hits: { total: 1000, hits: times(200, (i) => ({ i })) } }); // total = 400 expect((await req.getFetchParams()).body.size).to.be(500); await req.handleResponse({ hits: { total: 1000, hits: times(200, (i) => ({ i })) } }); // total = 600 expect((await req.getFetchParams()).body.size).to.be(500); await req.handleResponse({ hits: { total: 1000, hits: times(200, (i) => ({ i })) } }); expect((await req.getFetchParams()).body.size).to.be(0); await req.handleResponse({ hits: { total: 1000, hits: times(200, (i) => ({ i })) } }); expect((await req.getFetchParams()).body.size).to.be(0); await req.handleResponse({ hits: { total: 1000, hits: times(200, (i) => ({ i })) } }); });
it('fetches a single search source', function () { const resp = searchResp; const mresp = { responses: [resp] }; const source = new SearchSource({ index: indexPattern }); sinon.stub(es, 'msearch').returns(Promise.resolve(mresp)); return fetch .search(source) .then(function (courierResp) { expect(courierResp).to.be(resp); }); });
it('getId when there is: no cookie, no session in es', function (done) { const expectedId = 'does_not_exist'; sinon.stub(kibiSessionHelper, '_generateId', function () { return expectedId; }); makeSureKibiSessionHelperInitialized(kibiSessionHelper).then(function () { resetCounters(); kibiSessionHelper.destroy(); return kibiSessionHelper.getId().then(function (id) { expect(id).to.eql(expectedId); expect($cookies.get('ksid')).to.eql(expectedId); expect(saveSessionCounter).to.be(1); done(); }); }).catch(done); });
it('uses the params from #write() output as the agg params', function () { var vis = new Vis(indexPattern, { type: 'histogram', aggs: [ { type: 'date_histogram', schema: 'segment' } ] }); var aggConfig = vis.aggs.byTypeName.date_histogram[0]; var football = {}; sinon.stub(aggConfig, 'write').returns({ params: football }); var dsl = aggConfig.toDsl(); expect(dsl.date_histogram).to.be(football); });
it('resolves requests waiting for the queue to fill when appropriate', function () { let size = _.random(5, 50); queue.limit = size; let whenFull = Promise.defer(); sinon.stub(whenFull, 'resolve'); queue.resolveWhenFull(whenFull); // push all but one into the queue _.times(size - 1, function () { queue.push(Promise.defer()); }); expect(whenFull.resolve.callCount).to.be(0); queue.push(Promise.defer()); expect(whenFull.resolve.callCount).to.be(1); queue.empty(); });
it('should not save the session automatically', function (done) { const expectedId = 'toId'; sinon.stub(kibiSessionHelper, '_generateId', function () { return expectedId; }); makeSureKibiSessionHelperInitialized(kibiSessionHelper).then(function () { resetCounters(); kibiSessionHelper.destroy(); return kibiSessionHelper._copySessionFromTo('fromId', 'toId').then(function (savedSession) { expect(savedSession.session_data.secret).to.eql(42); expect(savedSession.id).to.eql(expectedId); expect(saveSessionCounter).to.be(0); expect(getSaveSessionCounter).to.be(2); done(); }); }).catch(done); });
ngMock.inject(function (Promise, _$rootScope_, $controller) { var fakeRoute = { current: { locals: { dashboardGroup: dashboardGroup } } }; dashboardGroup.save = sinon.stub().returns(Promise.resolve('123')); $scope = _$rootScope_; $controller('DashboardGroupsEditor', { $scope: $scope, $route: fakeRoute, $element: $('<div><form name="objectForm" class="ng-valid"></div>') }); $scope.$digest(); });
it('saved data should equal retrieved data - there is no cookie', function (done) { const expectedId = 'putget'; const testData = {secret: 1}; sinon.stub(kibiSessionHelper, '_generateId', function () { return expectedId; }); makeSureKibiSessionHelperInitialized(kibiSessionHelper).then(function () { resetCounters(); $cookies.remove('ksid'); return kibiSessionHelper.putData(testData).then(function () { return kibiSessionHelper.getData().then(function (data) { expect(saveSessionCounter).to.be(0); expect(getSaveSessionCounter).to.be(0); expect(data).to.eql(testData); done(); }); }); }).catch(done); });
it('dashboard exist and it has savedSearch and index exists', function () { sinon.stub(es, 'msearch').returns(Promise.resolve({ responses: [ { hits: { total: 42 } } ] })); return dashboardGroups._getDashboardsMetadata(['time-testing-4']) .then(function (metas) { expect(metas.length).to.equal(1); expect(metas[0].count).to.equal(42); expect(metas[0].isPruned).to.equal(false); expect(metas[0].dashboardId).to.equal('time-testing-4'); expect(metas[0].indices).to.eql(['time-testing-4']); }); });
beforeEach(function () { var row = getFakeRow(0, mapping); mapping._id = {indexed: true, type: 'string'}; row._source._id = 'foo'; init($elem, { row: row, columns: [], sorting: [], filtering: sinon.spy(), maxLength: 50, }); sinon.stub(config, 'get').withArgs('metaFields').returns(['_id']); // Open the row $scope.toggleRow(); $scope.$digest(); $details = $elem.next(); });
it('should submit the query', function (done) { let query = { title: '123', save: sinon.stub().returns(Promise.resolve('queryid')) }; let snippet = { john: 'connor', html: 'are you there' }; init({ query: query, snippet: snippet }); expect($scope.holder.htmlPreview).not.to.be.ok(); expect($scope.holder.jsonPreview).not.to.be.ok(); $scope.submit().then(function () { expect($scope.holder.htmlPreview).to.be('are you there'); expect($scope.holder.jsonPreview).to.be.ok(); done(); }).catch(done); });
it('should be null if there is no query nor filters', function () { sinon.stub(es, 'msearch').returns(Promise.resolve({ responses: [ { hits: { total: 42 } } ] })); return dashboardGroups.computeGroups() .then(() => dashboardGroups.updateMetadataOfDashboardIds(['myDashboard'])) .then(function () { const groups = dashboardGroups.getGroups(); const myGroup = _.find(groups, 'id', 'mygroup'); const myDashboard = _.find(myGroup.dashboards, 'id', 'myDashboard'); expect(myDashboard.filterIconMessage).to.be(null); }); });
it('should handle multiple identical emits in the same tick', function () { const obj = new Events(); const handler1 = sinon.stub(); obj.on('test', handler1); const emits = [ obj.emit('test', 'one'), obj.emit('test', 'two'), obj.emit('test', 'three') ]; return Promise .all(emits) .then(function () { expect(handler1.callCount).to.be(emits.length); expect(handler1.getCall(0).calledWith('one')).to.be(true); expect(handler1.getCall(1).calledWith('two')).to.be(true); expect(handler1.getCall(2).calledWith('three')).to.be(true); }); });
it('emit kibi:session:changed:deleted', function (done) { const expectedId1 = 'expectedId1'; const expectedId2 = 'expectedId2'; let counter = 1; sinon.stub(kibiSessionHelper, '_generateId', function () { if (counter === 1) { counter++; return expectedId1; } else if (counter === 2) { counter++; return expectedId2; } throw new Error('Unexpected call to _generateId function'); }); makeSureKibiSessionHelperInitialized(kibiSessionHelper).then(function () { resetCounters(); kibiSessionHelper.destroy(); return kibiSessionHelper.getId().then(function (sessionId1) { expect(sessionId1).to.equal(expectedId1); // now set up spys and emit event const destroySpy = sinon.spy(kibiSessionHelper, 'destroy'); const initSpy = sinon.spy(kibiSessionHelper, 'init'); $rootScope.$emit('kibi:session:changed:deleted', expectedId1); setTimeout(function () { try { expect(kibiSessionHelper.id).to.equal(expectedId2); expect(destroySpy.callCount).to.equal(1); expect(destroySpy.calledBefore(initSpy)).to.be(true); // here is 2 because init -> getId -> init expect(initSpy.callCount).to.equal(2); } catch (err) { done(err); } done(); }, 500); }); }).catch(done); });
it('fetches a single doc source', function () { const doc = { _index: 'test-index', _type: 'test-type', _id: 'test-id', }; const source = new DocSource({ index: doc._index, type: doc._type, id: doc._id }); sinon.stub(es, 'mget').returns(Promise.resolve({ docs: [doc] })); return fetch.doc(source).then(function (resp) { expect(resp).to.be(doc); }); });
it('should say there is 1 filter', function () { sinon.stub(es, 'msearch').returns(Promise.resolve({ responses: [ { hits: { total: 42 } } ] })); appState.filters = [ { meta: { disabled: false } } ]; return dashboardGroups.computeGroups() .then(() => dashboardGroups.updateMetadataOfDashboardIds(['myDashboard'])) .then(function () { const groups = dashboardGroups.getGroups(); const myGroup = _.find(groups, 'id', 'mygroup'); const myDashboard = _.find(myGroup.dashboards, 'id', 'myDashboard'); expect(myDashboard.filterIconMessage).to.be('filters: 1'); }); });
it('should set the default siren-join parameters', function (done) { init({ indexPatterns, savedDashboards, savedSearches }); const timeBasedIndicesStub = sinon.stub(kibiState, 'timeBasedIndices'); timeBasedIndicesStub.withArgs('ia').returns([ 'ia' ]); timeBasedIndicesStub.withArgs('ib').returns([ 'ib' ]); const button = { sourceField: 'fa', sourceIndexPatternId: 'ia', targetField: 'fb', targetIndexPatternId: 'ib' }; sequentialJoinVisHelper.getJoinSequenceFilter('dashboardA', button).then((rel) => { sinon.assert.called(timeBasedIndicesStub); expect(rel.join_sequence).to.have.length(1); expect(rel.join_sequence[0].relation).to.have.length(2); expect(rel.join_sequence[0].relation[0].termsEncoding).to.be('long'); expect(rel.join_sequence[0].relation[1].termsEncoding).to.be('long'); done(); }).catch(done); });
it('should detach a session correctly', function (done) { const currentSessionId = 'current'; const detachedSessionId = 'detached'; const sessionData = { x: true }; let counter = 1; sinon.stub(kibiSessionHelper, '_generateId', function () { if (counter === 1) { counter++; return detachedSessionId; } else if (counter === 2) { counter++; return currentSessionId; } }); makeSureKibiSessionHelperInitialized(kibiSessionHelper).then(function () { resetCounters(); kibiSessionHelper.destroy(); return kibiSessionHelper.putData(sessionData, true) .then(() => kibiSessionHelper.detach() .then((currentSessionId, detachedSessionId) => { expect(currentSessionId).to.be(currentSessionId); expect(detachedSessionId).to.be(detachedSessionId); kibiSessionHelper.getData().then((data) => { expect(data.x).to.be(true); expect(saveSessionCounter).to.be(3); done(); }) .catch(done); }) ) .catch(done); }).catch(done); });
it('should update counts of current dashboard on kibiState changes', function (done) { const stub = sinon.stub(kibiNavBarHelper, 'updateAllCounts'); var counter = 0; kibiState.on('save_with_changes', function (diff) { counter++; if (diff[0] === kibiState._properties.groups) { expect(stub.calledWith([ 'dashboard1' ], `KibiState change ${JSON.stringify([ 'g' ], null, ' ')}`)).to.be(true); } if (diff[0] === kibiState._properties.enabled_relational_panel) { expect(stub.calledWith([ 'dashboard1' ], `KibiState change ${JSON.stringify([ 'e' ], null, ' ')}`)).to.be(true); } if (counter === 2) { done(); } }); [ kibiState._properties.groups, kibiState._properties.enabled_relational_panel ].forEach(function (property) { kibiState.emit('save_with_changes', [ property ]); }); });
ngMock.module('kibana/url', 'kibana', function ($provide) { $provide.service('$route', function () { return { reload: _.noop }; }); appState = { destroy: sinon.stub() }; $provide.service('getAppState', function () { return function () { return appState; }; }); $provide.service('globalState', function () { globalStateMock = new MockState(); globalStateMock.removeFromUrl = function (url) { return url; }; return globalStateMock; }); });
ngMock.inject(function (_$timeout_, $injector, $rootScope, $controller, Private) { var urlHelper = Private(require('ui/kibi/helpers/url_helper')); indexToDashboardMapPromise = Promise.resolve(options.indexToDashboardsMap); var getIndexToDashboardMapStub = sinon.stub(urlHelper, 'getIndexToDashboardMap') .returns(indexToDashboardMapPromise); $timeout = _$timeout_; config = $injector.get('config'); config.set('kibi:relations', options.relations); $scope = $rootScope; var el = '<div><form name="dashboardsForm" class="ng-valid"/><form name="indicesForm" class="ng-valid"/></div>'; $controller('RelationsController', { $scope: $scope, $element: jQuery(el) }); if (options.events) { _.each(options.events, function (func, e) { unbind.push($scope.$on(e, func)); }); } $scope.$digest(); });
it('should get the query from the search meta', function (done) { init({ indexPatterns, savedDashboards, savedSearches }); const timeBasedIndicesStub = sinon.stub(kibiState, 'timeBasedIndices'); timeBasedIndicesStub.withArgs('ia').returns([ 'ia' ]); timeBasedIndicesStub.withArgs('ib').returns([ 'ib' ]); const button = { sourceField: 'fa', sourceIndexPatternId: 'ia', targetField: 'fb', targetIndexPatternId: 'ib' }; sequentialJoinVisHelper.getJoinSequenceFilter('dashboardA', button).then((rel) => { sinon.assert.called(timeBasedIndicesStub); expect(rel.join_sequence).to.have.length(1); expect(rel.join_sequence[0].relation).to.have.length(2); expect(rel.join_sequence[0].relation[0].indices).to.eql([ button.sourceIndexPatternId ]); expect(rel.join_sequence[0].relation[0].path).to.be(button.sourceField); expect(rel.join_sequence[0].relation[0].queries[0].query.bool.must).to.have.length(2); expect(rel.join_sequence[0].relation[0].queries[0].query.bool.must[0]).to.be.eql({ query: { query_string: { query: '*', analyze_wildcard: true } } }); expect(rel.join_sequence[0].relation[0].queries[0].query.bool.must[1]).to.be.eql({ query: { a: 123 } }); expect(rel.join_sequence[0].relation[0].termsEncoding).to.be('long'); expect(rel.join_sequence[0].relation[1].indices).to.eql([ button.targetIndexPatternId ]); expect(rel.join_sequence[0].relation[1].path).to.be(button.targetField); expect(rel.join_sequence[0].relation[1].termsEncoding).to.be('long'); done(); }).catch(done); });
it('should expand the time-based index pattern', function (done) { const currentDashboardId = 'dashboardA'; const button = { sourceField: 'fa', sourceIndexPatternId: 'ia-*', targetField: 'fb', targetIndexPatternId: 'ib' }; init({ currentDashboardId, indexPatterns, savedDashboards }); const timeBasedIndicesStub = sinon.stub(kibiState, 'timeBasedIndices'); timeBasedIndicesStub.withArgs('ia-*').returns([ 'ia-1', 'ia-2' ]); timeBasedIndicesStub.withArgs('ib').returns([ 'ib' ]); sequentialJoinVisHelper.getJoinSequenceFilter(currentDashboardId, button).then((rel) => { sinon.assert.called(timeBasedIndicesStub); expect(rel.join_sequence).to.have.length(1); expect(rel.join_sequence[0].relation).to.have.length(2); expect(rel.join_sequence[0].relation[0].indices).to.eql([ 'ia-1', 'ia-2' ]); expect(rel.join_sequence[0].relation[1].indices).to.eql([ button.targetIndexPatternId ]); done(); }).catch(done); });
ngMock.inject(function (Promise, _globalState_, _kibiState_, _$httpBackend_, _$timeout_, _$rootScope_, Private) { globalState = _globalState_; kibiState = _kibiState_; $timeout = _$timeout_; $rootScope = _$rootScope_; kibiNavBarHelper = Private(require('ui/kibi/directives/kibi_nav_bar_helper')); $httpBackend = _$httpBackend_; sinon.stub(kibiState, '_getDashboardsIdInConnectedComponent').returns(dashboardsIdsInConnectedComponents); sinon.stub(kibiState, '_getCurrentDashboardId').returns('dashboard1'); timeBasedIndicesStub = sinon.stub(kibiState, 'timeBasedIndices').returns(Promise.resolve([ 'id' ])); sinon.stub(chrome, 'getBasePath').returns(''); sinon.stub(chrome, 'getActiveTabId').returns('dashboard'); kibiNavBarHelper._setDashboardGroups(dashboardGroups); const dashboardGroupHelper = Private(require('ui/kibi/helpers/dashboard_group_helper')); getDashboardsMetadataStub = sinon.stub(dashboardGroupHelper, 'getDashboardsMetadata'); });
beforeEach(ngMock.module('kibana', function ($routeProvider) { $rp = $routeProvider; sinon.stub($rp, 'otherwise'); sinon.stub($rp, 'when'); }));
beforeEach(function () { getActiveTabStub = sinon.stub(require('ui/chrome'), 'getActiveTab'); });
describe('pattern checker', function () { let $httpBackend; let $compile; let $rootScope; let apiResponse; let $timeout; const notifyFatalStub = sinon.stub(); beforeEach(ngMock.module('kibana')); beforeEach(ngMock.module(function ($provide) { notifyFatalStub.reset(); $provide.value('Notifier', function () { this.fatal = notifyFatalStub; }); })); beforeEach(ngMock.inject(function ($injector) { $httpBackend = $injector.get('$httpBackend'); $compile = $injector.get('$compile'); $rootScope = $injector.get('$rootScope'); $timeout = $injector.get('$timeout'); apiResponse = $httpBackend.when('POST', /\/api\/kibana\/.*\/_count/); })); it('should display the number of documents in a given index pattern', function () { apiResponse.respond(200, { count: 1 }); const element = $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope); $httpBackend.flush(); $rootScope.$digest(); expect(_.contains(element.html(), `1 results`)).to.be.ok(); }); it('should poll the api for changes to the document count and update the ui', function () { apiResponse.respond(200, { count: 1 }); const element = $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope); $httpBackend.flush(); $rootScope.$digest(); expect(_.contains(element.html(), `1 results`)).to.be.ok(); apiResponse.respond(200, { count: 2 }); $timeout.flush(); $httpBackend.flush(); $rootScope.$digest(); expect(_.contains(element.html(), `2 results`)).to.be.ok(); }); it('should display 0 results when API responds with 404', function () { apiResponse.respond(404); const element = $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope); $httpBackend.flush(); $rootScope.$digest(); expect(_.contains(element.html(), `0 results`)).to.be.ok(); }); it('should throw a fatal notificaiton for any error other than a 404', function () { apiResponse.respond(500, 'Bad things happened'); $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope); $httpBackend.flush(); $rootScope.$digest(); expect(notifyFatalStub.called).to.be.ok(); }); it('should stop polling when the scope is destroyed', function () { apiResponse.respond(200, { count: 1 }); const element = $compile('<pattern-checker pattern="logstash"></pattern-checker>')($rootScope); const scope = element.scope(); $httpBackend.flush(); $rootScope.$digest(); expect(_.contains(element.html(), `1 results`)).to.be.ok(); scope.$destroy(); $timeout.flush(); $httpBackend.verifyNoOutstandingRequest(); }); });
it('unbinds when the $scope is destroyed', function () { let binder = new Binder($scope); sinon.stub(binder, 'destroy'); $scope.$destroy(); expect(binder.destroy.callCount).to.be(1); });
it('accepts a $scope and listens for $destroy', function () { sinon.stub($scope, '$on'); let binder = new Binder($scope); expect($scope.$on.callCount).to.be(1); expect($scope.$on.args[0][0]).to.be('$destroy'); });
beforeEach(function () { $offStub = sinon.stub($.fn, 'off'); reflowWatcher.destroy(); $offStub.restore(); });