Sha256: 3697a4a66aa8369c832c50263bb1840a84a3a6d1df71112a0346a5f64b83e021

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

module Softwear
  module Auth
    module Spec
      def spec_users
        User.instance_variable_get(:@_spec_users)
      end

      def stub_authentication!(config, *a)
        config.before(:each, *a) do
          User.instance_variable_set(:@_spec_users, [])

          allow(User).to receive(:all)   { spec_users }
          allow(User).to receive(:find)  { |n| spec_users.find { |u| u.id == n } }
          allow(User).to receive(:auth)  { @_signed_in_user or false }
          allow(User).to receive(:raw_query) { |q| raise "Unstubbed authentication query \"#{q}\"" }

          allow_any_instance_of(Softwear::Lib::ControllerAuthentication)
            .to receive(:user_token)
            .and_return('')

          if (controller rescue false)
            allow(controller).to receive(:current_user) { @_signed_in_user }
            controller.class_eval { helper_method :current_user }

            allow(controller).to receive(:user_signed_in?) { !!@_signed_in_user }
            controller.class_eval { helper_method :user_signed_in? }

            allow(controller).to receive(:destroy_user_session_path) { '#' }
            controller.class_eval { helper_method :destroy_user_session_path }

            allow(controller).to receive(:users_path) { '#' }
            controller.class_eval { helper_method :users_path }

            allow(controller).to receive(:edit_user_path) { '#' }
            controller.class_eval { protected; helper_method :edit_user_path }
          end
        end

        config.after(:each, *a) do
          User.instance_variable_set(:@_spec_users, nil)
        end
      end

      def sign_in_as(user)
        @_signed_in_user = user

        allow_any_instance_of(Softwear::Lib::ControllerAuthentication)
          .to receive(:user_token).and_return 'abc123'

        if respond_to?(:session) && session.respond_to?(:[]=)
          session[:user_token] = 'abc123'
        end
      end
      alias_method :sign_in, :sign_in_as
      alias_method :login_as, :sign_in_as
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
softwear-lib-1.5.4 lib/softwear/auth/spec.rb
softwear-lib-1.5.3 lib/softwear/auth/spec.rb
softwear-lib-1.5.2 lib/softwear/auth/spec.rb
softwear-lib-1.5.1 lib/softwear/auth/spec.rb