From 680196b83778496945eb6e215a3b133307ab6938 Mon Sep 17 00:00:00 2001 From: Hicham Boushaba Date: Thu, 3 Oct 2024 10:38:34 +0100 Subject: [PATCH 1/2] Pass the correct authorization URL to the application passwords tutorial --- .../LoginSiteCredentialsViewModel.kt | 2 +- .../LoginSiteCredentialsViewModelTest.kt | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModel.kt index 3acf1d422fe..cfe241dae71 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModel.kt @@ -312,7 +312,7 @@ class LoginSiteCredentialsViewModel @Inject constructor( if (site.fullAuthorizationUrl.isNotNullOrEmpty()) { triggerEvent( ShowApplicationPasswordTutorialScreen( - url = site.applicationPasswordsAuthorizeUrl, + url = site.fullAuthorizationUrl!!, errorMessage = errorMessage ) ) diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModelTest.kt index 2214017c931..d253ea464ab 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModelTest.kt @@ -15,6 +15,7 @@ import com.woocommerce.android.ui.login.WPApiSiteRepository.CookieNonceAuthentic import com.woocommerce.android.ui.login.sitecredentials.LoginSiteCredentialsViewModel.LoggedIn import com.woocommerce.android.ui.login.sitecredentials.LoginSiteCredentialsViewModel.ShowApplicationPasswordsUnavailableScreen import com.woocommerce.android.ui.login.sitecredentials.LoginSiteCredentialsViewModel.ShowNonWooErrorScreen +import com.woocommerce.android.util.getOrAwaitValue import com.woocommerce.android.util.observeForTesting import com.woocommerce.android.util.runAndCaptureValues import com.woocommerce.android.viewmodel.BaseUnitTest @@ -386,4 +387,23 @@ class LoginSiteCredentialsViewModelTest : BaseUnitTest() { assertThat(viewModel.event.value) .isEqualTo(ShowApplicationPasswordsUnavailableScreen(siteAddress, isJetpackConnected)) } + + @Test + fun `given application passwords enabled and login fails for an unknown reason, when user attempts to sign-in, then show WebView login flow`() = testBlocking { + setup { + whenever(wpApiSiteRepository.login(siteAddress, testUsername, testPassword)) + .thenReturn(Result.failure(Exception())) + } + + val event = viewModel.event.runAndCaptureValues { + viewModel.onUsernameChanged(testUsername) + viewModel.onPasswordChanged(testPassword) + viewModel.viewState.getOrAwaitValue() + viewModel.onContinueClick() + }.last() + + assertThat(event).isInstanceOf(LoginSiteCredentialsViewModel.ShowApplicationPasswordTutorialScreen::class.java) + assertThat((event as LoginSiteCredentialsViewModel.ShowApplicationPasswordTutorialScreen).url) + .isEqualTo(urlAuthFull) + } } From 2946ca4c58d7d2d6367f0b25d987c78f0279ea32 Mon Sep 17 00:00:00 2001 From: Hicham Boushaba Date: Thu, 3 Oct 2024 12:25:42 +0100 Subject: [PATCH 2/2] Fix unit test --- .../login/sitecredentials/LoginSiteCredentialsViewModelTest.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModelTest.kt index d253ea464ab..03961ae494a 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/login/sitecredentials/LoginSiteCredentialsViewModelTest.kt @@ -393,6 +393,8 @@ class LoginSiteCredentialsViewModelTest : BaseUnitTest() { setup { whenever(wpApiSiteRepository.login(siteAddress, testUsername, testPassword)) .thenReturn(Result.failure(Exception())) + whenever(wpApiSiteRepository.fetchSite(siteAddress, testUsername, testPassword)) + .thenReturn(Result.success(testSite.apply { applicationPasswordsAuthorizeUrl = urlAuthBase })) } val event = viewModel.event.runAndCaptureValues {