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/archive.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

116 lines
3.2 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 { createServices } = require('../../services');
const { UPDATE_FEATURE } = require('../../permissions');
const eventBus = new EventEmitter();
function getSetup() {
const base = `/random${Math.round(Math.random() * 1000)}`;
const stores = store.createStores();
const perms = permissions();
const config = {
baseUriPath: base,
stores,
eventBus,
extendedPermissions: true,
preRouterHook: perms.hook,
getLogger,
};
const services = createServices(stores, config);
const app = getApp(config, services);
return {
base,
perms,
archiveStore: stores.featureToggleStore,
eventStore: stores.eventStore,
featureToggleService: services.featureToggleService,
request: supertest(app),
};
}
test('should get empty getFeatures via admin', t => {
t.plan(1);
const { request, base } = getSetup();
return request
.get(`${base}/api/admin/archive/features`)
.expect('Content-Type', /json/)
.expect(200)
.expect(res => {
t.true(res.body.features.length === 0);
});
});
test('should get archived toggles via admin', t => {
t.plan(1);
const { request, base, archiveStore } = getSetup();
archiveStore.addArchivedFeature({
name: 'test1',
strategies: [{ name: 'default' }],
});
archiveStore.addArchivedFeature({
name: 'test2',
strategies: [{ name: 'default' }],
});
return request
.get(`${base}/api/admin/archive/features`)
.expect('Content-Type', /json/)
.expect(200)
.expect(res => {
t.true(res.body.features.length === 2);
});
});
test('should revive toggle', t => {
t.plan(0);
const name = 'name1';
const { request, base, archiveStore, perms } = getSetup();
perms.withPermissions(UPDATE_FEATURE);
archiveStore.addArchivedFeature({
name,
strategies: [{ name: 'default' }],
});
return request.post(`${base}/api/admin/archive/revive/${name}`).expect(200);
});
test('should create event when reviving toggle', async t => {
t.plan(4);
const name = 'name1';
const {
request,
base,
featureToggleService,
eventStore,
perms,
} = getSetup();
perms.withPermissions(UPDATE_FEATURE);
await featureToggleService.addArchivedFeature({
name,
strategies: [{ name: 'default' }],
});
await request.post(`${base}/api/admin/archive/revive/${name}`);
const events = await eventStore.getEvents();
t.is(events.length, 1);
t.is(events[0].type, 'feature-revived');
t.is(events[0].data.name, name);
t.is(events[0].createdBy, 'unknown');
});
test('should require toggle name when reviving', t => {
t.plan(0);
const { request, base } = getSetup();
return request.post(`${base}/api/admin/archive/revive/`).expect(404);
});