diff --git a/CHANGELOG.md b/CHANGELOG.md index 111dc799..d17bd5f6 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 diff --git a/lib/bugsnag/middleware/warden_user.rb b/lib/bugsnag/middleware/warden_user.rb index 59c6af0d..bb9b8432 100644 --- a/lib/bugsnag/middleware/warden_user.rb +++ b/lib/bugsnag/middleware/warden_user.rb @@ -23,7 +23,10 @@ def call(report) # Extract useful user information 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 03b0b5fd..1a228d9a 100644 --- a/spec/integrations/warden_user_spec.rb +++ b/spec/integrations/warden_user_spec.rb @@ -6,33 +6,100 @@ 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) + + 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)