adding unit test around polling for email verification
This commit is contained in:
parent
074e5bcfb6
commit
b2d8163aad
|
@ -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.RegistrationWizard
|
||||||
import org.matrix.android.sdk.api.auth.registration.RegistrationResult as SdkResult
|
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 A_SESSION = FakeSession()
|
||||||
private val AN_EXPECTED_RESULT = RegistrationResult.Complete(A_SESSION)
|
private val AN_EXPECTED_RESULT = RegistrationResult.Complete(A_SESSION)
|
||||||
private const val A_USERNAME = "a username"
|
private const val A_USERNAME = "a username"
|
||||||
|
@ -39,6 +41,9 @@ private val A_PID_TO_REGISTER = RegisterThreePid.Email("an email")
|
||||||
|
|
||||||
class RegistrationActionHandlerTest {
|
class RegistrationActionHandlerTest {
|
||||||
|
|
||||||
|
private val fakeRegistrationWizard = FakeRegistrationWizard()
|
||||||
|
private val registrationActionHandler = RegistrationActionHandler()
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when handling register action then delegates to wizard`() = runTest {
|
fun `when handling register action then delegates to wizard`() = runTest {
|
||||||
val cases = listOf(
|
val cases = listOf(
|
||||||
|
@ -60,8 +65,6 @@ class RegistrationActionHandlerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given adding an email ThreePid fails with 401, when handling register action, then infer EmailSuccess`() = runTest {
|
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(
|
fakeRegistrationWizard.givenAddEmailThreePidErrors(
|
||||||
cause = a401ServerError(),
|
cause = a401ServerError(),
|
||||||
email = A_PID_TO_REGISTER.email
|
email = A_PID_TO_REGISTER.email
|
||||||
|
@ -72,9 +75,40 @@ class RegistrationActionHandlerTest {
|
||||||
result shouldBeEqualTo RegistrationResult.SendEmailSuccess(A_PID_TO_REGISTER.email)
|
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) {
|
private suspend fun testSuccessfulActionDelegation(case: Case) {
|
||||||
val registrationActionHandler = RegistrationActionHandler()
|
|
||||||
val fakeRegistrationWizard = FakeRegistrationWizard()
|
val fakeRegistrationWizard = FakeRegistrationWizard()
|
||||||
|
val registrationActionHandler = RegistrationActionHandler()
|
||||||
fakeRegistrationWizard.givenSuccessFor(result = A_SESSION, case.expect)
|
fakeRegistrationWizard.givenSuccessFor(result = A_SESSION, case.expect)
|
||||||
|
|
||||||
val result = registrationActionHandler.handleRegisterAction(fakeRegistrationWizard, case.action)
|
val result = registrationActionHandler.handleRegisterAction(fakeRegistrationWizard, case.action)
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
package im.vector.app.test.fakes
|
package im.vector.app.test.fakes
|
||||||
|
|
||||||
import io.mockk.coEvery
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.coVerify
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import org.matrix.android.sdk.api.auth.registration.RegisterThreePid
|
import org.matrix.android.sdk.api.auth.registration.RegisterThreePid
|
||||||
import org.matrix.android.sdk.api.auth.registration.RegistrationResult
|
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) {
|
fun givenAddEmailThreePidErrors(cause: Throwable, email: String) {
|
||||||
coEvery { addThreePid(RegisterThreePid.Email(email)) } throws cause
|
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()) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue