test_plugin.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from functools import cached_property
  2. import responses
  3. from sentry.models import Rule
  4. from sentry.plugins.base import Notification
  5. from sentry.testutils import PluginTestCase
  6. from sentry.utils import json
  7. from sentry_plugins.opsgenie.plugin import OpsGeniePlugin
  8. class OpsGeniePluginTest(PluginTestCase):
  9. @cached_property
  10. def plugin(self):
  11. return OpsGeniePlugin()
  12. def test_conf_key(self):
  13. assert self.plugin.conf_key == "opsgenie"
  14. def test_entry_point(self):
  15. self.assertPluginInstalled("opsgenie", self.plugin)
  16. def test_is_configured(self):
  17. assert self.plugin.is_configured(self.project) is False
  18. self.plugin.set_option("api_key", "abcdef", self.project)
  19. assert self.plugin.is_configured(self.project) is False
  20. self.plugin.set_option("alert_url", "https://api.opsgenie.com/v2/alerts", self.project)
  21. assert self.plugin.is_configured(self.project) is True
  22. @responses.activate
  23. def test_simple_notification(self):
  24. responses.add("POST", "https://api.opsgenie.com/v2/alerts")
  25. self.plugin.set_option("api_key", "abcdef", self.project)
  26. self.plugin.set_option("alert_url", "https://api.opsgenie.com/v2/alerts", self.project)
  27. self.plugin.set_option("recipients", "me", self.project)
  28. event = self.store_event(
  29. data={
  30. "message": "Hello world",
  31. "level": "warning",
  32. "platform": "python",
  33. "culprit": "foo.bar",
  34. },
  35. project_id=self.project.id,
  36. )
  37. group = event.group
  38. rule = Rule.objects.create(project=self.project, label="my rule")
  39. notification = Notification(event=event, rule=rule)
  40. with self.options({"system.url-prefix": "http://example.com"}):
  41. self.plugin.notify(notification)
  42. request = responses.calls[0].request
  43. payload = json.loads(request.body)
  44. group_id = str(group.id)
  45. assert payload == {
  46. "recipients": "me",
  47. "tags": ["level:warning"],
  48. "entity": "foo.bar",
  49. "alias": "sentry: %s" % group_id,
  50. "details": {
  51. "Project Name": self.project.name,
  52. "Triggering Rules": '["my rule"]',
  53. "Sentry Group": "Hello world",
  54. "Sentry ID": group_id,
  55. "Logger": "",
  56. "Level": "warning",
  57. "Project ID": "bar",
  58. "URL": "http://example.com/organizations/baz/issues/%s/" % group_id,
  59. },
  60. "message": "Hello world",
  61. "source": "Sentry",
  62. }