test_plugin.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from functools import cached_property
  2. import responses
  3. from sentry.interfaces.base import Interface
  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.victorops.plugin import VictorOpsPlugin
  9. SUCCESS = """{
  10. "result":"success",
  11. "entity_id":"86dc4115-72d3-4219-9d8e-44939c1c409d"
  12. }"""
  13. class UnicodeTestInterface(Interface):
  14. def to_string(self, event):
  15. return self.body
  16. def get_title(self):
  17. return self.title
  18. class VictorOpsPluginTest(PluginTestCase):
  19. @cached_property
  20. def plugin(self):
  21. return VictorOpsPlugin()
  22. def test_conf_key(self):
  23. assert self.plugin.conf_key == "victorops"
  24. def test_entry_point(self):
  25. self.assertPluginInstalled("victorops", self.plugin)
  26. def test_is_configured(self):
  27. assert self.plugin.is_configured(self.project) is False
  28. self.plugin.set_option("api_key", "abcdef", self.project)
  29. assert self.plugin.is_configured(self.project) is True
  30. @responses.activate
  31. def test_simple_notification(self):
  32. responses.add(
  33. "POST",
  34. "https://alert.victorops.com/integrations/generic/20131114/alert/secret-api-key/everyone",
  35. body=SUCCESS,
  36. )
  37. self.plugin.set_option("api_key", "secret-api-key", self.project)
  38. self.plugin.set_option("routing_key", "everyone", self.project)
  39. event = self.store_event(
  40. data={
  41. "message": "Hello world",
  42. "level": "warning",
  43. "culprit": "foo.bar",
  44. "platform": "python",
  45. "stacktrace": {
  46. "frames": [
  47. {
  48. "filename": "sentry/models/foo.py",
  49. "context_line": " string_max_length=self.string_max_length)",
  50. "function": "build_msg",
  51. "lineno": 29,
  52. }
  53. ]
  54. },
  55. },
  56. project_id=self.project.id,
  57. )
  58. assert event.group is not None
  59. group = event.group
  60. rule = Rule.objects.create(project=self.project, label="my rule")
  61. notification = Notification(event=event, rule=rule)
  62. with self.options({"system.url-prefix": "http://example.com"}):
  63. self.plugin.notify(notification)
  64. request = responses.calls[0].request
  65. payload = json.loads(request.body)
  66. assert {
  67. "message_type": "WARNING",
  68. "entity_id": group.id,
  69. "entity_display_name": "Hello world",
  70. "monitoring_tool": "sentry",
  71. "state_message": 'Stacktrace\n-----------\n\nStacktrace (most recent call last):\n\n File "sentry/models/foo.py", line 29, in build_msg\n string_max_length=self.string_max_length)\n\nMessage\n-----------\n\nHello world',
  72. "timestamp": int(event.datetime.strftime("%s")),
  73. "issue_url": "http://example.com/organizations/baz/issues/%s/" % group.id,
  74. "issue_id": group.id,
  75. "project_id": group.project.id,
  76. } == payload
  77. def test_build_description_unicode(self):
  78. event = self.store_event(
  79. data={"message": "abcd\xde\xb4", "culprit": "foo.bar", "level": "error"},
  80. project_id=self.project.id,
  81. )
  82. event.interfaces = {
  83. "Message": UnicodeTestInterface(
  84. title="abcd\xde\xb4", body="\xdc\xea\x80\x80abcd\xde\xb4"
  85. )
  86. }
  87. description = self.plugin.build_description(event)
  88. assert description == "abcd\xde\xb4\n-----------\n\n\xdc\xea\x80\x80abcd\xde\xb4"