Skip to content

Commit

Permalink
Add microsoft teams validation error message (opensearch-project#746)
Browse files Browse the repository at this point in the history
* add validation failure message for Microsoft Teams

Signed-off-by: zhichao-aws <[email protected]>

* modify integtest

Signed-off-by: zhichao-aws <[email protected]>

---------

Signed-off-by: zhichao-aws <[email protected]>
Signed-off-by: Aniruddh <[email protected]>
  • Loading branch information
zhichao-aws authored and Noir01 committed Nov 16, 2023
1 parent 0ff5ba0 commit fbad722
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ object ConfigIndexingActions {
}

private fun validateMicrosoftTeamsConfig(microsoftTeams: MicrosoftTeams, user: User?) {
require(microsoftTeams.url.contains(Regex("https://.*\\.webhook\\.office\\.com")))
require(microsoftTeams.url.contains(Regex("https://.*\\.webhook\\.office\\.com"))) {
"Wrong Microsoft Teams url. Should contain \"webhook.office.com\""
}
}

@Suppress("UnusedPrivateMember")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
package org.opensearch.integtest.config

import org.junit.Assert
import org.opensearch.client.Request
import org.opensearch.client.RequestOptions
import org.opensearch.client.ResponseException
import org.opensearch.commons.notifications.model.ConfigType
import org.opensearch.commons.notifications.model.MicrosoftTeams
import org.opensearch.commons.notifications.model.NotificationConfig
import org.opensearch.core.rest.RestStatus
import org.opensearch.integtest.PluginRestTestCase
import org.opensearch.integtest.getResponseBody
import org.opensearch.integtest.jsonify
import org.opensearch.notifications.NotificationPlugin.Companion.PLUGIN_BASE_URI
import org.opensearch.notifications.verifySingleConfigEquals
import org.opensearch.rest.RestRequest
Expand Down Expand Up @@ -124,6 +129,42 @@ class MicrosoftTeamsNotificationConfigCrudIT : PluginRestTestCase() {
Thread.sleep(100)
}

fun `test create config with wrong Microsoft Teams url and get error text`() {
val sampleMicrosoftTeams = MicrosoftTeams("https://domain.office.com/1234567")
val referenceObject = NotificationConfig(
"this is a sample config name",
"this is a sample config description",
ConfigType.MICROSOFT_TEAMS,
isEnabled = true,
configData = sampleMicrosoftTeams
)
val createRequestJsonString = """
{
"config":{
"name":"${referenceObject.name}",
"description":"${referenceObject.description}",
"config_type":"microsoft_teams",
"is_enabled":${referenceObject.isEnabled},
"microsoft_teams":{"url":"${(referenceObject.configData as MicrosoftTeams).url}"}
}
}
""".trimIndent()
val response = try {
val request = Request(RestRequest.Method.POST.name, "$PLUGIN_BASE_URI/configs")
request.setJsonEntity(createRequestJsonString)
val restOptionsBuilder = RequestOptions.DEFAULT.toBuilder()
restOptionsBuilder.addHeader("Content-Type", "application/json")
request.setOptions(restOptionsBuilder)
client().performRequest(request)
fail("Expected wrong Microsoft Teams URL.")
} catch (exception: ResponseException) {
Assert.assertEquals(
"Wrong Microsoft Teams url. Should contain \"webhook.office.com\"",
jsonify(getResponseBody(exception.response))["error"].asJsonObject["reason"].asString
)
}
}

fun `test Bad Request for multiple config data for microsoft teams using REST Client`() {
// Create sample config request reference
val sampleMicrosoftTeams = MicrosoftTeams("https://domain.webhook.office.com/1234567")
Expand All @@ -143,7 +184,7 @@ class MicrosoftTeamsNotificationConfigCrudIT : PluginRestTestCase() {
"description":"${referenceObject.description}",
"config_type":"microsoft_teams",
"is_enabled":${referenceObject.isEnabled},
"chime":{"url":"https://dummy.com"}
"chime":{"url":"https://dummy.com"},
"microsoft_teams":{"url":"${(referenceObject.configData as MicrosoftTeams).url}"}
}
}
Expand Down

0 comments on commit fbad722

Please sign in to comment.