Use authentication type from AutoDiscoveryResult.Settings

This commit is contained in:
cketti 2023-07-19 20:14:19 +02:00
parent f73cd2ffef
commit 639f6e68b3
2 changed files with 41 additions and 54 deletions

View file

@ -2,12 +2,9 @@ package app.k9mail.feature.account.setup.ui.common.mapper
import app.k9mail.autodiscovery.api.ImapServerSettings
import app.k9mail.autodiscovery.api.SmtpServerSettings
import app.k9mail.feature.account.setup.domain.entity.ConnectionSecurity
import app.k9mail.feature.account.setup.domain.entity.IncomingProtocolType
import app.k9mail.feature.account.setup.domain.entity.toAuthenticationType
import app.k9mail.feature.account.setup.domain.entity.toConnectionSecurity
import app.k9mail.feature.account.setup.domain.entity.toImapDefaultPort
import app.k9mail.feature.account.setup.domain.entity.toIncomingProtocolType
import app.k9mail.feature.account.setup.domain.entity.toSmtpDefaultPort
import app.k9mail.feature.account.setup.domain.input.NumberInputField
import app.k9mail.feature.account.setup.domain.input.StringInputField
import app.k9mail.feature.account.setup.ui.autodiscovery.AccountAutoDiscoveryContract
@ -17,56 +14,41 @@ import app.k9mail.feature.account.setup.ui.outgoing.AccountOutgoingConfigContrac
internal fun AccountAutoDiscoveryContract.State.toIncomingConfigState(): AccountIncomingConfigContract.State {
val incomingSettings = autoDiscoverySettings?.incomingServerSettings as? ImapServerSettings?
return AccountIncomingConfigContract.State(
protocolType = incomingSettings?.toIncomingProtocolType() ?: IncomingProtocolType.DEFAULT,
server = StringInputField(
value = prefillServer(
hostname = incomingSettings?.hostname?.value,
),
),
security = incomingSettings?.connectionSecurity?.toConnectionSecurity() ?: ConnectionSecurity.DEFAULT,
port = NumberInputField(
value = incomingSettings?.port?.value?.toLong() ?: ConnectionSecurity.DEFAULT.toImapDefaultPort(),
),
username = StringInputField(
value = prefillUserName(
emailAddress = emailAddress.value,
username = incomingSettings?.username,
),
),
password = StringInputField(value = password.value),
)
return if (incomingSettings == null) {
AccountIncomingConfigContract.State(
username = StringInputField(value = emailAddress.value),
password = StringInputField(value = password.value),
)
} else {
AccountIncomingConfigContract.State(
protocolType = incomingSettings.toIncomingProtocolType(),
server = StringInputField(value = incomingSettings.hostname.value),
security = incomingSettings.connectionSecurity.toConnectionSecurity(),
port = NumberInputField(value = incomingSettings.port.value.toLong()),
authenticationType = incomingSettings.authenticationTypes.first().toAuthenticationType(),
username = StringInputField(value = incomingSettings.username),
password = StringInputField(value = password.value),
)
}
}
internal fun AccountAutoDiscoveryContract.State.toOutgoingConfigState(): AccountOutgoingConfigContract.State {
val outgoingSettings = autoDiscoverySettings?.outgoingServerSettings as? SmtpServerSettings?
return AccountOutgoingConfigContract.State(
server = StringInputField(
value = prefillServer(
hostname = outgoingSettings?.hostname?.value,
),
),
security = outgoingSettings?.connectionSecurity?.toConnectionSecurity() ?: ConnectionSecurity.DEFAULT,
port = NumberInputField(
value = outgoingSettings?.port?.value?.toLong() ?: ConnectionSecurity.DEFAULT.toSmtpDefaultPort(),
),
username = StringInputField(
value = prefillUserName(
emailAddress = emailAddress.value,
username = outgoingSettings?.username,
),
),
password = StringInputField(value = password.value),
)
}
private fun prefillServer(hostname: String?) = hostname.orEmpty()
internal fun prefillUserName(
emailAddress: String,
username: String?,
): String {
return username.takeUnless { it.isNullOrEmpty() } ?: emailAddress
return if (outgoingSettings == null) {
AccountOutgoingConfigContract.State(
username = StringInputField(value = emailAddress.value),
password = StringInputField(value = password.value),
)
} else {
AccountOutgoingConfigContract.State(
server = StringInputField(value = outgoingSettings.hostname.value),
security = outgoingSettings.connectionSecurity.toConnectionSecurity(),
port = NumberInputField(value = outgoingSettings.port.value.toLong()),
authenticationType = outgoingSettings.authenticationTypes.first().toAuthenticationType(),
username = StringInputField(value = outgoingSettings.username),
password = StringInputField(value = password.value),
)
}
}
internal fun AccountAutoDiscoveryContract.State.toOptionsState(): AccountOptionsContract.State {

View file

@ -5,6 +5,7 @@ import app.k9mail.autodiscovery.api.ImapServerSettings
import app.k9mail.autodiscovery.api.SmtpServerSettings
import app.k9mail.core.common.net.toHostname
import app.k9mail.core.common.net.toPort
import app.k9mail.feature.account.setup.domain.entity.AuthenticationType
import app.k9mail.feature.account.setup.domain.entity.AutoDiscoveryAuthenticationType
import app.k9mail.feature.account.setup.domain.entity.AutoDiscoveryConnectionSecurity
import app.k9mail.feature.account.setup.domain.entity.IncomingProtocolType
@ -50,6 +51,7 @@ class AccountAutoDiscoveryStateMapperKtTest {
server = StringInputField(value = AUTO_DISCOVERY_HOSTNAME.value),
security = AUTO_DISCOVERY_SECURITY.toConnectionSecurity(),
port = NumberInputField(value = AUTO_DISCOVERY_PORT_IMAP.value.toLong()),
authenticationType = AuthenticationType.PasswordEncrypted,
username = StringInputField(value = AUTO_DISCOVERY_USERNAME),
password = StringInputField(value = PASSWORD),
),
@ -57,7 +59,7 @@ class AccountAutoDiscoveryStateMapperKtTest {
}
@Test
fun `should map to email username IncomingConfigState when AutoDiscovery empty username`() {
fun `should map to empty username IncomingConfigState when AutoDiscovery empty username`() {
val incomingConfigState = AUTO_DISCOVERY_STATE_USERNAME_EMPTY.toIncomingConfigState()
assertThat(incomingConfigState).isEqualTo(
@ -66,7 +68,8 @@ class AccountAutoDiscoveryStateMapperKtTest {
server = StringInputField(value = AUTO_DISCOVERY_HOSTNAME.value),
security = AUTO_DISCOVERY_SECURITY.toConnectionSecurity(),
port = NumberInputField(value = AUTO_DISCOVERY_PORT_IMAP.value.toLong()),
username = StringInputField(value = EMAIL_ADDRESS),
authenticationType = AuthenticationType.PasswordEncrypted,
username = StringInputField(value = ""),
password = StringInputField(value = PASSWORD),
),
)
@ -100,6 +103,7 @@ class AccountAutoDiscoveryStateMapperKtTest {
server = StringInputField(value = AUTO_DISCOVERY_HOSTNAME.value),
security = AUTO_DISCOVERY_SECURITY.toConnectionSecurity(),
port = NumberInputField(value = AUTO_DISCOVERY_PORT_SMTP.value.toLong()),
authenticationType = AuthenticationType.PasswordEncrypted,
username = StringInputField(value = AUTO_DISCOVERY_USERNAME),
password = StringInputField(value = PASSWORD),
),
@ -107,7 +111,7 @@ class AccountAutoDiscoveryStateMapperKtTest {
}
@Test
fun `should map to email username OutgoingConfigState when AutoDiscovery empty username`() {
fun `should map to empty username OutgoingConfigState when AutoDiscovery empty username`() {
val outgoingConfigState = AUTO_DISCOVERY_STATE_USERNAME_EMPTY.toOutgoingConfigState()
assertThat(outgoingConfigState).isEqualTo(
@ -115,7 +119,8 @@ class AccountAutoDiscoveryStateMapperKtTest {
server = StringInputField(value = AUTO_DISCOVERY_HOSTNAME.value),
security = AUTO_DISCOVERY_SECURITY.toConnectionSecurity(),
port = NumberInputField(value = AUTO_DISCOVERY_PORT_SMTP.value.toLong()),
username = StringInputField(value = EMAIL_ADDRESS),
authenticationType = AuthenticationType.PasswordEncrypted,
username = StringInputField(value = ""),
password = StringInputField(value = PASSWORD),
),
)