Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 3d9235ce authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

Apply changes from renaming AccountLoader to AccountStateLoader

parent 2f9f757e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ val featureAccountEditModule = module {

    factory<AccountEditDomainContract.UseCase.LoadAccountState> {
        LoadAccountState(
            accountLoader = get(),
            accountStateLoader = get(),
            accountStateRepository = get(),
        )
    }
+3 −15
Original line number Diff line number Diff line
@@ -2,13 +2,11 @@ package app.k9mail.feature.account.edit.domain.usecase

import app.k9mail.feature.account.common.AccountCommonExternalContract
import app.k9mail.feature.account.common.domain.AccountDomainContract
import app.k9mail.feature.account.common.domain.entity.Account
import app.k9mail.feature.account.common.domain.entity.AccountState
import app.k9mail.feature.account.common.domain.entity.AuthorizationState
import app.k9mail.feature.account.edit.domain.AccountEditDomainContract

class LoadAccountState(
    private val accountLoader: AccountCommonExternalContract.AccountLoader,
    private val accountStateLoader: AccountCommonExternalContract.AccountStateLoader,
    private val accountStateRepository: AccountDomainContract.AccountStateRepository,
) : AccountEditDomainContract.UseCase.LoadAccountState {
    override suspend fun execute(accountUuid: String): AccountState {
@@ -21,21 +19,11 @@ class LoadAccountState(
    }

    private suspend fun loadState(accountUuid: String): AccountState {
        val account = accountLoader.loadAccount(accountUuid)
        return if (account != null) {
            val accountState = account.mapToAccountState()
        val accountState = accountStateLoader.loadAccountState(accountUuid)
        return if (accountState != null) {
            accountState
        } else {
            AccountState(uuid = accountUuid)
        }.also { accountStateRepository.setState(it) }
    }

    private fun Account.mapToAccountState() = AccountState(
        uuid = uuid,
        emailAddress = emailAddress,
        incomingServerSettings = incomingServerSettings,
        outgoingServerSettings = outgoingServerSettings,
        authorizationState = AuthorizationState(authorizationState),
        options = options,
    )
}
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import org.robolectric.RuntimeEnvironment
class AccountEditModuleKtTest : KoinTest {

    private val externalModule: Module = module {
        single<AccountCommonExternalContract.AccountLoader> { Mockito.mock() }
        single<AccountCommonExternalContract.AccountStateLoader> { Mockito.mock() }
        single<LocalKeyStore> { Mockito.mock() }
        single<TrustedSocketFactory> {
            TrustedSocketFactory { _, _, _, _ -> null }
+24 −54
Original line number Diff line number Diff line
package app.k9mail.feature.account.edit.domain.usecase

import app.k9mail.feature.account.common.data.InMemoryAccountStateRepository
import app.k9mail.feature.account.common.domain.entity.Account
import app.k9mail.feature.account.common.domain.entity.AccountOptions
import app.k9mail.feature.account.common.domain.entity.AccountState
import app.k9mail.feature.account.common.domain.entity.AuthorizationState
@@ -10,6 +9,7 @@ import assertk.assertThat
import assertk.assertions.isEqualTo
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ServerSettings
import kotlin.test.DefaultAsserter.fail
import kotlinx.coroutines.test.runTest
import org.junit.Test

@@ -18,78 +18,39 @@ class LoadAccountStateTest {
    @Test
    fun `should load account state WHEN account in state repository has different UUID`() = runTest {
        val testSubject = LoadAccountState(
            accountLoader = { accountUuid ->
                Account(
                    uuid = accountUuid,
                    emailAddress = EMAIL_ADDRESS,
                    incomingServerSettings = INCOMING_SERVER_SETTINGS,
                    outgoingServerSettings = OUTGOING_SERVER_SETTINGS,
                    authorizationState = AUTHORIZATION_STATE,
                    options = OPTIONS,
                )
            accountStateLoader = { _ ->
                ACCOUNT_STATE
            },
            accountStateRepository = InMemoryAccountStateRepository(),
            accountStateRepository = InMemoryAccountStateRepository(
                state = ACCOUNT_STATE.copy(uuid = "differentUuid"),
            ),
        )

        val result = testSubject.execute(ACCOUNT_UUID)

        assertThat(result).isEqualTo(
            AccountState(
                uuid = ACCOUNT_UUID,
                emailAddress = EMAIL_ADDRESS,
                incomingServerSettings = INCOMING_SERVER_SETTINGS,
                outgoingServerSettings = OUTGOING_SERVER_SETTINGS,
                authorizationState = AuthorizationState(AUTHORIZATION_STATE),
                options = OPTIONS,
            ),
        )
        assertThat(result).isEqualTo(ACCOUNT_STATE)
    }

    @Test
    fun `should return account state WHEN account in state repository has same UUID`() = runTest {
        val testSubject = LoadAccountState(
            accountLoader = { accountUuid ->
                Account(
                    uuid = accountUuid,
                    emailAddress = EMAIL_ADDRESS,
                    incomingServerSettings = INCOMING_SERVER_SETTINGS,
                    outgoingServerSettings = OUTGOING_SERVER_SETTINGS,
                    authorizationState = AUTHORIZATION_STATE,
                    options = OPTIONS,
                )
            accountStateLoader = { _ ->
                fail("AccountStateLoader should not be called in this test")
            },
            accountStateRepository = InMemoryAccountStateRepository().apply {
                setState(
                    AccountState(
                        uuid = ACCOUNT_UUID,
                        emailAddress = EMAIL_ADDRESS,
                        incomingServerSettings = INCOMING_SERVER_SETTINGS,
                        outgoingServerSettings = OUTGOING_SERVER_SETTINGS,
                        authorizationState = AuthorizationState(AUTHORIZATION_STATE),
                        options = OPTIONS,
            accountStateRepository = InMemoryAccountStateRepository(
                state = ACCOUNT_STATE,
            ),
        )
            },
        )

        val result = testSubject.execute(ACCOUNT_UUID)

        assertThat(result).isEqualTo(
            AccountState(
                uuid = ACCOUNT_UUID,
                emailAddress = EMAIL_ADDRESS,
                incomingServerSettings = INCOMING_SERVER_SETTINGS,
                outgoingServerSettings = OUTGOING_SERVER_SETTINGS,
                authorizationState = AuthorizationState(AUTHORIZATION_STATE),
                options = OPTIONS,
            ),
        )
        assertThat(result).isEqualTo(ACCOUNT_STATE)
    }

    @Test
    fun `should return empty account state WHEN account loader returns null`() = runTest {
        val testSubject = LoadAccountState(
            accountLoader = { null },
            accountStateLoader = { null },
            accountStateRepository = InMemoryAccountStateRepository(),
        )

@@ -131,7 +92,7 @@ class LoadAccountStateTest {
            clientCertificateAlias = null,
        )

        const val AUTHORIZATION_STATE = "authorization state"
        val AUTHORIZATION_STATE = AuthorizationState("authorization state")

        val OPTIONS = AccountOptions(
            accountName = "accountName",
@@ -141,5 +102,14 @@ class LoadAccountStateTest {
            messageDisplayCount = 25,
            showNotification = true,
        )

        val ACCOUNT_STATE = AccountState(
            uuid = ACCOUNT_UUID,
            emailAddress = EMAIL_ADDRESS,
            incomingServerSettings = INCOMING_SERVER_SETTINGS,
            outgoingServerSettings = OUTGOING_SERVER_SETTINGS,
            authorizationState = AUTHORIZATION_STATE,
            options = OPTIONS,
        )
    }
}