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

Unverified Commit 54253f78 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #7065 from thundernest/create_server_settings

Map IMAP advanced settings when creating `ServerSettings`
parents f4555669 e24cbaad
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
package app.k9mail.feature.account.setup.ui.incoming

import app.k9mail.feature.account.setup.domain.entity.IncomingProtocolType
import app.k9mail.feature.account.setup.domain.entity.toAuthType
import app.k9mail.feature.account.setup.domain.entity.toMailConnectionSecurity
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mail.store.imap.ImapStoreSettings

// TODO map extras
// TODO map clientCertificateAlias
internal fun AccountIncomingConfigContract.State.toServerSettings(): ServerSettings {
    return ServerSettings(
@@ -16,5 +17,19 @@ internal fun AccountIncomingConfigContract.State.toServerSettings(): ServerSetti
        username = username.value,
        password = if (authenticationType.isPasswordRequired) password.value else null,
        clientCertificateAlias = null, // TODO replace by actual client certificate alias
        extra = createExtras(),
    )
}

private fun AccountIncomingConfigContract.State.createExtras(): Map<String, String?> {
    return if (protocolType == IncomingProtocolType.IMAP) {
        ImapStoreSettings.createExtra(
            autoDetectNamespace = imapAutodetectNamespaceEnabled,
            pathPrefix = if (imapAutodetectNamespaceEnabled) null else imapPrefix.value,
            useCompression = imapUseCompression,
            sendClientId = imapSendClientId,
        )
    } else {
        emptyMap()
    }
}
+41 −1
Original line number Diff line number Diff line
@@ -11,12 +11,13 @@ import assertk.assertThat
import assertk.assertions.isEqualTo
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mail.store.imap.ImapStoreSettings
import org.junit.Test

class AccountIncomingConfigStateMapperKtTest {

    @Test
    fun `should map to server settings`() {
    fun `should map to IMAP server settings`() {
        val incomingState = State(
            protocolType = IncomingProtocolType.IMAP,
            server = StringInputField(value = "imap.example.org"),
@@ -44,6 +45,45 @@ class AccountIncomingConfigStateMapperKtTest {
                username = "user",
                password = "password",
                clientCertificateAlias = null,
                extra = ImapStoreSettings.createExtra(
                    autoDetectNamespace = true,
                    pathPrefix = null,
                    useCompression = true,
                    sendClientId = true,
                ),
            ),
        )
    }

    @Test
    fun `should map to POP3 server settings`() {
        val incomingState = State(
            protocolType = IncomingProtocolType.POP3,
            server = StringInputField(value = "pop3.domain.example"),
            port = NumberInputField(value = 995),
            security = ConnectionSecurity.TLS,
            authenticationType = AuthenticationType.PasswordCleartext,
            username = StringInputField(value = "user"),
            password = StringInputField(value = "password"),
            clientCertificate = "",
            imapAutodetectNamespaceEnabled = true,
            imapPrefix = StringInputField(value = "prefix"),
            imapUseCompression = true,
            imapSendClientId = true,
        )

        val result = incomingState.toServerSettings()

        assertThat(result).isEqualTo(
            ServerSettings(
                type = "pop3",
                host = "pop3.domain.example",
                port = 995,
                connectionSecurity = MailConnectionSecurity.SSL_TLS_REQUIRED,
                authenticationType = AuthType.PLAIN,
                username = "user",
                password = "password",
                clientCertificateAlias = null,
            ),
        )
    }