test_plugin.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from functools import cached_property
  2. import orjson
  3. import responses
  4. from sentry.models.rule import Rule
  5. from sentry.plugins.base import Notification
  6. from sentry.testutils.cases import PluginTestCase
  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. assert group is not None
  39. rule = Rule.objects.create(project=self.project, label="my rule")
  40. notification = Notification(event=event, rule=rule)
  41. with self.options({"system.url-prefix": "http://example.com"}):
  42. self.plugin.notify(notification)
  43. request = responses.calls[0].request
  44. payload = orjson.loads(request.body)
  45. group_id = str(group.id)
  46. assert payload == {
  47. "recipients": "me",
  48. "tags": ["level:warning"],
  49. "entity": "foo.bar",
  50. "alias": "sentry: %s" % group_id,
  51. "details": {
  52. "Project Name": self.project.name,
  53. "Triggering Rules": '["my rule"]',
  54. "Sentry Group": "Hello world",
  55. "Sentry ID": group_id,
  56. "Logger": "",
  57. "Level": "warning",
  58. "Project ID": "bar",
  59. "URL": "http://example.com/organizations/baz/issues/%s/" % group_id,
  60. },
  61. "message": "Hello world",
  62. "source": "Sentry",
  63. }