Skip to content

Commit

Permalink
Fix Email test for security integration test (opensearch-project#462)
Browse files Browse the repository at this point in the history
* Fix Email test for security integration test

Signed-off-by: Ashish Agrawal <[email protected]>
  • Loading branch information
lezzago committed Jun 29, 2022
1 parent b5c5570 commit 6a7d92a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.notifications.core.integTest
package org.opensearch.notifications.core.smtp

import org.junit.After
import org.junit.jupiter.api.Test
import org.opensearch.notifications.core.NotificationCoreImpl
import org.opensearch.notifications.core.transport.DestinationTransportProvider
import org.opensearch.notifications.core.transport.SmtpDestinationTransport
import org.opensearch.notifications.spi.model.MessageContent
import org.opensearch.notifications.spi.model.destination.DestinationType
import org.opensearch.notifications.spi.model.destination.SmtpDestination
import org.opensearch.rest.RestStatus
import org.opensearch.test.rest.OpenSearchRestTestCase
import org.springframework.integration.test.mail.TestMailServer
import kotlin.test.assertEquals

internal class SmtpEmailIT : OpenSearchRestTestCase() {
class SmtpEmailTests {

private val smtpServer: TestMailServer.SmtpServer
private val smtpPort = 10255 // use non-standard port > 1024 to avoid permission issue

init {
smtpServer = TestMailServer.smtp(smtpPort)
internal companion object {
private const val smtpPort = 10255 // use non-standard port > 1024 to avoid permission issue
private val smtpServer = TestMailServer.smtp(smtpPort)
}

@After
Expand All @@ -28,6 +30,7 @@ internal class SmtpEmailIT : OpenSearchRestTestCase() {
smtpServer.resetServer()
}

@Test
fun `test send email to one recipient over smtp server`() {
val smtpDestination = SmtpDestination(
"testAccountName",
Expand All @@ -46,11 +49,13 @@ internal class SmtpEmailIT : OpenSearchRestTestCase() {
"VGVzdCBtZXNzYWdlCgo=",
"application/octet-stream",
)
DestinationTransportProvider.destinationTransportMap = mapOf(DestinationType.SMTP to SmtpDestinationTransport())
val response = NotificationCoreImpl.sendMessage(smtpDestination, message, "ref")
assertEquals("Success", response.statusText)
assertEquals(RestStatus.OK.status, response.statusCode)
}

@Test
fun `test send email with non-available host`() {
val smtpDestination = SmtpDestination(
"testAccountName",
Expand All @@ -69,6 +74,7 @@ internal class SmtpEmailIT : OpenSearchRestTestCase() {
"VGVzdCBtZXNzYWdlCgo=",
"application/octet-stream",
)
DestinationTransportProvider.destinationTransportMap = mapOf(DestinationType.SMTP to SmtpDestinationTransport())
val response = NotificationCoreImpl.sendMessage(smtpDestination, message, "ref")
assertEquals(
"sendEmail Error, status:Couldn't connect to host, port: invalidHost, $smtpPort; timeout -1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ abstract class PluginRestTestCase : OpenSearchRestTestCase() {
return System.getProperty("https", "false")!!.toBoolean()
}

protected fun isLocalHost(): Boolean {
val host = System.getProperty("tests.cluster", "dummyHost")!!.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,
"[email protected]"
)
// 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":"[email protected]"}
],
"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 6a7d92a

Please sign in to comment.