Skip to content

Commit

Permalink
Merge pull request #28 from Iterable/feature/ITBL-6230-call-createUse…
Browse files Browse the repository at this point in the history
…rForUserId

Feature/itbl 6229 call create user for user
  • Loading branch information
tapashmajumder committed Oct 26, 2018
2 parents 973785a + e27975d commit b9414a3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 13 deletions.
62 changes: 51 additions & 11 deletions Tests/swift-sdk-swift-tests/IterableAutoRegistrationTests.swift
Expand Up @@ -89,7 +89,41 @@ class IterableAutoRegistrationTests: XCTestCase {
wait(for: [expectation1], timeout: testExpectationTimeout)
}

func testDoNotCallRegisterIfCreateUserFails() {
let expectation0 = expectation(description: "Call create user")
let expectation1 = expectation(description: "Call registerToken API endpoint")
expectation1.isInverted = true

let networkSession = MockNetworkSession(statusCode: 501)
let config = IterableConfig()
config.pushIntegrationName = "my-push-integration"
let notificationStateProvider = MockNotificationStateProvider(enabled: true)

IterableAPI.initialize(apiKey: IterableAutoRegistrationTests.apiKey,
config: config,
networkSession: networkSession,
notificationStateProvider: notificationStateProvider)
IterableAPI.userId = "userId1"
let token = "zeeToken".data(using: .utf8)!
networkSession.callback = {(_, _, _) in
expectation0.fulfill()
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)])
let body = networkSession.getRequestBody() as! [String : Any]
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_USER_ID, andValue: "userId1", inDictionary: body)
networkSession.callback = {(_, _, _) in
// first call back will be called on register
expectation1.fulfill()
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)])
}
}
IterableAPI.register(token: token)

// only wait for small time, supposed to error out
wait(for: [expectation0, expectation1], timeout: 1.0)
}

func testDoNotCallRegisterForRemoteNotificationsWhenNotificationsAreDisabled() {
let expectation0 = expectation(description: "Call create user")
let expectation1 = expectation(description: "Call registerToken API endpoint")
let expectation2 = expectation(description: "Disable API endpoint called")
let expectation3 = expectation(description: "do not call registerForRemoteNotifications")
Expand All @@ -107,23 +141,29 @@ class IterableAutoRegistrationTests: XCTestCase {
IterableAPI.userId = "userId1"
let token = "zeeToken".data(using: .utf8)!
networkSession.callback = {(_, _, _) in
// first call back will be called on register
expectation1.fulfill()
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)])
expectation0.fulfill()
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)])
let body = networkSession.getRequestBody() as! [String : Any]
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_USER_ID, andValue: "userId1", inDictionary: body)
networkSession.callback = {(_, _, _) in
// second call back is for disable when we set new user id
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)])
let body = networkSession.getRequestBody() as! [String : Any]
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_TOKEN, andValue: (token as NSData).iteHexadecimalString(), inDictionary: body)
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_USER_ID, andValue: "userId1", inDictionary: body)
expectation2.fulfill()
// second call back will be called on register
expectation1.fulfill()
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)])
networkSession.callback = {(_, _, _) in
// third call back is for disable when we set new user id
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)])
let body = networkSession.getRequestBody() as! [String : Any]
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_TOKEN, andValue: (token as NSData).iteHexadecimalString(), inDictionary: body)
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_USER_ID, andValue: "userId1", inDictionary: body)
expectation2.fulfill()
}
IterableAPI.userId = "userId2"
}
IterableAPI.userId = "userId2"
}
IterableAPI.register(token: token)

// only wait for small time, supposed to error out
wait(for: [expectation1, expectation2, expectation3], timeout: 1.0)
wait(for: [expectation0, expectation1, expectation2, expectation3], timeout: 1.0)
}

func testDoNotCallDisableOrEnableWhenAutoPushIsOff() {
Expand Down
1 change: 1 addition & 0 deletions swift-sdk/ITBConsts.swift
Expand Up @@ -26,6 +26,7 @@ public extension String {
public static let ITBL_PATH_TRACK_INAPP_CLICK = "events/trackInAppClick"
public static let ITBL_PATH_TRACK_INAPP_OPEN = "events/trackInAppOpen"
public static let ITBL_PATH_TRACK_PUSH_OPEN = "events/trackPushOpen"
public static let ITBL_PATH_CREATE_USER = "users/createUserForUserId"
public static let ITBL_PATH_UPDATE_USER = "users/update"
public static let ITBL_PATH_UPDATE_EMAIL = "users/updateEmail"
public static let ITBL_PATH_UPDATE_SUBSCRIPTIONS = "users/updateSubscriptions"
Expand Down
26 changes: 25 additions & 1 deletion swift-sdk/Internal/IterableAPIInternal.swift
Expand Up @@ -110,7 +110,22 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
return
}

register(token: token, appName: appName, pushServicePlatform: config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
if let userId = userId {
createUser(
withUserId: userId,
onSuccess: { (_) in
self.register(token: token, appName: appName, pushServicePlatform: self.config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
},
onFailure: {(errorMessage, _) in
if let errorMessage = errorMessage {
ITBError("Could not create user: \(errorMessage)")
} else {
ITBError()
}
})
} else {
register(token: token, appName: appName, pushServicePlatform: config.pushPlatform, onSuccess: onSuccess, onFailure: onFailure)
}
}

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

private func createUser(withUserId userId: String, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) {
var args = [AnyHashable : Any]()
args[.ITBL_KEY_USER_ID] = userId

if let request = createPostRequest(forPath: .ITBL_PATH_CREATE_USER, withBody: args) {
sendRequest(request, onSuccess: onSuccess, onFailure: onFailure)
}
}

func trackPurchase(_ total: NSNumber, items: [CommerceItem]) {
trackPurchase(total, items: items, dataFields: nil)
}
Expand Down
1 change: 0 additions & 1 deletion swift-sdk/IterableConstants.h
Expand Up @@ -22,4 +22,3 @@ typedef void (^OnSuccessHandler)(NSDictionary *data);
The prototype for the completion handler block that gets called when an Iterable call fails
*/
typedef void (^OnFailureHandler)(NSString *reason, NSData *_Nullable data);

0 comments on commit b9414a3

Please sign in to comment.