From b40ad5413cd29aad2d0212d5aa1c58e02f4d8a96 Mon Sep 17 00:00:00 2001 From: Ashish Agrawal Date: Thu, 19 May 2022 17:46:04 -0700 Subject: [PATCH] add integ email test Signed-off-by: Ashish Agrawal --- .../integtest/PluginRestTestCase.kt | 5 ++ .../send/SendTestMessageRestHandlerIT.kt | 81 +++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/notifications/notifications/src/test/kotlin/org/opensearch/integtest/PluginRestTestCase.kt b/notifications/notifications/src/test/kotlin/org/opensearch/integtest/PluginRestTestCase.kt index dc3fe7fc..a9cd3001 100644 --- a/notifications/notifications/src/test/kotlin/org/opensearch/integtest/PluginRestTestCase.kt +++ b/notifications/notifications/src/test/kotlin/org/opensearch/integtest/PluginRestTestCase.kt @@ -36,6 +36,11 @@ abstract class PluginRestTestCase : OpenSearchRestTestCase() { return System.getProperty("https", "false")!!.toBoolean() } + protected fun isLocalHost(): Boolean { + val host = System.getProperty("tests.cluster")!!.toString() + return host.startsWith("localhost:") + } + override fun getProtocol(): String { return if (isHttps()) { "https" diff --git a/notifications/notifications/src/test/kotlin/org/opensearch/integtest/send/SendTestMessageRestHandlerIT.kt b/notifications/notifications/src/test/kotlin/org/opensearch/integtest/send/SendTestMessageRestHandlerIT.kt index 9865a105..2b38314e 100644 --- a/notifications/notifications/src/test/kotlin/org/opensearch/integtest/send/SendTestMessageRestHandlerIT.kt +++ b/notifications/notifications/src/test/kotlin/org/opensearch/integtest/send/SendTestMessageRestHandlerIT.kt @@ -12,6 +12,7 @@ import org.opensearch.integtest.PluginRestTestCase import org.opensearch.notifications.NotificationPlugin.Companion.PLUGIN_BASE_URI import org.opensearch.rest.RestRequest import org.opensearch.rest.RestStatus +import org.springframework.integration.test.mail.TestMailServer internal class SendTestMessageRestHandlerIT : PluginRestTestCase() { @Suppress("EmptyFunctionBlock") @@ -209,4 +210,84 @@ internal class SendTestMessageRestHandlerIT : PluginRestTestCase() { val error = sendResponse.get("error").asJsonObject Assert.assertNotNull(error.get("reason").asString) } + + @Suppress("EmptyFunctionBlock") + fun `test send test smtp email message for localhost successfully`() { + if (isLocalHost()) { + val smtpPort = 10255 + val smtpServer = TestMailServer.smtp(smtpPort) + + val sampleSmtpAccount = SmtpAccount( + "localhost", + smtpPort, + MethodType.NONE, + "szhongna@localhost.com" + ) + // Create smtp account notification config + val smtpAccountCreateRequestJsonString = """ + { + "config":{ + "name":"this is a sample smtp", + "description":"this is a sample smtp description", + "config_type":"smtp_account", + "is_enabled":true, + "smtp_account":{ + "host":"${sampleSmtpAccount.host}", + "port":"${sampleSmtpAccount.port}", + "method":"${sampleSmtpAccount.method}", + "from_address":"${sampleSmtpAccount.fromAddress}" + } + } + } + """.trimIndent() + val createResponse = executeRequest( + RestRequest.Method.POST.name, + "$PLUGIN_BASE_URI/configs", + smtpAccountCreateRequestJsonString, + RestStatus.OK.status + ) + val smtpAccountConfigId = createResponse.get("config_id").asString + Assert.assertNotNull(smtpAccountConfigId) + Thread.sleep(1000) + + val emailCreateRequestJsonString = """ + { + "config":{ + "name":"email config name", + "description":"email description", + "config_type":"email", + "is_enabled":true, + "email":{ + "email_account_id":"$smtpAccountConfigId", + "recipient_list":[ + {"recipient":"chloe@localhost.com"} + ], + "email_group_id_list":[] + } + } + } + """.trimIndent() + + val emailCreateResponse = executeRequest( + RestRequest.Method.POST.name, + "$PLUGIN_BASE_URI/configs", + emailCreateRequestJsonString, + RestStatus.OK.status + ) + val emailConfigId = emailCreateResponse.get("config_id").asString + Assert.assertNotNull(emailConfigId) + Thread.sleep(1000) + + // send test message + val sendResponse = executeRequest( + RestRequest.Method.GET.name, + "$PLUGIN_BASE_URI/feature/test/$emailConfigId", + "", + RestStatus.OK.status + ) + + smtpServer.stop() + smtpServer.resetServer() + } + } }