Only flash if the request object that is loaded supports it (#4950)

This commit is contained in:
Colin Ross
2018-11-13 09:26:00 -08:00
committed by Leonardo Tegon
parent 3aedbf0a4d
commit 40f02ae69b
4 changed files with 24 additions and 3 deletions

View File

@@ -268,7 +268,7 @@ module Devise
# Check if flash messages should be emitted. Default is to do it on
# navigational formats
def is_flashing_format?
is_navigational_format?
request.respond_to?(:flash) && is_navigational_format?
end
private

View File

@@ -242,7 +242,7 @@ module Devise
# Check if flash messages should be emitted. Default is to do it on
# navigational formats
def is_flashing_format?
is_navigational_format?
request.respond_to?(:flash) && is_navigational_format?
end
def request_format

View File

@@ -312,6 +312,16 @@ class ControllerAuthenticatableTest < Devise::ControllerTestCase
end
end
test 'is_flashing_format? depends on is_navigation_format?' do
@controller.expects(:is_navigational_format?).returns(true)
assert @controller.is_flashing_format?
end
test 'is_flashing_format? is guarded against flash (middleware) not being loaded' do
@controller.request.expects(:respond_to?).with(:flash).returns(false)
refute @controller.is_flashing_format?
end
test 'is not a devise controller' do
refute @controller.devise_controller?
end

View File

@@ -44,6 +44,10 @@ class FailureTest < ActiveSupport::TestCase
end
end
class RequestWithoutFlashSupport < ActionDispatch::Request
undef_method :flash
end
def self.context(name, &block)
instance_eval(&block)
end
@@ -66,7 +70,7 @@ class FailureTest < ActiveSupport::TestCase
end
@response = (env.delete(:app) || Devise::FailureApp).call(env).to_a
@request = ActionDispatch::Request.new(env)
@request = (env.delete(:request_klass) || ActionDispatch::Request).new(env)
end
context 'When redirecting' do
@@ -343,4 +347,11 @@ class FailureTest < ActiveSupport::TestCase
assert_equal Devise::FailureApp.new.lazy_loading_works?, "yes it does"
end
end
context "Without Flash Support" do
test "returns to the default redirect location without a flash message" do
call_failure request_klass: RequestWithoutFlashSupport
assert_equal 302, @response.first
assert_equal 'http://test.host/users/sign_in', @response.second['Location']
end
end
end