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

Persist user when set on client #770

Merged
merged 1 commit into from
Aug 7, 2020
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
11 changes: 8 additions & 3 deletions Bugsnag/Client/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -801,17 +801,22 @@ - (void)leaveBreadcrumbWithMessage:(NSString *_Nonnull)message

- (BugsnagUser *_Nonnull)user
{
return _user;
return self.configuration.user;
}

- (void)setUser:(NSString *_Nullable)userId
withEmail:(NSString *_Nullable)email
andName:(NSString *_Nullable)name
{
_user = [[BugsnagUser alloc] initWithUserId:userId name:name emailAddress:email];
[self.configuration setUser:userId withEmail:email andName:name];
NSDictionary *userJson = [_user toJson];
[self.state addMetadata:userJson toSection:BSGKeyUser];
[self notifyObservers:[[BugsnagStateEvent alloc] initWithName:kStateEventUser data:userJson]];

NSMutableDictionary *dict = [NSMutableDictionary new];
BSGDictInsertIfNotNil(dict, userId, @"id");
BSGDictInsertIfNotNil(dict, email, @"email");
BSGDictInsertIfNotNil(dict, name, @"name");
[self notifyObservers:[[BugsnagStateEvent alloc] initWithName:kStateEventUser data:dict]];
}

// =============================================================================
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

## TBD

### Bug fixes

* Persist user when set on client
[#770](https://github.com/bugsnag/bugsnag-cocoa/pull/770)

## 6.1.2 (2020-07-21)

### Bug fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
@interface UserPersistencePersistUserScenario : Scenario
@end

@interface UserPersistencePersistUserClientScenario : Scenario
@end

@interface UserPersistenceDontPersistUserScenario : Scenario
@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import "UserPersistenceScenarios.h"

/**
* Set a user and persist it
* Set a user on the config and persist it
*/
@implementation UserPersistencePersistUserScenario

Expand All @@ -28,6 +28,25 @@ - (void)run {

@end

/**
* Set a user on the client and persist it
*/
@implementation UserPersistencePersistUserClientScenario

- (void)startBugsnag {
self.config.persistUser = YES;
[super startBugsnag];
}

- (void)run {
[Bugsnag setUser:@"foo" withEmail:@"[email protected]" andName:@"bar"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[Bugsnag notifyError:[NSError errorWithDomain:@"com.bugsnag" code:833 userInfo:nil]];
});
}

@end

/**
* Set a user but don't persist it
*/
Expand Down
33 changes: 32 additions & 1 deletion features/user_persistence.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Feature: Persisting User Information
Background:
Given I clear all UserDefaults data

Scenario: User Info is persisted across app runs
Scenario: User Info is persisted from config across app runs
When I run "UserPersistencePersistUserScenario"

# User is set and comes through
Expand Down Expand Up @@ -33,6 +33,37 @@ Feature: Persisting User Information
And the payload field "events.0.user.email" equals "[email protected]"
And the payload field "events.0.user.name" equals "bar"

Scenario: User Info is persisted from client across app runs
When I run "UserPersistencePersistUserClientScenario"

# Session is captured before the user can be set on the Client
And I wait to receive a request
And I relaunch the app
Then the request is valid for the session reporting API version "1.0" for the "iOS Bugsnag Notifier" notifier
And the session "user.id" is not null
And the session "user.email" is null
And the session "user.name" is null
And I discard the oldest request

# Generate session and event
Then I run "UserPersistenceNoUserScenario"
And I wait to receive 2 requests
And I relaunch the app

# Session - User persisted
Then the request is valid for the session reporting API version "1.0" for the "iOS Bugsnag Notifier" notifier
And the session "user.id" equals "foo"
And the session "user.email" equals "[email protected]"
And the session "user.name" equals "bar"
And I discard the oldest request

# Event - User persisted
Then the request is valid for the error reporting API version "4.0" for the "iOS Bugsnag Notifier" notifier
And the payload field "events.0.user.id" equals "foo"
And the payload field "events.0.user.email" equals "[email protected]"
And the payload field "events.0.user.name" equals "bar"


Scenario: User Info is not persisted across app runs
When I run "UserPersistenceDontPersistUserScenario"

Expand Down