handling msisdn 401 errors as success within the registration wizard delegate

This commit is contained in:
Adam Brown 2022-05-20 11:47:37 +01:00
parent 7617309058
commit 27b1bc9e66
6 changed files with 33 additions and 18 deletions

View File

@ -305,6 +305,7 @@ class OnboardingViewModel @AssistedInject constructor(
RegistrationActionHandler.Result.StartRegistration -> _viewEvents.post(OnboardingViewEvents.DisplayStartRegistration)
RegistrationActionHandler.Result.UnsupportedStage -> _viewEvents.post(OnboardingViewEvents.DisplayRegistrationFallback)
is RegistrationActionHandler.Result.SendEmailSuccess -> _viewEvents.post(OnboardingViewEvents.OnSendEmailSuccess(it.email))
is RegistrationActionHandler.Result.SendMsisdnSuccess -> _viewEvents.post(OnboardingViewEvents.OnSendMsisdnSuccess(it.msisdn.msisdn))
is RegistrationActionHandler.Result.Error -> _viewEvents.post(OnboardingViewEvents.Failure(it.cause))
RegistrationActionHandler.Result.MissingNextStage -> {
_viewEvents.post(OnboardingViewEvents.Failure(IllegalStateException("No next registration stage found")))

View File

@ -26,6 +26,7 @@ import im.vector.app.features.onboarding.ftueauth.MatrixOrgRegistrationStagesCom
import kotlinx.coroutines.flow.first
import org.matrix.android.sdk.api.auth.AuthenticationService
import org.matrix.android.sdk.api.auth.registration.FlowResult
import org.matrix.android.sdk.api.auth.registration.RegisterThreePid
import org.matrix.android.sdk.api.auth.registration.Stage
import org.matrix.android.sdk.api.session.Session
import javax.inject.Inject
@ -47,7 +48,8 @@ class RegistrationActionHandler @Inject constructor(
else -> when (result) {
is RegistrationResult.Complete -> Result.RegistrationComplete(result.session)
is RegistrationResult.NextStep -> processFlowResult(result, state)
is RegistrationResult.SendEmailSuccess -> Result.SendEmailSuccess(result.email)
is RegistrationResult.SendEmailSuccess -> Result.SendEmailSuccess(result.email.email)
is RegistrationResult.SendMsisdnSuccess -> Result.SendMsisdnSuccess(result.msisdn)
is RegistrationResult.Error -> Result.Error(result.cause)
}
}
@ -95,6 +97,7 @@ class RegistrationActionHandler @Inject constructor(
data class NextStage(val stage: Stage) : Result
data class Error(val cause: Throwable) : Result
data class SendEmailSuccess(val email: String) : Result
data class SendMsisdnSuccess(val msisdn: RegisterThreePid.Msisdn) : Result
object MissingNextStage : Result
object StartRegistration : Result
object UnsupportedStage : Result

View File

@ -24,6 +24,7 @@ import org.matrix.android.sdk.api.auth.registration.RegistrationResult.Success
import org.matrix.android.sdk.api.auth.registration.RegistrationWizard
import org.matrix.android.sdk.api.failure.is401
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.identity.ThreePid
import javax.inject.Inject
import org.matrix.android.sdk.api.auth.registration.RegistrationResult as MatrixRegistrationResult
@ -59,7 +60,8 @@ class RegistrationWizardActionDelegate @Inject constructor(
onSuccess = { it.toRegistrationResult() },
onFailure = {
when {
action.threePid is RegisterThreePid.Email && it.is401() -> RegistrationResult.SendEmailSuccess(action.threePid.email)
action.threePid is RegisterThreePid.Email && it.is401() -> RegistrationResult.SendEmailSuccess(action.threePid)
action.threePid is RegisterThreePid.Msisdn && it.is401() -> RegistrationResult.SendMsisdnSuccess(action.threePid)
else -> RegistrationResult.Error(it)
}
}
@ -95,7 +97,8 @@ sealed interface RegistrationResult {
data class Error(val cause: Throwable) : RegistrationResult
data class Complete(val session: Session) : RegistrationResult
data class NextStep(val flowResult: FlowResult) : RegistrationResult
data class SendEmailSuccess(val email: String) : RegistrationResult
data class SendEmailSuccess(val email: RegisterThreePid.Email) : RegistrationResult
data class SendMsisdnSuccess(val msisdn: RegisterThreePid.Msisdn) : RegistrationResult
}
sealed interface RegisterAction {

View File

@ -226,13 +226,8 @@ class FtueAuthGenericTextInputFormFragment @Inject constructor() : AbstractFtueA
views.loginGenericTextInputFormTil.error = errorFormatter.toHumanReadable(throwable)
}
TextInputFormFragmentMode.SetMsisdn -> {
if (throwable.is401()) {
// This is normal use case, we go to the enter code screen
viewModel.handle(OnboardingAction.PostViewEvent(OnboardingViewEvents.OnSendMsisdnSuccess(viewModel.currentThreePid ?: "")))
} else {
views.loginGenericTextInputFormTil.error = errorFormatter.toHumanReadable(throwable)
}
}
TextInputFormFragmentMode.ConfirmMsisdn -> {
when {
throwable is Failure.SuccessError ->

View File

@ -38,7 +38,8 @@ private const val AN_INITIAL_DEVICE_NAME = "a device name"
private const val A_CAPTCHA_RESPONSE = "a captcha response"
private const val A_PID_CODE = "a pid code"
private const val EMAIL_VALIDATED_DELAY = 10000L
private val A_PID_TO_REGISTER = RegisterThreePid.Email("an email")
private val AN_EMAIL_PID_TO_REGISTER = RegisterThreePid.Email("an email")
private val A_PHONE_PID_TO_REGISTER = RegisterThreePid.Msisdn("+11111111111", countryCode = "GB")
class RegistrationWizardActionDelegateTest {
@ -55,7 +56,7 @@ class RegistrationWizardActionDelegateTest {
case(RegisterAction.CaptchaDone(A_CAPTCHA_RESPONSE)) { performReCaptcha(A_CAPTCHA_RESPONSE) },
case(RegisterAction.AcceptTerms) { acceptTerms() },
case(RegisterAction.RegisterDummy) { dummy() },
case(RegisterAction.AddThreePid(A_PID_TO_REGISTER)) { addThreePid(A_PID_TO_REGISTER) },
case(RegisterAction.AddThreePid(AN_EMAIL_PID_TO_REGISTER)) { addThreePid(AN_EMAIL_PID_TO_REGISTER) },
case(RegisterAction.SendAgainThreePid) { sendAgainThreePid() },
case(RegisterAction.ValidateThreePid(A_PID_CODE)) { handleValidateThreePid(A_PID_CODE) },
case(RegisterAction.CheckIfEmailHasBeenValidated(EMAIL_VALIDATED_DELAY)) { checkIfEmailHasBeenValidated(EMAIL_VALIDATED_DELAY) },
@ -69,14 +70,14 @@ class RegistrationWizardActionDelegateTest {
@Test
fun `given adding an email ThreePid fails with 401, when handling register action, then infer EmailSuccess`() = runTest {
fakeRegistrationWizard.givenAddEmailThreePidErrors(
fakeRegistrationWizard.givenAddThreePidErrors(
cause = a401ServerError(),
email = A_PID_TO_REGISTER.email
threePid = AN_EMAIL_PID_TO_REGISTER
)
val result = registrationActionHandler.executeAction(RegisterAction.AddThreePid(A_PID_TO_REGISTER))
val result = registrationActionHandler.executeAction(RegisterAction.AddThreePid(AN_EMAIL_PID_TO_REGISTER))
result shouldBeEqualTo RegistrationResult.SendEmailSuccess(A_PID_TO_REGISTER.email)
result shouldBeEqualTo RegistrationResult.SendEmailSuccess(AN_EMAIL_PID_TO_REGISTER)
}
@Test
@ -110,6 +111,18 @@ class RegistrationWizardActionDelegateTest {
result shouldBeEqualTo RegistrationResult.Complete(A_SESSION)
}
@Test
fun `given adding an Msisdn ThreePid fails with 401, when handling register action, then infer EmailSuccess`() = runTest {
fakeRegistrationWizard.givenAddThreePidErrors(
cause = a401ServerError(),
threePid = A_PHONE_PID_TO_REGISTER
)
val result = registrationActionHandler.executeAction(RegisterAction.AddThreePid(A_PHONE_PID_TO_REGISTER))
result shouldBeEqualTo RegistrationResult.SendMsisdnSuccess(A_PHONE_PID_TO_REGISTER)
}
private suspend fun testSuccessfulActionDelegation(case: Case) {
val fakeRegistrationWizard = FakeRegistrationWizard()
val authenticationService = FakeAuthenticationService().also { it.givenRegistrationWizard(fakeRegistrationWizard) }

View File

@ -30,8 +30,8 @@ class FakeRegistrationWizard : RegistrationWizard by mockk(relaxed = false) {
coEvery { expect(this@FakeRegistrationWizard) } returns RegistrationResult.Success(result)
}
fun givenAddEmailThreePidErrors(cause: Throwable, email: String) {
coEvery { addThreePid(RegisterThreePid.Email(email)) } throws cause
fun givenAddThreePidErrors(cause: Throwable, threePid: RegisterThreePid) {
coEvery { addThreePid(threePid) } throws cause
}
fun givenCheckIfEmailHasBeenValidatedErrors(errors: List<Throwable>, finally: RegistrationResult? = null) {