Skip to content

Commit d4bdbb6

Browse files
Merge pull request #82 from Iterable/bugfix/mob-357-prefer
[MOB-357] - Prefer user id
2 parents 169fd17 + 50f413e commit d4bdbb6

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

Tests/swift-sdk-swift-tests/IterableAPITests.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ class IterableAPITests: XCTestCase {
101101
wait(for: [expectation], timeout: testExpectationTimeout)
102102
}
103103

104-
func testUpdateUser() {
105-
let expectation = XCTestExpectation(description: "testUpdateUser")
104+
func testUpdateUserWithEmail() {
105+
let expectation = XCTestExpectation(description: "testUpdateUserWithEmail")
106106

107107
let networkSession = MockNetworkSession(statusCode: 200)
108108
IterableAPI.initialize(apiKey: IterableAPITests.apiKey, networkSession: networkSession)
@@ -127,6 +127,34 @@ class IterableAPITests: XCTestCase {
127127
wait(for: [expectation], timeout: testExpectationTimeout)
128128
}
129129

130+
func testUpdateUserWithUserId() {
131+
let expectation = XCTestExpectation(description: "testUpdateUserWithUserId")
132+
133+
let userId = UUID().uuidString
134+
let networkSession = MockNetworkSession(statusCode: 200)
135+
IterableAPI.initialize(apiKey: IterableAPITests.apiKey, networkSession: networkSession)
136+
IterableAPI.userId = userId
137+
let dataFields: Dictionary<String, String> = ["var1" : "val1", "var2" : "val2"]
138+
IterableAPI.updateUser(dataFields, mergeNestedObjects: true, onSuccess: {(json) in
139+
TestUtils.validate(request: networkSession.request!, requestType: .post, apiEndPoint: .ITBL_ENDPOINT_API, path: .ITBL_PATH_UPDATE_USER, queryParams: [(name: "api_key", IterableAPITests.apiKey)])
140+
let body = networkSession.getRequestBody()
141+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_USER_ID, andValue: userId, inDictionary: body)
142+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_PREFER_USER_ID, andValue: true, inDictionary: body)
143+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_MERGE_NESTED, andValue: true, inDictionary: body)
144+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_DATA_FIELDS, andValue: dataFields, inDictionary: body)
145+
expectation.fulfill()
146+
}) {(reason, _) in
147+
if let reason = reason {
148+
XCTFail("encountered error: \(reason)")
149+
} else {
150+
XCTFail("encountered error")
151+
}
152+
expectation.fulfill()
153+
}
154+
155+
wait(for: [expectation], timeout: testExpectationTimeout)
156+
}
157+
130158
func testUpdateEmail() {
131159
let expectation = XCTestExpectation(description: "testUpdateEmail")
132160

swift-sdk/Internal/IterableAPIInternal.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
198198
args[.ITBL_KEY_DATA_FIELDS] = dataFields
199199
args[.ITBL_KEY_MERGE_NESTED] = mergeNested
200200
addEmailOrUserId(args: &args)
201+
if email == nil && userId != nil {
202+
args[.ITBL_KEY_PREFER_USER_ID] = true
203+
}
201204

202205
if let request = createPostRequest(forPath: .ITBL_PATH_UPDATE_USER, withBody: args) {
203206
sendRequest(request, onSuccess: onSuccess, onFailure: onFailure)

0 commit comments

Comments
 (0)