module Warden::Test::Mock

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!

Public Class Methods

included(_base) click to toggle source
# File lib/warden/test/mock.rb, line 11
def self.included(_base)
  ::Warden.test_mode!
end

Public Instance Methods

warden() click to toggle source

A helper method that provides the warden object by mocking the env variable. @api public

# File lib/warden/test/mock.rb, line 17
def warden
  @warden ||= begin
    env['warden']
  end
end

Private Instance Methods

app() click to toggle source
# File lib/warden/test/mock.rb, line 37
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
env() click to toggle source
# File lib/warden/test/mock.rb, line 25
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