Skip to content

Commit

Permalink
fix: persist user when set on client
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Aug 6, 2020
1 parent 9166ac7 commit 3f5d7b7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Bugsnag/Client/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -787,22 +787,22 @@ - (void)leaveBreadcrumbWithMessage:(NSString *_Nonnull)message

- (BugsnagUser *_Nonnull)user
{
NSDictionary *userInfo = [self.metadata getMetadataFromSection:BSGKeyUser];
return [[BugsnagUser alloc] initWithDictionary:userInfo ?: @{}];
return self.configuration.user;
}

- (void)setUser:(NSString *_Nullable)userId
withEmail:(NSString *_Nullable)email
andName:(NSString *_Nullable)name
{
[self.configuration setUser:userId withEmail:email andName:name];
[self.metadata addMetadata:userId withKey:BSGKeyId toSection:BSGKeyUser];
[self.metadata addMetadata:name withKey:BSGKeyName toSection:BSGKeyUser];
[self.metadata addMetadata:email withKey:BSGKeyEmail toSection:BSGKeyUser];

NSMutableDictionary *dict = [NSMutableDictionary new];
BSGDictInsertIfNotNil(dict, self.user.id, @"id");
BSGDictInsertIfNotNil(dict, self.user.email, @"email");
BSGDictInsertIfNotNil(dict, self.user.name, @"name");
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

0 comments on commit 3f5d7b7

Please sign in to comment.