test_plugin.py 2.6 KB

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