2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-17 09:24:49 +01:00
|
|
|
const test = require('ava');
|
2020-04-14 22:29:11 +02:00
|
|
|
const supertest = require('supertest');
|
|
|
|
const { EventEmitter } = require('events');
|
|
|
|
const store = require('../../../test/fixtures/store');
|
2018-12-19 10:36:56 +01:00
|
|
|
const permissions = require('../../../test/fixtures/permissions');
|
2019-04-30 21:14:23 +02:00
|
|
|
const getLogger = require('../../../test/fixtures/no-logger');
|
2017-06-28 10:20:22 +02:00
|
|
|
const getApp = require('../../app');
|
2018-12-19 10:36:56 +01:00
|
|
|
const { UPDATE_APPLICATION } = require('../../permissions');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
|
|
|
const eventBus = new EventEmitter();
|
|
|
|
|
|
|
|
function getSetup() {
|
|
|
|
const stores = store.createStores();
|
2018-12-19 10:36:56 +01:00
|
|
|
const perms = permissions();
|
2017-06-28 10:20:22 +02:00
|
|
|
const app = getApp({
|
|
|
|
baseUriPath: '',
|
|
|
|
stores,
|
|
|
|
eventBus,
|
2018-12-19 10:36:56 +01:00
|
|
|
extendedPermissions: true,
|
|
|
|
preRouterHook: perms.hook,
|
2019-04-30 21:14:23 +02:00
|
|
|
getLogger,
|
2017-06-28 10:20:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
request: supertest(app),
|
|
|
|
stores,
|
2018-12-19 10:36:56 +01:00
|
|
|
perms,
|
2017-06-28 10:20:22 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test('should return seen toggles even when there is nothing', t => {
|
|
|
|
t.plan(1);
|
|
|
|
const { request } = getSetup();
|
|
|
|
return request
|
|
|
|
.get('/api/admin/metrics/seen-toggles')
|
|
|
|
.expect(200)
|
|
|
|
.expect(res => {
|
|
|
|
t.true(res.body.length === 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return list of seen-toggles per app', t => {
|
|
|
|
t.plan(3);
|
|
|
|
const { request, stores } = getSetup();
|
|
|
|
const appName = 'asd!23';
|
|
|
|
stores.clientMetricsStore.emit('metrics', {
|
|
|
|
appName,
|
|
|
|
instanceId: 'instanceId',
|
|
|
|
bucket: {
|
|
|
|
start: new Date(),
|
|
|
|
stop: new Date(),
|
|
|
|
toggles: {
|
|
|
|
toggleX: { yes: 123, no: 0 },
|
|
|
|
toggleY: { yes: 123, no: 0 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return request
|
|
|
|
.get('/api/admin/metrics/seen-toggles')
|
|
|
|
.expect(200)
|
|
|
|
.expect(res => {
|
|
|
|
const seenAppsWithToggles = res.body;
|
|
|
|
t.true(seenAppsWithToggles.length === 1);
|
|
|
|
t.true(seenAppsWithToggles[0].appName === appName);
|
|
|
|
t.true(seenAppsWithToggles[0].seenToggles.length === 2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return feature-toggles metrics even when there is nothing', t => {
|
|
|
|
t.plan(0);
|
|
|
|
const { request } = getSetup();
|
|
|
|
return request.get('/api/admin/metrics/feature-toggles').expect(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should return metrics for all toggles', t => {
|
|
|
|
t.plan(2);
|
|
|
|
const { request, stores } = getSetup();
|
|
|
|
const appName = 'asd!23';
|
|
|
|
stores.clientMetricsStore.emit('metrics', {
|
|
|
|
appName,
|
|
|
|
instanceId: 'instanceId',
|
|
|
|
bucket: {
|
|
|
|
start: new Date(),
|
|
|
|
stop: new Date(),
|
|
|
|
toggles: {
|
|
|
|
toggleX: { yes: 123, no: 0 },
|
|
|
|
toggleY: { yes: 123, no: 0 },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return request
|
|
|
|
.get('/api/admin/metrics/feature-toggles')
|
|
|
|
.expect(200)
|
|
|
|
.expect(res => {
|
|
|
|
const metrics = res.body;
|
|
|
|
t.true(metrics.lastHour !== undefined);
|
|
|
|
t.true(metrics.lastMinute !== undefined);
|
|
|
|
});
|
|
|
|
});
|
2018-01-17 15:31:53 +01:00
|
|
|
|
2018-11-29 21:45:26 +01:00
|
|
|
test('should return empty list of client applications', t => {
|
|
|
|
t.plan(1);
|
|
|
|
const { request } = getSetup();
|
|
|
|
|
|
|
|
return request
|
|
|
|
.get('/api/admin/metrics/applications')
|
|
|
|
.expect(200)
|
|
|
|
.expect(res => {
|
|
|
|
t.true(res.body.applications.length === 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-17 15:31:53 +01:00
|
|
|
test('should return applications', t => {
|
|
|
|
t.plan(2);
|
|
|
|
const { request, stores } = getSetup();
|
|
|
|
const appName = '123!23';
|
|
|
|
|
|
|
|
stores.clientApplicationsStore.upsert({ appName });
|
|
|
|
|
|
|
|
return request
|
|
|
|
.get(`/api/admin/metrics/applications/`)
|
|
|
|
.expect(200)
|
|
|
|
.expect(res => {
|
|
|
|
const metrics = res.body;
|
|
|
|
t.true(metrics.applications.length === 1);
|
|
|
|
t.true(metrics.applications[0].appName === appName);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should store application', t => {
|
|
|
|
t.plan(0);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, perms } = getSetup();
|
2018-01-17 15:31:53 +01:00
|
|
|
const appName = '123!23';
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(UPDATE_APPLICATION);
|
2018-01-17 15:31:53 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`/api/admin/metrics/applications/${appName}`)
|
2018-12-19 10:36:56 +01:00
|
|
|
.send({ appName, strategies: ['default'] })
|
2018-01-17 15:31:53 +01:00
|
|
|
.expect(202);
|
|
|
|
});
|
2019-05-10 13:47:56 +02:00
|
|
|
|
|
|
|
test('should store application details wihtout strategies', t => {
|
|
|
|
t.plan(0);
|
|
|
|
const { request, perms } = getSetup();
|
|
|
|
const appName = '123!23';
|
|
|
|
perms.withPermissions(UPDATE_APPLICATION);
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`/api/admin/metrics/applications/${appName}`)
|
|
|
|
.send({ appName, url: 'htto://asd.com' })
|
|
|
|
.expect(202);
|
|
|
|
});
|