adding missing loading state when confirming password reset

- adds reset test cases to the onboarding view model
This commit is contained in:
Adam Brown 2022-05-30 13:51:33 +01:00
parent fa5b7c66ca
commit edfabb0f26
6 changed files with 82 additions and 3 deletions

View file

@ -30,7 +30,6 @@ import org.matrix.android.sdk.internal.auth.data.ThreePidMedium
import org.matrix.android.sdk.internal.auth.data.TokenLoginParams
import org.matrix.android.sdk.internal.auth.db.PendingSessionData
import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistrationParams
import org.matrix.android.sdk.internal.auth.registration.AddThreePidRegistrationResponse
import org.matrix.android.sdk.internal.auth.registration.RegisterAddThreePidTask
import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.session.content.DefaultContentUrlResolver

View file

@ -533,12 +533,13 @@ class LoginViewModel @AssistedInject constructor(
currentJob = viewModelScope.launch {
val state = awaitState()
if (state.resetPasswordNewPassword == null)
if (state.resetPasswordNewPassword == null) {
setState {
copy(
asyncResetPassword = Uninitialized,
asyncResetMailConfirmed = Fail(Throwable("Developer error - New password not set"))
)
}
} else {
try {
safeLoginWizard.resetPasswordMailConfirmed(state.resetPasswordNewPassword)

View file

@ -462,6 +462,7 @@ class OnboardingViewModel @AssistedInject constructor(
}
private fun handleResetPasswordMailConfirmed() {
setState { copy(isLoading = true) }
currentJob = viewModelScope.launch {
val resetState = awaitState().resetState
when (val newPassword = resetState.newPassword) {

View file

@ -31,6 +31,7 @@ import im.vector.app.test.fakes.FakeContext
import im.vector.app.test.fakes.FakeDirectLoginUseCase
import im.vector.app.test.fakes.FakeHomeServerConnectionConfigFactory
import im.vector.app.test.fakes.FakeHomeServerHistoryService
import im.vector.app.test.fakes.FakeLoginWizard
import im.vector.app.test.fakes.FakeRegisterActionHandler
import im.vector.app.test.fakes.FakeRegistrationWizard
import im.vector.app.test.fakes.FakeSession
@ -67,6 +68,8 @@ private val A_DIRECT_LOGIN = OnboardingAction.AuthenticateAction.LoginDirect("@a
private const val A_HOMESERVER_URL = "https://edited-homeserver.org"
private val A_HOMESERVER_CONFIG = HomeServerConnectionConfig(FakeUri().instance)
private val SELECTED_HOMESERVER_STATE = SelectedHomeserverState(preferredLoginMode = LoginMode.Password)
private const val AN_EMAIL = "hello@example.com"
private const val A_PASSWORD = "a-password"
class OnboardingViewModelTest {
@ -85,6 +88,7 @@ class OnboardingViewModelTest {
private val fakeHomeServerConnectionConfigFactory = FakeHomeServerConnectionConfigFactory()
private val fakeStartAuthenticationFlowUseCase = FakeStartAuthenticationFlowUseCase()
private val fakeHomeServerHistoryService = FakeHomeServerHistoryService()
private val fakeLoginWizard = FakeLoginWizard()
private var initialState = OnboardingViewState()
private lateinit var viewModel: OnboardingViewModel
@ -466,6 +470,43 @@ class OnboardingViewModelTest {
.finish()
}
@Test
fun `given can successfully reset password, when resetting password, then emits reset done event`() = runTest {
val test = viewModel.test()
fakeLoginWizard.givenResetPasswordSuccess(AN_EMAIL)
fakeAuthenticationService.givenLoginWizard(fakeLoginWizard)
viewModel.handle(OnboardingAction.ResetPassword(email = AN_EMAIL, newPassword = A_PASSWORD))
test
.assertStatesChanges(
initialState,
{ copy(isLoading = true) },
{ copy(isLoading = false, resetState = ResetState(AN_EMAIL, A_PASSWORD)) }
)
.assertEvents(OnboardingViewEvents.OnResetPasswordSendThreePidDone)
.finish()
}
@Test
fun `given can successfully confirm reset password, when confirm reset password, then emits reset success`() = runTest {
viewModelWith(initialState.copy(resetState = ResetState(AN_EMAIL, A_PASSWORD)))
val test = viewModel.test()
fakeLoginWizard.givenConfirmResetPasswordSuccess(A_PASSWORD)
fakeAuthenticationService.givenLoginWizard(fakeLoginWizard)
viewModel.handle(OnboardingAction.ResetPasswordMailConfirmed)
test
.assertStatesChanges(
initialState,
{ copy(isLoading = true) },
{ copy(isLoading = false, resetState = ResetState()) }
)
.assertEvents(OnboardingViewEvents.OnResetPasswordMailConfirmationSuccess)
.finish()
}
private fun viewModelWith(state: OnboardingViewState) {
OnboardingViewModel(
state,

View file

@ -23,6 +23,7 @@ import io.mockk.mockk
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.auth.data.LoginFlowResult
import org.matrix.android.sdk.api.auth.login.LoginWizard
import org.matrix.android.sdk.api.auth.registration.RegistrationWizard
import org.matrix.android.sdk.api.auth.wellknown.WellknownResult
@ -36,6 +37,10 @@ class FakeAuthenticationService : AuthenticationService by mockk() {
every { isRegistrationStarted() } returns started
}
fun givenLoginWizard(loginWizard: LoginWizard) {
every { getLoginWizard() } returns loginWizard
}
fun givenLoginFlow(config: HomeServerConnectionConfig, result: LoginFlowResult) {
coEvery { getLoginFlow(config) } returns result
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.test.fakes
import io.mockk.coJustRun
import io.mockk.mockk
import org.matrix.android.sdk.api.auth.login.LoginWizard
class FakeLoginWizard : LoginWizard by mockk() {
fun givenResetPasswordSuccess(email: String) {
coJustRun { resetPassword(email) }
}
fun givenConfirmResetPasswordSuccess(password: String) {
coJustRun { resetPasswordMailConfirmed(password) }
}
}