aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorLuc Donnet2017-07-10 11:35:06 +0200
committerLuc Donnet2017-07-10 11:35:06 +0200
commit272359b13aba516c8fa752d35adaac4981e7c5a6 (patch)
tree46dc03ef21a32196ffa56cd51c7b6fc95b7b6900 /spec
parentdecc41c797c58842faaeb69220a6ac8d60006536 (diff)
parent7c7451dc80b6f9b6092b3262fe524a8252309b1a (diff)
downloadchouette-core-272359b13aba516c8fa752d35adaac4981e7c5a6.tar.bz2
Merge branch 'master' of github.com:AF83/stif-boiv into staging
Diffstat (limited to 'spec')
-rw-r--r--spec/decorators/referential_decorator_spec.rb76
-rw-r--r--spec/policies/referential_policy_spec.rb97
-rw-r--r--spec/support/decorator_helpers.rb27
3 files changed, 181 insertions, 19 deletions
diff --git a/spec/decorators/referential_decorator_spec.rb b/spec/decorators/referential_decorator_spec.rb
new file mode 100644
index 000000000..5de6b7e95
--- /dev/null
+++ b/spec/decorators/referential_decorator_spec.rb
@@ -0,0 +1,76 @@
+RSpec.describe ReferentialDecorator, type: [:helper, :decorator] do
+
+ let( :object ){ build_stubbed :referential }
+ let( :referential ){ object }
+ let( :user ){ build_stubbed :user }
+
+ describe 'delegation' do
+ it 'delegates all' do
+ %i{xx xxx anything save!}.each do |method|
+ expect( object ).to receive(method)
+ end
+ # Almost as powerful as Quicktest :P
+ %i{xx xxx anything save!}.each do |method|
+ subject.send method
+ end
+ end
+ end
+
+ describe 'action links for' do
+
+ context 'unarchived referential' do
+ context 'no rights' do
+ it 'has only a Calendar action' do
+ expect_action_link_hrefs.to eq([referential_time_tables_path(object)])
+ end
+ end
+
+ context 'all rights and different organisation' do
+
+ let( :user ){ build_stubbed :allmighty_user }
+
+ it 'has only default actions' do
+ expect_action_link_elements.to be_empty
+ expect_action_link_hrefs.to eq([
+ referential_time_tables_path(object),
+ ])
+ end
+ end
+ context 'all rights and same organisation' do
+
+ let( :user ){ build_stubbed :allmighty_user, organisation: referential.organisation }
+
+ it 'has all actions' do
+ expect_action_link_elements.to eq(%w{Purger})
+ expect_action_link_hrefs.to eq([
+ referential_time_tables_path(object),
+ new_referential_path(from: object),
+ archive_referential_path(object),
+ referential_path(object)
+ ])
+ end
+ end
+ end
+
+ context 'archived referential' do
+ before { referential.archived_at = 42.seconds.ago }
+ context 'no rights' do
+ it 'has only a Calendar action' do
+ expect_action_link_hrefs.to eq([referential_time_tables_path(object)])
+ end
+ end
+
+ context 'all rights and different organisation' do
+ let( :user ){ build_stubbed :allmighty_user }
+ it 'has only default actions' do
+ expect_action_link_elements.to be_empty
+ expect_action_link_hrefs.to eq([
+ referential_time_tables_path(object),
+ ])
+ end
+ end
+ end
+ end
+
+
+end
diff --git a/spec/policies/referential_policy_spec.rb b/spec/policies/referential_policy_spec.rb
index d060317f9..33d8e13e8 100644
--- a/spec/policies/referential_policy_spec.rb
+++ b/spec/policies/referential_policy_spec.rb
@@ -56,22 +56,52 @@ RSpec.describe ReferentialPolicy, type: :policy do
add_permissions('referentials.update', for_user: user)
end
- it 'allowed for unarchived referentials' do
- expect_it.to permit(user_context, record)
+ context 'same organisation →' do
+ before do
+ user.organisation_id = referential.organisation_id
+ end
+ it "allows a user with the same organisation" do
+ expect_it.to permit(user_context, record)
+ end
+ describe "archived" do
+ let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago }
+ it 'does remove permission for archived referentials' do
+ expect_it.not_to permit(user_context, record)
+ end
+ end
end
- it 'forbidden for archived referentials' do
- record.archived_at = 1.second.ago
- expect_it.not_to permit(user_context, record)
+ context 'different organisations →' do
+ it "forbids a user with a different organisation" do
+ expect_it.not_to permit(user_context, record)
+ end
+
+ describe "archived" do
+ let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago }
+ it 'forbids for archived referentials' do
+ expect_it.not_to permit(user_context, record)
+ end
+ end
+
end
end
- context 'permission absent →' do
- it 'is forbidden' do
- expect_it.not_to permit(user_context, record)
+ context 'permission absent →' do
+ context 'same organisation →' do
+ before do
+ user.organisation_id = referential.organisation_id
+ end
+ it "forbids a user with the same organisation" do
+ expect_it.not_to permit(user_context, record)
+ end
+ describe "archived" do
+ let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago }
+ it 'forbids for archived referentials' do
+ expect_it.not_to permit(user_context, record)
+ end
+ end
end
end
-
end
permissions :unarchive? do
@@ -81,22 +111,51 @@ RSpec.describe ReferentialPolicy, type: :policy do
add_permissions('referentials.update', for_user: user)
end
- it 'forbidden for unarchived referentials' do
- expect_it.not_to permit(user_context, record)
+ context 'same organisation →' do
+ before do
+ user.organisation_id = referential.organisation_id
+ end
+ it "forbids a user with the same organisation" do
+ expect_it.not_to permit(user_context, record)
+ end
+ describe "archived" do
+ let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago }
+ it 'adds permission for archived referentials' do
+ expect_it.to permit(user_context, record)
+ end
+ end
end
- it 'allowed for archived referentials' do
- record.archived_at = 1.second.ago
- expect_it.to permit(user_context, record)
+ context 'different organisations →' do
+ it "forbids a user with a different organisation" do
+ expect_it.not_to permit(user_context, record)
+ end
+
+ describe "archived" do
+ let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago }
+ it 'still forbids for archived referentials' do
+ expect_it.not_to permit(user_context, record)
+ end
+ end
+
end
end
- context 'permission absent →' do
- it 'is forbidden' do
- record.archived_at = 1.second.ago
- expect_it.not_to permit(user_context, record)
+ context 'permission absent →' do
+ context 'same organisation →' do
+ before do
+ user.organisation_id = referential.organisation_id
+ end
+ it "forbids a user with a different rganisation" do
+ expect_it.not_to permit(user_context, record)
+ end
+ describe "archived" do
+ let( :record ){ build_stubbed :referential, archived_at: 2.minutes.ago }
+ it 'still forbids for archived referentials' do
+ expect_it.not_to permit(user_context, record)
+ end
+ end
end
end
-
end
end
diff --git a/spec/support/decorator_helpers.rb b/spec/support/decorator_helpers.rb
new file mode 100644
index 000000000..ffedd479b
--- /dev/null
+++ b/spec/support/decorator_helpers.rb
@@ -0,0 +1,27 @@
+module Support
+
+ module DecoratorHelpers
+ def self.included(into)
+ into.instance_eval do
+ subject{ object.decorate }
+ let( :policy ){ ::Pundit.policy(user_context, object) }
+ let( :user_context ){ UserContext.new(user, referential: referential) }
+
+ before do
+ allow_any_instance_of(Draper::HelperProxy).to receive(:policy).and_return policy
+ end
+ end
+ end
+
+ def expect_action_link_hrefs
+ expect( subject.action_links.select(&Link.method(:===)).map(&:href) )
+ end
+ def expect_action_link_elements
+ expect( subject.action_links.select(&HTMLElement.method(:===)).map(&:content) )
+ end
+ end
+end
+
+RSpec.configure do | c |
+ c.include Support::DecoratorHelpers, type: :decorator
+end