test_integration.py 10 KB

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