Use password from incoming server for outgoing server if necessary

This commit is contained in:
cketti 2023-11-24 12:58:45 +01:00
parent 1596adc0c5
commit 730279288a
2 changed files with 76 additions and 4 deletions

View file

@ -10,17 +10,28 @@ import app.k9mail.feature.account.common.domain.input.StringInputField
import app.k9mail.feature.account.server.settings.ui.outgoing.OutgoingServerSettingsContract.State
import com.fsck.k9.mail.ServerSettings
fun AccountState.toOutgoingServerSettingsState() = outgoingServerSettings?.toOutgoingServerSettingsState()
?: State(username = StringInputField(value = emailAddress ?: ""))
fun AccountState.toOutgoingServerSettingsState(): State {
val password = getOutgoingServerPassword()
private fun ServerSettings.toOutgoingServerSettingsState(): State {
return outgoingServerSettings?.toOutgoingServerSettingsState(password)
?: State(
username = StringInputField(value = emailAddress ?: ""),
password = StringInputField(value = password),
)
}
private fun AccountState.getOutgoingServerPassword(): String {
return outgoingServerSettings?.password ?: incomingServerSettings?.password ?: ""
}
private fun ServerSettings.toOutgoingServerSettingsState(password: String): State {
return State(
server = StringInputField(value = host ?: ""),
security = connectionSecurity.toConnectionSecurity(),
port = NumberInputField(value = port.toLong()),
authenticationType = authenticationType.toAuthenticationType(),
username = StringInputField(value = username),
password = StringInputField(value = password ?: ""),
password = StringInputField(value = password),
)
}

View file

@ -27,6 +27,24 @@ class OutgoingServerSettingsStateMapperKtTest {
assertThat(result).isEqualTo(State(username = StringInputField(value = "test@example.com")))
}
@Test
fun `should map to state with password from incomingServerSettings when outgoingServerSettings is null`() {
val accountState = AccountState(
emailAddress = "test@domain.example",
incomingServerSettings = IMAP_SERVER_SETTINGS,
outgoingServerSettings = null,
)
val result = accountState.toOutgoingServerSettingsState()
assertThat(result).isEqualTo(
State(
username = StringInputField(value = "test@domain.example"),
password = StringInputField(value = INCOMING_SERVER_PASSWORD),
),
)
}
@Test
fun `should map from SMTP server settings to state`() {
val accountState = AccountState(
@ -38,6 +56,37 @@ class OutgoingServerSettingsStateMapperKtTest {
assertThat(result).isEqualTo(OUTGOING_STATE)
}
@Test
fun `should use password from incomingServerSettings when outgoingServerSettings contains no password`() {
val accountState = AccountState(
incomingServerSettings = IMAP_SERVER_SETTINGS,
outgoingServerSettings = SMTP_SERVER_SETTINGS.copy(password = null),
)
val result = accountState.toOutgoingServerSettingsState()
assertThat(result).isEqualTo(
OUTGOING_STATE.copy(
password = StringInputField(value = INCOMING_SERVER_PASSWORD),
),
)
}
@Test
fun `should use empty password if neither incomingServerSettings nor outgoingServerSettings contain passwords`() {
val accountState = AccountState(
outgoingServerSettings = SMTP_SERVER_SETTINGS.copy(password = null),
)
val result = accountState.toOutgoingServerSettingsState()
assertThat(result).isEqualTo(
OUTGOING_STATE.copy(
password = StringInputField(value = ""),
),
)
}
@Test
fun `should map state to server settings`() {
val outgoingState = OUTGOING_STATE
@ -68,5 +117,17 @@ class OutgoingServerSettingsStateMapperKtTest {
password = "password",
clientCertificateAlias = null,
)
private const val INCOMING_SERVER_PASSWORD = "incoming-password"
private val IMAP_SERVER_SETTINGS = ServerSettings(
type = "imap",
host = "imap.domain.example",
port = 993,
connectionSecurity = MailConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = INCOMING_SERVER_PASSWORD,
clientCertificateAlias = null,
)
}
}