email_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package notifiers
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. m "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestEmailNotifier(t *testing.T) {
  9. Convey("Email notifier tests", t, func() {
  10. Convey("Parsing alert notification from settings", func() {
  11. Convey("empty settings should return error", func() {
  12. json := `{ }`
  13. settingsJSON, _ := simplejson.NewJson([]byte(json))
  14. model := &m.AlertNotification{
  15. Name: "ops",
  16. Type: "email",
  17. Settings: settingsJSON,
  18. }
  19. _, err := NewEmailNotifier(model)
  20. So(err, ShouldNotBeNil)
  21. })
  22. Convey("from settings", func() {
  23. json := `
  24. {
  25. "addresses": "ops@grafana.org"
  26. }`
  27. settingsJSON, _ := simplejson.NewJson([]byte(json))
  28. model := &m.AlertNotification{
  29. Name: "ops",
  30. Type: "email",
  31. Settings: settingsJSON,
  32. }
  33. not, err := NewEmailNotifier(model)
  34. emailNotifier := not.(*EmailNotifier)
  35. So(err, ShouldBeNil)
  36. So(emailNotifier.Name, ShouldEqual, "ops")
  37. So(emailNotifier.Type, ShouldEqual, "email")
  38. So(emailNotifier.Addresses[0], ShouldEqual, "ops@grafana.org")
  39. })
  40. Convey("from settings with two emails", func() {
  41. json := `
  42. {
  43. "addresses": "ops@grafana.org;dev@grafana.org"
  44. }`
  45. settingsJSON, err := simplejson.NewJson([]byte(json))
  46. So(err, ShouldBeNil)
  47. model := &m.AlertNotification{
  48. Name: "ops",
  49. Type: "email",
  50. Settings: settingsJSON,
  51. }
  52. not, err := NewEmailNotifier(model)
  53. emailNotifier := not.(*EmailNotifier)
  54. So(err, ShouldBeNil)
  55. So(emailNotifier.Name, ShouldEqual, "ops")
  56. So(emailNotifier.Type, ShouldEqual, "email")
  57. So(len(emailNotifier.Addresses), ShouldEqual, 2)
  58. So(emailNotifier.Addresses[0], ShouldEqual, "ops@grafana.org")
  59. So(emailNotifier.Addresses[1], ShouldEqual, "dev@grafana.org")
  60. })
  61. })
  62. })
  63. }