notifications_test.go 4.5 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. //
  53. // err := sendEmailCommandHandler(cmd)
  54. // So(err, ShouldBeNil)
  55. //
  56. // So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Critical")
  57. // So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting")
  58. // So(sentMsg.Body, ShouldContainSubstring, "Critical")
  59. // So(sentMsg.Body, ShouldContainSubstring, "Warn")
  60. // So(sentMsg.Body, ShouldContainSubstring, "mobile")
  61. // So(sentMsg.Body, ShouldContainSubstring, "desktop")
  62. // So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Critical ] ")
  63. // })
  64. //
  65. // Convey("given critical", func() {
  66. // cmd := &m.SendEmailCommand{
  67. // Data: map[string]interface{}{
  68. // "Name": "Name",
  69. // "State": "Warn",
  70. // "Description": "Description",
  71. // "DashboardLink": "http://localhost:3000/dashboard/db/alerting",
  72. // "DashboardImage": "http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500",
  73. // "AlertPageUrl": "http://localhost:3000/alerting",
  74. // "TriggeredAlerts": []testTriggeredAlert{
  75. // {Name: "desktop", State: "Critical", ActualValue: 13},
  76. // {Name: "mobile", State: "Warn", ActualValue: 5},
  77. // },
  78. // },
  79. // To: []string{"asd@asd.com "},
  80. // Template: "alert_notification.html",
  81. // }
  82. //
  83. // err := sendEmailCommandHandler(cmd)
  84. // So(err, ShouldBeNil)
  85. // So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Warn")
  86. // So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting")
  87. // So(sentMsg.Body, ShouldContainSubstring, "Critical")
  88. // So(sentMsg.Body, ShouldContainSubstring, "Warn")
  89. // So(sentMsg.Body, ShouldContainSubstring, "mobile")
  90. // So(sentMsg.Body, ShouldContainSubstring, "desktop")
  91. // So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Warn ]")
  92. // })
  93. //
  94. // Convey("given ok", func() {
  95. // cmd := &m.SendEmailCommand{
  96. // Data: map[string]interface{}{
  97. // "Name": "Name",
  98. // "State": "Ok",
  99. // "Description": "Description",
  100. // "DashboardLink": "http://localhost:3000/dashboard/db/alerting",
  101. // "AlertPageUrl": "http://localhost:3000/alerting",
  102. // },
  103. // To: []string{"asd@asd.com "},
  104. // Template: "alert_notification.html",
  105. // }
  106. //
  107. // err := sendEmailCommandHandler(cmd)
  108. // So(err, ShouldBeNil)
  109. // So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Ok ]")
  110. // })
  111. })
  112. })
  113. }