test_project_group_index.py 60 KB

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