test_plugin.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import responses
  2. from django.urls import reverse
  3. from exam import fixture
  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.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. @fixture
  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. group = event.group
  52. rule = Rule.objects.create(project=self.project, label="my rule")
  53. notification = Notification(event=event, rule=rule)
  54. with self.options({"system.url-prefix": "http://example.com"}):
  55. self.plugin.notify(notification)
  56. request = responses.calls[0].request
  57. payload = json.loads(request.body)
  58. assert payload == {
  59. "client_url": "http://example.com",
  60. "event_type": "trigger",
  61. "contexts": [
  62. {
  63. "text": "View Sentry Issue Details",
  64. "href": f"http://example.com/organizations/baz/issues/{group.id}/?referrer=pagerduty_plugin",
  65. "type": "link",
  66. }
  67. ],
  68. "incident_key": str(group.id),
  69. "client": "sentry",
  70. "details": {
  71. "project": self.project.name,
  72. "release": None,
  73. "url": f"http://example.com/organizations/baz/issues/{group.id}/?referrer=pagerduty_plugin",
  74. "culprit": group.culprit,
  75. "platform": "python",
  76. "event_id": event.event_id,
  77. "tags": {"level": "warning"},
  78. "datetime": event.datetime.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
  79. },
  80. "service_key": "abcdef",
  81. "description": event.message,
  82. }
  83. def test_no_secrets(self):
  84. self.user = self.create_user("foo@example.com")
  85. self.org = self.create_organization(owner=self.user, name="Rowdy Tiger")
  86. self.team = self.create_team(organization=self.org, name="Mariachi Band")
  87. self.project = self.create_project(organization=self.org, teams=[self.team], name="Bengal")
  88. self.login_as(self.user)
  89. self.plugin.set_option("service_key", "abcdef", self.project)
  90. url = reverse(
  91. "sentry-api-0-project-plugin-details",
  92. args=[self.org.slug, self.project.slug, "pagerduty"],
  93. )
  94. res = self.client.get(url)
  95. config = json.loads(res.content)["config"]
  96. key_config = [item for item in config if item["name"] == "service_key"][0]
  97. assert key_config.get("type") == "secret"
  98. assert key_config.get("value") is None
  99. assert key_config.get("hasSavedValue") is True
  100. assert key_config.get("prefix") == "abcd"