Skip to content

Commit

Permalink
Merge pull request #505 from bugsnag/v6-user
Browse files Browse the repository at this point in the history
Rename setUser/user interface on Bugsnag and BugsnagConfiguration
  • Loading branch information
fractalwrench authored Mar 27, 2020
2 parents 6961b20 + e423951 commit e9a9c92
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 57 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Bugsnag Notifiers on other platforms.

## Enhancements

* Rename setUser/user interface on `Bugsnag` and `BugsnagConfiguration`
[#505](https://github.com/bugsnag/bugsnag-cocoa/pull/505)

* Rename `config.notifierType` to `config.appType`
[#504](https://github.com/bugsnag/bugsnag-cocoa/pull/504)

Expand Down
13 changes: 11 additions & 2 deletions Source/Bugsnag.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ static NSString *_Nonnull const BugsnagSeverityInfo = @"info";
+ (void)clearMetadataInSection:(NSString *_Nonnull)sectionName
NS_SWIFT_NAME(clearMetadata(section:));

// =============================================================================
// MARK: - User
// =============================================================================

/**
* The current user
*/
+ (BugsnagUser *_Nonnull)user;

/**
* Set user metadata
*
Expand All @@ -307,8 +316,8 @@ static NSString *_Nonnull const BugsnagSeverityInfo = @"info";
* @param email Email address of the user
*/
+ (void)setUser:(NSString *_Nullable)userId
withName:(NSString *_Nullable)name
andEmail:(NSString *_Nullable)email;
withEmail:(NSString *_Nullable)email
andName:(NSString *_Nullable)name;

// =============================================================================
// MARK: - onSend
Expand Down
10 changes: 7 additions & 3 deletions Source/Bugsnag.m
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,14 @@ + (void)setContext:(NSString *_Nullable)context {
[self configuration].context = context;
}

+ (BugsnagUser *)user {
return [[self configuration] user];
}

+ (void)setUser:(NSString *_Nullable)userId
withName:(NSString *_Nullable)name
andEmail:(NSString *_Nullable)email {
[[self configuration] setUser:userId withName:name andEmail:email];
withEmail:(NSString *_Nullable)email
andName:(NSString *_Nullable)name {
[[self configuration] setUser:userId withEmail:email andName:name];
}

+ (void)addOnSessionBlock:(BugsnagOnSessionBlock _Nonnull)block
Expand Down
18 changes: 11 additions & 7 deletions Source/BugsnagConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ typedef NS_OPTIONS(NSUInteger, BSGErrorType) {
*/
@property(readwrite, strong, nonnull) NSURLSession *session;

/**
* The current user
*/
@property(retain, nullable) BugsnagUser *currentUser;

/**
* Optional handler invoked when an error or crash occurs
*/
Expand Down Expand Up @@ -194,6 +189,15 @@ typedef NS_OPTIONS(NSUInteger, BSGErrorType) {
- (void)setEndpointsForNotify:(NSString *_Nonnull)notify
sessions:(NSString *_Nonnull)sessions NS_SWIFT_NAME(setEndpoints(notify:sessions:));

// =============================================================================
// MARK: - User
// =============================================================================

/**
* The current user
*/
@property(readonly, retain, nonnull) BugsnagUser *user;

/**
* Set user metadata
*
Expand All @@ -202,8 +206,8 @@ typedef NS_OPTIONS(NSUInteger, BSGErrorType) {
* @param email Email address of the user
*/
- (void)setUser:(NSString *_Nullable)userId
withName:(NSString *_Nullable)name
andEmail:(NSString *_Nullable)email;
withEmail:(NSString *_Nullable)email
andName:(NSString *_Nullable)name;

// =============================================================================
// MARK: - onSession
Expand Down
36 changes: 18 additions & 18 deletions Source/BugsnagConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ - (instancetype _Nonnull)initWithApiKey:(NSString *_Nonnull)apiKey
_persistUser = YES;
// Only gets persisted user data if there is any, otherwise nil
// persistUser isn't settable until post-init.
_currentUser = [self getPersistedUserData];
[self setUserMetadataFromUser:_currentUser];
_user = [self getPersistedUserData];
[self setUserMetadataFromUser:_user];

#if !DEBUG
_enabledErrorTypes |= BSGErrorTypesOOMs;
Expand Down Expand Up @@ -188,18 +188,17 @@ - (BOOL)shouldSendReports {
[self.notifyReleaseStages containsObject:self.releaseStage];
}

- (void)setUser:(NSString *)userId
withName:(NSString *)userName
andEmail:(NSString *)userEmail
{
self.currentUser = [[BugsnagUser alloc] initWithUserId:userId name:userName emailAddress:userEmail];
- (void)setUser:(NSString *_Nullable)userId
withEmail:(NSString *_Nullable)email
andName:(NSString *_Nullable)name {
_user = [[BugsnagUser alloc] initWithUserId:userId name:name emailAddress:email];

// Persist the user
if (_persistUser)
[self persistUserData];

// Add user info to the metadata
[self setUserMetadataFromUser:self.currentUser];
[self setUserMetadataFromUser:self.user];
}

/**
Expand Down Expand Up @@ -300,10 +299,11 @@ - (BugsnagUser *)getPersistedUserData {
NSString *name = [BSG_SSKeychain passwordForService:kBugsnagUserName account:kBugsnagUserKeychainAccount];
NSString *userId = [BSG_SSKeychain passwordForService:kBugsnagUserUserId account:kBugsnagUserKeychainAccount];

if (email || name || userId)
if (email || name || userId) {
return [[BugsnagUser alloc] initWithUserId:userId name:name emailAddress:email];

return nil;
} else {
return [[BugsnagUser alloc] initWithUserId:nil name:nil emailAddress:nil];
}
}
}

Expand All @@ -313,10 +313,10 @@ - (BugsnagUser *)getPersistedUserData {
*/
- (void)persistUserData {
@synchronized(self) {
if (_currentUser) {
if (_user) {
// Email
if (_currentUser.emailAddress) {
[BSG_SSKeychain setPassword:_currentUser.emailAddress
if (_user.emailAddress) {
[BSG_SSKeychain setPassword:_user.emailAddress
forService:kBugsnagUserEmailAddress
account:kBugsnagUserKeychainAccount];
}
Expand All @@ -326,8 +326,8 @@ - (void)persistUserData {
}

// Name
if (_currentUser.name) {
[BSG_SSKeychain setPassword:_currentUser.name
if (_user.name) {
[BSG_SSKeychain setPassword:_user.name
forService:kBugsnagUserName
account:kBugsnagUserKeychainAccount];
}
Expand All @@ -337,8 +337,8 @@ - (void)persistUserData {
}

// UserId
if (_currentUser.userId) {
[BSG_SSKeychain setPassword:_currentUser.userId
if (_user.userId) {
[BSG_SSKeychain setPassword:_user.userId
forService:kBugsnagUserUserId
account:kBugsnagUserKeychainAccount];
}
Expand Down
2 changes: 1 addition & 1 deletion Source/BugsnagSessionTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ - (void)startNewSessionWithAutoCaptureValue:(BOOL)isAutoCaptured {

self.currentSession = [[BugsnagSession alloc] initWithId:[[NSUUID UUID] UUIDString]
startDate:[NSDate date]
user:self.config.currentUser
user:self.config.user
autoCaptured:isAutoCaptured];

[self.sessionStore write:self.currentSession];
Expand Down
29 changes: 16 additions & 13 deletions Tests/BugsnagConfigurationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ - (void)testUserPersistence {
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserUserId account:kBugsnagUserKeychainAccount]);

// user should be persisted by default
[config setUser:userId withName:name andEmail:email];
[config setUser:userId withEmail:email andName:name];

// Check values manually
// XCTAssertEqualObjects([bsg_SSKeychain passwordForService:kBugsnagUserEmailAddress account:kBugsnagUserKeychainAccount], email);
Expand Down Expand Up @@ -423,7 +423,10 @@ - (void)testUserNonPesistence {
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserUserId account:kBugsnagUserKeychainAccount]);

BugsnagConfiguration *config2 = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];
XCTAssertNil(config2.currentUser);
XCTAssertNotNil(config2.user);
XCTAssertNil(config2.user.userId);
XCTAssertNil(config2.user.name);
XCTAssertNil(config2.user.emailAddress);
}

/**
Expand All @@ -443,16 +446,16 @@ - (void)testPartialPesistence {
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserName account:kBugsnagUserKeychainAccount]);
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserUserId account:kBugsnagUserKeychainAccount]);

[config setUser:userId withName:nil andEmail:nil];
[config setUser:userId withEmail:nil andName:nil];
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserEmailAddress account:kBugsnagUserKeychainAccount]);
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserName account:kBugsnagUserKeychainAccount]);
// XCTAssertEqualObjects([bsg_SSKeychain passwordForService:kBugsnagUserUserId account:kBugsnagUserKeychainAccount], userId);
[config setUser:nil withName:name andEmail:nil];
[config setUser:nil withEmail:email andName:nil];
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserEmailAddress account:kBugsnagUserKeychainAccount]);
// XCTAssertEqualObjects([bsg_SSKeychain passwordForService:kBugsnagUserName account:kBugsnagUserKeychainAccount], name);
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserUserId account:kBugsnagUserKeychainAccount]);

[config setUser:nil withName:nil andEmail:email];
[config setUser:nil withEmail:nil andName:name];
// XCTAssertEqualObjects([bsg_SSKeychain passwordForService:kBugsnagUserEmailAddress account:kBugsnagUserKeychainAccount], email);
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserName account:kBugsnagUserKeychainAccount]);
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserUserId account:kBugsnagUserKeychainAccount]);
Expand All @@ -466,10 +469,10 @@ - (void)testAllUserDataNilPersistence {
XCTAssertTrue(config.persistUser);
[config deletePersistedUserData];

[config setUser:nil withName:nil andEmail:nil];
[config setUser:nil withEmail:nil andName:nil];

// currentUser should have been set
XCTAssertNotNil(config.currentUser);
XCTAssertNotNil(config.user);

// But there hould be no persisted data
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserEmailAddress account:kBugsnagUserKeychainAccount]);
Expand All @@ -495,7 +498,7 @@ - (void)testUserPersistenceAndMetadata {
// XCTAssertNil([bsg_SSKeychain passwordForService:kBugsnagUserUserId account:kBugsnagUserKeychainAccount]);

// Persist user data
[config setUser:userId withName:name andEmail:email];
[config setUser:userId withEmail:email andName:name];

// Check that retrieving persisted user data also sets configuration metadata
// Check persistence between invocations (when values have been set)
Expand Down Expand Up @@ -528,7 +531,7 @@ - (void)testNonPersistenceAndMetadata {
// XCTAssertNil([[config2 metadata] getMetadata:BSGKeyUser key:BSGKeyName]);
// XCTAssertNil([[config2 metadata] getMetadata:BSGKeyUser key:BSGKeyEmail]);

[config2 setUser:userId withName:name andEmail:email];
[config2 setUser:userId withEmail:email andName:name];
// XCTAssertEqualObjects([config2.metadata getMetadata:BSGKeyUser key:BSGKeyEmail], email);
// XCTAssertEqualObjects([config2.metadata getMetadata:BSGKeyUser key:BSGKeyName], name);
// XCTAssertEqualObjects([config2.metadata getMetadata:BSGKeyUser key:BSGKeyId], userId);
Expand Down Expand Up @@ -604,11 +607,11 @@ - (void)testErrorApiHeaders {
- (void)testUser {
BugsnagConfiguration *config = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1];

[config setUser:@"123" withName:@"foo" andEmail:@"[email protected]"];
[config setUser:@"123" withEmail:@"[email protected]" andName:@"foo"];

XCTAssertEqualObjects(@"123", config.currentUser.userId);
XCTAssertEqualObjects(@"foo", config.currentUser.name);
XCTAssertEqualObjects(@"[email protected]", config.currentUser.emailAddress);
XCTAssertEqualObjects(@"123", config.user.userId);
XCTAssertEqualObjects(@"foo", config.user.name);
XCTAssertEqualObjects(@"[email protected]", config.user.emailAddress);
}

- (void)testApiKeySetter {
Expand Down
4 changes: 2 additions & 2 deletions Tests/BugsnagSessionTrackerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ - (void)testStartNewSession {
}

- (void)testStartNewSessionWithUser {
[self.configuration setUser:@"123" withName:@"Bill" andEmail:nil];
[self.configuration setUser:@"123" withEmail:nil andName:@"Bill"];
XCTAssertNil(self.sessionTracker.runningSession);
[self.sessionTracker startNewSession];
BugsnagSession *session = self.sessionTracker.runningSession;
Expand Down Expand Up @@ -74,7 +74,7 @@ - (void)testStartNewAutoCapturedSession {
}

- (void)testStartNewAutoCapturedSessionWithUser {
[self.configuration setUser:@"123" withName:@"Bill" andEmail:@"[email protected]"];
[self.configuration setUser:@"123" withEmail:@"[email protected]" andName:@"Bill"];
XCTAssertNil(self.sessionTracker.runningSession);
[self.sessionTracker startNewSessionIfAutoCaptureEnabled];
BugsnagSession *session = self.sessionTracker.runningSession;
Expand Down
7 changes: 5 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ The exact error is available using the `BSGConfigurationErrorDomain` and
- config.reportOOMs
+ config.enabledErrorTypes

- config.currentUser
+ config.user

- config.notifierType
+ config.appType
```
Expand Down Expand Up @@ -112,7 +115,7 @@ Bugsnag.getMetadata("section" key:"key")
ObjC:
- [Bugsnag configuration]
+ [Bugsnag setUser:withName:andEmail:]
+ [Bugsnag setUser:withEmail:andName:]
- [Bugsnag addAttribute:WithValuetoTabWithName:]
+ [Bugsnag addMetadataToSection:key:value:]
Expand All @@ -132,7 +135,7 @@ ObjC:
Swift:
- Bugsnag.configuration()
+ Bugsnag.setUser(_:name:email:)
+ Bugsnag.setUser(_:email:name:)
- Bugsnag.addAttribute(attributeName:withValue:toTabWithName:)
+ Bugsnag.addMetadata(_:key:value:)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@implementation AutoSessionWithUserScenario

- (void)startBugsnag {
[self.config setUser:@"123" withName:@"Joe Bloggs" andEmail:@"[email protected]"];
[self.config setUser:@"123" withEmail:@"[email protected]" andName:@"Joe Bloggs"];
[super startBugsnag];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ - (void)startBugsnag {
self.config.autoTrackSessions = NO;
self.config.persistUser = NO;
[self.config deletePersistedUserData];
[self.config setCurrentUser:nil];
[self.config setUser:nil withEmail:nil andName:nil];
[super startBugsnag];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@implementation ManualSessionWithUserScenario

- (void)startBugsnag {
[self.config setUser:@"123" withName:@"Joe Bloggs" andEmail:@"[email protected]"];
[self.config setUser:@"123" withEmail:@"[email protected]" andName:@"Joe Bloggs"];
self.config.autoTrackSessions = NO;
[super startBugsnag];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class UserDisabledScenario: Scenario {
}

override func run() {
Bugsnag.setUser(nil, withName: nil, andEmail: nil)
Bugsnag.setUser(nil, withEmail: nil, andName: nil)
let error = NSError(domain: "UserDisabledScenario", code: 100, userInfo: nil)
Bugsnag.notifyError(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class UserEmailScenario: Scenario {
}

override func run() {
Bugsnag.setUser(nil, withName: nil, andEmail: "[email protected]")
Bugsnag.setUser(nil, withEmail: "[email protected]", andName: nil)
let error = NSError(domain: "UserEmailScenario", code: 100, userInfo: nil)
Bugsnag.notifyError(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class UserEnabledScenario: Scenario {
}

override func run() {
Bugsnag.setUser("123", withName: "Joe Bloggs", andEmail: "[email protected]")
Bugsnag.setUser("123", withEmail: "[email protected]", andName: "Joe Bloggs")
let error = NSError(domain: "UserEnabledScenario", code: 100, userInfo: nil)
Bugsnag.notifyError(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class UserIdScenario: Scenario {
}

override func run() {
Bugsnag.setUser("abc", withName: nil, andEmail: nil)
Bugsnag.setUser("abc", withEmail: nil, andName: nil)
let error = NSError(domain: "UserIdScenario", code: 100, userInfo: nil)
Bugsnag.notifyError(error)
}
Expand Down
Loading

0 comments on commit e9a9c92

Please sign in to comment.