-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2020 from alphagov/add-feature-flagging
Add Flipflop gem for feature-toggling
- Loading branch information
Showing
9 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class FeatureConstraint | ||
def initialize(feature_name) | ||
@feature_name = feature_name | ||
end | ||
|
||
def matches?(request) | ||
if request.cookies.key?(@feature_name) | ||
request.cookies[@feature_name] == "1" | ||
else | ||
Flipflop.enabled?(@feature_name.to_sym) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Flipflop.configure do | ||
# Strategies will be used in the order listed here. | ||
strategy :cookie | ||
strategy :default | ||
|
||
if Rails.env.test? | ||
feature :feature_for_tests, | ||
default: true, | ||
description: "A feature only used by tests; not to be used for any actual features." | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require "test_helper" | ||
|
||
class FeatureConstraintTest < ActiveSupport::TestCase | ||
context "Feature 'feature_for_tests' is enabled by default" do | ||
setup do | ||
test_strategy = Flipflop::FeatureSet.current.test! | ||
test_strategy.switch!(:feature_for_tests, true) | ||
end | ||
|
||
should "match when a request cookie explicitly enables feature" do | ||
request = stub(cookies: { "feature_for_tests" => "1" }) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal true, feature_constraint.matches?(request) | ||
end | ||
|
||
should "not match when a request cookie explicitly disables feature" do | ||
request = stub(cookies: { "feature_for_tests" => "0" }) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal false, feature_constraint.matches?(request) | ||
end | ||
|
||
should "match when a request cookie does not override default feature status" do | ||
request = stub(cookies: {}) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal true, feature_constraint.matches?(request) | ||
end | ||
end | ||
|
||
context "Feature 'feature_for_tests' is disabled by default" do | ||
setup do | ||
test_strategy = Flipflop::FeatureSet.current.test! | ||
test_strategy.switch!(:feature_for_tests, false) | ||
end | ||
|
||
should "match when a request cookie explicitly enables feature" do | ||
request = stub(cookies: { "feature_for_tests" => "1" }) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal true, feature_constraint.matches?(request) | ||
end | ||
|
||
should "not match when a request cookie explicitly disables feature" do | ||
request = stub(cookies: { "feature_for_tests" => "0" }) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal false, feature_constraint.matches?(request) | ||
end | ||
|
||
should "not match when a request cookie does not override default feature status" do | ||
request = stub(cookies: {}) | ||
|
||
feature_constraint = FeatureConstraint.new("feature_for_tests") | ||
|
||
assert_equal false, feature_constraint.matches?(request) | ||
end | ||
end | ||
end |