Skip to content

Commit

Permalink
Merge pull request #3850 from nebulab/kennyadsl/deprecate-foo-ability
Browse files Browse the repository at this point in the history
Remove all usage of FooAbility and BarAbility when testing abilities
  • Loading branch information
kennyadsl authored Nov 27, 2020
2 parents 8618504 + 9964f4a commit 46d5001
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions core/spec/models/spree/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 46d5001

Please sign in to comment.