Skip to content

Commit 9057728

Browse files
Cherry pick fix for update email using user id from master.
1 parent 84385c1 commit 9057728

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,40 @@ class IterableAPITests: XCTestCase {
186186

187187
wait(for: [expectation], timeout: testExpectationTimeout)
188188
}
189+
190+
func testUpdateEmailWithUserId() {
191+
let expectation = XCTestExpectation(description: "testUpdateEmailWithUserId")
192+
193+
let currentUserId = IterableUtil.generateUUID()
194+
let newEmail = "[email protected]"
195+
let networkSession = MockNetworkSession(statusCode: 200)
196+
IterableAPI.initialize(apiKey: IterableAPITests.apiKey, networkSession: networkSession)
197+
IterableAPI.userId = currentUserId
198+
IterableAPI.updateEmail(newEmail,
199+
onSuccess: {json in
200+
TestUtils.validate(request: networkSession.request!,
201+
requestType: .post,
202+
apiEndPoint: .ITBL_ENDPOINT_API,
203+
path: .ITBL_PATH_UPDATE_EMAIL,
204+
queryParams: [(name: "api_key", value: IterableAPITests.apiKey)])
205+
let body = networkSession.getRequestBody()
206+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_NEW_EMAIL, andValue: newEmail, inDictionary: body)
207+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_CURRENT_USER_ID, andValue: currentUserId, inDictionary: body)
208+
XCTAssertEqual(IterableAPI.email, newEmail)
209+
expectation.fulfill()
210+
},
211+
onFailure: {(reason, _) in
212+
expectation.fulfill()
213+
if let reason = reason {
214+
XCTFail("encountered error: \(reason)")
215+
} else {
216+
XCTFail("encountered error")
217+
}
218+
})
219+
220+
wait(for: [expectation], timeout: testExpectationTimeout)
221+
}
222+
189223

190224
func testRegisterTokenNilAppName() {
191225
let expectation = XCTestExpectation(description: "testRegisterToken")

swift-sdk/ITBConsts.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public extension AnyHashable {
3939
static let ITBL_KEY_CAMPAIGN_ID = "campaignId"
4040
static let ITBL_KEY_COUNT = "count"
4141
static let ITBL_KEY_CURRENT_EMAIL = "currentEmail"
42+
static let ITBL_KEY_CURRENT_USER_ID = "currentUserId"
4243
static let ITBL_KEY_DATA_FIELDS = "dataFields"
4344
static let ITBL_KEY_DEVICE = "device"
4445
static let ITBL_KEY_EMAIL = "email"

swift-sdk/Internal/IterableAPIInternal.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,20 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
208208
}
209209

210210
func updateEmail(_ newEmail: String, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) {
211-
guard let email = email else {
212-
onFailure?("updateEmail should not be called with a userId. Init SDK with email instead of userId.", nil)
213-
return
214-
}
215-
216-
let args: [String : Any] = [
217-
AnyHashable.ITBL_KEY_CURRENT_EMAIL: email,
211+
var args: [String : Any] = [
218212
AnyHashable.ITBL_KEY_NEW_EMAIL: newEmail
219213
]
220214

215+
if let userId = userId {
216+
args[AnyHashable.ITBL_KEY_CURRENT_USER_ID] = userId
217+
} else if let email = email {
218+
args[AnyHashable.ITBL_KEY_CURRENT_EMAIL] = email
219+
} else {
220+
ITBError("Both email and userId are nil")
221+
onFailure?("Both email and userId are nil", nil)
222+
return
223+
}
224+
221225
if let request = createPostRequest(forPath: .ITBL_PATH_UPDATE_EMAIL, withBody: args) {
222226
sendRequest(request,
223227
onSuccess: { data in

0 commit comments

Comments
 (0)