test_project_event_details.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. from django.urls import reverse
  2. from sentry.testutils import APITestCase, SnubaTestCase
  3. from sentry.testutils.helpers.datetime import before_now, iso_format
  4. class ProjectEventDetailsTest(APITestCase, SnubaTestCase):
  5. def setUp(self):
  6. super().setUp()
  7. self.login_as(user=self.user)
  8. project = self.create_project()
  9. one_min_ago = iso_format(before_now(minutes=1))
  10. two_min_ago = iso_format(before_now(minutes=2))
  11. three_min_ago = iso_format(before_now(minutes=3))
  12. four_min_ago = iso_format(before_now(minutes=4))
  13. self.prev_event = self.store_event(
  14. data={"event_id": "a" * 32, "timestamp": four_min_ago, "fingerprint": ["group-1"]},
  15. project_id=project.id,
  16. )
  17. self.cur_event = self.store_event(
  18. data={"event_id": "b" * 32, "timestamp": three_min_ago, "fingerprint": ["group-1"]},
  19. project_id=project.id,
  20. )
  21. self.next_event = self.store_event(
  22. data={
  23. "event_id": "c" * 32,
  24. "timestamp": two_min_ago,
  25. "fingerprint": ["group-1"],
  26. "environment": "production",
  27. "tags": {"environment": "production"},
  28. },
  29. project_id=project.id,
  30. )
  31. # Event in different group
  32. self.store_event(
  33. data={
  34. "event_id": "d" * 32,
  35. "timestamp": one_min_ago,
  36. "fingerprint": ["group-2"],
  37. "environment": "production",
  38. "tags": {"environment": "production"},
  39. },
  40. project_id=project.id,
  41. )
  42. def test_simple(self):
  43. url = reverse(
  44. "sentry-api-0-project-event-details",
  45. kwargs={
  46. "event_id": self.cur_event.event_id,
  47. "project_slug": self.cur_event.project.slug,
  48. "organization_slug": self.cur_event.project.organization.slug,
  49. },
  50. )
  51. response = self.client.get(url, format="json")
  52. assert response.status_code == 200, response.content
  53. assert response.data["id"] == str(self.cur_event.event_id)
  54. assert response.data["nextEventID"] == str(self.next_event.event_id)
  55. assert response.data["previousEventID"] == str(self.prev_event.event_id)
  56. assert response.data["groupID"] == str(self.cur_event.group.id)
  57. def test_snuba_no_prev(self):
  58. url = reverse(
  59. "sentry-api-0-project-event-details",
  60. kwargs={
  61. "event_id": self.prev_event.event_id,
  62. "project_slug": self.prev_event.project.slug,
  63. "organization_slug": self.prev_event.project.organization.slug,
  64. },
  65. )
  66. response = self.client.get(url, format="json")
  67. assert response.status_code == 200, response.content
  68. assert response.data["id"] == str(self.prev_event.event_id)
  69. assert response.data["previousEventID"] is None
  70. assert response.data["nextEventID"] == self.cur_event.event_id
  71. assert response.data["groupID"] == str(self.prev_event.group.id)
  72. def test_snuba_with_environment(self):
  73. url = reverse(
  74. "sentry-api-0-project-event-details",
  75. kwargs={
  76. "event_id": self.cur_event.event_id,
  77. "project_slug": self.cur_event.project.slug,
  78. "organization_slug": self.cur_event.project.organization.slug,
  79. },
  80. )
  81. response = self.client.get(
  82. url, format="json", data={"environment": ["production", "staging"]}
  83. )
  84. assert response.status_code == 200, response.content
  85. assert response.data["id"] == str(self.cur_event.event_id)
  86. assert response.data["previousEventID"] is None
  87. assert response.data["nextEventID"] == self.next_event.event_id
  88. assert response.data["groupID"] == str(self.prev_event.group.id)
  89. def test_ignores_different_group(self):
  90. url = reverse(
  91. "sentry-api-0-project-event-details",
  92. kwargs={
  93. "event_id": self.next_event.event_id,
  94. "project_slug": self.next_event.project.slug,
  95. "organization_slug": self.next_event.project.organization.slug,
  96. },
  97. )
  98. response = self.client.get(url, format="json")
  99. assert response.status_code == 200, response.content
  100. assert response.data["id"] == str(self.next_event.event_id)
  101. assert response.data["nextEventID"] is None
  102. class ProjectEventJsonEndpointTest(APITestCase, SnubaTestCase):
  103. def setUp(self):
  104. super().setUp()
  105. self.login_as(user=self.user)
  106. self.event_id = "c" * 32
  107. self.fingerprint = ["group_2"]
  108. self.min_ago = iso_format(before_now(minutes=1))
  109. self.event = self.store_event(
  110. data={
  111. "event_id": self.event_id,
  112. "timestamp": self.min_ago,
  113. "fingerprint": self.fingerprint,
  114. "user": {"email": self.user.email},
  115. },
  116. project_id=self.project.id,
  117. )
  118. self.url = reverse(
  119. "sentry-api-0-event-json",
  120. kwargs={
  121. "organization_slug": self.organization.slug,
  122. "project_slug": self.project.slug,
  123. "event_id": self.event_id,
  124. },
  125. )
  126. def assert_event(self, data):
  127. assert data["event_id"] == self.event_id
  128. assert data["user"]["email"] == self.user.email
  129. assert data["datetime"][:19] == self.min_ago
  130. assert data["fingerprint"] == self.fingerprint
  131. def test_simple(self):
  132. response = self.client.get(self.url, format="json")
  133. assert response.status_code == 200, response.content
  134. self.assert_event(response.data)
  135. def test_event_does_not_exist(self):
  136. self.url = reverse(
  137. "sentry-api-0-event-json",
  138. kwargs={
  139. "organization_slug": self.organization.slug,
  140. "project_slug": self.project.slug,
  141. "event_id": "no" * 16,
  142. },
  143. )
  144. response = self.client.get(self.url, format="json")
  145. assert response.status_code == 404, response.content
  146. assert response.data == {"detail": "Event not found"}
  147. def test_user_unauthorized(self):
  148. user = self.create_user()
  149. self.login_as(user)
  150. response = self.client.get(self.url, format="json")
  151. assert response.status_code == 403, response.content
  152. assert response.data == {"detail": "You do not have permission to perform this action."}
  153. def test_project_not_associated_with_event(self):
  154. project2 = self.create_project(organization=self.organization)
  155. url = reverse(
  156. "sentry-api-0-event-json",
  157. kwargs={
  158. "organization_slug": self.organization.slug,
  159. "project_slug": project2.slug,
  160. "event_id": self.event_id,
  161. },
  162. )
  163. response = self.client.get(url, format="json")
  164. assert response.status_code == 404, response.content
  165. assert response.data == {"detail": "Event not found"}