test_group_details.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. from unittest import mock
  2. from rest_framework.exceptions import ErrorDetail
  3. from sentry.models import (
  4. GROUP_OWNER_TYPE,
  5. Environment,
  6. GroupInboxReason,
  7. GroupOwner,
  8. GroupOwnerType,
  9. Release,
  10. )
  11. from sentry.models.groupinbox import add_group_to_inbox, remove_group_from_inbox
  12. from sentry.testutils import APITestCase, SnubaTestCase
  13. from sentry.testutils.helpers import Feature
  14. from sentry.testutils.helpers.datetime import before_now, iso_format
  15. from sentry.testutils.silo import region_silo_test
  16. @region_silo_test(stable=True)
  17. class GroupDetailsTest(APITestCase, SnubaTestCase):
  18. def test_multiple_environments(self):
  19. group = self.create_group()
  20. self.login_as(user=self.user)
  21. environment = Environment.get_or_create(group.project, "production")
  22. environment2 = Environment.get_or_create(group.project, "staging")
  23. url = f"/api/0/issues/{group.id}/"
  24. from sentry.api.endpoints.group_details import tsdb
  25. with mock.patch(
  26. "sentry.api.endpoints.group_details.tsdb.get_range", side_effect=tsdb.get_range
  27. ) as get_range:
  28. response = self.client.get(
  29. f"{url}?environment=production&environment=staging", format="json"
  30. )
  31. assert response.status_code == 200
  32. assert get_range.call_count == 2
  33. for args, kwargs in get_range.call_args_list:
  34. assert kwargs["environment_ids"] == [environment.id, environment2.id]
  35. response = self.client.get(f"{url}?environment=invalid", format="json")
  36. assert response.status_code == 404
  37. def test_with_first_last_release(self):
  38. self.login_as(user=self.user)
  39. first_release = {
  40. "firstEvent": before_now(minutes=3),
  41. "lastEvent": before_now(minutes=2, seconds=30),
  42. }
  43. last_release = {
  44. "firstEvent": before_now(minutes=1, seconds=30),
  45. "lastEvent": before_now(minutes=1),
  46. }
  47. for timestamp in first_release.values():
  48. self.store_event(
  49. data={"release": "1.0", "timestamp": iso_format(timestamp)},
  50. project_id=self.project.id,
  51. )
  52. self.store_event(
  53. data={"release": "1.1", "timestamp": iso_format(before_now(minutes=2))},
  54. project_id=self.project.id,
  55. )
  56. event = None
  57. for timestamp in last_release.values():
  58. event = self.store_event(
  59. data={"release": "1.0a", "timestamp": iso_format(timestamp)},
  60. project_id=self.project.id,
  61. )
  62. group = event.group
  63. url = f"/api/0/issues/{group.id}/"
  64. response = self.client.get(url, format="json")
  65. assert response.status_code == 200, response.content
  66. assert response.data["id"] == str(group.id)
  67. release = response.data["firstRelease"]
  68. assert release["version"] == "1.0"
  69. for event, timestamp in first_release.items():
  70. assert release[event].ctime() == timestamp.ctime()
  71. release = response.data["lastRelease"]
  72. assert release["version"] == "1.0a"
  73. for event, timestamp in last_release.items():
  74. assert release[event].ctime() == timestamp.ctime()
  75. def test_first_last_only_one_tagstore(self):
  76. self.login_as(user=self.user)
  77. event = self.store_event(
  78. data={"release": "1.0", "timestamp": iso_format(before_now(days=3))},
  79. project_id=self.project.id,
  80. )
  81. self.store_event(
  82. data={"release": "1.1", "timestamp": iso_format(before_now(minutes=3))},
  83. project_id=self.project.id,
  84. )
  85. group = event.group
  86. url = f"/api/0/issues/{group.id}/"
  87. with mock.patch(
  88. "sentry.api.endpoints.group_details.tagstore.get_release_tags"
  89. ) as get_release_tags:
  90. response = self.client.get(url, format="json")
  91. assert response.status_code == 200
  92. assert get_release_tags.call_count == 1
  93. def test_first_release_only(self):
  94. self.login_as(user=self.user)
  95. first_event = before_now(days=3)
  96. self.store_event(
  97. data={"release": "1.0", "timestamp": iso_format(first_event)},
  98. project_id=self.project.id,
  99. )
  100. event = self.store_event(
  101. data={"release": "1.1", "timestamp": iso_format(before_now(days=1))},
  102. project_id=self.project.id,
  103. )
  104. # Forcibly remove one of the releases
  105. Release.objects.get(version="1.1").delete()
  106. group = event.group
  107. url = f"/api/0/issues/{group.id}/"
  108. response = self.client.get(url, format="json")
  109. assert response.status_code == 200, response.content
  110. assert response.data["firstRelease"]["version"] == "1.0"
  111. # only one event
  112. assert (
  113. response.data["firstRelease"]["firstEvent"]
  114. == response.data["firstRelease"]["lastEvent"]
  115. )
  116. assert response.data["firstRelease"]["firstEvent"].ctime() == first_event.ctime()
  117. assert response.data["lastRelease"] is None
  118. def test_group_expand_inbox(self):
  119. self.login_as(user=self.user)
  120. event = self.store_event(
  121. data={"timestamp": iso_format(before_now(minutes=3))},
  122. project_id=self.project.id,
  123. )
  124. group = event.group
  125. add_group_to_inbox(group, GroupInboxReason.NEW)
  126. url = f"/api/0/issues/{group.id}/?expand=inbox"
  127. response = self.client.get(url, format="json")
  128. assert response.status_code == 200, response.content
  129. assert response.data["inbox"] is not None
  130. assert response.data["inbox"]["reason"] == GroupInboxReason.NEW.value
  131. assert response.data["inbox"]["reason_details"] is None
  132. remove_group_from_inbox(event.group)
  133. response = self.client.get(url, format="json")
  134. assert response.status_code == 200, response.content
  135. assert response.data["inbox"] is None
  136. def test_group_expand_owners(self):
  137. self.login_as(user=self.user)
  138. event = self.store_event(
  139. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  140. project_id=self.project.id,
  141. )
  142. group = event.group
  143. url = f"/api/0/issues/{group.id}/?expand=owners"
  144. self.login_as(user=self.user)
  145. # Test with no owner
  146. response = self.client.get(url, format="json")
  147. assert response.status_code == 200
  148. assert response.data["owners"] is None
  149. # Test with owners
  150. GroupOwner.objects.create(
  151. group=event.group,
  152. project=event.project,
  153. organization=event.project.organization,
  154. type=GroupOwnerType.SUSPECT_COMMIT.value,
  155. user_id=self.user.id,
  156. )
  157. response = self.client.get(url, format="json")
  158. assert response.status_code == 200, response.content
  159. assert response.data["owners"] is not None
  160. assert len(response.data["owners"]) == 1
  161. assert response.data["owners"][0]["owner"] == f"user:{self.user.id}"
  162. assert response.data["owners"][0]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.SUSPECT_COMMIT]
  163. def test_group_expand_forecasts(self):
  164. with Feature("organizations:escalating-issues"):
  165. self.login_as(user=self.user)
  166. event = self.store_event(
  167. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  168. project_id=self.project.id,
  169. )
  170. group = event.group
  171. url = f"/api/0/issues/{group.id}/?expand=forecast"
  172. response = self.client.get(url, format="json")
  173. assert response.status_code == 200, response.content
  174. assert response.data["forecast"] is not None
  175. assert response.data["forecast"]["data"] is not None
  176. assert response.data["forecast"]["date_added"] is not None
  177. def test_assigned_to_unknown(self):
  178. self.login_as(user=self.user)
  179. event = self.store_event(
  180. data={"timestamp": iso_format(before_now(minutes=3))},
  181. project_id=self.project.id,
  182. )
  183. group = event.group
  184. url = f"/api/0/issues/{group.id}/"
  185. response = self.client.put(
  186. url, {"assignedTo": "admin@localhost", "status": "unresolved"}, format="json"
  187. )
  188. assert response.status_code == 200
  189. response = self.client.put(
  190. url, {"assignedTo": "user@doesnotexist.com", "status": "unresolved"}, format="json"
  191. )
  192. assert response.status_code == 400
  193. assert response.data == {
  194. "assignedTo": [
  195. ErrorDetail(
  196. string="Could not parse actor. Format should be `type:id` where type is `team` or `user`.",
  197. code="invalid",
  198. )
  199. ]
  200. }
  201. def test_collapse_stats_does_not_work(self):
  202. """
  203. 'collapse' param should hide the stats data and not return anything in the response, but the impl
  204. doesn't seem to respect this param.
  205. include this test here in-case the endpoint behavior changes in the future.
  206. """
  207. self.login_as(user=self.user)
  208. event = self.store_event(
  209. data={"timestamp": iso_format(before_now(minutes=3))},
  210. project_id=self.project.id,
  211. )
  212. group = event.group
  213. url = f"/api/0/issues/{group.id}/"
  214. response = self.client.get(url, {"collapse": ["stats"]}, format="json")
  215. assert response.status_code == 200
  216. assert int(response.data["id"]) == event.group.id
  217. assert response.data["stats"] # key shouldn't be present
  218. assert response.data["count"] is not None # key shouldn't be present
  219. assert response.data["userCount"] is not None # key shouldn't be present
  220. assert response.data["firstSeen"] is not None # key shouldn't be present
  221. assert response.data["lastSeen"] is not None # key shouldn't be present
  222. def test_issue_type_category(self):
  223. """Test that the issue's type and category is returned in the results"""
  224. self.login_as(user=self.user)
  225. event = self.store_event(
  226. data={"timestamp": iso_format(before_now(minutes=3))},
  227. project_id=self.project.id,
  228. )
  229. url = f"/api/0/issues/{event.group.id}/"
  230. response = self.client.get(url, format="json")
  231. assert response.status_code == 200
  232. assert int(response.data["id"]) == event.group.id
  233. assert response.data["issueType"] == "error"
  234. assert response.data["issueCategory"] == "error"