test_project_group_index.py 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  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.integrations.models.external_issue import ExternalIssue
  10. from sentry.integrations.models.organization_integration import OrganizationIntegration
  11. from sentry.issues.grouptype import PerformanceSlowDBQueryGroupType
  12. from sentry.models.activity import Activity
  13. from sentry.models.apitoken import ApiToken
  14. from sentry.models.group import Group, GroupStatus
  15. from sentry.models.groupassignee import GroupAssignee
  16. from sentry.models.groupbookmark import GroupBookmark
  17. from sentry.models.grouphash import GroupHash
  18. from sentry.models.groupinbox import GroupInboxReason, add_group_to_inbox
  19. from sentry.models.grouplink import GroupLink
  20. from sentry.models.groupresolution import GroupResolution
  21. from sentry.models.groupseen import GroupSeen
  22. from sentry.models.groupshare import GroupShare
  23. from sentry.models.groupsnooze import GroupSnooze
  24. from sentry.models.groupsubscription import GroupSubscription
  25. from sentry.models.grouptombstone import GroupTombstone
  26. from sentry.models.release import Release
  27. from sentry.silo.base import SiloMode
  28. from sentry.testutils.cases import APITestCase, SnubaTestCase
  29. from sentry.testutils.helpers import parse_link_header
  30. from sentry.testutils.helpers.datetime import before_now, iso_format
  31. from sentry.testutils.silo import assume_test_silo_mode
  32. from sentry.types.activity import ActivityType
  33. from sentry.users.models.user_option import UserOption
  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 + "?query=is:unresolved", 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 + "?query=is:unresolved", 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&query=is:unresolved",
  319. data={"status": "resolved"},
  320. format="json",
  321. )
  322. assert response.status_code == 200, response.data
  323. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  324. # the previously resolved entry should not be included
  325. new_group1 = Group.objects.get(id=group1.id)
  326. assert new_group1.status == GroupStatus.RESOLVED
  327. assert new_group1.resolved_at is None
  328. # this wont exist because it wasn't affected
  329. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group1).exists()
  330. new_group2 = Group.objects.get(id=group2.id)
  331. assert new_group2.status == GroupStatus.RESOLVED
  332. assert new_group2.resolved_at is not None
  333. assert GroupSubscription.objects.filter(
  334. user_id=self.user.id, group=new_group2, is_active=True
  335. ).exists()
  336. # the ignored entry should not be included
  337. new_group3 = Group.objects.get(id=group3.id)
  338. assert new_group3.status == GroupStatus.IGNORED
  339. assert new_group3.resolved_at is None
  340. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group3)
  341. new_group4 = Group.objects.get(id=group4.id)
  342. assert new_group4.status == GroupStatus.UNRESOLVED
  343. assert new_group4.resolved_at is None
  344. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group4)
  345. def test_bulk_resolve(self):
  346. self.login_as(user=self.user)
  347. for i in range(200):
  348. self.create_group(status=GroupStatus.UNRESOLVED)
  349. response = self.client.get(f"{self.path}?sort_by=date&query=is:unresolved", format="json")
  350. assert len(response.data) == 100
  351. response = self.client.put(
  352. f"{self.path}?status=unresolved&query=is:unresolved",
  353. data={"status": "resolved"},
  354. 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. with assume_test_silo_mode(SiloMode.CONTROL):
  365. integration = self.create_provider_integration(provider="example", name="Example")
  366. integration.add_organization(org, self.user)
  367. group = self.create_group(status=GroupStatus.UNRESOLVED)
  368. with assume_test_silo_mode(SiloMode.CONTROL):
  369. OrganizationIntegration.objects.filter(
  370. integration_id=integration.id, organization_id=group.organization.id
  371. ).update(
  372. config={
  373. "sync_comments": True,
  374. "sync_status_outbound": True,
  375. "sync_status_inbound": True,
  376. "sync_assignee_outbound": True,
  377. "sync_assignee_inbound": True,
  378. }
  379. )
  380. external_issue = ExternalIssue.objects.get_or_create(
  381. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  382. )[0]
  383. GroupLink.objects.get_or_create(
  384. group_id=group.id,
  385. project_id=group.project_id,
  386. linked_type=GroupLink.LinkedType.issue,
  387. linked_id=external_issue.id,
  388. relationship=GroupLink.Relationship.references,
  389. )[0]
  390. response = self.client.get(f"{self.path}?sort_by=date&query=is:unresolved", format="json")
  391. assert len(response.data) == 1
  392. with self.tasks():
  393. with self.feature({"organizations:integrations-issue-sync": True}):
  394. response = self.client.put(
  395. f"{self.path}?status=unresolved&query=is:unresolved",
  396. data={"status": "resolved"},
  397. format="json",
  398. )
  399. assert response.status_code == 200, response.data
  400. group = Group.objects.get(id=group.id)
  401. assert group.status == GroupStatus.RESOLVED
  402. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  403. mock_sync_status_outbound.assert_called_once_with(
  404. external_issue, True, group.project_id
  405. )
  406. response = self.client.get(f"{self.path}?sort_by=date&query=is:unresolved", format="json")
  407. assert len(response.data) == 0
  408. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  409. def test_set_unresolved_with_integration(self, mock_sync_status_outbound):
  410. release = self.create_release(project=self.project, version="abc")
  411. group = self.create_group(status=GroupStatus.RESOLVED)
  412. org = self.organization
  413. with assume_test_silo_mode(SiloMode.CONTROL):
  414. integration = self.create_provider_integration(provider="example", name="Example")
  415. integration.add_organization(org, self.user)
  416. OrganizationIntegration.objects.filter(
  417. integration_id=integration.id, organization_id=group.organization.id
  418. ).update(
  419. config={
  420. "sync_comments": True,
  421. "sync_status_outbound": True,
  422. "sync_status_inbound": True,
  423. "sync_assignee_outbound": True,
  424. "sync_assignee_inbound": True,
  425. }
  426. )
  427. GroupResolution.objects.create(group=group, release=release)
  428. external_issue = ExternalIssue.objects.get_or_create(
  429. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  430. )[0]
  431. GroupLink.objects.get_or_create(
  432. group_id=group.id,
  433. project_id=group.project_id,
  434. linked_type=GroupLink.LinkedType.issue,
  435. linked_id=external_issue.id,
  436. relationship=GroupLink.Relationship.references,
  437. )[0]
  438. self.login_as(user=self.user)
  439. url = f"{self.path}?id={group.id}"
  440. with self.tasks():
  441. with self.feature({"organizations:integrations-issue-sync": True}):
  442. response = self.client.put(url, data={"status": "unresolved"}, format="json")
  443. assert response.status_code == 200
  444. assert response.data == {"status": "unresolved", "statusDetails": {}}
  445. group = Group.objects.get(id=group.id)
  446. assert group.status == GroupStatus.UNRESOLVED
  447. self.assertNoResolution(group)
  448. assert GroupSubscription.objects.filter(
  449. user_id=self.user.id, group=group, is_active=True
  450. ).exists()
  451. mock_sync_status_outbound.assert_called_once_with(
  452. external_issue, False, group.project_id
  453. )
  454. def test_self_assign_issue(self):
  455. group = self.create_group(status=GroupStatus.UNRESOLVED)
  456. user = self.user
  457. with assume_test_silo_mode(SiloMode.CONTROL):
  458. uo1 = UserOption.objects.create(
  459. key="self_assign_issue", value="1", project_id=None, user=user
  460. )
  461. self.login_as(user=user)
  462. url = f"{self.path}?id={group.id}"
  463. response = self.client.put(url, data={"status": "resolved"}, format="json")
  464. assert response.status_code == 200, response.data
  465. assert response.data["assignedTo"]["id"] == str(user.id)
  466. assert response.data["assignedTo"]["type"] == "user"
  467. assert response.data["status"] == "resolved"
  468. assert GroupAssignee.objects.filter(group=group, user_id=user.id).exists()
  469. assert GroupSubscription.objects.filter(
  470. user_id=user.id, group=group, is_active=True
  471. ).exists()
  472. with assume_test_silo_mode(SiloMode.CONTROL):
  473. uo1.delete()
  474. def test_self_assign_issue_next_release(self):
  475. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  476. release.add_project(self.project)
  477. group = self.create_group(status=GroupStatus.UNRESOLVED)
  478. with assume_test_silo_mode(SiloMode.CONTROL):
  479. uo1 = UserOption.objects.create(
  480. key="self_assign_issue", value="1", project_id=None, user=self.user
  481. )
  482. self.login_as(user=self.user)
  483. url = f"{self.path}?id={group.id}"
  484. response = self.client.put(url, data={"status": "resolvedInNextRelease"}, format="json")
  485. assert response.status_code == 200
  486. assert response.data["status"] == "resolved"
  487. assert response.data["statusDetails"]["inNextRelease"]
  488. assert response.data["assignedTo"]["id"] == str(self.user.id)
  489. assert response.data["assignedTo"]["type"] == "user"
  490. group = Group.objects.get(id=group.id)
  491. assert group.status == GroupStatus.RESOLVED
  492. assert GroupResolution.objects.filter(group=group, release=release).exists()
  493. assert GroupSubscription.objects.filter(
  494. user_id=self.user.id, group=group, is_active=True
  495. ).exists()
  496. activity = Activity.objects.get(
  497. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  498. )
  499. assert activity.data["version"] == ""
  500. with assume_test_silo_mode(SiloMode.CONTROL):
  501. uo1.delete()
  502. def test_selective_status_update(self):
  503. group1 = self.create_group(status=GroupStatus.RESOLVED)
  504. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  505. group3 = self.create_group(status=GroupStatus.IGNORED)
  506. group4 = self.create_group(
  507. project=self.create_project(slug="foo"),
  508. status=GroupStatus.UNRESOLVED,
  509. )
  510. self.login_as(user=self.user)
  511. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  512. response = self.client.put(url, data={"status": "resolved"}, format="json")
  513. assert response.status_code == 200
  514. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  515. new_group1 = Group.objects.get(id=group1.id)
  516. assert new_group1.resolved_at is not None
  517. assert new_group1.status == GroupStatus.RESOLVED
  518. new_group2 = Group.objects.get(id=group2.id)
  519. assert new_group2.resolved_at is not None
  520. assert new_group2.status == GroupStatus.RESOLVED
  521. assert GroupSubscription.objects.filter(
  522. user_id=self.user.id, group=new_group2, is_active=True
  523. ).exists()
  524. new_group3 = Group.objects.get(id=group3.id)
  525. assert new_group3.resolved_at is None
  526. assert new_group3.status == GroupStatus.IGNORED
  527. new_group4 = Group.objects.get(id=group4.id)
  528. assert new_group4.resolved_at is None
  529. assert new_group4.status == GroupStatus.UNRESOLVED
  530. def test_set_resolved_in_current_release(self):
  531. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  532. release.add_project(self.project)
  533. group = self.create_group(status=GroupStatus.UNRESOLVED)
  534. self.login_as(user=self.user)
  535. url = f"{self.path}?id={group.id}"
  536. response = self.client.put(
  537. url,
  538. data={"status": "resolved", "statusDetails": {"inRelease": "latest"}},
  539. format="json",
  540. )
  541. assert response.status_code == 200
  542. assert response.data["status"] == "resolved"
  543. assert response.data["statusDetails"]["inRelease"] == release.version
  544. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  545. assert "activity" in response.data
  546. group = Group.objects.get(id=group.id)
  547. assert group.status == GroupStatus.RESOLVED
  548. resolution = GroupResolution.objects.get(group=group)
  549. assert resolution.release == release
  550. assert resolution.type == GroupResolution.Type.in_release
  551. assert resolution.status == GroupResolution.Status.resolved
  552. assert resolution.actor_id == self.user.id
  553. assert GroupSubscription.objects.filter(
  554. user_id=self.user.id, group=group, is_active=True
  555. ).exists()
  556. activity = Activity.objects.get(
  557. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  558. )
  559. assert activity.data["version"] == release.version
  560. def test_set_resolved_in_explicit_release(self):
  561. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  562. release.add_project(self.project)
  563. release2 = Release.objects.create(organization_id=self.project.organization_id, version="b")
  564. release2.add_project(self.project)
  565. group = self.create_group(status=GroupStatus.UNRESOLVED)
  566. self.login_as(user=self.user)
  567. url = f"{self.path}?id={group.id}"
  568. response = self.client.put(
  569. url,
  570. data={"status": "resolved", "statusDetails": {"inRelease": release.version}},
  571. format="json",
  572. )
  573. assert response.status_code == 200
  574. assert response.data["status"] == "resolved"
  575. assert response.data["statusDetails"]["inRelease"] == release.version
  576. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  577. assert "activity" in response.data
  578. group = Group.objects.get(id=group.id)
  579. assert group.status == GroupStatus.RESOLVED
  580. resolution = GroupResolution.objects.get(group=group)
  581. assert resolution.release == release
  582. assert resolution.type == GroupResolution.Type.in_release
  583. assert resolution.status == GroupResolution.Status.resolved
  584. assert resolution.actor_id == self.user.id
  585. assert GroupSubscription.objects.filter(
  586. user_id=self.user.id, group=group, is_active=True
  587. ).exists()
  588. activity = Activity.objects.get(
  589. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  590. )
  591. assert activity.data["version"] == release.version
  592. def test_set_resolved_in_next_release(self):
  593. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  594. release.add_project(self.project)
  595. group = self.create_group(status=GroupStatus.UNRESOLVED)
  596. self.login_as(user=self.user)
  597. url = f"{self.path}?id={group.id}"
  598. response = self.client.put(
  599. url,
  600. data={"status": "resolved", "statusDetails": {"inNextRelease": True}},
  601. format="json",
  602. )
  603. assert response.status_code == 200
  604. assert response.data["status"] == "resolved"
  605. assert response.data["statusDetails"]["inNextRelease"]
  606. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  607. assert "activity" in response.data
  608. group = Group.objects.get(id=group.id)
  609. assert group.status == GroupStatus.RESOLVED
  610. resolution = GroupResolution.objects.get(group=group)
  611. assert resolution.release == release
  612. assert resolution.type == GroupResolution.Type.in_next_release
  613. assert resolution.status == GroupResolution.Status.pending
  614. assert resolution.actor_id == self.user.id
  615. assert GroupSubscription.objects.filter(
  616. user_id=self.user.id, group=group, is_active=True
  617. ).exists()
  618. activity = Activity.objects.get(
  619. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  620. )
  621. assert activity.data["version"] == ""
  622. def test_set_resolved_in_next_release_legacy(self):
  623. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  624. release.add_project(self.project)
  625. group = self.create_group(status=GroupStatus.UNRESOLVED)
  626. self.login_as(user=self.user)
  627. url = f"{self.path}?id={group.id}"
  628. response = self.client.put(url, data={"status": "resolvedInNextRelease"}, format="json")
  629. assert response.status_code == 200
  630. assert response.data["status"] == "resolved"
  631. assert response.data["statusDetails"]["inNextRelease"]
  632. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  633. group = Group.objects.get(id=group.id)
  634. assert group.status == GroupStatus.RESOLVED
  635. resolution = GroupResolution.objects.get(group=group)
  636. assert resolution.release == release
  637. assert resolution.type == GroupResolution.Type.in_next_release
  638. assert resolution.status == GroupResolution.Status.pending
  639. assert resolution.actor_id == self.user.id
  640. assert GroupSubscription.objects.filter(
  641. user_id=self.user.id, group=group, is_active=True
  642. ).exists()
  643. activity = Activity.objects.get(
  644. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  645. )
  646. assert activity.data["version"] == ""
  647. def test_set_resolved_in_upcoming_release(self):
  648. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  649. release.add_project(self.project)
  650. group = self.create_group(status=GroupStatus.UNRESOLVED)
  651. self.login_as(user=self.user)
  652. url = f"{self.path}?id={group.id}"
  653. response = self.client.put(
  654. url,
  655. data={"status": "resolved", "statusDetails": {"inUpcomingRelease": True}},
  656. format="json",
  657. )
  658. assert response.status_code == 200
  659. assert response.data["status"] == "resolved"
  660. assert response.data["statusDetails"]["inUpcomingRelease"]
  661. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  662. assert "activity" in response.data
  663. group = Group.objects.get(id=group.id)
  664. assert group.status == GroupStatus.RESOLVED
  665. resolution = GroupResolution.objects.get(group=group)
  666. assert resolution.release == release
  667. assert resolution.type == GroupResolution.Type.in_upcoming_release
  668. assert resolution.status == GroupResolution.Status.pending
  669. assert resolution.actor_id == self.user.id
  670. assert GroupSubscription.objects.filter(
  671. user_id=self.user.id, group=group, is_active=True
  672. ).exists()
  673. activity = Activity.objects.get(
  674. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  675. )
  676. assert activity.data["version"] == ""
  677. def test_upcoming_release_release_validation(self):
  678. group = self.create_group(status=GroupStatus.UNRESOLVED)
  679. self.login_as(user=self.user)
  680. url = f"{self.path}?id={group.id}"
  681. response = self.client.put(
  682. url,
  683. data={"status": "resolved", "statusDetails": {"inUpcomingRelease": True}},
  684. format="json",
  685. )
  686. assert response.status_code == 400
  687. assert (
  688. response.data["statusDetails"]["inUpcomingRelease"][0]
  689. == "No release data present in the system."
  690. )
  691. def test_set_resolved_in_explicit_commit_unreleased(self):
  692. repo = self.create_repo(project=self.project, name=self.project.name)
  693. commit = self.create_commit(project=self.project, repo=repo)
  694. group = self.create_group(status=GroupStatus.UNRESOLVED)
  695. self.login_as(user=self.user)
  696. url = f"{self.path}?id={group.id}"
  697. response = self.client.put(
  698. url,
  699. data={
  700. "status": "resolved",
  701. "statusDetails": {"inCommit": {"commit": commit.key, "repository": repo.name}},
  702. },
  703. format="json",
  704. )
  705. assert response.status_code == 200
  706. assert response.data["status"] == "resolved"
  707. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  708. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  709. group = Group.objects.get(id=group.id)
  710. assert group.status == GroupStatus.RESOLVED
  711. link = GroupLink.objects.get(group_id=group.id)
  712. assert link.linked_type == GroupLink.LinkedType.commit
  713. assert link.relationship == GroupLink.Relationship.resolves
  714. assert link.linked_id == commit.id
  715. assert GroupSubscription.objects.filter(
  716. user_id=self.user.id, group=group, is_active=True
  717. ).exists()
  718. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  719. assert activity.data["commit"] == commit.id
  720. def test_set_resolved_in_explicit_commit_released(self):
  721. release = self.create_release(project=self.project)
  722. repo = self.create_repo(project=self.project, name=self.project.name)
  723. commit = self.create_commit(project=self.project, repo=repo, release=release)
  724. group = self.create_group(status=GroupStatus.UNRESOLVED)
  725. self.login_as(user=self.user)
  726. url = f"{self.path}?id={group.id}"
  727. response = self.client.put(
  728. url,
  729. data={
  730. "status": "resolved",
  731. "statusDetails": {"inCommit": {"commit": commit.key, "repository": repo.name}},
  732. },
  733. format="json",
  734. )
  735. assert response.status_code == 200
  736. assert response.data["status"] == "resolved"
  737. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  738. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  739. group = Group.objects.get(id=group.id)
  740. assert group.status == GroupStatus.RESOLVED
  741. link = GroupLink.objects.get(group_id=group.id)
  742. assert link.project_id == self.project.id
  743. assert link.linked_type == GroupLink.LinkedType.commit
  744. assert link.relationship == GroupLink.Relationship.resolves
  745. assert link.linked_id == commit.id
  746. assert GroupSubscription.objects.filter(
  747. user_id=self.user.id, group=group, is_active=True
  748. ).exists()
  749. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  750. assert activity.data["commit"] == commit.id
  751. resolution = GroupResolution.objects.get(group=group)
  752. assert resolution.type == GroupResolution.Type.in_release
  753. assert resolution.status == GroupResolution.Status.resolved
  754. def test_set_resolved_in_explicit_commit_missing(self):
  755. repo = self.create_repo(project=self.project, name=self.project.name)
  756. group = self.create_group(status=GroupStatus.UNRESOLVED)
  757. self.login_as(user=self.user)
  758. url = f"{self.path}?id={group.id}"
  759. response = self.client.put(
  760. url,
  761. data={
  762. "status": "resolved",
  763. "statusDetails": {"inCommit": {"commit": "a" * 40, "repository": repo.name}},
  764. },
  765. format="json",
  766. )
  767. assert response.status_code == 400
  768. assert (
  769. response.data["statusDetails"]["inCommit"]["commit"][0]
  770. == "Unable to find the given commit."
  771. )
  772. def test_set_unresolved(self):
  773. release = self.create_release(project=self.project, version="abc")
  774. group = self.create_group(status=GroupStatus.RESOLVED)
  775. GroupResolution.objects.create(group=group, release=release)
  776. self.login_as(user=self.user)
  777. url = f"{self.path}?id={group.id}"
  778. response = self.client.put(url, data={"status": "unresolved"}, format="json")
  779. assert response.status_code == 200
  780. assert response.data == {"status": "unresolved", "statusDetails": {}}
  781. group = Group.objects.get(id=group.id)
  782. assert group.status == GroupStatus.UNRESOLVED
  783. self.assertNoResolution(group)
  784. assert GroupSubscription.objects.filter(
  785. user_id=self.user.id, group=group, is_active=True
  786. ).exists()
  787. def test_set_unresolved_on_snooze(self):
  788. group = self.create_group(status=GroupStatus.IGNORED)
  789. GroupSnooze.objects.create(group=group, until=timezone.now() - timedelta(days=1))
  790. self.login_as(user=self.user)
  791. url = f"{self.path}?id={group.id}"
  792. response = self.client.put(url, data={"status": "unresolved"}, format="json")
  793. assert response.status_code == 200
  794. assert response.data == {"status": "unresolved", "statusDetails": {}}
  795. group = Group.objects.get(id=group.id)
  796. assert group.status == GroupStatus.UNRESOLVED
  797. def test_basic_ignore(self):
  798. group = self.create_group(status=GroupStatus.RESOLVED)
  799. snooze = GroupSnooze.objects.create(group=group, until=timezone.now())
  800. self.login_as(user=self.user)
  801. url = f"{self.path}?id={group.id}"
  802. response = self.client.put(url, data={"status": "ignored"}, format="json")
  803. assert response.status_code == 200
  804. # existing snooze objects should be cleaned up
  805. assert not GroupSnooze.objects.filter(id=snooze.id).exists()
  806. group = Group.objects.get(id=group.id)
  807. assert group.status == GroupStatus.IGNORED
  808. assert response.data == {"status": "ignored", "statusDetails": {}, "inbox": None}
  809. def test_snooze_duration(self):
  810. group = self.create_group(status=GroupStatus.RESOLVED)
  811. self.login_as(user=self.user)
  812. url = f"{self.path}?id={group.id}"
  813. response = self.client.put(
  814. url, data={"status": "ignored", "ignoreDuration": 30}, format="json"
  815. )
  816. assert response.status_code == 200
  817. snooze = GroupSnooze.objects.get(group=group)
  818. now = timezone.now()
  819. assert snooze.count is None
  820. assert snooze.until is not None
  821. assert snooze.until > now + timedelta(minutes=29)
  822. assert snooze.until < now + timedelta(minutes=31)
  823. assert snooze.user_count is None
  824. assert snooze.user_window is None
  825. assert snooze.window is None
  826. assert response.data["status"] == "ignored"
  827. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  828. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  829. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  830. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  831. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  832. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  833. def test_snooze_count(self):
  834. group = self.create_group(status=GroupStatus.RESOLVED, times_seen=1)
  835. self.login_as(user=self.user)
  836. url = f"{self.path}?id={group.id}"
  837. response = self.client.put(
  838. url, data={"status": "ignored", "ignoreCount": 100}, format="json"
  839. )
  840. assert response.status_code == 200
  841. snooze = GroupSnooze.objects.get(group=group)
  842. assert snooze.count == 100
  843. assert snooze.until is None
  844. assert snooze.user_count is None
  845. assert snooze.user_window is None
  846. assert snooze.window is None
  847. assert snooze.state is not None
  848. assert snooze.state["times_seen"] == 1
  849. assert response.data["status"] == "ignored"
  850. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  851. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  852. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  853. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  854. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  855. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  856. def test_snooze_user_count(self):
  857. for i in range(10):
  858. event = self.store_event(
  859. data={
  860. "fingerprint": ["put-me-in-group-1"],
  861. "user": {"id": str(i)},
  862. "timestamp": iso_format(self.min_ago + timedelta(seconds=i)),
  863. },
  864. project_id=self.project.id,
  865. )
  866. group = Group.objects.get(id=event.group.id)
  867. group.status = GroupStatus.RESOLVED
  868. group.substatus = None
  869. group.save()
  870. self.login_as(user=self.user)
  871. url = f"{self.path}?id={group.id}"
  872. response = self.client.put(
  873. url, data={"status": "ignored", "ignoreUserCount": 10}, format="json"
  874. )
  875. assert response.status_code == 200
  876. snooze = GroupSnooze.objects.get(group=group)
  877. assert snooze.count is None
  878. assert snooze.until is None
  879. assert snooze.user_count == 10
  880. assert snooze.user_window is None
  881. assert snooze.window is None
  882. assert snooze.state is not None
  883. assert snooze.state["users_seen"] == 10
  884. assert response.data["status"] == "ignored"
  885. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  886. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  887. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  888. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  889. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  890. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  891. def test_set_bookmarked(self):
  892. group1 = self.create_group(status=GroupStatus.RESOLVED)
  893. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  894. group3 = self.create_group(status=GroupStatus.IGNORED)
  895. group4 = self.create_group(
  896. project=self.create_project(slug="foo"),
  897. status=GroupStatus.UNRESOLVED,
  898. )
  899. self.login_as(user=self.user)
  900. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  901. response = self.client.put(url, data={"isBookmarked": "true"}, format="json")
  902. assert response.status_code == 200
  903. assert response.data == {"isBookmarked": True}
  904. bookmark1 = GroupBookmark.objects.filter(group=group1, user_id=self.user.id)
  905. assert bookmark1.exists()
  906. assert GroupSubscription.objects.filter(
  907. user_id=self.user.id, group=group1, is_active=True
  908. ).exists()
  909. bookmark2 = GroupBookmark.objects.filter(group=group2, user_id=self.user.id)
  910. assert bookmark2.exists()
  911. assert GroupSubscription.objects.filter(
  912. user_id=self.user.id, group=group2, is_active=True
  913. ).exists()
  914. bookmark3 = GroupBookmark.objects.filter(group=group3, user_id=self.user.id)
  915. assert not bookmark3.exists()
  916. bookmark4 = GroupBookmark.objects.filter(group=group4, user_id=self.user.id)
  917. assert not bookmark4.exists()
  918. def test_subscription(self):
  919. group1 = self.create_group()
  920. group2 = self.create_group()
  921. group3 = self.create_group()
  922. group4 = self.create_group(project=self.create_project(slug="foo"))
  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={"isSubscribed": "true"}, format="json")
  926. assert response.status_code == 200
  927. assert response.data == {"isSubscribed": True, "subscriptionDetails": {"reason": "unknown"}}
  928. assert GroupSubscription.objects.filter(
  929. group=group1, user_id=self.user.id, is_active=True
  930. ).exists()
  931. assert GroupSubscription.objects.filter(
  932. group=group2, user_id=self.user.id, is_active=True
  933. ).exists()
  934. assert not GroupSubscription.objects.filter(group=group3, user_id=self.user.id).exists()
  935. assert not GroupSubscription.objects.filter(group=group4, user_id=self.user.id).exists()
  936. def test_set_public(self):
  937. group1 = self.create_group()
  938. group2 = self.create_group()
  939. self.login_as(user=self.user)
  940. url = f"{self.path}?id={group1.id}&id={group2.id}"
  941. response = self.client.put(url, data={"isPublic": "true"}, format="json")
  942. assert response.status_code == 200
  943. assert response.data["isPublic"] is True
  944. assert "shareId" in response.data
  945. new_group1 = Group.objects.get(id=group1.id)
  946. assert bool(new_group1.get_share_id())
  947. new_group2 = Group.objects.get(id=group2.id)
  948. assert bool(new_group2.get_share_id())
  949. def test_set_private(self):
  950. group1 = self.create_group()
  951. group2 = self.create_group()
  952. # Manually mark them as shared
  953. for g in group1, group2:
  954. GroupShare.objects.create(project_id=g.project_id, group=g)
  955. assert bool(g.get_share_id())
  956. self.login_as(user=self.user)
  957. url = f"{self.path}?id={group1.id}&id={group2.id}"
  958. response = self.client.put(url, data={"isPublic": "false"}, format="json")
  959. assert response.status_code == 200
  960. assert response.data == {"isPublic": False, "shareId": None}
  961. new_group1 = Group.objects.get(id=group1.id)
  962. assert not bool(new_group1.get_share_id())
  963. new_group2 = Group.objects.get(id=group2.id)
  964. assert not bool(new_group2.get_share_id())
  965. def test_set_has_seen(self):
  966. group1 = self.create_group(status=GroupStatus.RESOLVED)
  967. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  968. group3 = self.create_group(status=GroupStatus.IGNORED)
  969. group4 = self.create_group(
  970. project=self.create_project(slug="foo"),
  971. status=GroupStatus.UNRESOLVED,
  972. )
  973. self.login_as(user=self.user)
  974. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  975. response = self.client.put(url, data={"hasSeen": "true"}, format="json")
  976. assert response.status_code == 200
  977. assert response.data == {"hasSeen": True}
  978. r1 = GroupSeen.objects.filter(group=group1, user_id=self.user.id)
  979. assert r1.exists()
  980. r2 = GroupSeen.objects.filter(group=group2, user_id=self.user.id)
  981. assert r2.exists()
  982. r3 = GroupSeen.objects.filter(group=group3, user_id=self.user.id)
  983. assert not r3.exists()
  984. r4 = GroupSeen.objects.filter(group=group4, user_id=self.user.id)
  985. assert not r4.exists()
  986. def test_inbox_fields(self):
  987. group1 = self.create_group(status=GroupStatus.RESOLVED)
  988. add_group_to_inbox(group1, GroupInboxReason.NEW)
  989. self.login_as(user=self.user)
  990. url = f"{self.path}?id={group1.id}"
  991. response = self.client.put(url, data={"status": "resolved"}, format="json")
  992. assert "inbox" in response.data
  993. assert response.data["inbox"] is None
  994. @patch("sentry.issues.merge.uuid4")
  995. @patch("sentry.issues.merge.merge_groups")
  996. @patch("sentry.eventstream.backend")
  997. def test_merge(self, mock_eventstream, merge_groups, mock_uuid4):
  998. eventstream_state = object()
  999. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  1000. mock_uuid4.return_value = self.get_mock_uuid()
  1001. group1 = self.create_group(times_seen=1)
  1002. group2 = self.create_group(times_seen=50)
  1003. group3 = self.create_group(times_seen=2)
  1004. self.create_group()
  1005. self.login_as(user=self.user)
  1006. url = f"{self.path}?id={group1.id}&id={group2.id}&id={group3.id}"
  1007. response = self.client.put(url, data={"merge": "1"}, format="json")
  1008. assert response.status_code == 200
  1009. assert response.data["merge"]["parent"] == str(group2.id)
  1010. assert sorted(response.data["merge"]["children"]) == sorted(
  1011. [str(group1.id), str(group3.id)]
  1012. )
  1013. mock_eventstream.start_merge.assert_called_once_with(
  1014. group1.project_id, [group3.id, group1.id], group2.id
  1015. )
  1016. assert len(merge_groups.mock_calls) == 1
  1017. merge_groups.delay.assert_any_call(
  1018. from_object_ids=[group3.id, group1.id],
  1019. to_object_id=group2.id,
  1020. transaction_id="abc123",
  1021. eventstream_state=eventstream_state,
  1022. )
  1023. @patch("sentry.issues.merge.uuid4")
  1024. @patch("sentry.issues.merge.merge_groups")
  1025. @patch("sentry.eventstream.backend")
  1026. def test_merge_performance_issues(self, mock_eventstream, merge_groups, mock_uuid4):
  1027. eventstream_state = object()
  1028. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  1029. mock_uuid4.return_value = self.get_mock_uuid()
  1030. group1 = self.create_group(times_seen=1, type=PerformanceSlowDBQueryGroupType.type_id)
  1031. group2 = self.create_group(times_seen=50, type=PerformanceSlowDBQueryGroupType.type_id)
  1032. group3 = self.create_group(times_seen=2, type=PerformanceSlowDBQueryGroupType.type_id)
  1033. self.create_group()
  1034. self.login_as(user=self.user)
  1035. url = f"{self.path}?id={group1.id}&id={group2.id}&id={group3.id}"
  1036. response = self.client.put(url, data={"merge": "1"}, format="json")
  1037. assert response.status_code == 400, response.content
  1038. def test_assign(self):
  1039. group1 = self.create_group(is_public=True)
  1040. group2 = self.create_group(is_public=True)
  1041. user = self.user
  1042. self.login_as(user=user)
  1043. url = f"{self.path}?id={group1.id}"
  1044. response = self.client.put(url, data={"assignedTo": user.username})
  1045. assert response.status_code == 200
  1046. assert response.data["assignedTo"]["id"] == str(user.id)
  1047. assert response.data["assignedTo"]["type"] == "user"
  1048. assert GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  1049. assert not GroupAssignee.objects.filter(group=group2, user_id=user.id).exists()
  1050. assert (
  1051. Activity.objects.filter(
  1052. group=group1, user_id=user.id, type=ActivityType.ASSIGNED.value
  1053. ).count()
  1054. == 1
  1055. )
  1056. assert GroupSubscription.objects.filter(
  1057. user_id=user.id, group=group1, is_active=True
  1058. ).exists()
  1059. response = self.client.put(url, data={"assignedTo": ""}, format="json")
  1060. assert response.status_code == 200, response.content
  1061. assert response.data["assignedTo"] is None
  1062. assert not GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  1063. def test_assign_non_member(self):
  1064. group = self.create_group(is_public=True)
  1065. member = self.user
  1066. non_member = self.create_user("bar@example.com")
  1067. self.login_as(user=member)
  1068. url = f"{self.path}?id={group.id}"
  1069. response = self.client.put(url, data={"assignedTo": non_member.username}, format="json")
  1070. assert response.status_code == 400, response.content
  1071. def test_assign_team(self):
  1072. self.login_as(user=self.user)
  1073. group = self.create_group()
  1074. other_member = self.create_user("bar@example.com")
  1075. team = self.create_team(
  1076. organization=group.project.organization, members=[self.user, other_member]
  1077. )
  1078. group.project.add_team(team)
  1079. url = f"{self.path}?id={group.id}"
  1080. response = self.client.put(url, data={"assignedTo": f"team:{team.id}"})
  1081. assert response.status_code == 200
  1082. assert response.data["assignedTo"]["id"] == str(team.id)
  1083. assert response.data["assignedTo"]["type"] == "team"
  1084. assert GroupAssignee.objects.filter(group=group, team=team).exists()
  1085. assert Activity.objects.filter(group=group, type=ActivityType.ASSIGNED.value).count() == 1
  1086. assert GroupSubscription.objects.filter(group=group, is_active=True).count() == 2
  1087. response = self.client.put(url, data={"assignedTo": ""}, format="json")
  1088. assert response.status_code == 200, response.content
  1089. assert response.data["assignedTo"] is None
  1090. def test_discard(self):
  1091. group1 = self.create_group(is_public=True)
  1092. group2 = self.create_group(is_public=True)
  1093. group_hash = GroupHash.objects.create(hash="x" * 32, project=group1.project, group=group1)
  1094. user = self.user
  1095. self.login_as(user=user)
  1096. url = f"{self.path}?id={group1.id}"
  1097. with self.tasks():
  1098. with self.feature("projects:discard-groups"):
  1099. response = self.client.put(url, data={"discard": True})
  1100. assert response.status_code == 204
  1101. assert not Group.objects.filter(id=group1.id).exists()
  1102. assert Group.objects.filter(id=group2.id).exists()
  1103. assert GroupHash.objects.filter(id=group_hash.id).exists()
  1104. tombstone = GroupTombstone.objects.get(
  1105. id=GroupHash.objects.get(id=group_hash.id).group_tombstone_id
  1106. )
  1107. assert tombstone.message == group1.message
  1108. assert tombstone.culprit == group1.culprit
  1109. assert tombstone.project == group1.project
  1110. assert tombstone.data == group1.data
  1111. @patch(
  1112. "sentry.models.OrganizationMember.get_scopes",
  1113. return_value=frozenset(s for s in settings.SENTRY_SCOPES if s != "event:admin"),
  1114. )
  1115. def test_discard_requires_events_admin(self, mock_get_scopes):
  1116. group1 = self.create_group(is_public=True)
  1117. user = self.user
  1118. self.login_as(user=user)
  1119. url = f"{self.path}?id={group1.id}"
  1120. with self.tasks(), self.feature("projects:discard-groups"):
  1121. response = self.client.put(url, data={"discard": True})
  1122. assert response.status_code == 400
  1123. assert Group.objects.filter(id=group1.id).exists()
  1124. class GroupDeleteTest(APITestCase, SnubaTestCase):
  1125. @cached_property
  1126. def path(self):
  1127. return f"/api/0/projects/{self.project.organization.slug}/{self.project.slug}/issues/"
  1128. @patch("sentry.eventstream.backend")
  1129. def test_delete_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(status=GroupStatus.RESOLVED)
  1133. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  1134. group3 = self.create_group(status=GroupStatus.IGNORED)
  1135. group4 = self.create_group(
  1136. project=self.create_project(slug="foo"),
  1137. status=GroupStatus.UNRESOLVED,
  1138. )
  1139. hashes = []
  1140. for g in group1, group2, group3, group4:
  1141. hash = uuid4().hex
  1142. hashes.append(hash)
  1143. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  1144. self.login_as(user=self.user)
  1145. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  1146. response = self.client.delete(url, format="json")
  1147. mock_eventstream.start_delete_groups.assert_called_once_with(
  1148. group1.project_id, [group1.id, group2.id]
  1149. )
  1150. assert response.status_code == 204
  1151. assert Group.objects.get(id=group1.id).status == GroupStatus.PENDING_DELETION
  1152. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  1153. assert Group.objects.get(id=group2.id).status == GroupStatus.PENDING_DELETION
  1154. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  1155. assert Group.objects.get(id=group3.id).status != GroupStatus.PENDING_DELETION
  1156. assert GroupHash.objects.filter(group_id=group3.id).exists()
  1157. assert Group.objects.get(id=group4.id).status != GroupStatus.PENDING_DELETION
  1158. assert GroupHash.objects.filter(group_id=group4.id).exists()
  1159. Group.objects.filter(id__in=(group1.id, group2.id)).update(status=GroupStatus.UNRESOLVED)
  1160. with self.tasks():
  1161. response = self.client.delete(url, format="json")
  1162. # XXX(markus): Something is sending duplicated replacements to snuba --
  1163. # once from within tasks.deletions.groups and another time from
  1164. # sentry.deletions.defaults.groups
  1165. assert mock_eventstream.end_delete_groups.call_args_list == [
  1166. call(eventstream_state),
  1167. call(eventstream_state),
  1168. ]
  1169. assert response.status_code == 204
  1170. assert not Group.objects.filter(id=group1.id).exists()
  1171. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  1172. assert not Group.objects.filter(id=group2.id).exists()
  1173. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  1174. assert Group.objects.filter(id=group3.id).exists()
  1175. assert GroupHash.objects.filter(group_id=group3.id).exists()
  1176. assert Group.objects.filter(id=group4.id).exists()
  1177. assert GroupHash.objects.filter(group_id=group4.id).exists()
  1178. @patch("sentry.eventstream.backend")
  1179. def test_delete_performance_issue_by_id(self, mock_eventstream):
  1180. eventstream_state = {"event_stream_state": uuid4()}
  1181. mock_eventstream.start_delete_groups = Mock(return_value=eventstream_state)
  1182. group1 = self.create_group(
  1183. status=GroupStatus.RESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  1184. )
  1185. group2 = self.create_group(
  1186. status=GroupStatus.UNRESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  1187. )
  1188. hashes = []
  1189. for g in group1, group2:
  1190. hash = uuid4().hex
  1191. hashes.append(hash)
  1192. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  1193. self.login_as(user=self.user)
  1194. url = f"{self.path}?id={group1.id}&id={group2.id}"
  1195. response = self.client.delete(url, format="json")
  1196. assert response.status_code == 400
  1197. assert Group.objects.filter(id=group1.id).exists()
  1198. assert GroupHash.objects.filter(group_id=group1.id).exists()
  1199. assert Group.objects.filter(id=group2.id).exists()
  1200. assert GroupHash.objects.filter(group_id=group2.id).exists()
  1201. def test_bulk_delete(self):
  1202. groups = []
  1203. for i in range(10, 41):
  1204. groups.append(
  1205. self.create_group(
  1206. project=self.project,
  1207. status=GroupStatus.RESOLVED,
  1208. )
  1209. )
  1210. hashes = []
  1211. for group in groups:
  1212. hash = uuid4().hex
  1213. hashes.append(hash)
  1214. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  1215. self.login_as(user=self.user)
  1216. # if query is '' it defaults to is:unresolved
  1217. url = self.path + "?query="
  1218. response = self.client.delete(url, format="json")
  1219. assert response.status_code == 204
  1220. for group in groups:
  1221. assert Group.objects.get(id=group.id).status == GroupStatus.PENDING_DELETION
  1222. assert not GroupHash.objects.filter(group_id=group.id).exists()
  1223. Group.objects.filter(id__in=[group.id for group in groups]).update(
  1224. status=GroupStatus.UNRESOLVED
  1225. )
  1226. with self.tasks():
  1227. response = self.client.delete(url, format="json")
  1228. assert response.status_code == 204
  1229. for group in groups:
  1230. assert not Group.objects.filter(id=group.id).exists()
  1231. assert not GroupHash.objects.filter(group_id=group.id).exists()
  1232. def test_bulk_delete_performance_issues(self):
  1233. groups = []
  1234. for i in range(10, 41):
  1235. groups.append(
  1236. self.create_group(
  1237. project=self.project,
  1238. status=GroupStatus.RESOLVED,
  1239. type=PerformanceSlowDBQueryGroupType.type_id,
  1240. )
  1241. )
  1242. hashes = []
  1243. for group in groups:
  1244. hash = uuid4().hex
  1245. hashes.append(hash)
  1246. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  1247. self.login_as(user=self.user)
  1248. # if query is '' it defaults to is:unresolved
  1249. url = self.path + "?query="
  1250. response = self.client.delete(url, format="json")
  1251. assert response.status_code == 400
  1252. for group in groups:
  1253. assert Group.objects.filter(id=group.id).exists()
  1254. assert GroupHash.objects.filter(group_id=group.id).exists()