Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add microsoft teams validation error message #746

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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