Skip to content
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
47 changes: 44 additions & 3 deletions UnitTests/MPRoktTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

@interface MPRokt ()
- (NSArray<NSDictionary<NSString *, NSString *> *> *)getRoktPlacementAttributesMapping;
- (NSNumber *)getRoktHashedEmailUserIdentityType;
- (void)confirmUser:(NSDictionary<NSString *, NSString *> *)attributes user:(MParticleUser * _Nullable)user completion:(void (^)(MParticleUser *_Nullable))completion;
@end

Expand Down Expand Up @@ -398,16 +399,56 @@ - (void)testTriggeredIdentifyWithMismatchedEmailIdentity {
}

- (void)testTriggeredIdentifyWithMismatchedOtherIdentity {
[self hashedIdentityTest: MPIdentityOther];
}

- (void)testTriggeredIdentifyWithMismatchedOther2Identity {
[self hashedIdentityTest: MPIdentityOther2];
}

- (void)testTriggeredIdentifyWithMismatchedOther3Identity {
[self hashedIdentityTest: MPIdentityOther3];
}

- (void)testTriggeredIdentifyWithMismatchedOther4Identity {
[self hashedIdentityTest: MPIdentityOther4];
}

- (void)testTriggeredIdentifyWithMismatchedOther5Identity {
[self hashedIdentityTest: MPIdentityOther5];
}

- (void)testTriggeredIdentifyWithMismatchedOther6Identity {
[self hashedIdentityTest: MPIdentityOther6];
}

- (void)testTriggeredIdentifyWithMismatchedOther7Identity {
[self hashedIdentityTest: MPIdentityOther7];
}
- (void)testTriggeredIdentifyWithMismatchedOther8Identity {
[self hashedIdentityTest: MPIdentityOther8];
}

- (void)testTriggeredIdentifyWithMismatchedOther9Identity {
[self hashedIdentityTest: MPIdentityOther9];
}

- (void)testTriggeredIdentifyWithMismatchedOther10Identity {
[self hashedIdentityTest: MPIdentityOther10];
}

