-
-
Notifications
You must be signed in to change notification settings - Fork 444
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
Fix: Apply user from the scope to transaction. #1424
Changes from 4 commits
de41eb8
a79f6a3
ddd27b1
f003a37
86c0a40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,12 +37,13 @@ class SentryUserProviderEventProcessorTest { | |
val result = processor.process(event, null) | ||
|
||
assertNotNull(result) | ||
assertNotNull(result.user) | ||
assertEquals("john.doe", result.user.username) | ||
assertEquals("user-id", result.user.id) | ||
assertEquals("192.168.0.1", result.user.ipAddress) | ||
assertEquals("john.doe@example.com", result.user.email) | ||
assertEquals(mapOf("key" to "value"), result.user.others) | ||
assertNotNull(result.user) { | ||
assertEquals("john.doe", it.username) | ||
assertEquals("user-id", it.id) | ||
assertEquals("192.168.0.1", it.ipAddress) | ||
assertEquals("john.doe@example.com", it.email) | ||
assertEquals(mapOf("key" to "value"), it.others) | ||
} | ||
Comment on lines
+40
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this block is duplicated across tests, maybe we extract it to a method called asserUser or something like that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in case of this test class duplicated code is a good trade off. Having fixed string values in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tbh I'm not a big fan of that, it's the same strings and same asserts across every test, every time you'd need to change either a value or adding/removing a field, you've to change it everywhere. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree 100% with @marandaneto. Treat your test code as production code. A few duplications are fine, but in this case, I would extract them to a method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will merge as it is because I am blocked by this PR, but lets please discuss and refactor this as soon as we touch this class again There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not willing to die on this hill so bare with me: I believe a test should be as self-sufficient as possible. When a test calls out to a private method It has the advantage, obviously, of having a single place to "fix" things, or even to add a new property to be set and asserted. It But that comes with the cost of potentially changing a test that wasn't supposed to be changed. Perhaps what we're missing here is a library that validates two objects are "equivalent". It solves the problem of adding a new property to the field and also expecting that to be mapped through which is a common mistake when mapping objects by hand. Both these principles/approaches are used in the .NET SDK. The assertion library (introduced by @Tyrrrz, who from what I noticed also follows the idea of self-sufficient tests) is called All of that said, this class in particular does have a lot of duplication for basic object mapping, to the point that this diff was mainly changes to asserting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kotlin generates another approach is having a test per field, instead of the whole user every time. I do agree that's a trade-off, but I often spend more time scaling the tests other than "understanding" them. |
||
} | ||
|
||
@Test | ||
|
@@ -62,12 +63,13 @@ class SentryUserProviderEventProcessorTest { | |
val result = processor.process(event, null) | ||
|
||
assertNotNull(result) | ||
assertNotNull(result.user) | ||
assertEquals("john.doe", result.user.username) | ||
assertEquals("user-id", result.user.id) | ||
assertEquals("192.168.0.1", result.user.ipAddress) | ||
assertEquals("john.doe@example.com", result.user.email) | ||
assertEquals(mapOf("key" to "value"), result.user.others) | ||
assertNotNull(result.user) { | ||
assertEquals("john.doe", it.username) | ||
assertEquals("user-id", it.id) | ||
assertEquals("192.168.0.1", it.ipAddress) | ||
assertEquals("john.doe@example.com", it.email) | ||
assertEquals(mapOf("key" to "value"), it.others) | ||
} | ||
} | ||
|
||
@Test | ||
|
@@ -78,22 +80,24 @@ class SentryUserProviderEventProcessorTest { | |
} | ||
|
||
val event = SentryEvent() | ||
event.user = User() | ||
event.user.username = "jane.smith" | ||
event.user.id = "jane-smith" | ||
event.user.ipAddress = "192.168.0.3" | ||
event.user.email = "jane.smith@example.com" | ||
event.user.others = mapOf("key" to "value") | ||
event.user = User().apply { | ||
username = "jane.smith" | ||
id = "jane-smith" | ||
ipAddress = "192.168.0.3" | ||
email = "jane.smith@example.com" | ||
others = mapOf("key" to "value") | ||
} | ||
|
||
val result = processor.process(event, null) | ||
|
||
assertNotNull(result) | ||
assertNotNull(result.user) | ||
assertEquals("jane.smith", result.user.username) | ||
assertEquals("jane-smith", result.user.id) | ||
assertEquals("192.168.0.3", result.user.ipAddress) | ||
assertEquals("jane.smith@example.com", result.user.email) | ||
assertEquals(mapOf("key" to "value"), result.user.others) | ||
assertNotNull(result.user) { | ||
assertEquals("jane.smith", it.username) | ||
assertEquals("jane-smith", it.id) | ||
assertEquals("192.168.0.3", it.ipAddress) | ||
assertEquals("jane.smith@example.com", it.email) | ||
assertEquals(mapOf("key" to "value"), it.others) | ||
} | ||
} | ||
|
||
@Test | ||
|
@@ -103,22 +107,24 @@ class SentryUserProviderEventProcessorTest { | |
} | ||
|
||
val event = SentryEvent() | ||
event.user = User() | ||
event.user.username = "jane.smith" | ||
event.user.id = "jane-smith" | ||
event.user.ipAddress = "192.168.0.3" | ||
event.user.email = "jane.smith@example.com" | ||
event.user.others = mapOf("key" to "value") | ||
event.user = User().apply { | ||
username = "jane.smith" | ||
id = "jane-smith" | ||
ipAddress = "192.168.0.3" | ||
email = "jane.smith@example.com" | ||
others = mapOf("key" to "value") | ||
} | ||
|
||
val result = processor.process(event, null) | ||
|
||
assertNotNull(result) | ||
assertNotNull(result.user) | ||
assertEquals("jane.smith", result.user.username) | ||
assertEquals("jane-smith", result.user.id) | ||
assertEquals("192.168.0.3", result.user.ipAddress) | ||
assertEquals("jane.smith@example.com", result.user.email) | ||
assertEquals(mapOf("key" to "value"), result.user.others) | ||
assertNotNull(result.user) { | ||
assertEquals("jane.smith", it.username) | ||
assertEquals("jane-smith", it.id) | ||
assertEquals("192.168.0.3", it.ipAddress) | ||
assertEquals("jane.smith@example.com", it.email) | ||
assertEquals(mapOf("key" to "value"), it.others) | ||
} | ||
} | ||
|
||
@Test | ||
|
@@ -130,14 +136,16 @@ class SentryUserProviderEventProcessorTest { | |
} | ||
|
||
val event = SentryEvent() | ||
event.user = User() | ||
event.user.others = mapOf("new-key" to "new-value") | ||
event.user = User().apply { | ||
others = mapOf("new-key" to "new-value") | ||
} | ||
|
||
val result = processor.process(event, null) | ||
|
||
assertNotNull(result) | ||
assertNotNull(result.user) | ||
assertEquals(mapOf("key" to "value", "new-key" to "new-value"), result.user.others) | ||
assertNotNull(result.user) { | ||
assertEquals(mapOf("key" to "value", "new-key" to "new-value"), it.others) | ||
} | ||
} | ||
|
||
@Test | ||
|
@@ -169,7 +177,9 @@ class SentryUserProviderEventProcessorTest { | |
val result = processor.process(event, null) | ||
|
||
assertNotNull(result) | ||
assertEquals(user.ipAddress, result.user.ipAddress) | ||
assertNotNull(result.user) { | ||
assertEquals(user.ipAddress, it.ipAddress) | ||
} | ||
} | ||
|
||
@Test | ||
|
@@ -186,6 +196,8 @@ class SentryUserProviderEventProcessorTest { | |
val result = processor.process(event, null) | ||
|
||
assertNotNull(result) | ||
assertNull(result.user.ipAddress) | ||
assertNotNull(result.user) { | ||
assertNull(it.ipAddress) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -496,6 +496,9 @@ private <T extends SentryBaseEvent> T applyScope( | |
if (sentryBaseEvent.getRequest() == null) { | ||
sentryBaseEvent.setRequest(scope.getRequest()); | ||
} | ||
if (sentryBaseEvent.getUser() == null) { | ||
sentryBaseEvent.setUser(scope.getUser()); | ||
} | ||
Comment on lines
+499
to
+501
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's about the combination with this check and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If user is not set on the event, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed, apply scope happens before event processors, that's why, the other way around would be problematic I believe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a problem that I see is, MainEventProcessor takes into consideration the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could start setting the default value even if a user is set, but the IpAddress is null. Can't we merge values instead? If I do |
||
if (sentryBaseEvent.getTags() == null) { | ||
sentryBaseEvent.setTags(new HashMap<>(scope.getTags())); | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe use
assertNotNull
with the lambda so we avoid!!
for all user asserts on this PR?