2016-07-27 05:09:55 -05:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-14 01:15:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/encryption/ossencryption"
|
2020-11-23 03:37:53 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-07-27 05:09:55 -05:00
|
|
|
)
|
|
|
|
|
2020-11-23 03:37:53 -06:00
|
|
|
func TestWebhookNotifier_parsingFromSettings(t *testing.T) {
|
|
|
|
t.Run("Empty settings should cause error", func(t *testing.T) {
|
|
|
|
const json = `{}`
|
|
|
|
|
|
|
|
settingsJSON, err := simplejson.NewJson([]byte(json))
|
|
|
|
require.NoError(t, err)
|
|
|
|
model := &models.AlertNotification{
|
|
|
|
Name: "ops",
|
|
|
|
Type: "webhook",
|
|
|
|
Settings: settingsJSON,
|
|
|
|
}
|
|
|
|
|
2022-02-03 06:26:05 -06:00
|
|
|
_, err = NewWebHookNotifier(model, ossencryption.ProvideService().GetDecryptedValue, nil)
|
2020-11-23 03:37:53 -06:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Valid settings should result in a valid notifier", func(t *testing.T) {
|
|
|
|
const json = `{"url": "http://google.com"}`
|
|
|
|
|
|
|
|
settingsJSON, err := simplejson.NewJson([]byte(json))
|
|
|
|
require.NoError(t, err)
|
|
|
|
model := &models.AlertNotification{
|
|
|
|
Name: "ops",
|
|
|
|
Type: "webhook",
|
|
|
|
Settings: settingsJSON,
|
|
|
|
}
|
|
|
|
|
2022-02-03 06:26:05 -06:00
|
|
|
not, err := NewWebHookNotifier(model, ossencryption.ProvideService().GetDecryptedValue, nil)
|
2020-11-23 03:37:53 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
webhookNotifier := not.(*WebhookNotifier)
|
|
|
|
|
|
|
|
assert.Equal(t, "ops", webhookNotifier.Name)
|
|
|
|
assert.Equal(t, "webhook", webhookNotifier.Type)
|
|
|
|
assert.Equal(t, "http://google.com", webhookNotifier.URL)
|
2016-07-27 05:09:55 -05:00
|
|
|
})
|
|
|
|
}
|