Skip to content

Commit

Permalink
Only flash if the request object that is loaded supports it (#4950)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinross authored and tegon committed Nov 13, 2018
1 parent 3aedbf0 commit 40f02ae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/devise/controllers/helpers.rb
Expand Up @@ -268,7 +268,7 @@ def is_navigational_format?
# 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
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/failure_app.rb
Expand Up @@ -242,7 +242,7 @@ def is_navigational_format?
# 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
Expand Down
10 changes: 10 additions & 0 deletions test/controllers/helpers_test.rb
Expand Up @@ -312,6 +312,16 @@ def setup
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
Expand Down
13 changes: 12 additions & 1 deletion test/failure_app_test.rb
Expand Up @@ -44,6 +44,10 @@ def fake_engine
end
end

class RequestWithoutFlashSupport < ActionDispatch::Request
undef_method :flash
end

def self.context(name, &block)
instance_eval(&block)
end
Expand All @@ -66,7 +70,7 @@ def call_failure(env_params={})
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
Expand Down Expand Up @@ -343,4 +347,11 @@ def call_failure(env_params={})
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

0 comments on commit 40f02ae

Please sign in to comment.