slack_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. So(slackNotifier.Token, ShouldEqual, "")
  42. })
  43. Convey("from settings with Recipient, Mention, and Token", func() {
  44. json := `
  45. {
  46. "url": "http://google.com",
  47. "recipient": "#ds-opentsdb",
  48. "mention": "@carl",
  49. "token": "xoxb-XXXXXXXX-XXXXXXXX-XXXXXXXXXX"
  50. }`
  51. settingsJSON, _ := simplejson.NewJson([]byte(json))
  52. model := &m.AlertNotification{
  53. Name: "ops",
  54. Type: "slack",
  55. Settings: settingsJSON,
  56. }
  57. not, err := NewSlackNotifier(model)
  58. slackNotifier := not.(*SlackNotifier)
  59. So(err, ShouldBeNil)
  60. So(slackNotifier.Name, ShouldEqual, "ops")
  61. So(slackNotifier.Type, ShouldEqual, "slack")
  62. So(slackNotifier.Url, ShouldEqual, "http://google.com")
  63. So(slackNotifier.Recipient, ShouldEqual, "#ds-opentsdb")
  64. So(slackNotifier.Mention, ShouldEqual, "@carl")
  65. So(slackNotifier.Token, ShouldEqual, "xoxb-XXXXXXXX-XXXXXXXX-XXXXXXXXXX")
  66. })
  67. })
  68. })
  69. }