mirror of
https://github.com/heartcombo/devise.git
synced 2026-01-09 23:08:05 -05:00
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:
@@ -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?
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user