From 4342894bb1b73878f6f35925064929c704f63cfa Mon Sep 17 00:00:00 2001 From: Chris Gunther Date: Sat, 1 Mar 2025 14:27:40 -0500 Subject: [PATCH] Set `attempted_path` in `warden.options` before calling failure app in controller test helpers In controller tests, the failure app seems to be called directly rather than going through Warden. As such, it duplicates some of the set up Warden does before calling the failure app, however didn't set `attempted_path`, like Warden does. Therefore, if your custom failure app relied on `attempted_path`, it wouldn't be set in your controller tests, leading to different behavior in tests versus production. This now brings the test helper closer in-line with the Warden implementation. https://github.com/wardencommunity/warden/blob/67f5ba6baaa7564ec79afef02cf3a4d0f7d312e5/lib/warden/manager.rb#L138-L143 --- CHANGELOG.md | 5 +++++ lib/devise/test/controller_helpers.rb | 1 + test/test/controller_helpers_test.rb | 13 +++++++++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ccb0882a4..f147a731dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### Unreleased + +* bug fixes + * Set `attempted_path` in `warden.options` before calling failure app in controller test helpers. #5768 + ### 5.0.4 - 2026-05-08 * security fixes diff --git a/lib/devise/test/controller_helpers.rb b/lib/devise/test/controller_helpers.rb index d3522a3466..6a29b5f33f 100644 --- a/lib/devise/test/controller_helpers.rb +++ b/lib/devise/test/controller_helpers.rb @@ -125,6 +125,7 @@ def _process_unauthenticated(env, options = {}) when :custom proxy.custom_response else + options.merge!(:attempted_path => ::Rack::Request.new(env).fullpath) request.env["PATH_INFO"] = "/#{options[:action]}" request.env["warden.options"] = options Warden::Manager._run_callbacks(:before_failure, env, options) diff --git a/test/test/controller_helpers_test.rb b/test/test/controller_helpers_test.rb index a158e87536..f76c7f81d3 100644 --- a/test/test/controller_helpers_test.rb +++ b/test/test/controller_helpers_test.rb @@ -95,6 +95,19 @@ def respond end end + test "sets attempted_path in warden.options" do + custom_failure_app = Class.new(Devise::FailureApp) do + def redirect_url + attempted_path + end + end + + swap Devise.warden_config, failure_app: custom_failure_app do + get :index + assert_redirected_to "/users" + end + end + test "returns the body of a failure app" do get :index