Skip to content

Commit

Permalink
Added integration test for SES email channel (opensearch-project#22)
Browse files Browse the repository at this point in the history
* added SES IT

* update

* update
  • Loading branch information
chloe-zh authored Nov 4, 2020
1 parent 175c94e commit e52394d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ abstract class NotificationsRestTestCase : ODFERestTestCase() {

private val smtpPort = PluginSettings.smtpPort
private val smtpServer: TestMailServer.SmtpServer
private val fromAddress = "[email protected]"

init {
smtpServer = TestMailServer.smtp(smtpPort)
Expand All @@ -42,6 +43,7 @@ abstract class NotificationsRestTestCase : ODFERestTestCase() {
initClient()
}

resetFromAddress()
init()
}

Expand Down Expand Up @@ -103,14 +105,24 @@ abstract class NotificationsRestTestCase : ODFERestTestCase() {
/** Provided for each test to load test index, data and other setup work */
protected open fun init() {}

protected fun updateFromAddress(address: String): JsonObject? {
protected fun setFromAddress(address: String): JsonObject? {
return updateClusterSettings(
ClusterSetting(
"persistent", "opendistro.notifications.email.fromAddress", address))
}

protected fun resetFromAddress(): JsonObject? {
return updateFromAddress(PluginSettings.emailFromAddress)
return setFromAddress(fromAddress)
}

protected fun setChannelType(type: String) {
updateClusterSettings(ClusterSetting(
"persistent", "opendistro.notifications.email.channel", type
))
}

protected fun resetChannelType() {
setChannelType(PluginSettings.emailChannel)
}

@Throws(IOException::class)
Expand Down Expand Up @@ -139,7 +151,7 @@ abstract class NotificationsRestTestCase : ODFERestTestCase() {
updateClusterSettings(ClusterSetting("transient", "*", null))
}

protected class ClusterSetting(val type: String, val name: String, var value: String?) {
protected class ClusterSetting(val type: String, val name: String, var value: Any?) {
init {
this.value = if (value == null) "null" else "\"" + value + "\""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ fun verifyResponse(response: JsonObject, refTag: String, recipients: List<String
}
}

fun getStatusCode(response: JsonObject): Int {
return response
.getAsJsonArray("recipients")
.get(0).asJsonObject
.get("statusCode").asInt
}

fun getStatusText(response: JsonObject): String {
return response
.getAsJsonArray("recipients")
.get(0).asJsonObject
.get("statusText").asString
}

/** Util class to build Json entity of request body */
class NotificationsJsonEntity(
private val refTag: String?,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
*/

package com.amazon.opendistroforelasticsearch.notifications.channel

import com.amazon.opendistroforelasticsearch.notifications.NotificationsRestTestCase
import com.amazon.opendistroforelasticsearch.notifications.jsonify
import com.amazon.opendistroforelasticsearch.notifications.getStatusCode
import com.amazon.opendistroforelasticsearch.notifications.getStatusText
import org.elasticsearch.rest.RestStatus
import org.junit.After

class SesChannelIT : NotificationsRestTestCase() {
private val refTag = "ref"
private val title = "title"
private val textDescription = "text"
private val htmlDescription = "html"
private val attachment = jsonify(
"""
{
"fileName": "odfe.data",
"fileEncoding": "base64",
"fileContentType": "application/octet-stream",
"fileData": "VGVzdCBtZXNzYWdlCgo="
}
""".trimIndent())

override fun init() {
setChannelType("ses")
}

@After
fun reset() {
resetChannelType()
}

fun `test send email over ses channel due to ses authorization failure`() {
val recipients = listOf("mailto:test@localhost")
val response = executeRequest(refTag, recipients, title, textDescription, htmlDescription, attachment)

val statusCode = getStatusCode(response)
assertEquals(RestStatus.SERVICE_UNAVAILABLE.status, statusCode)

val statusText = getStatusText(response)
assertEquals("sendEmail Error, SES status:400:Optional[Bad Request]", statusText)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package com.amazon.opendistroforelasticsearch.notifications.channel

import com.amazon.opendistroforelasticsearch.notifications.NotificationsRestTestCase
import com.amazon.opendistroforelasticsearch.notifications.getStatusCode
import com.amazon.opendistroforelasticsearch.notifications.getStatusText
import com.amazon.opendistroforelasticsearch.notifications.jsonify
import com.amazon.opendistroforelasticsearch.notifications.settings.PluginSettings
import com.amazon.opendistroforelasticsearch.notifications.verifyResponse
import com.google.gson.JsonObject
import org.elasticsearch.rest.RestStatus

internal class SmtpChannelIT : NotificationsRestTestCase() {
Expand Down Expand Up @@ -51,7 +52,7 @@ internal class SmtpChannelIT : NotificationsRestTestCase() {
}

fun `test send email with unconfigured address`() {
updateFromAddress(PluginSettings.UNCONFIGURED_EMAIL_ADDRESS)
setFromAddress(PluginSettings.UNCONFIGURED_EMAIL_ADDRESS)
val recipients = listOf("mailto:test@localhost")
val response = executeRequest(refTag, recipients, title, textDescription, htmlDescription, attachment)

Expand All @@ -62,20 +63,4 @@ internal class SmtpChannelIT : NotificationsRestTestCase() {
assertEquals("Email from: address not configured", statusText)
resetFromAddress()
}

/** Private test util to extract status code from response of the first recipient */
private fun getStatusCode(response: JsonObject): Int {
return response
.getAsJsonArray("recipients")
.get(0).asJsonObject
.get("statusCode").asInt
}

/** Private test util to extract status text from response of the first recipient */
private fun getStatusText(response: JsonObject): String {
return response
.getAsJsonArray("recipients")
.get(0).asJsonObject
.get("statusText").asString
}
}

0 comments on commit e52394d

Please sign in to comment.