Skip to content

Commit

Permalink
refactor: test composition and other error codes (#3348)
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonfournier committed Mar 23, 2023
1 parent 24f9d51 commit a79a76f
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 213 deletions.
65 changes: 21 additions & 44 deletions src/lib/features/export-import-toggles/export-import.e2e.test.ts
Expand Up @@ -112,12 +112,7 @@ const createProjects = async (projects: string[] = [DEFAULT_PROJECT]) => {
id: project,
mode: 'open' as const,
});
await app.request
.post(`/api/admin/projects/${project}/environments`)
.send({
environment: DEFAULT_ENV,
})
.expect(200);
await app.linkProjectToEnvironment(project, DEFAULT_ENV);
}
};

Expand All @@ -127,18 +122,6 @@ const createSegment = (postData: UpsertSegmentSchema): Promise<ISegment> => {
});
};

const createContextField = async (contextField: IContextFieldDto) => {
await app.createContextField(contextField);
};

const createFeature = async (featureName: string) => {
await app.createFeature(featureName);
};

const archiveFeature = async (featureName: string) => {
await app.archiveFeature(featureName);
};

const unArchiveFeature = async (featureName: string) => {
await app.request
.post(`/api/admin/archive/revive/${featureName}`)
Expand Down Expand Up @@ -176,7 +159,7 @@ beforeEach(async () => {
await environmentStore.deleteAll();

await contextFieldStore.deleteAll();
await createContextField({ name: 'appName' });
await app.createContextField({ name: 'appName' });
});

afterAll(async () => {
Expand Down Expand Up @@ -475,18 +458,6 @@ test('returns no features, when no feature was requested', async () => {
expect(body.features).toHaveLength(0);
});

const importToggles = (
importPayload: ImportTogglesSchema,
status = 200,
expect: (response) => void = () => {},
) =>
app.request
.post('/api/admin/features-batch/import')
.send(importPayload)
.set('Content-Type', 'application/json')
.expect(status)
.expect(expect);

const defaultFeature = 'first_feature';

const variants: VariantsSchema = [
Expand Down Expand Up @@ -610,7 +581,7 @@ const validateImport = (importPayload: ImportTogglesSchema, status = 200) =>
test('import features to existing project and environment', async () => {
await createProjects();

await importToggles(defaultImportPayload);
await app.importToggles(defaultImportPayload);

const { body: importedFeature } = await getFeature(defaultFeature);
expect(importedFeature).toMatchObject({
Expand Down Expand Up @@ -645,8 +616,8 @@ test('import features to existing project and environment', async () => {

test('importing same JSON should work multiple times in a row', async () => {
await createProjects();
await importToggles(defaultImportPayload);
await importToggles(defaultImportPayload);
await app.importToggles(defaultImportPayload);
await app.importToggles(defaultImportPayload);

const { body: importedFeature } = await getFeature(defaultFeature);
expect(importedFeature).toMatchObject({
Expand Down Expand Up @@ -681,7 +652,7 @@ test('reject import with unknown context fields', async () => {
name: 'ContextField1',
legalValues: [{ value: 'Value1', description: '' }],
};
await createContextField(contextField);
await app.createContextField(contextField);
const importPayloadWithContextFields: ImportTogglesSchema = {
...defaultImportPayload,
data: {
Expand All @@ -695,7 +666,10 @@ test('reject import with unknown context fields', async () => {
},
};

const { body } = await importToggles(importPayloadWithContextFields, 400);
const { body } = await app.importToggles(
importPayloadWithContextFields,
400,
);

expect(body).toMatchObject({
details: [
Expand All @@ -718,7 +692,10 @@ test('reject import with unsupported strategies', async () => {
},
};

const { body } = await importToggles(importPayloadWithContextFields, 400);
const { body } = await app.importToggles(
importPayloadWithContextFields,
400,
);

expect(body).toMatchObject({
details: [
Expand All @@ -741,10 +718,10 @@ test('validate import data', async () => {
legalValues: [{ value: 'new_value' }],
};

await createFeature(defaultFeature);
await archiveFeature(defaultFeature);
await app.createFeature(defaultFeature);
await app.archiveFeature(defaultFeature);

await createContextField(contextField);
await app.createContextField(contextField);
const importPayloadWithContextFields: ImportTogglesSchema = {
...defaultImportPayload,
data: {
Expand Down Expand Up @@ -801,19 +778,19 @@ test('should create new context', async () => {
},
};

await importToggles(importPayloadWithContextFields, 200);
await app.importToggles(importPayloadWithContextFields);

const { body } = await getContextField(context.name);
expect(body).toMatchObject(context);
});

test('should not import archived features tags', async () => {
await createProjects();
await importToggles(defaultImportPayload);
await app.importToggles(defaultImportPayload);

await archiveFeature(defaultFeature);
await app.archiveFeature(defaultFeature);

await importToggles({
await app.importToggles({
...defaultImportPayload,
data: {
...defaultImportPayload.data,
Expand Down
138 changes: 47 additions & 91 deletions src/test/e2e/api/admin/feature-archive.e2e.test.ts
@@ -1,19 +1,13 @@
import { setupAppWithCustomConfig } from '../../helpers/test-helper';
import dbInit from '../../helpers/database-init';
import {
IUnleashTest,
setupAppWithCustomConfig,
} from '../../helpers/test-helper';
import dbInit, { ITestDb } from '../../helpers/database-init';
import getLogger from '../../../fixtures/no-logger';
import { DEFAULT_PROJECT } from '../../../../lib/types';

let app;
let db;

const createFeatureToggle = (
featureName: string,
project = DEFAULT_PROJECT,
) => {
return app.request.post(`/api/admin/projects/${project}/features`).send({
name: featureName,
});
};
let app: IUnleashTest;
let db: ITestDb;

beforeAll(async () => {
db = await dbInit('archive_serial', getLogger);
Expand All @@ -25,78 +19,42 @@ beforeAll(async () => {
},
},
});
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{
name: 'featureX',
description: 'the #1 feature',
},
'test',
);
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{
name: 'featureY',
description: 'soon to be the #1 feature',
},
'test',
);
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{
name: 'featureZ',
description: 'terrible feature',
},
'test',
);
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{
name: 'featureArchivedX',
description: 'the #1 feature',
},
'test',
);
await app.services.featureToggleServiceV2.archiveToggle(
'featureArchivedX',
'test',
);
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{
name: 'featureArchivedY',
description: 'soon to be the #1 feature',
},
'test',
);
await app.services.featureToggleServiceV2.archiveToggle(
'featureArchivedY',
'test',
);
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{
name: 'featureArchivedZ',
description: 'terrible feature',
},
'test',
);
await app.services.featureToggleServiceV2.archiveToggle(
'featureArchivedZ',
'test',
);
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{
name: 'feature.with.variants',
description: 'A feature toggle with variants',
variants: [
{ name: 'control', weight: 50 },
{ name: 'new', weight: 50 },
],
},
'test',
);
await app.createFeature({
name: 'featureX',
description: 'the #1 feature',
});
await app.createFeature({
name: 'featureY',
description: 'soon to be the #1 feature',
});
await app.createFeature({
name: 'featureZ',
description: 'terrible feature',
});
await app.createFeature({
name: 'featureArchivedX',
description: 'the #1 feature',
});
await app.archiveFeature('featureArchivedX');

await app.createFeature({
name: 'featureArchivedY',
description: 'soon to be the #1 feature',
});
await app.archiveFeature('featureArchivedY');
await app.createFeature({
name: 'featureArchivedZ',
description: 'terrible feature',
});
await app.archiveFeature('featureArchivedZ');
await app.createFeature({
name: 'feature.with.variants',
description: 'A feature toggle with variants',
variants: [
{ name: 'control', weight: 50 },
{ name: 'new', weight: 50 },
],
});
});

afterAll(async () => {
Expand Down Expand Up @@ -138,10 +96,8 @@ test('revives a feature by name', async () => {
test('archived feature is not accessible via /features/:featureName', async () => {
expect.assertions(0);

return app.request
.get('/api/admin/features/featureArchivedZ')
.set('Content-Type', 'application/json')
.expect(404);
await app.getFeatures('featureArchivedZ', 404);
await app.getProjectFeatures('default', 'featureArchivedZ', 404);
});

test('must set name when reviving toggle', async () => {
Expand Down Expand Up @@ -267,8 +223,8 @@ test('Should be able to bulk archive features', async () => {
const featureName1 = 'archivedFeature1';
const featureName2 = 'archivedFeature2';

await createFeatureToggle(featureName1);
await createFeatureToggle(featureName2);
await app.createFeature(featureName1);
await app.createFeature(featureName2);

await app.request
.post(`/api/admin/projects/${DEFAULT_PROJECT}/archive`)
Expand Down

0 comments on commit a79a76f

Please sign in to comment.