From f31a79c5bc3678949b7334486daa196a17a5db92 Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Tue, 19 Dec 2017 19:42:52 +0100 Subject: Create Organisation#features and FeatureChecker controller concerns. Refs #5339 --- spec/controllers/concerns/feature_checker_spec.rb | 36 +++++++++++++++++++++++ spec/models/organisation_spec.rb | 22 ++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 spec/controllers/concerns/feature_checker_spec.rb (limited to 'spec') diff --git a/spec/controllers/concerns/feature_checker_spec.rb b/spec/controllers/concerns/feature_checker_spec.rb new file mode 100644 index 000000000..0ffee0dfd --- /dev/null +++ b/spec/controllers/concerns/feature_checker_spec.rb @@ -0,0 +1,36 @@ +require "rails_helper" + +RSpec.describe "FeatureChecker", type: :controller do + + controller do + include FeatureChecker + requires_feature :test, only: :protected + + def protected; render :text => "protected"; end + def not_protected; render :text => "not protected"; end + + def current_organisation + @organisation ||= Organisation.new + end + end + + before do + routes.draw do + get "protected" => "anonymous#protected" + get "not_protected" => "anonymous#not_protected" + end + end + + it "refuse access when organisation has not the feature" do + expect{ get(:protected) }.to raise_error(FeatureChecker::NotAuthorizedError) + end + + it "accept access on unprotected action" do + get :not_protected + end + + it 'accept access when organisation has feature' do + controller.current_organisation.features << "test" + get :protected + end +end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 359417d88..0d54ed143 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -62,4 +62,26 @@ describe Organisation, :type => :model do expect{Organisation.portail_sync}.to change{ Organisation.count }.by(4) end end + + describe "#has_feature?" do + + let(:organisation) { Organisation.new } + + it 'return false if Organisation features is nil' do + organisation.features = nil + expect(organisation.has_feature?(:dummy)).to be_falsy + end + + it 'return true if Organisation features contains given feature' do + organisation.features = %w{present} + expect(organisation.has_feature?(:present)).to be_truthy + end + + it "return true if Organisation features doesn't contains given feature" do + organisation.features = %w{other} + expect(organisation.has_feature?(:absent)).to be_falsy + end + + end + end -- cgit v1.2.3 From 5404543c7438a0f574af49950af52ac27454126d Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Wed, 20 Dec 2017 17:21:38 +0100 Subject: Fixes Organisation#has_feature? spec title. Refs #5339 --- spec/models/organisation_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 0d54ed143..595b08058 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -77,7 +77,7 @@ describe Organisation, :type => :model do expect(organisation.has_feature?(:present)).to be_truthy end - it "return true if Organisation features doesn't contains given feature" do + it "return false if Organisation features doesn't contains given feature" do organisation.features = %w{other} expect(organisation.has_feature?(:absent)).to be_falsy end -- cgit v1.2.3 From 3305dca57e2e0a46b583f183040a82819ffd6efb Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Wed, 20 Dec 2017 21:06:41 +0100 Subject: Improve styles in FeatureCheck code. Refs #5339 --- spec/controllers/concerns/feature_checker_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec') diff --git a/spec/controllers/concerns/feature_checker_spec.rb b/spec/controllers/concerns/feature_checker_spec.rb index 0ffee0dfd..50b21954f 100644 --- a/spec/controllers/concerns/feature_checker_spec.rb +++ b/spec/controllers/concerns/feature_checker_spec.rb @@ -6,8 +6,8 @@ RSpec.describe "FeatureChecker", type: :controller do include FeatureChecker requires_feature :test, only: :protected - def protected; render :text => "protected"; end - def not_protected; render :text => "not protected"; end + def protected; render text: "protected"; end + def not_protected; render text: "not protected"; end def current_organisation @organisation ||= Organisation.new @@ -21,7 +21,7 @@ RSpec.describe "FeatureChecker", type: :controller do end end - it "refuse access when organisation has not the feature" do + it "refuse access when organisation does not have the feature" do expect{ get(:protected) }.to raise_error(FeatureChecker::NotAuthorizedError) end -- cgit v1.2.3 From 462c1c257e954c77f1dedfc770d3c78111c1b499 Mon Sep 17 00:00:00 2001 From: Alban Peignier Date: Wed, 20 Dec 2017 21:25:59 +0100 Subject: Use login_user for anonymous controller used into FeatureChecker specs. Refs #5339 --- spec/controllers/concerns/feature_checker_spec.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec') diff --git a/spec/controllers/concerns/feature_checker_spec.rb b/spec/controllers/concerns/feature_checker_spec.rb index 50b21954f..1d289bb15 100644 --- a/spec/controllers/concerns/feature_checker_spec.rb +++ b/spec/controllers/concerns/feature_checker_spec.rb @@ -1,6 +1,7 @@ require "rails_helper" RSpec.describe "FeatureChecker", type: :controller do + login_user controller do include FeatureChecker -- cgit v1.2.3