test_group_details.py 12 KB

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