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.
*/
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.moleculeFlow
import app.cash.turbine.test
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.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.auth.FakeAuthenticationService
import kotlinx.coroutines.test.runTest
import org.junit.Test
class ChangeServerPresenterTest {
class AccountProviderPresenterTest {
@Test
fun `present - should start with default homeserver`() = runTest {
val presenter = ChangeServerPresenter(
fun `present - initial test`() = runTest {
val presenter = AccountProviderPresenter(
AccountProviderPresenterParams(isAccountCreation = false),
AccountProviderDataSource(),
FakeAuthenticationService(),
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
val initialState = awaitItem()
assertThat(initialState.homeserver).isEqualTo(LoginConstants.DEFAULT_HOMESERVER_URL)
assertThat(initialState.isAccountCreation).isFalse()
assertThat(initialState.submitEnabled).isTrue()
assertThat(initialState.accountProvider).isEqualTo(defaultAccountProvider)
assertThat(initialState.loginFlow).isEqualTo(Async.Uninitialized)
}
}
@Test
fun `present - authentication service can provide a homeserver`() = runTest {
val presenter = ChangeServerPresenter(
FakeAuthenticationService().apply {
givenHomeserver(A_HOMESERVER.copy(url = A_HOMESERVER_URL_2))
},
fun `present - continue password login`() = runTest {
val authServer = FakeAuthenticationService()
val presenter = AccountProviderPresenter(
AccountProviderPresenterParams(isAccountCreation = false),
AccountProviderDataSource(),
authServer,
)
authServer.givenHomeserver(A_HOMESERVER)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
val initialState = awaitItem()
assertThat(initialState.homeserver).isEqualTo(A_HOMESERVER_URL_2)
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)
initialState.eventSink.invoke(AccountProviderEvents.Continue)
val loadingState = awaitItem()
assertThat(loadingState.submitEnabled).isTrue()
assertThat(loadingState.changeServerAction).isInstanceOf(Async.Loading::class.java)
assertThat(loadingState.loginFlow).isInstanceOf(Async.Loading::class.java)
val successState = awaitItem()
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
fun `present - submit parses URL`() = runTest {
val presenter = ChangeServerPresenter(
FakeAuthenticationService(),
fun `present - continue oidc`() = runTest {
val authServer = FakeAuthenticationService()
val presenter = AccountProviderPresenter(
AccountProviderPresenterParams(isAccountCreation = false),
AccountProviderDataSource(),
authServer,
)
authServer.givenHomeserver(A_HOMESERVER_OIDC)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
val longUrl = "https://matrix.org/.well-known/"
val initialState = awaitItem()
initialState.eventSink.invoke(ChangeServerEvents.SetServer(longUrl))
awaitItem()
initialState.eventSink.invoke(ChangeServerEvents.Submit)
initialState.eventSink.invoke(AccountProviderEvents.Continue)
val loadingState = awaitItem()
assertThat(loadingState.submitEnabled).isTrue()
assertThat(loadingState.changeServerAction).isInstanceOf(Async.Loading::class.java)
awaitItem() // Skip changing the url to the parsed domain
assertThat(loadingState.loginFlow).isInstanceOf(Async.Loading::class.java)
val successState = awaitItem()
assertThat(successState.submitEnabled).isFalse()
assertThat(successState.changeServerAction).isInstanceOf(Async.Success::class.java)
assertThat(successState.homeserver).isEqualTo("matrix.org")
assertThat(successState.loginFlow).isInstanceOf(Async.Success::class.java)
assertThat(successState.loginFlow.dataOrNull()).isInstanceOf(LoginFlow.OidcFlow::class.java)
}
}
@Test
fun `present - submit fails`() = runTest {
val authServer = FakeAuthenticationService()
val presenter = ChangeServerPresenter(authServer)
val presenter = AccountProviderPresenter(
AccountProviderPresenterParams(isAccountCreation = false),
AccountProviderDataSource(),
authServer,
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
}.test {
val initialState = awaitItem()
authServer.givenChangeServerError(Throwable())
initialState.eventSink.invoke(ChangeServerEvents.Submit)
initialState.eventSink.invoke(AccountProviderEvents.Continue)
skipItems(1) // Loading
val failureState = awaitItem()
assertThat(failureState.submitEnabled).isFalse()
assertThat(failureState.changeServerAction).isInstanceOf(Async.Failure::class.java)
assertThat(failureState.loginFlow).isInstanceOf(Async.Failure::class.java)
}
}
@Test
fun `present - clear error`() = runTest {
val authenticationService = FakeAuthenticationService()
val presenter = ChangeServerPresenter(
val presenter = AccountProviderPresenter(
AccountProviderPresenterParams(isAccountCreation = false),
AccountProviderDataSource(),
authenticationService,
)
moleculeFlow(RecompositionClock.Immediate) {
@@ -149,18 +133,18 @@ class ChangeServerPresenterTest {
// Submit will return an error
authenticationService.givenChangeServerError(A_THROWABLE)
initialState.eventSink(ChangeServerEvents.Submit)
initialState.eventSink(AccountProviderEvents.Continue)
skipItems(1) // Loading
// Check an error was returned
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
submittedState.eventSink(ChangeServerEvents.ClearError)
submittedState.eventSink(AccountProviderEvents.ClearError)
val clearedState = awaitItem()
assertThat(clearedState.changeServerAction).isEqualTo(Async.Uninitialized)
assertThat(clearedState.loginFlow).isEqualTo(Async.Uninitialized)
}
}
}