1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/test/unit/routes/metrics.test.js

181 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-11-10 22:05:50 +01:00
'use strict';
2016-11-13 15:41:35 +01:00
const test = require('ava');
const store = require('./fixtures/store');
2016-11-10 22:05:50 +01:00
const supertest = require('supertest');
2016-11-13 15:41:35 +01:00
const logger = require('../../../lib/logger');
const getApp = require('../../../lib/app');
2016-11-10 22:05:50 +01:00
const { EventEmitter } = require('events');
const eventBus = new EventEmitter();
2016-11-13 15:41:35 +01:00
test.beforeEach(() => {
logger.setLevel('FATAL');
});
2016-11-10 22:05:50 +01:00
2016-11-13 15:41:35 +01:00
function getSetup () {
const stores = store.createStores();
const app = getApp({
2016-11-13 15:41:35 +01:00
baseUriPath: '',
stores,
eventBus,
2016-11-10 22:05:50 +01:00
});
2016-11-13 15:41:35 +01:00
return {
request: supertest(app),
2016-12-04 14:09:37 +01:00
stores,
2016-11-13 15:41:35 +01:00
};
}
2016-11-10 22:05:50 +01:00
2016-11-13 15:41:35 +01:00
test('should register client', () => {
const { request } = getSetup();
return request
.post('/api/client/register')
.send({
appName: 'demo',
instanceId: 'test',
strategies: ['default'],
started: Date.now(),
interval: 10,
})
.expect(202);
});
2016-11-11 15:46:59 +01:00
2016-11-13 15:41:35 +01:00
test('should require appName field', () => {
const { request } = getSetup();
return request
.post('/api/client/register')
.expect(400);
});
2016-11-11 15:46:59 +01:00
2016-11-13 15:41:35 +01:00
test('should require strategies field', () => {
const { request } = getSetup();
return request
.post('/api/client/register')
.send({
appName: 'demo',
instanceId: 'test',
// strategies: ['default'],
started: Date.now(),
interval: 10,
})
.expect(400);
});
2016-11-11 15:46:59 +01:00
2016-12-02 17:19:59 +01:00
test('should validate client metrics', () => {
const { request } = getSetup();
return request
.post('/api/client/metrics')
2016-12-04 14:09:37 +01:00
.send({ random: 'blush' })
2016-12-02 17:19:59 +01:00
.expect(400);
});
2016-11-13 15:41:35 +01:00
test('should accept client metrics', () => {
const { request } = getSetup();
return request
.post('/api/client/metrics')
.send({
appName: 'demo',
instanceId: '1',
bucket: {
start: Date.now(),
stop: Date.now(),
toggles: {},
},
})
.expect(202);
2016-11-10 22:05:50 +01:00
});
2016-12-01 17:15:55 +01:00
test('should return seen toggles even when there is nothing', t => {
const { request } = getSetup();
return request
.get('/api/client/seen-toggles')
.expect(200)
.expect((res) => {
t.true(res.body.length === 0);
});
});
test('should return list of seen-toggles per app', t => {
const { request, stores } = getSetup();
2016-12-04 14:09:37 +01:00
const appName = 'asd!23';
2016-12-01 17:15:55 +01:00
stores.clientMetricsStore.emit('metrics', {
appName,
instanceId: 'instanceId',
bucket: {
start: new Date(),
stop: new Date(),
toggles: {
2016-12-04 14:09:37 +01:00
toggleX: { yes: 123, no: 0 },
toggleY: { yes: 123, no: 0 }
2016-12-01 17:15:55 +01:00
},
},
});
return request
.get('/api/client/seen-toggles')
.expect(200)
.expect((res) => {
2016-12-04 14:09:37 +01:00
const seenAppsWithToggles = res.body;
2016-12-01 17:15:55 +01:00
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 => {
const { request } = getSetup();
return request
.get('/api/client/metrics/feature-toggles')
2016-12-04 14:09:37 +01:00
.expect(200);
2016-12-01 17:15:55 +01:00
});
test('should return metrics for all toggles', t => {
const { request, stores } = getSetup();
2016-12-04 14:09:37 +01:00
const appName = 'asd!23';
2016-12-01 17:15:55 +01:00
stores.clientMetricsStore.emit('metrics', {
appName,
instanceId: 'instanceId',
bucket: {
start: new Date(),
stop: new Date(),
toggles: {
2016-12-04 14:09:37 +01:00
toggleX: { yes: 123, no: 0 },
toggleY: { yes: 123, no: 0 },
2016-12-01 17:15:55 +01:00
},
},
});
return request
.get('/api/client/metrics/feature-toggles')
.expect(200)
.expect((res) => {
2016-12-04 14:09:37 +01:00
const metrics = res.body;
2016-12-01 17:15:55 +01:00
t.true(metrics.lastHour !== undefined);
t.true(metrics.lastMinute !== undefined);
});
});
test('should return list of client strategies', t => {
2016-12-04 14:09:37 +01:00
const { request } = getSetup();
2016-12-01 17:15:55 +01:00
return request
.get('/api/client/strategies')
.expect(200)
.expect((res) => {
t.true(res.body.length === 0);
});
});
test('should return list of client applications', t => {
2016-12-04 14:09:37 +01:00
const { request } = getSetup();
2016-12-01 17:15:55 +01:00
return request
.get('/api/client/applications')
.expect(200)
.expect((res) => {
t.true(res.body.applications.length === 0);
});
});