test_integration.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. from io import BytesIO
  2. from unittest import mock
  3. from uuid import uuid4
  4. import pytest
  5. from sentry.models.eventattachment import EventAttachment
  6. from sentry.tasks.relay import invalidate_project_config
  7. from sentry.testutils.cases import TransactionTestCase
  8. from sentry.testutils.helpers.datetime import before_now, iso_format, timestamp_format
  9. from sentry.testutils.relay import RelayStoreHelper
  10. from sentry.testutils.skips import requires_kafka
  11. pytestmark = [requires_kafka]
  12. class SentryRemoteTest(RelayStoreHelper, TransactionTestCase):
  13. # used to be test_ungzipped_data
  14. def test_simple_data(self):
  15. event_data = {"message": "hello", "timestamp": iso_format(before_now(seconds=1))}
  16. event = self.post_and_retrieve_event(event_data)
  17. assert event.message == "hello"
  18. def test_csp(self):
  19. event_data = {
  20. "csp-report": {
  21. "document-uri": "https://example.com/foo/bar",
  22. "referrer": "https://www.google.com/",
  23. "violated-directive": "default-src self",
  24. "original-policy": "default-src self; report-uri /csp-hotline.php",
  25. "blocked-uri": "http://evilhackerscripts.com",
  26. }
  27. }
  28. event = self.post_and_retrieve_security_report(event_data)
  29. assert event.message == "Blocked 'default-src' from 'evilhackerscripts.com'"
  30. def test_hpkp(self):
  31. event_data = {
  32. "date-time": "2014-04-06T13:00:50Z",
  33. "hostname": "www.example.com",
  34. "port": 443,
  35. "effective-expiration-date": "2014-05-01T12:40:50Z",
  36. "include-subdomains": False,
  37. "served-certificate-chain": [
  38. "-----BEGIN CERTIFICATE-----\n MIIEBDCCAuygBQUAMEIxCzAJBgNVBAYTAlVT\n -----END CERTIFICATE-----"
  39. ],
  40. "validated-certificate-chain": [
  41. "-----BEGIN CERTIFICATE-----\n MIIEBDCCAuygAwIBAgIDCzAJBgNVBAYTAlVT\n -----END CERTIFICATE-----"
  42. ],
  43. "known-pins": [
  44. 'pin-sha256="d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM="',
  45. 'pin-sha256="E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g="',
  46. ],
  47. }
  48. event = self.post_and_retrieve_security_report(event_data)
  49. assert event.message == "Public key pinning validation failed for 'www.example.com'"
  50. assert event.group.title == "Public key pinning validation failed for 'www.example.com'"
  51. def test_expect_ct(self):
  52. event_data = {
  53. "expect-ct-report": {
  54. "date-time": "2014-04-06T13:00:50Z",
  55. "hostname": "www.example.com",
  56. "port": 443,
  57. "effective-expiration-date": "2014-05-01T12:40:50Z",
  58. "served-certificate-chain": [
  59. "-----BEGIN CERTIFICATE-----\nABC\n-----END CERTIFICATE-----"
  60. ],
  61. "validated-certificate-chain": [
  62. "-----BEGIN CERTIFICATE-----\nCDE\n-----END CERTIFICATE-----"
  63. ],
  64. "scts": [
  65. {
  66. "version": 1,
  67. "status": "invalid",
  68. "source": "embedded",
  69. "serialized_sct": "ABCD==",
  70. }
  71. ],
  72. }
  73. }
  74. event = self.post_and_retrieve_security_report(event_data)
  75. assert event.message == "Expect-CT failed for 'www.example.com'"
  76. assert event.group.title == "Expect-CT failed for 'www.example.com'"
  77. def test_expect_staple(self):
  78. event_data = {
  79. "expect-staple-report": {
  80. "date-time": "2014-04-06T13:00:50Z",
  81. "hostname": "www.example.com",
  82. "port": 443,
  83. "response-status": "ERROR_RESPONSE",
  84. "cert-status": "REVOKED",
  85. "effective-expiration-date": "2014-05-01T12:40:50Z",
  86. "served-certificate-chain": [
  87. "-----BEGIN CERTIFICATE-----\nABC\n-----END CERTIFICATE-----"
  88. ],
  89. "validated-certificate-chain": [
  90. "-----BEGIN CERTIFICATE-----\nCDE\n-----END CERTIFICATE-----"
  91. ],
  92. }
  93. }
  94. event = self.post_and_retrieve_security_report(event_data)
  95. assert event.message == "Expect-Staple failed for 'www.example.com'"
  96. assert event.group.title == "Expect-Staple failed for 'www.example.com'"
  97. def test_standalone_attachment(self):
  98. event_id = uuid4().hex
  99. # First, ingest the attachment and ensure it is saved
  100. files = {"some_file": ("hello.txt", BytesIO(b"Hello World!"))}
  101. self.post_and_retrieve_attachment(event_id, files)
  102. # Next, ingest an error event
  103. event = self.post_and_retrieve_event({"event_id": event_id, "message": "my error"})
  104. assert event.event_id == event_id
  105. assert event.group_id
  106. # Finally, fetch the updated attachment and compare the group id
  107. attachment = EventAttachment.objects.get(project_id=self.project.id, event_id=event_id)
  108. assert attachment.group_id == event.group_id
  109. def test_blob_only_attachment(self):
  110. event_id = uuid4().hex
  111. files = {"some_file": ("hello.txt", BytesIO(b"Hello World! default"))}
  112. self.post_and_retrieve_attachment(event_id, files)
  113. attachments = EventAttachment.objects.filter(project_id=self.project.id)
  114. assert len(attachments) == 1
  115. attachment = EventAttachment.objects.get(event_id=event_id)
  116. with attachment.getfile() as blob:
  117. assert blob.read() == b"Hello World! default"
  118. assert attachment.blob_path is not None
  119. def test_transaction(self):
  120. event_data = {
  121. "event_id": "d2132d31b39445f1938d7e21b6bf0ec4",
  122. "type": "transaction",
  123. "transaction": "/organizations/:orgId/performance/:eventSlug/",
  124. "start_timestamp": iso_format(before_now(minutes=1, milliseconds=500)),
  125. "timestamp": iso_format(before_now(minutes=1)),
  126. "contexts": {
  127. "trace": {
  128. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  129. "span_id": "8f5a2b8768cafb4e",
  130. "type": "trace",
  131. }
  132. },
  133. "spans": [
  134. {
  135. "description": "<OrganizationContext>",
  136. "op": "react.mount",
  137. "parent_span_id": "8f5a2b8768cafb4e",
  138. "span_id": "bd429c44b67a3eb4",
  139. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=250)),
  140. "timestamp": timestamp_format(before_now(minutes=1)),
  141. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  142. },
  143. {
  144. "description": "browser span",
  145. "op": "browser",
  146. "parent_span_id": "bd429c44b67a3eb4",
  147. "span_id": "a99fd04e79e17631",
  148. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  149. "timestamp": timestamp_format(before_now(minutes=1)),
  150. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  151. },
  152. {
  153. "description": "resource span",
  154. "op": "resource",
  155. "parent_span_id": "bd429c44b67a3eb4",
  156. "span_id": "a71a5e67db5ce938",
  157. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  158. "timestamp": timestamp_format(before_now(minutes=1)),
  159. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  160. },
  161. {
  162. "description": "http span",
  163. "op": "http",
  164. "parent_span_id": "a99fd04e79e17631",
  165. "span_id": "abe79ad9292b90a9",
  166. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  167. "timestamp": timestamp_format(before_now(minutes=1)),
  168. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  169. },
  170. {
  171. "description": "db span",
  172. "op": "db",
  173. "parent_span_id": "abe79ad9292b90a9",
  174. "span_id": "9c045ea336297177",
  175. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  176. "timestamp": timestamp_format(before_now(minutes=1)),
  177. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  178. },
  179. ],
  180. }
  181. event = self.post_and_retrieve_event(event_data)
  182. raw_event = event.get_raw_data()
  183. assert raw_event["breakdowns"] == {
  184. "span_ops": {
  185. "ops.browser": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  186. "ops.resource": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  187. "ops.http": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  188. "ops.db": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  189. "total.time": {"unit": "millisecond", "value": pytest.approx(1050, abs=2)},
  190. }
  191. }
  192. @pytest.mark.skip(
  193. "Fails when test suite is run in region mode, possibly due to cache pollution"
  194. )
  195. # Background: This test used to pass reliably when the undecorated test cases were
  196. # run in monolith mode by default, but began failing during CI for unclear reasons
  197. # when the global default was switched to region mode. (See
  198. # get_default_silo_mode_for_test_cases in sentry/testutils/pytest/sentry.py.) Note
  199. # that this case was marked as @region_silo_test when it was passing, so there was
  200. # no change in the silo mode in which *this* case is run. The probable explanation
  201. # is that the test is sensitive to side effects (possibly in Redis?) of other test
  202. # cases, which *did* have their silo mode changed.
  203. def test_project_config_compression(self):
  204. # Populate redis cache with compressed config:
  205. invalidate_project_config(public_key=self.projectkey, trigger="test")
  206. # Disable project config endpoint, to make sure Relay gets its data
  207. # from redis:
  208. with mock.patch(
  209. "sentry.api.endpoints.relay.project_configs.RelayProjectConfigsEndpoint.post"
  210. ):
  211. event_data = {"message": "hello", "timestamp": iso_format(before_now(seconds=1))}
  212. event = self.post_and_retrieve_event(event_data)
  213. assert event.message == "hello"