From 66902785a10e97a519651239acd1b851fe6e4e47 Mon Sep 17 00:00:00 2001 From: Javier Julio Date: Wed, 1 May 2024 17:25:58 -0400 Subject: [PATCH 1/4] Include warden scope in user info --- lib/bugsnag/middleware/warden_user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bugsnag/middleware/warden_user.rb b/lib/bugsnag/middleware/warden_user.rb index 59c6af0de..6650aa52e 100644 --- a/lib/bugsnag/middleware/warden_user.rb +++ b/lib/bugsnag/middleware/warden_user.rb @@ -21,7 +21,7 @@ def call(report) best_scope = warden_scopes.include?("user") ? "user" : warden_scopes.first # Extract useful user information - user = {} + user = { :warden_scope => best_scope } user_object = env["warden"].user({:scope => best_scope, :run_callbacks => false}) rescue nil if user_object # Build the user info for this scope From d0b34ea419568e79d78f1365aa765af06d45b07b Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Fri, 17 May 2024 08:56:28 +0100 Subject: [PATCH 2/4] Update tests to include the warden scope in user --- spec/integrations/warden_user_spec.rb | 58 ++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/spec/integrations/warden_user_spec.rb b/spec/integrations/warden_user_spec.rb index 03b0b5fdf..cfa3eeb9e 100644 --- a/spec/integrations/warden_user_spec.rb +++ b/spec/integrations/warden_user_spec.rb @@ -6,31 +6,71 @@ user = double allow(user).to receive_messages( - :email => "TEST_EMAIL", - :name => "TEST_NAME", - :created_at => "TEST_NOW" + email: "TEST_EMAIL", + name: "TEST_NAME", + created_at: "TEST_NOW", ) warden = double allow(warden).to receive(:user).with({ - :scope => "user", - :run_callbacks => false + scope: "user", + run_callbacks: false, }).and_return(user) report = double("Bugsnag::Report") + expect(report).to receive(:request_data).exactly(3).times.and_return({ + rack_env: { + "warden" => warden, + "rack.session" => { + "warden.user.user.key" => "TEST_USER", + } + } + }) + + expect(report).to receive(:user=).with({ + email: "TEST_EMAIL", + name: "TEST_NAME", + created_at: "TEST_NOW", + warden_scope: "user", + }) + + expect(callback).to receive(:call).with(report) + + middleware = Bugsnag::Middleware::WardenUser.new(callback) + middleware.call(report) + end + + it "sets the scope to the 'best scope'" do + callback = double + + user = double + allow(user).to receive_messages( + email: "TEST_EMAIL", + name: "TEST_NAME", + created_at: "TEST_NOW", + ) + + warden = double + allow(warden).to receive(:user).with({ + scope: "admin", + run_callbacks: false, + }).and_return(user) + + report = double("Bugsnag::Report") expect(report).to receive(:request_data).exactly(3).times.and_return({ :rack_env => { "warden" => warden, "rack.session" => { - "warden.user.user.key" => "TEST_USER" + "warden.user.admin.key" => "TEST_USER" } } }) expect(report).to receive(:user=).with({ - :email => "TEST_EMAIL", - :name => "TEST_NAME", - :created_at => "TEST_NOW" + email: "TEST_EMAIL", + name: "TEST_NAME", + created_at: "TEST_NOW", + warden_scope: "admin", }) expect(callback).to receive(:call).with(report) From 5be33d4ca9d392ae9f18303680a5f5f762d6661b Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Fri, 17 May 2024 08:57:24 +0100 Subject: [PATCH 3/4] Only set the warden scope if there's a user object This prevents having a user object that only contains the scope with no other information --- lib/bugsnag/middleware/warden_user.rb | 5 ++++- spec/integrations/warden_user_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/bugsnag/middleware/warden_user.rb b/lib/bugsnag/middleware/warden_user.rb index 6650aa52e..bb9b84327 100644 --- a/lib/bugsnag/middleware/warden_user.rb +++ b/lib/bugsnag/middleware/warden_user.rb @@ -21,9 +21,12 @@ def call(report) best_scope = warden_scopes.include?("user") ? "user" : warden_scopes.first # Extract useful user information - user = { :warden_scope => best_scope } + user = {} user_object = env["warden"].user({:scope => best_scope, :run_callbacks => false}) rescue nil + if user_object + user[:warden_scope] = best_scope + # Build the user info for this scope COMMON_USER_FIELDS.each do |field| user[field] = user_object.send(field) if user_object.respond_to?(field) diff --git a/spec/integrations/warden_user_spec.rb b/spec/integrations/warden_user_spec.rb index cfa3eeb9e..1a228d9a4 100644 --- a/spec/integrations/warden_user_spec.rb +++ b/spec/integrations/warden_user_spec.rb @@ -78,4 +78,31 @@ middleware = Bugsnag::Middleware::WardenUser.new(callback) middleware.call(report) end + + it "doesn't set the user if the user object is empty" do + callback = double + + warden = double + allow(warden).to receive(:user).with({ + scope: "user", + run_callbacks: false, + }).and_return(nil) + + report = double("Bugsnag::Report") + expect(report).to receive(:request_data).exactly(3).times.and_return({ + :rack_env => { + "warden" => warden, + "rack.session" => { + "warden.user.user.key" => "TEST_USER" + } + } + }) + + expect(report).not_to receive(:user=) + + expect(callback).to receive(:call).with(report) + + middleware = Bugsnag::Middleware::WardenUser.new(callback) + middleware.call(report) + end end From 4692e1c40f7a83b1a6c4b6ac60ca7823c5c3e6b0 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Fri, 17 May 2024 08:58:10 +0100 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 111dc7998..d17bd5f65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ Changelog ========= +## TBD + +### Enhancements + +* Include the Warden scope in user metadata + | [#821](https://github.com/bugsnag/bugsnag-ruby/pull/821) + | [javierjulio](https://github.com/javierjulio) + ## v6.26.4 (25 March 2024) ### Fixes