Alerting: validate that the receiver exist when updating routing tree (#51561) (#51758)

* Alerting: validate that the receiver exist when updating routing tree

* rename interface

* add missing file

* change constructor

* fix e2e tests

* only import package once

* add unit test for bug

* wording

* close response body

* Update pkg/services/ngalert/api/tooling/definitions/alertmanager_validation.go

* refactor to remove database roundtrip

(cherry picked from commit e64cde8727)

Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com>
This commit is contained in:
Grot (@grafanabot)
2022-07-05 12:47:11 -04:00
committed by GitHub
co-authored by Jean-Philippe Quéméner
parent 2f76963700
commit 7f5dec12bd
4 changed files with 103 additions and 3 deletions
@@ -47,13 +47,32 @@ func TestProvisioning(t *testing.T) {
url := fmt.Sprintf("http://%s/api/v1/provisioning/policies", grafanaListedAddr)
body := `
{
"receiver": "grafana-default-email",
"receiver": "test-receiver",
"group_by": [
"..."
],
"routes": []
}`
// As we check if the receiver exists that is referenced in the policy,
// we first need to create it, so the tests passes correctly.
urlReceiver := fmt.Sprintf("http://%s/api/v1/provisioning/contact-points", grafanaListedAddr)
bodyReceiver := `
{
"name": "test-receiver",
"type": "slack",
"settings": {
"recipient": "value_recipient",
"token": "value_token"
}
}`
req := createTestRequest("POST", urlReceiver, "admin", bodyReceiver)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
require.Equal(t, 202, resp.StatusCode)
t.Run("un-authenticated GET should 401", func(t *testing.T) {
req := createTestRequest("GET", url, "", "")
@@ -130,7 +149,6 @@ func TestProvisioning(t *testing.T) {
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.NoError(t, resp.Body.Close())
require.Equal(t, 202, resp.StatusCode)
})
})