Sha256: 666db698d9548132587235c5707638817364a25860494deaf1e0c00f6342815f

Contents?: true

Size: 1.59 KB

Versions: 7

Compression:

Stored size: 1.59 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

require 'rack'

module Warden
  module Test
    # A mock of an application to get a Warden object to test on
    # Note: During the teardown phase of your specs you should include: Warden.test_reset!
    module Mock
      def self.included(_base)
        ::Warden.test_mode!
      end

      # A helper method that provides the warden object by mocking the env variable.
      # @api public
      def warden
        @warden ||= begin
          env['warden']
        end
      end

      private

      def env
        @env ||= begin
          request = Rack::MockRequest.env_for(
            "/?#{Rack::Utils.build_query({})}",
            { 'HTTP_VERSION' => '1.1', 'REQUEST_METHOD' => 'GET' }
          )
          app.call(request)

          request
        end
      end

      def app
        @app ||= begin
          opts = {
            failure_app: lambda { |_e|
              [401, { 'Content-Type' => 'text/plain' }, ['You Fail!']]
            },
            default_strategies: :password,
            default_serializers: :session
          }
          Rack::Builder.new do
            use Warden::Test::Mock::Session
            use Warden::Manager, opts, &proc {}
            run lambda { |_e|
              [200, { 'Content-Type' => 'text/plain' }, ['You Win']]
            }
          end
        end
      end

      class Session
        attr_accessor :app
        def initialize(app, _configs={})
          @app = app
        end

        def call(e)
          e['rack.session'] ||= {}
          @app.call(e)
        end
      end # session
    end
  end
end

Version data entries

7 entries across 6 versions & 4 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/warden-1.2.9/lib/warden/test/mock.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/warden-1.2.9/lib/warden/test/mock.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/warden-1.2.9/lib/warden/test/mock.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/warden-1.2.9/lib/warden/test/mock.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/warden-1.2.9/lib/warden/test/mock.rb
warden-1.2.9 lib/warden/test/mock.rb
warden-1.2.8 lib/warden/test/mock.rb