Sha256: a0829ae0c3bda802590ec9add1144676c40c58025ef2c6be57208be5312486ec

Contents?: true

Size: 1.87 KB

Versions: 85

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

require 'cancan'

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

          after(:all) do
            Spree::Ability.remove_ability(ability)
          end

          before(:all) do
            Spree::Ability.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)
          after(:all) do
            Spree::Ability.remove_ability(ability)
          end
          before(:all) do
            Spree::Ability.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
  config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :request
  config.extend Spree::TestingSupport::AuthorizationHelpers::Request, type: :system
end

Version data entries

85 entries across 85 versions & 1 rubygems

Version Path
solidus_core-3.3.0 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.2.5 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.2.4 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.2.3 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.1.8 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.0.8 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.2.2 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.2.1 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.2.0 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.2.0.alpha lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.0.7 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.11.17 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.1.7 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.11.16 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.0.6 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.1.6 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.11.15 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.11.14 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.0.5 lib/spree/testing_support/authorization_helpers.rb
solidus_core-3.1.5 lib/spree/testing_support/authorization_helpers.rb