- (void)hashedIdentityTest: (MPIdentity)mpIdentity {
MParticleUser *currentUser = [MParticle sharedInstance].identity.currentUser;

MPUserDefaults *userDefaults = [MPUserDefaults standardUserDefaultsWithStateMachine:[MParticle sharedInstance].stateMachine backendController:[MParticle sharedInstance].backendController identity:[MParticle sharedInstance].identity];

NSArray *userIdentityArray = @[@{@"n" : [NSNumber numberWithLong:MPUserIdentityOther], @"i" : @"[email protected]"}];
NSArray *userIdentityArray = @[@{@"n" : [NSNumber numberWithLong:mpIdentity], @"i" : @"[email protected]"}];

[userDefaults setMPObject:userIdentityArray forKey:kMPUserIdentityArrayKey userId:currentUser.userId];
XCTAssertEqualObjects(currentUser.identities[@(MPIdentityOther)], @"[email protected]");
XCTAssertEqualObjects(currentUser.identities[@(mpIdentity)], @"[email protected]");

//Mock Identity as needed
[[[self.mockRokt stub] andReturn:@(mpIdentity)] getRoktHashedEmailUserIdentityType];
MParticle *instance = [MParticle sharedInstance];
self.mockInstance = OCMPartialMock(instance);
self.identityMock = OCMClassMock([MPIdentityApi class]);
Expand All @@ -416,7 +457,7 @@ - (void)testTriggeredIdentifyWithMismatchedOtherIdentity {
[[[self.identityMock stub] andReturn:currentUser] currentUser];

[[self.identityMock expect] identify:[OCMArg checkWithBlock:^BOOL(MPIdentityApiRequest *request) {
XCTAssertEqualObjects([request.identities objectForKey:@(MPIdentityOther)], @"[email protected]");
XCTAssertEqualObjects([request.identities objectForKey:@(mpIdentity)], @"[email protected]");
return true;
}] completion:OCMOCK_ANY];

Expand Down
1 change: 1 addition & 0 deletions mParticle-Apple-SDK/MPIConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,5 @@ extern NSString * _Nonnull const kMPDeviceInvalidVendorId;

// MPRokt Constants
extern NSString * _Nonnull const kMPPlacementAttributesMapping;
extern NSString * _Nonnull const kMPHashedEmailUserIdentityType;
#endif
1 change: 1 addition & 0 deletions mParticle-Apple-SDK/MPIConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@

// MPRokt Constants
NSString * const kMPPlacementAttributesMapping = @"placementAttributesMapping";
NSString * const kMPHashedEmailUserIdentityType = @"hashedEmailUserIdentityType";

//
// Primitive data type constants
Expand Down
27 changes: 23 additions & 4 deletions mParticle-Apple-SDK/MPRokt.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "MPForwardQueueParameters.h"
#import "MPILogger.h"
#import "MPIConstants.h"
#import "MPIdentityDTO.h"

@interface MParticle ()

Expand Down Expand Up @@ -178,6 +179,23 @@ - (void)close {
return attributeMap;
}

- (NSNumber *)getRoktHashedEmailUserIdentityType {
// Get the kit configuration
NSArray<NSDictionary *> *kitConfigs = [MParticle sharedInstance].kitContainer_PRIVATE.originalConfig.copy;
NSDictionary *roktKitConfig;
for (NSDictionary *kitConfig in kitConfigs) {
if (kitConfig[@"id"] != nil && [kitConfig[@"id"] integerValue] == 181) {
roktKitConfig = kitConfig;
}
}

// Get the string representing which identity to use and convert it to the key (NSNumber)
NSString *hashedIdentityTypeString = roktKitConfig[kMPHashedEmailUserIdentityType];
NSNumber *hashedIdentityTypeNumber = [MPIdentityHTTPIdentities identityTypeForString:hashedIdentityTypeString.lowercaseString];

return hashedIdentityTypeNumber != nil ? hashedIdentityTypeNumber : @(MPIdentityOther);
}

- (NSDictionary<NSString *, NSString *> *)confirmSandboxAttribute:(NSDictionary<NSString *, NSString *> * _Nullable)attributes {
NSMutableDictionary<NSString *, NSString *> *finalAttributes = attributes.mutableCopy;
NSString *sandboxKey = @"sandbox";
Expand All @@ -200,18 +218,19 @@ - (void)close {
- (void)confirmUser:(NSDictionary<NSString *, NSString *> * _Nullable)attributes user:(MParticleUser * _Nullable)user completion:(void (^)(MParticleUser *_Nullable))completion {
NSString *email = attributes[@"email"];
NSString *hashedEmail = attributes[@"emailsha256"];
NSNumber *hashedEmailIdentity = [self getRoktHashedEmailUserIdentityType];

if ((email && ![email isEqualToString:user.identities[@(MPIdentityEmail)]]) || (hashedEmail && ![hashedEmail isEqualToString: user.identities[@(MPIdentityOther)]])) {
if ((email && ![email isEqualToString:user.identities[@(MPIdentityEmail)]]) || (hashedEmail && ![hashedEmail isEqualToString: user.identities[hashedEmailIdentity]])) {
// If there is an existing email or hashed email but it doesn't match the what was passed in, warn the customer
if (email && user.identities[@(MPIdentityEmail)]) {
NSLog(@"The existing email on the user (%@) does not match the email passed in to `selectPlacements:` (%@). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before contuing to `selectPlacements:`", user.identities[@(MPIdentityEmail)], email);
} else if (hashedEmail && user.identities[@(MPIdentityOther)]) {
NSLog(@"The existing hashed email on the user (%@) does not match the email passed in to `selectPlacements:` (%@). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before contuing to `selectPlacements:`", user.identities[@(MPIdentityOther)], hashedEmail);
} else if (hashedEmail && user.identities[hashedEmailIdentity]) {
NSLog(@"The existing hashed email on the user (%@) does not match the email passed in to `selectPlacements:` (%@). Please remember to sync the email identity to mParticle as soon as you receive it. We will now identify the user before contuing to `selectPlacements:`", user.identities[hashedEmailIdentity], hashedEmail);
}

MPIdentityApiRequest *identityRequest = [MPIdentityApiRequest requestWithUser:user];
[identityRequest setIdentity:email identityType:MPIdentityEmail];
[identityRequest setIdentity:hashedEmail identityType:MPIdentityOther];
[identityRequest setIdentity:hashedEmail identityType:hashedEmailIdentity.unsignedIntegerValue];

[[[MParticle sharedInstance] identity] identify:identityRequest completion:^(MPIdentityApiResult *_Nullable apiResult, NSError *_Nullable error) {
if (error) {
Expand Down
Loading