aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/concerns/feature_checker_spec.rb37
-rw-r--r--spec/models/organisation_spec.rb22
2 files changed, 59 insertions, 0 deletions
diff --git a/spec/controllers/concerns/feature_checker_spec.rb b/spec/controllers/concerns/feature_checker_spec.rb
new file mode 100644
index 000000000..1d289bb15
--- /dev/null
+++ b/spec/controllers/concerns/feature_checker_spec.rb
@@ -0,0 +1,37 @@
+require "rails_helper"
+
+RSpec.describe "FeatureChecker", type: :controller do
+ login_user
+
+ 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 does not have 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..595b08058 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 false if Organisation features doesn't contains given feature" do
+ organisation.features = %w{other}
+ expect(organisation.has_feature?(:absent)).to be_falsy
+ end
+
+ end
+
end