diff --git a/lib/bugsnag/middleware/warden_user.rb b/lib/bugsnag/middleware/warden_user.rb index 6650aa52..bb9b8432 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 cfa3eeb9..1a228d9a 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