From 3c53f69af130b936a1f02a4fedff3f21fda2846d Mon Sep 17 00:00:00 2001 From: zhichao-aws Date: Tue, 29 Aug 2023 17:54:00 +0800 Subject: [PATCH] Add microsoft teams validation error message (#746) * add validation failure message for Microsoft Teams Signed-off-by: zhichao-aws * modify integtest Signed-off-by: zhichao-aws --------- Signed-off-by: zhichao-aws --- .../index/ConfigIndexingActions.kt | 4 +- .../MicrosoftTeamsNotificationConfigCrudIT.kt | 43 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) 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}"} } }