test_group_details.py 11 KB

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