test_project_group_index.py 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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, with_feature
  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. @with_feature("organizations:resolve-in-upcoming-release")
  644. def test_set_resolved_in_upcoming_release(self):
  645. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  646. release.add_project(self.project)
  647. group = self.create_group(status=GroupStatus.UNRESOLVED)
  648. self.login_as(user=self.user)
  649. url = f"{self.path}?id={group.id}"
  650. response = self.client.put(
  651. url,
  652. data={"status": "resolved", "statusDetails": {"inUpcomingRelease": True}},
  653. format="json",
  654. )
  655. assert response.status_code == 200
  656. assert response.data["status"] == "resolved"
  657. assert response.data["statusDetails"]["inUpcomingRelease"]
  658. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  659. assert "activity" in response.data
  660. group = Group.objects.get(id=group.id)
  661. assert group.status == GroupStatus.RESOLVED
  662. resolution = GroupResolution.objects.get(group=group)
  663. assert resolution.release == release
  664. assert resolution.type == GroupResolution.Type.in_upcoming_release
  665. assert resolution.status == GroupResolution.Status.pending
  666. assert resolution.actor_id == self.user.id
  667. assert GroupSubscription.objects.filter(
  668. user_id=self.user.id, group=group, is_active=True
  669. ).exists()
  670. activity = Activity.objects.get(
  671. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  672. )
  673. assert activity.data["version"] == ""
  674. def test_upcoming_release_flag_validation(self):
  675. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  676. release.add_project(self.project)
  677. group = self.create_group(status=GroupStatus.UNRESOLVED)
  678. self.login_as(user=self.user)
  679. url = f"{self.path}?id={group.id}"
  680. response = self.client.put(
  681. url,
  682. data={"status": "resolved", "statusDetails": {"inUpcomingRelease": True}},
  683. format="json",
  684. )
  685. assert response.status_code == 400
  686. assert (
  687. response.data["statusDetails"]["inUpcomingRelease"][0]
  688. == "Your organization does not have access to this feature."
  689. )
  690. @with_feature("organizations:resolve-in-upcoming-release")
  691. def test_upcoming_release_release_validation(self):
  692. group = self.create_group(status=GroupStatus.UNRESOLVED)
  693. self.login_as(user=self.user)
  694. url = f"{self.path}?id={group.id}"
  695. response = self.client.put(
  696. url,
  697. data={"status": "resolved", "statusDetails": {"inUpcomingRelease": True}},
  698. format="json",
  699. )
  700. assert response.status_code == 400
  701. assert (
  702. response.data["statusDetails"]["inUpcomingRelease"][0]
  703. == "No release data present in the system."
  704. )
  705. def test_set_resolved_in_explicit_commit_unreleased(self):
  706. repo = self.create_repo(project=self.project, name=self.project.name)
  707. commit = self.create_commit(project=self.project, repo=repo)
  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": commit.key, "repository": repo.name}},
  716. },
  717. format="json",
  718. )
  719. assert response.status_code == 200
  720. assert response.data["status"] == "resolved"
  721. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  722. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  723. group = Group.objects.get(id=group.id)
  724. assert group.status == GroupStatus.RESOLVED
  725. link = GroupLink.objects.get(group_id=group.id)
  726. assert link.linked_type == GroupLink.LinkedType.commit
  727. assert link.relationship == GroupLink.Relationship.resolves
  728. assert link.linked_id == commit.id
  729. assert GroupSubscription.objects.filter(
  730. user_id=self.user.id, group=group, is_active=True
  731. ).exists()
  732. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  733. assert activity.data["commit"] == commit.id
  734. def test_set_resolved_in_explicit_commit_released(self):
  735. release = self.create_release(project=self.project)
  736. repo = self.create_repo(project=self.project, name=self.project.name)
  737. commit = self.create_commit(project=self.project, repo=repo, release=release)
  738. group = self.create_group(status=GroupStatus.UNRESOLVED)
  739. self.login_as(user=self.user)
  740. url = f"{self.path}?id={group.id}"
  741. response = self.client.put(
  742. url,
  743. data={
  744. "status": "resolved",
  745. "statusDetails": {"inCommit": {"commit": commit.key, "repository": repo.name}},
  746. },
  747. format="json",
  748. )
  749. assert response.status_code == 200
  750. assert response.data["status"] == "resolved"
  751. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  752. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  753. group = Group.objects.get(id=group.id)
  754. assert group.status == GroupStatus.RESOLVED
  755. link = GroupLink.objects.get(group_id=group.id)
  756. assert link.project_id == self.project.id
  757. assert link.linked_type == GroupLink.LinkedType.commit
  758. assert link.relationship == GroupLink.Relationship.resolves
  759. assert link.linked_id == commit.id
  760. assert GroupSubscription.objects.filter(
  761. user_id=self.user.id, group=group, is_active=True
  762. ).exists()
  763. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  764. assert activity.data["commit"] == commit.id
  765. resolution = GroupResolution.objects.get(group=group)
  766. assert resolution.type == GroupResolution.Type.in_release
  767. assert resolution.status == GroupResolution.Status.resolved
  768. def test_set_resolved_in_explicit_commit_missing(self):
  769. repo = self.create_repo(project=self.project, name=self.project.name)
  770. group = self.create_group(status=GroupStatus.UNRESOLVED)
  771. self.login_as(user=self.user)
  772. url = f"{self.path}?id={group.id}"
  773. response = self.client.put(
  774. url,
  775. data={
  776. "status": "resolved",
  777. "statusDetails": {"inCommit": {"commit": "a" * 40, "repository": repo.name}},
  778. },
  779. format="json",
  780. )
  781. assert response.status_code == 400
  782. assert (
  783. response.data["statusDetails"]["inCommit"]["commit"][0]
  784. == "Unable to find the given commit."
  785. )
  786. def test_set_unresolved(self):
  787. release = self.create_release(project=self.project, version="abc")
  788. group = self.create_group(status=GroupStatus.RESOLVED)
  789. GroupResolution.objects.create(group=group, release=release)
  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. self.assertNoResolution(group)
  798. assert GroupSubscription.objects.filter(
  799. user_id=self.user.id, group=group, is_active=True
  800. ).exists()
  801. def test_set_unresolved_on_snooze(self):
  802. group = self.create_group(status=GroupStatus.IGNORED)
  803. GroupSnooze.objects.create(group=group, until=timezone.now() - timedelta(days=1))
  804. self.login_as(user=self.user)
  805. url = f"{self.path}?id={group.id}"
  806. response = self.client.put(url, data={"status": "unresolved"}, format="json")
  807. assert response.status_code == 200
  808. assert response.data == {"status": "unresolved", "statusDetails": {}}
  809. group = Group.objects.get(id=group.id)
  810. assert group.status == GroupStatus.UNRESOLVED
  811. def test_basic_ignore(self):
  812. group = self.create_group(status=GroupStatus.RESOLVED)
  813. snooze = GroupSnooze.objects.create(group=group, until=timezone.now())
  814. self.login_as(user=self.user)
  815. url = f"{self.path}?id={group.id}"
  816. response = self.client.put(url, data={"status": "ignored"}, format="json")
  817. assert response.status_code == 200
  818. # existing snooze objects should be cleaned up
  819. assert not GroupSnooze.objects.filter(id=snooze.id).exists()
  820. group = Group.objects.get(id=group.id)
  821. assert group.status == GroupStatus.IGNORED
  822. assert response.data == {"status": "ignored", "statusDetails": {}, "inbox": None}
  823. def test_snooze_duration(self):
  824. group = self.create_group(status=GroupStatus.RESOLVED)
  825. self.login_as(user=self.user)
  826. url = f"{self.path}?id={group.id}"
  827. response = self.client.put(
  828. url, data={"status": "ignored", "ignoreDuration": 30}, format="json"
  829. )
  830. assert response.status_code == 200
  831. snooze = GroupSnooze.objects.get(group=group)
  832. now = timezone.now()
  833. assert snooze.count is None
  834. assert snooze.until is not None
  835. assert snooze.until > now + timedelta(minutes=29)
  836. assert snooze.until < now + timedelta(minutes=31)
  837. assert snooze.user_count is None
  838. assert snooze.user_window is None
  839. assert snooze.window is None
  840. assert response.data["status"] == "ignored"
  841. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  842. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  843. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  844. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  845. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  846. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  847. def test_snooze_count(self):
  848. group = self.create_group(status=GroupStatus.RESOLVED, times_seen=1)
  849. self.login_as(user=self.user)
  850. url = f"{self.path}?id={group.id}"
  851. response = self.client.put(
  852. url, data={"status": "ignored", "ignoreCount": 100}, format="json"
  853. )
  854. assert response.status_code == 200
  855. snooze = GroupSnooze.objects.get(group=group)
  856. assert snooze.count == 100
  857. assert snooze.until is None
  858. assert snooze.user_count is None
  859. assert snooze.user_window is None
  860. assert snooze.window is None
  861. assert snooze.state["times_seen"] == 1
  862. assert response.data["status"] == "ignored"
  863. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  864. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  865. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  866. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  867. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  868. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  869. def test_snooze_user_count(self):
  870. for i in range(10):
  871. event = self.store_event(
  872. data={
  873. "fingerprint": ["put-me-in-group-1"],
  874. "user": {"id": str(i)},
  875. "timestamp": iso_format(self.min_ago + timedelta(seconds=i)),
  876. },
  877. project_id=self.project.id,
  878. )
  879. group = Group.objects.get(id=event.group.id)
  880. group.status = GroupStatus.RESOLVED
  881. group.substatus = None
  882. group.save()
  883. self.login_as(user=self.user)
  884. url = f"{self.path}?id={group.id}"
  885. response = self.client.put(
  886. url, data={"status": "ignored", "ignoreUserCount": 10}, format="json"
  887. )
  888. assert response.status_code == 200
  889. snooze = GroupSnooze.objects.get(group=group)
  890. assert snooze.count is None
  891. assert snooze.until is None
  892. assert snooze.user_count == 10
  893. assert snooze.user_window is None
  894. assert snooze.window is None
  895. assert snooze.state["users_seen"] == 10
  896. assert response.data["status"] == "ignored"
  897. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  898. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  899. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  900. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  901. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  902. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  903. def test_set_bookmarked(self):
  904. group1 = self.create_group(status=GroupStatus.RESOLVED)
  905. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  906. group3 = self.create_group(status=GroupStatus.IGNORED)
  907. group4 = self.create_group(
  908. project=self.create_project(slug="foo"),
  909. status=GroupStatus.UNRESOLVED,
  910. )
  911. self.login_as(user=self.user)
  912. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  913. response = self.client.put(url, data={"isBookmarked": "true"}, format="json")
  914. assert response.status_code == 200
  915. assert response.data == {"isBookmarked": True}
  916. bookmark1 = GroupBookmark.objects.filter(group=group1, user_id=self.user.id)
  917. assert bookmark1.exists()
  918. assert GroupSubscription.objects.filter(
  919. user_id=self.user.id, group=group1, is_active=True
  920. ).exists()
  921. bookmark2 = GroupBookmark.objects.filter(group=group2, user_id=self.user.id)
  922. assert bookmark2.exists()
  923. assert GroupSubscription.objects.filter(
  924. user_id=self.user.id, group=group2, is_active=True
  925. ).exists()
  926. bookmark3 = GroupBookmark.objects.filter(group=group3, user_id=self.user.id)
  927. assert not bookmark3.exists()
  928. bookmark4 = GroupBookmark.objects.filter(group=group4, user_id=self.user.id)
  929. assert not bookmark4.exists()
  930. def test_subscription(self):
  931. group1 = self.create_group()
  932. group2 = self.create_group()
  933. group3 = self.create_group()
  934. group4 = self.create_group(project=self.create_project(slug="foo"))
  935. self.login_as(user=self.user)
  936. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  937. response = self.client.put(url, data={"isSubscribed": "true"}, format="json")
  938. assert response.status_code == 200
  939. assert response.data == {"isSubscribed": True, "subscriptionDetails": {"reason": "unknown"}}
  940. assert GroupSubscription.objects.filter(
  941. group=group1, user_id=self.user.id, is_active=True
  942. ).exists()
  943. assert GroupSubscription.objects.filter(
  944. group=group2, user_id=self.user.id, is_active=True
  945. ).exists()
  946. assert not GroupSubscription.objects.filter(group=group3, user_id=self.user.id).exists()
  947. assert not GroupSubscription.objects.filter(group=group4, user_id=self.user.id).exists()
  948. def test_set_public(self):
  949. group1 = self.create_group()
  950. group2 = self.create_group()
  951. self.login_as(user=self.user)
  952. url = f"{self.path}?id={group1.id}&id={group2.id}"
  953. response = self.client.put(url, data={"isPublic": "true"}, format="json")
  954. assert response.status_code == 200
  955. assert response.data["isPublic"] is True
  956. assert "shareId" in response.data
  957. new_group1 = Group.objects.get(id=group1.id)
  958. assert bool(new_group1.get_share_id())
  959. new_group2 = Group.objects.get(id=group2.id)
  960. assert bool(new_group2.get_share_id())
  961. def test_set_private(self):
  962. group1 = self.create_group()
  963. group2 = self.create_group()
  964. # Manually mark them as shared
  965. for g in group1, group2:
  966. GroupShare.objects.create(project_id=g.project_id, group=g)
  967. assert bool(g.get_share_id())
  968. self.login_as(user=self.user)
  969. url = f"{self.path}?id={group1.id}&id={group2.id}"
  970. response = self.client.put(url, data={"isPublic": "false"}, format="json")
  971. assert response.status_code == 200
  972. assert response.data == {"isPublic": False, "shareId": None}
  973. new_group1 = Group.objects.get(id=group1.id)
  974. assert not bool(new_group1.get_share_id())
  975. new_group2 = Group.objects.get(id=group2.id)
  976. assert not bool(new_group2.get_share_id())
  977. def test_set_has_seen(self):
  978. group1 = self.create_group(status=GroupStatus.RESOLVED)
  979. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  980. group3 = self.create_group(status=GroupStatus.IGNORED)
  981. group4 = self.create_group(
  982. project=self.create_project(slug="foo"),
  983. status=GroupStatus.UNRESOLVED,
  984. )
  985. self.login_as(user=self.user)
  986. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  987. response = self.client.put(url, data={"hasSeen": "true"}, format="json")
  988. assert response.status_code == 200
  989. assert response.data == {"hasSeen": True}
  990. r1 = GroupSeen.objects.filter(group=group1, user_id=self.user.id)
  991. assert r1.exists()
  992. r2 = GroupSeen.objects.filter(group=group2, user_id=self.user.id)
  993. assert r2.exists()
  994. r3 = GroupSeen.objects.filter(group=group3, user_id=self.user.id)
  995. assert not r3.exists()
  996. r4 = GroupSeen.objects.filter(group=group4, user_id=self.user.id)
  997. assert not r4.exists()
  998. def test_inbox_fields(self):
  999. group1 = self.create_group(status=GroupStatus.RESOLVED)
  1000. add_group_to_inbox(group1, GroupInboxReason.NEW)
  1001. self.login_as(user=self.user)
  1002. url = f"{self.path}?id={group1.id}"
  1003. response = self.client.put(url, data={"status": "resolved"}, format="json")
  1004. assert "inbox" in response.data
  1005. assert response.data["inbox"] is None
  1006. @patch("sentry.issues.merge.uuid4")
  1007. @patch("sentry.issues.merge.merge_groups")
  1008. @patch("sentry.eventstream.backend")
  1009. def test_merge(self, mock_eventstream, merge_groups, mock_uuid4):
  1010. eventstream_state = object()
  1011. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  1012. mock_uuid4.return_value = self.get_mock_uuid()
  1013. group1 = self.create_group(times_seen=1)
  1014. group2 = self.create_group(times_seen=50)
  1015. group3 = self.create_group(times_seen=2)
  1016. self.create_group()
  1017. self.login_as(user=self.user)
  1018. url = f"{self.path}?id={group1.id}&id={group2.id}&id={group3.id}"
  1019. response = self.client.put(url, data={"merge": "1"}, format="json")
  1020. assert response.status_code == 200
  1021. assert response.data["merge"]["parent"] == str(group2.id)
  1022. assert sorted(response.data["merge"]["children"]) == sorted(
  1023. [str(group1.id), str(group3.id)]
  1024. )
  1025. mock_eventstream.start_merge.assert_called_once_with(
  1026. group1.project_id, [group3.id, group1.id], group2.id
  1027. )
  1028. assert len(merge_groups.mock_calls) == 1
  1029. merge_groups.delay.assert_any_call(
  1030. from_object_ids=[group3.id, group1.id],
  1031. to_object_id=group2.id,
  1032. transaction_id="abc123",
  1033. eventstream_state=eventstream_state,
  1034. )
  1035. @patch("sentry.issues.merge.uuid4")
  1036. @patch("sentry.issues.merge.merge_groups")
  1037. @patch("sentry.eventstream.backend")
  1038. def test_merge_performance_issues(self, mock_eventstream, merge_groups, mock_uuid4):
  1039. eventstream_state = object()
  1040. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  1041. mock_uuid4.return_value = self.get_mock_uuid()
  1042. group1 = self.create_group(times_seen=1, type=PerformanceSlowDBQueryGroupType.type_id)
  1043. group2 = self.create_group(times_seen=50, type=PerformanceSlowDBQueryGroupType.type_id)
  1044. group3 = self.create_group(times_seen=2, type=PerformanceSlowDBQueryGroupType.type_id)
  1045. self.create_group()
  1046. self.login_as(user=self.user)
  1047. url = f"{self.path}?id={group1.id}&id={group2.id}&id={group3.id}"
  1048. response = self.client.put(url, data={"merge": "1"}, format="json")
  1049. assert response.status_code == 400, response.content
  1050. def test_assign(self):
  1051. group1 = self.create_group(is_public=True)
  1052. group2 = self.create_group(is_public=True)
  1053. user = self.user
  1054. self.login_as(user=user)
  1055. url = f"{self.path}?id={group1.id}"
  1056. response = self.client.put(url, data={"assignedTo": user.username})
  1057. assert response.status_code == 200
  1058. assert response.data["assignedTo"]["id"] == str(user.id)
  1059. assert response.data["assignedTo"]["type"] == "user"
  1060. assert GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  1061. assert not GroupAssignee.objects.filter(group=group2, user_id=user.id).exists()
  1062. assert (
  1063. Activity.objects.filter(
  1064. group=group1, user_id=user.id, type=ActivityType.ASSIGNED.value
  1065. ).count()
  1066. == 1
  1067. )
  1068. assert GroupSubscription.objects.filter(
  1069. user_id=user.id, group=group1, is_active=True
  1070. ).exists()
  1071. response = self.client.put(url, data={"assignedTo": ""}, format="json")
  1072. assert response.status_code == 200, response.content
  1073. assert response.data["assignedTo"] is None
  1074. assert not GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  1075. def test_assign_non_member(self):
  1076. group = self.create_group(is_public=True)
  1077. member = self.user
  1078. non_member = self.create_user("bar@example.com")
  1079. self.login_as(user=member)
  1080. url = f"{self.path}?id={group.id}"
  1081. response = self.client.put(url, data={"assignedTo": non_member.username}, format="json")
  1082. assert response.status_code == 400, response.content
  1083. def test_assign_team(self):
  1084. self.login_as(user=self.user)
  1085. group = self.create_group()
  1086. other_member = self.create_user("bar@example.com")
  1087. team = self.create_team(
  1088. organization=group.project.organization, members=[self.user, other_member]
  1089. )
  1090. group.project.add_team(team)
  1091. url = f"{self.path}?id={group.id}"
  1092. response = self.client.put(url, data={"assignedTo": f"team:{team.id}"})
  1093. assert response.status_code == 200
  1094. assert response.data["assignedTo"]["id"] == str(team.id)
  1095. assert response.data["assignedTo"]["type"] == "team"
  1096. assert GroupAssignee.objects.filter(group=group, team=team).exists()
  1097. assert Activity.objects.filter(group=group, type=ActivityType.ASSIGNED.value).count() == 1
  1098. assert GroupSubscription.objects.filter(group=group, is_active=True).count() == 2
  1099. response = self.client.put(url, data={"assignedTo": ""}, format="json")
  1100. assert response.status_code == 200, response.content
  1101. assert response.data["assignedTo"] is None
  1102. def test_discard(self):
  1103. group1 = self.create_group(is_public=True)
  1104. group2 = self.create_group(is_public=True)
  1105. group_hash = GroupHash.objects.create(hash="x" * 32, project=group1.project, group=group1)
  1106. user = self.user
  1107. self.login_as(user=user)
  1108. url = f"{self.path}?id={group1.id}"
  1109. with self.tasks():
  1110. with self.feature("projects:discard-groups"):
  1111. response = self.client.put(url, data={"discard": True})
  1112. assert response.status_code == 204
  1113. assert not Group.objects.filter(id=group1.id).exists()
  1114. assert Group.objects.filter(id=group2.id).exists()
  1115. assert GroupHash.objects.filter(id=group_hash.id).exists()
  1116. tombstone = GroupTombstone.objects.get(
  1117. id=GroupHash.objects.get(id=group_hash.id).group_tombstone_id
  1118. )
  1119. assert tombstone.message == group1.message
  1120. assert tombstone.culprit == group1.culprit
  1121. assert tombstone.project == group1.project
  1122. assert tombstone.data == group1.data
  1123. @patch(
  1124. "sentry.models.OrganizationMember.get_scopes",
  1125. return_value=frozenset(s for s in settings.SENTRY_SCOPES if s != "event:admin"),
  1126. )
  1127. def test_discard_requires_events_admin(self, mock_get_scopes):
  1128. group1 = self.create_group(is_public=True)
  1129. user = self.user
  1130. self.login_as(user=user)
  1131. url = f"{self.path}?id={group1.id}"
  1132. with self.tasks(), self.feature("projects:discard-groups"):
  1133. response = self.client.put(url, data={"discard": True})
  1134. assert response.status_code == 400
  1135. assert Group.objects.filter(id=group1.id).exists()
  1136. class GroupDeleteTest(APITestCase, SnubaTestCase):
  1137. @cached_property
  1138. def path(self):
  1139. return f"/api/0/projects/{self.project.organization.slug}/{self.project.slug}/issues/"
  1140. @patch("sentry.eventstream.backend")
  1141. def test_delete_by_id(self, mock_eventstream):
  1142. eventstream_state = {"event_stream_state": uuid4()}
  1143. mock_eventstream.start_delete_groups = Mock(return_value=eventstream_state)
  1144. group1 = self.create_group(status=GroupStatus.RESOLVED)
  1145. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  1146. group3 = self.create_group(status=GroupStatus.IGNORED)
  1147. group4 = self.create_group(
  1148. project=self.create_project(slug="foo"),
  1149. status=GroupStatus.UNRESOLVED,
  1150. )
  1151. hashes = []
  1152. for g in group1, group2, group3, group4:
  1153. hash = uuid4().hex
  1154. hashes.append(hash)
  1155. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  1156. self.login_as(user=self.user)
  1157. url = f"{self.path}?id={group1.id}&id={group2.id}&group4={group4.id}"
  1158. response = self.client.delete(url, format="json")
  1159. mock_eventstream.start_delete_groups.assert_called_once_with(
  1160. group1.project_id, [group1.id, group2.id]
  1161. )
  1162. assert response.status_code == 204
  1163. assert Group.objects.get(id=group1.id).status == GroupStatus.PENDING_DELETION
  1164. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  1165. assert Group.objects.get(id=group2.id).status == GroupStatus.PENDING_DELETION
  1166. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  1167. assert Group.objects.get(id=group3.id).status != GroupStatus.PENDING_DELETION
  1168. assert GroupHash.objects.filter(group_id=group3.id).exists()
  1169. assert Group.objects.get(id=group4.id).status != GroupStatus.PENDING_DELETION
  1170. assert GroupHash.objects.filter(group_id=group4.id).exists()
  1171. Group.objects.filter(id__in=(group1.id, group2.id)).update(status=GroupStatus.UNRESOLVED)
  1172. with self.tasks():
  1173. response = self.client.delete(url, format="json")
  1174. # XXX(markus): Something is sending duplicated replacements to snuba --
  1175. # once from within tasks.deletions.groups and another time from
  1176. # sentry.deletions.defaults.groups
  1177. assert mock_eventstream.end_delete_groups.call_args_list == [
  1178. call(eventstream_state),
  1179. call(eventstream_state),
  1180. ]
  1181. assert response.status_code == 204
  1182. assert not Group.objects.filter(id=group1.id).exists()
  1183. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  1184. assert not Group.objects.filter(id=group2.id).exists()
  1185. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  1186. assert Group.objects.filter(id=group3.id).exists()
  1187. assert GroupHash.objects.filter(group_id=group3.id).exists()
  1188. assert Group.objects.filter(id=group4.id).exists()
  1189. assert GroupHash.objects.filter(group_id=group4.id).exists()
  1190. @patch("sentry.eventstream.backend")
  1191. def test_delete_performance_issue_by_id(self, mock_eventstream):
  1192. eventstream_state = {"event_stream_state": uuid4()}
  1193. mock_eventstream.start_delete_groups = Mock(return_value=eventstream_state)
  1194. group1 = self.create_group(
  1195. status=GroupStatus.RESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  1196. )
  1197. group2 = self.create_group(
  1198. status=GroupStatus.UNRESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  1199. )
  1200. hashes = []
  1201. for g in group1, group2:
  1202. hash = uuid4().hex
  1203. hashes.append(hash)
  1204. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  1205. self.login_as(user=self.user)
  1206. url = f"{self.path}?id={group1.id}&id={group2.id}"
  1207. response = self.client.delete(url, format="json")
  1208. assert response.status_code == 400
  1209. assert Group.objects.filter(id=group1.id).exists()
  1210. assert GroupHash.objects.filter(group_id=group1.id).exists()
  1211. assert Group.objects.filter(id=group2.id).exists()
  1212. assert GroupHash.objects.filter(group_id=group2.id).exists()
  1213. def test_bulk_delete(self):
  1214. groups = []
  1215. for i in range(10, 41):
  1216. groups.append(
  1217. self.create_group(
  1218. project=self.project,
  1219. status=GroupStatus.RESOLVED,
  1220. )
  1221. )
  1222. hashes = []
  1223. for group in groups:
  1224. hash = uuid4().hex
  1225. hashes.append(hash)
  1226. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  1227. self.login_as(user=self.user)
  1228. # if query is '' it defaults to is:unresolved
  1229. url = self.path + "?query="
  1230. response = self.client.delete(url, format="json")
  1231. assert response.status_code == 204
  1232. for group in groups:
  1233. assert Group.objects.get(id=group.id).status == GroupStatus.PENDING_DELETION
  1234. assert not GroupHash.objects.filter(group_id=group.id).exists()
  1235. Group.objects.filter(id__in=[group.id for group in groups]).update(
  1236. status=GroupStatus.UNRESOLVED
  1237. )
  1238. with self.tasks():
  1239. response = self.client.delete(url, format="json")
  1240. assert response.status_code == 204
  1241. for group in groups:
  1242. assert not Group.objects.filter(id=group.id).exists()
  1243. assert not GroupHash.objects.filter(group_id=group.id).exists()
  1244. def test_bulk_delete_performance_issues(self):
  1245. groups = []
  1246. for i in range(10, 41):
  1247. groups.append(
  1248. self.create_group(
  1249. project=self.project,
  1250. status=GroupStatus.RESOLVED,
  1251. type=PerformanceSlowDBQueryGroupType.type_id,
  1252. )
  1253. )
  1254. hashes = []
  1255. for group in groups:
  1256. hash = uuid4().hex
  1257. hashes.append(hash)
  1258. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  1259. self.login_as(user=self.user)
  1260. # if query is '' it defaults to is:unresolved
  1261. url = self.path + "?query="
  1262. response = self.client.delete(url, format="json")
  1263. assert response.status_code == 400
  1264. for group in groups:
  1265. assert Group.objects.filter(id=group.id).exists()
  1266. assert GroupHash.objects.filter(group_id=group.id).exists()