Skip to content

Commit

Permalink
Add missing extra mapping for incoming and outgoing server settings
Browse files Browse the repository at this point in the history
  • Loading branch information
wmontwe committed Oct 20, 2023
1 parent b90301e commit 7415955
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import app.k9mail.feature.account.common.domain.entity.InteractionMode
import app.k9mail.feature.account.edit.domain.AccountEditDomainContract
import app.k9mail.feature.account.server.settings.ui.incoming.IncomingServerSettingsContract
import app.k9mail.feature.account.server.settings.ui.incoming.IncomingServerSettingsViewModel
import app.k9mail.feature.account.server.settings.ui.incoming.toIncomingConfigState
import app.k9mail.feature.account.server.settings.ui.incoming.toIncomingServerSettingsState
import kotlinx.coroutines.launch

class ModifyIncomingServerSettingsViewModel(
Expand All @@ -27,7 +27,7 @@ class ModifyIncomingServerSettingsViewModel(
val state = accountStateLoader.execute(accountUuid)

updateState {
state.toIncomingConfigState()
state.toIncomingServerSettingsState()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import app.k9mail.feature.account.common.domain.entity.InteractionMode
import app.k9mail.feature.account.edit.domain.AccountEditDomainContract
import app.k9mail.feature.account.server.settings.ui.outgoing.OutgoingServerSettingsContract
import app.k9mail.feature.account.server.settings.ui.outgoing.OutgoingServerSettingsViewModel
import app.k9mail.feature.account.server.settings.ui.outgoing.toOutgoingConfigState
import app.k9mail.feature.account.server.settings.ui.outgoing.toOutgoingServerSettingsState
import kotlinx.coroutines.launch

class ModifyOutgoingServerSettingsViewModel(
Expand All @@ -26,7 +26,7 @@ class ModifyOutgoingServerSettingsViewModel(
val state = accountStateLoader.execute(accountUuid)

updateState {
state.toOutgoingConfigState()
state.toOutgoingServerSettingsState()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class ModifyIncomingServerSettingsViewModelTest {
authenticationType = AuthenticationType.PasswordCleartext,
username = StringInputField(value = "username"),
password = StringInputField(value = "password"),
imapAutodetectNamespaceEnabled = false,
imapPrefix = StringInputField(value = ""),
imapUseCompression = false,
imapSendClientId = false,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ import app.k9mail.feature.account.server.settings.ui.incoming.IncomingServerSett
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mail.store.imap.ImapStoreSettings

fun AccountState.toIncomingConfigState(): State {
val incomingServerSettings = incomingServerSettings
return if (incomingServerSettings == null) {
State(
username = StringInputField(value = emailAddress ?: ""),
)
} else {
State(
protocolType = IncomingProtocolType.fromName(incomingServerSettings.type),
server = StringInputField(value = incomingServerSettings.host ?: ""),
security = incomingServerSettings.connectionSecurity.toConnectionSecurity(),
port = NumberInputField(value = incomingServerSettings.port.toLong()),
authenticationType = incomingServerSettings.authenticationType.toAuthenticationType(),
username = StringInputField(value = incomingServerSettings.username),
password = StringInputField(value = incomingServerSettings.password ?: ""),
)
}
fun AccountState.toIncomingServerSettingsState() = incomingServerSettings?.toIncomingServerSettingsState()
?: State(username = StringInputField(value = emailAddress ?: ""))

private fun ServerSettings.toIncomingServerSettingsState(): State {
return State(
protocolType = IncomingProtocolType.fromName(type),
server = StringInputField(value = host ?: ""),
security = connectionSecurity.toConnectionSecurity(),
port = NumberInputField(value = port.toLong()),
authenticationType = authenticationType.toAuthenticationType(),
username = StringInputField(value = username),
password = StringInputField(value = password ?: ""),
clientCertificateAlias = clientCertificateAlias,
imapAutodetectNamespaceEnabled = extra[ImapStoreSettings.AUTODETECT_NAMESPACE_KEY] == "true",
imapPrefix = StringInputField(value = extra[ImapStoreSettings.PATH_PREFIX_KEY] ?: ""),
imapUseCompression = extra[ImapStoreSettings.USE_COMPRESSION] == "true",
imapSendClientId = extra[ImapStoreSettings.SEND_CLIENT_ID] == "true",
)
}

internal fun State.toServerSettings(): ServerSettings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ open class IncomingServerSettingsViewModel(

protected open fun loadAccountState() {
updateState {
accountStateRepository.getState().toIncomingConfigState()
accountStateRepository.getState().toIncomingServerSettingsState()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,18 @@ 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.toOutgoingConfigState(): State {
val outgoingServerSettings = outgoingServerSettings
return if (outgoingServerSettings == null) {
State(
username = StringInputField(value = emailAddress ?: ""),
)
} else {
State(
server = StringInputField(value = outgoingServerSettings.host ?: ""),
security = outgoingServerSettings.connectionSecurity.toConnectionSecurity(),
port = NumberInputField(value = outgoingServerSettings.port.toLong()),
authenticationType = outgoingServerSettings.authenticationType.toAuthenticationType(),
username = StringInputField(value = outgoingServerSettings.username),
password = StringInputField(value = outgoingServerSettings.password ?: ""),
)
}
fun AccountState.toOutgoingServerSettingsState() = outgoingServerSettings?.toOutgoingServerSettingsState()
?: State(username = StringInputField(value = emailAddress ?: ""))

private fun ServerSettings.toOutgoingServerSettingsState(): 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 ?: ""),
)
}

internal fun State.toServerSettings(): ServerSettings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ open class OutgoingServerSettingsViewModel(

protected open fun loadAccountState() {
updateState {
accountStateRepository.getState().toOutgoingConfigState()
accountStateRepository.getState().toOutgoingServerSettingsState()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.k9mail.feature.account.server.settings.ui.incoming

import app.k9mail.feature.account.common.domain.entity.AccountState
import app.k9mail.feature.account.common.domain.entity.AuthenticationType
import app.k9mail.feature.account.common.domain.entity.ConnectionSecurity
import app.k9mail.feature.account.common.domain.entity.IncomingProtocolType
Expand All @@ -17,8 +18,48 @@ import org.junit.Test
class IncomingServerSettingsStateMapperKtTest {

@Test
fun `should map to IMAP server settings`() {
val incomingState = State(
fun `should map to state with email as username when server settings are null`() {
val accountState = AccountState(
emailAddress = "[email protected]",
incomingServerSettings = null,
)

val result = accountState.toIncomingServerSettingsState()

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

@Test
fun `should map from IMAP server settings to state`() {
val serverSettings = AccountState(
incomingServerSettings = IMAP_SERVER_SETTINGS,
)

val result = serverSettings.toIncomingServerSettingsState()

assertThat(result).isEqualTo(INCOMING_IMAP_STATE)
}

@Test
fun `should map from state to IMAP server settings`() {
val incomingState = INCOMING_IMAP_STATE

val result = incomingState.toServerSettings()

assertThat(result).isEqualTo(IMAP_SERVER_SETTINGS)
}

@Test
fun `should map from state to POP3 server settings`() {
val incomingState = INCOMING_POP3_STATE

val result = incomingState.toServerSettings()

assertThat(result).isEqualTo(POP3_SERVER_SETTINGS)
}

private companion object {
private val INCOMING_IMAP_STATE = State(
protocolType = IncomingProtocolType.IMAP,
server = StringInputField(value = "imap.example.org"),
port = NumberInputField(value = 993),
Expand All @@ -27,64 +68,49 @@ class IncomingServerSettingsStateMapperKtTest {
username = StringInputField(value = "user"),
password = StringInputField(value = "password"),
clientCertificateAlias = null,
imapAutodetectNamespaceEnabled = true,
imapAutodetectNamespaceEnabled = false,
imapPrefix = StringInputField(value = "prefix"),
imapUseCompression = true,
imapSendClientId = true,
)

val result = incomingState.toServerSettings()

assertThat(result).isEqualTo(
ServerSettings(
type = "imap",
host = "imap.example.org",
port = 993,
connectionSecurity = MailConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "password",
clientCertificateAlias = null,
extra = ImapStoreSettings.createExtra(
autoDetectNamespace = true,
pathPrefix = null,
useCompression = true,
sendClientId = true,
),
private val IMAP_SERVER_SETTINGS = ServerSettings(
type = "imap",
host = "imap.example.org",
port = 993,
connectionSecurity = MailConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "password",
clientCertificateAlias = null,
extra = ImapStoreSettings.createExtra(
autoDetectNamespace = false,
pathPrefix = "prefix",
useCompression = true,
sendClientId = true,
),
)
}

@Test
fun `should map to POP3 server settings`() {
val incomingState = State(
private val INCOMING_POP3_STATE = State(
protocolType = IncomingProtocolType.POP3,
server = StringInputField(value = "pop3.domain.example"),
server = StringInputField(value = "pop3.example.org"),
port = NumberInputField(value = 995),
security = ConnectionSecurity.TLS,
authenticationType = AuthenticationType.PasswordCleartext,
username = StringInputField(value = "user"),
password = StringInputField(value = "password"),
clientCertificateAlias = null,
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,
),
private val POP3_SERVER_SETTINGS = ServerSettings(
type = "pop3",
host = "pop3.example.org",
port = 995,
connectionSecurity = MailConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "password",
clientCertificateAlias = null,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class IncomingServerSettingsViewModelTest {
authenticationType = AuthenticationType.PasswordCleartext,
username = StringInputField(value = "username"),
password = StringInputField(value = "password"),
imapAutodetectNamespaceEnabled = false,
imapPrefix = StringInputField(value = ""),
imapUseCompression = false,
imapSendClientId = false,
),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.k9mail.feature.account.server.settings.ui.outgoing

import app.k9mail.feature.account.common.domain.entity.AccountState
import app.k9mail.feature.account.common.domain.entity.AuthenticationType
import app.k9mail.feature.account.common.domain.entity.ConnectionSecurity
import app.k9mail.feature.account.common.domain.entity.MailConnectionSecurity
Expand All @@ -15,8 +16,39 @@ import org.junit.Test
class OutgoingServerSettingsStateMapperKtTest {

@Test
fun `should map to server settings`() {
val outgoingState = State(
fun `should map to state with email as username when server settings are null`() {
val accountState = AccountState(
emailAddress = "[email protected]",
outgoingServerSettings = null,
)

val result = accountState.toOutgoingServerSettingsState()

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

@Test
fun `should map from SMTP server settings to state`() {
val accountState = AccountState(
outgoingServerSettings = SMTP_SERVER_SETTINGS,
)

val result = accountState.toOutgoingServerSettingsState()

assertThat(result).isEqualTo(OUTGOING_STATE)
}

@Test
fun `should map state to server settings`() {
val outgoingState = OUTGOING_STATE

val result = outgoingState.toServerSettings()

assertThat(result).isEqualTo(SMTP_SERVER_SETTINGS)
}

private companion object {
private val OUTGOING_STATE = State(
server = StringInputField(value = "smtp.example.org"),
port = NumberInputField(value = 587),
security = ConnectionSecurity.TLS,
Expand All @@ -26,19 +58,15 @@ class OutgoingServerSettingsStateMapperKtTest {
clientCertificateAlias = null,
)

val result = outgoingState.toServerSettings()

assertThat(result).isEqualTo(
ServerSettings(
type = "smtp",
host = "smtp.example.org",
port = 587,
connectionSecurity = MailConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "password",
clientCertificateAlias = null,
),
private val SMTP_SERVER_SETTINGS = ServerSettings(
type = "smtp",
host = "smtp.example.org",
port = 587,
connectionSecurity = MailConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "password",
clientCertificateAlias = null,
)
}
}
Loading

0 comments on commit 7415955

Please sign in to comment.