Skip to content

Commit

Permalink
Minor fixes in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Aug 9, 2024
1 parent 37fd7ad commit 0d1d881
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 112 deletions.
55 changes: 26 additions & 29 deletions Sources/SpeziFirebaseAccount/FirebaseAccountService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ public final class FirebaseAccountService: AccountService { // swiftlint:disable
@MainActor private var authStateDidChangeListenerHandle: AuthStateDidChangeListenerHandle?
@MainActor private var lastNonce: String?

private var isConfiguring = false
private var shouldQueue = false
private var queuedUpdates: [UserUpdate] = []
private var actionSemaphore = AsyncSemaphore()
private var skipNextStateChange = false


private var unsupportedKeys: AccountKeyCollection {
Expand Down Expand Up @@ -179,13 +179,12 @@ public final class FirebaseAccountService: AccountService { // swiftlint:disable
Auth.auth().useEmulator(withHost: emulatorSettings.host, port: emulatorSettings.port)
}

isConfiguring = true
defer {
isConfiguring = false
}
checkForInitialUserAccount()

// get notified about changes of the User reference
authStateDidChangeListenerHandle = Auth.auth().addStateDidChangeListener { [weak self] auth, user in
// We could safely assume main actor isolation here, see
// https://firebase.google.com/docs/reference/swift/firebaseauth/api/reference/Classes/Auth#/c:@M@FirebaseAuth@objc(cs)FIRAuth(im)addAuthStateDidChangeListener:
self?.handleStateDidChange(auth: auth, user: user)
}

Expand Down Expand Up @@ -528,32 +527,30 @@ public final class FirebaseAccountService: AccountService { // swiftlint:disable

extension FirebaseAccountService {
@MainActor
private func handleStateDidChange(auth: Auth, user: User?) {
if isConfiguring {
// We can safely assume main actor isolation, see
// https://firebase.google.com/docs/reference/swift/firebaseauth/api/reference/Classes/Auth#/c:@M@FirebaseAuth@objc(cs)FIRAuth(im)addAuthStateDidChangeListener:
let skip = MainActor.assumeIsolated {
// Ensure that there are details associated as soon as possible.
// Mark them as incomplete if we know there might be account details that are stored externally,
// we update the details later anyways, even if we might be wrong.
if let user {
var details = buildUser(user, isNewUser: false)
details.isIncomplete = !self.unsupportedKeys.isEmpty

logger.debug("Supply initial user details of associated Firebase account.")
account.supplyUserDetails(details)
return !details.isIncomplete
} else {
account.removeUserDetails()
return true
}
}

guard !skip else {
return // don't spin of the task below if we know it wouldn't change anything.
}
private func checkForInitialUserAccount() {
guard let user = Auth.auth().currentUser else {
skipNextStateChange = true
return
}

// Ensure that there are details associated as soon as possible.
// Mark them as incomplete if we know there might be account details that are stored externally,
// we update the details later anyways, even if we might be wrong.

var details = buildUser(user, isNewUser: false)
details.isIncomplete = !self.unsupportedKeys.isEmpty

logger.debug("Supply initial user details of associated Firebase account.")
account.supplyUserDetails(details)
skipNextStateChange = !details.isIncomplete
}

@MainActor
private func handleStateDidChange(auth: Auth, user: User?) {
if skipNextStateChange {
skipNextStateChange = false
return
}

Task {
do {
Expand Down
25 changes: 25 additions & 0 deletions Tests/UITests/TestApp/Shared/TestAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@ import SwiftUI


class TestAppDelegate: SpeziAppDelegate {
private class Logout: Module {
@Application(\.logger)
private var logger

@Dependency(Account.self)
private var account
@Dependency(FirebaseAccountService.self)
private var service

func configure() {
if account.signedIn {
Task { [logger, service] in
do {
logger.info("Performing initial logout!")
try await service.logout()
} catch {
logger.error("Failed initial logout")
}
}
}
}
}

override var configuration: Configuration {
Configuration {
let configuration: AccountValueConfiguration = FeatureFlags.accountStorageTests
Expand Down Expand Up @@ -46,6 +69,8 @@ class TestAppDelegate: SpeziAppDelegate {
AccountConfiguration(service: service, configuration: configuration)
}

Logout()

Firestore(settings: .emulator)
FirebaseStorageConfiguration(emulatorSettings: (host: "localhost", port: 9199))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ final class FirebaseAccountStorageTests: XCTestCase {
XCTAssert(app.buttons["FirebaseAccount"].waitForExistence(timeout: 2.0))
app.buttons["FirebaseAccount"].tap()

if app.buttons["Logout"].waitForExistence(timeout: 2.0) && app.buttons["Logout"].isHittable {
app.buttons["Logout"].tap()
}


try app.signup(
username: "[email protected]",
password: "TestPassword1",
Expand Down
Loading

0 comments on commit 0d1d881

Please sign in to comment.