test_plugin.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import responses
  2. from exam import fixture
  3. from sentry.shared_integrations.exceptions import ApiError
  4. from sentry.testutils import PluginTestCase
  5. from sentry.utils import json
  6. from sentry_plugins.splunk.plugin import SplunkPlugin
  7. class SplunkPluginTest(PluginTestCase):
  8. @fixture
  9. def plugin(self):
  10. return SplunkPlugin()
  11. def test_conf_key(self):
  12. assert self.plugin.conf_key == "splunk"
  13. def test_entry_point(self):
  14. self.assertPluginInstalled("splunk", self.plugin)
  15. @responses.activate
  16. def test_simple_notification(self):
  17. responses.add(responses.POST, "https://splunk.example.com:8088/services/collector")
  18. self.plugin.set_option("token", "12345678-1234-1234-1234-1234567890AB", self.project)
  19. self.plugin.set_option("index", "main", self.project)
  20. self.plugin.set_option("instance", "https://splunk.example.com:8088", self.project)
  21. event = self.store_event(
  22. data={"message": "Hello world", "level": "warning"}, project_id=self.project.id
  23. )
  24. with self.options({"system.url-prefix": "http://example.com"}):
  25. self.plugin.post_process(event)
  26. request = responses.calls[0].request
  27. payload = json.loads(request.body)
  28. assert payload == self.plugin.get_event_payload(event)
  29. headers = request.headers
  30. assert headers["Authorization"] == "Splunk 12345678-1234-1234-1234-1234567890AB"
  31. @responses.activate
  32. def test_dont_reraise_error(self):
  33. responses.add(
  34. responses.POST, "https://splunk.example.com:8088/services/collector", status=404
  35. )
  36. self.plugin.set_option("token", "12345678-1234-1234-1234-1234567890AB", self.project)
  37. self.plugin.set_option("index", "main", self.project)
  38. self.plugin.set_option("instance", "https://splunk.example.com:8088", self.project)
  39. event = self.store_event(
  40. data={"message": "Hello world", "level": "warning"}, project_id=self.project.id
  41. )
  42. with self.options({"system.url-prefix": "http://example.com"}):
  43. self.plugin.post_process(event)
  44. resp = responses.calls[0].response
  45. assert resp.status_code == 404
  46. @responses.activate
  47. def test_reraise_error(self):
  48. responses.add(
  49. responses.POST, "https://splunk.example.com:8088/services/collector", status=500
  50. )
  51. self.plugin.set_option("token", "12345678-1234-1234-1234-1234567890AB", self.project)
  52. self.plugin.set_option("index", "main", self.project)
  53. self.plugin.set_option("instance", "https://splunk.example.com:8088", self.project)
  54. event = self.store_event(
  55. data={"message": "Hello world", "level": "warning"}, project_id=self.project.id
  56. )
  57. with self.options({"system.url-prefix": "http://example.com"}):
  58. with self.assertRaises(ApiError):
  59. self.plugin.post_process(event)
  60. def test_http_payload(self):
  61. event = self.store_event(
  62. data={
  63. "request": {
  64. "url": "http://example.com",
  65. "method": "POST",
  66. "headers": {"Referer": "http://example.com/foo"},
  67. }
  68. },
  69. project_id=self.project.id,
  70. )
  71. result = self.plugin.get_event_payload(event)
  72. assert result["event"]["request_url"] == "http://example.com/"
  73. assert result["event"]["request_method"] == "POST"
  74. assert result["event"]["request_referer"] == "http://example.com/foo"
  75. def test_error_payload(self):
  76. event = self.store_event(
  77. data={
  78. "exception": {"values": [{"type": "ValueError", "value": "foo bar"}]},
  79. "type": "error",
  80. },
  81. project_id=self.project.id,
  82. )
  83. result = self.plugin.get_event_payload(event)
  84. assert result["event"]["type"] == "error"
  85. assert result["event"]["exception_type"] == "ValueError"
  86. assert result["event"]["exception_value"] == "foo bar"
  87. def test_csp_payload(self):
  88. event = self.store_event(
  89. data={
  90. "csp": {
  91. "document_uri": "http://example.com/",
  92. "violated_directive": "style-src cdn.example.com",
  93. "blocked_uri": "http://example.com/style.css",
  94. "effective_directive": "style-src",
  95. },
  96. "type": "csp",
  97. },
  98. project_id=self.project.id,
  99. )
  100. result = self.plugin.get_event_payload(event)
  101. assert result["event"]["type"] == "csp"
  102. assert result["event"]["csp_document_uri"] == "http://example.com/"
  103. assert result["event"]["csp_violated_directive"] == "style-src cdn.example.com"
  104. assert result["event"]["csp_blocked_uri"] == "http://example.com/style.css"
  105. assert result["event"]["csp_effective_directive"] == "style-src"
  106. def test_user_payload(self):
  107. event = self.store_event(
  108. data={"user": {"id": "1", "email": "foo@example.com", "ip_address": "127.0.0.1"}},
  109. project_id=self.project.id,
  110. )
  111. result = self.plugin.get_event_payload(event)
  112. assert result["event"]["user_id"] == "1"
  113. assert result["event"]["user_email_hash"] == "b48def645758b95537d4424c84d1a9ff"
  114. assert result["event"]["user_ip_trunc"] == "127.0.0.0"