2016-08-23 01:29:47 +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_FEATURE, CREATE_FEATURE } = require('../../permissions');
|
2016-11-11 15:46:59 +01:00
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
const eventBus = new EventEmitter();
|
|
|
|
|
2017-06-28 10:20:22 +02:00
|
|
|
function getSetup() {
|
2016-11-13 15:41:35 +01:00
|
|
|
const base = `/random${Math.round(Math.random() * 1000)}`;
|
|
|
|
const stores = store.createStores();
|
2018-12-19 10:36:56 +01:00
|
|
|
const perms = permissions();
|
2016-11-13 21:07:14 +01:00
|
|
|
const app = getApp({
|
2016-11-13 15:41:35 +01:00
|
|
|
baseUriPath: base,
|
|
|
|
stores,
|
2016-11-30 23:41:57 +01:00
|
|
|
eventBus,
|
2018-12-19 10:36:56 +01:00
|
|
|
extendedPermissions: true,
|
|
|
|
preRouterHook: perms.hook,
|
2019-04-30 21:14:23 +02:00
|
|
|
getLogger,
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2016-08-23 01:29:47 +02:00
|
|
|
|
2016-11-13 15:41:35 +01:00
|
|
|
return {
|
|
|
|
base,
|
2018-12-19 10:36:56 +01:00
|
|
|
perms,
|
2016-11-13 15:41:35 +01:00
|
|
|
featureToggleStore: stores.featureToggleStore,
|
|
|
|
request: supertest(app),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:20:22 +02:00
|
|
|
test('should get empty getFeatures via admin', t => {
|
|
|
|
t.plan(1);
|
2016-11-13 15:41:35 +01:00
|
|
|
const { request, base } = getSetup();
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.get(`${base}/api/admin/features`)
|
2016-11-13 15:41:35 +01:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2017-06-28 10:20:22 +02:00
|
|
|
.expect(res => {
|
2016-11-13 15:41:35 +01:00
|
|
|
t.true(res.body.features.length === 0);
|
2016-08-23 01:29:47 +02:00
|
|
|
});
|
2016-11-13 15:41:35 +01:00
|
|
|
});
|
2016-08-23 01:29:47 +02:00
|
|
|
|
2016-11-13 15:41:35 +01:00
|
|
|
test('should get one getFeature', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(1);
|
2016-11-13 15:41:35 +01:00
|
|
|
const { request, featureToggleStore, base } = getSetup();
|
2017-06-28 10:20:22 +02:00
|
|
|
featureToggleStore.addFeature({
|
|
|
|
name: 'test_',
|
|
|
|
strategies: [{ name: 'default_' }],
|
|
|
|
});
|
2016-08-23 01:29:47 +02:00
|
|
|
|
2016-11-13 15:41:35 +01:00
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.get(`${base}/api/admin/features`)
|
2016-11-13 15:41:35 +01:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2017-06-28 10:20:22 +02:00
|
|
|
.expect(res => {
|
2016-11-13 15:41:35 +01:00
|
|
|
t.true(res.body.features.length === 1);
|
|
|
|
});
|
|
|
|
});
|
2016-08-23 01:29:47 +02:00
|
|
|
|
2016-11-13 15:41:35 +01:00
|
|
|
test('should add version numbers for /features', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(1);
|
2016-11-13 15:41:35 +01:00
|
|
|
const { request, featureToggleStore, base } = getSetup();
|
2017-06-28 10:20:22 +02:00
|
|
|
featureToggleStore.addFeature({
|
|
|
|
name: 'test2',
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
});
|
2016-09-05 16:50:52 +02:00
|
|
|
|
2016-11-13 15:41:35 +01:00
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.get(`${base}/api/admin/features`)
|
2016-11-13 15:41:35 +01:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2017-06-28 10:20:22 +02:00
|
|
|
.expect(res => {
|
2016-11-13 15:41:35 +01:00
|
|
|
t.true(res.body.version === 1);
|
|
|
|
});
|
2016-08-23 01:29:47 +02:00
|
|
|
});
|
2016-11-13 15:41:35 +01:00
|
|
|
|
2016-11-15 21:27:03 +01:00
|
|
|
test('should require at least one strategy when creating a feature toggle', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(0);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(CREATE_FEATURE);
|
2016-11-15 21:27:03 +01:00
|
|
|
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.post(`${base}/api/admin/features`)
|
2016-11-15 21:27:03 +01:00
|
|
|
.send({ name: 'sample.missing.strategy' })
|
|
|
|
.set('Content-Type', 'application/json')
|
2017-06-28 10:20:22 +02:00
|
|
|
.expect(400);
|
2016-11-15 21:27:03 +01:00
|
|
|
});
|
|
|
|
|
2018-01-20 13:28:04 +01:00
|
|
|
test('should be allowed to use new toggle name', t => {
|
|
|
|
t.plan(0);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(CREATE_FEATURE);
|
2018-01-20 13:28:04 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/features/validate`)
|
|
|
|
.send({ name: 'new.name' })
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
|
|
|
});
|
|
|
|
|
2019-02-08 13:56:47 +01:00
|
|
|
test('should be allowed to have variants="null"', t => {
|
|
|
|
t.plan(0);
|
|
|
|
const { request, base, perms } = getSetup();
|
|
|
|
perms.withPermissions(CREATE_FEATURE);
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/features`)
|
|
|
|
.send({
|
|
|
|
name: 'new.name.null',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
variants: null,
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(201);
|
|
|
|
});
|
|
|
|
|
2018-01-20 13:28:04 +01:00
|
|
|
test('should not be allowed to reuse active toggle name', t => {
|
|
|
|
t.plan(1);
|
|
|
|
const { request, featureToggleStore, base } = getSetup();
|
|
|
|
featureToggleStore.addFeature({
|
|
|
|
name: 'ts',
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
});
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/features/validate`)
|
|
|
|
.send({ name: 'ts' })
|
|
|
|
.set('Content-Type', 'application/json')
|
2019-01-02 12:38:58 +01:00
|
|
|
.expect(400)
|
2018-01-20 13:28:04 +01:00
|
|
|
.expect(res => {
|
2019-01-02 12:38:58 +01:00
|
|
|
t.true(
|
|
|
|
res.body.details[0].message ===
|
2020-04-14 22:29:11 +02:00
|
|
|
'A toggle with that name already exist',
|
2019-01-02 12:38:58 +01:00
|
|
|
);
|
2018-01-20 13:28:04 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not be allowed to reuse archived toggle name', t => {
|
|
|
|
t.plan(1);
|
|
|
|
const { request, featureToggleStore, base } = getSetup();
|
|
|
|
featureToggleStore.addArchivedFeature({
|
|
|
|
name: 'ts.archived',
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
});
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/features/validate`)
|
|
|
|
.send({ name: 'ts.archived' })
|
|
|
|
.set('Content-Type', 'application/json')
|
2019-01-02 12:38:58 +01:00
|
|
|
.expect(400)
|
2018-01-20 13:28:04 +01:00
|
|
|
.expect(res => {
|
|
|
|
t.true(
|
2019-01-02 12:38:58 +01:00
|
|
|
res.body.details[0].message ===
|
2020-04-14 22:29:11 +02:00
|
|
|
'An archived toggle with that name already exist',
|
2018-01-20 13:28:04 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-15 21:27:03 +01:00
|
|
|
test('should require at least one strategy when updating a feature toggle', t => {
|
2017-06-28 10:20:22 +02:00
|
|
|
t.plan(0);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, featureToggleStore, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(UPDATE_FEATURE);
|
2017-06-28 10:20:22 +02:00
|
|
|
featureToggleStore.addFeature({
|
|
|
|
name: 'ts',
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
});
|
2016-11-15 21:27:03 +01:00
|
|
|
|
|
|
|
return request
|
2017-06-28 10:20:22 +02:00
|
|
|
.put(`${base}/api/admin/features/ts`)
|
2016-11-15 21:27:03 +01:00
|
|
|
.send({ name: 'ts' })
|
|
|
|
.set('Content-Type', 'application/json')
|
2017-06-28 10:20:22 +02:00
|
|
|
.expect(400);
|
2016-11-15 21:27:03 +01:00
|
|
|
});
|
2017-10-23 17:13:26 +02:00
|
|
|
|
2017-11-02 10:30:14 +01:00
|
|
|
test('valid feature names should pass validation', t => {
|
2017-10-23 17:13:26 +02:00
|
|
|
t.plan(0);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(CREATE_FEATURE);
|
2017-10-23 17:13:26 +02:00
|
|
|
|
|
|
|
const validNames = [
|
|
|
|
'com.example',
|
|
|
|
'com.exampleFeature',
|
|
|
|
'com.example-company.feature',
|
|
|
|
'com.example-company.exampleFeature',
|
|
|
|
'123',
|
|
|
|
'com.example-company.someFeature.123',
|
|
|
|
];
|
|
|
|
|
|
|
|
return Promise.all(
|
|
|
|
validNames.map(name =>
|
|
|
|
request
|
|
|
|
.post(`${base}/api/admin/features`)
|
|
|
|
.send({
|
|
|
|
name,
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
2020-04-14 22:29:11 +02:00
|
|
|
.expect(201),
|
|
|
|
),
|
2017-10-23 17:13:26 +02:00
|
|
|
);
|
|
|
|
});
|
2017-11-02 10:30:14 +01:00
|
|
|
|
|
|
|
test('invalid feature names should not pass validation', t => {
|
|
|
|
t.plan(0);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(CREATE_FEATURE);
|
2017-11-02 10:30:14 +01:00
|
|
|
|
|
|
|
const invalidNames = [
|
|
|
|
'some example',
|
|
|
|
'some$example',
|
|
|
|
'me&me',
|
|
|
|
' ',
|
|
|
|
'o2%ae',
|
|
|
|
];
|
|
|
|
|
|
|
|
return Promise.all(
|
|
|
|
invalidNames.map(name =>
|
|
|
|
request
|
|
|
|
.post(`${base}/api/admin/features`)
|
|
|
|
.send({
|
|
|
|
name,
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
2020-04-14 22:29:11 +02:00
|
|
|
.expect(400),
|
|
|
|
),
|
2017-11-02 10:30:14 +01:00
|
|
|
);
|
|
|
|
});
|
2018-12-01 12:03:47 +01:00
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
// Make sure current UI works. Should align on joi errors in future.
|
2018-12-01 12:03:47 +01:00
|
|
|
test('invalid feature names should have error msg', t => {
|
|
|
|
t.plan(1);
|
2018-12-19 10:36:56 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
2018-12-19 13:17:44 +01:00
|
|
|
perms.withPermissions(CREATE_FEATURE);
|
2018-12-01 12:03:47 +01:00
|
|
|
|
|
|
|
const name = 'ØÆ`';
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/features`)
|
|
|
|
.send({
|
|
|
|
name,
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.expect(400)
|
|
|
|
.expect(res => {
|
2018-12-12 10:09:38 +01:00
|
|
|
t.true(
|
2020-04-14 22:29:11 +02:00
|
|
|
res.body.details[0].message === '"name" must be URL friendly',
|
2018-12-12 10:09:38 +01:00
|
|
|
);
|
2018-12-01 12:03:47 +01:00
|
|
|
});
|
|
|
|
});
|
2019-01-24 08:39:21 +01:00
|
|
|
|
|
|
|
test('should not allow variants with same name when creating feature flag', t => {
|
|
|
|
t.plan(0);
|
2019-01-24 11:26:07 +01:00
|
|
|
const { request, base, perms } = getSetup();
|
|
|
|
perms.withPermissions(CREATE_FEATURE);
|
2019-01-24 08:39:21 +01:00
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/features`)
|
|
|
|
.send({
|
2019-01-24 11:26:07 +01:00
|
|
|
name: 't.variant',
|
|
|
|
enabled: true,
|
2019-01-24 08:39:21 +01:00
|
|
|
strategies: [{ name: 'default' }],
|
2019-01-24 11:26:07 +01:00
|
|
|
variants: [
|
|
|
|
{ name: 'variant1', weight: 50 },
|
|
|
|
{ name: 'variant1', weight: 50 },
|
|
|
|
],
|
2019-01-24 08:39:21 +01:00
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
2019-01-24 11:26:07 +01:00
|
|
|
.expect(400);
|
2019-01-24 08:39:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should not allow variants with same name when updating feature flag', t => {
|
|
|
|
t.plan(0);
|
2019-01-24 11:26:07 +01:00
|
|
|
const { request, featureToggleStore, base, perms } = getSetup();
|
|
|
|
perms.withPermissions(UPDATE_FEATURE);
|
|
|
|
|
2019-01-24 08:39:21 +01:00
|
|
|
featureToggleStore.addFeature({
|
|
|
|
name: 'ts',
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
});
|
|
|
|
|
|
|
|
return request
|
|
|
|
.put(`${base}/api/admin/features/ts`)
|
|
|
|
.send({
|
|
|
|
name: 'ts',
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
variants: [{ name: 'variant1' }, { name: 'variant1' }],
|
|
|
|
})
|
|
|
|
.set('Content-Type', 'application/json')
|
2019-01-24 11:26:07 +01:00
|
|
|
.expect(400);
|
2019-01-24 08:39:21 +01:00
|
|
|
});
|
2019-03-07 22:19:48 +01:00
|
|
|
|
|
|
|
test('should toggle on', t => {
|
|
|
|
t.plan(1);
|
|
|
|
const { request, featureToggleStore, base, perms } = getSetup();
|
|
|
|
perms.withPermissions(UPDATE_FEATURE);
|
|
|
|
|
|
|
|
featureToggleStore.addFeature({
|
|
|
|
name: 'toggle.disabled',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
});
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/features/toggle.disabled/toggle/on`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.expect(res => {
|
|
|
|
t.true(res.body.enabled === true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should toggle off', t => {
|
|
|
|
t.plan(1);
|
|
|
|
const { request, featureToggleStore, base, perms } = getSetup();
|
|
|
|
perms.withPermissions(UPDATE_FEATURE);
|
|
|
|
|
|
|
|
featureToggleStore.addFeature({
|
|
|
|
name: 'toggle.enabled',
|
|
|
|
enabled: true,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
});
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/features/toggle.enabled/toggle/off`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.expect(res => {
|
|
|
|
t.true(res.body.enabled === false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should toggle', t => {
|
|
|
|
t.plan(1);
|
|
|
|
const { request, featureToggleStore, base, perms } = getSetup();
|
|
|
|
perms.withPermissions(UPDATE_FEATURE);
|
|
|
|
|
|
|
|
featureToggleStore.addFeature({
|
|
|
|
name: 'toggle.disabled',
|
|
|
|
enabled: false,
|
|
|
|
strategies: [{ name: 'default' }],
|
|
|
|
});
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(`${base}/api/admin/features/toggle.disabled/toggle`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.expect(res => {
|
|
|
|
t.true(res.body.enabled === true);
|
|
|
|
});
|
|
|
|
});
|