Module: Devise::TestHelpers
- Defined in:
- lib/devise/test_helpers.rb
Overview
Devise::TestHelpers provides a facility to test controllers in isolation when using ActionController::TestCase allowing you to quickly sign_in or sign_out a user. Do not use Devise::TestHelpers in integration tests.
Notice you should not test Warden specific behavior (like Warden callbacks) using Devise::TestHelpers since it is a stub of the actual behavior. Such callbacks should be tested in your integration suite instead.
Class Method Summary collapse
Instance Method Summary collapse
-
#process ⇒ Object
Override process to consider warden.
-
#setup_controller_for_warden ⇒ Object
We need to setup the environment variables and the response in the controller.
-
#sign_in(resource_or_scope, resource = nil) ⇒ Object
sign_in a given resource by storing its keys in the session.
-
#sign_out(resource_or_scope) ⇒ Object
Sign out a given resource or scope by calling logout on Warden.
-
#warden ⇒ Object
Quick access to Warden::Proxy.
Class Method Details
.included(base) ⇒ Object
10 11 12 13 14 |
# File 'lib/devise/test_helpers.rb', line 10 def self.included(base) base.class_eval do setup :setup_controller_for_warden, :warden if respond_to?(:setup) end end |
Instance Method Details
#process ⇒ Object
Override process to consider warden.
17 18 19 20 21 |
# File 'lib/devise/test_helpers.rb', line 17 def process(*) result = nil _catch_warden { result = super } result end |
#setup_controller_for_warden ⇒ Object
We need to setup the environment variables and the response in the controller.
24 25 26 |
# File 'lib/devise/test_helpers.rb', line 24 def setup_controller_for_warden #:nodoc: @request.env['action_controller.instance'] = @controller end |
#sign_in(resource_or_scope, resource = nil) ⇒ Object
sign_in a given resource by storing its keys in the session. This method bypass any warden authentication callback.
Examples:
sign_in :user, @user # sign_in(scope, resource)
sign_in @user # sign_in(resource)
46 47 48 49 50 |
# File 'lib/devise/test_helpers.rb', line 46 def sign_in(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. This method bypass any warden logout callback.
Examples:
sign_out :user # sign_out(scope)
sign_out @user # sign_out(resource)
60 61 62 63 64 65 |
# File 'lib/devise/test_helpers.rb', line 60 def sign_out(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) @controller.instance_variable_set(:"@current_#{scope}", nil) user = warden.instance_variable_get(:@users).delete(scope) warden.session_serializer.delete(scope, user) end |
#warden ⇒ Object
Quick access to Warden::Proxy.
29 30 31 32 33 34 35 36 |
# File 'lib/devise/test_helpers.rb', line 29 def warden #:nodoc: @warden ||= begin manager = Warden::Manager.new(nil) do |config| config.merge! Devise.warden_config end @request.env['warden'] = Warden::Proxy.new(@request.env, manager) end end |