grafana/pkg/services/ngalert/notifier/channels/email_test.go
Ganesh Vernekar 0b788b5ce8
AlertingNG: Notification channel for emails (#31768)
* Email notification channel in ngalert

Signed-off-by: Ganesh Vernekar <cs15btech11018@iith.ac.in>

* Use existing templating system

Signed-off-by: Ganesh Vernekar <cs15btech11018@iith.ac.in>

* Update template and add unit tests

Signed-off-by: Ganesh Vernekar <cs15btech11018@iith.ac.in>
2021-03-18 14:55:11 +00:00

60 lines
1.6 KiB
Go

package channels
import (
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/require"
)
func TestEmailNotifier(t *testing.T) {
t.Run("empty settings should return error", func(t *testing.T) {
json := `{ }`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "ops",
Type: "email",
Settings: settingsJSON,
}
_, err := NewEmailNotifier(model)
require.Error(t, err)
})
t.Run("from settings", func(t *testing.T) {
json := `{"addresses": "ops@grafana.org"}`
settingsJSON, err := simplejson.NewJson([]byte(json))
require.NoError(t, err)
emailNotifier, err := NewEmailNotifier(&models.AlertNotification{
Name: "ops",
Type: "email",
Settings: settingsJSON,
})
require.NoError(t, err)
require.Equal(t, "ops", emailNotifier.Name)
require.Equal(t, "email", emailNotifier.Type)
require.Equal(t, []string{"ops@grafana.org"}, emailNotifier.Addresses)
})
t.Run("from settings with two emails", func(t *testing.T) {
json := `{"addresses": "ops@grafana.org;dev@grafana.org"}`
settingsJSON, err := simplejson.NewJson([]byte(json))
require.NoError(t, err)
emailNotifier, err := NewEmailNotifier(&models.AlertNotification{
Name: "ops",
Type: "email",
Settings: settingsJSON,
})
require.NoError(t, err)
require.Equal(t, "ops", emailNotifier.Name)
require.Equal(t, "email", emailNotifier.Type)
require.Equal(t, []string{"ops@grafana.org", "dev@grafana.org"}, emailNotifier.Addresses)
})
}