Module: Devise::Test::ControllerHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/devise/test/controller_helpers.rb
Overview
‘Devise::Test::ControllerHelpers` 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::Test::ControllerHelpers` in integration tests.
Examples
class PostsTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
test 'authenticated users can GET index' do
sign_in users(:bob)
get :index
assert_response :success
end
end
Important: you should not test Warden specific behavior (like callbacks) using ‘Devise::Test::ControllerHelpers` since it is a stub of the actual behavior. Such callbacks should be tested in your integration suite instead.
Instance Method Summary collapse
-
#process ⇒ Object
Override process to consider warden.
-
#setup_controller_for_warden ⇒ Object
We need to set up the environment variables and the response in the controller.
-
#sign_in(resource, deprecated = nil, scope: 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.
Instance Method Details
#process ⇒ Object
Override process to consider warden.
34 35 36 37 38 |
# File 'lib/devise/test/controller_helpers.rb', line 34 def process(*) _catch_warden { super } @response end |
#setup_controller_for_warden ⇒ Object
We need to set up the environment variables and the response in the controller.
43 44 45 |
# File 'lib/devise/test/controller_helpers.rb', line 43 def setup_controller_for_warden #:nodoc: @request.env['action_controller.instance'] = @controller end |
#sign_in(resource, deprecated = nil, scope: nil) ⇒ Object
sign_in a given resource by storing its keys in the session. This method bypass any warden authentication callback.
-
resource
- The resource that should be authenticated -
scope
- An optionalSymbol
with the scope where the resourceshould be signed in with.
Examples:
sign_in users(:alice) sign_in users(:alice), scope: :admin
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/devise/test/controller_helpers.rb', line 67 def sign_in(resource, deprecated = nil, scope: nil) if deprecated.present? scope = resource resource = deprecated Devise.deprecator.warn <<-DEPRECATION.strip_heredoc [Devise] sign_in(:#{scope}, resource) on controller tests is deprecated and will be removed from Devise. Please use sign_in(resource, scope: :#{scope}) instead. DEPRECATION end scope ||= Devise::Mapping.find_scope!(resource) warden.instance_variable_get(:@users).delete(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)
92 93 94 95 96 97 |
# File 'lib/devise/test/controller_helpers.rb', line 92 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.
48 49 50 51 52 53 54 55 |
# File 'lib/devise/test/controller_helpers.rb', line 48 def warden #:nodoc: @request.env['warden'] ||= begin manager = Warden::Manager.new(nil) do |config| config.merge! Devise.warden_config end Warden::Proxy.new(@request.env, manager) end end |