Skip to content

Commit b9414a3

Browse files
Merge pull request #28 from Iterable/feature/ITBL-6230-call-createUserForUserId
Feature/itbl 6229 call create user for user
2 parents 973785a + e27975d commit b9414a3

File tree

4 files changed

+77
-13
lines changed

4 files changed

+77
-13
lines changed

Tests/swift-sdk-swift-tests/IterableAutoRegistrationTests.swift

+51-11
Original file line numberDiff line numberDiff line change
@@ -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() {

swift-sdk/ITBConsts.swift

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public extension String {
2626
public static let ITBL_PATH_TRACK_INAPP_CLICK = "events/trackInAppClick"
2727
public static let ITBL_PATH_TRACK_INAPP_OPEN = "events/trackInAppOpen"
2828
public static let ITBL_PATH_TRACK_PUSH_OPEN = "events/trackPushOpen"
29+
public static let ITBL_PATH_CREATE_USER = "users/createUserForUserId"
2930
public static let ITBL_PATH_UPDATE_USER = "users/update"
3031
public static let ITBL_PATH_UPDATE_EMAIL = "users/updateEmail"
3132
public static let ITBL_PATH_UPDATE_SUBSCRIPTIONS = "users/updateSubscriptions"

swift-sdk/Internal/IterableAPIInternal.swift

+25-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,22 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
110110
return
111111
}
112112

113-
register(token: token, appName: appName, pushServicePlatform: config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
113+
if let userId = userId {
114+
createUser(
115+
withUserId: userId,
116+
onSuccess: { (_) in
117+
self.register(token: token, appName: appName, pushServicePlatform: self.config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
118+
},
119+
onFailure: {(errorMessage, _) in
120+
if let errorMessage = errorMessage {
121+
ITBError("Could not create user: \(errorMessage)")
122+
} else {
123+
ITBError()
124+
}
125+
})
126+
} else {
127+
register(token: token, appName: appName, pushServicePlatform: config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
128+
}
114129
}
115130

116131
private func register(token: Data, appName: String, pushServicePlatform: PushServicePlatform, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) {
@@ -221,6 +236,15 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
221236
}
222237
}
223238

239+
private func createUser(withUserId userId: String, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) {
240+
var args = [AnyHashable : Any]()
241+
args[.ITBL_KEY_USER_ID] = userId
242+
243+
if let request = createPostRequest(forPath: .ITBL_PATH_CREATE_USER, withBody: args) {
244+
sendRequest(request, onSuccess: onSuccess, onFailure: onFailure)
245+
}
246+
}
247+
224248
func trackPurchase(_ total: NSNumber, items: [CommerceItem]) {
225249
trackPurchase(total, items: items, dataFields: nil)
226250
}

swift-sdk/IterableConstants.h

-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ typedef void (^OnSuccessHandler)(NSDictionary *data);
2222
The prototype for the completion handler block that gets called when an Iterable call fails
2323
*/
2424
typedef void (^OnFailureHandler)(NSString *reason, NSData *_Nullable data);
25-

0 commit comments

Comments
 (0)