Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include warden scope in user info #821

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/bugsnag/middleware/warden_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
85 changes: 76 additions & 9 deletions spec/integrations/warden_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading