test_project_group_index.py 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. import time
  2. from datetime import timedelta
  3. from functools import cached_property
  4. from unittest.mock import Mock, patch
  5. from urllib.parse import quote
  6. from uuid import uuid4
  7. from django.conf import settings
  8. from django.utils import timezone
  9. from sentry.issues.grouptype import PerformanceSlowDBQueryGroupType
  10. from sentry.models import (
  11. Activity,
  12. ApiToken,
  13. ExternalIssue,
  14. Group,
  15. GroupAssignee,
  16. GroupBookmark,
  17. GroupHash,
  18. GroupLink,
  19. GroupResolution,
  20. GroupSeen,
  21. GroupShare,
  22. GroupSnooze,
  23. GroupStatus,
  24. GroupSubscription,
  25. GroupTombstone,
  26. Integration,
  27. OrganizationIntegration,
  28. Release,
  29. UserOption,
  30. )
  31. from sentry.models.groupinbox import GroupInboxReason, add_group_to_inbox
  32. from sentry.testutils import APITestCase, SnubaTestCase
  33. from sentry.testutils.helpers import parse_link_header
  34. from sentry.testutils.helpers.datetime import before_now, iso_format
  35. from sentry.testutils.silo import region_silo_test
  36. from sentry.types.activity import ActivityType
  37. from sentry.utils import json
  38. @region_silo_test
  39. class GroupListTest(APITestCase, SnubaTestCase):
  40. def setUp(self):
  41. super().setUp()
  42. self.min_ago = before_now(minutes=1)
  43. def _parse_links(self, header):
  44. # links come in {url: {...attrs}}, but we need {rel: {...attrs}}
  45. links = {}
  46. for url, attrs in parse_link_header(header).items():
  47. links[attrs["rel"]] = attrs
  48. attrs["href"] = url
  49. return links
  50. @cached_property
  51. def path(self):
  52. return f"/api/0/projects/{self.project.organization.slug}/{self.project.slug}/issues/"
  53. def test_sort_by_date_with_tag(self):
  54. # XXX(dcramer): this tests a case where an ambiguous column name existed
  55. group1 = self.create_group(last_seen=before_now(seconds=1))
  56. self.login_as(user=self.user)
  57. response = self.client.get(f"{self.path}?sort_by=date&query=is:unresolved", format="json")
  58. assert response.status_code == 200
  59. assert len(response.data) == 1
  60. assert response.data[0]["id"] == str(group1.id)
  61. def test_invalid_query(self):
  62. self.create_group(last_seen=before_now(seconds=1))
  63. self.login_as(user=self.user)
  64. response = self.client.get(f"{self.path}?sort_by=date&query=timesSeen:>1t", format="json")
  65. assert response.status_code == 400
  66. assert "Error parsing search query" in response.data["detail"]
  67. def test_simple_pagination(self):
  68. event1 = self.store_event(
  69. data={
  70. "fingerprint": ["put-me-in-group-1"],
  71. "timestamp": iso_format(self.min_ago - timedelta(seconds=2)),
  72. },
  73. project_id=self.project.id,
  74. )
  75. event2 = self.store_event(
  76. data={
  77. "fingerprint": ["put-me-in-group-2"],
  78. "timestamp": iso_format(self.min_ago - timedelta(seconds=1)),
  79. },
  80. project_id=self.project.id,
  81. )
  82. self.login_as(user=self.user)
  83. response = self.client.get(f"{self.path}?sort_by=date&limit=1", format="json")
  84. assert response.status_code == 200
  85. assert len(response.data) == 1
  86. assert response.data[0]["id"] == str(event2.group.id)
  87. links = self._parse_links(response["Link"])
  88. assert links["previous"]["results"] == "false"
  89. assert links["next"]["results"] == "true"
  90. response = self.client.get(links["next"]["href"], format="json")
  91. assert response.status_code == 200
  92. assert len(response.data) == 1
  93. assert response.data[0]["id"] == str(event1.group.id)
  94. links = self._parse_links(response["Link"])
  95. assert links["previous"]["results"] == "true"
  96. assert links["next"]["results"] == "false"
  97. def test_stats_period(self):
  98. # TODO(dcramer): this test really only checks if validation happens
  99. # on statsPeriod
  100. self.create_group(last_seen=before_now(seconds=1))
  101. self.create_group(last_seen=timezone.now())
  102. self.login_as(user=self.user)
  103. response = self.client.get(f"{self.path}?statsPeriod=24h", format="json")
  104. assert response.status_code == 200
  105. response = self.client.get(f"{self.path}?statsPeriod=14d", format="json")
  106. assert response.status_code == 200
  107. response = self.client.get(f"{self.path}?statsPeriod=", format="json")
  108. assert response.status_code == 200
  109. time.sleep(1)
  110. response = self.client.get(f"{self.path}?statsPeriod=48h", format="json")
  111. assert response.status_code == 400
  112. def test_environment(self):
  113. self.store_event(
  114. data={
  115. "fingerprint": ["put-me-in-group1"],
  116. "timestamp": iso_format(self.min_ago),
  117. "environment": "production",
  118. },
  119. project_id=self.project.id,
  120. )
  121. self.store_event(
  122. data={
  123. "fingerprint": ["put-me-in-group2"],
  124. "timestamp": iso_format(self.min_ago),
  125. "environment": "staging",
  126. },
  127. project_id=self.project.id,
  128. )
  129. self.login_as(user=self.user)
  130. response = self.client.get(self.path + "?environment=production", format="json")
  131. assert response.status_code == 200
  132. assert len(response.data) == 1
  133. response = self.client.get(self.path + "?environment=garbage", format="json")
  134. assert response.status_code == 200
  135. assert len(response.data) == 0
  136. def test_auto_resolved(self):
  137. project = self.project
  138. project.update_option("sentry:resolve_age", 1)
  139. self.create_group(last_seen=before_now(days=1))
  140. group2 = self.create_group(last_seen=timezone.now())
  141. self.login_as(user=self.user)
  142. response = self.client.get(self.path, format="json")
  143. assert response.status_code == 200
  144. assert len(response.data) == 1
  145. assert response.data[0]["id"] == str(group2.id)
  146. def test_lookup_by_event_id(self):
  147. project = self.project
  148. project.update_option("sentry:resolve_age", 1)
  149. event_id = "c" * 32
  150. event = self.store_event(
  151. data={"event_id": event_id, "timestamp": iso_format(self.min_ago)},
  152. project_id=self.project.id,
  153. )
  154. self.login_as(user=self.user)
  155. response = self.client.get("{}?query={}".format(self.path, "c" * 32), format="json")
  156. assert response.status_code == 200
  157. assert len(response.data) == 1
  158. assert response.data[0]["id"] == str(event.group.id)
  159. assert response.data[0]["matchingEventId"] == event.event_id
  160. def test_lookup_by_event_with_matching_environment(self):
  161. project = self.project
  162. project.update_option("sentry:resolve_age", 1)
  163. self.create_environment(name="test", project=project)
  164. event = self.store_event(
  165. data={"environment": "test", "timestamp": iso_format(self.min_ago)},
  166. project_id=self.project.id,
  167. )
  168. self.login_as(user=self.user)
  169. response = self.client.get(
  170. f"{self.path}?query={event.event_id}&environment=test", format="json"
  171. )
  172. assert response.status_code == 200
  173. assert len(response.data) == 1
  174. assert response.data[0]["id"] == str(event.group.id)
  175. assert response.data[0]["matchingEventId"] == event.event_id
  176. assert response.data[0]["matchingEventEnvironment"] == "test"
  177. def test_lookup_by_event_id_with_whitespace(self):
  178. project = self.project
  179. project.update_option("sentry:resolve_age", 1)
  180. event = self.store_event(
  181. data={"event_id": "c" * 32, "timestamp": iso_format(self.min_ago)},
  182. project_id=self.project.id,
  183. )
  184. self.login_as(user=self.user)
  185. response = self.client.get(
  186. "{}?query=%20%20{}%20%20".format(self.path, "c" * 32), format="json"
  187. )
  188. assert response.status_code == 200
  189. assert len(response.data) == 1
  190. assert response.data[0]["id"] == str(event.group.id)
  191. def test_lookup_by_unknown_event_id(self):
  192. project = self.project
  193. project.update_option("sentry:resolve_age", 1)
  194. self.create_group()
  195. self.create_group()
  196. self.login_as(user=self.user)
  197. response = self.client.get("{}?query={}".format(self.path, "c" * 32), format="json")
  198. assert response.status_code == 200
  199. assert len(response.data) == 0
  200. def test_lookup_by_short_id(self):
  201. group = self.group
  202. short_id = group.qualified_short_id
  203. self.login_as(user=self.user)
  204. response = self.client.get(f"{self.path}?query={short_id}&shortIdLookup=1", format="json")
  205. assert response.status_code == 200
  206. assert len(response.data) == 1
  207. def test_lookup_by_short_id_no_perms(self):
  208. organization = self.create_organization()
  209. project = self.create_project(organization=organization)
  210. project2 = self.create_project(organization=organization)
  211. team = self.create_team(organization=organization)
  212. project2.add_team(team)
  213. group = self.create_group(project=project)
  214. user = self.create_user()
  215. self.create_member(organization=organization, user=user, teams=[team])
  216. short_id = group.qualified_short_id
  217. self.login_as(user=user)
  218. path = f"/api/0/projects/{organization.slug}/{project2.slug}/issues/"
  219. response = self.client.get(f"{path}?query={short_id}&shortIdLookup=1", format="json")
  220. assert response.status_code == 200
  221. assert len(response.data) == 0
  222. def test_lookup_by_first_release(self):
  223. self.login_as(self.user)
  224. project = self.project
  225. project2 = self.create_project(name="baz", organization=project.organization)
  226. release = Release.objects.create(organization=project.organization, version="12345")
  227. release.add_project(project)
  228. release.add_project(project2)
  229. group = self.store_event(
  230. data={"release": release.version, "timestamp": iso_format(before_now(seconds=1))},
  231. project_id=project.id,
  232. ).group
  233. self.store_event(
  234. data={"release": release.version, "timestamp": iso_format(before_now(seconds=1))},
  235. project_id=project2.id,
  236. )
  237. url = "{}?query={}".format(self.path, 'first-release:"%s"' % release.version)
  238. response = self.client.get(url, format="json")
  239. issues = json.loads(response.content)
  240. assert response.status_code == 200
  241. assert len(issues) == 1
  242. assert int(issues[0]["id"]) == group.id
  243. def test_lookup_by_release(self):
  244. self.login_as(self.user)
  245. version = "12345"
  246. event = self.store_event(
  247. data={"tags": {"sentry:release": version}}, project_id=self.project.id
  248. )
  249. group = event.group
  250. url = "{}?query={}".format(self.path, quote('release:"%s"' % version))
  251. response = self.client.get(url, format="json")
  252. issues = json.loads(response.content)
  253. assert response.status_code == 200
  254. assert len(issues) == 1
  255. assert int(issues[0]["id"]) == group.id
  256. def test_lookup_by_release_wildcard(self):
  257. self.login_as(self.user)
  258. version = "12345"
  259. event = self.store_event(
  260. data={"tags": {"sentry:release": version}}, project_id=self.project.id
  261. )
  262. group = event.group
  263. release_wildcard = version[:3] + "*"
  264. url = "{}?query={}".format(self.path, quote('release:"%s"' % release_wildcard))
  265. response = self.client.get(url, format="json")
  266. issues = json.loads(response.content)
  267. assert response.status_code == 200
  268. assert len(issues) == 1
  269. assert int(issues[0]["id"]) == group.id
  270. def test_pending_delete_pending_merge_excluded(self):
  271. self.create_group(status=GroupStatus.PENDING_DELETION)
  272. group = self.create_group()
  273. self.create_group(status=GroupStatus.DELETION_IN_PROGRESS)
  274. self.create_group(status=GroupStatus.PENDING_MERGE)
  275. self.login_as(user=self.user)
  276. response = self.client.get(self.path, format="json")
  277. assert len(response.data) == 1
  278. assert response.data[0]["id"] == str(group.id)
  279. def test_filters_based_on_retention(self):
  280. self.login_as(user=self.user)
  281. self.create_group(last_seen=timezone.now() - timedelta(days=2))
  282. with self.options({"system.event-retention-days": 1}):
  283. response = self.client.get(self.path)
  284. assert response.status_code == 200, response.content
  285. assert len(response.data) == 0
  286. def test_token_auth(self):
  287. token = ApiToken.objects.create(user=self.user, scopes=256)
  288. response = self.client.get(
  289. self.path, format="json", HTTP_AUTHORIZATION=f"Bearer {token.token}"
  290. )
  291. assert response.status_code == 200, response.content
  292. def test_filter_not_unresolved(self):
  293. event = self.store_event(
  294. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  295. project_id=self.project.id,
  296. )
  297. event.group.update(status=GroupStatus.RESOLVED, substatus=None)
  298. self.login_as(user=self.user)
  299. response = self.client.get(f"{self.path}?query=!is:unresolved", format="json")
  300. assert response.status_code == 200
  301. assert [int(r["id"]) for r in response.data] == [event.group.id]
  302. @region_silo_test
  303. class GroupUpdateTest(APITestCase, SnubaTestCase):
  304. def setUp(self):
  305. super().setUp()
  306. self.min_ago = timezone.now() - timedelta(minutes=1)
  307. @cached_property
  308. def path(self):
  309. return f"/api/0/projects/{self.project.organization.slug}/{self.project.slug}/issues/"
  310. def assertNoResolution(self, group):
  311. assert not GroupResolution.objects.filter(group=group).exists()
  312. def test_global_resolve(self):
  313. group1 = self.create_group(status=GroupStatus.RESOLVED)
  314. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  315. group3 = self.create_group(status=GroupStatus.IGNORED)
  316. group4 = self.create_group(
  317. project=self.create_project(slug="foo"),
  318. status=GroupStatus.UNRESOLVED,
  319. )
  320. self.login_as(user=self.user)
  321. response = self.client.put(
  322. f"{self.path}?status=unresolved", data={"status": "resolved"}, format="json"
  323. )
  324. assert response.status_code == 200, response.data
  325. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  326. # the previously resolved entry should not be included
  327. new_group1 = Group.objects.get(id=group1.id)
  328. assert new_group1.status == GroupStatus.RESOLVED
  329. assert new_group1.resolved_at is None
  330. # this wont exist because it wasn't affected
  331. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group1).exists()
  332. new_group2 = Group.objects.get(id=group2.id)
  333. assert new_group2.status == GroupStatus.RESOLVED
  334. assert new_group2.resolved_at is not None
  335. assert GroupSubscription.objects.filter(
  336. user_id=self.user.id, group=new_group2, is_active=True
  337. ).exists()
  338. # the ignored entry should not be included
  339. new_group3 = Group.objects.get(id=group3.id)
  340. assert new_group3.status == GroupStatus.IGNORED
  341. assert new_group3.resolved_at is None
  342. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group3)
  343. new_group4 = Group.objects.get(id=group4.id)
  344. assert new_group4.status == GroupStatus.UNRESOLVED
  345. assert new_group4.resolved_at is None
  346. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group4)
  347. def test_bulk_resolve(self):
  348. self.login_as(user=self.user)
  349. for i in range(200):
  350. self.create_group(status=GroupStatus.UNRESOLVED)
  351. response = self.client.get(f"{self.path}?sort_by=date&query=is:unresolved", format="json")
  352. assert len(response.data) == 100
  353. response = self.client.put(
  354. f"{self.path}?status=unresolved", data={"status": "resolved"}, format="json"
  355. )
  356. assert response.status_code == 200, response.data
  357. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  358. response = self.client.get(f"{self.path}?sort_by=date&query=is:unresolved", format="json")
  359. assert len(response.data) == 0
  360. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  361. def test_resolve_with_integration(self, mock_sync_status_outbound):
  362. self.login_as(user=self.user)
  363. org = self.organization
  364. integration = Integration.objects.create(provider="example", name="Example")
  365. integration.add_organization(org, self.user)
  366. group = self.create_group(status=GroupStatus.UNRESOLVED)
  367. OrganizationIntegration.objects.filter(
  368. integration_id=integration.id, organization_id=group.organization.id
  369. ).update(
  370. config={
  371. "sync_comments": True,
  372. "sync_status_outbound": True,
  373. "sync_status_inbound": True,
  374. "sync_assignee_outbound": True,
  375. "sync_assignee_inbound": True,
  376. }
  377. )
  378. external_issue = ExternalIssue.objects.get_or_create(
  379. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  380. )[0]
  381. GroupLink.objects.get_or_create(
  382. group_id=group.id,
  383. project_id=group.project_id,
  384. linked_type=GroupLink.LinkedType.issue,
  385. linked_id=external_issue.id,
  386. relationship=GroupLink.Relationship.references,
  387. )[0]
  388. response = self.client.get(f"{self.path}?sort_by=date&query=is:unresolved", format="json")
  389. assert len(response.data) == 1
  390. with self.tasks():
  391. with self.feature({"organizations:integrations-issue-sync": True}):
  392. response = self.client.put(
  393. f"{self.path}?status=unresolved",
  394. data={"status": "resolved"},
  395. format="json",
  396. )
  397. assert response.status_code == 200, response.data
  398. group = Group.objects.get(id=group.id)
  399. assert group.status == GroupStatus.RESOLVED
  400. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  401. mock_sync_status_outbound.assert_called_once_with(
  402. external_issue, True, group.project_id
  403. )
  404. response = self.client.get(f"{self.path}?sort_by=date&query=is:unresolved", format="json")
  405. assert len(response.data) == 0
  406. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  407. def test_set_unresolved_with_integration(self, mock_sync_status_outbound):
  408. release = self.create_release(project=self.project, version="abc")
  409. group = self.create_group(status=GroupStatus.RESOLVED)
  410. org = self.organization
  411. integration = Integration.objects.create(provider="example", name="Example")
  412. integration.add_organization(org, self.user)
  413. OrganizationIntegration.objects.filter(
  414. integration_id=integration.id, organization_id=group.organization.id
  415. ).update(
  416. config={
  417. "sync_comments": True,
  418. "sync_status_outbound": True,
  419. "sync_status_inbound": True,
  420. "sync_assignee_outbound": True,
  421. "sync_assignee_inbound": True,
  422. }
  423. )
  424. GroupResolution.objects.create(group=group, release=release)
  425. external_issue = ExternalIssue.objects.get_or_create(
  426. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  427. )[0]
  428. GroupLink.objects.get_or_create(
  429. group_id=group.id,
  430. project_id=group.project_id,
  431. linked_type=GroupLink.LinkedType.issue,
  432. linked_id=external_issue.id,
  433. relationship=GroupLink.Relationship.references,
  434. )[0]
  435. self.login_as(user=self.user)
  436. url = f"{self.path}?id={group.id}"
  437. with self.tasks():
  438. with self.feature({"organizations:integrations-issue-sync": True}):
  439. response = self.client.put(url, data={"status": "unresolved"}, format="json")
  440. assert response.status_code == 200
  441. assert response.data == {"status": "unresolved", "statusDetails": {}}
  442. group = Group.objects.get(id=group.id)
  443. assert group.status == GroupStatus.UNRESOLVED
  444. self.assertNoResolution(group)
  445. assert GroupSubscription.objects.filter(
  446. user_id=self.user.id, group=group, is_active=True
  447. ).exists()
  448. mock_sync_status_outbound.assert_called_once_with(
  449. external_issue, False, group.project_id
  450. )
  451. def test_self_assign_issue(self):
  452. group = self.create_group(status=GroupStatus.UNRESOLVED)
  453. user = self.user
  454. uo1 = UserOption.objects.create(
  455. key="self_assign_issue", value="1", project_id=None, user=user
  456. )
  457. self.login_as(user=user)
  458. url = f"{self.path}?id={group.id}"
  459. response = self.client.put(url, data={"status": "resolved"}, format="json")
  460. assert response.status_code == 200, response.data
  461. assert response.data["assignedTo"]["id"] == str(user.id)
  462. assert response.data["assignedTo"]["type"] == "user"
  463. assert response.data["status"] == "resolved"
  464. assert GroupAssignee.objects.filter(group=group, user_id=user.id).exists()
  465. assert GroupSubscription.objects.filter(
  466. user_id=user.id, group=group, is_active=True
  467. ).exists()
  468. uo1.delete()
  469. def test_self_assign_issue_next_release(self):
  470. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  471. release.add_project(self.project)
  472. group = self.create_group(status=GroupStatus.UNRESOLVED)
  473. uo1 = UserOption.objects.create(
  474. key="self_assign_issue", value="1", project_id=None, user=self.user
  475. )
  476. self.login_as(user=self.user)
  477. url = f"{self.path}?id={group.id}"
  478. response = self.client.put(url, data={"status": "resolvedInNextRelease"}, format="json")
  479. assert response.status_code == 200
  480. assert response.data["status"] == "resolved"
  481. assert response.data["statusDetails"]["inNextRelease"]
  482. assert response.data["assignedTo"]["id"] == str(self.user.id)
  483. assert response.data["assignedTo"]["type"] == "user"
  484. group = Group.objects.get(id=group.id)
  485. assert group.status == GroupStatus.RESOLVED
  486. assert GroupResolution.objects.filter(group=group, release=release).exists()
  487. assert GroupSubscription.objects.filter(
  488. user_id=self.user.id, group=group, is_active=True
  489. ).exists()
  490. activity = Activity.objects.get(
  491. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  492. )
  493. assert activity.data["version"] == ""
  494. uo1.delete()
  495. def test_selective_status_update(self):
  496. group1 = self.create_group(status=GroupStatus.RESOLVED)
  497. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  498. group3 = self.create_group(status=GroupStatus.IGNORED)
  499. group4 = self.create_group(
  500. project=self.create_project(slug="foo"),
  501. status=GroupStatus.UNRESOLVED,
  502. )
  503. self.login_as(user=self.user)
  504. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  505. response = self.client.put(url, data={"status": "resolved"}, format="json")
  506. assert response.status_code == 200
  507. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  508. new_group1 = Group.objects.get(id=group1.id)
  509. assert new_group1.resolved_at is not None
  510. assert new_group1.status == GroupStatus.RESOLVED
  511. new_group2 = Group.objects.get(id=group2.id)
  512. assert new_group2.resolved_at is not None
  513. assert new_group2.status == GroupStatus.RESOLVED
  514. assert GroupSubscription.objects.filter(
  515. user_id=self.user.id, group=new_group2, is_active=True
  516. ).exists()
  517. new_group3 = Group.objects.get(id=group3.id)
  518. assert new_group3.resolved_at is None
  519. assert new_group3.status == GroupStatus.IGNORED
  520. new_group4 = Group.objects.get(id=group4.id)
  521. assert new_group4.resolved_at is None
  522. assert new_group4.status == GroupStatus.UNRESOLVED
  523. def test_set_resolved_in_current_release(self):
  524. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  525. release.add_project(self.project)
  526. group = self.create_group(status=GroupStatus.UNRESOLVED)
  527. self.login_as(user=self.user)
  528. url = f"{self.path}?id={group.id}"
  529. response = self.client.put(
  530. url,
  531. data={"status": "resolved", "statusDetails": {"inRelease": "latest"}},
  532. format="json",
  533. )
  534. assert response.status_code == 200
  535. assert response.data["status"] == "resolved"
  536. assert response.data["statusDetails"]["inRelease"] == release.version
  537. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  538. assert "activity" in response.data
  539. group = Group.objects.get(id=group.id)
  540. assert group.status == GroupStatus.RESOLVED
  541. resolution = GroupResolution.objects.get(group=group)
  542. assert resolution.release == release
  543. assert resolution.type == GroupResolution.Type.in_release
  544. assert resolution.status == GroupResolution.Status.resolved
  545. assert resolution.actor_id == self.user.id
  546. assert GroupSubscription.objects.filter(
  547. user_id=self.user.id, group=group, is_active=True
  548. ).exists()
  549. activity = Activity.objects.get(
  550. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  551. )
  552. assert activity.data["version"] == release.version
  553. def test_set_resolved_in_explicit_release(self):
  554. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  555. release.add_project(self.project)
  556. release2 = Release.objects.create(organization_id=self.project.organization_id, version="b")
  557. release2.add_project(self.project)
  558. group = self.create_group(status=GroupStatus.UNRESOLVED)
  559. self.login_as(user=self.user)
  560. url = f"{self.path}?id={group.id}"
  561. response = self.client.put(
  562. url,
  563. data={"status": "resolved", "statusDetails": {"inRelease": release.version}},
  564. format="json",
  565. )
  566. assert response.status_code == 200
  567. assert response.data["status"] == "resolved"
  568. assert response.data["statusDetails"]["inRelease"] == release.version
  569. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  570. assert "activity" in response.data
  571. group = Group.objects.get(id=group.id)
  572. assert group.status == GroupStatus.RESOLVED
  573. resolution = GroupResolution.objects.get(group=group)
  574. assert resolution.release == release
  575. assert resolution.type == GroupResolution.Type.in_release
  576. assert resolution.status == GroupResolution.Status.resolved
  577. assert resolution.actor_id == self.user.id
  578. assert GroupSubscription.objects.filter(
  579. user_id=self.user.id, group=group, is_active=True
  580. ).exists()
  581. activity = Activity.objects.get(
  582. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  583. )
  584. assert activity.data["version"] == release.version
  585. def test_set_resolved_in_next_release(self):
  586. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  587. release.add_project(self.project)
  588. group = self.create_group(status=GroupStatus.UNRESOLVED)
  589. self.login_as(user=self.user)
  590. url = f"{self.path}?id={group.id}"
  591. response = self.client.put(
  592. url,
  593. data={"status": "resolved", "statusDetails": {"inNextRelease": True}},
  594. format="json",
  595. )
  596. assert response.status_code == 200
  597. assert response.data["status"] == "resolved"
  598. assert response.data["statusDetails"]["inNextRelease"]
  599. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  600. assert "activity" in response.data
  601. group = Group.objects.get(id=group.id)
  602. assert group.status == GroupStatus.RESOLVED
  603. resolution = GroupResolution.objects.get(group=group)
  604. assert resolution.release == release
  605. assert resolution.type == GroupResolution.Type.in_next_release
  606. assert resolution.status == GroupResolution.Status.pending
  607. assert resolution.actor_id == self.user.id
  608. assert GroupSubscription.objects.filter(
  609. user_id=self.user.id, group=group, is_active=True
  610. ).exists()
  611. activity = Activity.objects.get(
  612. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  613. )
  614. assert activity.data["version"] == ""
  615. def test_set_resolved_in_next_release_legacy(self):
  616. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  617. release.add_project(self.project)
  618. group = self.create_group(status=GroupStatus.UNRESOLVED)
  619. self.login_as(user=self.user)
  620. url = f"{self.path}?id={group.id}"
  621. response = self.client.put(url, data={"status": "resolvedInNextRelease"}, format="json")
  622. assert response.status_code == 200
  623. assert response.data["status"] == "resolved"
  624. assert response.data["statusDetails"]["inNextRelease"]
  625. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  626. group = Group.objects.get(id=group.id)
  627. assert group.status == GroupStatus.RESOLVED
  628. resolution = GroupResolution.objects.get(group=group)
  629. assert resolution.release == release
  630. assert resolution.type == GroupResolution.Type.in_next_release
  631. assert resolution.status == GroupResolution.Status.pending
  632. assert resolution.actor_id == self.user.id
  633. assert GroupSubscription.objects.filter(
  634. user_id=self.user.id, group=group, is_active=True
  635. ).exists()
  636. activity = Activity.objects.get(
  637. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  638. )
  639. assert activity.data["version"] == ""
  640. def test_set_resolved_in_explicit_commit_unreleased(self):
  641. repo = self.create_repo(project=self.project, name=self.project.name)
  642. commit = self.create_commit(project=self.project, repo=repo)
  643. group = self.create_group(status=GroupStatus.UNRESOLVED)
  644. self.login_as(user=self.user)
  645. url = f"{self.path}?id={group.id}"
  646. response = self.client.put(
  647. url,
  648. data={
  649. "status": "resolved",
  650. "statusDetails": {"inCommit": {"commit": commit.key, "repository": repo.name}},
  651. },
  652. format="json",
  653. )
  654. assert response.status_code == 200
  655. assert response.data["status"] == "resolved"
  656. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  657. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  658. group = Group.objects.get(id=group.id)
  659. assert group.status == GroupStatus.RESOLVED
  660. link = GroupLink.objects.get(group_id=group.id)
  661. assert link.linked_type == GroupLink.LinkedType.commit
  662. assert link.relationship == GroupLink.Relationship.resolves
  663. assert link.linked_id == commit.id
  664. assert GroupSubscription.objects.filter(
  665. user_id=self.user.id, group=group, is_active=True
  666. ).exists()
  667. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  668. assert activity.data["commit"] == commit.id
  669. def test_set_resolved_in_explicit_commit_released(self):
  670. release = self.create_release(project=self.project)
  671. repo = self.create_repo(project=self.project, name=self.project.name)
  672. commit = self.create_commit(project=self.project, repo=repo, release=release)
  673. group = self.create_group(status=GroupStatus.UNRESOLVED)
  674. self.login_as(user=self.user)
  675. url = f"{self.path}?id={group.id}"
  676. response = self.client.put(
  677. url,
  678. data={
  679. "status": "resolved",
  680. "statusDetails": {"inCommit": {"commit": commit.key, "repository": repo.name}},
  681. },
  682. format="json",
  683. )
  684. assert response.status_code == 200
  685. assert response.data["status"] == "resolved"
  686. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  687. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  688. group = Group.objects.get(id=group.id)
  689. assert group.status == GroupStatus.RESOLVED
  690. link = GroupLink.objects.get(group_id=group.id)
  691. assert link.project_id == self.project.id
  692. assert link.linked_type == GroupLink.LinkedType.commit
  693. assert link.relationship == GroupLink.Relationship.resolves
  694. assert link.linked_id == commit.id
  695. assert GroupSubscription.objects.filter(
  696. user_id=self.user.id, group=group, is_active=True
  697. ).exists()
  698. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  699. assert activity.data["commit"] == commit.id
  700. resolution = GroupResolution.objects.get(group=group)
  701. assert resolution.type == GroupResolution.Type.in_release
  702. assert resolution.status == GroupResolution.Status.resolved
  703. def test_set_resolved_in_explicit_commit_missing(self):
  704. repo = self.create_repo(project=self.project, name=self.project.name)
  705. group = self.create_group(status=GroupStatus.UNRESOLVED)
  706. self.login_as(user=self.user)
  707. url = f"{self.path}?id={group.id}"
  708. response = self.client.put(
  709. url,
  710. data={
  711. "status": "resolved",
  712. "statusDetails": {"inCommit": {"commit": "a" * 40, "repository": repo.name}},
  713. },
  714. format="json",
  715. )
  716. assert response.status_code == 400
  717. assert (
  718. response.data["statusDetails"]["inCommit"]["commit"][0]
  719. == "Unable to find the given commit."
  720. )
  721. def test_set_unresolved(self):
  722. release = self.create_release(project=self.project, version="abc")
  723. group = self.create_group(status=GroupStatus.RESOLVED)
  724. GroupResolution.objects.create(group=group, release=release)
  725. self.login_as(user=self.user)
  726. url = f"{self.path}?id={group.id}"
  727. response = self.client.put(url, data={"status": "unresolved"}, format="json")
  728. assert response.status_code == 200
  729. assert response.data == {"status": "unresolved", "statusDetails": {}}
  730. group = Group.objects.get(id=group.id)
  731. assert group.status == GroupStatus.UNRESOLVED
  732. self.assertNoResolution(group)
  733. assert GroupSubscription.objects.filter(
  734. user_id=self.user.id, group=group, is_active=True
  735. ).exists()
  736. def test_set_unresolved_on_snooze(self):
  737. group = self.create_group(status=GroupStatus.IGNORED)
  738. GroupSnooze.objects.create(group=group, until=timezone.now() - timedelta(days=1))
  739. self.login_as(user=self.user)
  740. url = f"{self.path}?id={group.id}"
  741. response = self.client.put(url, data={"status": "unresolved"}, format="json")
  742. assert response.status_code == 200
  743. assert response.data == {"status": "unresolved", "statusDetails": {}}
  744. group = Group.objects.get(id=group.id)
  745. assert group.status == GroupStatus.UNRESOLVED
  746. def test_basic_ignore(self):
  747. group = self.create_group(status=GroupStatus.RESOLVED)
  748. snooze = GroupSnooze.objects.create(group=group, until=timezone.now())
  749. self.login_as(user=self.user)
  750. url = f"{self.path}?id={group.id}"
  751. response = self.client.put(url, data={"status": "ignored"}, format="json")
  752. assert response.status_code == 200
  753. # existing snooze objects should be cleaned up
  754. assert not GroupSnooze.objects.filter(id=snooze.id).exists()
  755. group = Group.objects.get(id=group.id)
  756. assert group.status == GroupStatus.IGNORED
  757. assert response.data == {"status": "ignored", "statusDetails": {}, "inbox": None}
  758. def test_snooze_duration(self):
  759. group = self.create_group(status=GroupStatus.RESOLVED)
  760. self.login_as(user=self.user)
  761. url = f"{self.path}?id={group.id}"
  762. response = self.client.put(
  763. url, data={"status": "ignored", "ignoreDuration": 30}, format="json"
  764. )
  765. assert response.status_code == 200
  766. snooze = GroupSnooze.objects.get(group=group)
  767. now = timezone.now()
  768. assert snooze.count is None
  769. assert snooze.until > now + timedelta(minutes=29)
  770. assert snooze.until < now + timedelta(minutes=31)
  771. assert snooze.user_count is None
  772. assert snooze.user_window is None
  773. assert snooze.window is None
  774. assert response.data["status"] == "ignored"
  775. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  776. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  777. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  778. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  779. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  780. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  781. def test_snooze_count(self):
  782. group = self.create_group(status=GroupStatus.RESOLVED, times_seen=1)
  783. self.login_as(user=self.user)
  784. url = f"{self.path}?id={group.id}"
  785. response = self.client.put(
  786. url, data={"status": "ignored", "ignoreCount": 100}, format="json"
  787. )
  788. assert response.status_code == 200
  789. snooze = GroupSnooze.objects.get(group=group)
  790. assert snooze.count == 100
  791. assert snooze.until is None
  792. assert snooze.user_count is None
  793. assert snooze.user_window is None
  794. assert snooze.window is None
  795. assert snooze.state["times_seen"] == 1
  796. assert response.data["status"] == "ignored"
  797. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  798. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  799. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  800. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  801. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  802. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  803. def test_snooze_user_count(self):
  804. event = {}
  805. for i in range(10):
  806. event = self.store_event(
  807. data={
  808. "fingerprint": ["put-me-in-group-1"],
  809. "user": {"id": str(i)},
  810. "timestamp": iso_format(self.min_ago + timedelta(seconds=i)),
  811. },
  812. project_id=self.project.id,
  813. )
  814. group = Group.objects.get(id=event.group.id)
  815. group.status = GroupStatus.RESOLVED
  816. group.substatus = None
  817. group.save()
  818. self.login_as(user=self.user)
  819. url = f"{self.path}?id={group.id}"
  820. response = self.client.put(
  821. url, data={"status": "ignored", "ignoreUserCount": 10}, format="json"
  822. )
  823. assert response.status_code == 200
  824. snooze = GroupSnooze.objects.get(group=group)
  825. assert snooze.count is None
  826. assert snooze.until is None
  827. assert snooze.user_count == 10
  828. assert snooze.user_window is None
  829. assert snooze.window is None
  830. assert snooze.state["users_seen"] == 10
  831. assert response.data["status"] == "ignored"
  832. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  833. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  834. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  835. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  836. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  837. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  838. def test_set_bookmarked(self):
  839. group1 = self.create_group(status=GroupStatus.RESOLVED)
  840. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  841. group3 = self.create_group(status=GroupStatus.IGNORED)
  842. group4 = self.create_group(
  843. project=self.create_project(slug="foo"),
  844. status=GroupStatus.UNRESOLVED,
  845. )
  846. self.login_as(user=self.user)
  847. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  848. response = self.client.put(url, data={"isBookmarked": "true"}, format="json")
  849. assert response.status_code == 200
  850. assert response.data == {"isBookmarked": True}
  851. bookmark1 = GroupBookmark.objects.filter(group=group1, user_id=self.user.id)
  852. assert bookmark1.exists()
  853. assert GroupSubscription.objects.filter(
  854. user_id=self.user.id, group=group1, is_active=True
  855. ).exists()
  856. bookmark2 = GroupBookmark.objects.filter(group=group2, user_id=self.user.id)
  857. assert bookmark2.exists()
  858. assert GroupSubscription.objects.filter(
  859. user_id=self.user.id, group=group2, is_active=True
  860. ).exists()
  861. bookmark3 = GroupBookmark.objects.filter(group=group3, user_id=self.user.id)
  862. assert not bookmark3.exists()
  863. bookmark4 = GroupBookmark.objects.filter(group=group4, user_id=self.user.id)
  864. assert not bookmark4.exists()
  865. def test_subscription(self):
  866. group1 = self.create_group()
  867. group2 = self.create_group()
  868. group3 = self.create_group()
  869. group4 = self.create_group(project=self.create_project(slug="foo"))
  870. self.login_as(user=self.user)
  871. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  872. response = self.client.put(url, data={"isSubscribed": "true"}, format="json")
  873. assert response.status_code == 200
  874. assert response.data == {"isSubscribed": True, "subscriptionDetails": {"reason": "unknown"}}
  875. assert GroupSubscription.objects.filter(
  876. group=group1, user_id=self.user.id, is_active=True
  877. ).exists()
  878. assert GroupSubscription.objects.filter(
  879. group=group2, user_id=self.user.id, is_active=True
  880. ).exists()
  881. assert not GroupSubscription.objects.filter(group=group3, user_id=self.user.id).exists()
  882. assert not GroupSubscription.objects.filter(group=group4, user_id=self.user.id).exists()
  883. def test_set_public(self):
  884. group1 = self.create_group()
  885. group2 = self.create_group()
  886. self.login_as(user=self.user)
  887. url = f"{self.path}?id={group1.id}&id={group2.id}"
  888. response = self.client.put(url, data={"isPublic": "true"}, format="json")
  889. assert response.status_code == 200
  890. assert response.data["isPublic"] is True
  891. assert "shareId" in response.data
  892. new_group1 = Group.objects.get(id=group1.id)
  893. assert bool(new_group1.get_share_id())
  894. new_group2 = Group.objects.get(id=group2.id)
  895. assert bool(new_group2.get_share_id())
  896. def test_set_private(self):
  897. group1 = self.create_group()
  898. group2 = self.create_group()
  899. # Manually mark them as shared
  900. for g in group1, group2:
  901. GroupShare.objects.create(project_id=g.project_id, group=g)
  902. assert bool(g.get_share_id())
  903. self.login_as(user=self.user)
  904. url = f"{self.path}?id={group1.id}&id={group2.id}"
  905. response = self.client.put(url, data={"isPublic": "false"}, format="json")
  906. assert response.status_code == 200
  907. assert response.data == {"isPublic": False, "shareId": None}
  908. new_group1 = Group.objects.get(id=group1.id)
  909. assert not bool(new_group1.get_share_id())
  910. new_group2 = Group.objects.get(id=group2.id)
  911. assert not bool(new_group2.get_share_id())
  912. def test_set_has_seen(self):
  913. group1 = self.create_group(status=GroupStatus.RESOLVED)
  914. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  915. group3 = self.create_group(status=GroupStatus.IGNORED)
  916. group4 = self.create_group(
  917. project=self.create_project(slug="foo"),
  918. status=GroupStatus.UNRESOLVED,
  919. )
  920. self.login_as(user=self.user)
  921. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  922. response = self.client.put(url, data={"hasSeen": "true"}, format="json")
  923. assert response.status_code == 200
  924. assert response.data == {"hasSeen": True}
  925. r1 = GroupSeen.objects.filter(group=group1, user_id=self.user.id)
  926. assert r1.exists()
  927. r2 = GroupSeen.objects.filter(group=group2, user_id=self.user.id)
  928. assert r2.exists()
  929. r3 = GroupSeen.objects.filter(group=group3, user_id=self.user.id)
  930. assert not r3.exists()
  931. r4 = GroupSeen.objects.filter(group=group4, user_id=self.user.id)
  932. assert not r4.exists()
  933. def test_inbox_fields(self):
  934. group1 = self.create_group(status=GroupStatus.RESOLVED)
  935. add_group_to_inbox(group1, GroupInboxReason.NEW)
  936. self.login_as(user=self.user)
  937. url = f"{self.path}?id={group1.id}"
  938. response = self.client.put(url, data={"status": "resolved"}, format="json")
  939. assert "inbox" in response.data
  940. assert response.data["inbox"] is None
  941. @patch("sentry.issues.merge.uuid4")
  942. @patch("sentry.issues.merge.merge_groups")
  943. @patch("sentry.issues.merge.eventstream")
  944. def test_merge(self, mock_eventstream, merge_groups, mock_uuid4):
  945. eventstream_state = object()
  946. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  947. mock_uuid4.return_value = self.get_mock_uuid()
  948. group1 = self.create_group(times_seen=1)
  949. group2 = self.create_group(times_seen=50)
  950. group3 = self.create_group(times_seen=2)
  951. self.create_group()
  952. self.login_as(user=self.user)
  953. url = f"{self.path}?id={group1.id}&id={group2.id}&id={group3.id}"
  954. response = self.client.put(url, data={"merge": "1"}, format="json")
  955. assert response.status_code == 200
  956. assert response.data["merge"]["parent"] == str(group2.id)
  957. assert sorted(response.data["merge"]["children"]) == sorted(
  958. [str(group1.id), str(group3.id)]
  959. )
  960. mock_eventstream.start_merge.assert_called_once_with(
  961. group1.project_id, [group3.id, group1.id], group2.id
  962. )
  963. assert len(merge_groups.mock_calls) == 1
  964. merge_groups.delay.assert_any_call(
  965. from_object_ids=[group3.id, group1.id],
  966. to_object_id=group2.id,
  967. transaction_id="abc123",
  968. eventstream_state=eventstream_state,
  969. )
  970. @patch("sentry.issues.merge.uuid4")
  971. @patch("sentry.issues.merge.merge_groups")
  972. @patch("sentry.issues.merge.eventstream")
  973. def test_merge_performance_issues(self, mock_eventstream, merge_groups, mock_uuid4):
  974. eventstream_state = object()
  975. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  976. mock_uuid4.return_value = self.get_mock_uuid()
  977. group1 = self.create_group(times_seen=1, type=PerformanceSlowDBQueryGroupType.type_id)
  978. group2 = self.create_group(times_seen=50, type=PerformanceSlowDBQueryGroupType.type_id)
  979. group3 = self.create_group(times_seen=2, type=PerformanceSlowDBQueryGroupType.type_id)
  980. self.create_group()
  981. self.login_as(user=self.user)
  982. url = f"{self.path}?id={group1.id}&id={group2.id}&id={group3.id}"
  983. response = self.client.put(url, data={"merge": "1"}, format="json")
  984. assert response.status_code == 400, response.content
  985. def test_assign(self):
  986. group1 = self.create_group(is_public=True)
  987. group2 = self.create_group(is_public=True)
  988. user = self.user
  989. self.login_as(user=user)
  990. url = f"{self.path}?id={group1.id}"
  991. response = self.client.put(url, data={"assignedTo": user.username})
  992. assert response.status_code == 200
  993. assert response.data["assignedTo"]["id"] == str(user.id)
  994. assert response.data["assignedTo"]["type"] == "user"
  995. assert GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  996. assert not GroupAssignee.objects.filter(group=group2, user_id=user.id).exists()
  997. assert (
  998. Activity.objects.filter(
  999. group=group1, user_id=user.id, type=ActivityType.ASSIGNED.value
  1000. ).count()
  1001. == 1
  1002. )
  1003. assert GroupSubscription.objects.filter(
  1004. user_id=user.id, group=group1, is_active=True
  1005. ).exists()
  1006. response = self.client.put(url, data={"assignedTo": ""}, format="json")
  1007. assert response.status_code == 200, response.content
  1008. assert response.data["assignedTo"] is None
  1009. assert not GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  1010. def test_assign_non_member(self):
  1011. group = self.create_group(is_public=True)
  1012. member = self.user
  1013. non_member = self.create_user("bar@example.com")
  1014. self.login_as(user=member)
  1015. url = f"{self.path}?id={group.id}"
  1016. response = self.client.put(url, data={"assignedTo": non_member.username}, format="json")
  1017. assert response.status_code == 400, response.content
  1018. def test_assign_team(self):
  1019. self.login_as(user=self.user)
  1020. group = self.create_group()
  1021. other_member = self.create_user("bar@example.com")
  1022. team = self.create_team(
  1023. organization=group.project.organization, members=[self.user, other_member]
  1024. )
  1025. group.project.add_team(team)
  1026. url = f"{self.path}?id={group.id}"
  1027. response = self.client.put(url, data={"assignedTo": f"team:{team.id}"})
  1028. assert response.status_code == 200
  1029. assert response.data["assignedTo"]["id"] == str(team.id)
  1030. assert response.data["assignedTo"]["type"] == "team"
  1031. assert GroupAssignee.objects.filter(group=group, team=team).exists()
  1032. assert Activity.objects.filter(group=group, type=ActivityType.ASSIGNED.value).count() == 1
  1033. assert GroupSubscription.objects.filter(group=group, is_active=True).count() == 2
  1034. response = self.client.put(url, data={"assignedTo": ""}, format="json")
  1035. assert response.status_code == 200, response.content
  1036. assert response.data["assignedTo"] is None
  1037. def test_discard(self):
  1038. group1 = self.create_group(is_public=True)
  1039. group2 = self.create_group(is_public=True)
  1040. group_hash = GroupHash.objects.create(hash="x" * 32, project=group1.project, group=group1)
  1041. user = self.user
  1042. self.login_as(user=user)
  1043. url = f"{self.path}?id={group1.id}"
  1044. with self.tasks():
  1045. with self.feature("projects:discard-groups"):
  1046. response = self.client.put(url, data={"discard": True})
  1047. assert response.status_code == 204
  1048. assert not Group.objects.filter(id=group1.id).exists()
  1049. assert Group.objects.filter(id=group2.id).exists()
  1050. assert GroupHash.objects.filter(id=group_hash.id).exists()
  1051. tombstone = GroupTombstone.objects.get(
  1052. id=GroupHash.objects.get(id=group_hash.id).group_tombstone_id
  1053. )
  1054. assert tombstone.message == group1.message
  1055. assert tombstone.culprit == group1.culprit
  1056. assert tombstone.project == group1.project
  1057. assert tombstone.data == group1.data
  1058. @patch(
  1059. "sentry.models.OrganizationMember.get_scopes",
  1060. return_value=frozenset(s for s in settings.SENTRY_SCOPES if s != "event:admin"),
  1061. )
  1062. def test_discard_requires_events_admin(self, mock_get_scopes):
  1063. group1 = self.create_group(is_public=True)
  1064. user = self.user
  1065. self.login_as(user=user)
  1066. url = f"{self.path}?id={group1.id}"
  1067. with self.tasks(), self.feature("projects:discard-groups"):
  1068. response = self.client.put(url, data={"discard": True})
  1069. assert response.status_code == 400
  1070. assert Group.objects.filter(id=group1.id).exists()
  1071. @region_silo_test
  1072. class GroupDeleteTest(APITestCase, SnubaTestCase):
  1073. @cached_property
  1074. def path(self):
  1075. return f"/api/0/projects/{self.project.organization.slug}/{self.project.slug}/issues/"
  1076. @patch("sentry.api.helpers.group_index.delete.eventstream")
  1077. @patch("sentry.eventstream")
  1078. def test_delete_by_id(self, mock_eventstream_task, mock_eventstream_api):
  1079. eventstream_state = {"event_stream_state": uuid4()}
  1080. mock_eventstream_api.start_delete_groups = Mock(return_value=eventstream_state)
  1081. group1 = self.create_group(status=GroupStatus.RESOLVED)
  1082. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  1083. group3 = self.create_group(status=GroupStatus.IGNORED)
  1084. group4 = self.create_group(
  1085. project=self.create_project(slug="foo"),
  1086. status=GroupStatus.UNRESOLVED,
  1087. )
  1088. hashes = []
  1089. for g in group1, group2, group3, group4:
  1090. hash = uuid4().hex
  1091. hashes.append(hash)
  1092. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  1093. self.login_as(user=self.user)
  1094. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  1095. response = self.client.delete(url, format="json")
  1096. mock_eventstream_api.start_delete_groups.assert_called_once_with(
  1097. group1.project_id, [group1.id, group2.id]
  1098. )
  1099. assert response.status_code == 204
  1100. assert Group.objects.get(id=group1.id).status == GroupStatus.PENDING_DELETION
  1101. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  1102. assert Group.objects.get(id=group2.id).status == GroupStatus.PENDING_DELETION
  1103. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  1104. assert Group.objects.get(id=group3.id).status != GroupStatus.PENDING_DELETION
  1105. assert GroupHash.objects.filter(group_id=group3.id).exists()
  1106. assert Group.objects.get(id=group4.id).status != GroupStatus.PENDING_DELETION
  1107. assert GroupHash.objects.filter(group_id=group4.id).exists()
  1108. Group.objects.filter(id__in=(group1.id, group2.id)).update(status=GroupStatus.UNRESOLVED)
  1109. with self.tasks():
  1110. response = self.client.delete(url, format="json")
  1111. mock_eventstream_task.end_delete_groups.assert_called_once_with(eventstream_state)
  1112. assert response.status_code == 204
  1113. assert not Group.objects.filter(id=group1.id).exists()
  1114. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  1115. assert not Group.objects.filter(id=group2.id).exists()
  1116. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  1117. assert Group.objects.filter(id=group3.id).exists()
  1118. assert GroupHash.objects.filter(group_id=group3.id).exists()
  1119. assert Group.objects.filter(id=group4.id).exists()
  1120. assert GroupHash.objects.filter(group_id=group4.id).exists()
  1121. @patch("sentry.api.helpers.group_index.delete.eventstream")
  1122. @patch("sentry.eventstream")
  1123. def test_delete_performance_issue_by_id(self, mock_eventstream_task, mock_eventstream_api):
  1124. eventstream_state = {"event_stream_state": uuid4()}
  1125. mock_eventstream_api.start_delete_groups = Mock(return_value=eventstream_state)
  1126. group1 = self.create_group(
  1127. status=GroupStatus.RESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  1128. )
  1129. group2 = self.create_group(
  1130. status=GroupStatus.UNRESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  1131. )
  1132. hashes = []
  1133. for g in group1, group2:
  1134. hash = uuid4().hex
  1135. hashes.append(hash)
  1136. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  1137. self.login_as(user=self.user)
  1138. url = f"{self.path}?id={group1.id}&id={group2.id}"
  1139. response = self.client.delete(url, format="json")
  1140. assert response.status_code == 400
  1141. assert Group.objects.filter(id=group1.id).exists()
  1142. assert GroupHash.objects.filter(group_id=group1.id).exists()
  1143. assert Group.objects.filter(id=group2.id).exists()
  1144. assert GroupHash.objects.filter(group_id=group2.id).exists()
  1145. def test_bulk_delete(self):
  1146. groups = []
  1147. for i in range(10, 41):
  1148. groups.append(
  1149. self.create_group(
  1150. project=self.project,
  1151. status=GroupStatus.RESOLVED,
  1152. )
  1153. )
  1154. hashes = []
  1155. for group in groups:
  1156. hash = uuid4().hex
  1157. hashes.append(hash)
  1158. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  1159. self.login_as(user=self.user)
  1160. # if query is '' it defaults to is:unresolved
  1161. url = self.path + "?query="
  1162. response = self.client.delete(url, format="json")
  1163. assert response.status_code == 204
  1164. for group in groups:
  1165. assert Group.objects.get(id=group.id).status == GroupStatus.PENDING_DELETION
  1166. assert not GroupHash.objects.filter(group_id=group.id).exists()
  1167. Group.objects.filter(id__in=[group.id for group in groups]).update(
  1168. status=GroupStatus.UNRESOLVED
  1169. )
  1170. with self.tasks():
  1171. response = self.client.delete(url, format="json")
  1172. assert response.status_code == 204
  1173. for group in groups:
  1174. assert not Group.objects.filter(id=group.id).exists()
  1175. assert not GroupHash.objects.filter(group_id=group.id).exists()
  1176. def test_bulk_delete_performance_issues(self):
  1177. groups = []
  1178. for i in range(10, 41):
  1179. groups.append(
  1180. self.create_group(
  1181. project=self.project,
  1182. status=GroupStatus.RESOLVED,
  1183. type=PerformanceSlowDBQueryGroupType.type_id,
  1184. )
  1185. )
  1186. hashes = []
  1187. for group in groups:
  1188. hash = uuid4().hex
  1189. hashes.append(hash)
  1190. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  1191. self.login_as(user=self.user)
  1192. # if query is '' it defaults to is:unresolved
  1193. url = self.path + "?query="
  1194. response = self.client.delete(url, format="json")
  1195. assert response.status_code == 400
  1196. for group in groups:
  1197. assert Group.objects.filter(id=group.id).exists()
  1198. assert GroupHash.objects.filter(group_id=group.id).exists()