2021-09-20 12:29:16 +02:00
|
|
|
import dbInit, { ITestDb } from '../../helpers/database-init';
|
|
|
|
import { IUnleashTest, setupApp } from '../../helpers/test-helper';
|
2021-08-12 15:04:37 +02:00
|
|
|
import getLogger from '../../../fixtures/no-logger';
|
2021-09-24 13:55:00 +02:00
|
|
|
import { DEFAULT_ENV } from '../../../../lib/util/constants';
|
2019-03-14 17:56:02 +01:00
|
|
|
|
|
|
|
const importData = require('../../../examples/import.json');
|
2019-10-03 15:01:33 +02:00
|
|
|
|
2021-09-20 12:29:16 +02:00
|
|
|
let app: IUnleashTest;
|
|
|
|
let db: ITestDb;
|
2019-10-03 15:01:33 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeAll(async () => {
|
2021-03-19 22:25:21 +01:00
|
|
|
db = await dbInit('state_api_serial', getLogger);
|
2021-05-28 11:10:24 +02:00
|
|
|
app = await setupApp(db.stores);
|
2019-10-03 15:01:33 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
afterAll(async () => {
|
|
|
|
await app.destroy();
|
2021-03-19 22:25:21 +01:00
|
|
|
await db.destroy();
|
2019-10-03 15:01:33 +02:00
|
|
|
});
|
2019-03-14 17:56:02 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('exports strategies and features as json by default', async () => {
|
|
|
|
expect.assertions(2);
|
|
|
|
|
|
|
|
return app.request
|
2019-03-14 17:56:02 +01:00
|
|
|
.get('/api/admin/state/export')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
2021-08-12 15:04:37 +02:00
|
|
|
.expect((res) => {
|
2021-05-28 11:10:24 +02:00
|
|
|
expect('features' in res.body).toBe(true);
|
|
|
|
expect('strategies' in res.body).toBe(true);
|
2019-10-03 15:01:33 +02:00
|
|
|
});
|
2019-03-14 17:56:02 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('exports strategies and features as yaml', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2019-03-14 17:56:02 +01:00
|
|
|
.get('/api/admin/state/export?format=yaml')
|
|
|
|
.expect('Content-Type', /yaml/)
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(200);
|
2019-03-14 17:56:02 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('exports only features as yaml', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2020-07-30 23:57:13 +02:00
|
|
|
.get('/api/admin/state/export?format=yaml&featureToggles=1')
|
|
|
|
.expect('Content-Type', /yaml/)
|
|
|
|
.expect(200);
|
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('exports strategies and features as attachment', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2019-03-14 17:56:02 +01:00
|
|
|
.get('/api/admin/state/export?download=1')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Content-Disposition', /attachment/)
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(200);
|
2019-03-14 17:56:02 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('imports strategies and features', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2019-03-14 17:56:02 +01:00
|
|
|
.post('/api/admin/state/import')
|
|
|
|
.send(importData)
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(202);
|
2019-03-14 17:56:02 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('does not not accept gibberish', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2019-03-14 17:56:02 +01:00
|
|
|
.post('/api/admin/state/import')
|
|
|
|
.send({ features: 'nonsense' })
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(400);
|
2019-03-14 17:56:02 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('imports strategies and features from json file', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2019-03-14 17:56:02 +01:00
|
|
|
.post('/api/admin/state/import')
|
2021-02-12 11:42:00 +01:00
|
|
|
.attach('file', 'src/test/examples/import.json')
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(202);
|
2019-03-14 17:56:02 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('imports strategies and features from yaml file', async () => {
|
|
|
|
expect.assertions(0);
|
|
|
|
|
|
|
|
return app.request
|
2019-03-14 17:56:02 +01:00
|
|
|
.post('/api/admin/state/import')
|
2021-02-12 11:42:00 +01:00
|
|
|
.attach('file', 'src/test/examples/import.yml')
|
2019-10-03 15:01:33 +02:00
|
|
|
.expect(202);
|
2019-03-14 17:56:02 +01:00
|
|
|
});
|
2021-09-20 12:29:16 +02:00
|
|
|
|
|
|
|
test('import works for 3.17 json format', async () => {
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/state/import')
|
|
|
|
.attach('file', 'src/test/examples/exported3176.json')
|
|
|
|
.expect(202);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('import works for 3.17 enterprise json format', async () => {
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/state/import')
|
|
|
|
.attach('file', 'src/test/examples/exported-3175-enterprise.json')
|
|
|
|
.expect(202);
|
|
|
|
});
|
|
|
|
test('import works for 4.0 enterprise format', async () => {
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/state/import')
|
|
|
|
.attach('file', 'src/test/examples/exported405-enterprise.json')
|
|
|
|
.expect(202);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('import for 4.1.2 enterprise format fails', async () => {
|
|
|
|
await expect(async () =>
|
|
|
|
app.request
|
|
|
|
.post('/api/admin/state/import')
|
|
|
|
.attach('file', 'src/test/examples/exported412-enterprise.json')
|
|
|
|
.expect(202),
|
|
|
|
).rejects;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('import for 4.1.2 enterprise format fixed works', async () => {
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/state/import')
|
|
|
|
.attach(
|
|
|
|
'file',
|
|
|
|
'src/test/examples/exported412-enterprise-necessary-fixes.json',
|
|
|
|
)
|
|
|
|
.expect(202);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can roundtrip. I.e. export and then import', async () => {
|
|
|
|
const projectId = 'export-project';
|
|
|
|
const environmentId = 'export-environment';
|
|
|
|
const userName = 'export-user';
|
|
|
|
const featureName = 'export.feature';
|
|
|
|
await db.stores.environmentStore.create({
|
|
|
|
name: environmentId,
|
|
|
|
type: 'test',
|
|
|
|
});
|
|
|
|
await db.stores.projectStore.create({
|
|
|
|
name: projectId,
|
|
|
|
id: projectId,
|
|
|
|
description: 'Project for export',
|
|
|
|
});
|
|
|
|
await app.services.environmentService.addEnvironmentToProject(
|
|
|
|
environmentId,
|
|
|
|
projectId,
|
|
|
|
);
|
|
|
|
await app.services.featureToggleServiceV2.createFeatureToggle(
|
|
|
|
projectId,
|
|
|
|
{
|
|
|
|
type: 'Release',
|
|
|
|
name: featureName,
|
|
|
|
description: 'Feature for export',
|
|
|
|
},
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
await app.services.featureToggleServiceV2.createStrategy(
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
constraints: [
|
|
|
|
{ contextName: 'userId', operator: 'IN', values: ['123'] },
|
|
|
|
],
|
|
|
|
parameters: {},
|
|
|
|
},
|
|
|
|
projectId,
|
|
|
|
featureName,
|
|
|
|
environmentId,
|
|
|
|
);
|
|
|
|
const data = await app.services.stateService.export({});
|
|
|
|
await app.services.stateService.import({
|
|
|
|
data,
|
|
|
|
dropBeforeImport: true,
|
|
|
|
keepExisting: false,
|
|
|
|
userName: 'export-tester',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Roundtrip with tags works', async () => {
|
|
|
|
const projectId = 'tags-project';
|
|
|
|
const environmentId = 'tags-environment';
|
|
|
|
const userName = 'tags-user';
|
|
|
|
const featureName = 'tags.feature';
|
|
|
|
await db.stores.environmentStore.create({
|
|
|
|
name: environmentId,
|
|
|
|
type: 'test',
|
|
|
|
});
|
|
|
|
await db.stores.projectStore.create({
|
|
|
|
name: projectId,
|
|
|
|
id: projectId,
|
|
|
|
description: 'Project for export',
|
|
|
|
});
|
|
|
|
await app.services.environmentService.addEnvironmentToProject(
|
|
|
|
environmentId,
|
|
|
|
projectId,
|
|
|
|
);
|
|
|
|
await app.services.featureToggleServiceV2.createFeatureToggle(
|
|
|
|
projectId,
|
|
|
|
{
|
|
|
|
type: 'Release',
|
|
|
|
name: featureName,
|
|
|
|
description: 'Feature for export',
|
|
|
|
},
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
await app.services.featureToggleServiceV2.createStrategy(
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
constraints: [
|
|
|
|
{ contextName: 'userId', operator: 'IN', values: ['123'] },
|
|
|
|
],
|
|
|
|
parameters: {},
|
|
|
|
},
|
|
|
|
projectId,
|
|
|
|
featureName,
|
|
|
|
environmentId,
|
|
|
|
);
|
|
|
|
await app.services.featureTagService.addTag(
|
|
|
|
featureName,
|
|
|
|
{ type: 'simple', value: 'export-test' },
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
await app.services.featureTagService.addTag(
|
|
|
|
featureName,
|
|
|
|
{ type: 'simple', value: 'export-test-2' },
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
const data = await app.services.stateService.export({});
|
|
|
|
await app.services.stateService.import({
|
|
|
|
data,
|
|
|
|
dropBeforeImport: true,
|
|
|
|
keepExisting: false,
|
|
|
|
userName: 'export-tester',
|
|
|
|
});
|
|
|
|
|
|
|
|
const f = await app.services.featureTagService.listTags(featureName);
|
|
|
|
expect(f).toHaveLength(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Roundtrip with strategies in multiple environments works', async () => {
|
|
|
|
const projectId = 'multiple-environment-project';
|
|
|
|
const environmentId = 'multiple-environment-environment';
|
|
|
|
const userName = 'multiple-environment-user';
|
|
|
|
const featureName = 'multiple-environment.feature';
|
|
|
|
await db.stores.environmentStore.create({
|
|
|
|
name: environmentId,
|
|
|
|
type: 'test',
|
|
|
|
});
|
|
|
|
await db.stores.projectStore.create({
|
|
|
|
name: projectId,
|
|
|
|
id: projectId,
|
|
|
|
description: 'Project for export',
|
|
|
|
});
|
|
|
|
await app.services.environmentService.addEnvironmentToProject(
|
|
|
|
environmentId,
|
|
|
|
projectId,
|
|
|
|
);
|
|
|
|
await app.services.environmentService.addEnvironmentToProject(
|
2021-09-24 13:55:00 +02:00
|
|
|
DEFAULT_ENV,
|
2021-09-20 12:29:16 +02:00
|
|
|
projectId,
|
|
|
|
);
|
|
|
|
await app.services.featureToggleServiceV2.createFeatureToggle(
|
|
|
|
projectId,
|
|
|
|
{
|
|
|
|
type: 'Release',
|
|
|
|
name: featureName,
|
|
|
|
description: 'Feature for export',
|
|
|
|
},
|
|
|
|
userName,
|
|
|
|
);
|
|
|
|
await app.services.featureToggleServiceV2.createStrategy(
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
constraints: [
|
|
|
|
{ contextName: 'userId', operator: 'IN', values: ['123'] },
|
|
|
|
],
|
|
|
|
parameters: {},
|
|
|
|
},
|
|
|
|
projectId,
|
|
|
|
featureName,
|
|
|
|
environmentId,
|
|
|
|
);
|
|
|
|
await app.services.featureToggleServiceV2.createStrategy(
|
|
|
|
{
|
|
|
|
name: 'default',
|
|
|
|
constraints: [
|
|
|
|
{ contextName: 'userId', operator: 'IN', values: ['123'] },
|
|
|
|
],
|
|
|
|
parameters: {},
|
|
|
|
},
|
|
|
|
projectId,
|
|
|
|
featureName,
|
2021-09-24 13:55:00 +02:00
|
|
|
DEFAULT_ENV,
|
2021-09-20 12:29:16 +02:00
|
|
|
);
|
|
|
|
const data = await app.services.stateService.export({});
|
|
|
|
await app.services.stateService.import({
|
|
|
|
data,
|
|
|
|
dropBeforeImport: true,
|
|
|
|
keepExisting: false,
|
|
|
|
userName: 'export-tester',
|
|
|
|
});
|
|
|
|
const f = await app.services.featureToggleServiceV2.getFeature(featureName);
|
|
|
|
expect(f.environments).toHaveLength(2);
|
|
|
|
});
|
2021-09-24 13:55:00 +02:00
|
|
|
|
|
|
|
test(`Importing version 2 replaces :global: environment with 'default'`, async () => {
|
|
|
|
await app.request
|
|
|
|
.post('/api/admin/state/import')
|
|
|
|
.attach('file', 'src/test/examples/exported412-version2.json')
|
|
|
|
.expect(202);
|
|
|
|
const env = await app.services.environmentService.get(DEFAULT_ENV);
|
|
|
|
expect(env).toBeTruthy();
|
|
|
|
const feature = await app.services.featureToggleServiceV2.getFeatureToggle(
|
|
|
|
'this-is-fun',
|
|
|
|
);
|
|
|
|
expect(feature.environments).toHaveLength(1);
|
|
|
|
expect(feature.environments[0].name).toBe(DEFAULT_ENV);
|
|
|
|
});
|