mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 16:24:11 +00:00
22d14ff8e4
* create DependenciesProtocol and replace ServiceLocator registration with it replace mocking calls for ServiceLocator to Dependencies replace ServiceLocator with instance level calls replace ServiceLocator configs with TestDependencies config in Tests replace servicelocator usage in analytics service replace remaining ServiceLocator calls in unit tests that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances fix swiftlint warning remove remaining references to ServiceLocator make dependencies properties consistent in tests delete Dependencies and DependenciesProtocol fix whitespace modifications updates from PR feedback a pass over converting AppSettings and AnalyticsService test instance properties to per test instantiation fix a missed PR feedback Sync up after rebase create DependenciesProtocol and replace ServiceLocator registration with it replace mocking calls for ServiceLocator to Dependencies replace ServiceLocator with instance level calls replace ServiceLocator configs with TestDependencies config in Tests replace servicelocator usage in analytics service replace remaining ServiceLocator calls in unit tests that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances fix swiftlint warning remove remaining references to ServiceLocator make dependencies properties consistent in tests delete Dependencies and DependenciesProtocol * AppSettings is instance level mockable now * replace mocked and preview usages of reset + AppSettings global init with AppSettings(store: UserDefaultsMock()) * AppSettings.UserDefaultsKeys is now a RawRep struct, making it extensible * update tests with new AppSettings so it builds * update remaining AppSettings test/mock inits * AppSettings.UserDefaultsKey conformed to ExpressibleByStringLiteral * addressed issues found in first pass ai review * update AppSettings instantiations after rebase * fix accessibility tests coordinator after rebase * Rename to VolatileUserDefaults * AppSettings init uses mandatory parameter * Revert UserDefaultsKey to enum * Add TrackedUserDefaults to be able to reset the defaults object * Added volatile static func to AppSettings * Use .volatile() static func instead of full init * review fixups * Resolve build issues after rebase * updated enterprise module * remove the static reset settings call from the preview tests --------- Co-authored-by: Mauro Romito <mauro.romito@element.io>
167 lines
8.1 KiB
Swift
167 lines
8.1 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2022-2025 New Vector Ltd.
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
|
// Please see LICENSE files in the repository root for full details.
|
|
//
|
|
|
|
@testable import ElementX
|
|
import Testing
|
|
|
|
@MainActor
|
|
struct ServerSelectionScreenViewModelTests {
|
|
var clientFactory: AuthenticationClientFactoryMock!
|
|
var service: AuthenticationServiceProtocol!
|
|
var viewModel: ServerSelectionScreenViewModelProtocol!
|
|
|
|
var context: ServerSelectionScreenViewModelType.Context {
|
|
viewModel.context
|
|
}
|
|
|
|
@Test
|
|
mutating func selectForLogin() async throws {
|
|
// Given a view model for login.
|
|
setup(authenticationFlow: .login)
|
|
#expect(service.homeserver.value.loginMode == .unknown)
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 0)
|
|
|
|
// When selecting matrix.org.
|
|
context.homeserverAddress = "matrix.org"
|
|
let deferred = deferFulfillment(viewModel.actions) { $0 == .updated }
|
|
context.send(viewAction: .confirm)
|
|
try await deferred.fulfill()
|
|
|
|
// Then selection should succeed.
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 1)
|
|
#expect(service.homeserver.value == .mockMatrixDotOrg)
|
|
}
|
|
|
|
@Test
|
|
mutating func loginNotSupportedAlert() async throws {
|
|
// Given a view model for login.
|
|
setup(authenticationFlow: .login)
|
|
#expect(service.homeserver.value.loginMode == .unknown)
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 0)
|
|
#expect(context.alertInfo == nil)
|
|
|
|
// When selecting a server that doesn't support login.
|
|
context.homeserverAddress = "server.net"
|
|
let deferred = deferFulfillment(context.observe(\.alertInfo)) { $0 != nil }
|
|
context.send(viewAction: .confirm)
|
|
try await deferred.fulfill()
|
|
|
|
// Then selection should fail with an alert about not supporting registration.
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 1)
|
|
#expect(context.alertInfo?.id == .loginAlert)
|
|
}
|
|
|
|
@Test
|
|
mutating func selectForRegistration() async throws {
|
|
// Given a view model for registration.
|
|
setup(authenticationFlow: .register)
|
|
#expect(service.homeserver.value.loginMode == .unknown)
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 0)
|
|
|
|
// When selecting matrix.org.
|
|
context.homeserverAddress = "matrix.org"
|
|
let deferred = deferFulfillment(viewModel.actions) { $0 == .updated }
|
|
context.send(viewAction: .confirm)
|
|
try await deferred.fulfill()
|
|
|
|
// Then selection should succeed.
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 1)
|
|
#expect(service.homeserver.value == .mockMatrixDotOrg)
|
|
}
|
|
|
|
@Test
|
|
mutating func registrationNotSupportedAlert() async throws {
|
|
// Given a view model for registration.
|
|
setup(authenticationFlow: .register)
|
|
#expect(service.homeserver.value.loginMode == .unknown)
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 0)
|
|
#expect(context.alertInfo == nil)
|
|
|
|
// When selecting a server that doesn't support registration.
|
|
context.homeserverAddress = "example.com"
|
|
let deferred = deferFulfillment(context.observe(\.alertInfo)) { $0 != nil }
|
|
context.send(viewAction: .confirm)
|
|
try await deferred.fulfill()
|
|
|
|
// Then selection should fail with an alert about not supporting registration.
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 1)
|
|
#expect(context.alertInfo?.id == .registrationAlert)
|
|
}
|
|
|
|
@Test
|
|
mutating func elementProRequiredAlert() async throws {
|
|
// Given a view model for login.
|
|
setup(authenticationFlow: .login)
|
|
#expect(service.homeserver.value.loginMode == .unknown)
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 0)
|
|
#expect(context.alertInfo == nil)
|
|
|
|
// When selecting a server that requires Element Pro
|
|
context.homeserverAddress = "secure.gov"
|
|
let deferred = deferFulfillment(context.observe(\.alertInfo)) { $0 != nil }
|
|
context.send(viewAction: .confirm)
|
|
try await deferred.fulfill()
|
|
|
|
// Then selection should fail with an alert telling the user to download Element Pro.
|
|
#expect(clientFactory.makeClientHomeserverAddressSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount == 1)
|
|
#expect(context.alertInfo?.id == .elementProAlert)
|
|
}
|
|
|
|
@Test
|
|
mutating func invalidServer() async throws {
|
|
// Given a new instance of the view model.
|
|
setup(authenticationFlow: .login)
|
|
#expect(!context.viewState.isShowingFooterError, "There should not be an error message for a new view model.")
|
|
#expect(context.viewState.footerErrorMessage == nil, "There should not be an error message for a new view model.")
|
|
#expect(String(context.viewState.footerMessage) == L10n.screenChangeServerFormNotice,
|
|
"The standard footer message should be shown.")
|
|
|
|
// When attempting to discover an invalid server
|
|
var deferred = deferFulfillment(context.observe(\.viewState.isShowingFooterError)) { $0 }
|
|
context.homeserverAddress = "idontexist"
|
|
context.send(viewAction: .confirm)
|
|
try await deferred.fulfill()
|
|
|
|
// Then the footer should now be showing an error.
|
|
#expect(context.viewState.isShowingFooterError, "The error message should be stored.")
|
|
#expect(context.viewState.footerErrorMessage != nil, "The error message should be stored.")
|
|
#expect(String(context.viewState.footerMessage) != L10n.screenChangeServerFormNotice,
|
|
"The error message should be shown.")
|
|
|
|
// And when clearing the error.
|
|
deferred = deferFulfillment(context.observe(\.viewState.isShowingFooterError)) { !$0 }
|
|
context.homeserverAddress = ""
|
|
context.send(viewAction: .clearFooterError)
|
|
try await deferred.fulfill()
|
|
|
|
// Then the error message should now be removed.
|
|
#expect(context.viewState.footerErrorMessage == nil, "The error message should have been cleared.")
|
|
#expect(String(context.viewState.footerMessage) == L10n.screenChangeServerFormNotice,
|
|
"The standard footer message should be shown again.")
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private mutating func setup(authenticationFlow: AuthenticationFlow) {
|
|
let appSettings = AppSettings.volatile()
|
|
|
|
clientFactory = AuthenticationClientFactoryMock(configuration: .init())
|
|
service = AuthenticationService(userSessionStore: UserSessionStoreMock(configuration: .init()),
|
|
encryptionKeyProvider: EncryptionKeyProvider(),
|
|
classicAppManager: nil,
|
|
clientFactory: clientFactory,
|
|
appSettings: appSettings,
|
|
appHooks: AppHooks())
|
|
|
|
viewModel = ServerSelectionScreenViewModel(authenticationService: service,
|
|
authenticationFlow: authenticationFlow,
|
|
appSettings: appSettings,
|
|
userIndicatorController: UserIndicatorControllerMock())
|
|
}
|
|
}
|