slack_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 TestSlackNotifier(t *testing.T) {
  9. Convey("Slack 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: "slack",
  17. Settings: settingsJSON,
  18. }
  19. _, err := NewSlackNotifier(model)
  20. So(err, ShouldNotBeNil)
  21. })
  22. Convey("from settings", func() {
  23. json := `
  24. {
  25. "url": "http://google.com"
  26. }`
  27. settingsJSON, _ := simplejson.NewJson([]byte(json))
  28. model := &m.AlertNotification{
  29. Name: "ops",
  30. Type: "slack",
  31. Settings: settingsJSON,
  32. }
  33. not, err := NewSlackNotifier(model)
  34. slackNotifier := not.(*SlackNotifier)
  35. So(err, ShouldBeNil)
  36. So(slackNotifier.Name, ShouldEqual, "ops")
  37. So(slackNotifier.Type, ShouldEqual, "slack")
  38. So(slackNotifier.Url, ShouldEqual, "http://google.com")
  39. So(slackNotifier.Recipient, ShouldEqual, "")
  40. So(slackNotifier.Mention, ShouldEqual, "")
  41. })
  42. Convey("from settings with Recipient and Mention", func() {
  43. json := `
  44. {
  45. "url": "http://google.com",
  46. "recipient": "#ds-opentsdb",
  47. "mention": "@carl"
  48. }`
  49. settingsJSON, _ := simplejson.NewJson([]byte(json))
  50. model := &m.AlertNotification{
  51. Name: "ops",
  52. Type: "slack",
  53. Settings: settingsJSON,
  54. }
  55. not, err := NewSlackNotifier(model)
  56. slackNotifier := not.(*SlackNotifier)
  57. So(err, ShouldBeNil)
  58. So(slackNotifier.Name, ShouldEqual, "ops")
  59. So(slackNotifier.Type, ShouldEqual, "slack")
  60. So(slackNotifier.Url, ShouldEqual, "http://google.com")
  61. So(slackNotifier.Recipient, ShouldEqual, "#ds-opentsdb")
  62. So(slackNotifier.Mention, ShouldEqual, "@carl")
  63. })
  64. })
  65. })
  66. }