Skip to content

Commit

Permalink
Merge pull request #540 from Iterable/jay/MOB-4239-updateEmail-withToken
Browse files Browse the repository at this point in the history
[MOB-4239] fix update email with token
  • Loading branch information
roninopf authored May 5, 2022
2 parents 6bfea1a + d0e0390 commit 07173cd
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 15 deletions.
8 changes: 7 additions & 1 deletion swift-sdk/Internal/AuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class AuthManager: IterableAuthManagerProtocol {
}
}

func setNewToken(_ newToken: String) {
ITBInfo()

onAuthTokenReceived(retrievedAuthToken: newToken)
}

func logoutUser() {
ITBInfo()

Expand Down Expand Up @@ -86,7 +92,7 @@ class AuthManager: IterableAuthManagerProtocol {
queueAuthTokenExpirationRefresh(authToken)
}

private func onAuthTokenReceived(retrievedAuthToken: String?, onSuccess: AuthTokenRetrievalHandler?) {
private func onAuthTokenReceived(retrievedAuthToken: String?, onSuccess: AuthTokenRetrievalHandler? = nil) {
pendingAuth = false

authToken = retrievedAuthToken
Expand Down
22 changes: 14 additions & 8 deletions swift-sdk/Internal/InternalIterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
deviceAttributes.removeValue(forKey: name)
}

func setEmail(_ email: String?) {
func setEmail(_ email: String?, authToken: String? = nil) {
ITBInfo()

if _email == email {
Expand All @@ -109,10 +109,10 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {

storeIdentifierData()

onLogin()
onLogin(authToken)
}

func setUserId(_ userId: String?) {
func setUserId(_ userId: String?, authToken: String? = nil) {
ITBInfo()

if _userId == userId {
Expand All @@ -126,7 +126,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {

storeIdentifierData()

onLogin()
onLogin(authToken)
}

func logoutUser() {
Expand Down Expand Up @@ -200,10 +200,13 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
withToken token: String? = nil,
onSuccess: OnSuccessHandler? = nil,
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
requestHandler.updateEmail(newEmail, onSuccess: nil, onFailure: nil).onSuccess { json in
requestHandler.updateEmail(newEmail,
onSuccess: nil,
onFailure: nil).onSuccess { json in
if self.email != nil {
self.setEmail(newEmail)
self.setEmail(newEmail, authToken: token)
}

onSuccess?(json)
}.onError { error in
onFailure?(error.reason, error.data)
Expand Down Expand Up @@ -475,10 +478,13 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
localStorage.userId = _userId
}

private func onLogin() {
private func onLogin(_ authToken: String? = nil) {
ITBInfo()

if isEitherUserIdOrEmailSet() && config.authDelegate != nil {
if let authToken = authToken {
self.authManager.setNewToken(authToken)
completeUserLogin()
} else if isEitherUserIdOrEmailSet() && config.authDelegate != nil {
requestNewAuthToken()
} else {
completeUserLogin()
Expand Down
4 changes: 2 additions & 2 deletions swift-sdk/IterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public final class IterableAPI: NSObject {
///
/// - Parameters:
/// - newEmail: The new email of this user
/// - token: The new authentication token for this user
/// - token: The new authentication token for this user, if left out, the SDK will not update the token in any way
/// - onSuccess: `OnSuccessHandler` to invoke if update is successful
/// - onFailure: `OnFailureHandler` to invoke if update fails
///
Expand All @@ -321,7 +321,7 @@ public final class IterableAPI: NSObject {
withToken token: String,
onSuccess: OnSuccessHandler?,
onFailure: OnFailureHandler?) {
internalImplementation?.updateEmail(newEmail, onSuccess: onSuccess, onFailure: onFailure)
internalImplementation?.updateEmail(newEmail, withToken: token, onSuccess: onSuccess, onFailure: onFailure)
}

/// Tracks what's in the shopping cart (or equivalent) at this point in time
Expand Down
1 change: 1 addition & 0 deletions swift-sdk/IterableAuthManagerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import Foundation
func getAuthToken() -> String?
func resetFailedAuthCount()
func requestNewAuthToken(hasFailedPriorAuth: Bool, onSuccess: ((String?) -> Void)?)
func setNewToken(_ newToken: String)
func logoutUser()
}
52 changes: 48 additions & 4 deletions tests/unit-tests/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class AuthTests: XCTestCase {
XCTAssertNil(internalAPI.auth.authToken)
}

func testNewEmailWithTokenChange() {
func testNewEmailAndThenChangeToken() {
var internalAPI: InternalIterableAPI?

let originalEmail = "[email protected]"
Expand Down Expand Up @@ -128,7 +128,7 @@ class AuthTests: XCTestCase {
XCTAssertEqual(API.auth.authToken, newToken)
}

func testNewUserIdWithTokenChange() {
func testNewUserIdAndThenChangeToken() {
var internalAPI: InternalIterableAPI?

let originalUserId = "firstUserId"
Expand Down Expand Up @@ -166,8 +166,8 @@ class AuthTests: XCTestCase {
XCTAssertEqual(API.auth.authToken, newToken)
}

func testUpdateEmailWithToken() {
let condition1 = expectation(description: "update email with auth token")
func testUpdateEmailAndThenChangeToken() {
let condition1 = expectation(description: "update email and then change auth token")

var internalAPI: InternalIterableAPI?

Expand Down Expand Up @@ -211,6 +211,50 @@ class AuthTests: XCTestCase {
wait(for: [condition1], timeout: testExpectationTimeout)
}

func testUpdateEmailWithTokenParam() {
let condition1 = expectation(description: #function)

var internalAPI: InternalIterableAPI?

let originalEmail = "rtbo"
let originalToken = "hngk"

let updatedEmail = "2"
let updatedToken = "564g"

let authDelegate = DefaultAuthDelegate {
return originalToken
}

let config = IterableConfig()
config.authDelegate = authDelegate

internalAPI = InternalIterableAPI.initializeForTesting(config: config)

guard let API = internalAPI else {
XCTFail()
return
}

API.setEmail(originalEmail)

XCTAssertEqual(API.email, originalEmail)
XCTAssertNil(API.userId)
XCTAssertEqual(API.auth.authToken, originalToken)

API.updateEmail(updatedEmail, withToken: updatedToken) { data in
XCTAssertEqual(API.email, updatedEmail)
XCTAssertNil(API.userId)
XCTAssertEqual(API.auth.authToken, updatedToken)

condition1.fulfill()
} onFailure: { reason, data in
XCTFail()
}

wait(for: [condition1], timeout: testExpectationTimeout)
}

func testLogoutUser() {
let authDelegate = createStockAuthDelegate()

Expand Down

0 comments on commit 07173cd

Please sign in to comment.