test_plugin.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from functools import cached_property
  2. import orjson
  3. import responses
  4. from django.urls import reverse
  5. from sentry.models.rule import Rule
  6. from sentry.plugins.base import Notification
  7. from sentry.testutils.cases import PluginTestCase
  8. from sentry_plugins.pagerduty.plugin import PagerDutyPlugin
  9. INVALID_METHOD = (
  10. '{"status":"invalid method","message":"You must use HTTP POST to submit your event"}'
  11. )
  12. SUCCESS = """{
  13. "status": "success",
  14. "message": "Event processed",
  15. "incident_key": "73af7a305bd7012d7c06002500d5d1a6"
  16. }"""
  17. class PagerDutyPluginTest(PluginTestCase):
  18. @cached_property
  19. def plugin(self):
  20. return PagerDutyPlugin()
  21. def test_conf_key(self):
  22. assert self.plugin.conf_key == "pagerduty"
  23. def test_entry_point(self):
  24. self.assertPluginInstalled("pagerduty", self.plugin)
  25. def test_is_configured(self):
  26. assert self.plugin.is_configured(self.project) is False
  27. self.plugin.set_option("service_key", "abcdef", self.project)
  28. assert self.plugin.is_configured(self.project) is True
  29. @responses.activate
  30. def test_simple_notification(self):
  31. responses.add(
  32. "GET",
  33. "https://events.pagerduty.com/generic/2010-04-15/create_event.json",
  34. body=INVALID_METHOD,
  35. )
  36. responses.add(
  37. "POST",
  38. "https://events.pagerduty.com/generic/2010-04-15/create_event.json",
  39. body=SUCCESS,
  40. )
  41. self.plugin.set_option("service_key", "abcdef", self.project)
  42. event = self.store_event(
  43. data={
  44. "message": "Hello world",
  45. "level": "warning",
  46. "platform": "python",
  47. "culprit": "foo.bar",
  48. },
  49. project_id=self.project.id,
  50. )
  51. assert event.group is not None
  52. group = event.group
  53. rule = Rule.objects.create(project=self.project, label="my rule")
  54. notification = Notification(event=event, rule=rule)
  55. with self.options({"system.url-prefix": "http://example.com"}):
  56. self.plugin.notify(notification)
  57. request = responses.calls[0].request
  58. payload = orjson.loads(request.body)
  59. assert payload == {
  60. "client_url": "http://example.com",
  61. "event_type": "trigger",
  62. "contexts": [
  63. {
  64. "text": "View Sentry Issue Details",
  65. "href": f"http://example.com/organizations/baz/issues/{group.id}/?referrer=pagerduty_plugin",
  66. "type": "link",
  67. }
  68. ],
  69. "incident_key": str(group.id),
  70. "client": "sentry",
  71. "details": {
  72. "project": self.project.name,
  73. "release": None,
  74. "url": f"http://example.com/organizations/baz/issues/{group.id}/?referrer=pagerduty_plugin",
  75. "culprit": group.culprit,
  76. "platform": "python",
  77. "event_id": event.event_id,
  78. "tags": {"level": "warning"},
  79. "datetime": event.datetime.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
  80. },
  81. "service_key": "abcdef",
  82. "description": event.message,
  83. }
  84. def test_no_secrets(self):
  85. self.user = self.create_user("foo@example.com")
  86. self.org = self.create_organization(owner=self.user, name="Rowdy Tiger")
  87. self.team = self.create_team(organization=self.org, name="Mariachi Band")
  88. self.project = self.create_project(organization=self.org, teams=[self.team], name="Bengal")
  89. self.login_as(self.user)
  90. self.plugin.set_option("service_key", "abcdef", self.project)
  91. url = reverse(
  92. "sentry-api-0-project-plugin-details",
  93. args=[self.org.slug, self.project.slug, "pagerduty"],
  94. )
  95. res = self.client.get(url)
  96. config = orjson.loads(res.content)["config"]
  97. key_config = [item for item in config if item["name"] == "service_key"][0]
  98. assert key_config.get("type") == "secret"
  99. assert key_config.get("value") is None
  100. assert key_config.get("hasSavedValue") is True
  101. assert key_config.get("prefix") == "abcd"