From 57d7a75b658a193be4cb611a9068d436e1856b9e Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 10 Jul 2014 08:48:37 -0300 Subject: [PATCH] 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. --- test/test_helpers_test.rb | 47 ++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/test/test_helpers_test.rb b/test/test_helpers_test.rb index 997ba8e8..fd1a2028 100644 --- a/test/test_helpers_test.rb +++ b/test/test_helpers_test.rb @@ -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, "You are being redirected." @@ -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