diff options
author | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-07-03 18:05:49 +1000 |
---|---|---|
committer | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-07-10 16:59:46 +1000 |
commit | f0d9c729aa70a193cbe5438591b5e0691328733a (patch) | |
tree | 9f2ec4d898ca067c7646fd8345b20096b60e4945 | |
parent | 275fbf24b1810e2fbef92b6599d5372855b97b46 (diff) | |
download | gitlab-ce-jej/cookie-restriction-toggled-by-feature-flag.tar.gz |
CookieRestriction helper controlled by feature flagsjej/cookie-restriction-toggled-by-feature-flag
-rw-r--r-- | app/models/cookie_restriction.rb | 27 | ||||
-rw-r--r-- | spec/models/cookie_restriction_spec.rb | 64 |
2 files changed, 91 insertions, 0 deletions
diff --git a/app/models/cookie_restriction.rb b/app/models/cookie_restriction.rb new file mode 100644 index 00000000000..857db213e81 --- /dev/null +++ b/app/models/cookie_restriction.rb @@ -0,0 +1,27 @@ +class CookieRestriction + def initialize(name) + @name = name + end + + def cookie_name + "enable_#{@name}" + end + + def cookie_feature + :"skip_#{@name}_cookie_restriction" + end + + def cookie_required? + !Feature.enabled?(cookie_feature) + end + + def active?(cookies) + !cookie_required? || cookie_enabled?(cookies) + end + + private + + def cookie_enabled?(cookies) + ::Gitlab::Utils.to_boolean(cookies[cookie_name]) + end +end diff --git a/spec/models/cookie_restriction_spec.rb b/spec/models/cookie_restriction_spec.rb new file mode 100644 index 00000000000..30d11967283 --- /dev/null +++ b/spec/models/cookie_restriction_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' + +describe CookieRestriction do + subject { described_class.new(:my_feature) } + + before do + allow(Feature).to receive(:enabled?).and_call_original + end + + describe "#cookie_name" do + it "returns the name of the enable cookie" do + expect(subject.cookie_name).to eq "enable_my_feature" + end + end + + describe "#cookie_feature" do + it "returns a feature name used to lift the cookie restriction" do + expect(subject.cookie_feature).to eq :skip_my_feature_cookie_restriction + end + end + + describe "#cookie_required?" do + it "defaults to requiring a cookie" do + expect(subject.cookie_required?).to eq true + end + + context "with the restriction disabled" do + before do + stub_feature_flags(skip_my_feature_cookie_restriction: true) + end + + it "doesn't require a cookie set to use the feature" do + expect(subject.cookie_required?).to eq false + end + end + end + + describe "#active?" do + it "is false by default when the cookie is not present" do + cookies = {} + + expect(subject.active?(cookies)).to be_falsey + end + + it "looks up cookies to enable the feature" do + cookies = { 'enable_my_feature' => "true" } + + expect(subject.active?(cookies)).to eq true + end + + it "can be disabled by cookie" do + cookies = { 'enable_my_feature' => "false" } + + expect(subject.active?(cookies)).to eq false + end + + it "is true when the cookie restriction has been lifted" do + stub_feature_flags(skip_my_feature_cookie_restriction: true) + cookies = [] + + expect(subject.active?(cookies)).to eq true + end + end +end |