test_integration.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.spans.grouping.utils import hash_values
  7. from sentry.tasks.relay import invalidate_project_config
  8. from sentry.testutils.cases import TransactionTestCase
  9. from sentry.testutils.helpers import Feature
  10. from sentry.testutils.helpers.datetime import before_now, iso_format, timestamp_format
  11. from sentry.testutils.relay import RelayStoreHelper
  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_transaction(self):
  110. event_data = {
  111. "event_id": "d2132d31b39445f1938d7e21b6bf0ec4",
  112. "type": "transaction",
  113. "transaction": "/organizations/:orgId/performance/:eventSlug/",
  114. "start_timestamp": iso_format(before_now(minutes=1, milliseconds=500)),
  115. "timestamp": iso_format(before_now(minutes=1)),
  116. "contexts": {
  117. "trace": {
  118. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  119. "span_id": "8f5a2b8768cafb4e",
  120. "type": "trace",
  121. }
  122. },
  123. "spans": [
  124. {
  125. "description": "<OrganizationContext>",
  126. "op": "react.mount",
  127. "parent_span_id": "8f5a2b8768cafb4e",
  128. "span_id": "bd429c44b67a3eb4",
  129. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=250)),
  130. "timestamp": timestamp_format(before_now(minutes=1)),
  131. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  132. },
  133. {
  134. "description": "browser span",
  135. "op": "browser",
  136. "parent_span_id": "bd429c44b67a3eb4",
  137. "span_id": "a99fd04e79e17631",
  138. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  139. "timestamp": timestamp_format(before_now(minutes=1)),
  140. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  141. },
  142. {
  143. "description": "resource span",
  144. "op": "resource",
  145. "parent_span_id": "bd429c44b67a3eb4",
  146. "span_id": "a71a5e67db5ce938",
  147. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  148. "timestamp": timestamp_format(before_now(minutes=1)),
  149. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  150. },
  151. {
  152. "description": "http span",
  153. "op": "http",
  154. "parent_span_id": "a99fd04e79e17631",
  155. "span_id": "abe79ad9292b90a9",
  156. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  157. "timestamp": timestamp_format(before_now(minutes=1)),
  158. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  159. },
  160. {
  161. "description": "db span",
  162. "op": "db",
  163. "parent_span_id": "abe79ad9292b90a9",
  164. "span_id": "9c045ea336297177",
  165. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  166. "timestamp": timestamp_format(before_now(minutes=1)),
  167. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  168. },
  169. ],
  170. }
  171. with Feature(
  172. {
  173. "projects:performance-suspect-spans-ingestion": True,
  174. }
  175. ):
  176. event = self.post_and_retrieve_event(event_data)
  177. raw_event = event.get_raw_data()
  178. exclusive_times = [
  179. pytest.approx(50, abs=2),
  180. pytest.approx(0, abs=2),
  181. pytest.approx(200, abs=2),
  182. pytest.approx(0, abs=2),
  183. pytest.approx(200, abs=2),
  184. ]
  185. for actual, expected, exclusive_time in zip(
  186. raw_event["spans"], event_data["spans"], exclusive_times
  187. ):
  188. assert actual == dict(
  189. expected,
  190. exclusive_time=exclusive_time,
  191. hash=hash_values([expected["description"]]),
  192. )
  193. assert raw_event["breakdowns"] == {
  194. "span_ops": {
  195. "ops.browser": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  196. "ops.resource": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  197. "ops.http": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  198. "ops.db": {"unit": "millisecond", "value": pytest.approx(200, abs=2)},
  199. "total.time": {"unit": "millisecond", "value": pytest.approx(1050, abs=2)},
  200. }
  201. }
  202. def test_project_config_compression(self):
  203. # Populate redis cache with compressed config:
  204. invalidate_project_config(public_key=self.projectkey, trigger="test")
  205. # Disable project config endpoint, to make sure Relay gets its data
  206. # from redis:
  207. with mock.patch(
  208. "sentry.api.endpoints.relay.project_configs.RelayProjectConfigsEndpoint.post"
  209. ):
  210. event_data = {"message": "hello", "timestamp": iso_format(before_now(seconds=1))}
  211. event = self.post_and_retrieve_event(event_data)
  212. assert event.message == "hello"