generated from StanfordSpezi/SpeziTemplateApplication
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reusable Weight Scale Service, UI Tests (#13)
# Reusable Weight Scale Service, UI Tests ## ♻️ Current situation & Problem This PR migrates to the reusable WeightScaleService implementation provided by SpeziBluetooth (see StanfordSpezi/SpeziBluetooth#27). Further, it adds testing to verify the functionality of adding new weight measurements for a registered user. This required to add functionality to automatically set up a test environment via a feature flag to inject an existing user. The new `InvitationCodeModule` centrally manages invitation codes and setting up the test environment. ## ⚙️ Release Notes * Migrate to WeightScaleService from SpeziBluetooth * Add UI tests for adding measurement using a SpeziBluetooth-supported Mock device. ## 📚 Documentation -- ## ✅ Testing -- ### Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
Showing
29 changed files
with
465 additions
and
510 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// | ||
// This source file is part of the ENGAGE-HF project based on the Stanford Spezi Template Application project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import Firebase | ||
import FirebaseAuth | ||
import FirebaseFunctions | ||
import Spezi | ||
import SpeziAccount | ||
import SpeziFirebaseConfiguration | ||
|
||
|
||
class InvitationCodeModule: Module, EnvironmentAccessible { | ||
@Dependency private var firebase: ConfigureFirebaseApp | ||
|
||
@Application(\.logger) private var logger | ||
|
||
func configure() { | ||
if FeatureFlags.useFirebaseEmulator { | ||
Functions.functions().useEmulator(withHost: "localhost", port: 5001) | ||
} | ||
} | ||
|
||
func signOutAccount() { | ||
do { | ||
try Auth.auth().signOut() | ||
} catch { | ||
logger.debug("Failed to sing out firebase account: \(error)") | ||
} | ||
} | ||
|
||
func verifyOnboardingCode(_ invitationCode: String) async throws { | ||
do { | ||
if FeatureFlags.disableFirebase { | ||
guard invitationCode == "ENGAGEHFTEST1" else { | ||
throw InvitationCodeError.invitationCodeInvalid | ||
} | ||
|
||
try? await Task.sleep(for: .seconds(0.25)) | ||
} else { | ||
try Auth.auth().signOut() | ||
|
||
try await Auth.auth().signInAnonymously() | ||
let checkInvitationCode = Functions.functions().httpsCallable("checkInvitationCode") | ||
|
||
do { | ||
_ = try await checkInvitationCode.call( | ||
[ | ||
"invitationCode": invitationCode | ||
] | ||
) | ||
} catch { | ||
logger.error("Failed to check invitation code: \(error)") | ||
throw InvitationCodeError.invitationCodeInvalid | ||
} | ||
} | ||
} catch let error as NSError { | ||
if let errorCode = FunctionsErrorCode(rawValue: error.code) { | ||
// Handle Firebase-specific errors. | ||
switch errorCode { | ||
case .unauthenticated: | ||
throw InvitationCodeError.userNotAuthenticated | ||
case .notFound: | ||
throw InvitationCodeError.invitationCodeInvalid | ||
default: | ||
throw InvitationCodeError.generalError(error.localizedDescription) | ||
} | ||
} else { | ||
// Handle other errors, such as network issues or unexpected behavior. | ||
throw InvitationCodeError.generalError(error.localizedDescription) | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
func setupTestEnvironment(account: Account, invitationCode: String) async throws { | ||
let email = "[email protected]" | ||
let password = "123456789" | ||
|
||
// let the initial stateChangeDelegate of FirebaseAuth get called. Otherwise, we will interfere with that. | ||
try await Task.sleep(for: .milliseconds(500)) | ||
|
||
if let details = account.details, | ||
details.email == email { | ||
return | ||
} | ||
|
||
guard let service = account.registeredAccountServices.compactMap({ $0 as? any UserIdPasswordAccountService }).first else { | ||
preconditionFailure("Failed to retrieve a user-id-password based account service for test account setup!") | ||
} | ||
|
||
do { | ||
try await service.login(userId: email, password: password) | ||
return // account was already established previously | ||
} catch { | ||
// probably doesn't exists. We try to create a new one below | ||
} | ||
|
||
try await verifyOnboardingCode(invitationCode) | ||
try await setupTestAccount(service: service, email: email, password: password) | ||
} | ||
|
||
private func setupTestAccount(service: any UserIdPasswordAccountService, email: String, password: String) async throws { | ||
do { | ||
let details = SignupDetails.Builder() | ||
.set(\.userId, value: email) | ||
.set(\.name, value: PersonNameComponents(givenName: "Leland", familyName: "Stanford")) | ||
.set(\.password, value: password) | ||
.build() | ||
try await service.signUp(signupDetails: details) | ||
} catch { | ||
logger.error("Failed setting up test account : \(error)") | ||
throw error | ||
} | ||
} | ||
} |
Oops, something went wrong.