Skip to content

Commit

Permalink
add integ email test
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Agrawal <ashisagr@amazon.com>
  • Loading branch information
lezzago committed May 20, 2022
1 parent 964e384 commit b40ad54
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
}
}
}

0 comments on commit b40ad54

Please sign in to comment.