diff --git a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt index c612644576..216cb76084 100644 --- a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt @@ -48,6 +48,7 @@ import im.vector.app.test.fakes.FakeVectorOverrides import im.vector.app.test.fakes.toTestString import im.vector.app.test.fixtures.a401ServerError import im.vector.app.test.fixtures.aHomeServerCapabilities +import im.vector.app.test.fixtures.anUnrecognisedCertificateError import im.vector.app.test.test import kotlinx.coroutines.test.runTest 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 val A_SERVER_ERROR = a401ServerError() 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_NON_LOADABLE_REGISTER_ACTION = RegisterAction.CheckIfEmailHasBeenValidated(delayMillis = -1L) private val A_RESULT_IGNORED_REGISTER_ACTION = RegisterAction.SendAgainThreePid @@ -322,6 +324,25 @@ class OnboardingViewModelTest { .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 fun `when handling SignUp then sets sign mode to sign up and starts registration`() = runTest { givenRegistrationResultFor(RegisterAction.StartRegistration, ANY_CONTINUING_REGISTRATION_RESULT) @@ -550,6 +571,44 @@ class OnboardingViewModelTest { .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 fun `given unavailable full matrix id, when a register username is entered, then emits availability error`() = runTest { viewModelWith(initialRegistrationState("ignored-url")) @@ -726,7 +785,6 @@ class OnboardingViewModelTest { .finish() } - @Test fun `given in sign in mode, when accepting user certificate with SelectHomeserver retry action, then emits OnHomeserverEdited`() = runTest { viewModelWith(initialState.copy(onboardingFlow = OnboardingFlow.SignIn)) diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeStartAuthenticationFlowUseCase.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeStartAuthenticationFlowUseCase.kt index bfbef9e565..4b2709facc 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeStartAuthenticationFlowUseCase.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeStartAuthenticationFlowUseCase.kt @@ -31,6 +31,10 @@ class FakeStartAuthenticationFlowUseCase { coEvery { instance.execute(config) } returns result } + fun givenErrors(config: HomeServerConnectionConfig, error: Throwable) { + coEvery { instance.execute(config) } throws error + } + fun givenHomeserverUnavailable(config: HomeServerConnectionConfig) { coEvery { instance.execute(config) } throws aHomeserverUnavailableError() } diff --git a/vector/src/test/java/im/vector/app/test/fixtures/FailureFixture.kt b/vector/src/test/java/im/vector/app/test/fixtures/FailureFixture.kt index 0f44976ab3..8437401294 100644 --- a/vector/src/test/java/im/vector/app/test/fixtures/FailureFixture.kt +++ b/vector/src/test/java/im/vector/app/test/fixtures/FailureFixture.kt @@ -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.MatrixError +import org.matrix.android.sdk.api.network.ssl.Fingerprint import java.net.UnknownHostException import javax.net.ssl.HttpsURLConnection @@ -38,3 +39,5 @@ fun aLoginEmailUnknownError() = Failure.ServerError( ) fun aHomeserverUnavailableError() = Failure.NetworkConnection(UnknownHostException()) + +fun anUnrecognisedCertificateError() = Failure.UnrecognizedCertificateFailure("a-url", Fingerprint(ByteArray(1), Fingerprint.HashType.SHA1))