c9654c4bc0
* recipient validation regex modified, validation at creation/modification implemented * Remove validation for recipient, fix tests * Log level changed from Warn to Error
89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
package ualert
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_validateAlertmanagerConfig(t *testing.T) {
|
|
tc := []struct {
|
|
name string
|
|
receivers []*PostableGrafanaReceiver
|
|
err error
|
|
}{
|
|
{
|
|
name: "when a slack receiver does not have a valid URL - it should error",
|
|
receivers: []*PostableGrafanaReceiver{
|
|
{
|
|
UID: util.GenerateShortUID(),
|
|
Name: "SlackWithBadURL",
|
|
Type: "slack",
|
|
Settings: simplejson.NewFromAny(map[string]interface{}{}),
|
|
SecureSettings: map[string]string{"url": invalidUri},
|
|
},
|
|
},
|
|
err: fmt.Errorf("failed to validate receiver \"SlackWithBadURL\" of type \"slack\": invalid URL %q: parse %q: net/url: invalid control character in URL", invalidUri, invalidUri),
|
|
},
|
|
{
|
|
name: "when a slack receiver has an invalid recipient - it should not error",
|
|
receivers: []*PostableGrafanaReceiver{
|
|
{
|
|
UID: util.GenerateShortUID(),
|
|
Name: "SlackWithBadRecipient",
|
|
Type: "slack",
|
|
Settings: simplejson.NewFromAny(map[string]interface{}{"recipient": "this passes"}),
|
|
SecureSettings: map[string]string{"url": "http://webhook.slack.com/myuser"},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "when the configuration is valid - it should not error",
|
|
receivers: []*PostableGrafanaReceiver{
|
|
{
|
|
UID: util.GenerateShortUID(),
|
|
Name: "SlackWithBadURL",
|
|
Type: "slack",
|
|
Settings: simplejson.NewFromAny(map[string]interface{}{"recipient": "#a-good-channel"}),
|
|
SecureSettings: map[string]string{"url": "http://webhook.slack.com/myuser"},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tc {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mg := newTestMigration(t)
|
|
orgID := int64(1)
|
|
|
|
config := configFromReceivers(t, tt.receivers)
|
|
require.NoError(t, config.EncryptSecureSettings()) // make sure we encrypt the settings
|
|
err := mg.validateAlertmanagerConfig(orgID, config)
|
|
if tt.err != nil {
|
|
require.Error(t, err)
|
|
require.EqualError(t, err, tt.err.Error())
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func configFromReceivers(t *testing.T, receivers []*PostableGrafanaReceiver) *PostableUserConfig {
|
|
t.Helper()
|
|
|
|
return &PostableUserConfig{
|
|
AlertmanagerConfig: PostableApiAlertingConfig{
|
|
Receivers: []*PostableApiReceiver{
|
|
{GrafanaManagedReceivers: receivers},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
const invalidUri = "�6�M��)uk譹1(�h`$�o�N>mĕ����cS2�dh![ę� ���`csB�!��OSxP�{�"
|