From 308cf42a75333841a756b97c1a3d0552883c6880 Mon Sep 17 00:00:00 2001 From: Alessandro Barbieri Date: Fri, 17 Nov 2017 15:42:39 +0100 Subject: [PATCH 1/4] FIX plataformatec/devise#4127 --- lib/devise/failure_app.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/devise/failure_app.rb b/lib/devise/failure_app.rb index 4f7a3ddc8b..3340e92616 100644 --- a/lib/devise/failure_app.rb +++ b/lib/devise/failure_app.rb @@ -142,11 +142,15 @@ def scope_url opts[:format] = request_format unless skip_format? - opts[:script_name] = relative_url_root if relative_url_root? - router_name = Devise.mappings[scope].router_name || Devise.available_router_name context = send(router_name) + if relative_url_root? + opts[:script_name] = relative_url_root + elsif defined? context.routes + opts[:script_name] = context.routes.url_helpers.root_path + end + if context.respond_to?(route) context.send(route, opts) elsif respond_to?(:root_url) From 71f45bf117996cb8a552e438cd0621e286f5ff88 Mon Sep 17 00:00:00 2001 From: Alessandro Barbieri Date: Fri, 17 Nov 2017 16:24:13 +0100 Subject: [PATCH 2/4] FIX issue with trailing slash --- lib/devise/failure_app.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/devise/failure_app.rb b/lib/devise/failure_app.rb index 3340e92616..a815e089ff 100644 --- a/lib/devise/failure_app.rb +++ b/lib/devise/failure_app.rb @@ -148,7 +148,8 @@ def scope_url if relative_url_root? opts[:script_name] = relative_url_root elsif defined? context.routes - opts[:script_name] = context.routes.url_helpers.root_path + rootpath = context.routes.url_helpers.root_path + opts[:script_name] = rootpath.chomp('/') unless rootpath.length <= 1 end if context.respond_to?(route) From 2987e80125e7d444a535882b7fe837aa4c28c68b Mon Sep 17 00:00:00 2001 From: Alessandro Barbieri Date: Wed, 17 Jan 2018 15:34:23 +0100 Subject: [PATCH 3/4] Add test for mountable engine for root and secondary routes --- test/integration/mounted_engine_test.rb | 32 +++++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/test/integration/mounted_engine_test.rb b/test/integration/mounted_engine_test.rb index 98dcf47594..36e40776aa 100644 --- a/test/integration/mounted_engine_test.rb +++ b/test/integration/mounted_engine_test.rb @@ -1,9 +1,22 @@ require 'test_helper' -class MyMountableEngine - def self.call(env) - ['200', { 'Content-Type' => 'text/html' }, ['Rendered content of MyMountableEngine']] +module MyMountableEngine + class Engine < ::Rails::Engine + isolate_namespace MyMountableEngine end + class TestsController < ActionController::Base + def index + render plain: 'Root test successful' + end + def inner_route + render plain: 'Inner route test successful' + end + end +end + +MyMountableEngine::Engine.routes.draw do + get 'test', to: 'tests#inner_route' + root to: 'tests#index' end # If disable_clear_and_finalize is set to true, Rails will not clear other routes when calling @@ -13,7 +26,7 @@ def self.call(env) Rails.application.routes.draw do authenticate(:user) do - mount MyMountableEngine, at: '/mountable_engine' + mount MyMountableEngine::Engine, at: '/mountable_engine' end end @@ -31,6 +44,15 @@ class AuthenticatedMountedEngineTest < Devise::IntegrationTest get '/mountable_engine' assert_response :success - assert_contain 'Rendered content of MyMountableEngine' + assert_contain 'Root test successful' + end + + + test 'renders a inner route of the mounted engine when authenticated' do + sign_in_as_user + get '/mountable_engine/test' + + assert_response :success + assert_contain 'Inner route test successful' end end From f271f7636ec8a9da784287bfc0b27fa67589b75e Mon Sep 17 00:00:00 2001 From: Alessandro Barbieri Date: Wed, 17 Jan 2018 15:45:55 +0100 Subject: [PATCH 4/4] Add test for 404 error --- test/integration/mounted_engine_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/integration/mounted_engine_test.rb b/test/integration/mounted_engine_test.rb index 36e40776aa..4f79f79dd1 100644 --- a/test/integration/mounted_engine_test.rb +++ b/test/integration/mounted_engine_test.rb @@ -55,4 +55,12 @@ class AuthenticatedMountedEngineTest < Devise::IntegrationTest assert_response :success assert_contain 'Inner route test successful' end + + test 'respond properly to a non existing route of the mounted engine' do + sign_in_as_user + + assert_raise ActionController::RoutingError do + get '/mountable_engine/non-existing-route' + end + end end