Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix evaluation: FeatureFlag.enabled defaults to false #11

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/featureManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export class FeatureManager {
return false;
}

if (featureFlag.enabled === false) {
// If the feature is explicitly disabled, then it is disabled.
// Ensure that the feature flag is in the correct format. Feature providers should validate the feature flags, but we do it here as a safeguard.
validateFeatureFlagFormat(featureFlag);

if (featureFlag.enabled !== true) {
// If the feature is not explicitly enabled, then it is disabled by default.
return false;
}

Expand Down Expand Up @@ -77,3 +80,9 @@ export class FeatureManager {
interface FeatureManagerOptions {
customFilters?: IFeatureFilter[];
}

function validateFeatureFlagFormat(featureFlag: any): void {
if (featureFlag.enabled !== undefined && typeof featureFlag.enabled !== "boolean") {
Eskibear marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Feature flag ${featureFlag.id} has an invalid 'enabled' value.`);
}
}
70 changes: 70 additions & 0 deletions test/noFilters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";
chai.use(chaiAsPromised);
const expect = chai.expect;

import { FeatureManager, ConfigurationObjectFeatureFlagProvider } from "./exportedApi";

const featureFlagsDataObject = {
"feature_management": {
"feature_flags": [
{
"id": "BooleanTrue",
"description": "A feature flag with no Filters, that returns true.",
"enabled": true,
"conditions": {
"client_filters": []
}
},
{
"id": "BooleanFalse",
"description": "A feature flag with no Filters, that returns false.",
"enabled": false,
"conditions": {
"client_filters": []
}
},
{
"id": "InvalidEnabled",
"description": "A feature flag with an invalid 'enabled' value, that throws an exception.",
"enabled": "invalid",
Eskibear marked this conversation as resolved.
Show resolved Hide resolved
"conditions": {
"client_filters": []
}
},
{
"id": "Minimal",
"enabled": true
},
{
"id": "NoEnabled"
},
{
"id": "EmptyConditions",
"description": "A feature flag with no values in conditions, that returns true.",
"enabled": true,
"conditions": {
}
}
]
}
};

describe("feature flags with no filters", () => {
it("should validate feature flags without filters", () => {
const provider = new ConfigurationObjectFeatureFlagProvider(featureFlagsDataObject);
const featureManager = new FeatureManager(provider);

return Promise.all([
expect(featureManager.isEnabled("BooleanTrue")).eventually.eq(true),
expect(featureManager.isEnabled("BooleanFalse")).eventually.eq(false),
expect(featureManager.isEnabled("InvalidEnabled")).eventually.rejectedWith("Feature flag InvalidEnabled has an invalid 'enabled' value."),
expect(featureManager.isEnabled("Minimal")).eventually.eq(true),
expect(featureManager.isEnabled("NoEnabled")).eventually.eq(false),
expect(featureManager.isEnabled("EmptyConditions")).eventually.eq(true)
]);
});
});