Skip to content

Commit

Permalink
Set test environment. Write tests. Fix bugs.
Browse files Browse the repository at this point in the history
An example of server test

Rename reports scope var.

Now controllers do not reload to get new data as result of timefilter change.

Add test file for alarms controller. Fix description in the controllers.

Add first test for Watcher factory.

Add tests for watcher factory. Better handle errors in watcher factory. Refactor watcher factory to work with tests.

Fix bug in Watcher.nestedSource(). Properties with false as value were not nested.
Add tests for nestedSource and flatSource methods.

Add more tests for factores which use Sentinl API.

Add tests for saved objects api.
Catch errors in asynch tests.
Execute asynch tests in synch modei, one by one.

Create mock for saved objects API.

Fix saved objects api for Watcher. Use sinon to stub backend.

Delete unused code.

Use scopes in tests. Catch promise exception.

Add saved objects api tests for Script factory.

Fix doc type name for user.

Fix searching savedObjectsAPI using query string.

Fix dashboard spy button watcher creation. Parse catched error objects.

Add firs tests for watchersController.

Delete obsolete code.

Uses standard jasmine done to catch rejections in promisses;

Do not stringify entire error message object. Dosplay only message property.

Dsiplay notify message when user is saved. Display original exception error.

Remove console log.

Do not timeout notifications after index/delete of docs. Indexes
are refreshed after this operations to make new data available instantly.
Fix doc id in reports and alarms controllers.

Delete unneded error message.

Add a test for watchers controller.

Add tests for editor controller.

Add test for watcher edit.
  • Loading branch information
sergibondarenko committed Sep 1, 2017
1 parent a1cf2dd commit f3046b1
Show file tree
Hide file tree
Showing 32 changed files with 1,291 additions and 182 deletions.
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var include = [
'index.js',
'init.js',
'server',
'lib',
'public',
'phantomjs'
];
Expand Down
1 change: 1 addition & 0 deletions lib/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../../server/__tests__/index');
7 changes: 7 additions & 0 deletions public/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
describe('Sentinl', function () {
require('../controllers/__tests__/watchersController');
require('../controllers/__tests__/reportsController');
require('../controllers/__tests__/alarmsController');
require('../services/__tests__/watcher');
require('../services/__tests__/alarm');
require('../services/__tests__/user');
require('../services/__tests__/script');
require('../services/__tests__/report');
});
64 changes: 64 additions & 0 deletions public/controllers/__tests__/alarmsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import moment from 'moment';
import sinon from 'auto-release-sinon';
import Promise from 'bluebird';
import ngMock from 'ng_mock';
import expect from 'expect.js';

import '../alarmsController';

describe('Alarms Controller', function () {

var $scope;
var $httpBackend;
var $route;

function init({hits = []}) {
ngMock.module('kibana');

ngMock.inject(function ($rootScope, $controller, _$route_, $injector, _$httpBackend_) {
$httpBackend = _$httpBackend_;
$httpBackend.whenGET(/\.\.\/api\/sentinl\/set\/interval\/.+/).respond(200, {
status: '200 OK'
});
$httpBackend.whenGET('../api/sentinl/list/alarms').respond(200, {
hits: {
hits: hits
}
});

$route = _$route_;
$route.current = {
locals: {
currentTime: moment('2016-08-08T11:56:42.108Z')
}
};
$scope = $rootScope;
$controller('AlarmsController', {
$scope,
$route,
$uibModal: {}
});
$scope.$digest();
$httpBackend.flush();
});
}


afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});

it('Title and description', function () {
init({});
expect($scope.title).to.equal('Sentinl: Alarms');
expect($scope.description).to.be('Kibi/Kibana Report App for Elasticsearch');
});

it('2 http requests should be made when controller is created', function () {
init({hits: [{id: 1}, {id: 2}]});
expect($scope.alarms.length).to.equal(2);
expect($scope.alarms).to.eql([{id: 1}, {id: 2}]);
});

});
187 changes: 187 additions & 0 deletions public/controllers/__tests__/editorController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import moment from 'moment';
import sinon from 'auto-release-sinon';
import Promise from 'bluebird';
import ngMock from 'ng_mock';
import expect from 'expect.js';
import _ from 'lodash';

import defaultEmailSource from '../../defaults/email_watcher';

import '../editorController';

