notifier_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. "from": "alerting@grafana.org",
  15. "to": "ops@grafana.org"
  16. }`
  17. settingsJSON, _ := simplejson.NewJson([]byte(json))
  18. model := &m.AlertNotification{
  19. Name: "ops",
  20. Type: "email",
  21. Settings: settingsJSON,
  22. }
  23. not, err := NewNotificationFromDBModel(model)
  24. So(err, ShouldBeNil)
  25. So(not.Name, ShouldEqual, "ops")
  26. So(not.Type, ShouldEqual, "email")
  27. So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.EmailNotifier")
  28. email := not.Notifierr.(*EmailNotifier)
  29. So(email.To, ShouldEqual, "ops@grafana.org")
  30. So(email.From, ShouldEqual, "alerting@grafana.org")
  31. })
  32. Convey("Parsing webhook notification from settings", func() {
  33. json := `
  34. {
  35. "url": "http://localhost:3000",
  36. "username": "username",
  37. "password": "password"
  38. }`
  39. settingsJSON, _ := simplejson.NewJson([]byte(json))
  40. model := &m.AlertNotification{
  41. Name: "slack",
  42. Type: "webhook",
  43. Settings: settingsJSON,
  44. }
  45. not, err := NewNotificationFromDBModel(model)
  46. So(err, ShouldBeNil)
  47. So(not.Name, ShouldEqual, "slack")
  48. So(not.Type, ShouldEqual, "webhook")
  49. So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.WebhookNotifier")
  50. webhook := not.Notifierr.(*WebhookNotifier)
  51. So(webhook.Url, ShouldEqual, "http://localhost:3000")
  52. })
  53. })
  54. }