@@ -89,7 +89,41 @@ 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( ) {
126+ let expectation0 = expectation ( description: " Call create user " )
93127 let expectation1 = expectation ( description: " Call registerToken API endpoint " )
94128 let expectation2 = expectation ( description: " Disable API endpoint called " )
95129 let expectation3 = expectation ( description: " do not call registerForRemoteNotifications " )
@@ -107,23 +141,29 @@ class IterableAutoRegistrationTests: XCTestCase {
107141 IterableAPI . userId = " userId1 "
108142 let token = " zeeToken " . data ( using: . utf8) !
109143 networkSession. callback = { ( _, _, _) in
110- // first call back will be called on register
111- expectation1. fulfill ( )
112- 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) ] )
144+ expectation0. fulfill ( )
145+ 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) ] )
146+ let body = networkSession. getRequestBody ( ) as! [ String : Any ]
147+ TestUtils . validateElementPresent ( withName: AnyHashable . ITBL_KEY_USER_ID, andValue: " userId1 " , inDictionary: body)
113148 networkSession. callback = { ( _, _, _) in
114- // second call back is for disable when we set new user id
115- 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) ] )
116- let body = networkSession. getRequestBody ( ) as! [ String : Any ]
117- TestUtils . validateElementPresent ( withName: AnyHashable . ITBL_KEY_TOKEN, andValue: ( token as NSData ) . iteHexadecimalString ( ) , inDictionary: body)
118- TestUtils . validateElementPresent ( withName: AnyHashable . ITBL_KEY_USER_ID, andValue: " userId1 " , inDictionary: body)
119- expectation2. fulfill ( )
149+ // second call back will be called on register
150+ expectation1. fulfill ( )
151+ 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) ] )
152+ networkSession. callback = { ( _, _, _) in
153+ // third call back is for disable when we set new user id
154+ 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) ] )
155+ let body = networkSession. getRequestBody ( ) as! [ String : Any ]
156+ TestUtils . validateElementPresent ( withName: AnyHashable . ITBL_KEY_TOKEN, andValue: ( token as NSData ) . iteHexadecimalString ( ) , inDictionary: body)
157+ TestUtils . validateElementPresent ( withName: AnyHashable . ITBL_KEY_USER_ID, andValue: " userId1 " , inDictionary: body)
158+ expectation2. fulfill ( )
159+ }
160+ IterableAPI . userId = " userId2 "
120161 }
121- IterableAPI . userId = " userId2 "
122162 }
123163 IterableAPI . register ( token: token)
124164
125165 // only wait for small time, supposed to error out
126- wait ( for: [ expectation1, expectation2, expectation3] , timeout: 1.0 )
166+ wait ( for: [ expectation0 , expectation1, expectation2, expectation3] , timeout: 1.0 )
127167 }
128168
129169 func testDoNotCallDisableOrEnableWhenAutoPushIsOff( ) {
0 commit comments