diff --git a/lib/devise/test_helpers.rb b/lib/devise/test_helpers.rb index a17b906e..acc88166 100644 --- a/lib/devise/test_helpers.rb +++ b/lib/devise/test_helpers.rb @@ -1,4 +1,11 @@ module Devise + # Devise::TestHelpers provides a facility to test controllers in isolation + # when using ActionController::TestCase allowing you to quickly sign_in or + # sign_out an 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. module TestHelpers def self.included(base) base.class_eval do @@ -61,6 +68,7 @@ module Devise end # sign_in a given resource by storing its keys in the session. + # This method bypass any warden authentication callback. # # Examples: # @@ -74,6 +82,7 @@ module Devise end # Sign out a given resource or scope by calling logout on Warden. + # This method bypass any warden logout callback. # # Examples: # @@ -83,7 +92,8 @@ module Devise def sign_out(resource_or_scope) scope = Devise::Mapping.find_scope!(resource_or_scope) @controller.instance_variable_set(:"@current_#{scope}", nil) - warden.logout(scope) + user = warden.instance_variable_get(:@users).delete(scope) + warden.session_serializer.delete(scope, user) end end diff --git a/test/test_helpers_test.rb b/test/test_helpers_test.rb index d19d3da7..5b6b0113 100644 --- a/test/test_helpers_test.rb +++ b/test/test_helpers_test.rb @@ -53,51 +53,33 @@ class TestHelpersTest < ActionController::TestCase assert_redirected_to new_user_session_path end - test "defined Warden after_authentication callback should be called when sign_in is called" do - Warden::Manager.after_authentication do |user, auth, opts| - @after_authentication_called = true - end - user = create_user - user.confirm! + test "defined Warden after_authentication callback should not be called when sign_in is called" do + begin + Warden::Manager.after_authentication do |user, auth, opts| + flunk "callback was called while it should not" + end - sign_in user - assert_equal true, @after_authentication_called + user = create_user + user.confirm! + sign_in user + ensure + Warden::Manager._after_set_user.pop + end end - test "defined Warden before_logout callback should be called when sign_out is called" do - Warden::Manager.before_logout do |user, auth, opts| - @before_logout_called = true + test "defined Warden before_logout callback should not be called when sign_out is called" do + begin + Warden::Manager.before_logout do |user, auth, opts| + flunk "callback was called while it should not" + end + user = create_user + user.confirm! + + sign_in user + sign_out user + ensure + Warden::Manager._before_logout.pop end - user = create_user - user.confirm! - - sign_in user - sign_out user - assert_equal true, @before_logout_called - end - - test "the user parameter in warden after_authentication callbacks should not be nil" do - Warden::Manager.after_authentication do |user, auth, opts| - assert_not_nil user - end - user = create_user - user.confirm! - - sign_in user - end - - # Not sure if the warden manager needs to be reset after the test cases which modify - # the callbacks, maybe the original values can just be restored or the warden manager - # class definition file can be reloaded. - test "the user parameter in warden before_logout callbacks should not be nil" do - Warden::Manager.before_logout do |user, auth, opts| - assert_not_nil user - end - user = create_user - user.confirm! - - sign_in user - sign_out user end test "allows to sign in with different users" do