1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Use Devise's swap mechanism in failure app test

Also refactor tests to remove the custom failure app class only used
once in favor of an inline class for the specific test, makes it easier
to follow what's going on.
This commit is contained in:
Carlos Antonio da Silva 2014-07-10 08:48:37 -03:00
parent 2e8aecdac2
commit 57d7a75b65

View file

@ -4,12 +4,6 @@ class TestHelpersTest < ActionController::TestCase
tests UsersController
include Devise::TestHelpers
class CustomFailureApp < Devise::FailureApp
def redirect
self.status = 306
end
end
test "redirects if attempting to access a page unauthenticated" do
get :index
assert_redirected_to new_user_session_path
@ -72,12 +66,33 @@ class TestHelpersTest < ActionController::TestCase
end
test "respects custom failure app" do
swap Devise.warden_config, failure_app: CustomFailureApp do
custom_failure_app = Class.new(Devise::FailureApp) do
def redirect
self.status = 306
end
end
swap Devise.warden_config, failure_app: custom_failure_app do
get :index
assert_response 306
end
end
test "passes given headers from the failure app to the response" do
custom_failure_app = Class.new(Devise::FailureApp) do
def respond
self.status = 401
self.response.headers["CUSTOMHEADER"] = 1
end
end
swap Devise.warden_config, failure_app: custom_failure_app do
sign_in create_user
get :index
assert_equal 1, @response.headers["CUSTOMHEADER"]
end
end
test "returns the body of a failure app" do
get :index
assert_equal response.body, "<html><body>You are being <a href=\"http://test.host/users/sign_in\">redirected</a>.</body></html>"
@ -145,22 +160,4 @@ class TestHelpersTest < ActionController::TestCase
get :index
assert_match /User ##{second_user.id}/, @response.body
end
test "passes given headers from the failure app to the response" do
begin
old_failure_app = Devise.warden_config[:failure_app]
Devise.warden_config[:failure_app] = Class.new(Devise::FailureApp) do
def respond
self.status = 401
self.response.headers["CUSTOMHEADER"] = 1
end
end
sign_in create_user
get :index
assert_equal 1, @response.headers["CUSTOMHEADER"]
ensure
Devise.warden_config[:failure_app] = old_failure_app
end
end
end