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

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

Change to AccountManager and add test for AccountLoader

parent eff8b25a
Loading
Loading
Loading
Loading
+5 −11
Original line number Diff line number Diff line
package com.fsck.k9.account

import app.k9mail.feature.account.common.AccountCommonExternalContract
import app.k9mail.feature.account.common.domain.entity.Account
import app.k9mail.feature.account.common.domain.entity.AccountOptions
import com.fsck.k9.Preferences
import app.k9mail.feature.account.common.domain.entity.AccountState
import app.k9mail.feature.account.common.domain.entity.AuthorizationState
import com.fsck.k9.logging.Timber
import com.fsck.k9.preferences.AccountManager
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import com.fsck.k9.Account as K9Account

class AccountLoader(
    private val preferences: Preferences,
    private val accountManager: AccountManager,
    private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO,
) : AccountCommonExternalContract.AccountLoader {

@@ -28,14 +28,8 @@ class AccountLoader(
        }
    }

        val existingAccount = preferences.getAccount(accountUuid)
    private fun load(accountUuid: String): AccountState? {

        return if (existingAccount != null) {
            mapToAccount(existingAccount)
        } else {
            null
        }
        return accountManager.getAccount(accountUuid)?.let { mapToAccount(it) }
    }

    private fun mapToAccount(account: K9Account): AccountState {
+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ val newAccountModule = module {

    factory<AccountCommonExternalContract.AccountLoader> {
        AccountLoader(
            preferences = get(),
            accountManager = get(),
        )
    }

+82 −0
Original line number Diff line number Diff line
package com.fsck.k9.account

import app.k9mail.feature.account.common.domain.entity.AccountState
import app.k9mail.feature.account.common.domain.entity.AuthorizationState
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNull
import com.fsck.k9.Identity
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
import com.fsck.k9.mail.ServerSettings
import kotlinx.coroutines.test.runTest
import org.junit.Test
import com.fsck.k9.Account as K9Account

class AccountLoaderTest {

    @Test
    fun `loadAccount() should return null when accountManager returns null`() = runTest {
        val accountManager = FakeAccountManager()
        val accountLoader = AccountLoader(accountManager)

        val result = accountLoader.loadAccount("accountUuid")

        assertThat(result).isNull()
    }

    @Test
    fun `loadAccount() should return account when present in accountManager`() = runTest {
        val accounts = mapOf(
            "accountUuid" to K9Account(
                uuid = "accountUuid",
            ).also {
                it.identities = mutableListOf(Identity())
                it.email = "emailAddress"
                it.incomingServerSettings = INCOMING_SERVER_SETTINGS
                it.outgoingServerSettings = OUTGOING_SERVER_SETTINGS
                it.oAuthState = "oAuthState"
            },
        )
        val accountManager = FakeAccountManager(accounts = accounts)
        val accountLoader = AccountLoader(accountManager)

        val result = accountLoader.loadAccount("accountUuid")

        assertThat(result).isEqualTo(
            AccountState(
                uuid = "accountUuid",
                emailAddress = "emailAddress",
                incomingServerSettings = INCOMING_SERVER_SETTINGS,
                outgoingServerSettings = OUTGOING_SERVER_SETTINGS,
                authorizationState = AuthorizationState("oAuthState"),
            ),
        )
    }

    private companion object {
        val INCOMING_SERVER_SETTINGS = ServerSettings(
            type = "imap",
            host = "imap.example.org",
            port = 143,
            connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
            authenticationType = AuthType.PLAIN,
            username = "username",
            password = "password",
            clientCertificateAlias = null,
            extra = emptyMap(),
        )

        val OUTGOING_SERVER_SETTINGS = ServerSettings(
            type = "smtp",
            host = "smtp.example.org",
            port = 587,
            connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
            authenticationType = AuthType.PLAIN,
            username = "username",
            password = "password",
            clientCertificateAlias = null,
            extra = emptyMap(),
        )
    }
}
+42 −0
Original line number Diff line number Diff line
package com.fsck.k9.account

import com.fsck.k9.Account
import com.fsck.k9.AccountRemovedListener
import com.fsck.k9.AccountsChangeListener
import com.fsck.k9.preferences.AccountManager
import kotlinx.coroutines.flow.Flow

class FakeAccountManager(
    private val accounts: Map<String, Account> = emptyMap(),
) : AccountManager {

    override fun getAccountsFlow(): Flow<List<Account>> {
        TODO("Not yet implemented")
    }

    override fun getAccount(accountUuid: String): Account? = accounts[accountUuid]

    override fun getAccountFlow(accountUuid: String): Flow<Account> {
        TODO("Not yet implemented")
    }

    override fun addAccountRemovedListener(listener: AccountRemovedListener) {
        TODO("Not yet implemented")
    }

    override fun moveAccount(account: Account, newPosition: Int) {
        TODO("Not yet implemented")
    }

    override fun addOnAccountsChangeListener(accountsChangeListener: AccountsChangeListener) {
        TODO("Not yet implemented")
    }

    override fun removeOnAccountsChangeListener(accountsChangeListener: AccountsChangeListener) {
        TODO("Not yet implemented")
    }

    override fun saveAccount(account: Account) {
        TODO("Not yet implemented")
    }
}