test_plugin.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from urllib.parse import parse_qs
  2. import responses
  3. from exam import fixture
  4. from sentry.models import Rule
  5. from sentry.plugins.base import Notification
  6. from sentry.testutils import PluginTestCase
  7. from sentry.utils import json
  8. from sentry_plugins.slack.plugin import SlackPlugin
  9. class SlackPluginTest(PluginTestCase):
  10. @fixture
  11. def plugin(self):
  12. return SlackPlugin()
  13. def test_conf_key(self):
  14. assert self.plugin.conf_key == "slack"
  15. def test_entry_point(self):
  16. self.assertPluginInstalled("slack", self.plugin)
  17. @responses.activate
  18. def test_simple_notification(self):
  19. responses.add("POST", "http://example.com/slack")
  20. self.plugin.set_option("webhook", "http://example.com/slack", self.project)
  21. event = self.store_event(
  22. data={"message": "Hello world", "level": "warning", "culprit": "foo.bar"},
  23. project_id=self.project.id,
  24. )
  25. group = event.group
  26. rule = Rule.objects.create(project=self.project, label="my rule")
  27. notification = Notification(event=event, rule=rule)
  28. with self.options({"system.url-prefix": "http://example.com"}):
  29. self.plugin.notify(notification)
  30. request = responses.calls[0].request
  31. payload = json.loads(parse_qs(request.body)["payload"][0])
  32. assert payload == {
  33. "username": "Sentry",
  34. "attachments": [
  35. {
  36. "color": "#f18500",
  37. "fields": [
  38. {"short": False, "value": "foo.bar", "title": "Culprit"},
  39. {"short": True, "value": "bar", "title": "Project"},
  40. ],
  41. "fallback": "[bar] Hello world",
  42. "title": "Hello world",
  43. "title_link": "http://example.com/organizations/baz/issues/%s/?referrer=slack"
  44. % group.id,
  45. }
  46. ],
  47. }
  48. @responses.activate
  49. def test_notification_without_culprit(self):
  50. responses.add("POST", "http://example.com/slack")
  51. self.plugin.set_option("webhook", "http://example.com/slack", self.project)
  52. self.plugin.set_option("exclude_culprit", True, self.project)
  53. event = self.store_event(
  54. data={"message": "Hello world", "level": "warning"}, project_id=self.project.id
  55. )
  56. group = event.group
  57. rule = Rule.objects.create(project=self.project, label="my rule")
  58. notification = Notification(event=event, rule=rule)
  59. with self.options({"system.url-prefix": "http://example.com"}):
  60. self.plugin.notify(notification)
  61. request = responses.calls[0].request
  62. payload = json.loads(parse_qs(request.body)["payload"][0])
  63. assert payload == {
  64. "username": "Sentry",
  65. "attachments": [
  66. {
  67. "color": "#f18500",
  68. "fields": [{"short": True, "value": "bar", "title": "Project"}],
  69. "fallback": "[bar] Hello world",
  70. "title": "Hello world",
  71. "title_link": "http://example.com/organizations/baz/issues/%s/?referrer=slack"
  72. % group.id,
  73. }
  74. ],
  75. }
  76. @responses.activate
  77. def test_notification_without_project(self):
  78. responses.add("POST", "http://example.com/slack")
  79. self.plugin.set_option("webhook", "http://example.com/slack", self.project)
  80. self.plugin.set_option("exclude_project", True, self.project)
  81. event = self.store_event(
  82. data={"message": "Hello world", "level": "warning", "culprit": "foo.bar"},
  83. project_id=self.project.id,
  84. )
  85. group = event.group
  86. rule = Rule.objects.create(project=self.project, label="my rule")
  87. notification = Notification(event=event, rule=rule)
  88. with self.options({"system.url-prefix": "http://example.com"}):
  89. self.plugin.notify(notification)
  90. request = responses.calls[0].request
  91. payload = json.loads(parse_qs(request.body)["payload"][0])
  92. assert payload == {
  93. "username": "Sentry",
  94. "attachments": [
  95. {
  96. "color": "#f18500",
  97. "fields": [{"short": False, "value": "foo.bar", "title": "Culprit"}],
  98. "fallback": "[bar] Hello world",
  99. "title": "Hello world",
  100. "title_link": "http://example.com/organizations/baz/issues/%s/?referrer=slack"
  101. % group.id,
  102. }
  103. ],
  104. }