Add test for AccountProviderPresenter

This commit is contained in:
Benoit Marty 2023-06-07 14:47:42 +02:00
parent 3d0156e5e3
commit c94b29bf83

View file

@ -14,132 +14,116 @@
* limitations under the License. * limitations under the License.
*/ */
package io.element.android.features.login.impl.changeserver package io.element.android.features.login.impl.accountprovider
import app.cash.molecule.RecompositionClock import app.cash.molecule.RecompositionClock
import app.cash.molecule.moleculeFlow import app.cash.molecule.moleculeFlow
import app.cash.turbine.test import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import io.element.android.features.login.impl.util.LoginConstants import io.element.android.features.login.impl.datasource.AccountProviderDataSource
import io.element.android.features.login.impl.util.defaultAccountProvider
import io.element.android.libraries.architecture.Async import io.element.android.libraries.architecture.Async
import io.element.android.libraries.matrix.test.A_HOMESERVER import io.element.android.libraries.matrix.test.A_HOMESERVER
import io.element.android.libraries.matrix.test.A_HOMESERVER_URL_2 import io.element.android.libraries.matrix.test.A_HOMESERVER_OIDC
import io.element.android.libraries.matrix.test.A_THROWABLE import io.element.android.libraries.matrix.test.A_THROWABLE
import io.element.android.libraries.matrix.test.auth.FakeAuthenticationService import io.element.android.libraries.matrix.test.auth.FakeAuthenticationService
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.Test import org.junit.Test
class ChangeServerPresenterTest { class AccountProviderPresenterTest {
@Test @Test
fun `present - should start with default homeserver`() = runTest { fun `present - initial test`() = runTest {
val presenter = ChangeServerPresenter( val presenter = AccountProviderPresenter(
AccountProviderPresenterParams(isAccountCreation = false),
AccountProviderDataSource(),
FakeAuthenticationService(), FakeAuthenticationService(),
) )
moleculeFlow(RecompositionClock.Immediate) { moleculeFlow(RecompositionClock.Immediate) {
presenter.present() presenter.present()
}.test { }.test {
val initialState = awaitItem() val initialState = awaitItem()
assertThat(initialState.homeserver).isEqualTo(LoginConstants.DEFAULT_HOMESERVER_URL) assertThat(initialState.isAccountCreation).isFalse()
assertThat(initialState.submitEnabled).isTrue() assertThat(initialState.submitEnabled).isTrue()
assertThat(initialState.accountProvider).isEqualTo(defaultAccountProvider)
assertThat(initialState.loginFlow).isEqualTo(Async.Uninitialized)
} }
} }
@Test @Test
fun `present - authentication service can provide a homeserver`() = runTest { fun `present - continue password login`() = runTest {
val presenter = ChangeServerPresenter( val authServer = FakeAuthenticationService()
FakeAuthenticationService().apply { val presenter = AccountProviderPresenter(
givenHomeserver(A_HOMESERVER.copy(url = A_HOMESERVER_URL_2)) AccountProviderPresenterParams(isAccountCreation = false),
}, AccountProviderDataSource(),
authServer,
) )
authServer.givenHomeserver(A_HOMESERVER)
moleculeFlow(RecompositionClock.Immediate) { moleculeFlow(RecompositionClock.Immediate) {
presenter.present() presenter.present()
}.test { }.test {
val initialState = awaitItem() val initialState = awaitItem()
assertThat(initialState.homeserver).isEqualTo(A_HOMESERVER_URL_2) initialState.eventSink.invoke(AccountProviderEvents.Continue)
assertThat(initialState.submitEnabled).isTrue()
}
}
@Test
fun `present - disable if empty or not correct`() = runTest {
val presenter = ChangeServerPresenter(
FakeAuthenticationService(),
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
val initialState = awaitItem()
initialState.eventSink.invoke(ChangeServerEvents.SetServer(""))
val emptyState = awaitItem()
assertThat(emptyState.homeserver).isEqualTo("")
assertThat(emptyState.submitEnabled).isFalse()
}
}
@Test
fun `present - submit`() = runTest {
val presenter = ChangeServerPresenter(
FakeAuthenticationService(),
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
val initialState = awaitItem()
initialState.eventSink.invoke(ChangeServerEvents.Submit)
val loadingState = awaitItem() val loadingState = awaitItem()
assertThat(loadingState.submitEnabled).isTrue() assertThat(loadingState.submitEnabled).isTrue()
assertThat(loadingState.changeServerAction).isInstanceOf(Async.Loading::class.java) assertThat(loadingState.loginFlow).isInstanceOf(Async.Loading::class.java)
val successState = awaitItem() val successState = awaitItem()
assertThat(successState.submitEnabled).isFalse() assertThat(successState.submitEnabled).isFalse()
assertThat(successState.changeServerAction).isInstanceOf(Async.Success::class.java) assertThat(successState.loginFlow).isInstanceOf(Async.Success::class.java)
assertThat(successState.loginFlow.dataOrNull()).isEqualTo(LoginFlow.PasswordLogin)
} }
} }
@Test @Test
fun `present - submit parses URL`() = runTest { fun `present - continue oidc`() = runTest {
val presenter = ChangeServerPresenter( val authServer = FakeAuthenticationService()
FakeAuthenticationService(), val presenter = AccountProviderPresenter(
AccountProviderPresenterParams(isAccountCreation = false),
AccountProviderDataSource(),
authServer,
) )
authServer.givenHomeserver(A_HOMESERVER_OIDC)
moleculeFlow(RecompositionClock.Immediate) { moleculeFlow(RecompositionClock.Immediate) {
presenter.present() presenter.present()
}.test { }.test {
val longUrl = "https://matrix.org/.well-known/"
val initialState = awaitItem() val initialState = awaitItem()
initialState.eventSink.invoke(ChangeServerEvents.SetServer(longUrl)) initialState.eventSink.invoke(AccountProviderEvents.Continue)
awaitItem()
initialState.eventSink.invoke(ChangeServerEvents.Submit)
val loadingState = awaitItem() val loadingState = awaitItem()
assertThat(loadingState.submitEnabled).isTrue() assertThat(loadingState.submitEnabled).isTrue()
assertThat(loadingState.changeServerAction).isInstanceOf(Async.Loading::class.java) assertThat(loadingState.loginFlow).isInstanceOf(Async.Loading::class.java)
awaitItem() // Skip changing the url to the parsed domain
val successState = awaitItem() val successState = awaitItem()
assertThat(successState.submitEnabled).isFalse() assertThat(successState.submitEnabled).isFalse()
assertThat(successState.changeServerAction).isInstanceOf(Async.Success::class.java) assertThat(successState.loginFlow).isInstanceOf(Async.Success::class.java)
assertThat(successState.homeserver).isEqualTo("matrix.org") assertThat(successState.loginFlow.dataOrNull()).isInstanceOf(LoginFlow.OidcFlow::class.java)
} }
} }
@Test @Test
fun `present - submit fails`() = runTest { fun `present - submit fails`() = runTest {
val authServer = FakeAuthenticationService() val authServer = FakeAuthenticationService()
val presenter = ChangeServerPresenter(authServer) val presenter = AccountProviderPresenter(
AccountProviderPresenterParams(isAccountCreation = false),
AccountProviderDataSource(),
authServer,
)
moleculeFlow(RecompositionClock.Immediate) { moleculeFlow(RecompositionClock.Immediate) {
presenter.present() presenter.present()
}.test { }.test {
val initialState = awaitItem() val initialState = awaitItem()
authServer.givenChangeServerError(Throwable()) authServer.givenChangeServerError(Throwable())
initialState.eventSink.invoke(ChangeServerEvents.Submit) initialState.eventSink.invoke(AccountProviderEvents.Continue)
skipItems(1) // Loading skipItems(1) // Loading
val failureState = awaitItem() val failureState = awaitItem()
assertThat(failureState.submitEnabled).isFalse() assertThat(failureState.submitEnabled).isFalse()
assertThat(failureState.changeServerAction).isInstanceOf(Async.Failure::class.java) assertThat(failureState.loginFlow).isInstanceOf(Async.Failure::class.java)
} }
} }
@Test @Test
fun `present - clear error`() = runTest { fun `present - clear error`() = runTest {
val authenticationService = FakeAuthenticationService() val authenticationService = FakeAuthenticationService()
val presenter = ChangeServerPresenter( val presenter = AccountProviderPresenter(
AccountProviderPresenterParams(isAccountCreation = false),
AccountProviderDataSource(),
authenticationService, authenticationService,
) )
moleculeFlow(RecompositionClock.Immediate) { moleculeFlow(RecompositionClock.Immediate) {
@ -149,18 +133,18 @@ class ChangeServerPresenterTest {
// Submit will return an error // Submit will return an error
authenticationService.givenChangeServerError(A_THROWABLE) authenticationService.givenChangeServerError(A_THROWABLE)
initialState.eventSink(ChangeServerEvents.Submit) initialState.eventSink(AccountProviderEvents.Continue)
skipItems(1) // Loading skipItems(1) // Loading
// Check an error was returned // Check an error was returned
val submittedState = awaitItem() val submittedState = awaitItem()
assertThat(submittedState.changeServerAction).isInstanceOf(Async.Failure::class.java) assertThat(submittedState.loginFlow).isInstanceOf(Async.Failure::class.java)
// Assert the error is then cleared // Assert the error is then cleared
submittedState.eventSink(ChangeServerEvents.ClearError) submittedState.eventSink(AccountProviderEvents.ClearError)
val clearedState = awaitItem() val clearedState = awaitItem()
assertThat(clearedState.changeServerAction).isEqualTo(Async.Uninitialized) assertThat(clearedState.loginFlow).isEqualTo(Async.Uninitialized)
} }
} }
} }