adding tests around login usecase

This commit is contained in:
Adam Brown 2022-12-13 20:45:44 +00:00
parent 27e29ebd34
commit df9e97cbf8
5 changed files with 76 additions and 4 deletions

@ -1 +1 @@
Subproject commit 8139eaaf57cee4ce9d0616d617e8aff7eb1480e3
Subproject commit cdf3e1bffba4b69dd8f752c6cc7588b0e89a17af

View File

@ -28,7 +28,7 @@ fun loginReducer(
dispatch(LoginAction.UpdateContent(LoginScreenState.Content.Loading))
val request = LoginRequest(action.userName, action.password, action.serverUrl.takeIfNotEmpty())
when (val result = loginUseCase.run(request)) {
when (val result = loginUseCase.login(request)) {
is LoginResult.Error -> dispatch(LoginAction.UpdateContent(LoginScreenState.Content.Error(result.cause)))
LoginResult.MissingWellKnown -> {

View File

@ -15,7 +15,7 @@ class LoginUseCase(
private val pushTokenRegistrar: PushTokenRegistrar,
private val errorTracker: ErrorTracker,
) {
suspend fun run(request: LoginRequest): LoginResult {
suspend fun login(request: LoginRequest): LoginResult {
return logP("login") {
when (val result = chatEngine.login(request)) {
is LoginResult.Success -> {

View File

@ -0,0 +1,72 @@
package app.dapk.st.login.state
import app.dapk.st.engine.LoginRequest
import app.dapk.st.engine.LoginResult
import app.dapk.st.matrix.common.DeviceId
import app.dapk.st.matrix.common.HomeServerUrl
import app.dapk.st.matrix.common.UserCredentials
import app.dapk.st.push.PushTokenRegistrar
import fake.FakeChatEngine
import fake.FakeErrorTracker
import fixture.aUserId
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import test.expect
private val A_LOGIN_ERROR = LoginResult.Error(RuntimeException())
private val A_LOGIN_SUCCESS = LoginResult.Success(aUserCredentials())
private val A_LOGIN_REQUEST = LoginRequest(
userName = "a-username",
password = "a-password",
serverUrl = "a-server-url",
)
class LoginUseCaseTest {
private val fakeChatEngine = FakeChatEngine()
private val fakePushTokenRegistrar = FakePushTokenRegistrar()
private val fakeErrorTracker = FakeErrorTracker()
private val useCase = LoginUseCase(
fakeChatEngine,
fakePushTokenRegistrar,
fakeErrorTracker,
)
@Test
fun `when logging in succeeds, then registers push token and preload me`() = runTest {
fakeChatEngine.givenLogin(A_LOGIN_REQUEST).returns(A_LOGIN_SUCCESS)
fakePushTokenRegistrar.expect { it.registerCurrentToken() }
fakeChatEngine.expect { it.me(forceRefresh = false) }
val result = useCase.login(A_LOGIN_REQUEST)
result shouldBeEqualTo A_LOGIN_SUCCESS
}
@Test
fun `when logging in fails with MissingWellKnown, then does nothing`() = runTest {
fakeChatEngine.givenLogin(A_LOGIN_REQUEST).returns(LoginResult.MissingWellKnown)
val result = useCase.login(A_LOGIN_REQUEST)
result shouldBeEqualTo LoginResult.MissingWellKnown
}
@Test
fun `when logging in errors, then tracks cause`() = runTest {
fakeChatEngine.givenLogin(A_LOGIN_REQUEST).returns(A_LOGIN_ERROR)
fakeErrorTracker.expect { it.track(A_LOGIN_ERROR.cause) }
val result = useCase.login(A_LOGIN_REQUEST)
result shouldBeEqualTo A_LOGIN_ERROR
}
}
class FakePushTokenRegistrar : PushTokenRegistrar by mockk()
private fun aUserCredentials() = UserCredentials("ignored", HomeServerUrl("ignored"), aUserId(), DeviceId("ignored"))

View File

@ -10,6 +10,6 @@ class FakeLoginUseCase {
val instance = mockk<LoginUseCase>()
fun given(loginRequest: LoginRequest) = coEvery { instance.run(loginRequest) }.delegateReturn()
fun given(loginRequest: LoginRequest) = coEvery { instance.login(loginRequest) }.delegateReturn()
}