test_project_event_details.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. from django.urls import reverse
  2. from sentry.testutils.cases import APITestCase, PerformanceIssueTestCase, SnubaTestCase
  3. from sentry.testutils.helpers.datetime import before_now, iso_format
  4. from sentry.testutils.silo import region_silo_test
  5. from sentry.utils.samples import load_data
  6. from tests.sentry.issues.test_utils import OccurrenceTestMixin
  7. class ProjectEventDetailsTest(APITestCase, SnubaTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. self.login_as(user=self.user)
  11. self.setup_data()
  12. def setup_data(self):
  13. one_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. four_min_ago = iso_format(before_now(minutes=4))
  17. self.prev_event = self.store_event(
  18. data={"event_id": "a" * 32, "timestamp": four_min_ago, "fingerprint": ["group-1"]},
  19. project_id=self.project.id,
  20. )
  21. self.cur_event = self.store_event(
  22. data={"event_id": "b" * 32, "timestamp": three_min_ago, "fingerprint": ["group-1"]},
  23. project_id=self.project.id,
  24. )
  25. self.next_event = self.store_event(
  26. data={
  27. "event_id": "c" * 32,
  28. "timestamp": two_min_ago,
  29. "fingerprint": ["group-1"],
  30. "environment": "production",
  31. "tags": {"environment": "production"},
  32. },
  33. project_id=self.project.id,
  34. )
  35. self.cur_group = self.next_event.group
  36. # Event in different group
  37. self.store_event(
  38. data={
  39. "event_id": "d" * 32,
  40. "timestamp": one_min_ago,
  41. "fingerprint": ["group-2"],
  42. "environment": "production",
  43. "tags": {"environment": "production"},
  44. },
  45. project_id=self.project.id,
  46. )
  47. def test_simple(self):
  48. url = reverse(
  49. "sentry-api-0-project-event-details",
  50. kwargs={
  51. "event_id": self.cur_event.event_id,
  52. "project_slug": self.project.slug,
  53. "organization_slug": self.project.organization.slug,
  54. },
  55. )
  56. response = self.client.get(url, format="json")
  57. assert response.status_code == 200, response.content
  58. assert response.data["id"] == str(self.cur_event.event_id)
  59. assert response.data["nextEventID"] == str(self.next_event.event_id)
  60. assert response.data["previousEventID"] == str(self.prev_event.event_id)
  61. assert response.data["groupID"] == str(self.cur_group.id)
  62. def test_snuba_no_prev(self):
  63. url = reverse(
  64. "sentry-api-0-project-event-details",
  65. kwargs={
  66. "event_id": self.prev_event.event_id,
  67. "project_slug": self.project.slug,
  68. "organization_slug": self.project.organization.slug,
  69. },
  70. )
  71. response = self.client.get(url, format="json")
  72. assert response.status_code == 200, response.content
  73. assert response.data["id"] == str(self.prev_event.event_id)
  74. assert response.data["previousEventID"] is None
  75. assert response.data["nextEventID"] == self.cur_event.event_id
  76. assert response.data["groupID"] == str(self.cur_group.id)
  77. def test_snuba_with_environment(self):
  78. url = reverse(
  79. "sentry-api-0-project-event-details",
  80. kwargs={
  81. "event_id": self.cur_event.event_id,
  82. "project_slug": self.project.slug,
  83. "organization_slug": self.project.organization.slug,
  84. },
  85. )
  86. response = self.client.get(
  87. url, format="json", data={"environment": ["production", "staging"]}
  88. )
  89. assert response.status_code == 200, response.content
  90. assert response.data["id"] == str(self.cur_event.event_id)
  91. assert response.data["previousEventID"] is None
  92. assert response.data["nextEventID"] == self.next_event.event_id
  93. assert response.data["groupID"] == str(self.cur_group.id)
  94. def test_ignores_different_group(self):
  95. url = reverse(
  96. "sentry-api-0-project-event-details",
  97. kwargs={
  98. "event_id": self.next_event.event_id,
  99. "project_slug": self.project.slug,
  100. "organization_slug": self.project.organization.slug,
  101. },
  102. )
  103. response = self.client.get(url, format="json")
  104. assert response.status_code == 200, response.content
  105. assert response.data["id"] == str(self.next_event.event_id)
  106. assert response.data["nextEventID"] is None
  107. @region_silo_test
  108. class ProjectEventDetailsGenericTest(OccurrenceTestMixin, ProjectEventDetailsTest):
  109. def setup_data(self):
  110. one_min_ago = iso_format(before_now(minutes=1))
  111. two_min_ago = iso_format(before_now(minutes=2))
  112. three_min_ago = iso_format(before_now(minutes=3))
  113. four_min_ago = iso_format(before_now(minutes=4))
  114. prev_event_id = "a" * 32
  115. self.prev_event, _ = self.process_occurrence(
  116. event_id=prev_event_id,
  117. project_id=self.project.id,
  118. fingerprint=["group-1"],
  119. event_data={
  120. "timestamp": four_min_ago,
  121. "message_timestamp": four_min_ago,
  122. },
  123. )
  124. cur_event_id = "b" * 32
  125. self.cur_event, cur_group_info = self.process_occurrence(
  126. event_id=cur_event_id,
  127. project_id=self.project.id,
  128. fingerprint=["group-1"],
  129. event_data={
  130. "timestamp": three_min_ago,
  131. "message_timestamp": three_min_ago,
  132. },
  133. )
  134. assert cur_group_info is not None
  135. self.cur_group = cur_group_info.group
  136. next_event_id = "c" * 32
  137. self.next_event, _ = self.process_occurrence(
  138. event_id=next_event_id,
  139. project_id=self.project.id,
  140. fingerprint=["group-1"],
  141. event_data={
  142. "timestamp": two_min_ago,
  143. "message_timestamp": two_min_ago,
  144. "tags": {"environment": "production"},
  145. },
  146. )
  147. unrelated_event_id = "d" * 32
  148. self.process_occurrence(
  149. event_id=unrelated_event_id,
  150. project_id=self.project.id,
  151. fingerprint=["group-2"],
  152. event_data={
  153. "timestamp": one_min_ago,
  154. "message_timestamp": one_min_ago,
  155. "tags": {"environment": "production"},
  156. },
  157. )
  158. def test_generic_event_with_occurrence(self):
  159. url = reverse(
  160. "sentry-api-0-project-event-details",
  161. kwargs={
  162. "event_id": self.cur_event.event_id,
  163. "project_slug": self.project.slug,
  164. "organization_slug": self.project.organization.slug,
  165. },
  166. )
  167. response = self.client.get(url, format="json", data={"group_id": self.cur_group.id})
  168. assert response.status_code == 200, response.content
  169. assert response.data["id"] == self.cur_event.event_id
  170. assert response.data["occurrence"] is not None
  171. assert response.data["occurrence"]["id"] == self.cur_event.id
  172. @region_silo_test
  173. class ProjectEventDetailsTransactionTest(APITestCase, SnubaTestCase, PerformanceIssueTestCase):
  174. def setUp(self):
  175. super().setUp()
  176. self.login_as(user=self.user)
  177. project = self.create_project()
  178. one_min_ago = before_now(minutes=1)
  179. two_min_ago = before_now(minutes=2)
  180. three_min_ago = before_now(minutes=3)
  181. four_min_ago = before_now(minutes=4)
  182. self.prev_transaction_event = self.create_performance_issue(
  183. event_data=load_data(
  184. event_id="a" * 32,
  185. platform="transaction-n-plus-one",
  186. timestamp=four_min_ago,
  187. start_timestamp=four_min_ago,
  188. ),
  189. project_id=project.id,
  190. )
  191. self.group = self.prev_transaction_event.group
  192. self.cur_transaction_event = self.create_performance_issue(
  193. event_data=load_data(
  194. event_id="b" * 32,
  195. platform="transaction-n-plus-one",
  196. timestamp=three_min_ago,
  197. start_timestamp=three_min_ago,
  198. ),
  199. project_id=project.id,
  200. )
  201. self.next_transaction_event = self.create_performance_issue(
  202. event_data=load_data(
  203. event_id="c" * 32,
  204. platform="transaction-n-plus-one",
  205. timestamp=two_min_ago,
  206. start_timestamp=two_min_ago,
  207. ),
  208. project_id=project.id,
  209. )
  210. self.create_performance_issue(
  211. event_data=load_data(
  212. event_id="d" * 32,
  213. platform="transaction-n-plus-one",
  214. timestamp=one_min_ago,
  215. start_timestamp=one_min_ago,
  216. ),
  217. fingerprint="other_group",
  218. project_id=project.id,
  219. )
  220. def test_transaction_event(self):
  221. """Test that you can look up a transaction event w/ a prev and next event"""
  222. url = reverse(
  223. "sentry-api-0-project-event-details",
  224. kwargs={
  225. "event_id": self.cur_transaction_event.event_id,
  226. "project_slug": self.cur_transaction_event.project.slug,
  227. "organization_slug": self.cur_transaction_event.project.organization.slug,
  228. },
  229. )
  230. response = self.client.get(url, format="json", data={"group_id": self.group.id})
  231. assert response.status_code == 200, response.content
  232. assert response.data["id"] == str(self.cur_transaction_event.event_id)
  233. assert response.data["nextEventID"] == str(self.next_transaction_event.event_id)
  234. assert response.data["previousEventID"] == str(self.prev_transaction_event.event_id)
  235. assert response.data["groupID"] == str(self.cur_transaction_event.group.id)
  236. def test_no_previous_event(self):
  237. """Test the case in which there is no previous event"""
  238. url = reverse(
  239. "sentry-api-0-project-event-details",
  240. kwargs={
  241. "event_id": self.prev_transaction_event.event_id,
  242. "project_slug": self.prev_transaction_event.project.slug,
  243. "organization_slug": self.prev_transaction_event.project.organization.slug,
  244. },
  245. )
  246. response = self.client.get(url, format="json", data={"group_id": self.group.id})
  247. assert response.status_code == 200, response.content
  248. assert response.data["id"] == str(self.prev_transaction_event.event_id)
  249. assert response.data["previousEventID"] is None
  250. assert response.data["nextEventID"] == self.cur_transaction_event.event_id
  251. assert response.data["groupID"] == str(self.prev_transaction_event.group.id)
  252. def test_ignores_different_group(self):
  253. """Test that a different group's events aren't attributed to the one that was passed"""
  254. url = reverse(
  255. "sentry-api-0-project-event-details",
  256. kwargs={
  257. "event_id": self.next_transaction_event.event_id,
  258. "project_slug": self.next_transaction_event.project.slug,
  259. "organization_slug": self.next_transaction_event.project.organization.slug,
  260. },
  261. )
  262. response = self.client.get(url, format="json", data={"group_id": self.group.id})
  263. assert response.status_code == 200, response.content
  264. assert response.data["id"] == str(self.next_transaction_event.event_id)
  265. assert response.data["nextEventID"] is None
  266. def test_no_group_id(self):
  267. """Test the case where a group_id was not passed"""
  268. url = reverse(
  269. "sentry-api-0-project-event-details",
  270. kwargs={
  271. "event_id": self.cur_transaction_event.event_id,
  272. "project_slug": self.cur_transaction_event.project.slug,
  273. "organization_slug": self.cur_transaction_event.project.organization.slug,
  274. },
  275. )
  276. response = self.client.get(url, format="json")
  277. assert response.status_code == 200, response.content
  278. assert response.data["id"] == str(self.cur_transaction_event.event_id)
  279. assert response.data["previousEventID"] is None
  280. assert response.data["nextEventID"] is None
  281. assert response.data["groupID"] is None
  282. @region_silo_test
  283. class ProjectEventJsonEndpointTest(APITestCase, SnubaTestCase):
  284. def setUp(self):
  285. super().setUp()
  286. self.login_as(user=self.user)
  287. self.event_id = "c" * 32
  288. self.fingerprint = ["group_2"]
  289. self.min_ago = iso_format(before_now(minutes=1))
  290. self.event = self.store_event(
  291. data={
  292. "event_id": self.event_id,
  293. "timestamp": self.min_ago,
  294. "fingerprint": self.fingerprint,
  295. "user": {"email": self.user.email},
  296. },
  297. project_id=self.project.id,
  298. )
  299. self.url = reverse(
  300. "sentry-api-0-event-json",
  301. kwargs={
  302. "organization_slug": self.organization.slug,
  303. "project_slug": self.project.slug,
  304. "event_id": self.event_id,
  305. },
  306. )
  307. def assert_event(self, data):
  308. assert data["event_id"] == self.event_id
  309. assert data["user"]["email"] == self.user.email
  310. assert data["datetime"][:19] == self.min_ago
  311. assert data["fingerprint"] == self.fingerprint
  312. def test_simple(self):
  313. response = self.client.get(self.url, format="json")
  314. assert response.status_code == 200, response.content
  315. self.assert_event(response.data)
  316. def test_event_does_not_exist(self):
  317. self.url = reverse(
  318. "sentry-api-0-event-json",
  319. kwargs={
  320. "organization_slug": self.organization.slug,
  321. "project_slug": self.project.slug,
  322. "event_id": "no" * 16,
  323. },
  324. )
  325. response = self.client.get(self.url, format="json")
  326. assert response.status_code == 404, response.content
  327. assert response.data == {"detail": "Event not found"}
  328. def test_user_unauthorized(self):
  329. user = self.create_user()
  330. self.login_as(user)
  331. response = self.client.get(self.url, format="json")
  332. assert response.status_code == 403, response.content
  333. assert response.data == {"detail": "You do not have permission to perform this action."}
  334. def test_project_not_associated_with_event(self):
  335. project2 = self.create_project(organization=self.organization)
  336. url = reverse(
  337. "sentry-api-0-event-json",
  338. kwargs={
  339. "organization_slug": self.organization.slug,
  340. "project_slug": project2.slug,
  341. "event_id": self.event_id,
  342. },
  343. )
  344. response = self.client.get(url, format="json")
  345. assert response.status_code == 404, response.content
  346. assert response.data == {"detail": "Event not found"}