Skip to content

Commit

Permalink
Format Kotlin code
Browse files Browse the repository at this point in the history
  • Loading branch information
cketti committed Oct 16, 2020
1 parent 10be88d commit 1873593
Show file tree
Hide file tree
Showing 112 changed files with 1,390 additions and 994 deletions.
Expand Up @@ -30,40 +30,44 @@ class ProvidersXmlDiscovery(
val emailUrlEncoded = UrlEncodingHelper.encodeUtf8(email)

val incomingUserUrlEncoded = provider.incomingUsernameTemplate
.replace("\$email", emailUrlEncoded)
.replace("\$user", userUrlEncoded)
.replace("\$domain", domain)
.replace("\$email", emailUrlEncoded)
.replace("\$user", userUrlEncoded)
.replace("\$domain", domain)
val incomingUri = with(URI(provider.incomingUriTemplate)) {
URI(scheme, "$incomingUserUrlEncoded:$password", host, port, null, null, null).toString()
}
val incomingSettings = backendManager.decodeStoreUri(incomingUri).let {
listOf(DiscoveredServerSettings(
it.type,
it.host,
it.port,
it.connectionSecurity,
it.authenticationType,
it.username
))
listOf(
DiscoveredServerSettings(
it.type,
it.host,
it.port,
it.connectionSecurity,
it.authenticationType,
it.username
)
)
}

val outgoingUserUrlEncoded = provider.outgoingUsernameTemplate
?.replace("\$email", emailUrlEncoded)
?.replace("\$user", userUrlEncoded)
?.replace("\$domain", domain)
?.replace("\$email", emailUrlEncoded)
?.replace("\$user", userUrlEncoded)
?.replace("\$domain", domain)
val outgoingUserInfo = if (outgoingUserUrlEncoded != null) "$outgoingUserUrlEncoded:$password" else null
val outgoingUri = with(URI(provider.outgoingUriTemplate)) {
URI(scheme, outgoingUserInfo, host, port, null, null, null).toString()
}
val outgoingSettings = backendManager.decodeTransportUri(outgoingUri).let {
listOf(DiscoveredServerSettings(
it.type,
it.host,
it.port,
it.connectionSecurity,
it.authenticationType,
it.username
))
listOf(
DiscoveredServerSettings(
it.type,
it.host,
it.port,
it.connectionSecurity,
it.authenticationType,
it.username
)
)
}

return DiscoveryResults(incomingSettings, outgoingSettings)
Expand Down Expand Up @@ -120,8 +124,7 @@ class ProvidersXmlDiscovery(
}
} while (!(xmlEventType == XmlPullParser.END_TAG && xml.name == "provider"))

