Skip to content

Commit

Permalink
Check if root_path is defined with #respond_to? instead of `#pres…
Browse files Browse the repository at this point in the history
…ent` (#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:

* 1aab449#diff-c1be825bdb5f3160081e41432f83d0d7R278
* #5021
  • Loading branch information
tegon authored Feb 11, 2019
1 parent 4501682 commit fcb04f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/devise/failure_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
28 changes: 28 additions & 0 deletions test/failure_app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fcb04f5

Please sign in to comment.