Skip to content

Commit

Permalink
Rewire current user email in AppEnvironment/AppDelegateViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
amy-at-kickstarter committed Mar 4, 2024
1 parent bfa07f7 commit 6ee9775
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Kickstarter-iOS/AppDelegateViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,11 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
AppEnvironment.current.apiService.fetchGraphUserEmail().wrapInOptional().materialize()
}

_ = fetchUserEmailEvent.values()
self.fetchUserEmail = fetchUserEmailEvent.values()
.map { user in
guard let email = user?.me.email else { return }
guard let email = user?.me.email else {
return
}

AppEnvironment.updateCurrentUserEmail(email)
}
Expand Down Expand Up @@ -904,6 +906,7 @@ public final class AppDelegateViewModel: AppDelegateViewModelType, AppDelegateVi
public let emailVerificationCompleted: Signal<(String, Bool), Never>
public let findRedirectUrl: Signal<URL, Never>
public let forceLogout: Signal<(), Never>
private let fetchUserEmail: Signal<(), Never>
public let goToActivity: Signal<(), Never>
public let goToDiscovery: Signal<DiscoveryParams?, Never>
public let goToLoginWithIntent: Signal<LoginIntent, Never>
Expand Down
31 changes: 31 additions & 0 deletions Kickstarter-iOS/AppDelegateViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2434,6 +2434,37 @@ final class AppDelegateViewModelTests: TestCase {
self.postNotificationName.assertValues([.ksr_remoteConfigClientConfigurationFailed])
}
}

func testUserSessionStarted_fetchesUserEmail_andClearsOnLogout() {
let fetchUserEmailQueryData = GraphAPI.FetchUserEmailQuery
.Data(unsafeResultMap: [
"me": [
"email": "nativesquad@ksr.com"
]
]
)

guard let envelope = UserEnvelope<GraphUserEmail>.userEnvelope(from: fetchUserEmailQueryData) else {
XCTFail()
return
}

let mockService = MockService(
fetchGraphUserEmailResult: .success(envelope)
)

withEnvironment(apiService: mockService) {
XCTAssertNil(AppEnvironment.current.currentUserEmail)

self.vm.inputs.userSessionStarted()

XCTAssertEqual(AppEnvironment.current.currentUserEmail, "nativesquad@ksr.com")

AppEnvironment.logout()

XCTAssertNil(AppEnvironment.current.currentUserEmail)
}
}
}

private let backingForCreatorPushData: [String: Any] = [
Expand Down
1 change: 1 addition & 0 deletions Library/AppEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public struct AppEnvironment: AppEnvironmentType {
apiService: AppEnvironment.current.apiService.logout(),
cache: type(of: AppEnvironment.current.cache).init(),
currentUser: nil,
currentUserEmail: nil,
ksrAnalytics: self.current.ksrAnalytics |> KSRAnalytics.lens.loggedInUser .~ nil
)
}
Expand Down

0 comments on commit 6ee9775

Please sign in to comment.