From a4ea47e7409ba6e8eff1fe4a276fae8350f39fe6 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 4 Aug 2022 11:58:31 +0100 Subject: [PATCH] catching username availabilty exceptions and handling as user facing error --- .../onboarding/OnboardingViewModel.kt | 43 +++++++++++-------- .../onboarding/OnboardingViewModelTest.kt | 14 ++++++ .../app/test/fakes/FakeRegistrationWizard.kt | 4 ++ 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt index 8136dc379b..73288bd6d5 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt @@ -193,25 +193,32 @@ class OnboardingViewModel @AssistedInject constructor( } private suspend fun checkUserNameAvailability(userName: String) { - when (val result = registrationWizard.registrationAvailable(userName)) { - RegistrationAvailability.Available -> { - setState { - copy( - registrationState = RegistrationState( - isUserNameAvailable = true, - selectedMatrixId = when { - userName.isMatrixId() -> userName - else -> "@$userName:${selectedHomeserver.userFacingUrl.toReducedUrl()}" - }, - ) - ) - } - } + runCatching { registrationWizard.registrationAvailable(userName) }.fold( + onSuccess = { result -> + when (result) { + RegistrationAvailability.Available -> { + setState { + copy( + registrationState = RegistrationState( + isUserNameAvailable = true, + selectedMatrixId = when { + userName.isMatrixId() -> userName + else -> "@$userName:${selectedHomeserver.userFacingUrl.toReducedUrl()}" + }, + ) + ) + } + } - is RegistrationAvailability.NotAvailable -> { - _viewEvents.post(OnboardingViewEvents.Failure(result.failure)) - } - } + is RegistrationAvailability.NotAvailable -> { + _viewEvents.post(OnboardingViewEvents.Failure(result.failure)) + } + } + }, + onFailure = { + _viewEvents.post(OnboardingViewEvents.Failure(it)) + } + ) } private fun withAction(action: OnboardingAction, block: (OnboardingAction) -> Unit) { diff --git a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt index 61d3101b64..f734a62cf8 100644 --- a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt @@ -390,6 +390,20 @@ class OnboardingViewModelTest { .finish() } + @Test + fun `given available username throws, when a register username is entered, then emits error`() = runTest { + viewModelWith(initialRegistrationState(A_HOMESERVER_URL)) + fakeAuthenticationService.givenRegistrationWizard(FakeRegistrationWizard().also { it.givenUserNameIsAvailableThrows(A_USERNAME, AN_ERROR) }) + val test = viewModel.test() + + viewModel.handle(OnboardingAction.UserNameEnteredAction.Registration(A_USERNAME)) + + test + .assertStates(initialState) + .assertEvents(OnboardingViewEvents.Failure(AN_ERROR)) + .finish() + } + @Test fun `given available username, when a register username is entered, then emits available registration state`() = runTest { viewModelWith(initialRegistrationState(A_HOMESERVER_URL)) diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeRegistrationWizard.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeRegistrationWizard.kt index 4f0b1fe083..6cacd6c1e5 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeRegistrationWizard.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeRegistrationWizard.kt @@ -57,6 +57,10 @@ class FakeRegistrationWizard : RegistrationWizard by mockk(relaxed = false) { coEvery { registrationAvailable(userName) } returns RegistrationAvailability.Available } + fun givenUserNameIsAvailableThrows(userName: String, cause: Throwable) { + coEvery { registrationAvailable(userName) } throws cause + } + fun givenUserNameIsUnavailable(userName: String, failure: Failure.ServerError) { coEvery { registrationAvailable(userName) } returns RegistrationAvailability.NotAvailable(failure) }