adding unit test around polling for email verification

This commit is contained in:
Adam Brown 2022-04-27 14:52:54 +01:00
parent 074e5bcfb6
commit b2d8163aad
2 changed files with 51 additions and 3 deletions

View File

@ -27,6 +27,8 @@ import org.matrix.android.sdk.api.auth.registration.RegisterThreePid
import org.matrix.android.sdk.api.auth.registration.RegistrationWizard
import org.matrix.android.sdk.api.auth.registration.RegistrationResult as SdkResult
private const val IGNORED_DELAY = 0L
private val AN_ERROR = RuntimeException()
private val A_SESSION = FakeSession()
private val AN_EXPECTED_RESULT = RegistrationResult.Complete(A_SESSION)
private const val A_USERNAME = "a username"
@ -39,6 +41,9 @@ private val A_PID_TO_REGISTER = RegisterThreePid.Email("an email")
class RegistrationActionHandlerTest {
private val fakeRegistrationWizard = FakeRegistrationWizard()
private val registrationActionHandler = RegistrationActionHandler()
@Test
fun `when handling register action then delegates to wizard`() = runTest {
val cases = listOf(
@ -60,8 +65,6 @@ class RegistrationActionHandlerTest {
@Test
fun `given adding an email ThreePid fails with 401, when handling register action, then infer EmailSuccess`() = runTest {
val registrationActionHandler = RegistrationActionHandler()
val fakeRegistrationWizard = FakeRegistrationWizard()
fakeRegistrationWizard.givenAddEmailThreePidErrors(
cause = a401ServerError(),
email = A_PID_TO_REGISTER.email
@ -72,9 +75,40 @@ class RegistrationActionHandlerTest {
result shouldBeEqualTo RegistrationResult.SendEmailSuccess(A_PID_TO_REGISTER.email)
}
@Test
fun `given email verification errors with 401 then fatal error, when checking email validation, then continues to poll until non 401 error`() = runTest {
val errorsToThrow = listOf(
a401ServerError(),
a401ServerError(),
a401ServerError(),
AN_ERROR
)
fakeRegistrationWizard.givenCheckIfEmailHasBeenValidatedErrors(errorsToThrow)
val result = registrationActionHandler.handleRegisterAction(fakeRegistrationWizard, RegisterAction.CheckIfEmailHasBeenValidated(IGNORED_DELAY))
fakeRegistrationWizard.verifyCheckedEmailedVerification(times = errorsToThrow.size)
result shouldBeEqualTo RegistrationResult.Error(AN_ERROR)
}
@Test
fun `given email verification errors with 401 and succeeds, when checking email validation, then continues to poll until success`() = runTest {
val errorsToThrow = listOf(
a401ServerError(),
a401ServerError(),
a401ServerError()
)
fakeRegistrationWizard.givenCheckIfEmailHasBeenValidatedErrors(errorsToThrow, finally = SdkResult.Success(A_SESSION))
val result = registrationActionHandler.handleRegisterAction(fakeRegistrationWizard, RegisterAction.CheckIfEmailHasBeenValidated(IGNORED_DELAY))
fakeRegistrationWizard.verifyCheckedEmailedVerification(times = errorsToThrow.size + 1)
result shouldBeEqualTo RegistrationResult.Complete(A_SESSION)
}
private suspend fun testSuccessfulActionDelegation(case: Case) {
val registrationActionHandler = RegistrationActionHandler()
val fakeRegistrationWizard = FakeRegistrationWizard()
val registrationActionHandler = RegistrationActionHandler()
fakeRegistrationWizard.givenSuccessFor(result = A_SESSION, case.expect)
val result = registrationActionHandler.handleRegisterAction(fakeRegistrationWizard, case.action)

View File

@ -17,6 +17,7 @@
package im.vector.app.test.fakes
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import org.matrix.android.sdk.api.auth.registration.RegisterThreePid
import org.matrix.android.sdk.api.auth.registration.RegistrationResult
@ -32,4 +33,17 @@ class FakeRegistrationWizard : RegistrationWizard by mockk(relaxed = false) {
fun givenAddEmailThreePidErrors(cause: Throwable, email: String) {
coEvery { addThreePid(RegisterThreePid.Email(email)) } throws cause
}
fun givenCheckIfEmailHasBeenValidatedErrors(errors: List<Throwable>, finally: RegistrationResult? = null) {
var index = 0
coEvery { checkIfEmailHasBeenValidated(any()) } answers {
val current = index
index++
errors.getOrNull(current)?.let { throw it } ?: finally ?: throw RuntimeException("Developer error")
}
}
fun verifyCheckedEmailedVerification(times: Int) {
coVerify(exactly = times) { checkIfEmailHasBeenValidated(any()) }
}
}