pushover_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package notifiers
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "github.com/grafana/grafana/pkg/services/alerting"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. "github.com/grafana/grafana/pkg/models"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. func TestPushoverNotifier(t *testing.T) {
  12. Convey("Pushover notifier tests", t, func() {
  13. Convey("Parsing alert notification from settings", func() {
  14. Convey("empty settings should return error", func() {
  15. json := `{ }`
  16. settingsJSON, _ := simplejson.NewJson([]byte(json))
  17. model := &models.AlertNotification{
  18. Name: "Pushover",
  19. Type: "pushover",
  20. Settings: settingsJSON,
  21. }
  22. _, err := NewPushoverNotifier(model)
  23. So(err, ShouldNotBeNil)
  24. })
  25. Convey("from settings", func() {
  26. json := `
  27. {
  28. "apiToken": "4SrUFQL4A5V5TQ1z5Pg9nxHXPXSTve",
  29. "userKey": "tzNZYf36y0ohWwXo4XoUrB61rz1A4o",
  30. "priority": "1",
  31. "sound": "pushover",
  32. "okSound": "magic"
  33. }`
  34. settingsJSON, _ := simplejson.NewJson([]byte(json))
  35. model := &models.AlertNotification{
  36. Name: "Pushover",
  37. Type: "pushover",
  38. Settings: settingsJSON,
  39. }
  40. not, err := NewPushoverNotifier(model)
  41. pushoverNotifier := not.(*PushoverNotifier)
  42. So(err, ShouldBeNil)
  43. So(pushoverNotifier.Name, ShouldEqual, "Pushover")
  44. So(pushoverNotifier.Type, ShouldEqual, "pushover")
  45. So(pushoverNotifier.APIToken, ShouldEqual, "4SrUFQL4A5V5TQ1z5Pg9nxHXPXSTve")
  46. So(pushoverNotifier.UserKey, ShouldEqual, "tzNZYf36y0ohWwXo4XoUrB61rz1A4o")
  47. So(pushoverNotifier.Priority, ShouldEqual, 1)
  48. So(pushoverNotifier.AlertingSound, ShouldEqual, "pushover")
  49. So(pushoverNotifier.OkSound, ShouldEqual, "magic")
  50. })
  51. })
  52. })
  53. }
  54. func TestGenPushoverBody(t *testing.T) {
  55. Convey("Pushover body generation tests", t, func() {
  56. Convey("Given common sounds", func() {
  57. sirenSound := "siren_sound_tst"
  58. successSound := "success_sound_tst"
  59. notifier := &PushoverNotifier{AlertingSound: sirenSound, OkSound: successSound}
  60. Convey("When alert is firing - should use siren sound", func() {
  61. evalContext := alerting.NewEvalContext(context.Background(),
  62. &alerting.Rule{
  63. State: models.AlertStateAlerting,
  64. })
  65. _, pushoverBody, err := notifier.genPushoverBody(evalContext, "", "")
  66. So(err, ShouldBeNil)
  67. So(strings.Contains(pushoverBody.String(), sirenSound), ShouldBeTrue)
  68. })
  69. Convey("When alert is ok - should use success sound", func() {
  70. evalContext := alerting.NewEvalContext(context.Background(),
  71. &alerting.Rule{
  72. State: models.AlertStateOK,
  73. })
  74. _, pushoverBody, err := notifier.genPushoverBody(evalContext, "", "")
  75. So(err, ShouldBeNil)
  76. So(strings.Contains(pushoverBody.String(), successSound), ShouldBeTrue)
  77. })
  78. })
  79. })
  80. }