mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-20 16:42:44 +02:00
b298dedc22
Merge commit 'f823ab9aae70e8d15ed7cc079210dd9bbbb6c8e1' into feature/foss_update_1_11_19 * commit 'f823ab9aae70e8d15ed7cc079210dd9bbbb6c8e1': finish version++ version++ comments update submodule remove obsolete tests removed unused code update submodule fix Libolm removal update license macro update license Prepare for new sprint # Conflicts: # Config/AppVersion.xcconfig # IDETemplateMacros.plist # LICENSE # README.md # Riot/Categories/MXSession+Riot.m # Riot/Managers/EncryptionKeyManager/EncryptionKeyManager.swift # Riot/Managers/KeyValueStorage/Extensions/Keychain.swift # Riot/Managers/KeyValueStorage/KeyValueStore.swift # Riot/Managers/KeyValueStorage/KeychainStore.swift # Riot/Managers/KeyValueStorage/MemoryStore.swift # Riot/Managers/PushNotification/PushNotificationService.m # Riot/Managers/Settings/RiotSettings.swift # Riot/Managers/Settings/Shared/RiotSharedSettings.swift # Riot/Modules/Analytics/AnalyticsUIElement.swift # Riot/Modules/Application/AppCoordinator.swift # Riot/Modules/Application/LegacyAppDelegate.h # Riot/Modules/Application/LegacyAppDelegate.m # Riot/Modules/Authentication/Legacy/AuthenticationViewController.h # Riot/Modules/Authentication/Legacy/AuthenticationViewController.m # Riot/Modules/Authentication/Legacy/Views/AuthInputsView.h # Riot/Modules/Authentication/Legacy/Views/AuthInputsView.m # Riot/Modules/Common/Recents/DataSources/RecentsDataSource.m # Riot/Modules/Common/Recents/RecentsViewController.m # Riot/Modules/Common/WebViewController/WebViewViewController.m # Riot/Modules/Contacts/Details/ContactDetailsViewController.m # Riot/Modules/Contacts/Views/ContactTableViewCell.m # Riot/Modules/Favorites/FavouritesViewController.h # Riot/Modules/Favorites/FavouritesViewController.m # Riot/Modules/GlobalSearch/UnifiedSearchViewController.m # Riot/Modules/People/PeopleViewController.h # Riot/Modules/People/PeopleViewController.m # Riot/Modules/Room/ContextualMenu/ReactionsMenu/ReactionsMenuViewModel.swift # Riot/Modules/Room/DataSources/RoomDataSource.m # Riot/Modules/Room/Files/RoomFilesViewController.m # Riot/Modules/Room/Members/Detail/RoomMemberDetailsViewController.m # Riot/Modules/Room/Members/RoomParticipantsViewController.m # Riot/Modules/Room/RoomViewController.m # Riot/Modules/Room/Settings/RoomSettingsViewController.m # Riot/Modules/Room/TimelineCells/RoomCreationIntro/RoomCreationIntroCell.swift # Riot/Modules/Room/TimelineCells/RoomCreationIntro/RoomCreationIntroCellContentView.swift # Riot/Modules/Room/TimelineCells/RoomCreationIntro/RoomCreationIntroViewData.swift # Riot/Modules/Room/TimelineCells/RoomTimelineCellIdentifier.h # Riot/Modules/Rooms/RoomsViewController.h # Riot/Modules/Rooms/ShowDirectory/Cells/Network/DirectoryNetworkTableHeaderFooterView.swift # Riot/Modules/Rooms/ShowDirectory/Cells/Room/DirectoryRoomTableViewCell.swift # Riot/Modules/Rooms/ShowDirectory/PublicRoomsDirectoryViewModel.swift # Riot/Modules/Secrets/Recover/RecoverWithKey/SecretsRecoveryWithKeyCoordinator.swift # Riot/Modules/Secrets/Recover/RecoverWithKey/SecretsRecoveryWithKeyViewController.swift # Riot/Modules/Secrets/Recover/RecoverWithPassphrase/SecretsRecoveryWithPassphraseCoordinator.swift # Riot/Modules/Secrets/Recover/RecoverWithPassphrase/SecretsRecoveryWithPassphraseViewController.swift # Riot/Modules/Secrets/Recover/SecretsRecoveryCoordinator.swift # Riot/Modules/SecureBackup/Setup/Intro/SecureBackupSetupIntroViewController.swift # Riot/Modules/SecureBackup/Setup/Intro/SecureBackupSetupIntroViewModel.swift # Riot/Modules/SecureBackup/Setup/Intro/SecureBackupSetupIntroViewModelType.swift # Riot/Modules/SetPinCode/PinCodePreferences.swift # Riot/Modules/SetPinCode/SetupBiometrics/BiometricsAuthenticationPresenter.swift # Riot/Modules/Settings/Security/ManageSession/ManageSessionViewController.m # Riot/Modules/Settings/Security/SecurityViewController.m # Riot/Modules/Settings/SettingsViewController.m # Riot/Modules/SplitView/SplitViewCoordinator.swift # Riot/Modules/SplitView/SplitViewCoordinatorType.swift # Riot/Modules/StartChat/StartChatViewController.m # Riot/Modules/TabBar/MasterTabBarController.h # Riot/Modules/TabBar/MasterTabBarController.m # Riot/Utils/EventFormatter.m # Riot/Utils/HTMLFormatter.swift # Riot/Utils/Tools.m # RiotNSE/NotificationService.swift
282 lines
16 KiB
Swift
282 lines
16 KiB
Swift
//
|
|
// Copyright 2021-2024 New Vector Ltd.
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
// Please see LICENSE in the repository root for full details.
|
|
//
|
|
|
|
import Combine
|
|
import XCTest
|
|
|
|
@testable import RiotSwiftUI
|
|
|
|
@MainActor class AuthenticationRegistrationViewModelTests: XCTestCase {
|
|
let defaultHomeserver = AuthenticationHomeserverViewData.mockMatrixDotOrg
|
|
var viewModel: AuthenticationRegistrationViewModelProtocol!
|
|
var context: AuthenticationRegistrationViewModelType.Context!
|
|
|
|
@MainActor override func setUp() async throws {
|
|
viewModel = AuthenticationRegistrationViewModel(homeserver: defaultHomeserver)
|
|
context = viewModel.context
|
|
}
|
|
|
|
func testMatrixDotOrg() {
|
|
// Given the initial view model configured for matrix.org with some SSO providers.
|
|
let homeserver = defaultHomeserver
|
|
|
|
// Then the view state should contain a homeserver that matches matrix.org and shows SSO buttons.
|
|
XCTAssertEqual(context.viewState.homeserver, homeserver, "The homeserver data should match the original.")
|
|
XCTAssertTrue(context.viewState.showSSOButtons, "The SSO buttons should be shown.")
|
|
}
|
|
|
|
func testBasicServer() {
|
|
// Given a basic server example.com that only supports password registration.
|
|
let homeserver = AuthenticationHomeserverViewData.mockBasicServer
|
|
|
|
// When updating the view model with the server.
|
|
viewModel.update(homeserver: homeserver)
|
|
|
|
// Then the view state should be updated with the homeserver and hide the SSO buttons.
|
|
XCTAssertEqual(context.viewState.homeserver, homeserver, "The homeserver data should should match the new homeserver.")
|
|
XCTAssertFalse(context.viewState.showSSOButtons, "The SSO buttons should not be shown.")
|
|
}
|
|
|
|
func testFallbackServer() {
|
|
// Given a basic server example.com that only supports password registration.
|
|
let homeserver = AuthenticationHomeserverViewData.mockFallback
|
|
|
|
// When updating the view model with the server.
|
|
viewModel.update(homeserver: homeserver)
|
|
|
|
// Then the view state should be updated with the homeserver and hide the SSO buttons and registration form.
|
|
XCTAssertFalse(context.viewState.homeserver.showRegistrationForm, "The registration form should not be shown.")
|
|
XCTAssertFalse(context.viewState.showSSOButtons, "The SSO buttons should not be shown.")
|
|
}
|
|
|
|
func testUsernameError() async throws {
|
|
// Given a form with an entered username.
|
|
context.username = "bob"
|
|
XCTAssertEqual(context.viewState.usernameAvailability, .unknown, "The username availability should be unknown when the view model is created.")
|
|
XCTAssertEqual(context.viewState.usernameFooterMessage, VectorL10n.authenticationRegistrationUsernameFooter, "The standard footer message should be shown.")
|
|
XCTAssertFalse(context.viewState.isUsernameInvalid, "The username should be valid if there is no error.")
|
|
|
|
// When displaying the error as a username error.
|
|
let errorMessage = "Username unavailable"
|
|
viewModel.displayError(.usernameUnavailable(errorMessage))
|
|
|
|
// Then the error should be shown in the footer.
|
|
guard case let .invalid(displayedError) = context.viewState.usernameAvailability else {
|
|
XCTFail("The username should be invalid when an error is shown.")
|
|
return
|
|
}
|
|
XCTAssertEqual(displayedError, errorMessage, "The error message should match.")
|
|
XCTAssertEqual(context.viewState.usernameFooterMessage, errorMessage, "The error message should replace the standard footer message.")
|
|
XCTAssertTrue(context.viewState.isUsernameInvalid, "The username should be invalid when an error is shown.")
|
|
|
|
// When clearing the error.
|
|
context.send(viewAction: .resetUsernameAvailability)
|
|
|
|
// Wait for the action to spawn a Task on the main actor as the Context protocol doesn't support actors.
|
|
await Task.yield()
|
|
|
|
// Then the error should be hidden again.
|
|
XCTAssertEqual(context.viewState.usernameAvailability, .unknown, "The username availability should return to an unknown state.")
|
|
XCTAssertEqual(context.viewState.usernameFooterMessage, VectorL10n.authenticationRegistrationUsernameFooter, "The standard footer message should be shown again.")
|
|
XCTAssertFalse(context.viewState.isUsernameInvalid, "The username should be valid when an error is cleared.")
|
|
}
|
|
|
|
func testUsernameAvailability() async throws {
|
|
// Given a form with an entered username.
|
|
context.username = "bob"
|
|
XCTAssertEqual(context.viewState.usernameAvailability, .unknown, "The username availability should be unknown when the view model is created.")
|
|
XCTAssertEqual(context.viewState.usernameFooterMessage, VectorL10n.authenticationRegistrationUsernameFooter, "The standard footer message should be shown.")
|
|
XCTAssertFalse(context.viewState.isUsernameInvalid, "The username should be valid if there is no error.")
|
|
|
|
// When updating the state for an available username
|
|
viewModel.confirmUsernameAvailability("bob")
|
|
|
|
// Then the error should be shown in the footer.
|
|
XCTAssertEqual(context.viewState.usernameAvailability, .available,
|
|
"The username should be detected as available.")
|
|
XCTAssertEqual(context.viewState.usernameFooterMessage, VectorL10n.authenticationRegistrationUsernameFooterAvailable("@bob:matrix.org"),
|
|
"The footer message should display that the username is available.")
|
|
XCTAssertFalse(context.viewState.isUsernameInvalid,
|
|
"The username should continue to be valid when it is available.")
|
|
|
|
// When clearing the error.
|
|
context.send(viewAction: .resetUsernameAvailability)
|
|
|
|
// Wait for the action to spawn a Task on the main actor as the Context protocol doesn't support actors.
|
|
await Task.yield()
|
|
|
|
// Then the error should be hidden again.
|
|
XCTAssertEqual(context.viewState.usernameAvailability, .unknown, "The username availability should return to an unknown state.")
|
|
XCTAssertEqual(context.viewState.usernameFooterMessage, VectorL10n.authenticationRegistrationUsernameFooter, "The standard footer message should be shown again.")
|
|
XCTAssertFalse(context.viewState.isUsernameInvalid, "The username should be valid when an error is cleared.")
|
|
}
|
|
|
|
func testUsernameAvailabilityWhenChanged() async throws {
|
|
// Given a form with an entered username.
|
|
context.username = "robert"
|
|
XCTAssertEqual(context.viewState.usernameAvailability, .unknown, "The username availability should be unknown when the view model is created.")
|
|
XCTAssertEqual(context.viewState.usernameFooterMessage, VectorL10n.authenticationRegistrationUsernameFooter, "The standard footer message should be shown.")
|
|
XCTAssertFalse(context.viewState.isUsernameInvalid, "The username should be valid if there is no error.")
|
|
|
|
// When updating the state for an available username that was previously entered.
|
|
viewModel.confirmUsernameAvailability("bob")
|
|
|
|
// Then the username should not be shown as available.
|
|
XCTAssertEqual(context.viewState.usernameAvailability, .unknown, "The username availability should not be updated.")
|
|
XCTAssertEqual(context.viewState.usernameFooterMessage, VectorL10n.authenticationRegistrationUsernameFooter, "The standard footer message should be shown.")
|
|
XCTAssertFalse(context.viewState.isUsernameInvalid, "The username should continue to be valid when unverified.")
|
|
}
|
|
|
|
func testEmptyUsernameWithShortPassword() {
|
|
// Given a form with an empty username and password.
|
|
XCTAssertTrue(context.password.isEmpty, "The initial value for the password should be empty.")
|
|
XCTAssertTrue(context.username.isEmpty, "The initial value for the username should be empty.")
|
|
XCTAssertTrue(context.viewState.isPasswordInvalid, "An empty password should be invalid.")
|
|
XCTAssertTrue(context.viewState.isUsernameInvalid, "An empty username should be invalid.")
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid.")
|
|
|
|
// When entering a password of 7 characters without a username.
|
|
context.username = ""
|
|
context.password = "1234567" //NOSONAR Password for Unittests
|
|
|
|
// Then the credentials should remain invalid.
|
|
XCTAssertTrue(context.viewState.isPasswordInvalid, "A 7-character password should be invalid.")
|
|
XCTAssertTrue(context.viewState.isUsernameInvalid, "An empty username should be invalid.")
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid.")
|
|
}
|
|
|
|
func testEmptyUsernameWithValidPassword() {
|
|
// Given a form with an empty username and password.
|
|
XCTAssertTrue(context.password.isEmpty, "The initial value for the password should be empty.")
|
|
XCTAssertTrue(context.username.isEmpty, "The initial value for the username should be empty.")
|
|
XCTAssertTrue(context.viewState.isPasswordInvalid, "An empty password should be invalid.")
|
|
XCTAssertTrue(context.viewState.isUsernameInvalid, "An empty username should be invalid.")
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid.")
|
|
|
|
// When entering a password of 8 characters without a username.
|
|
context.username = ""
|
|
context.password = "12345678" //NOSONAR Password for Unittests
|
|
|
|
// Then the password should be valid but the credentials should still be invalid.
|
|
XCTAssertFalse(context.viewState.isPasswordInvalid, "An 8-character password should be valid.")
|
|
XCTAssertTrue(context.viewState.isUsernameInvalid, "An empty username should be invalid.")
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid.")
|
|
}
|
|
|
|
func testValidUsernameWithEmptyPassword() {
|
|
// Given a form with an empty username and password.
|
|
XCTAssertTrue(context.password.isEmpty, "The initial value for the password should be empty.")
|
|
XCTAssertTrue(context.username.isEmpty, "The initial value for the username should be empty.")
|
|
XCTAssertTrue(context.viewState.isPasswordInvalid, "An empty password should be invalid.")
|
|
XCTAssertTrue(context.viewState.isUsernameInvalid, "An empty username should be invalid.")
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid.")
|
|
|
|
// When entering a username without a password.
|
|
context.username = "bob"
|
|
context.password = ""
|
|
|
|
// Then the username should be valid but the credentials should still be invalid.
|
|
XCTAssertTrue(context.viewState.isPasswordInvalid, "An empty password should be invalid.")
|
|
XCTAssertFalse(context.viewState.isUsernameInvalid, "The username should be valid when unverified.")
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid.")
|
|
}
|
|
|
|
func testUsernameErrorWithValidPassword() {
|
|
// Given a form with an empty username and password.
|
|
XCTAssertTrue(context.password.isEmpty, "The initial value for the password should be empty.")
|
|
XCTAssertTrue(context.username.isEmpty, "The initial value for the username should be empty.")
|
|
XCTAssertTrue(context.viewState.isPasswordInvalid, "An empty password should be invalid.")
|
|
XCTAssertTrue(context.viewState.isUsernameInvalid, "An empty username should be invalid.")
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid.")
|
|
|
|
// When entering a username and password and encountering a username error
|
|
context.username = "bob"
|
|
context.password = "12345678" //NOSONAR Password for Unittests
|
|
|
|
let errorMessage = "Username unavailable"
|
|
viewModel.displayError(.usernameUnavailable(errorMessage))
|
|
|
|
// Then the password should be valid but the credentials should still be invalid.
|
|
XCTAssertFalse(context.viewState.isPasswordInvalid, "An 8-character password should be valid.")
|
|
XCTAssertTrue(context.viewState.isUsernameInvalid, "The username should be invalid when an error is shown.")
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid.")
|
|
}
|
|
|
|
func testValidCredentials() {
|
|
// Given a form with an empty username and password.
|
|
XCTAssertTrue(context.password.isEmpty, "The initial value for the password should be empty.")
|
|
XCTAssertTrue(context.username.isEmpty, "The initial value for the username should be empty.")
|
|
XCTAssertTrue(context.viewState.isPasswordInvalid, "An empty password should be invalid.")
|
|
XCTAssertTrue(context.viewState.isUsernameInvalid, "An empty username should be invalid.")
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid.")
|
|
|
|
// When entering a username and an 8-character password.
|
|
context.username = "bob"
|
|
context.password = "12345678" //NOSONAR Password for Unittests
|
|
|
|
// Then the credentials should be considered valid.
|
|
XCTAssertFalse(context.viewState.isPasswordInvalid, "An 8-character password should be valid.")
|
|
XCTAssertFalse(context.viewState.isUsernameInvalid, "The username should be valid when unverified.")
|
|
XCTAssertTrue(context.viewState.hasValidCredentials, "The credentials should be valid when the username and password are valid.")
|
|
}
|
|
|
|
@MainActor func testLoadingServer() {
|
|
// Given a form with valid credentials.
|
|
context.username = "bob"
|
|
context.password = "12345678" //NOSONAR Password for Unittests
|
|
XCTAssertTrue(context.viewState.hasValidCredentials, "The credentials should be valid.")
|
|
XCTAssertTrue(context.viewState.canSubmit, "The form should be valid to submit.")
|
|
XCTAssertFalse(context.viewState.isLoading, "The view shouldn't start in a loading state.")
|
|
|
|
// When updating the view model whilst loading a homeserver.
|
|
viewModel.update(isLoading: true)
|
|
|
|
// Then the view state should reflect that the homeserver is loading.
|
|
XCTAssertTrue(context.viewState.isLoading, "The view should now be in a loading state.")
|
|
XCTAssertFalse(context.viewState.canSubmit, "The form should be blocked from submission.")
|
|
|
|
// When updating the view model after loading a homeserver.
|
|
viewModel.update(isLoading: false)
|
|
|
|
// Then the view state should reflect that the homeserver is now loaded.
|
|
XCTAssertFalse(context.viewState.isLoading, "The view should be back in a loaded state.")
|
|
XCTAssertTrue(context.viewState.canSubmit, "The form should once again be valid to submit.")
|
|
}
|
|
|
|
@MainActor func testUpdatingUsername() {
|
|
// Given a form with valid credentials.
|
|
let fullMXID = "@bob:example.com"
|
|
context.username = fullMXID
|
|
XCTAssertFalse(context.viewState.hasValidCredentials, "The credentials should be invalid without a password.")
|
|
XCTAssertFalse(context.viewState.canSubmit, "The form not be ready to submit without a password.")
|
|
XCTAssertFalse(context.viewState.isLoading, "The view shouldn't start in a loading state.")
|
|
|
|
// When updating the view model with a new username.
|
|
let localPart = "bob"
|
|
viewModel.update(username: localPart)
|
|
|
|
// Then the view state should reflect that the homeserver is loading.
|
|
XCTAssertEqual(context.username, localPart, "The username should match the value passed to the update method.")
|
|
}
|
|
}
|
|
|
|
extension AuthenticationRegistrationViewState.UsernameAvailability: Equatable {
|
|
public static func == (lhs: AuthenticationRegistrationViewState.UsernameAvailability,
|
|
rhs: AuthenticationRegistrationViewState.UsernameAvailability) -> Bool {
|
|
switch (lhs, rhs) {
|
|
case (.unknown, .unknown):
|
|
return true
|
|
case (.available, .available):
|
|
return true
|
|
case (.invalid, .invalid):
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
}
|