notifications_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package notifications
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. m "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/setting"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. type testTriggeredAlert struct {
  10. ActualValue float64
  11. Name string
  12. State string
  13. }
  14. func TestNotifications(t *testing.T) {
  15. Convey("Given the notifications service", t, func() {
  16. bus.ClearBusHandlers()
  17. setting.StaticRootPath = "../../../public/"
  18. setting.Smtp.Enabled = true
  19. setting.Smtp.TemplatesPattern = "emails/*.html"
  20. setting.Smtp.FromAddress = "from@address.com"
  21. err := Init()
  22. So(err, ShouldBeNil)
  23. var sentMsg *Message
  24. addToMailQueue = func(msg *Message) {
  25. sentMsg = msg
  26. }
  27. Convey("When sending reset email password", func() {
  28. err := sendResetPasswordEmail(&m.SendResetPasswordEmailCommand{User: &m.User{Email: "asd@asd.com"}})
  29. So(err, ShouldBeNil)
  30. So(sentMsg.Body, ShouldContainSubstring, "body")
  31. So(sentMsg.Subject, ShouldEqual, "Reset your Grafana password - asd@asd.com")
  32. So(sentMsg.Body, ShouldNotContainSubstring, "Subject")
  33. })
  34. Convey("Alert notifications", func() {
  35. Convey("When sending reset email password", func() {
  36. cmd := &m.SendEmailCommand{
  37. Data: map[string]interface{}{
  38. "Name": "Name",
  39. "State": "Critical",
  40. "Description": "Description",
  41. "DashboardLink": "http://localhost:3000/dashboard/db/alerting",
  42. "AlertPageUrl": "http://localhost:3000/alerting",
  43. "DashboardImage": "http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500",
  44. "TriggeredAlerts": []testTriggeredAlert{
  45. {Name: "desktop", State: "Critical", ActualValue: 13},
  46. {Name: "mobile", State: "Warn", ActualValue: 5},
  47. },
  48. },
  49. To: []string{"asd@asd.com "},
  50. Template: "alert_notification.html",
  51. }
  52. err := sendEmailCommandHandler(cmd)
  53. So(err, ShouldBeNil)
  54. So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Critical")
  55. So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting")
  56. So(sentMsg.Body, ShouldContainSubstring, "Critical")
  57. So(sentMsg.Body, ShouldContainSubstring, "Warn")
  58. So(sentMsg.Body, ShouldContainSubstring, "mobile")
  59. So(sentMsg.Body, ShouldContainSubstring, "desktop")
  60. So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Critical ] ")
  61. })
  62. Convey("given critical", func() {
  63. cmd := &m.SendEmailCommand{
  64. Data: map[string]interface{}{
  65. "Name": "Name",
  66. "State": "Warn",
  67. "Description": "Description",
  68. "DashboardLink": "http://localhost:3000/dashboard/db/alerting",
  69. "DashboardImage": "http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500",
  70. "AlertPageUrl": "http://localhost:3000/alerting",
  71. "TriggeredAlerts": []testTriggeredAlert{
  72. {Name: "desktop", State: "Critical", ActualValue: 13},
  73. {Name: "mobile", State: "Warn", ActualValue: 5},
  74. },
  75. },
  76. To: []string{"asd@asd.com "},
  77. Template: "alert_notification.html",
  78. }
  79. err := sendEmailCommandHandler(cmd)
  80. So(err, ShouldBeNil)
  81. So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Warn")
  82. So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting")
  83. So(sentMsg.Body, ShouldContainSubstring, "Critical")
  84. So(sentMsg.Body, ShouldContainSubstring, "Warn")
  85. So(sentMsg.Body, ShouldContainSubstring, "mobile")
  86. So(sentMsg.Body, ShouldContainSubstring, "desktop")
  87. So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Warn ]")
  88. })
  89. Convey("given ok", func() {
  90. cmd := &m.SendEmailCommand{
  91. Data: map[string]interface{}{
  92. "Name": "Name",
  93. "State": "Ok",
  94. "Description": "Description",
  95. "DashboardLink": "http://localhost:3000/dashboard/db/alerting",
  96. "AlertPageUrl": "http://localhost:3000/alerting",
  97. },
  98. To: []string{"asd@asd.com "},
  99. Template: "alert_notification.html",
  100. }
  101. err := sendEmailCommandHandler(cmd)
  102. So(err, ShouldBeNil)
  103. So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Ok ]")
  104. })
  105. })
  106. })
  107. }