Merge pull request #7088 from thundernest/add_validate_server_settings_use_case

Add ValidateServerSettings use case
This commit is contained in:
Wolf-Martell Montwé 2023-07-27 10:59:11 +00:00 committed by GitHub
commit 42e5bde2d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 163 additions and 0 deletions

View file

@ -14,6 +14,10 @@ interface DomainContract {
suspend fun execute(emailAddress: String): AutoDiscoveryResult
}
fun interface ValidateServerSettings {
suspend fun execute(settings: ServerSettings): ServerSettingsValidationResult
}
fun interface CheckIncomingServerConfig {
suspend fun execute(
protocolType: IncomingProtocolType,

View file

@ -0,0 +1,28 @@
package app.k9mail.feature.account.setup.domain.usecase
import app.k9mail.feature.account.setup.domain.DomainContract.UseCase
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mail.server.ServerSettingsValidationResult
import com.fsck.k9.mail.server.ServerSettingsValidator
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
internal class ValidateServerSettings(
private val imapValidator: ServerSettingsValidator,
private val pop3Validator: ServerSettingsValidator,
private val smtpValidator: ServerSettingsValidator,
private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO,
) : UseCase.ValidateServerSettings {
override suspend fun execute(settings: ServerSettings): ServerSettingsValidationResult =
withContext(coroutineDispatcher) {
return@withContext when (settings.type) {
"imap" -> imapValidator.checkServerSettings(settings)
"pop3" -> pop3Validator.checkServerSettings(settings)
"smtp" -> smtpValidator.checkServerSettings(settings)
else -> {
throw IllegalArgumentException("Unsupported server type: ${settings.type}")
}
}
}
}

View file

@ -0,0 +1,131 @@
package app.k9mail.feature.account.setup.domain.usecase
import assertk.assertThat
import assertk.assertions.isEqualTo
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mail.server.ServerSettingsValidationResult
import java.io.IOException
import kotlinx.coroutines.test.runTest
import org.junit.Test
class ValidateServerSettingsTest {
@Test
fun `should check with imap validator when protocol is imap`() = runTest {
val testSubject = ValidateServerSettings(
imapValidator = { ServerSettingsValidationResult.Success },
pop3Validator = { ServerSettingsValidationResult.NetworkError(IOException("Failed POP3")) },
smtpValidator = { ServerSettingsValidationResult.NetworkError(IOException("Failed SMTP")) },
)
val result = testSubject.execute(IMAP_SERVER_SETTINGS)
assertThat(result).isEqualTo(ServerSettingsValidationResult.Success)
}
@Test
fun `should check with imap validator when protocol is imap and return failure`() = runTest {
val failure = ServerSettingsValidationResult.ServerError("Failed")
val testSubject = ValidateServerSettings(
imapValidator = { failure },
pop3Validator = { ServerSettingsValidationResult.NetworkError(IOException("Failed POP3")) },
smtpValidator = { ServerSettingsValidationResult.NetworkError(IOException("Failed SMTP")) },
)
val result = testSubject.execute(IMAP_SERVER_SETTINGS)
assertThat(result).isEqualTo(failure)
}
@Test
fun `should check with pop3 validator when protocol is pop3`() = runTest {
val testSubject = ValidateServerSettings(
imapValidator = { ServerSettingsValidationResult.NetworkError(IOException("Failed IMAP")) },
pop3Validator = { ServerSettingsValidationResult.Success },
smtpValidator = { ServerSettingsValidationResult.NetworkError(IOException("Failed SMTP")) },
)
val result = testSubject.execute(POP3_SERVER_SETTINGS)
assertThat(result).isEqualTo(ServerSettingsValidationResult.Success)
}
@Test
fun `should check with pop3 validator when protocol is pop3 and return failure`() = runTest {
val failure = ServerSettingsValidationResult.ServerError("Failed POP3")
val testSubject = ValidateServerSettings(
imapValidator = { ServerSettingsValidationResult.NetworkError(IOException("Failed IMAP")) },
pop3Validator = { failure },
smtpValidator = { ServerSettingsValidationResult.NetworkError(IOException("Failed SMTP")) },
)
val result = testSubject.execute(POP3_SERVER_SETTINGS)
assertThat(result).isEqualTo(failure)
}
@Test
fun `should check with smtp validator when protocol is smtp`() = runTest {
val testSubject = ValidateServerSettings(
imapValidator = { ServerSettingsValidationResult.NetworkError(IOException("Failed IMAP")) },
pop3Validator = { ServerSettingsValidationResult.NetworkError(IOException("Failed POP3")) },
smtpValidator = { ServerSettingsValidationResult.Success },
)
val result = testSubject.execute(SMTP_SERVER_SETTINGS)
assertThat(result).isEqualTo(ServerSettingsValidationResult.Success)
}
@Test
fun `should check with smtp validator when protocol is smtp and return failure`() = runTest {
val failure = ServerSettingsValidationResult.ServerError("Failed SMTP")
val testSubject = ValidateServerSettings(
imapValidator = { ServerSettingsValidationResult.NetworkError(IOException("Failed IMAP")) },
pop3Validator = { ServerSettingsValidationResult.NetworkError(IOException("Failed POP3")) },
smtpValidator = { failure },
)
val result = testSubject.execute(SMTP_SERVER_SETTINGS)
assertThat(result).isEqualTo(failure)
}
private companion object {
val IMAP_SERVER_SETTINGS = ServerSettings(
type = "imap",
host = "imap.example.org",
port = 993,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "password",
clientCertificateAlias = null,
)
val POP3_SERVER_SETTINGS = ServerSettings(
type = "pop3",
host = "pop3.example.org",
port = 993,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "password",
clientCertificateAlias = null,
)
val SMTP_SERVER_SETTINGS = ServerSettings(
type = "smtp",
host = "smtp.example.org",
port = 993,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "password",
clientCertificateAlias = null,
)
}
}