Skip to content

Commit

Permalink
fix(2fa): login button disabled after verifiction backpress [WPB-3590] (
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorhugods authored Sep 7, 2023
1 parent 46a57b7 commit bb0ab62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ class LoginEmailViewModel @Inject constructor(
private suspend fun handleAuthenticationFailure(it: AuthenticationResult.Failure, authScope: AuthenticationScope) {
when (it) {
is AuthenticationResult.Failure.InvalidCredentials.Missing2FA -> {
loginState = loginState.updateEmailLoginEnabled()
loginState = loginState.copy(emailLoginLoading = false).updateEmailLoginEnabled()
request2FACode(authScope)
}

is AuthenticationResult.Failure.InvalidCredentials.Invalid2FA -> {
loginState = loginState.updateEmailLoginEnabled()
loginState = loginState.copy(emailLoginLoading = false).updateEmailLoginEnabled()
secondFactorVerificationCodeState = secondFactorVerificationCodeState.copy(isCurrentCodeInvalid = true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Instant
Expand Down Expand Up @@ -126,6 +128,10 @@ class LoginEmailViewModelTest {

private val userId: QualifiedID = QualifiedID("userId", "domain")

private val dispatcherProvider = TestDispatcherProvider(StandardTestDispatcher())

private fun runTest(test: suspend TestScope.() -> Unit) = runTest(dispatcherProvider.main(), testBody = test)

@BeforeEach
fun setup() {
MockKAnnotations.init(this)
Expand All @@ -152,7 +158,7 @@ class LoginEmailViewModelTest {
authServerConfigProvider,
userDataStoreProvider,
coreLogic,
TestDispatcherProvider()
dispatcherProvider
)
}

Expand Down Expand Up @@ -333,6 +339,21 @@ class LoginEmailViewModelTest {
coVerify(exactly = 1) { requestSecondFactorCodeUseCase(email, VerifiableAction.LOGIN_OR_CLIENT_REGISTRATION) }
}

@Test
fun `given missing 2fa, when logging in, then email should be enabled and not loading`() = runTest {
val email = "[email protected]"
coEvery { loginUseCase(any(), any(), any(), any(), any()) } returns AuthenticationResult.Failure.InvalidCredentials.Missing2FA
coEvery { requestSecondFactorCodeUseCase(any(), any()) } returns RequestSecondFactorVerificationCodeUseCase.Result.Success

loginViewModel.onUserIdentifierChange(TextFieldValue(email))
loginViewModel.onPasswordChange(TextFieldValue("somePassword"))
loginViewModel.login(onSuccess)
advanceUntilIdle()

loginViewModel.loginState.emailLoginLoading shouldBe false
loginViewModel.loginState.emailLoginEnabled shouldBe true
}

@Test
fun `given login fails with 2fa missing and 2fa request succeeds, when logging in, then should request user input`() = runTest {
val email = "[email protected]"
Expand Down

0 comments on commit bb0ab62

Please sign in to comment.