Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all usage of FooAbility and BarAbility when testing abilities #3850

Merged
merged 4 commits into from
Nov 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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