diff --git a/notifications/notifications/src/main/kotlin/org/opensearch/notifications/index/ConfigIndexingActions.kt b/notifications/notifications/src/main/kotlin/org/opensearch/notifications/index/ConfigIndexingActions.kt index 741f8e79..2ef5b93a 100644 --- a/notifications/notifications/src/main/kotlin/org/opensearch/notifications/index/ConfigIndexingActions.kt +++ b/notifications/notifications/src/main/kotlin/org/opensearch/notifications/index/ConfigIndexingActions.kt @@ -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") diff --git a/notifications/notifications/src/test/kotlin/org/opensearch/integtest/config/MicrosoftTeamsNotificationConfigCrudIT.kt b/notifications/notifications/src/test/kotlin/org/opensearch/integtest/config/MicrosoftTeamsNotificationConfigCrudIT.kt index 2707d0c6..f830ece4 100644 --- a/notifications/notifications/src/test/kotlin/org/opensearch/integtest/config/MicrosoftTeamsNotificationConfigCrudIT.kt +++ b/notifications/notifications/src/test/kotlin/org/opensearch/integtest/config/MicrosoftTeamsNotificationConfigCrudIT.kt @@ -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 @@ -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") @@ -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}"} } }