From 3f5d7b7a481c911fc037a0c90f261cd000d8d3d2 Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Thu, 6 Aug 2020 12:39:42 +0100 Subject: [PATCH] fix: persist user when set on client --- Bugsnag/Client/BugsnagClient.m | 10 +++--- CHANGELOG.md | 7 ++++ .../scenarios/UserPersistenceScenarios.h | 3 ++ .../scenarios/UserPersistenceScenarios.m | 21 +++++++++++- features/user_persistence.feature | 33 ++++++++++++++++++- 5 files changed, 67 insertions(+), 7 deletions(-) diff --git a/Bugsnag/Client/BugsnagClient.m b/Bugsnag/Client/BugsnagClient.m index 60e426085..658f671cd 100644 --- a/Bugsnag/Client/BugsnagClient.m +++ b/Bugsnag/Client/BugsnagClient.m @@ -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]]; } diff --git a/CHANGELOG.md b/CHANGELOG.md index 55b2ebd69..1c4314122 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/features/fixtures/ios-swift-cocoapods/iOSTestApp/scenarios/UserPersistenceScenarios.h b/features/fixtures/ios-swift-cocoapods/iOSTestApp/scenarios/UserPersistenceScenarios.h index 275253036..94281d238 100644 --- a/features/fixtures/ios-swift-cocoapods/iOSTestApp/scenarios/UserPersistenceScenarios.h +++ b/features/fixtures/ios-swift-cocoapods/iOSTestApp/scenarios/UserPersistenceScenarios.h @@ -15,6 +15,9 @@ @interface UserPersistencePersistUserScenario : Scenario @end +@interface UserPersistencePersistUserClientScenario : Scenario +@end + @interface UserPersistenceDontPersistUserScenario : Scenario @end diff --git a/features/fixtures/ios-swift-cocoapods/iOSTestApp/scenarios/UserPersistenceScenarios.m b/features/fixtures/ios-swift-cocoapods/iOSTestApp/scenarios/UserPersistenceScenarios.m index 16bac4b23..5c31a5599 100644 --- a/features/fixtures/ios-swift-cocoapods/iOSTestApp/scenarios/UserPersistenceScenarios.m +++ b/features/fixtures/ios-swift-cocoapods/iOSTestApp/scenarios/UserPersistenceScenarios.m @@ -10,7 +10,7 @@ #import "UserPersistenceScenarios.h" /** - * Set a user and persist it + * Set a user on the config and persist it */ @implementation UserPersistencePersistUserScenario @@ -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:@"baz@grok.com" 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 */ diff --git a/features/user_persistence.feature b/features/user_persistence.feature index 594e2eeb0..a3533d428 100644 --- a/features/user_persistence.feature +++ b/features/user_persistence.feature @@ -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 @@ -33,6 +33,37 @@ Feature: Persisting User Information And the payload field "events.0.user.email" equals "baz@grok.com" 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 "baz@grok.com" + 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 "baz@grok.com" + And the payload field "events.0.user.name" equals "bar" + + Scenario: User Info is not persisted across app runs When I run "UserPersistenceDontPersistUserScenario"