Remove DiscoveryTarget

This commit is contained in:
cketti 2022-10-26 18:48:52 +02:00
parent 35b380a97d
commit 880b4d88b1
7 changed files with 19 additions and 82 deletions

View file

@ -4,13 +4,7 @@ import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
interface ConnectionSettingsDiscovery {
fun discover(email: String, target: DiscoveryTarget): DiscoveryResults?
}
enum class DiscoveryTarget(val outgoing: Boolean, val incoming: Boolean) {
OUTGOING(true, false),
INCOMING(false, true),
INCOMING_AND_OUTGOING(true, true)
fun discover(email: String): DiscoveryResults?
}
data class DiscoveryResults(val incoming: List<DiscoveredServerSettings>, val outgoing: List<DiscoveredServerSettings>)

View file

@ -5,7 +5,6 @@ import android.net.Uri
import com.fsck.k9.autodiscovery.api.ConnectionSettingsDiscovery
import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings
import com.fsck.k9.autodiscovery.api.DiscoveryResults
import com.fsck.k9.autodiscovery.api.DiscoveryTarget
import com.fsck.k9.helper.EmailHelper
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
@ -19,7 +18,7 @@ class ProvidersXmlDiscovery(
private val oAuthConfigurationProvider: OAuthConfigurationProvider
) : ConnectionSettingsDiscovery {
override fun discover(email: String, target: DiscoveryTarget): DiscoveryResults? {
override fun discover(email: String): DiscoveryResults? {
val domain = EmailHelper.getDomainFromEmailAddress(email) ?: return null
val provider = findProviderForDomain(domain) ?: return null

View file

@ -2,7 +2,6 @@ package com.fsck.k9.autodiscovery.providersxml
import androidx.test.core.app.ApplicationProvider
import com.fsck.k9.RobolectricTest
import com.fsck.k9.autodiscovery.api.DiscoveryTarget
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
import com.fsck.k9.oauth.OAuthConfiguration
@ -17,7 +16,7 @@ class ProvidersXmlDiscoveryTest : RobolectricTest() {
@Test
fun discover_withGmailDomain_shouldReturnCorrectSettings() {
val connectionSettings = providersXmlDiscovery.discover("user@gmail.com", DiscoveryTarget.INCOMING_AND_OUTGOING)
val connectionSettings = providersXmlDiscovery.discover("user@gmail.com")
assertThat(connectionSettings).isNotNull()
with(connectionSettings!!.incoming.first()) {
@ -37,7 +36,7 @@ class ProvidersXmlDiscoveryTest : RobolectricTest() {
@Test
fun discover_withUnknownDomain_shouldReturnNull() {
val connectionSettings = providersXmlDiscovery.discover(
"user@not.present.in.providers.xml.example", DiscoveryTarget.INCOMING_AND_OUTGOING
"user@not.present.in.providers.xml.example"
)
assertThat(connectionSettings).isNull()

View file

@ -3,7 +3,6 @@ package com.fsck.k9.autodiscovery.srvrecords
import com.fsck.k9.autodiscovery.api.ConnectionSettingsDiscovery
import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings
import com.fsck.k9.autodiscovery.api.DiscoveryResults
import com.fsck.k9.autodiscovery.api.DiscoveryTarget
import com.fsck.k9.helper.EmailHelper
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
@ -12,19 +11,19 @@ class SrvServiceDiscovery(
private val srvResolver: MiniDnsSrvResolver
) : ConnectionSettingsDiscovery {
override fun discover(email: String, target: DiscoveryTarget): DiscoveryResults? {
override fun discover(email: String): DiscoveryResults? {
val domain = EmailHelper.getDomainFromEmailAddress(email) ?: return null
val mailServicePriority = compareBy<MailService> { it.priority }.thenByDescending { it.security }
val outgoingSettings = if (target.outgoing)
listOf(SrvType.SUBMISSIONS, SrvType.SUBMISSION).flatMap { srvResolver.lookup(domain, it) }
.sortedWith(mailServicePriority).map { newServerSettings(it, email) }
else listOf()
val outgoingSettings = listOf(SrvType.SUBMISSIONS, SrvType.SUBMISSION)
.flatMap { srvResolver.lookup(domain, it) }
.sortedWith(mailServicePriority)
.map { newServerSettings(it, email) }
val incomingSettings = if (target.incoming)
listOf(SrvType.IMAPS, SrvType.IMAP).flatMap { srvResolver.lookup(domain, it) }
.sortedWith(mailServicePriority).map { newServerSettings(it, email) }
else listOf()
val incomingSettings = listOf(SrvType.IMAPS, SrvType.IMAP)
.flatMap { srvResolver.lookup(domain, it) }
.sortedWith(mailServicePriority)
.map { newServerSettings(it, email) }
return DiscoveryResults(incoming = incomingSettings, outgoing = outgoingSettings)
}

View file

@ -1,15 +1,11 @@
package com.fsck.k9.autodiscovery.srvrecords
import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings
import com.fsck.k9.autodiscovery.api.DiscoveryResults
import com.fsck.k9.autodiscovery.api.DiscoveryTarget
import com.fsck.k9.mail.ConnectionSecurity
import org.junit.Assert.assertEquals
import org.junit.Test
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoMoreInteractions
class SrvServiceDiscoveryTest {
@ -18,7 +14,7 @@ class SrvServiceDiscoveryTest {
val srvResolver = newMockSrvResolver()
val srvServiceDiscovery = SrvServiceDiscovery(srvResolver)
val result = srvServiceDiscovery.discover("test@example.com", DiscoveryTarget.INCOMING_AND_OUTGOING)
val result = srvServiceDiscovery.discover("test@example.com")
assertEquals(DiscoveryResults(listOf(), listOf()), result)
}
@ -33,7 +29,7 @@ class SrvServiceDiscoveryTest {
)
val srvServiceDiscovery = SrvServiceDiscovery(srvResolver)
val result = srvServiceDiscovery.discover("test@example.com", DiscoveryTarget.INCOMING_AND_OUTGOING)
val result = srvServiceDiscovery.discover("test@example.com")
assertEquals(2, result!!.incoming.size)
assertEquals(0, result.outgoing.size)
@ -57,7 +53,7 @@ class SrvServiceDiscoveryTest {
)
val srvServiceDiscovery = SrvServiceDiscovery(srvResolver)
val result = srvServiceDiscovery.discover("test@example.com", DiscoveryTarget.INCOMING_AND_OUTGOING)
val result = srvServiceDiscovery.discover("test@example.com")
assertEquals(0, result!!.incoming.size)
assertEquals(2, result.outgoing.size)
@ -133,7 +129,7 @@ class SrvServiceDiscoveryTest {
)
val srvServiceDiscovery = SrvServiceDiscovery(srvResolver)
val result = srvServiceDiscovery.discover("test@example.com", DiscoveryTarget.INCOMING_AND_OUTGOING)
val result = srvServiceDiscovery.discover("test@example.com")
assertEquals(
listOf(
@ -155,54 +151,6 @@ class SrvServiceDiscoveryTest {
)
}
@Test
fun discover_whenOnlyOutgoingTrue_shouldOnlyFetchOutgoing() {
val srvResolver = newMockSrvResolver(
submissionServices = listOf(
newMailService(
host = "smtp.example.com",
port = 465,
srvType = SrvType.SUBMISSIONS,
security = ConnectionSecurity.SSL_TLS_REQUIRED,
priority = 0
)
)
)
val srvServiceDiscovery = SrvServiceDiscovery(srvResolver)
val result = srvServiceDiscovery.discover("test@example.com", DiscoveryTarget.OUTGOING)
verify(srvResolver).lookup("example.com", SrvType.SUBMISSIONS)
verify(srvResolver).lookup("example.com", SrvType.SUBMISSION)
verifyNoMoreInteractions(srvResolver)
assertEquals(1, result!!.outgoing.size)
assertEquals(listOf<DiscoveredServerSettings>(), result.incoming)
}
@Test
fun discover_whenOnlyIncomingTrue_shouldOnlyFetchIncoming() {
val srvResolver = newMockSrvResolver(
imapsServices = listOf(
newMailService(
host = "imaps.example.com",
port = 993,
srvType = SrvType.IMAPS,
security = ConnectionSecurity.SSL_TLS_REQUIRED,
priority = 0
)
)
)
val srvServiceDiscovery = SrvServiceDiscovery(srvResolver)
val result = srvServiceDiscovery.discover("test@example.com", DiscoveryTarget.INCOMING)
verify(srvResolver).lookup("example.com", SrvType.IMAPS)
verify(srvResolver).lookup("example.com", SrvType.IMAP)
verifyNoMoreInteractions(srvResolver)
assertEquals(1, result!!.incoming.size)
assertEquals(listOf<DiscoveredServerSettings>(), result.outgoing)
}
private fun newMailService(
host: String = "example.com",
priority: Int = 0,

View file

@ -2,14 +2,13 @@ package com.fsck.k9.autodiscovery.thunderbird
import com.fsck.k9.autodiscovery.api.ConnectionSettingsDiscovery
import com.fsck.k9.autodiscovery.api.DiscoveryResults
import com.fsck.k9.autodiscovery.api.DiscoveryTarget
class ThunderbirdDiscovery(
private val fetcher: ThunderbirdAutoconfigFetcher,
private val parser: ThunderbirdAutoconfigParser
) : ConnectionSettingsDiscovery {
override fun discover(email: String, target: DiscoveryTarget): DiscoveryResults? {
override fun discover(email: String): DiscoveryResults? {
val autoconfigInputStream = fetcher.fetchAutoconfigFile(email) ?: return null
return autoconfigInputStream.use {

View file

@ -17,7 +17,6 @@ import com.fsck.k9.Preferences
import com.fsck.k9.account.AccountCreator
import com.fsck.k9.activity.setup.AccountSetupCheckSettings.CheckDirection
import com.fsck.k9.autodiscovery.api.DiscoveredServerSettings
import com.fsck.k9.autodiscovery.api.DiscoveryTarget
import com.fsck.k9.autodiscovery.providersxml.ProvidersXmlDiscovery
import com.fsck.k9.helper.SimpleTextWatcher
import com.fsck.k9.helper.Utility.requiredFieldValid
@ -311,7 +310,7 @@ class AccountSetupBasics : K9Activity() {
}
private fun providersXmlDiscoveryDiscover(email: String): ConnectionSettings? {
val discoveryResults = providersXmlDiscovery.discover(email, DiscoveryTarget.INCOMING_AND_OUTGOING)
val discoveryResults = providersXmlDiscovery.discover(email)
if (discoveryResults == null || discoveryResults.incoming.isEmpty() || discoveryResults.outgoing.isEmpty()) {
return null
}