Skip to content

Commit

Permalink
fix: load contexts not setting the user (#2089)
Browse files Browse the repository at this point in the history
* Update

* Update CHANGELOG

* add test

* Update

* Add test

* Apply formatting
  • Loading branch information
buenaflor authored Jun 17, 2024
1 parent 0b22d2d commit 3cac5bf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixes

- Load contexts integration not setting `SentryUser` ([#2089](https://github.com/getsentry/sentry-dart/pull/2089))
- Change app start span description from `Cold start` to `Cold Start` and `Warm start` to `Warm Start` ([#2076](https://github.com/getsentry/sentry-dart/pull/2076))
- Parse `PlatformException` from details instead of message ([#2052](https://github.com/getsentry/sentry-dart/pull/2052))

Expand Down
4 changes: 2 additions & 2 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ class SentryClient {
return _sentryId;
}

preparedEvent = _createUserOrSetDefaultIpAddress(preparedEvent);

preparedEvent = await _runBeforeSend(
preparedEvent,
hint,
Expand Down Expand Up @@ -176,8 +178,6 @@ class SentryClient {
platform: event.platform ?? sdkPlatform(_options.platformChecker.isWeb),
);

event = _createUserOrSetDefaultIpAddress(event);

if (event is SentryTransaction) {
return event;
}
Expand Down
2 changes: 2 additions & 0 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,7 @@ class Fixture {

class ExceptionWithCause {
ExceptionWithCause(this.cause, this.stackTrace);

final dynamic cause;
final dynamic stackTrace;
}
Expand All @@ -1698,6 +1699,7 @@ class ExceptionWithCauseExtractor

class ExceptionWithStackTrace {
ExceptionWithStackTrace(this.stackTrace);

final StackTrace stackTrace;
}

Expand Down
39 changes: 39 additions & 0 deletions flutter/test/integrations/load_contexts_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,45 @@ void main() {
expect(event.breadcrumbs!.length, 1);
expect(event.breadcrumbs!.first.message, 'native-mutated-applied');
});

test('apply default IP to user during captureEvent after loading context',
() async {
fixture.options.enableScopeSync = true;

const expectedIp = '{{auto}}';
String? actualIp;

const expectedId = '1';
String? actualId;

fixture.options.beforeSend = (event, hint) {
actualIp = event.user?.ipAddress;
actualId = event.user?.id;
return event;
};

final options = fixture.options;

final user = SentryUser(id: expectedId);
Map<String, dynamic> loadContexts = {'user': user.toJson()};
final future = Future.value(loadContexts);
when(fixture.methodChannel.invokeMethod<dynamic>('loadContexts'))
.thenAnswer((_) => future);
// ignore: deprecated_member_use
_channel.setMockMethodCallHandler((MethodCall methodCall) async {});

final integration = LoadContextsIntegration(fixture.methodChannel);
options.addIntegration(integration);
options.integrations.first.call(fixture.hub, options);

final client = SentryClient(options);
final event = SentryEvent();

await client.captureEvent(event);

expect(expectedIp, actualIp);
expect(expectedId, actualId);
});
});
}

Expand Down

0 comments on commit 3cac5bf

Please sign in to comment.