77//
88
99#import " BNCTestCase.h"
10- #import " BranchSetIdentityRequest.h"
1110#import " BranchConstants.h"
1211#import " BNCPreferenceHelper.h"
12+ #import " Branch.h"
1313#import < OCMock/OCMock.h>
1414
1515static NSString * const IDENTITY_TEST_USER_ID = @" foo_id" ;
@@ -19,170 +19,11 @@ @interface BranchSetIdentityRequestTests : BNCTestCase
1919
2020@implementation BranchSetIdentityRequestTests
2121
22- - (void )testRequestBody {
23- BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance ];
24- NSDictionary * const expectedParams = @{
25- BRANCH_REQUEST_KEY_DEVELOPER_IDENTITY: IDENTITY_TEST_USER_ID,
26- BRANCH_REQUEST_KEY_RANDOMIZED_BUNDLE_TOKEN: preferenceHelper.randomizedBundleToken ,
27- BRANCH_REQUEST_KEY_RANDOMIZED_DEVICE_TOKEN: preferenceHelper.randomizedDeviceToken ,
28- BRANCH_REQUEST_KEY_SESSION_ID: preferenceHelper.sessionID
29- };
30-
31- BranchSetIdentityRequest *request = [[BranchSetIdentityRequest alloc ] initWithUserId: IDENTITY_TEST_USER_ID callback: NULL ];
32- id serverInterfaceMock = OCMClassMock ([BNCServerInterface class ]);
33- [[serverInterfaceMock expect ] postRequest: expectedParams url: [self stringMatchingPattern: BRANCH_REQUEST_ENDPOINT_SET_IDENTITY] key: [OCMArg any ] callback: [OCMArg any ]];
34-
35- [request makeRequest: serverInterfaceMock key: nil callback: NULL ];
36-
37- [serverInterfaceMock verify ];
38- }
39-
40- - (void )testBasicSuccess {
41- NSString * const PRE_RESPONSE_USER_IDENTITY = @" foo" ;
42- NSString * const PRE_RESPONSE_IDENTITY = @" foo" ;
43- NSString * const PRE_RESPONSE_USER_URL = @" http://foo" ;
44- NSString * const PRE_RESPONSE_INSTALL_PARAMS = @" {\" foo\" :\" bar\" }" ;
45- NSString * const RESPONSE_IDENTITY = @" bar" ;
46- NSString * const RESPONSE_USER_URL = @" http://bar" ;
47- NSString * const RESPONSE_INSTALL_PARAMS = @" {\" bar\" :\" foo\" }" ;
48- NSDictionary * const RESPONSE_INSTALL_PARAMS_DICT = @{ @" bar" : @" foo" };
49- __block NSInteger callbackCount = 0 ;
50-
51-
52- BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance ];
53- preferenceHelper.userIdentity = PRE_RESPONSE_USER_IDENTITY;
54- preferenceHelper.randomizedBundleToken = PRE_RESPONSE_IDENTITY;
55- preferenceHelper.userUrl = PRE_RESPONSE_USER_URL;
56- preferenceHelper.installParams = PRE_RESPONSE_INSTALL_PARAMS;
57-
58- BNCServerResponse * const goodResponse = [[BNCServerResponse alloc ] init ];
59- goodResponse.data = @{
60- BRANCH_RESPONSE_KEY_RANDOMIZED_BUNDLE_TOKEN: RESPONSE_IDENTITY,
61- BRANCH_RESPONSE_KEY_USER_URL: RESPONSE_USER_URL,
62- BRANCH_RESPONSE_KEY_INSTALL_PARAMS: RESPONSE_INSTALL_PARAMS
63- };
64-
65- BranchSetIdentityRequest *request = [[BranchSetIdentityRequest alloc ] initWithUserId: IDENTITY_TEST_USER_ID callback: ^(NSDictionary *params, NSError *error) {
66- callbackCount++;
67- XCTAssertEqualObjects (params, RESPONSE_INSTALL_PARAMS_DICT);
68- XCTAssertNil (error);
69- }];
70-
71- [request processResponse: goodResponse error: nil ];
72-
73- XCTAssertEqual (callbackCount, 1 );
74- XCTAssertEqualObjects (preferenceHelper.userIdentity , IDENTITY_TEST_USER_ID);
75- XCTAssertEqualObjects (preferenceHelper.randomizedBundleToken , RESPONSE_IDENTITY);
76- XCTAssertEqualObjects (preferenceHelper.userUrl , RESPONSE_USER_URL);
77- XCTAssertEqualObjects (preferenceHelper.installParams , RESPONSE_INSTALL_PARAMS);
78- }
79-
80- - (void )testBasicErrorHandling {
81- NSError * const TEST_ERROR = [NSError errorWithDomain: @" foo" code: 1 userInfo: nil ];
82- __block NSInteger callbackCount = 0 ;
83-
84- BranchSetIdentityRequest *request =
85- [[BranchSetIdentityRequest alloc ]
86- initWithUserId: IDENTITY_TEST_USER_ID
87- callback: ^(NSDictionary *params, NSError *error) {
88- callbackCount++;
89- XCTAssert (params != nil && params.count == 0 );
90- XCTAssertEqual (error, TEST_ERROR);
91- }];
92-
93- [request processResponse: nil error: TEST_ERROR];
94- XCTAssertEqual (callbackCount, 1 );
95- }
96-
97- - (void )testMultipleErrors {
98- NSError * const TEST_ERROR = [NSError errorWithDomain: @" foo" code: 1 userInfo: nil ];
99- __block NSInteger callbackCount = 0 ;
100-
101- BranchSetIdentityRequest *request =
102- [[BranchSetIdentityRequest alloc ]
103- initWithUserId: IDENTITY_TEST_USER_ID
104- callback: ^(NSDictionary *params, NSError *error) {
105- callbackCount++;
106- XCTAssert (params != nil && params.count == 0 );
107- XCTAssertEqual (error, TEST_ERROR);
108- }];
109-
110- [request processResponse: nil error: TEST_ERROR];
111- [request processResponse: nil error: TEST_ERROR];
112-
113- XCTAssertEqual (callbackCount, 1 );
114- }
115-
116- - (void )testErrorFollowedBySuccess {
117- NSError * const TEST_ERROR = [NSError errorWithDomain: @" foo" code: 1 userInfo: nil ];
118- NSString * const PRE_RESPONSE_USER_IDENTITY = @" foo" ;
119- NSString * const PRE_RESPONSE_IDENTITY = @" foo" ;
120- NSString * const PRE_RESPONSE_USER_URL = @" http://foo" ;
121- NSString * const PRE_RESPONSE_INSTALL_PARAMS = @" {\" foo\" :\" bar\" }" ;
122- NSString * const RESPONSE_IDENTITY = @" bar" ;
123- NSString * const RESPONSE_USER_URL = @" http://bar" ;
124- NSString * const RESPONSE_INSTALL_PARAMS = @" {\" bar\" :\" foo\" }" ;
125- __block NSInteger callbackCount = 0 ;
126-
127- BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance ];
128- preferenceHelper.userIdentity = PRE_RESPONSE_USER_IDENTITY;
129- preferenceHelper.randomizedBundleToken = PRE_RESPONSE_IDENTITY;
130- preferenceHelper.userUrl = PRE_RESPONSE_USER_URL;
131- preferenceHelper.installParams = PRE_RESPONSE_INSTALL_PARAMS;
132-
133- BNCServerResponse * const goodResponse = [[BNCServerResponse alloc ] init ];
134- goodResponse.data = @{
135- BRANCH_RESPONSE_KEY_RANDOMIZED_BUNDLE_TOKEN: RESPONSE_IDENTITY,
136- BRANCH_RESPONSE_KEY_USER_URL: RESPONSE_USER_URL,
137- BRANCH_RESPONSE_KEY_INSTALL_PARAMS: RESPONSE_INSTALL_PARAMS
138- };
139-
140- BranchSetIdentityRequest *request =
141- [[BranchSetIdentityRequest alloc ]
142- initWithUserId: IDENTITY_TEST_USER_ID
143- callback: ^(NSDictionary *params, NSError *error) {
144- callbackCount++;
145- XCTAssert (params != nil && params.count == 0 );
146- XCTAssertEqual (error, TEST_ERROR);
147- }];
148-
149- [request processResponse: nil error: TEST_ERROR];
150- [request processResponse: goodResponse error: nil ];
151-
152- XCTAssertEqual (callbackCount, 1 ); // callback should have only been called once, but preferences should be updated
153- XCTAssertEqualObjects (preferenceHelper.userIdentity , IDENTITY_TEST_USER_ID);
154- XCTAssertEqualObjects (preferenceHelper.randomizedBundleToken , RESPONSE_IDENTITY);
155- XCTAssertEqualObjects (preferenceHelper.userUrl , RESPONSE_USER_URL);
156- XCTAssertEqualObjects (preferenceHelper.installParams , RESPONSE_INSTALL_PARAMS);
157- }
158-
159-
160- - (void )testEmptyResponseFieldsAfterSetIdentity {
161-
162- BNCServerResponse *response = [[BNCServerResponse alloc ] init ];
163- BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance ];
164-
165- __block NSInteger callbackCount = 0 ;
166-
167- BranchSetIdentityRequest *request = [[BranchSetIdentityRequest alloc ] initWithUserId: IDENTITY_TEST_USER_ID callback: ^(NSDictionary *params, NSError *error) {
168- callbackCount++;
169- XCTAssertNil (error);
170- }];
171-
172- response.data = @{};
173- [request processResponse: response error: nil ];
174-
175- XCTAssertNotNil (preferenceHelper.randomizedDeviceToken );
176- XCTAssertNotNil (preferenceHelper.userUrl );
177- XCTAssertNotNil (preferenceHelper.sessionID );
178- XCTAssertNotNil (preferenceHelper.randomizedBundleToken );
179- }
180-
18122#pragma mark - setIdentity Tests
18223- (void )testSetIdentityWithCallback {
18324 Branch *branch = [Branch getInstance ];
18425 [branch logoutWithCallback: ^(BOOL changed, NSError * _Nullable error) {
185- BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper new ];
26+ BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance ];
18627
18728 XCTestExpectation *expectation = [self expectationWithDescription: @" setIdentity callback is called" ];
18829
@@ -198,7 +39,7 @@ - (void)testSetIdentityWithCallback {
19839- (void )testSetIdentityWithNilUserId {
19940 Branch *branch = [Branch getInstance ];
20041 [branch logoutWithCallback: ^(BOOL changed, NSError * _Nullable error) {
201- BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper new ];
42+ BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance ];
20243
20344 XCTestExpectation *expectation = [self expectationWithDescription: @" setIdentityWithNil callback is called" ];
20445
@@ -216,7 +57,7 @@ - (void)testSetIdentityWithNilUserId {
21657- (void )testSetIdentityWithUserId {
21758 Branch *branch = [Branch getInstance ];
21859 [branch logoutWithCallback: ^(BOOL changed, NSError * _Nullable error) {
219- BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper new ];
60+ BNCPreferenceHelper *preferenceHelper = [BNCPreferenceHelper sharedInstance ];
22061
22162 NSString *testUserId = @" testUserId" ;
22263 [branch setIdentity: testUserId withCallback: nil ];
0 commit comments