test_organization_event_details.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. from datetime import timedelta
  2. import pytest
  3. from django.urls import NoReverseMatch, reverse
  4. from sentry.models import Group
  5. from sentry.testutils import APITestCase, SnubaTestCase
  6. from sentry.testutils.helpers.datetime import before_now, iso_format
  7. from sentry.utils.samples import load_data
  8. def format_project_event(project_slug, event_id):
  9. return f"{project_slug}:{event_id}"
  10. class OrganizationEventDetailsEndpointTest(APITestCase, SnubaTestCase):
  11. def setUp(self):
  12. super().setUp()
  13. min_ago = iso_format(before_now(minutes=1))
  14. two_min_ago = iso_format(before_now(minutes=2))
  15. three_min_ago = iso_format(before_now(minutes=3))
  16. self.login_as(user=self.user)
  17. self.project = self.create_project()
  18. self.project_2 = self.create_project()
  19. self.store_event(
  20. data={
  21. "event_id": "a" * 32,
  22. "message": "oh no",
  23. "timestamp": three_min_ago,
  24. "fingerprint": ["group-1"],
  25. },
  26. project_id=self.project.id,
  27. )
  28. self.store_event(
  29. data={
  30. "event_id": "b" * 32,
  31. "message": "very bad",
  32. "timestamp": two_min_ago,
  33. "fingerprint": ["group-1"],
  34. },
  35. project_id=self.project.id,
  36. )
  37. self.store_event(
  38. data={
  39. "event_id": "c" * 32,
  40. "message": "very bad",
  41. "timestamp": min_ago,
  42. "fingerprint": ["group-2"],
  43. },
  44. project_id=self.project.id,
  45. )
  46. self.groups = list(Group.objects.all().order_by("id"))
  47. def test_performance_flag(self):
  48. url = reverse(
  49. "sentry-api-0-organization-event-details",
  50. kwargs={
  51. "organization_slug": self.project.organization.slug,
  52. "project_slug": self.project.slug,
  53. "event_id": "a" * 32,
  54. },
  55. )
  56. with self.feature(
  57. {"organizations:discover-basic": False, "organizations:performance-view": True}
  58. ):
  59. response = self.client.get(url, format="json")
  60. assert response.status_code == 200, response.content
  61. assert response.data["id"] == "a" * 32
  62. assert response.data["projectSlug"] == self.project.slug
  63. def test_simple(self):
  64. url = reverse(
  65. "sentry-api-0-organization-event-details",
  66. kwargs={
  67. "organization_slug": self.project.organization.slug,
  68. "project_slug": self.project.slug,
  69. "event_id": "a" * 32,
  70. },
  71. )
  72. with self.feature("organizations:discover-basic"):
  73. response = self.client.get(url, format="json")
  74. assert response.status_code == 200, response.content
  75. assert response.data["id"] == "a" * 32
  76. assert response.data["projectSlug"] == self.project.slug
  77. def test_simple_transaction(self):
  78. min_ago = iso_format(before_now(minutes=1))
  79. event = self.store_event(
  80. data={
  81. "event_id": "d" * 32,
  82. "type": "transaction",
  83. "transaction": "api.issue.delete",
  84. "spans": [],
  85. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  86. "start_timestamp": iso_format(before_now(minutes=1, seconds=5)),
  87. "timestamp": min_ago,
  88. },
  89. project_id=self.project.id,
  90. )
  91. url = reverse(
  92. "sentry-api-0-organization-event-details",
  93. kwargs={
  94. "organization_slug": self.project.organization.slug,
  95. "project_slug": self.project.slug,
  96. "event_id": event.event_id,
  97. },
  98. )
  99. with self.feature("organizations:discover-basic"):
  100. response = self.client.get(url, format="json")
  101. assert response.status_code == 200
  102. assert response.data["id"] == "d" * 32
  103. assert response.data["type"] == "transaction"
  104. def test_no_access_missing_feature(self):
  105. with self.feature({"organizations:discover-basic": False}):
  106. url = reverse(
  107. "sentry-api-0-organization-event-details",
  108. kwargs={
  109. "organization_slug": self.project.organization.slug,
  110. "project_slug": self.project.slug,
  111. "event_id": "a" * 32,
  112. },
  113. )
  114. response = self.client.get(url, format="json")
  115. assert response.status_code == 404, response.content
  116. def test_access_non_member_project(self):
  117. # Add a new user to a project and then access events on project they are not part of.
  118. member_user = self.create_user()
  119. team = self.create_team(members=[member_user])
  120. self.create_project(organization=self.organization, teams=[team])
  121. # Enable open membership
  122. self.organization.flags.allow_joinleave = True
  123. self.organization.save()
  124. self.login_as(member_user)
  125. url = reverse(
  126. "sentry-api-0-organization-event-details",
  127. kwargs={
  128. "organization_slug": self.organization.slug,
  129. "project_slug": self.project.slug,
  130. "event_id": "a" * 32,
  131. },
  132. )
  133. with self.feature("organizations:discover-basic"):
  134. response = self.client.get(url, format="json")
  135. assert response.status_code == 200, response.content
  136. # When open membership is off, access should be denied to non owner users
  137. self.organization.flags.allow_joinleave = False
  138. self.organization.save()
  139. with self.feature("organizations:discover-basic"):
  140. response = self.client.get(url, format="json")
  141. assert response.status_code == 404, response.content
  142. def test_no_event(self):
  143. url = reverse(
  144. "sentry-api-0-organization-event-details",
  145. kwargs={
  146. "organization_slug": self.project.organization.slug,
  147. "project_slug": self.project.slug,
  148. "event_id": "d" * 32,
  149. },
  150. )
  151. with self.feature("organizations:discover-basic"):
  152. response = self.client.get(url, format="json")
  153. assert response.status_code == 404, response.content
  154. def test_invalid_event_id(self):
  155. with pytest.raises(NoReverseMatch):
  156. reverse(
  157. "sentry-api-0-organization-event-details",
  158. kwargs={
  159. "organization_slug": self.project.organization.slug,
  160. "project_slug": self.project.slug,
  161. "event_id": "not-an-event",
  162. },
  163. )
  164. def test_long_trace_description(self):
  165. data = load_data("transaction")
  166. data["event_id"] = "d" * 32
  167. data["timestamp"] = iso_format(before_now(minutes=1))
  168. data["start_timestamp"] = iso_format(before_now(minutes=1) - timedelta(seconds=5))
  169. data["contexts"]["trace"]["description"] = "b" * 512
  170. self.store_event(data=data, project_id=self.project.id)
  171. url = reverse(
  172. "sentry-api-0-organization-event-details",
  173. kwargs={
  174. "organization_slug": self.project.organization.slug,
  175. "project_slug": self.project.slug,
  176. "event_id": "d" * 32,
  177. },
  178. )
  179. with self.feature("organizations:discover-basic"):
  180. response = self.client.get(url, format="json")
  181. assert response.status_code == 200, response.content
  182. trace = response.data["contexts"]["trace"]
  183. original_trace = data["contexts"]["trace"]
  184. assert trace["trace_id"] == original_trace["trace_id"]
  185. assert trace["span_id"] == original_trace["span_id"]
  186. assert trace["parent_span_id"] == original_trace["parent_span_id"]
  187. assert trace["description"][:-3] in original_trace["description"]
  188. def test_blank_fields(self):
  189. url = reverse(
  190. "sentry-api-0-organization-event-details",
  191. kwargs={
  192. "organization_slug": self.project.organization.slug,
  193. "project_slug": self.project.slug,
  194. "event_id": "a" * 32,
  195. },
  196. )
  197. with self.feature("organizations:discover-basic"):
  198. response = self.client.get(
  199. url,
  200. data={"field": ["", " "], "statsPeriod": "24h"},
  201. format="json",
  202. )
  203. assert response.status_code == 200, response.content
  204. assert response.data["id"] == "a" * 32
  205. assert response.data["projectSlug"] == self.project.slug
  206. def test_out_of_retention(self):
  207. self.store_event(
  208. data={
  209. "event_id": "d" * 32,
  210. "message": "oh no",
  211. "timestamp": iso_format(before_now(days=2)),
  212. "fingerprint": ["group-1"],
  213. },
  214. project_id=self.project.id,
  215. )
  216. url = reverse(
  217. "sentry-api-0-organization-event-details",
  218. kwargs={
  219. "organization_slug": self.project.organization.slug,
  220. "project_slug": self.project.slug,
  221. "event_id": "d" * 32,
  222. },
  223. )
  224. with self.options({"system.event-retention-days": 1}):
  225. response = self.client.get(
  226. url,
  227. format="json",
  228. )
  229. assert response.status_code == 404, response.content