Sha256: 62d4c610aa1962552a05c0c877ec4e76f4ef6e3f1729f7642ae900061da4e45d

Contents?: true

Size: 1.74 KB

Versions: 15

Compression:

Stored size: 1.74 KB

Contents

module Spree
  module TestingSupport
    module AuthorizationHelpers
      module CustomAbility
        def build_ability(&block)
          block ||= proc { |_u| can :manage, :all }
          Class.new do
            include CanCan::Ability
            define_method(:initialize, block)
          end
        end
      end

      module Controller
        include CustomAbility

        def stub_authorization!(&block)
          ability_class = build_ability(&block)
          before do
            allow(controller).to receive(:current_ability).and_return(ability_class.new(nil))
          end
        end
      end

      module Request
        include CustomAbility

        def stub_authorization!
          ability = build_ability
          ability_class = Spree::Dependencies.ability_class.constantize

          after(:all) do
            ability_class.remove_ability(ability)
          end

          before(:all) do
            ability_class.register_ability(ability)
          end

          before do
            allow(Spree.user_class).to receive(:find_by).
              with(hash_including(:spree_api_key)).
              and_return(Spree.user_class.new)
          end
        end

        def custom_authorization!(&block)
          ability = build_ability(&block)
          ability_class = Spree::Dependencies.ability_class.constantize

          after(:all) do
            ability_class.remove_ability(ability)
          end
          before(:all) do
            ability_class.register_ability(ability)
          end
        end
      end
    end
  end
end

RSpec.configure do |config|
  config.extend Spree::TestingSupport::AuthorizationHelpers::Controller, type: :controller
  config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :feature
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
spree_core-4.2.7 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.6 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.5 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.4 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.3.1 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.3 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.2 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.1 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.0 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.0.rc5 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.0.rc4 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.0.rc3 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.0.rc2 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.0.rc1 lib/spree/testing_support/authorization_helpers.rb
spree_core-4.2.0.beta lib/spree/testing_support/authorization_helpers.rb