Sha256: d98d1eaf1b295f8d2d4a048206594588dbea03c1e8d51966f8745ad543c4f938

Contents?: true

Size: 1.79 KB

Versions: 34

Compression:

Stored size: 1.79 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
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
solidus_core-2.10.5 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.10.3 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.10.2 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.9.6 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.8.6 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.10.1 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.9.5 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.10.0 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.9.4 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.6.6 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.7.4 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.8.5 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.9.3 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.9.2 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.10.0.beta1 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.7.3 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.6.5 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.9.1 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.9.0 lib/spree/testing_support/authorization_helpers.rb
solidus_core-2.9.0.rc.1 lib/spree/testing_support/authorization_helpers.rb