notifier_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package alerting
  2. import (
  3. "testing"
  4. "reflect"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestAlertNotificationExtraction(t *testing.T) {
  10. Convey("Parsing alert notification from settings", t, func() {
  11. Convey("Parsing email notification from settings", func() {
  12. json := `
  13. {
  14. "to": "ops@grafana.org"
  15. }`
  16. settingsJSON, _ := simplejson.NewJson([]byte(json))
  17. model := &m.AlertNotification{
  18. Name: "ops",
  19. Type: "email",
  20. Settings: settingsJSON,
  21. }
  22. not, err := NewNotificationFromDBModel(model)
  23. So(err, ShouldBeNil)
  24. So(not.Name, ShouldEqual, "ops")
  25. So(not.Type, ShouldEqual, "email")
  26. So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.EmailNotifier")
  27. email := not.Notifierr.(*EmailNotifier)
  28. So(email.To, ShouldEqual, "ops@grafana.org")
  29. })
  30. Convey("Parsing webhook notification from settings", func() {
  31. json := `
  32. {
  33. "url": "http://localhost:3000",
  34. "username": "username",
  35. "password": "password"
  36. }`
  37. settingsJSON, _ := simplejson.NewJson([]byte(json))
  38. model := &m.AlertNotification{
  39. Name: "slack",
  40. Type: "webhook",
  41. Settings: settingsJSON,
  42. }
  43. not, err := NewNotificationFromDBModel(model)
  44. So(err, ShouldBeNil)
  45. So(not.Name, ShouldEqual, "slack")
  46. So(not.Type, ShouldEqual, "webhook")
  47. So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.WebhookNotifier")
  48. webhook := not.Notifierr.(*WebhookNotifier)
  49. So(webhook.Url, ShouldEqual, "http://localhost:3000")
  50. })
  51. })
  52. }