notifier_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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", func() {
  12. Convey("empty settings should return error", func() {
  13. json := `{ }`
  14. settingsJSON, _ := simplejson.NewJson([]byte(json))
  15. model := &m.AlertNotification{
  16. Name: "ops",
  17. Type: "email",
  18. Settings: settingsJSON,
  19. }
  20. _, err := NewNotificationFromDBModel(model)
  21. So(err, ShouldNotBeNil)
  22. })
  23. Convey("from settings", func() {
  24. json := `
  25. {
  26. "to": "ops@grafana.org"
  27. }`
  28. settingsJSON, _ := simplejson.NewJson([]byte(json))
  29. model := &m.AlertNotification{
  30. Name: "ops",
  31. Type: "email",
  32. Settings: settingsJSON,
  33. }
  34. not, err := NewNotificationFromDBModel(model)
  35. So(err, ShouldBeNil)
  36. So(not.Name, ShouldEqual, "ops")
  37. So(not.Type, ShouldEqual, "email")
  38. So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.EmailNotifier")
  39. email := not.Notifierr.(*EmailNotifier)
  40. So(email.To, ShouldEqual, "ops@grafana.org")
  41. })
  42. })
  43. Convey("Parsing webhook", func() {
  44. Convey("empty settings should return error", func() {
  45. json := `{ }`
  46. settingsJSON, _ := simplejson.NewJson([]byte(json))
  47. model := &m.AlertNotification{
  48. Name: "ops",
  49. Type: "webhook",
  50. Settings: settingsJSON,
  51. }
  52. _, err := NewNotificationFromDBModel(model)
  53. So(err, ShouldNotBeNil)
  54. })
  55. Convey("from settings", func() {
  56. json := `
  57. {
  58. "url": "http://localhost:3000",
  59. "username": "username",
  60. "password": "password"
  61. }`
  62. settingsJSON, _ := simplejson.NewJson([]byte(json))
  63. model := &m.AlertNotification{
  64. Name: "slack",
  65. Type: "webhook",
  66. Settings: settingsJSON,
  67. }
  68. not, err := NewNotificationFromDBModel(model)
  69. So(err, ShouldBeNil)
  70. So(not.Name, ShouldEqual, "slack")
  71. So(not.Type, ShouldEqual, "webhook")
  72. So(reflect.TypeOf(not.Notifierr).Elem().String(), ShouldEqual, "alerting.WebhookNotifier")
  73. webhook := not.Notifierr.(*WebhookNotifier)
  74. So(webhook.Url, ShouldEqual, "http://localhost:3000")
  75. })
  76. })
  77. })
  78. }