From bb9459fcabfaaea285d2f6ccc89f388c34eb8309 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 28 Jul 2022 11:41:40 +0100 Subject: [PATCH] adding test around sso url fetching --- .../onboarding/OnboardingViewModelTest.kt | 26 ++++++++++++++++++- .../test/fakes/FakeAuthenticationService.kt | 4 +++ 2 files changed, 29 insertions(+), 1 deletion(-) 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 1bc1b5bbf4..3785729230 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 @@ -49,10 +49,12 @@ import im.vector.app.test.fixtures.aBuildMeta import im.vector.app.test.fixtures.aHomeServerCapabilities import im.vector.app.test.test import kotlinx.coroutines.test.runTest +import org.amshove.kluent.shouldBeEqualTo import org.junit.Before import org.junit.Rule import org.junit.Test import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig +import org.matrix.android.sdk.api.auth.data.SsoIdentityProvider import org.matrix.android.sdk.api.auth.registration.Stage import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.session.Session @@ -81,6 +83,10 @@ private const val A_DEVICE_NAME = "a-device-name" private const val A_MATRIX_ID = "@$A_USERNAME:matrix.org" private const val A_LOGIN_TOKEN = "a-login-token" private val A_REGISTRATION_STATE = aRegistrationState(email = AN_EMAIL) +private const val A_SSO_URL = "https://a-sso.url" +private const val A_REDIRECT_URI = "https://a-redirect.uri" +private const val A_DEVICE_ID = "a-device-id" +private val SSO_REGISTRATION_DESCRIPTION = AuthenticationDescription.Register(AuthenticationDescription.AuthenticationType.SSO) class OnboardingViewModelTest { @@ -853,7 +859,6 @@ class OnboardingViewModelTest { fakeAuthenticationService.verifyCancelsPendingLogin() } - @Test fun `given reset state, when resetting reset state, then resets state`() = runTest { viewModelWith(initialState.copy(isLoading = true, resetState = ResetState(AN_EMAIL))) @@ -862,6 +867,7 @@ class OnboardingViewModelTest { viewModel.handle(OnboardingAction.ResetResetPassword) test + .assertStatesChanges( initialState, { copy(isLoading = false, resetState = ResetState()) }, @@ -886,6 +892,24 @@ class OnboardingViewModelTest { .finish() } + @Test + fun `given returns Sso url, when fetching Sso url, then updates authentication state and returns supplied Sso url`() = runTest { + val test = viewModel.test() + val provider = SsoIdentityProvider(id = "provider_id", null, null, null) + fakeAuthenticationService.givenSsoUrl(A_REDIRECT_URI, A_DEVICE_ID, provider.id, result = A_SSO_URL) + + val result = viewModel.fetchSsoUrl(A_REDIRECT_URI, A_DEVICE_ID, provider) + + result shouldBeEqualTo A_SSO_URL + test + .assertStatesChanges( + initialState, + { copy(selectedAuthenticationState = SelectedAuthenticationState(SSO_REGISTRATION_DESCRIPTION)) } + ) + .finish() + + } + private fun viewModelWith(state: OnboardingViewState) { OnboardingViewModel( state, diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeAuthenticationService.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeAuthenticationService.kt index 0075fb29b1..af53913169 100644 --- a/vector/src/test/java/im/vector/app/test/fakes/FakeAuthenticationService.kt +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeAuthenticationService.kt @@ -77,4 +77,8 @@ class FakeAuthenticationService : AuthenticationService by mockk() { fun verifyCancelsPendingLogin() { coVerify { cancelPendingLoginOrRegistration() } } + + fun givenSsoUrl(redirectUri: String, deviceId: String, providerId: String, result: String) { + coEvery { getSsoUrl(redirectUri, deviceId, providerId) } returns result + } }