Module: Devise::TestHelpers

Defined in:
lib/devise/test_helpers.rb

Defined Under Namespace

Classes: TestWarden

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
# File 'lib/devise/test_helpers.rb', line 3

def self.included(base)
  base.class_eval do
    setup :setup_controller_for_warden, :warden if respond_to?(:setup)
  end
end

Instance Method Details

#setup_controller_for_wardenObject

We need to setup the environment variables and the response in the controller.



51
52
53
54
55
56
# File 'lib/devise/test_helpers.rb', line 51

def setup_controller_for_warden #:nodoc:
  @request.env['action_controller.rescue.request']  = @request
  @request.env['action_controller.rescue.response'] = @response
  @request.env['rack.session'] = session
  @controller.response = @response
end

#sign_in(resource_or_scope, resource = nil) ⇒ Object

sign_in a given resource by storing its keys in the session.

Examples:

 :user, @user   # sign_in(scope, resource)
 @user          # sign_in(resource)


70
71
72
73
74
# File 'lib/devise/test_helpers.rb', line 70

def (resource_or_scope, resource=nil)
  scope    ||= Devise::Mapping.find_scope!(resource_or_scope)
  resource ||= resource_or_scope
  warden.session_serializer.store(resource, scope)
end

#sign_out(resource_or_scope) ⇒ Object

Sign out a given resource or scope by calling logout on Warden.

Examples:

sign_out :user     # sign_out(scope)
sign_out @user     # sign_out(resource)


83
84
85
86
87
# File 'lib/devise/test_helpers.rb', line 83

def sign_out(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  @controller.instance_variable_set(:"@current_#{scope}", nil)
  warden.logout(scope)
end

#wardenObject

Quick access to Warden::Proxy.



59
60
61
# File 'lib/devise/test_helpers.rb', line 59

def warden #:nodoc:
  @warden ||= (@request.env['warden'] = TestWarden.new(@controller))
end