test_plugin.py 3.7 KB

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