From 7025f968c690775de42cf31a969c0e97913df59f Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sun, 6 Sep 2015 00:05:19 -0700 Subject: [PATCH] Account for relative_url_root in FailureApp's recall method Closes #3738 --- lib/devise/failure_app.rb | 13 ++++++++++++- test/failure_app_test.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/devise/failure_app.rb b/lib/devise/failure_app.rb index 56d5cbb5..bf388185 100644 --- a/lib/devise/failure_app.rb +++ b/lib/devise/failure_app.rb @@ -51,7 +51,18 @@ module Devise end def recall - env["PATH_INFO"] = attempted_path + config = Rails.application.config + + if config.try(:relative_url_root) + base_path = Pathname.new(config.relative_url_root) + full_path = Pathname.new(attempted_path) + + env["SCRIPT_NAME"] = config.relative_url_root + env["PATH_INFO"] = '/' + full_path.relative_path_from(base_path).to_s + else + env["PATH_INFO"] = attempted_path + end + flash.now[:alert] = i18n_message(:invalid) if is_flashing_format? self.response = recall_app(warden_options[:recall]).call(env) end diff --git a/test/failure_app_test.rb b/test/failure_app_test.rb index 140ac14a..44ac7671 100644 --- a/test/failure_app_test.rb +++ b/test/failure_app_test.rb @@ -294,5 +294,22 @@ class FailureTest < ActiveSupport::TestCase assert @response.third.body.include?('

Log in

') assert @response.third.body.include?('Your account is not activated yet.') end + + if Rails.application.config.respond_to?(:relative_url_root) + test 'calls the original controller with the proper environment considering the relative url root' do + swap Rails.application.config, relative_url_root: "/sample" do + env = { + "warden.options" => { recall: "devise/sessions#new", attempted_path: "/sample/users/sign_in"}, + "devise.mapping" => Devise.mappings[:user], + "warden" => stub_everything + } + call_failure(env) + assert @response.third.body.include?('

Log in

') + assert @response.third.body.include?('Invalid email or password.') + assert_equal @request.env["SCRIPT_NAME"], '/sample' + assert_equal @request.env["PATH_INFO"], '/users/sign_in' + end + end + end end end