Skip to content

Commit 2280294

Browse files
Merge pull request #54 from Iterable/feature/mob-94-current-user-id
[MOB-94] - Update email with userId
2 parents eb5af30 + aea2e6a commit 2280294

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ class IterableAPITests: XCTestCase {
127127
wait(for: [expectation], timeout: testExpectationTimeout)
128128
}
129129

130-
func testUpdateEmail() {
131-
let expectation = XCTestExpectation(description: "testUpdateEmail")
130+
func testUpdateEmailWithEmail() {
131+
let expectation = XCTestExpectation(description: "testUpdateEmailWithEmail")
132132

133133
let newEmail = "[email protected]"
134134
let networkSession = MockNetworkSession(statusCode: 200)
@@ -158,6 +158,40 @@ class IterableAPITests: XCTestCase {
158158

159159
wait(for: [expectation], timeout: testExpectationTimeout)
160160
}
161+
162+
func testUpdateEmailWithUserId() {
163+
let expectation = XCTestExpectation(description: "testUpdateEmailWithUserId")
164+
165+
let currentUserId = IterableUtil.generateUUID()
166+
let newEmail = "[email protected]"
167+
let networkSession = MockNetworkSession(statusCode: 200)
168+
IterableAPI.initializeForTesting(apiKey: IterableAPITests.apiKey, networkSession: networkSession)
169+
IterableAPI.userId = currentUserId
170+
IterableAPI.updateEmail(newEmail,
171+
onSuccess: {json in
172+
TestUtils.validate(request: networkSession.request!,
173+
requestType: .post,
174+
apiEndPoint: .ITBL_ENDPOINT_API,
175+
path: .ITBL_PATH_UPDATE_EMAIL,
176+
queryParams: [(name: "api_key", value: IterableAPITests.apiKey)])
177+
let body = networkSession.getRequestBody()
178+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_NEW_EMAIL, andValue: newEmail, inDictionary: body)
179+
TestUtils.validateElementPresent(withName: AnyHashable.ITBL_KEY_CURRENT_USER_ID, andValue: currentUserId, inDictionary: body)
180+
XCTAssertEqual(IterableAPI.email, newEmail)
181+
expectation.fulfill()
182+
},
183+
onFailure: {(reason, _) in
184+
expectation.fulfill()
185+
if let reason = reason {
186+
XCTFail("encountered error: \(reason)")
187+
} else {
188+
XCTFail("encountered error")
189+
}
190+
})
191+
192+
wait(for: [expectation], timeout: testExpectationTimeout)
193+
}
194+
161195

162196
func testRegisterTokenNilAppName() {
163197
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
public static let ITBL_KEY_CAMPAIGN_ID = "campaignId"
4040
public static let ITBL_KEY_COUNT = "count"
4141
public static let ITBL_KEY_CURRENT_EMAIL = "currentEmail"
42+
public static let ITBL_KEY_CURRENT_USER_ID = "currentUserId"
4243
public static let ITBL_KEY_DATA_FIELDS = "dataFields"
4344
public static let ITBL_KEY_DEVICE = "device"
4445
public 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
@@ -218,16 +218,20 @@ final class IterableAPIInternal : NSObject, PushTrackerProtocol {
218218
}
219219

220220
func updateEmail(_ newEmail: String, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) {
221-
guard let email = email else {
222-
onFailure?("updateEmail should not be called with a userId. Init SDK with email instead of userId.", nil)
223-
return
224-
}
225-
226-
let args: [String : Any] = [
227-
AnyHashable.ITBL_KEY_CURRENT_EMAIL: email,
221+
var args: [String : Any] = [
228222
AnyHashable.ITBL_KEY_NEW_EMAIL: newEmail
229223
]
230224

225+
if let email = email {
226+
args[AnyHashable.ITBL_KEY_CURRENT_EMAIL] = email
227+
} else if let userId = userId {
228+
args[AnyHashable.ITBL_KEY_CURRENT_USER_ID] = userId
229+
} else {
230+
ITBError("Both email and userId are nil")
231+
onFailure?("Both email and userId are nil", nil)
232+
return
233+
}
234+
231235
if let request = createPostRequest(forPath: .ITBL_PATH_UPDATE_EMAIL, withBody: args) {
232236
sendRequest(request,
233237
onSuccess: { data in

0 commit comments

Comments
 (0)