describe('editorController', function () {
let $httpBackend;
let $scope;
let $route;
let $location;
let $routeParams;
let Watcher;
let Script;
let dataTransfer;
let Notifier;
let watcher;


describe('new watcher', function () {

const initNew = function (done) {
ngMock.module('kibana');

ngMock.inject(function ($rootScope, $controller, _$location_, _$httpBackend_, _$route_,
_Watcher_, _dataTransfer_, _Notifier_, _$routeParams_, _Script_) {
$scope = $rootScope;
$route = _$route_;
$location = _$location_;
$httpBackend = _$httpBackend_;
$routeParams = _$routeParams_;
Watcher = _Watcher_;
Script = _Script_;
dataTransfer = _dataTransfer_;
Notifier = _Notifier_;

$route.current = {
locals: {
currentTime: moment('2016-08-08T11:56:42.108Z')
}
};

watcher = {
_id: '123',
_type: 'sentinl-watcher',
_source: _.cloneDeep(defaultEmailSource)
};

dataTransfer.setWatcher(watcher);

$routeParams.watcherId = undefined;
$location.$$path = '/editor';

$httpBackend.when('GET', '../api/sentinl/config').respond(200, {
es: {
numer_of_results: 50
},
authentication: {
enabled: false,
mode: ''
}
});

sinon.stub(Script, 'list', () => {
return Promise.resolve([
{ _id: '123' },
{ _id: '456' }
]);
});

$controller('EditorController', {
$scope,
$route,
$uibModal: {}
});

$httpBackend.flush();
});
};

beforeEach(function () {
initNew();
});

afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
Notifier.prototype._notifs.length = 0;
});

it('set watcher defaults', function () {
expect($scope.watcher._id).to.equal(watcher._id);
expect(_.isEqual(_.keys($scope.watcher._source).sort(), _.keys(watcher._source).sort())).to.be(true);
});

});


describe('edit watcher', function () {

const initEdit = function (done) {
ngMock.module('kibana');

ngMock.inject(function ($rootScope, $controller, _$location_, _$httpBackend_, _$route_,
_Watcher_, _dataTransfer_, _Notifier_, _$routeParams_, _Script_) {
$scope = $rootScope;
$route = _$route_;
$location = _$location_;
$httpBackend = _$httpBackend_;
$routeParams = _$routeParams_;
Watcher = _Watcher_;
Script = _Script_;
dataTransfer = _dataTransfer_;
Notifier = _Notifier_;

$route.current = {
locals: {
currentTime: moment('2016-08-08T11:56:42.108Z')
}
};

watcher = {
_id: '123',
_type: 'sentinl-watcher',
_source: _.cloneDeep(defaultEmailSource)
};

$routeParams.watcherId = watcher._id;
$location.$$path = '/editor';

$httpBackend.when('GET', '../api/sentinl/config').respond(200, {
es: {
numer_of_results: 50
},
authentication: {
enabled: false,
mode: ''
}
});

sinon.stub(Watcher, 'get', () => {
return Promise.resolve(watcher);
});

sinon.stub(Script, 'list', () => {
return Promise.resolve([
{ _id: '123' },
{ _id: '456' }
]);
});

$controller('EditorController', {
$scope,
$route,
$uibModal: {}
});

});
};

beforeEach(function () {
initEdit();
});

afterEach(function () {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
Notifier.prototype._notifs.length = 0;
});

it('get watcher data', function (done) {
setTimeout(function () {
$httpBackend.flush();
expect($scope.watcher._id).to.equal(watcher._id);
expect(_.isEqual(_.keys($scope.watcher._source).sort(), _.keys(watcher._source).sort())).to.be(true);
done();
});
});

});

});
29 changes: 5 additions & 24 deletions public/controllers/__tests__/reportsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@ describe('Reports Controller', function () {
var $route;

function init({hits = []}) {
ngMock.module('kibana', function ($provide) {
$provide.constant('kbnDefaultAppId', '');
$provide.constant('kibiDefaultDashboardTitle', '');
$provide.constant('elasticsearchPlugins', ['siren-join']);
});
ngMock.module('kibana');

ngMock.inject(function (kibiState, $rootScope, $controller, _$route_, $injector, _$httpBackend_) {
ngMock.inject(function ($rootScope, $controller, _$route_, $injector, _$httpBackend_) {
$httpBackend = _$httpBackend_;
$httpBackend.whenGET(/\.\.\/api\/sentinl\/set\/interval\/.+/).respond(200, {
status: '200 OK'
Expand All @@ -33,7 +29,7 @@ describe('Reports Controller', function () {
$route = _$route_;
$route.current = {
locals: {
currentTime: moment('2016-12-08T11:56:42.108Z')
currentTime: moment('2016-08-08T11:56:42.108Z')
}
};
$scope = $rootScope;
Expand Down Expand Up @@ -61,23 +57,8 @@ describe('Reports Controller', function () {

it('2 http requests should be made when controller is created', function () {
init({hits: [{id: 1}, {id: 2}]});
expect($scope.elasticReports.length).to.equal(2);
expect($scope.elasticReports).to.eql([{id: 1}, {id: 2}]);
});

it('should refresh when timefilter.time changed', function () {
init({});
const spy = sinon.spy($route, 'reload');
$scope.timefilter = {
time: {
from: 'now/y',
mode: 'quick',
to: 'now/y'
}
};
$scope.$digest();
sinon.assert.calledOnce(spy);
$httpBackend.flush();
expect($scope.reports.length).to.equal(2);
expect($scope.reports).to.eql([{id: 1}, {id: 2}]);
});

});
Loading

0 comments on commit f3046b1

Please sign in to comment.