2017-07-04 08:16:32 -05:00
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
2019-12-11 10:01:01 -06:00
|
|
|
"context"
|
2017-07-04 08:16:32 -05:00
|
|
|
"testing"
|
|
|
|
|
2023-01-23 07:19:25 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2017-07-04 08:16:32 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-12-11 10:01:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2023-01-23 07:19:25 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting/models"
|
2022-09-19 02:54:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations/annotationstest"
|
2022-08-02 08:08:09 -05:00
|
|
|
encryptionservice "github.com/grafana/grafana/pkg/services/encryption/service"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/validations"
|
2017-07-04 08:16:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDingDingNotifier(t *testing.T) {
|
2022-08-02 08:08:09 -05:00
|
|
|
encryptionService := encryptionservice.SetupTestService(t)
|
|
|
|
|
2021-10-21 10:04:43 -05:00
|
|
|
t.Run("empty settings should return error", func(t *testing.T) {
|
|
|
|
json := `{ }`
|
|
|
|
|
|
|
|
settingsJSON, _ := simplejson.NewJson([]byte(json))
|
|
|
|
model := &models.AlertNotification{
|
|
|
|
Name: "dingding_testing",
|
|
|
|
Type: "dingding",
|
|
|
|
Settings: settingsJSON,
|
|
|
|
}
|
|
|
|
|
2024-01-23 05:36:22 -06:00
|
|
|
_, err := newDingDingNotifier(nil, model, encryptionService.GetDecryptedValue, nil)
|
2021-10-21 10:04:43 -05:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("settings should trigger incident", func(t *testing.T) {
|
|
|
|
json := `{ "url": "https://www.google.com" }`
|
|
|
|
|
|
|
|
settingsJSON, _ := simplejson.NewJson([]byte(json))
|
|
|
|
model := &models.AlertNotification{
|
|
|
|
Name: "dingding_testing",
|
|
|
|
Type: "dingding",
|
|
|
|
Settings: settingsJSON,
|
|
|
|
}
|
|
|
|
|
2024-01-23 05:36:22 -06:00
|
|
|
not, err := newDingDingNotifier(nil, model, encryptionService.GetDecryptedValue, nil)
|
2021-10-21 10:04:43 -05:00
|
|
|
notifier := not.(*DingDingNotifier)
|
|
|
|
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, "dingding_testing", notifier.Name)
|
|
|
|
require.Equal(t, "dingding", notifier.Type)
|
|
|
|
require.Equal(t, "https://www.google.com", notifier.URL)
|
|
|
|
|
|
|
|
t.Run("genBody should not panic", func(t *testing.T) {
|
|
|
|
evalContext := alerting.NewEvalContext(context.Background(),
|
|
|
|
&alerting.Rule{
|
|
|
|
State: models.AlertStateAlerting,
|
|
|
|
Message: `{host="localhost"}`,
|
2022-09-19 02:54:37 -05:00
|
|
|
}, &validations.OSSPluginRequestValidator{}, nil, nil, nil, annotationstest.NewFakeAnnotationsRepo())
|
2021-10-21 10:04:43 -05:00
|
|
|
_, err = notifier.genBody(evalContext, "")
|
|
|
|
require.Nil(t, err)
|
2017-07-04 08:16:32 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|