Skip to content

Commit

Permalink
Duplicate Events fix - Add request ID and creation timestamp (#1436)
Browse files Browse the repository at this point in the history
* 'branch_sdk_request_unique_id' and 'branch_sdk_request_timestamp' to post params of all the SDK requests.
* Added Unit Tests.
* Updated version to 3.6.4 .
  • Loading branch information
NidhiDixit09 authored Oct 4, 2024
1 parent 66dfe77 commit 146bf4e
Show file tree
Hide file tree
Showing 21 changed files with 325 additions and 41 deletions.
67 changes: 53 additions & 14 deletions Branch-TestBed/Branch-SDK-Tests/BNCRequestFactoryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,60 @@

#import <XCTest/XCTest.h>
#import "BNCRequestFactory.h"
#import "BranchConstants.h"
#import "BNCEncodingUtils.h"

@interface BNCRequestFactoryTests : XCTestCase

@property (nonatomic, copy, readwrite) NSString *requestUUID;
@property (nonatomic, copy, readwrite) NSNumber *requestCreationTimeStamp;
@end

@implementation BNCRequestFactoryTests

- (void)setUp {

_requestUUID = [[NSUUID UUID ] UUIDString];
_requestCreationTimeStamp = BNCWireFormatFromDate([NSDate date]);
}

- (void)tearDown {

}

- (void)testInitWithBranchKeyNil {
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:nil];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:nil UUID:_requestUUID TimeStamp:_requestCreationTimeStamp];
NSDictionary *json = [factory dataForInstallWithURLString:@"https://branch.io"];
XCTAssertNotNil(json);

// key is omitted when nil
XCTAssertNil([json objectForKey:@"branch_key"]);
XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testInitWithBranchKeyEmpty {
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@""];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForInstallWithURLString:@"https://branch.io"];
XCTAssertNotNil(json);

// empty string is allowed
XCTAssertTrue([@"" isEqualToString:[json objectForKey:@"branch_key"]]);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testInitWithBranchKey {
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForInstallWithURLString:@"https://branch.io"];
XCTAssertNotNil(json);
XCTAssertTrue([@"key_abcd" isEqualToString:[json objectForKey:@"branch_key"]]);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testDataForInstall {
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForInstallWithURLString:@"https://branch.io"];
XCTAssertNotNil(json);

Expand All @@ -60,10 +72,13 @@ - (void)testDataForInstall {

// not present on installs
XCTAssertNil([json objectForKey:@"randomized_bundle_token"]);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testDataForOpen {
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForOpenWithURLString:@"https://branch.io"];
XCTAssertNotNil(json);

Expand All @@ -75,12 +90,15 @@ - (void)testDataForOpen {
// Present only on opens. Assumes test runs after the host app completes an install.
// This is not a reliable assumption on test runners
//XCTAssertNotNil([json objectForKey:@"randomized_bundle_token"]);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testDataForEvent {
NSDictionary *event = @{@"name": @"ADD_TO_CART"};

BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForEventWithEventDictionary:[event mutableCopy]];
XCTAssertNotNil(json);

Expand All @@ -89,6 +107,9 @@ - (void)testDataForEvent {
NSDictionary *userData = [json objectForKey:@"user_data"];
XCTAssertNotNil(userData);
XCTAssertNotNil([userData objectForKey:@"idfv"]);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testDataForEventWithContentItem {
Expand All @@ -104,7 +125,7 @@ - (void)testDataForEventWithContentItem {
]
};

BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForEventWithEventDictionary:[event mutableCopy]];
XCTAssertNotNil(json);

Expand All @@ -117,6 +138,9 @@ - (void)testDataForEventWithContentItem {
NSDictionary *userData = [json objectForKey:@"user_data"];
XCTAssertNotNil(userData);
XCTAssertNotNil([userData objectForKey:@"idfv"]);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testDataForEventWithTwoContentItem {
Expand All @@ -138,7 +162,7 @@ - (void)testDataForEventWithTwoContentItem {
]
};

BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForEventWithEventDictionary:[event mutableCopy]];
XCTAssertNotNil(json);

Expand All @@ -151,12 +175,15 @@ - (void)testDataForEventWithTwoContentItem {
NSDictionary *userData = [json objectForKey:@"user_data"];
XCTAssertNotNil(userData);
XCTAssertNotNil([userData objectForKey:@"idfv"]);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testDataForEventEmpty {
NSDictionary *event = @{};

BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForEventWithEventDictionary:[event mutableCopy]];
XCTAssertNotNil(json);

Expand All @@ -165,10 +192,13 @@ - (void)testDataForEventEmpty {
NSDictionary *userData = [json objectForKey:@"user_data"];
XCTAssertNotNil(userData);
XCTAssertNotNil([userData objectForKey:@"idfv"]);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testDataForEventNil {
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForEventWithEventDictionary:nil];
XCTAssertNotNil(json);

Expand All @@ -177,19 +207,28 @@ - (void)testDataForEventNil {
NSDictionary *userData = [json objectForKey:@"user_data"];
XCTAssertNotNil(userData);
XCTAssertNotNil([userData objectForKey:@"idfv"]);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}


- (void)testDataForShortURL {
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForShortURLWithLinkDataDictionary:@{}.mutableCopy isSpotlightRequest:NO];
XCTAssertNotNil(json);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

- (void)testDataForLATD {
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd"];
BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:self.requestUUID TimeStamp:self.requestCreationTimeStamp];
NSDictionary *json = [factory dataForLATDWithDataDictionary:@{}.mutableCopy];
XCTAssertNotNil(json);

XCTAssertTrue(self.requestCreationTimeStamp == [json objectForKey:BRANCH_REQUEST_KEY_REQUEST_CREATION_TIME_STAMP]);
XCTAssertTrue([self.requestUUID isEqualToString:[json objectForKey:BRANCH_REQUEST_KEY_REQUEST_UUID]]);
}

@end
Loading

0 comments on commit 146bf4e

Please sign in to comment.