From fcb04f53023d04b4b308acedb762bcefcf86f9a7 Mon Sep 17 00:00:00 2001 From: Leonardo Tegon Date: Mon, 11 Feb 2019 11:00:56 -0200 Subject: [PATCH] Check if `root_path` is defined with `#respond_to?` instead of `#present` (#5022) When an application does not define a `root`, the method will be undefined instead of returning a falsey value. This commit also includes a new test with fake objects that mimic this behavior. Related resources: * https://github.com/plataformatec/devise/commit/1aab4499330c8a7641a3dea7e2ec3b96acdf46ae#diff-c1be825bdb5f3160081e41432f83d0d7R278 * https://github.com/plataformatec/devise/issues/5021 --- lib/devise/failure_app.rb | 2 +- test/failure_app_test.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/devise/failure_app.rb b/lib/devise/failure_app.rb index 4894ecb779..9dcaea8d8c 100644 --- a/lib/devise/failure_app.rb +++ b/lib/devise/failure_app.rb @@ -275,7 +275,7 @@ def relative_url_root? private def root_path_defined?(context) - defined?(context.routes) && context.routes.url_helpers.root_path.present? + defined?(context.routes) && context.routes.url_helpers.respond_to?(:root_path) end def rails_5_and_down? diff --git a/test/failure_app_test.rb b/test/failure_app_test.rb index 2429d40673..af622fff26 100644 --- a/test/failure_app_test.rb +++ b/test/failure_app_test.rb @@ -28,6 +28,27 @@ def i18n_options(options) end end + class FailureWithoutRootPath < Devise::FailureApp + class FakeURLHelpers + end + + class FakeRoutesWithoutRoot + def url_helpers + FakeURLHelpers.new + end + end + + class FakeAppWithoutRootPath + def routes + FakeRoutesWithoutRoot.new + end + end + + def main_app + FakeAppWithoutRootPath.new + end + end + class FakeEngineApp < Devise::FailureApp class FakeEngine def new_user_on_engine_session_url _ @@ -103,6 +124,13 @@ def call_failure(env_params={}) end end + test 'returns to the root path even when it\'s not defined' do + call_failure app: FailureWithoutRootPath + assert_equal 302, @response.first + assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert] + assert_equal 'http://test.host/', @response.second['Location'] + end + test 'returns to the root path considering subdomain if no session path is available' do swap Devise, router_name: :fake_app do call_failure app: FailureWithSubdomain