diff --git a/core/spec/models/spree/ability_spec.rb b/core/spec/models/spree/ability_spec.rb index 524277ea50b..3863b03baf0 100644 --- a/core/spec/models/spree/ability_spec.rb +++ b/core/spec/models/spree/ability_spec.rb @@ -5,24 +5,6 @@ require 'cancan/matchers' require 'spree/testing_support/ability_helpers' -Spree::Deprecation.silence do - require 'spree/testing_support/bar_ability' -end - -# Fake ability for testing registration of additional abilities -class FooAbility - include CanCan::Ability - - def initialize(_user) - # allow anyone to perform index on Order - can :index, Spree::Order - # allow anyone to update an Order with id of 1 - can :update, Spree::Order do |order| - order.id == 1 - end - end -end - RSpec.describe Spree::Ability, type: :model do let(:user) { build(:user) } let(:ability) { Spree::Ability.new(user) } @@ -45,12 +27,31 @@ def initialize(_user) context 'register_ability' do it 'should add the ability to the list of abilties' do - Spree::Ability.register_ability(FooAbility) + foo_ability = Class.new do + include CanCan::Ability + + def initialize(_user) + can :index, Spree::Order + end + end + + Spree::Ability.register_ability(foo_ability) expect(Spree::Ability.new(user).abilities).not_to be_empty end it 'should apply the registered abilities permissions' do - Spree::Ability.register_ability(FooAbility) + foo_ability = Class.new do + include CanCan::Ability + + def initialize(_user) + can :index, Spree::Order + can :update, Spree::Order do |order| + order.id == 1 + end + end + end + + Spree::Ability.register_ability(foo_ability) expect(Spree::Ability.new(user).can?(:update, mock_model(Spree::Order, user: nil, id: 1))).to be true end end @@ -93,7 +94,17 @@ def initialize(_user) it 'should be able to admin on the order and shipment pages' do user.spree_roles << Spree::Role.find_or_create_by(name: 'bar') - Spree::Ability.register_ability(BarAbility) + bar_ability = Class.new do + include CanCan::Ability + + def initialize(user) + if user.has_spree_role? 'bar' + can [:admin, :index, :show], Spree::Order + can [:admin, :manage], Spree::Shipment + end + end + end + Spree::Ability.register_ability(bar_ability) expect(ability).not_to be_able_to :admin, resource @@ -117,8 +128,6 @@ def initialize(_user) # It can create new users if is has access to the :admin, User!! # TODO: change the Ability class so only users and customers get the extra premissions? - - Spree::Ability.remove_ability(BarAbility) end end