mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Do not execute Warden::Callbacks on Devise::TestHelpers. Closes #414.
This commit is contained in:
parent
57ab87a1b6
commit
7c630fdb5e
2 changed files with 34 additions and 42 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
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|
|
||||
@after_authentication_called = true
|
||||
flunk "callback was called while it should not"
|
||||
end
|
||||
|
||||
user = create_user
|
||||
user.confirm!
|
||||
|
||||
sign_in user
|
||||
assert_equal true, @after_authentication_called
|
||||
ensure
|
||||
Warden::Manager._after_set_user.pop
|
||||
end
|
||||
end
|
||||
|
||||
test "defined Warden before_logout callback should be called when sign_out is called" do
|
||||
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|
|
||||
@before_logout_called = true
|
||||
flunk "callback was called while it should not"
|
||||
end
|
||||
user = create_user
|
||||
user.confirm!
|
||||
|
||||
sign_in user
|
||||
sign_out user
|
||||
assert_equal true, @before_logout_called
|
||||
ensure
|
||||
Warden::Manager._before_logout.pop
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue