test_plugin.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from urllib.parse import parse_qs
  2. import responses
  3. from django.urls import reverse
  4. from exam import fixture
  5. from sentry.models import Rule
  6. from sentry.plugins.base import Notification
  7. from sentry.testutils import PluginTestCase
  8. from sentry.utils import json
  9. from sentry_plugins.pushover.plugin import PushoverPlugin
  10. SUCCESS = """{"status":1,"request":"e460545a8b333d0da2f3602aff3133d6"}"""
  11. class PushoverPluginTest(PluginTestCase):
  12. @fixture
  13. def plugin(self):
  14. return PushoverPlugin()
  15. def test_conf_key(self):
  16. assert self.plugin.conf_key == "pushover"
  17. def test_entry_point(self):
  18. self.assertPluginInstalled("pushover", self.plugin)
  19. def test_is_configured(self):
  20. assert self.plugin.is_configured(self.project) is False
  21. self.plugin.set_option("apikey", "abcdef", self.project)
  22. assert self.plugin.is_configured(self.project) is False
  23. self.plugin.set_option("userkey", "abcdef", self.project)
  24. assert self.plugin.is_configured(self.project) is True
  25. @responses.activate
  26. def test_simple_notification(self):
  27. responses.add("POST", "https://api.pushover.net/1/messages.json", body=SUCCESS)
  28. self.plugin.set_option("userkey", "abcdef", self.project)
  29. self.plugin.set_option("apikey", "ghijkl", self.project)
  30. event = self.store_event(
  31. data={"message": "Hello world", "level": "warning"}, project_id=self.project.id
  32. )
  33. group = event.group
  34. rule = Rule.objects.create(project=self.project, label="my rule")
  35. notification = Notification(event=event, rule=rule)
  36. with self.options({"system.url-prefix": "http://example.com"}):
  37. self.plugin.notify(notification)
  38. request = responses.calls[0].request
  39. payload = parse_qs(request.body)
  40. assert payload == {
  41. "message": [f"{event.title}\n\nTags: level=warning"],
  42. "title": ["Bar: Hello world"],
  43. "url": [
  44. f"http://example.com/organizations/baz/issues/{group.id}/?referrer=pushover_plugin"
  45. ],
  46. "url_title": ["Issue Details"],
  47. "priority": ["0"],
  48. "user": ["abcdef"],
  49. "token": ["ghijkl"],
  50. "expire": ["90"],
  51. "retry": ["30"],
  52. }
  53. @responses.activate
  54. def test_emergency_notification(self):
  55. responses.add("POST", "https://api.pushover.net/1/messages.json", body=SUCCESS)
  56. self.plugin.set_option("userkey", "abcdef", self.project)
  57. self.plugin.set_option("apikey", "ghijkl", self.project)
  58. self.plugin.set_option("priority", "2", self.project)
  59. self.plugin.set_option("expire", 90, self.project)
  60. self.plugin.set_option("retry", 30, self.project)
  61. event = self.store_event(
  62. data={"message": "Hello world", "level": "warning"}, project_id=self.project.id
  63. )
  64. group = event.group
  65. rule = Rule.objects.create(project=self.project, label="my rule")
  66. notification = Notification(event=event, rule=rule)
  67. with self.options({"system.url-prefix": "http://example.com"}):
  68. self.plugin.notify(notification)
  69. request = responses.calls[0].request
  70. payload = parse_qs(request.body)
  71. assert payload == {
  72. "message": [f"{event.title}\n\nTags: level=warning"],
  73. "title": ["Bar: Hello world"],
  74. "url": [
  75. f"http://example.com/organizations/baz/issues/{group.id}/?referrer=pushover_plugin"
  76. ],
  77. "url_title": ["Issue Details"],
  78. "priority": ["2"],
  79. "user": ["abcdef"],
  80. "token": ["ghijkl"],
  81. "expire": ["90"],
  82. "retry": ["30"],
  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("userkey", "abcdef", self.project)
  91. self.plugin.set_option("apikey", "abcdef", self.project)
  92. url = reverse(
  93. "sentry-api-0-project-plugin-details",
  94. args=[self.org.slug, self.project.slug, "pushover"],
  95. )
  96. res = self.client.get(url)
  97. config = json.loads(res.content)["config"]
  98. userkey_config = [item for item in config if item["name"] == "userkey"][0]
  99. apikey_config = [item for item in config if item["name"] == "apikey"][0]
  100. assert userkey_config.get("type") == "secret"
  101. assert userkey_config.get("value") is None
  102. assert userkey_config.get("hasSavedValue") is True
  103. assert userkey_config.get("prefix") == "abcd"
  104. assert apikey_config.get("type") == "secret"
  105. assert apikey_config.get("value") is None
  106. assert apikey_config.get("hasSavedValue") is True
  107. assert apikey_config.get("prefix") == "abcd"