From ffe4ad7bc6f9abfd664975ab288b42296b04df56 Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Fri, 30 Apr 2021 17:28:57 -0300 Subject: [PATCH] Add support for testing scope conditions --- lib/consent/rspec/consent_view.rb | 7 ++++++- spec/consent/rspec_spec.rb | 8 ++++++++ spec/permissions/some_model.rb | 4 ++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/consent/rspec/consent_view.rb b/lib/consent/rspec/consent_view.rb index fbecec3..219de0f 100644 --- a/lib/consent/rspec/consent_view.rb +++ b/lib/consent/rspec/consent_view.rb @@ -41,7 +41,12 @@ def failure_message_when_negated private def comparable_conditions(conditions) - conditions + case conditions.class.to_s + when 'ActiveRecord::Relation' + conditions.to_sql + else + conditions + end end def failure_message_base(failure) # rubocop:disable Metrics/MethodLength diff --git a/spec/consent/rspec_spec.rb b/spec/consent/rspec_spec.rb index 3e8ac0e..5aa9dee 100644 --- a/spec/consent/rspec_spec.rb +++ b/spec/consent/rspec_spec.rb @@ -30,5 +30,13 @@ it "invalidates when conditions don't match" do expect(SomeModel).to_not consent_view(:self, owner_id: 14).to(user) end + + it 'validates when conditions are a scope' do + expect(SomeModel).to_not( + consent_view(:scoped_self) + .with_conditions(SomeModel.where(owner_id: 13)) + .to(user) + ) + end end end diff --git a/spec/permissions/some_model.rb b/spec/permissions/some_model.rb index 2030d06..67619e2 100644 --- a/spec/permissions/some_model.rb +++ b/spec/permissions/some_model.rb @@ -9,6 +9,10 @@ { owner_id: user.id } end + view :scoped_self, 'Default view', + ->(_user, _obj) { true } + ->(user) { SomeModel.where(owner_id: user.id) } + view :view1, 'View 1' view :lol, 'Lol Only' do |_| { name: 'lol' }