threema_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/grafana/grafana/pkg/services/alerting"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestThreemaNotifier(t *testing.T) {
  10. Convey("Threema notifier tests", t, func() {
  11. Convey("Parsing alert notification from settings", func() {
  12. Convey("empty settings should return error", func() {
  13. json := `{ }`
  14. settingsJSON, _ := simplejson.NewJson([]byte(json))
  15. model := &m.AlertNotification{
  16. Name: "threema_testing",
  17. Type: "threema",
  18. Settings: settingsJSON,
  19. }
  20. _, err := NewThreemaNotifier(model)
  21. So(err, ShouldNotBeNil)
  22. })
  23. Convey("valid settings should be parsed successfully", func() {
  24. json := `
  25. {
  26. "gateway_id": "*3MAGWID",
  27. "recipient_id": "ECHOECHO",
  28. "api_secret": "1234"
  29. }`
  30. settingsJSON, _ := simplejson.NewJson([]byte(json))
  31. model := &m.AlertNotification{
  32. Name: "threema_testing",
  33. Type: "threema",
  34. Settings: settingsJSON,
  35. }
  36. not, err := NewThreemaNotifier(model)
  37. So(err, ShouldBeNil)
  38. threemaNotifier := not.(*ThreemaNotifier)
  39. So(err, ShouldBeNil)
  40. So(threemaNotifier.Name, ShouldEqual, "threema_testing")
  41. So(threemaNotifier.Type, ShouldEqual, "threema")
  42. So(threemaNotifier.GatewayID, ShouldEqual, "*3MAGWID")
  43. So(threemaNotifier.RecipientID, ShouldEqual, "ECHOECHO")
  44. So(threemaNotifier.APISecret, ShouldEqual, "1234")
  45. })
  46. Convey("invalid Threema Gateway IDs should be rejected (prefix)", func() {
  47. json := `
  48. {
  49. "gateway_id": "ECHOECHO",
  50. "recipient_id": "ECHOECHO",
  51. "api_secret": "1234"
  52. }`
  53. settingsJSON, _ := simplejson.NewJson([]byte(json))
  54. model := &m.AlertNotification{
  55. Name: "threema_testing",
  56. Type: "threema",
  57. Settings: settingsJSON,
  58. }
  59. not, err := NewThreemaNotifier(model)
  60. So(not, ShouldBeNil)
  61. So(err.(alerting.ValidationError).Reason, ShouldEqual, "Invalid Threema Gateway ID: Must start with a *")
  62. })
  63. Convey("invalid Threema Gateway IDs should be rejected (length)", func() {
  64. json := `
  65. {
  66. "gateway_id": "*ECHOECHO",
  67. "recipient_id": "ECHOECHO",
  68. "api_secret": "1234"
  69. }`
  70. settingsJSON, _ := simplejson.NewJson([]byte(json))
  71. model := &m.AlertNotification{
  72. Name: "threema_testing",
  73. Type: "threema",
  74. Settings: settingsJSON,
  75. }
  76. not, err := NewThreemaNotifier(model)
  77. So(not, ShouldBeNil)
  78. So(err.(alerting.ValidationError).Reason, ShouldEqual, "Invalid Threema Gateway ID: Must be 8 characters long")
  79. })
  80. Convey("invalid Threema Recipient IDs should be rejected (length)", func() {
  81. json := `
  82. {
  83. "gateway_id": "*3MAGWID",
  84. "recipient_id": "ECHOECH",
  85. "api_secret": "1234"
  86. }`
  87. settingsJSON, _ := simplejson.NewJson([]byte(json))
  88. model := &m.AlertNotification{
  89. Name: "threema_testing",
  90. Type: "threema",
  91. Settings: settingsJSON,
  92. }
  93. not, err := NewThreemaNotifier(model)
  94. So(not, ShouldBeNil)
  95. So(err.(alerting.ValidationError).Reason, ShouldEqual, "Invalid Threema Recipient ID: Must be 8 characters long")
  96. })
  97. })
  98. })
  99. }