1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00

fix: client api should return feature toggles for disabled environments (#995)

* fix: client api should return feature toggles for disabled environments

* fix: add test

* lint
This commit is contained in:
Ivar Conradi Østhus 2021-10-01 12:27:05 +02:00 committed by GitHub
parent 54a99460ce
commit ee660c8eef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 28 deletions

View File

@ -45,16 +45,20 @@ export default class FeatureToggleClientStore
r: any, r: any,
includeId: boolean = true, includeId: boolean = true,
): IStrategyConfig { ): IStrategyConfig {
const strategy = { if (includeId) {
return {
name: r.strategy_name, name: r.strategy_name,
constraints: r.constraints || [], constraints: r.constraints || [],
parameters: r.parameters, parameters: r.parameters,
id: r.strategy_id, id: r.strategy_id,
}; };
if (!includeId) { } else {
delete strategy.id; return {
name: r.strategy_name,
constraints: r.constraints || [],
parameters: r.parameters,
};
} }
return strategy;
} }
private async getAll( private async getAll(
@ -74,28 +78,29 @@ export default class FeatureToggleClientStore
'features.variants as variants', 'features.variants as variants',
'features.created_at as created_at', 'features.created_at as created_at',
'features.last_seen_at as last_seen_at', 'features.last_seen_at as last_seen_at',
'feature_environments.enabled as enabled', 'fe.enabled as enabled',
'feature_environments.environment as environment', 'fe.environment as environment',
'feature_strategies.id as strategy_id', 'fs.id as strategy_id',
'feature_strategies.strategy_name as strategy_name', 'fs.strategy_name as strategy_name',
'feature_strategies.parameters as parameters', 'fs.parameters as parameters',
'feature_strategies.constraints as constraints', 'fs.constraints as constraints',
) )
.fullOuterJoin( .fullOuterJoin(
'feature_environments', this.db('feature_strategies')
'feature_environments.feature_name', .select('*')
.where({ environment })
.as('fs'),
'fs.feature_name',
'features.name', 'features.name',
) )
.fullOuterJoin('feature_strategies', function () { .fullOuterJoin(
this.on( this.db('feature_environments')
'feature_strategies.feature_name', .select('feature_name', 'enabled', 'environment')
.where({ environment })
.as('fe'),
'fe.feature_name',
'features.name', 'features.name',
).andOn( )
'feature_strategies.environment',
'feature_environments.environment',
);
})
.where('feature_environments.environment', environment)
.where({ archived }); .where({ archived });
if (featureQuery) { if (featureQuery) {
@ -117,6 +122,7 @@ export default class FeatureToggleClientStore
); );
} }
} }
const rows = await query; const rows = await query;
stopTimer(); stopTimer();
const featureToggles = rows.reduce((acc, r) => { const featureToggles = rows.reduce((acc, r) => {
@ -132,7 +138,7 @@ export default class FeatureToggleClientStore
if (r.strategy_name) { if (r.strategy_name) {
feature.strategies.push(this.getAdminStrategy(r, isAdmin)); feature.strategies.push(this.getAdminStrategy(r, isAdmin));
} }
feature.enabled = r.enabled; feature.enabled = !!r.enabled;
feature.name = r.name; feature.name = r.name;
feature.description = r.description; feature.description = r.description;
feature.project = r.project; feature.project = r.project;

View File

@ -0,0 +1,70 @@
import { IUnleashTest, setupApp } from '../../helpers/test-helper';
import dbInit, { ITestDb } from '../../helpers/database-init';
import getLogger from '../../../fixtures/no-logger';
let app: IUnleashTest;
let db: ITestDb;
const featureName = 'feature.default.1';
beforeAll(async () => {
db = await dbInit('feature_api_client', getLogger);
app = await setupApp(db.stores);
await app.services.featureToggleServiceV2.createFeatureToggle(
'default',
{
name: featureName,
description: 'the #1 feature',
},
'test',
);
await app.services.featureToggleServiceV2.createStrategy(
{ name: 'default', constraints: [], parameters: {} },
'default',
featureName,
'test',
);
});
afterAll(async () => {
await app.destroy();
await db.destroy();
});
test('returns feature toggle for default env', async () => {
await app.services.featureToggleServiceV2.updateEnabled(
'default',
'feature.default.1',
'default',
true,
'test',
);
await app.request
.get('/api/client/features')
.expect('Content-Type', /json/)
.expect(200)
.expect((res) => {
expect(res.body.features).toHaveLength(1);
expect(res.body.features[0].enabled).toBe(true);
expect(res.body.features[0].strategies).toHaveLength(1);
});
});
test('returns feature toggle for default env even if it is removed from project', async () => {
await app.services.environmentService.removeEnvironmentFromProject(
'default',
'default',
);
await app.request
.get('/api/client/features')
.expect('Content-Type', /json/)
.expect(200)
.expect((res) => {
expect(res.body.features).toHaveLength(1);
expect(res.body.features[0].enabled).toBe(false);
expect(res.body.features[0].strategies).toHaveLength(1);
});
});