mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
Selecting a server that doesn't support login now fails instead of letting you continue to a failure later. (#3342)
* Fail configuration of the authentication service if the homeserver doesn't support login. * Move the ServerSelectionCoordinator logic into the ViewModel. - Handle the new login alert. - Add more tests
This commit is contained in:
@@ -89,7 +89,7 @@ class ServerConfirmationScreenViewModelTests: XCTestCase {
|
||||
|
||||
func testRegistrationNotSupportedAlert() async throws {
|
||||
// Given a view model for registration using a service that hasn't been configured and the default server doesn't support registration.
|
||||
setupViewModel(authenticationFlow: .register, elementWellKnown: false)
|
||||
setupViewModel(authenticationFlow: .register, supportsRegistrationHelper: false)
|
||||
XCTAssertEqual(service.homeserver.value.loginMode, .unknown)
|
||||
XCTAssertFalse(service.homeserver.value.supportsRegistration)
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 0)
|
||||
@@ -105,10 +105,34 @@ class ServerConfirmationScreenViewModelTests: XCTestCase {
|
||||
XCTAssertEqual(context.alertInfo?.id, .registration)
|
||||
}
|
||||
|
||||
func testLoginNotSupportedAlert() async throws {
|
||||
// Given a view model for login using a service that hasn't been configured and the default server doesn't support login.
|
||||
setupViewModel(authenticationFlow: .login, supportsRegistrationHelper: false, supportsPasswordLogin: false)
|
||||
XCTAssertEqual(service.homeserver.value.loginMode, .unknown)
|
||||
XCTAssertFalse(service.homeserver.value.supportsRegistration)
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 0)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
// When continuing from the confirmation screen.
|
||||
let deferred = deferFulfillment(context.$viewState) { $0.bindings.alertInfo != nil }
|
||||
context.send(viewAction: .confirm)
|
||||
try await deferred.fulfill()
|
||||
|
||||
// Then the configuration should fail with an alert about not supporting login.
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 1)
|
||||
XCTAssertEqual(context.alertInfo?.id, .login)
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func setupViewModel(authenticationFlow: AuthenticationFlow, elementWellKnown: Bool = true) {
|
||||
let client = ClientSDKMock(configuration: elementWellKnown ? .init() : .init(elementWellKnown: ""))
|
||||
private func setupViewModel(authenticationFlow: AuthenticationFlow, supportsRegistrationHelper: Bool = true, supportsPasswordLogin: Bool = true) {
|
||||
// Manually create a configuration as the default homeserver address setting is immutable.
|
||||
let clientConfiguration: ClientSDKMock.Configuration = if supportsRegistrationHelper {
|
||||
.init(supportsPasswordLogin: supportsPasswordLogin)
|
||||
} else {
|
||||
.init(supportsPasswordLogin: supportsPasswordLogin, elementWellKnown: "")
|
||||
}
|
||||
let client = ClientSDKMock(configuration: clientConfiguration)
|
||||
let configuration = AuthenticationClientBuilderMock.Configuration(homeserverClients: ["matrix.org": client],
|
||||
qrCodeClient: client)
|
||||
|
||||
|
||||
@@ -11,38 +11,131 @@ import XCTest
|
||||
|
||||
@MainActor
|
||||
class ServerSelectionViewModelTests: XCTestCase {
|
||||
var viewModel: ServerSelectionScreenViewModelProtocol!
|
||||
var context: ServerSelectionScreenViewModelType.Context!
|
||||
var clientBuilderFactory: AuthenticationClientBuilderFactoryMock!
|
||||
var service: AuthenticationServiceProtocol!
|
||||
|
||||
@MainActor override func setUp() {
|
||||
viewModel = ServerSelectionScreenViewModel(homeserverAddress: "",
|
||||
slidingSyncLearnMoreURL: ServiceLocator.shared.settings.slidingSyncLearnMoreURL)
|
||||
context = viewModel.context
|
||||
var viewModel: ServerSelectionScreenViewModelProtocol!
|
||||
var context: ServerSelectionScreenViewModelType.Context { viewModel.context }
|
||||
|
||||
func testSelectForLogin() async throws {
|
||||
// Given a view model for login.
|
||||
setupViewModel(authenticationFlow: .login)
|
||||
XCTAssertEqual(service.homeserver.value.loginMode, .unknown)
|
||||
XCTAssertFalse(service.homeserver.value.supportsRegistration)
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 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.
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 1)
|
||||
XCTAssertEqual(service.homeserver.value, .mockMatrixDotOrg)
|
||||
}
|
||||
|
||||
func testErrorMessage() async throws {
|
||||
|
||||
func testLoginNotSupportedAlert() async throws {
|
||||
// Given a view model for login.
|
||||
setupViewModel(authenticationFlow: .login)
|
||||
XCTAssertEqual(service.homeserver.value.loginMode, .unknown)
|
||||
XCTAssertFalse(service.homeserver.value.supportsRegistration)
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 0)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
// When selecting a server that doesn't support login.
|
||||
context.homeserverAddress = "server.net"
|
||||
let deferred = deferFulfillment(context.$viewState) { $0.bindings.alertInfo != nil }
|
||||
context.send(viewAction: .confirm)
|
||||
try await deferred.fulfill()
|
||||
|
||||
// Then selection should fail with an alert about not supporting registration.
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 1)
|
||||
XCTAssertEqual(context.alertInfo?.id, .loginAlert)
|
||||
}
|
||||
|
||||
func testSelectForRegistration() async throws {
|
||||
// Given a view model for registration.
|
||||
setupViewModel(authenticationFlow: .register)
|
||||
XCTAssertEqual(service.homeserver.value.loginMode, .unknown)
|
||||
XCTAssertFalse(service.homeserver.value.supportsRegistration)
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 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.
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 1)
|
||||
XCTAssertEqual(service.homeserver.value, .mockMatrixDotOrg)
|
||||
}
|
||||
|
||||
func testRegistrationNotSupportedAlert() async throws {
|
||||
// Given a view model for registration.
|
||||
setupViewModel(authenticationFlow: .register)
|
||||
XCTAssertEqual(service.homeserver.value.loginMode, .unknown)
|
||||
XCTAssertFalse(service.homeserver.value.supportsRegistration)
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 0)
|
||||
XCTAssertNil(context.alertInfo)
|
||||
|
||||
// When selecting a server that doesn't support registration.
|
||||
context.homeserverAddress = "example.com"
|
||||
let deferred = deferFulfillment(context.$viewState) { $0.bindings.alertInfo != nil }
|
||||
context.send(viewAction: .confirm)
|
||||
try await deferred.fulfill()
|
||||
|
||||
// Then selection should fail with an alert about not supporting registration.
|
||||
XCTAssertEqual(clientBuilderFactory.makeBuilderSessionDirectoriesPassphraseClientSessionDelegateAppSettingsAppHooksCallsCount, 1)
|
||||
XCTAssertEqual(context.alertInfo?.id, .registrationAlert)
|
||||
}
|
||||
|
||||
func testInvalidServer() async throws {
|
||||
// Given a new instance of the view model.
|
||||
setupViewModel(authenticationFlow: .login)
|
||||
XCTAssertFalse(context.viewState.isShowingFooterError, "There should not be an error message for a new view model.")
|
||||
XCTAssertNil(context.viewState.footerErrorMessage, "There should not be an error message for a new view model.")
|
||||
XCTAssertEqual(String(context.viewState.footerMessage.characters), L10n.screenChangeServerFormNotice(L10n.actionLearnMore),
|
||||
"The standard footer message should be shown.")
|
||||
|
||||
// When an error occurs.
|
||||
let message = "Unable to contact server."
|
||||
viewModel.displayError(.footerMessage(message))
|
||||
// When attempting to discover an invalid server
|
||||
var deferred = deferFulfillment(context.$viewState) { $0.isShowingFooterError }
|
||||
context.homeserverAddress = "idontexist"
|
||||
context.send(viewAction: .confirm)
|
||||
try await deferred.fulfill()
|
||||
|
||||
// Then the footer should now be showing an error.
|
||||
XCTAssertEqual(context.viewState.footerErrorMessage, message, "The error message should be stored.")
|
||||
XCTAssertEqual(String(context.viewState.footerMessage.characters), message, "The error message should be shown.")
|
||||
XCTAssertTrue(context.viewState.isShowingFooterError, "The error message should be stored.")
|
||||
XCTAssertNotNil(context.viewState.footerErrorMessage, "The error message should be stored.")
|
||||
XCTAssertNotEqual(String(context.viewState.footerMessage.characters), L10n.screenChangeServerFormNotice(L10n.actionLearnMore),
|
||||
"The error message should be shown.")
|
||||
|
||||
// And when clearing the error.
|
||||
deferred = deferFulfillment(context.$viewState) { !$0.isShowingFooterError }
|
||||
context.homeserverAddress = ""
|
||||
context.send(viewAction: .clearFooterError)
|
||||
|
||||
// Wait for the action to spawn a Task.
|
||||
await Task.yield()
|
||||
try await deferred.fulfill()
|
||||
|
||||
// Then the error message should now be removed.
|
||||
XCTAssertNil(context.viewState.footerErrorMessage, "The error message should have been cleared.")
|
||||
XCTAssertEqual(String(context.viewState.footerMessage.characters), L10n.screenChangeServerFormNotice(L10n.actionLearnMore),
|
||||
"The standard footer message should be shown again.")
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func setupViewModel(authenticationFlow: AuthenticationFlow) {
|
||||
clientBuilderFactory = AuthenticationClientBuilderFactoryMock(configuration: .init())
|
||||
service = AuthenticationService(userSessionStore: UserSessionStoreMock(configuration: .init()),
|
||||
encryptionKeyProvider: EncryptionKeyProvider(),
|
||||
clientBuilderFactory: clientBuilderFactory,
|
||||
appSettings: ServiceLocator.shared.settings,
|
||||
appHooks: AppHooks())
|
||||
|
||||
viewModel = ServerSelectionScreenViewModel(authenticationService: service,
|
||||
authenticationFlow: authenticationFlow,
|
||||
slidingSyncLearnMoreURL: ServiceLocator.shared.settings.slidingSyncLearnMoreURL,
|
||||
userIndicatorController: UserIndicatorControllerMock())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user