return if (incomingUriTemplate != null && incomingUsernameTemplate != null &&
outgoingUriTemplate != null) {
return if (incomingUriTemplate != null && incomingUsernameTemplate != null && outgoingUriTemplate != null) {
Provider(incomingUriTemplate, incomingUsernameTemplate, outgoingUriTemplate, outgoingUsernameTemplate)
} else {
null
Expand Down
Expand Up @@ -46,7 +46,8 @@ 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", DiscoveryTarget.INCOMING_AND_OUTGOING
)

assertThat(connectionSettings).isNull()
}
Expand Down
Expand Up @@ -28,8 +28,9 @@ class SrvServiceDiscoveryTest : RobolectricTest() {
fun discover_whenNoSMTP_shouldReturnJustIMAP() {
val srvResolver = newMockSrvResolver(
imapServices = listOf(newMailService(port = 143, srvType = SrvType.IMAP)),
imapsServices = listOf(newMailService(port = 993, srvType = SrvType.IMAPS,
security = ConnectionSecurity.SSL_TLS_REQUIRED))
imapsServices = listOf(
newMailService(port = 993, srvType = SrvType.IMAPS, security = ConnectionSecurity.SSL_TLS_REQUIRED)
)
)

val srvServiceDiscovery = SrvServiceDiscovery(srvResolver)
Expand All @@ -41,10 +42,20 @@ class SrvServiceDiscoveryTest : RobolectricTest() {

@Test
fun discover_whenNoIMAP_shouldReturnJustSMTP() {
val srvResolver = newMockSrvResolver(submissionServices = listOf(
newMailService(port = 25, srvType = SrvType.SUBMISSION, security = ConnectionSecurity.STARTTLS_REQUIRED),
newMailService(port = 465, srvType = SrvType.SUBMISSIONS, security = ConnectionSecurity.SSL_TLS_REQUIRED)
))
val srvResolver = newMockSrvResolver(
submissionServices = listOf(
newMailService(
port = 25,
srvType = SrvType.SUBMISSION,
security = ConnectionSecurity.STARTTLS_REQUIRED
),
newMailService(
port = 465,
srvType = SrvType.SUBMISSIONS,
security = ConnectionSecurity.SSL_TLS_REQUIRED
)
)
)

val srvServiceDiscovery = SrvServiceDiscovery(srvResolver)
val result = srvServiceDiscovery.discover("test@example.com", DiscoveryTarget.INCOMING_AND_OUTGOING)
Expand All @@ -55,64 +66,109 @@ class SrvServiceDiscoveryTest : RobolectricTest() {

@Test
fun discover_withRequiredServices_shouldCorrectlyPrioritize() {
val srvResolver = newMockSrvResolver(submissionServices = listOf(
newMailService(
host = "smtp1.example.com", port = 25, srvType = SrvType.SUBMISSION,
security = ConnectionSecurity.STARTTLS_REQUIRED, priority = 0),
newMailService(
host = "smtp2.example.com", port = 25, srvType = SrvType.SUBMISSION,
security = ConnectionSecurity.STARTTLS_REQUIRED, priority = 1)
), submissionsServices = listOf(
newMailService(
host = "smtp3.example.com", port = 465, srvType = SrvType.SUBMISSIONS,
security = ConnectionSecurity.SSL_TLS_REQUIRED, priority = 0),
newMailService(
host = "smtp4.example.com", port = 465, srvType = SrvType.SUBMISSIONS,
security = ConnectionSecurity.SSL_TLS_REQUIRED, priority = 1)
), imapServices = listOf(
newMailService(
host = "imap1.example.com", port = 143, srvType = SrvType.IMAP,
security = ConnectionSecurity.STARTTLS_REQUIRED, priority = 0),
newMailService(
host = "imap2.example.com", port = 143, srvType = SrvType.IMAP,
security = ConnectionSecurity.STARTTLS_REQUIRED, priority = 1)
), imapsServices = listOf(
newMailService(
host = "imaps1.example.com", port = 993, srvType = SrvType.IMAPS,
security = ConnectionSecurity.SSL_TLS_REQUIRED, priority = 0),
newMailService(
host = "imaps2.example.com", port = 993, srvType = SrvType.IMAPS,
security = ConnectionSecurity.SSL_TLS_REQUIRED, priority = 1)
))
val srvResolver = newMockSrvResolver(
submissionServices = listOf(
newMailService(
host = "smtp1.example.com",
port = 25,
srvType = SrvType.SUBMISSION,
security = ConnectionSecurity.STARTTLS_REQUIRED,
priority = 0
),
newMailService(
host = "smtp2.example.com",
port = 25,
srvType = SrvType.SUBMISSION,
security = ConnectionSecurity.STARTTLS_REQUIRED,
priority = 1
)
),
submissionsServices = listOf(
newMailService(
host = "smtp3.example.com",
port = 465,
srvType = SrvType.SUBMISSIONS,
security = ConnectionSecurity.SSL_TLS_REQUIRED,
priority = 0
),
newMailService(
host = "smtp4.example.com",
port = 465,
srvType = SrvType.SUBMISSIONS,
security = ConnectionSecurity.SSL_TLS_REQUIRED,
priority = 1
)
),
imapServices = listOf(
newMailService(
host = "imap1.example.com",
port = 143,
srvType = SrvType.IMAP,
security = ConnectionSecurity.STARTTLS_REQUIRED,
priority = 0
),
newMailService(
host = "imap2.example.com",
port = 143,
srvType = SrvType.IMAP,
security = ConnectionSecurity.STARTTLS_REQUIRED,
priority = 1
)
),
imapsServices = listOf(
newMailService(
host = "imaps1.example.com",
port = 993,
srvType = SrvType.IMAPS,
security = ConnectionSecurity.SSL_TLS_REQUIRED,
priority = 0
),
newMailService(
host = "imaps2.example.com",
port = 993,
srvType = SrvType.IMAPS,
security = ConnectionSecurity.SSL_TLS_REQUIRED,
priority = 1
)
)
)

val srvServiceDiscovery = SrvServiceDiscovery(srvResolver)
val result = srvServiceDiscovery.discover("test@example.com", DiscoveryTarget.INCOMING_AND_OUTGOING)

assertEquals(listOf(
"smtp3.example.com",
"smtp1.example.com",
"smtp4.example.com",
"smtp2.example.com"
),
assertEquals(
listOf(
"smtp3.example.com",
"smtp1.example.com",
"smtp4.example.com",
"smtp2.example.com"
),
result?.outgoing?.map { it.host }
)
assertEquals(listOf(
"imaps1.example.com",
"imap1.example.com",
"imaps2.example.com",
"imap2.example.com"
),
assertEquals(
listOf(
"imaps1.example.com",
"imap1.example.com",
"imaps2.example.com",
"imap2.example.com"
),
result?.incoming?.map { it.host }
)
}

@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 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)
Expand All @@ -126,11 +182,17 @@ class SrvServiceDiscoveryTest : RobolectricTest() {

@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 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)
Expand Down
Expand Up @@ -29,11 +29,11 @@ class ThunderbirdAutoconfigFetcher(private val okHttpClient: OkHttpClient) {
requireNotNull(domain) { "Couldn't extract domain from email address: $email" }

return HttpUrl.Builder()
.scheme("https")
.host(domain)
.addEncodedPathSegments(".well-known/autoconfig/mail/config-v1.1.xml")
.addQueryParameter("emailaddress", email)
.build()
.scheme("https")
.host(domain)
.addEncodedPathSegments(".well-known/autoconfig/mail/config-v1.1.xml")
.addQueryParameter("emailaddress", email)
.build()
}
}
}
Expand Up @@ -154,19 +154,29 @@ class ThunderbirdAutoconfigTest : RobolectricTest() {

val connectionSettings = parser.parseSettings(input, "test@metacode.biz")

assertThat(connectionSettings).isEqualTo(DiscoveryResults(listOf(
DiscoveredServerSettings(
protocol = "imap", host = "imap.googlemail.com", port = 993,
security = ConnectionSecurity.SSL_TLS_REQUIRED, authType = AuthType.PLAIN,
username = "test@metacode.biz")
), listOf()))
assertThat(connectionSettings).isEqualTo(
DiscoveryResults(
listOf(
DiscoveredServerSettings(
protocol = "imap",
host = "imap.googlemail.com",
port = 993,
security = ConnectionSecurity.SSL_TLS_REQUIRED,
authType = AuthType.PLAIN,
username = "test@metacode.biz"
)
),
listOf()
)
)
}

@Test
fun generatedUrls() {
val autoDiscoveryAddress = ThunderbirdAutoconfigFetcher.getAutodiscoveryAddress("test@metacode.biz")

assertThat(autoDiscoveryAddress.toString()).isEqualTo("https://metacode.biz/" +
".well-known/autoconfig/mail/config-v1.1.xml?emailaddress=test%40metacode.biz")
assertThat(autoDiscoveryAddress.toString()).isEqualTo(
"https://metacode.biz/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=test%40metacode.biz"
)
}
}

0 comments on commit 1873593

Please sign in to comment.