test_plugin.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from functools import cached_property
  2. import responses
  3. from sentry.interfaces.base import Interface
  4. from sentry.models.rule import Rule
  5. from sentry.plugins.base import Notification
  6. from sentry.testutils.cases 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. self.plugin.notify(notification)
  63. request = responses.calls[0].request
  64. payload = json.loads(request.body)
  65. assert {
  66. "message_type": "WARNING",
  67. "entity_id": group.id,
  68. "entity_display_name": "Hello world",
  69. "monitoring_tool": "sentry",
  70. "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',
  71. "timestamp": int(event.datetime.strftime("%s")),
  72. "issue_url": group.get_absolute_url(),
  73. "issue_id": group.id,
  74. "project_id": group.project.id,
  75. } == payload
  76. def test_build_description_unicode(self):
  77. event = self.store_event(
  78. data={"message": "abcd\xde\xb4", "culprit": "foo.bar", "level": "error"},
  79. project_id=self.project.id,
  80. )
  81. event.interfaces = {
  82. "Message": UnicodeTestInterface(
  83. title="abcd\xde\xb4", body="\xdc\xea\x80\x80abcd\xde\xb4"
  84. )
  85. }
  86. description = self.plugin.build_description(event)
  87. assert description == "abcd\xde\xb4\n-----------\n\n\xdc\xea\x80\x80abcd\xde\xb4"