tests.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. from io import BytesIO
  2. from uuid import uuid4
  3. from sentry.models.eventattachment import EventAttachment
  4. from sentry.testutils import RelayStoreHelper, TransactionTestCase
  5. from sentry.testutils.helpers import Feature
  6. from sentry.testutils.helpers.datetime import before_now, iso_format, timestamp_format
  7. class SentryRemoteTest(RelayStoreHelper, TransactionTestCase):
  8. # used to be test_ungzipped_data
  9. def test_simple_data(self):
  10. event_data = {"message": "hello", "timestamp": iso_format(before_now(seconds=1))}
  11. event = self.post_and_retrieve_event(event_data)
  12. assert event.message == "hello"
  13. def test_csp(self):
  14. event_data = {
  15. "csp-report": {
  16. "document-uri": "https://example.com/foo/bar",
  17. "referrer": "https://www.google.com/",
  18. "violated-directive": "default-src self",
  19. "original-policy": "default-src self; report-uri /csp-hotline.php",
  20. "blocked-uri": "http://evilhackerscripts.com",
  21. }
  22. }
  23. event = self.post_and_retrieve_security_report(event_data)
  24. assert event.message == "Blocked 'default-src' from 'evilhackerscripts.com'"
  25. def test_hpkp(self):
  26. event_data = {
  27. "date-time": "2014-04-06T13:00:50Z",
  28. "hostname": "www.example.com",
  29. "port": 443,
  30. "effective-expiration-date": "2014-05-01T12:40:50Z",
  31. "include-subdomains": False,
  32. "served-certificate-chain": [
  33. "-----BEGIN CERTIFICATE-----\n MIIEBDCCAuygBQUAMEIxCzAJBgNVBAYTAlVT\n -----END CERTIFICATE-----"
  34. ],
  35. "validated-certificate-chain": [
  36. "-----BEGIN CERTIFICATE-----\n MIIEBDCCAuygAwIBAgIDCzAJBgNVBAYTAlVT\n -----END CERTIFICATE-----"
  37. ],
  38. "known-pins": [
  39. 'pin-sha256="d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM="',
  40. 'pin-sha256="E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g="',
  41. ],
  42. }
  43. event = self.post_and_retrieve_security_report(event_data)
  44. assert event.message == "Public key pinning validation failed for 'www.example.com'"
  45. assert event.group.title == "Public key pinning validation failed for 'www.example.com'"
  46. def test_expect_ct(self):
  47. event_data = {
  48. "expect-ct-report": {
  49. "date-time": "2014-04-06T13:00:50Z",
  50. "hostname": "www.example.com",
  51. "port": 443,
  52. "effective-expiration-date": "2014-05-01T12:40:50Z",
  53. "served-certificate-chain": [
  54. "-----BEGIN CERTIFICATE-----\nABC\n-----END CERTIFICATE-----"
  55. ],
  56. "validated-certificate-chain": [
  57. "-----BEGIN CERTIFICATE-----\nCDE\n-----END CERTIFICATE-----"
  58. ],
  59. "scts": [
  60. {
  61. "version": 1,
  62. "status": "invalid",
  63. "source": "embedded",
  64. "serialized_sct": "ABCD==",
  65. }
  66. ],
  67. }
  68. }
  69. event = self.post_and_retrieve_security_report(event_data)
  70. assert event.message == "Expect-CT failed for 'www.example.com'"
  71. assert event.group.title == "Expect-CT failed for 'www.example.com'"
  72. def test_expect_staple(self):
  73. event_data = {
  74. "expect-staple-report": {
  75. "date-time": "2014-04-06T13:00:50Z",
  76. "hostname": "www.example.com",
  77. "port": 443,
  78. "response-status": "ERROR_RESPONSE",
  79. "cert-status": "REVOKED",
  80. "effective-expiration-date": "2014-05-01T12:40:50Z",
  81. "served-certificate-chain": [
  82. "-----BEGIN CERTIFICATE-----\nABC\n-----END CERTIFICATE-----"
  83. ],
  84. "validated-certificate-chain": [
  85. "-----BEGIN CERTIFICATE-----\nCDE\n-----END CERTIFICATE-----"
  86. ],
  87. }
  88. }
  89. event = self.post_and_retrieve_security_report(event_data)
  90. assert event.message == "Expect-Staple failed for 'www.example.com'"
  91. assert event.group.title == "Expect-Staple failed for 'www.example.com'"
  92. def test_standalone_attachment(self):
  93. event_id = uuid4().hex
  94. # First, ingest the attachment and ensure it is saved
  95. files = {"some_file": ("hello.txt", BytesIO(b"Hello World!"))}
  96. self.post_and_retrieve_attachment(event_id, files)
  97. # Next, ingest an error event
  98. event = self.post_and_retrieve_event({"event_id": event_id, "message": "my error"})
  99. assert event.event_id == event_id
  100. assert event.group_id
  101. # Finally, fetch the updated attachment and compare the group id
  102. attachment = EventAttachment.objects.get(project_id=self.project.id, event_id=event_id)
  103. assert attachment.group_id == event.group_id
  104. def test_transaction(self):
  105. event_data = {
  106. "event_id": "d2132d31b39445f1938d7e21b6bf0ec4",
  107. "type": "transaction",
  108. "transaction": "/organizations/:orgId/performance/:eventSlug/",
  109. "start_timestamp": iso_format(before_now(minutes=1, milliseconds=500)),
  110. "timestamp": iso_format(before_now(minutes=1)),
  111. "contexts": {
  112. "trace": {
  113. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  114. "span_id": "8f5a2b8768cafb4e",
  115. "type": "trace",
  116. }
  117. },
  118. "spans": [
  119. {
  120. "description": "<OrganizationContext>",
  121. "op": "react.mount",
  122. "parent_span_id": "8f5a2b8768cafb4e",
  123. "span_id": "bd429c44b67a3eb4",
  124. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=250)),
  125. "timestamp": timestamp_format(before_now(minutes=1)),
  126. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  127. },
  128. {
  129. "description": "browser span",
  130. "op": "browser",
  131. "parent_span_id": "8f5a2b8768cafb4e",
  132. "span_id": "a99fd04e79e17631",
  133. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  134. "timestamp": timestamp_format(before_now(minutes=1)),
  135. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  136. },
  137. {
  138. "description": "resource span",
  139. "op": "resource",
  140. "parent_span_id": "8f5a2b8768cafb4e",
  141. "span_id": "a71a5e67db5ce938",
  142. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  143. "timestamp": timestamp_format(before_now(minutes=1)),
  144. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  145. },
  146. {
  147. "description": "http span",
  148. "op": "http",
  149. "parent_span_id": "8f5a2b8768cafb4e",
  150. "span_id": "abe79ad9292b90a9",
  151. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  152. "timestamp": timestamp_format(before_now(minutes=1)),
  153. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  154. },
  155. {
  156. "description": "db span",
  157. "op": "db",
  158. "parent_span_id": "8f5a2b8768cafb4e",
  159. "span_id": "9c045ea336297177",
  160. "start_timestamp": timestamp_format(before_now(minutes=1, milliseconds=200)),
  161. "timestamp": timestamp_format(before_now(minutes=1)),
  162. "trace_id": "ff62a8b040f340bda5d830223def1d81",
  163. },
  164. ],
  165. }
  166. with Feature({"organizations:performance-ops-breakdown": True}):
  167. event = self.post_and_retrieve_event(event_data)
  168. raw_event = event.get_raw_data()
  169. assert raw_event["spans"] == event_data["spans"]
  170. assert raw_event["breakdowns"] == {
  171. "span_ops": {
  172. "ops.browser": {"value": 200.000048},
  173. "ops.resource": {"value": 200.000048},
  174. "ops.http": {"value": 200.000048},
  175. "ops.db": {"value": 200.000048},
  176. "total.time": {"value": 1050.000192},
  177. }
  178. }