@@ -89,6 +89,39 @@ class IterableAutoRegistrationTests: XCTestCase {
8989 wait ( for: [ expectation1] , timeout: testExpectationTimeout)
9090 }
9191
92+ func testDoNotCallRegisterIfCreateUserFails( ) {
93+ let expectation0 = expectation ( description: " Call create user " )
94+ let expectation1 = expectation ( description: " Call registerToken API endpoint " )
95+ expectation1. isInverted = true
96+
97+ let networkSession = MockNetworkSession ( statusCode: 501 )
98+ let config = IterableConfig ( )
99+ config. pushIntegrationName = " my-push-integration "
100+ let notificationStateProvider = MockNotificationStateProvider ( enabled: true )
101+
102+ IterableAPI . initialize ( apiKey: IterableAutoRegistrationTests . apiKey,
103+ config: config,
104+ networkSession: networkSession,
105+ notificationStateProvider: notificationStateProvider)
106+ IterableAPI . userId = " userId1 "
107+ let token = " zeeToken " . data ( using: . utf8) !
108+ networkSession. callback = { ( _, _, _) in
109+ expectation0. fulfill ( )
110+ TestUtils . validate ( request: networkSession. request!, requestType: . post, apiEndPoint: . ITBL_ENDPOINT_API, path: . ITBL_PATH_CREATE_USER, queryParams: [ ( name: AnyHashable . ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests . apiKey) ] )
111+ let body = networkSession. getRequestBody ( ) as! [ String : Any ]
112+ TestUtils . validateElementPresent ( withName: AnyHashable . ITBL_KEY_USER_ID, andValue: " userId1 " , inDictionary: body)
113+ networkSession. callback = { ( _, _, _) in
114+ // first call back will be called on register
115+ expectation1. fulfill ( )
116+ TestUtils . validate ( request: networkSession. request!, requestType: . post, apiEndPoint: . ITBL_ENDPOINT_API, path: . ITBL_PATH_REGISTER_DEVICE_TOKEN, queryParams: [ ( name: AnyHashable . ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests . apiKey) ] )
117+ }
118+ }
119+ IterableAPI . register ( token: token)
120+
121+ // only wait for small time, supposed to error out
122+ wait ( for: [ expectation0, expectation1] , timeout: 1.0 )
123+ }
124+
92125 func testDoNotCallRegisterForRemoteNotificationsWhenNotificationsAreDisabled( ) {
93126 let expectation0 = expectation ( description: " Call create user " )
94127 let expectation1 = expectation ( description: " Call registerToken API endpoint " )
@@ -113,11 +146,11 @@ class IterableAutoRegistrationTests: XCTestCase {
113146 let body = networkSession. getRequestBody ( ) as! [ String : Any ]
114147 TestUtils . validateElementPresent ( withName: AnyHashable . ITBL_KEY_USER_ID, andValue: " userId1 " , inDictionary: body)
115148 networkSession. callback = { ( _, _, _) in
116- // first call back will be called on register
149+ // second call back will be called on register
117150 expectation1. fulfill ( )
118151 TestUtils . validate ( request: networkSession. request!, requestType: . post, apiEndPoint: . ITBL_ENDPOINT_API, path: . ITBL_PATH_REGISTER_DEVICE_TOKEN, queryParams: [ ( name: AnyHashable . ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests . apiKey) ] )
119152 networkSession. callback = { ( _, _, _) in
120- // second call back is for disable when we set new user id
153+ // third call back is for disable when we set new user id
121154 TestUtils . validate ( request: networkSession. request!, requestType: . post, apiEndPoint: . ITBL_ENDPOINT_API, path: . ITBL_PATH_DISABLE_DEVICE, queryParams: [ ( name: AnyHashable . ITBL_KEY_API_KEY, value: IterableAutoRegistrationTests . apiKey) ] )
122155 let body = networkSession. getRequestBody ( ) as! [ String : Any ]
123156 TestUtils . validateElementPresent ( withName: AnyHashable . ITBL_KEY_TOKEN, andValue: ( token as NSData ) . iteHexadecimalString ( ) , inDictionary: body)
0 commit comments