adding test cases around certificate errors being thrown and mapped

This commit is contained in:
Adam Brown 2022-08-18 11:22:38 +01:00
parent e4a08d1be1
commit 91176eca22
3 changed files with 66 additions and 1 deletions

View file

@ -48,6 +48,7 @@ import im.vector.app.test.fakes.FakeVectorOverrides
import im.vector.app.test.fakes.toTestString import im.vector.app.test.fakes.toTestString
import im.vector.app.test.fixtures.a401ServerError import im.vector.app.test.fixtures.a401ServerError
import im.vector.app.test.fixtures.aHomeServerCapabilities import im.vector.app.test.fixtures.aHomeServerCapabilities
import im.vector.app.test.fixtures.anUnrecognisedCertificateError
import im.vector.app.test.test import im.vector.app.test.test
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo import org.amshove.kluent.shouldBeEqualTo
@ -66,6 +67,7 @@ private const val A_DISPLAY_NAME = "a display name"
private const val A_PICTURE_FILENAME = "a-picture.png" private const val A_PICTURE_FILENAME = "a-picture.png"
private val A_SERVER_ERROR = a401ServerError() private val A_SERVER_ERROR = a401ServerError()
private val AN_ERROR = RuntimeException("an error!") private val AN_ERROR = RuntimeException("an error!")
private val AN_UNRECOGNISED_CERTIFICATE_ERROR = anUnrecognisedCertificateError()
private val A_LOADABLE_REGISTER_ACTION = RegisterAction.StartRegistration private val A_LOADABLE_REGISTER_ACTION = RegisterAction.StartRegistration
private val A_NON_LOADABLE_REGISTER_ACTION = RegisterAction.CheckIfEmailHasBeenValidated(delayMillis = -1L) private val A_NON_LOADABLE_REGISTER_ACTION = RegisterAction.CheckIfEmailHasBeenValidated(delayMillis = -1L)
private val A_RESULT_IGNORED_REGISTER_ACTION = RegisterAction.SendAgainThreePid private val A_RESULT_IGNORED_REGISTER_ACTION = RegisterAction.SendAgainThreePid
@ -322,6 +324,25 @@ class OnboardingViewModelTest {
.finish() .finish()
} }
@Test
fun `given has sign in with matrix id sign mode, when handling login or register action fails with certificate error, then emits error`() = runTest {
viewModelWith(initialState.copy(signMode = SignMode.SignInWithMatrixId))
fakeDirectLoginUseCase.givenFailureResult(A_DIRECT_LOGIN, config = null, cause = AN_UNRECOGNISED_CERTIFICATE_ERROR)
givenInitialisesSession(fakeSession)
val test = viewModel.test()
viewModel.handle(A_DIRECT_LOGIN)
test
.assertStatesChanges(
initialState,
{ copy(isLoading = true) },
{ copy(isLoading = false) }
)
.assertEvents(OnboardingViewEvents.UnrecognisedCertificateFailure(A_DIRECT_LOGIN, AN_UNRECOGNISED_CERTIFICATE_ERROR))
.finish()
}
@Test @Test
fun `when handling SignUp then sets sign mode to sign up and starts registration`() = runTest { fun `when handling SignUp then sets sign mode to sign up and starts registration`() = runTest {
givenRegistrationResultFor(RegisterAction.StartRegistration, ANY_CONTINUING_REGISTRATION_RESULT) givenRegistrationResultFor(RegisterAction.StartRegistration, ANY_CONTINUING_REGISTRATION_RESULT)
@ -550,6 +571,44 @@ class OnboardingViewModelTest {
.finish() .finish()
} }
@Test
fun `when editing homeserver errors with certificate error, then emits error`() = runTest {
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprint = null, A_HOMESERVER_CONFIG)
fakeStartAuthenticationFlowUseCase.givenErrors(A_HOMESERVER_CONFIG, AN_UNRECOGNISED_CERTIFICATE_ERROR)
val editAction = OnboardingAction.HomeServerChange.EditHomeServer(A_HOMESERVER_URL)
val test = viewModel.test()
viewModel.handle(editAction)
test
.assertStatesChanges(
initialState,
{ copy(isLoading = true) },
{ copy(isLoading = false) }
)
.assertEvents(OnboardingViewEvents.UnrecognisedCertificateFailure(editAction, AN_UNRECOGNISED_CERTIFICATE_ERROR))
.finish()
}
@Test
fun `when selecting homeserver errors with certificate error, then emits error`() = runTest {
fakeHomeServerConnectionConfigFactory.givenConfigFor(A_HOMESERVER_URL, fingerprint = null, A_HOMESERVER_CONFIG)
fakeStartAuthenticationFlowUseCase.givenErrors(A_HOMESERVER_CONFIG, AN_UNRECOGNISED_CERTIFICATE_ERROR)
val selectAction = OnboardingAction.HomeServerChange.SelectHomeServer(A_HOMESERVER_URL)
val test = viewModel.test()
viewModel.handle(selectAction)
test
.assertStatesChanges(
initialState,
{ copy(isLoading = true) },
{ copy(isLoading = false) }
)
.assertEvents(OnboardingViewEvents.UnrecognisedCertificateFailure(selectAction, AN_UNRECOGNISED_CERTIFICATE_ERROR))
.finish()
}
@Test @Test
fun `given unavailable full matrix id, when a register username is entered, then emits availability error`() = runTest { fun `given unavailable full matrix id, when a register username is entered, then emits availability error`() = runTest {
viewModelWith(initialRegistrationState("ignored-url")) viewModelWith(initialRegistrationState("ignored-url"))
@ -726,7 +785,6 @@ class OnboardingViewModelTest {
.finish() .finish()
} }
@Test @Test
fun `given in sign in mode, when accepting user certificate with SelectHomeserver retry action, then emits OnHomeserverEdited`() = runTest { fun `given in sign in mode, when accepting user certificate with SelectHomeserver retry action, then emits OnHomeserverEdited`() = runTest {
viewModelWith(initialState.copy(onboardingFlow = OnboardingFlow.SignIn)) viewModelWith(initialState.copy(onboardingFlow = OnboardingFlow.SignIn))

View file

@ -31,6 +31,10 @@ class FakeStartAuthenticationFlowUseCase {
coEvery { instance.execute(config) } returns result coEvery { instance.execute(config) } returns result
} }
fun givenErrors(config: HomeServerConnectionConfig, error: Throwable) {
coEvery { instance.execute(config) } throws error
}
fun givenHomeserverUnavailable(config: HomeServerConnectionConfig) { fun givenHomeserverUnavailable(config: HomeServerConnectionConfig) {
coEvery { instance.execute(config) } throws aHomeserverUnavailableError() coEvery { instance.execute(config) } throws aHomeserverUnavailableError()
} }

View file

@ -18,6 +18,7 @@ package im.vector.app.test.fixtures
import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.failure.MatrixError import org.matrix.android.sdk.api.failure.MatrixError
import org.matrix.android.sdk.api.network.ssl.Fingerprint
import java.net.UnknownHostException import java.net.UnknownHostException
import javax.net.ssl.HttpsURLConnection import javax.net.ssl.HttpsURLConnection
@ -38,3 +39,5 @@ fun aLoginEmailUnknownError() = Failure.ServerError(
) )
fun aHomeserverUnavailableError() = Failure.NetworkConnection(UnknownHostException()) fun aHomeserverUnavailableError() = Failure.NetworkConnection(UnknownHostException())
fun anUnrecognisedCertificateError() = Failure.UnrecognizedCertificateFailure("a-url", Fingerprint(ByteArray(1), Fingerprint.HashType.SHA1))