Skip to content

Commit 6e90149

Browse files
Merge pull request #32 from Iterable/bugfix/mob-33
[MOB-33] - No need to call createUserForUserId anymore
2 parents 19dd619 + 2f22c4a commit 6e90149

File tree

3 files changed

+19
-78
lines changed

3 files changed

+19
-78
lines changed

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

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -89,41 +89,7 @@ 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-
12592
func testDoNotCallRegisterForRemoteNotificationsWhenNotificationsAreDisabled() {
126-
let expectation0 = expectation(description: "Call create user")
12793
let expectation1 = expectation(description: "Call registerToken API endpoint")
12894
let expectation2 = expectation(description: "Disable API endpoint called")
12995
let expectation3 = expectation(description: "do not call registerForRemoteNotifications")
@@ -141,31 +107,26 @@ class IterableAutoRegistrationTests: XCTestCase {
141107
IterableAPI.userId = "userId1"
142108
let token = "zeeToken".data(using: .utf8)!
143109
networkSession.callback = {(_, _, _) in
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)
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)])
113+
TestUtils.validateMatch(keyPath: KeyPath(AnyHashable.ITBL_KEY_PREFER_USER_ID), value: true, inDictionary: networkSession.getRequestBody() as! [String : Any], message: "Expected to find preferUserId set to true")
148114
networkSession.callback = {(_, _, _) in
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"
115+
// second call back is for disable when we set new user id
116+
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)])
117+
let body = networkSession.getRequestBody() as! [String : Any]
118+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_TOKEN, andValue: (token as NSData).iteHexadecimalString(), inDictionary: body)
119+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_USER_ID, andValue: "userId1", inDictionary: body)
120+
expectation2.fulfill()
161121
}
122+
IterableAPI.userId = "userId2"
162123
}
163124
IterableAPI.register(token: token)
164125

165126
// only wait for small time, supposed to error out
166-
wait(for: [expectation0, expectation1, expectation2, expectation3], timeout: 1.0)
127+
wait(for: [expectation1, expectation2, expectation3], timeout: 1.0)
167128
}
168-
129+
169130
func testDoNotCallDisableOrEnableWhenAutoPushIsOff() {
170131
let expectation1 = expectation(description: "do not call register for remote")
171132
expectation1.isInverted = true

swift-sdk/ITBConsts.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ 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"
3029
public static let ITBL_PATH_UPDATE_USER = "users/update"
3130
public static let ITBL_PATH_UPDATE_EMAIL = "users/updateEmail"
3231
public static let ITBL_PATH_UPDATE_SUBSCRIPTIONS = "users/updateSubscriptions"
@@ -60,6 +59,7 @@ public extension AnyHashable {
6059
public static let ITBL_KEY_USER_ID = "userId"
6160
public static let ITBL_KEY_ACTION_IDENTIFIER = "actionIdentifier"
6261
public static let ITBL_KEY_USER_TEXT = "userText"
62+
public static let ITBL_KEY_PREFER_USER_ID = "preferUserId"
6363
}
6464

6565
// More Keys

swift-sdk/Internal/IterableAPIInternal.swift

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,7 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
110110
return
111111
}
112112

113-
if let userId = userId {
114-
// if we are using userId, then create a user first, then register
115-
createUser(withUserId: userId).flatMap({ (_) in
116-
return self.register(token: token, appName: appName, pushServicePlatform: self.config.pushPlatform)
117-
}).onSuccess { (json) in
118-
onSuccess?(json)
119-
}.onError { (error) in
120-
if let sendError = error as? SendRequestError {
121-
onFailure?(sendError.reason, sendError.data)
122-
} else {
123-
onFailure?("failed to create user", nil)
124-
}
125-
}
126-
} else {
127-
register(token: token, appName: appName, pushServicePlatform: config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
128-
}
113+
register(token: token, appName: appName, pushServicePlatform: config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
129114
}
130115

131116
@discardableResult private func register(token: Data, appName: String, pushServicePlatform: PushServicePlatform, onSuccess: OnSuccessHandler? = nil, onFailure: OnFailureHandler? = nil) -> Future<SendRequestValue> {
@@ -175,6 +160,10 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
175160
args[.ITBL_KEY_DEVICE] = deviceDictionary
176161
addEmailOrUserId(args: &args)
177162

163+
if email == nil && userId != nil {
164+
args[.ITBL_KEY_PREFER_USER_ID] = true
165+
}
166+
178167
ITBInfo("sending registerToken request with args \(args)")
179168
return
180169
createPostRequest(forPath: .ITBL_PATH_REGISTER_DEVICE_TOKEN, withBody: args)
@@ -236,15 +225,6 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
236225
}
237226
}
238227

239-
private func createUser(withUserId userId: String) -> Future<SendRequestValue> {
240-
var args = [AnyHashable : Any]()
241-
args[.ITBL_KEY_USER_ID] = userId
242-
243-
return createPostRequest(forPath: .ITBL_PATH_CREATE_USER, withBody: args).map {
244-
sendRequest($0)
245-
} ?? SendRequestError.createErroredFuture(reason: "Could not create createUser Reqeust")
246-
}
247-
248228
func trackPurchase(_ total: NSNumber, items: [CommerceItem]) {
249229
trackPurchase(total, items: items, dataFields: nil)
250230
}

0 commit comments

Comments
 (0)