Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use password from incoming server for outgoing server if necessary #7377

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ class OutgoingServerSettingsStateMapperKtTest {
assertThat(result).isEqualTo(State(username = StringInputField(value = "[email protected]")))
}

@Test
fun `should map to state with password from incomingServerSettings when outgoingServerSettings is null`() {
val accountState = AccountState(
emailAddress = "[email protected]",
incomingServerSettings = IMAP_SERVER_SETTINGS,
outgoingServerSettings = null,
)

val result = accountState.toOutgoingServerSettingsState()

assertThat(result).isEqualTo(
State(
username = StringInputField(value = "[email protected]"),
password = StringInputField(value = INCOMING_SERVER_PASSWORD),
),
)
}

@Test
fun `should map from SMTP server settings to state`() {
val accountState = AccountState(
Expand All @@ -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
Expand Down Expand Up @@ -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,
)
}
}