1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/routes/admin-api/metrics.test.js
Christopher Kolstad c17a1980a2
Add service layer
This simplifies stores to just be storage interaction, they no longer react to events.

Controllers now call services and awaits the result from the call.

When the service calls are returned the database is updated.
This simplifies testing dramatically, cause you know that your state is
updated when returned from a call, rather than hoping the store has
picked up the event (which really was a command) and reacted to it.

Events are still emitted from eventStore, so other parts of the app can
react to events as they're being sent out.

As part of the move to services, we now also emit an application-created
event when we see a new client application.

Fixes: #685
Fixes: #595
2021-01-21 10:59:19 +01:00

183 lines
5.1 KiB
JavaScript

'use strict';
const test = require('ava');
const supertest = require('supertest');
const { EventEmitter } = require('events');
const store = require('../../../test/fixtures/store');
const permissions = require('../../../test/fixtures/permissions');
const getLogger = require('../../../test/fixtures/no-logger');
const getApp = require('../../app');
const { UPDATE_APPLICATION } = require('../../permissions');
const { createServices } = require('../../services');
const eventBus = new EventEmitter();
function getSetup() {
const stores = store.createStores();
const perms = permissions();
const config = {
baseUriPath: '',
eventBus,
extendedPermissions: true,
preRouterHook: perms.hook,
getLogger,
};
const services = createServices(stores, config);
const app = getApp({ ...config, stores }, services);
return {
request: supertest(app),
stores,
perms,
};
}
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);
});
});
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);
});
});
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);
const { request, perms } = getSetup();
const appName = '123!23';
perms.withPermissions(UPDATE_APPLICATION);
return request
.post(`/api/admin/metrics/applications/${appName}`)
.send({ appName, strategies: ['default'] })
.expect(202);
});
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);
});
test('should accept a delete call to unknown application', t => {
t.plan(0);
const { request, perms } = getSetup();
const appName = 'unknown';
perms.withPermissions(UPDATE_APPLICATION);
return request
.delete(`/api/admin/metrics/applications/${appName}`)
.expect(200);
});
test('should delete application', t => {
t.plan(0);
const { request, stores, perms } = getSetup();
const appName = 'deletable-test';
perms.withPermissions(UPDATE_APPLICATION);
stores.clientApplicationsStore.upsert({ appName });
return request
.delete(`/api/admin/metrics/applications/${appName}`)
.expect(200);
});