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:

* 1aab449933 (diff-c1be825bdb5f3160081e41432f83d0d7R278)
* https://github.com/plataformatec/devise/issues/5021
This commit is contained in:
Leonardo Tegon
2019-02-11 11:00:56 -02:00
committed by GitHub
parent 45016829d6
commit fcb04f5302
2 changed files with 29 additions and 1 deletions

View File

@@ -275,7 +275,7 @@ module Devise
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?

View File

@@ -28,6 +28,27 @@ class FailureTest < ActiveSupport::TestCase
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 @@ class FailureTest < ActiveSupport::TestCase
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