test_organization_group_index.py 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559
  1. import functools
  2. from datetime import timedelta
  3. from unittest.mock import Mock, call, patch
  4. from uuid import uuid4
  5. from dateutil.parser import parse as parse_datetime
  6. from django.test import override_settings
  7. from django.urls import reverse
  8. from django.utils import timezone
  9. from freezegun import freeze_time
  10. from rest_framework import status
  11. from sentry import options
  12. from sentry.issues.grouptype import PerformanceNPlusOneGroupType, PerformanceSlowDBQueryGroupType
  13. from sentry.models import (
  14. GROUP_OWNER_TYPE,
  15. Activity,
  16. ApiToken,
  17. ExternalIssue,
  18. Group,
  19. GroupAssignee,
  20. GroupBookmark,
  21. GroupHash,
  22. GroupHistory,
  23. GroupInbox,
  24. GroupInboxReason,
  25. GroupLink,
  26. GroupOwner,
  27. GroupOwnerType,
  28. GroupResolution,
  29. GroupSeen,
  30. GroupShare,
  31. GroupSnooze,
  32. GroupStatus,
  33. GroupSubscription,
  34. GroupTombstone,
  35. Integration,
  36. OrganizationIntegration,
  37. Release,
  38. ReleaseStages,
  39. UserOption,
  40. add_group_to_inbox,
  41. remove_group_from_inbox,
  42. )
  43. from sentry.models.grouphistory import GroupHistoryStatus, record_group_history
  44. from sentry.search.events.constants import (
  45. RELEASE_STAGE_ALIAS,
  46. SEMVER_ALIAS,
  47. SEMVER_BUILD_ALIAS,
  48. SEMVER_PACKAGE_ALIAS,
  49. )
  50. from sentry.silo import SiloMode
  51. from sentry.testutils.cases import APITestCase, SnubaTestCase
  52. from sentry.testutils.helpers import parse_link_header
  53. from sentry.testutils.helpers.datetime import before_now, iso_format
  54. from sentry.testutils.helpers.features import Feature, with_feature
  55. from sentry.testutils.silo import assume_test_silo_mode, region_silo_test
  56. from sentry.types.activity import ActivityType
  57. from sentry.types.group import GroupSubStatus
  58. from sentry.utils import json
  59. @region_silo_test(stable=True)
  60. class GroupListTest(APITestCase, SnubaTestCase):
  61. endpoint = "sentry-api-0-organization-group-index"
  62. def setUp(self):
  63. super().setUp()
  64. self.min_ago = before_now(minutes=1)
  65. def _parse_links(self, header):
  66. # links come in {url: {...attrs}}, but we need {rel: {...attrs}}
  67. links = {}
  68. for url, attrs in parse_link_header(header).items():
  69. links[attrs["rel"]] = attrs
  70. attrs["href"] = url
  71. return links
  72. def get_response(self, *args, **kwargs):
  73. if not args:
  74. org = self.project.organization.slug
  75. else:
  76. org = args[0]
  77. return super().get_response(org, **kwargs)
  78. def test_sort_by_date_with_tag(self):
  79. # XXX(dcramer): this tests a case where an ambiguous column name existed
  80. event = self.store_event(
  81. data={"event_id": "a" * 32, "timestamp": iso_format(before_now(seconds=1))},
  82. project_id=self.project.id,
  83. )
  84. group = event.group
  85. self.login_as(user=self.user)
  86. response = self.get_success_response(sort_by="date", query="is:unresolved")
  87. assert len(response.data) == 1
  88. assert response.data[0]["id"] == str(group.id)
  89. def test_query_for_archived(self):
  90. event = self.store_event(
  91. data={"event_id": "a" * 32, "timestamp": iso_format(before_now(seconds=1))},
  92. project_id=self.project.id,
  93. )
  94. group = event.group
  95. Group.objects.update_group_status(
  96. groups=[group],
  97. status=GroupStatus.IGNORED,
  98. substatus=None,
  99. activity_type=ActivityType.SET_IGNORED,
  100. )
  101. self.login_as(user=self.user)
  102. response = self.get_success_response(sort_by="date", query="is:archived")
  103. assert len(response.data) == 1
  104. assert response.data[0]["id"] == str(group.id)
  105. def test_sort_by_priority(self):
  106. group = self.store_event(
  107. data={
  108. "timestamp": iso_format(before_now(seconds=10)),
  109. "fingerprint": ["group-1"],
  110. },
  111. project_id=self.project.id,
  112. ).group
  113. self.store_event(
  114. data={
  115. "timestamp": iso_format(before_now(seconds=10)),
  116. "fingerprint": ["group-1"],
  117. },
  118. project_id=self.project.id,
  119. )
  120. self.store_event(
  121. data={
  122. "timestamp": iso_format(before_now(hours=13)),
  123. "fingerprint": ["group-1"],
  124. },
  125. project_id=self.project.id,
  126. )
  127. group_2 = self.store_event(
  128. data={
  129. "timestamp": iso_format(before_now(seconds=5)),
  130. "fingerprint": ["group-2"],
  131. },
  132. project_id=self.project.id,
  133. ).group
  134. self.store_event(
  135. data={
  136. "timestamp": iso_format(before_now(hours=13)),
  137. "fingerprint": ["group-2"],
  138. },
  139. project_id=self.project.id,
  140. )
  141. self.login_as(user=self.user)
  142. aggregate_kwargs: dict = {
  143. "log_level": "3",
  144. "has_stacktrace": "5",
  145. "relative_volume": "1",
  146. "event_halflife_hours": "4",
  147. "issue_halflife_hours": "4",
  148. "v2": "true",
  149. "norm": "False",
  150. }
  151. response = self.get_success_response(
  152. sort="priority",
  153. query="is:unresolved",
  154. limit=25,
  155. start=iso_format(before_now(days=1)),
  156. end=iso_format(before_now(seconds=1)),
  157. **aggregate_kwargs,
  158. )
  159. assert len(response.data) == 2
  160. assert [item["id"] for item in response.data] == [str(group.id), str(group_2.id)]
  161. def test_sort_by_inbox(self):
  162. group_1 = self.store_event(
  163. data={
  164. "event_id": "a" * 32,
  165. "timestamp": iso_format(before_now(seconds=1)),
  166. "fingerprint": ["group-1"],
  167. },
  168. project_id=self.project.id,
  169. ).group
  170. inbox_1 = add_group_to_inbox(group_1, GroupInboxReason.NEW)
  171. group_2 = self.store_event(
  172. data={
  173. "event_id": "a" * 32,
  174. "timestamp": iso_format(before_now(seconds=1)),
  175. "fingerprint": ["group-2"],
  176. },
  177. project_id=self.project.id,
  178. ).group
  179. inbox_2 = add_group_to_inbox(group_2, GroupInboxReason.NEW)
  180. inbox_2.update(date_added=inbox_1.date_added - timedelta(hours=1))
  181. self.login_as(user=self.user)
  182. response = self.get_success_response(
  183. sort="inbox", query="is:unresolved is:for_review", limit=1
  184. )
  185. assert len(response.data) == 1
  186. assert response.data[0]["id"] == str(group_1.id)
  187. header_links = parse_link_header(response["Link"])
  188. cursor = [link for link in header_links.values() if link["rel"] == "next"][0]["cursor"]
  189. response = self.get_response(
  190. sort="inbox", cursor=cursor, query="is:unresolved is:for_review", limit=1
  191. )
  192. assert [item["id"] for item in response.data] == [str(group_2.id)]
  193. def test_sort_by_inbox_me_or_none(self):
  194. group_1 = self.store_event(
  195. data={
  196. "event_id": "a" * 32,
  197. "timestamp": iso_format(before_now(seconds=1)),
  198. "fingerprint": ["group-1"],
  199. },
  200. project_id=self.project.id,
  201. ).group
  202. inbox_1 = add_group_to_inbox(group_1, GroupInboxReason.NEW)
  203. group_2 = self.store_event(
  204. data={
  205. "event_id": "b" * 32,
  206. "timestamp": iso_format(before_now(seconds=1)),
  207. "fingerprint": ["group-2"],
  208. },
  209. project_id=self.project.id,
  210. ).group
  211. inbox_2 = add_group_to_inbox(group_2, GroupInboxReason.NEW)
  212. inbox_2.update(date_added=inbox_1.date_added - timedelta(hours=1))
  213. GroupOwner.objects.create(
  214. group=group_2,
  215. project=self.project,
  216. organization=self.organization,
  217. type=GroupOwnerType.OWNERSHIP_RULE.value,
  218. user_id=self.user.id,
  219. )
  220. owner_by_other = self.store_event(
  221. data={
  222. "event_id": "c" * 32,
  223. "timestamp": iso_format(before_now(seconds=1)),
  224. "fingerprint": ["group-3"],
  225. },
  226. project_id=self.project.id,
  227. ).group
  228. inbox_3 = add_group_to_inbox(owner_by_other, GroupInboxReason.NEW)
  229. inbox_3.update(date_added=inbox_1.date_added - timedelta(hours=1))
  230. other_user = self.create_user()
  231. GroupOwner.objects.create(
  232. group=owner_by_other,
  233. project=self.project,
  234. organization=self.organization,
  235. type=GroupOwnerType.OWNERSHIP_RULE.value,
  236. user_id=other_user.id,
  237. )
  238. owned_me_assigned_to_other = self.store_event(
  239. data={
  240. "event_id": "d" * 32,
  241. "timestamp": iso_format(before_now(seconds=1)),
  242. "fingerprint": ["group-4"],
  243. },
  244. project_id=self.project.id,
  245. ).group
  246. inbox_4 = add_group_to_inbox(owned_me_assigned_to_other, GroupInboxReason.NEW)
  247. inbox_4.update(date_added=inbox_1.date_added - timedelta(hours=1))
  248. GroupAssignee.objects.assign(owned_me_assigned_to_other, other_user)
  249. GroupOwner.objects.create(
  250. group=owned_me_assigned_to_other,
  251. project=self.project,
  252. organization=self.organization,
  253. type=GroupOwnerType.OWNERSHIP_RULE.value,
  254. user_id=self.user.id,
  255. )
  256. unowned_assigned_to_other = self.store_event(
  257. data={
  258. "event_id": "e" * 32,
  259. "timestamp": iso_format(before_now(seconds=1)),
  260. "fingerprint": ["group-5"],
  261. },
  262. project_id=self.project.id,
  263. ).group
  264. inbox_5 = add_group_to_inbox(unowned_assigned_to_other, GroupInboxReason.NEW)
  265. inbox_5.update(date_added=inbox_1.date_added - timedelta(hours=1))
  266. GroupAssignee.objects.assign(unowned_assigned_to_other, other_user)
  267. self.login_as(user=self.user)
  268. response = self.get_success_response(
  269. sort="inbox",
  270. query="is:unresolved is:for_review assigned_or_suggested:[me, none]",
  271. limit=10,
  272. )
  273. assert [item["id"] for item in response.data] == [str(group_1.id), str(group_2.id)]
  274. def test_trace_search(self):
  275. event = self.store_event(
  276. data={
  277. "event_id": "a" * 32,
  278. "timestamp": iso_format(before_now(seconds=1)),
  279. "contexts": {
  280. "trace": {
  281. "parent_span_id": "8988cec7cc0779c1",
  282. "type": "trace",
  283. "op": "foobar",
  284. "trace_id": "a7d67cf796774551a95be6543cacd459",
  285. "span_id": "babaae0d4b7512d9",
  286. "status": "ok",
  287. }
  288. },
  289. },
  290. project_id=self.project.id,
  291. )
  292. self.login_as(user=self.user)
  293. response = self.get_success_response(
  294. sort_by="date", query="is:unresolved trace:a7d67cf796774551a95be6543cacd459"
  295. )
  296. assert len(response.data) == 1
  297. assert response.data[0]["id"] == str(event.group.id)
  298. def test_feature_gate(self):
  299. # ensure there are two or more projects
  300. self.create_project(organization=self.project.organization)
  301. self.login_as(user=self.user)
  302. response = self.get_response()
  303. assert response.status_code == 400
  304. assert response.data["detail"] == "You do not have the multi project stream feature enabled"
  305. with self.feature("organizations:global-views"):
  306. response = self.get_response()
  307. assert response.status_code == 200
  308. def test_replay_feature_gate(self):
  309. # allow replays to query for backend
  310. self.create_project(organization=self.project.organization)
  311. self.login_as(user=self.user)
  312. self.get_success_response(extra_headers={"HTTP_X-Sentry-Replay-Request": "1"})
  313. def test_with_all_projects(self):
  314. # ensure there are two or more projects
  315. self.create_project(organization=self.project.organization)
  316. self.login_as(user=self.user)
  317. with self.feature("organizations:global-views"):
  318. response = self.get_success_response(project_id=[-1])
  319. assert response.status_code == 200
  320. def test_boolean_search_feature_flag(self):
  321. self.login_as(user=self.user)
  322. response = self.get_response(sort_by="date", query="title:hello OR title:goodbye")
  323. assert response.status_code == 400
  324. assert (
  325. response.data["detail"]
  326. == 'Error parsing search query: Boolean statements containing "OR" or "AND" are not supported in this search'
  327. )
  328. response = self.get_response(sort_by="date", query="title:hello AND title:goodbye")
  329. assert response.status_code == 400
  330. assert (
  331. response.data["detail"]
  332. == 'Error parsing search query: Boolean statements containing "OR" or "AND" are not supported in this search'
  333. )
  334. def test_invalid_query(self):
  335. now = timezone.now()
  336. self.create_group(last_seen=now - timedelta(seconds=1))
  337. self.login_as(user=self.user)
  338. response = self.get_response(sort_by="date", query="timesSeen:>1t")
  339. assert response.status_code == 400
  340. assert "Invalid number" in response.data["detail"]
  341. def test_valid_numeric_query(self):
  342. now = timezone.now()
  343. self.create_group(last_seen=now - timedelta(seconds=1))
  344. self.login_as(user=self.user)
  345. response = self.get_response(sort_by="date", query="timesSeen:>1k")
  346. assert response.status_code == 200
  347. def test_invalid_sort_key(self):
  348. now = timezone.now()
  349. self.create_group(last_seen=now - timedelta(seconds=1))
  350. self.login_as(user=self.user)
  351. response = self.get_response(sort="meow", query="is:unresolved")
  352. assert response.status_code == 400
  353. def test_simple_pagination(self):
  354. event1 = self.store_event(
  355. data={"timestamp": iso_format(before_now(seconds=2)), "fingerprint": ["group-1"]},
  356. project_id=self.project.id,
  357. )
  358. group1 = event1.group
  359. event2 = self.store_event(
  360. data={"timestamp": iso_format(before_now(seconds=1)), "fingerprint": ["group-2"]},
  361. project_id=self.project.id,
  362. )
  363. group2 = event2.group
  364. self.login_as(user=self.user)
  365. response = self.get_success_response(sort_by="date", limit=1)
  366. assert len(response.data) == 1
  367. assert response.data[0]["id"] == str(group2.id)
  368. links = self._parse_links(response["Link"])
  369. assert links["previous"]["results"] == "false"
  370. assert links["next"]["results"] == "true"
  371. response = self.client.get(links["next"]["href"], format="json")
  372. assert response.status_code == 200
  373. assert len(response.data) == 1
  374. assert response.data[0]["id"] == str(group1.id)
  375. links = self._parse_links(response["Link"])
  376. assert links["previous"]["results"] == "true"
  377. assert links["next"]["results"] == "false"
  378. def test_stats_period(self):
  379. # TODO(dcramer): this test really only checks if validation happens
  380. # on groupStatsPeriod
  381. now = timezone.now()
  382. self.create_group(last_seen=now - timedelta(seconds=1))
  383. self.create_group(last_seen=now)
  384. self.login_as(user=self.user)
  385. self.get_success_response(groupStatsPeriod="24h")
  386. self.get_success_response(groupStatsPeriod="14d")
  387. self.get_success_response(groupStatsPeriod="")
  388. response = self.get_response(groupStatsPeriod="48h")
  389. assert response.status_code == 400
  390. def test_environment(self):
  391. self.store_event(
  392. data={
  393. "fingerprint": ["put-me-in-group1"],
  394. "timestamp": iso_format(self.min_ago),
  395. "environment": "production",
  396. },
  397. project_id=self.project.id,
  398. )
  399. self.store_event(
  400. data={
  401. "fingerprint": ["put-me-in-group2"],
  402. "timestamp": iso_format(self.min_ago),
  403. "environment": "staging",
  404. },
  405. project_id=self.project.id,
  406. )
  407. self.login_as(user=self.user)
  408. response = self.get_success_response(environment="production")
  409. assert len(response.data) == 1
  410. response = self.get_response(environment="garbage")
  411. assert response.status_code == 404
  412. def test_project(self):
  413. self.store_event(
  414. data={
  415. "fingerprint": ["put-me-in-group1"],
  416. "timestamp": iso_format(self.min_ago),
  417. "environment": "production",
  418. },
  419. project_id=self.project.id,
  420. )
  421. project = self.project
  422. self.login_as(user=self.user)
  423. response = self.get_success_response(query=f"project:{project.slug}")
  424. assert len(response.data) == 1
  425. def test_auto_resolved(self):
  426. project = self.project
  427. project.update_option("sentry:resolve_age", 1)
  428. self.store_event(
  429. data={"event_id": "a" * 32, "timestamp": iso_format(before_now(seconds=1))},
  430. project_id=project.id,
  431. )
  432. event2 = self.store_event(
  433. data={"event_id": "b" * 32, "timestamp": iso_format(before_now(seconds=1))},
  434. project_id=project.id,
  435. )
  436. group2 = event2.group
  437. self.login_as(user=self.user)
  438. response = self.get_success_response()
  439. assert len(response.data) == 1
  440. assert response.data[0]["id"] == str(group2.id)
  441. def test_perf_issue(self):
  442. perf_group = self.create_group(type=PerformanceNPlusOneGroupType.type_id)
  443. self.login_as(user=self.user)
  444. with self.feature(
  445. [
  446. "organizations:issue-search-allow-postgres-only-search",
  447. ]
  448. ):
  449. response = self.get_success_response(query="issue.category:performance")
  450. assert len(response.data) == 1
  451. assert response.data[0]["id"] == str(perf_group.id)
  452. def test_lookup_by_event_id(self):
  453. project = self.project
  454. project.update_option("sentry:resolve_age", 1)
  455. event_id = "c" * 32
  456. event = self.store_event(
  457. data={"event_id": event_id, "timestamp": iso_format(self.min_ago)},
  458. project_id=self.project.id,
  459. )
  460. self.login_as(user=self.user)
  461. response = self.get_success_response(query="c" * 32)
  462. assert response["X-Sentry-Direct-Hit"] == "1"
  463. assert len(response.data) == 1
  464. assert response.data[0]["id"] == str(event.group.id)
  465. assert response.data[0]["matchingEventId"] == event_id
  466. def test_lookup_by_event_id_incorrect_project_id(self):
  467. self.store_event(
  468. data={"event_id": "a" * 32, "timestamp": iso_format(self.min_ago)},
  469. project_id=self.project.id,
  470. )
  471. event_id = "b" * 32
  472. event = self.store_event(
  473. data={"event_id": event_id, "timestamp": iso_format(self.min_ago)},
  474. project_id=self.project.id,
  475. )
  476. other_project = self.create_project(teams=[self.team])
  477. user = self.create_user()
  478. self.create_member(organization=self.organization, teams=[self.team], user=user)
  479. self.login_as(user=user)
  480. with self.feature("organizations:global-views"):
  481. response = self.get_success_response(query=event_id, project=[other_project.id])
  482. assert response["X-Sentry-Direct-Hit"] == "1"
  483. assert len(response.data) == 1
  484. assert response.data[0]["id"] == str(event.group.id)
  485. assert response.data[0]["matchingEventId"] == event_id
  486. def test_lookup_by_event_id_with_whitespace(self):
  487. project = self.project
  488. project.update_option("sentry:resolve_age", 1)
  489. event_id = "c" * 32
  490. event = self.store_event(
  491. data={"event_id": event_id, "timestamp": iso_format(self.min_ago)},
  492. project_id=self.project.id,
  493. )
  494. self.login_as(user=self.user)
  495. response = self.get_success_response(query=" {} ".format("c" * 32))
  496. assert response["X-Sentry-Direct-Hit"] == "1"
  497. assert len(response.data) == 1
  498. assert response.data[0]["id"] == str(event.group.id)
  499. assert response.data[0]["matchingEventId"] == event_id
  500. def test_lookup_by_unknown_event_id(self):
  501. project = self.project
  502. project.update_option("sentry:resolve_age", 1)
  503. self.create_group()
  504. self.create_group()
  505. self.login_as(user=self.user)
  506. response = self.get_success_response(query="c" * 32)
  507. assert len(response.data) == 0
  508. def test_lookup_by_short_id(self):
  509. group = self.group
  510. short_id = group.qualified_short_id
  511. self.login_as(user=self.user)
  512. response = self.get_success_response(query=short_id, shortIdLookup=1)
  513. assert len(response.data) == 1
  514. def test_lookup_by_short_id_alias(self):
  515. event_id = "f" * 32
  516. group = self.store_event(
  517. data={"event_id": event_id, "timestamp": iso_format(before_now(seconds=1))},
  518. project_id=self.project.id,
  519. ).group
  520. short_id = group.qualified_short_id
  521. self.login_as(user=self.user)
  522. response = self.get_success_response(query=f"issue:{short_id}")
  523. assert len(response.data) == 1
  524. def test_lookup_by_multiple_short_id_alias(self):
  525. self.login_as(self.user)
  526. project = self.project
  527. project2 = self.create_project(name="baz", organization=project.organization)
  528. event = self.store_event(
  529. data={"timestamp": iso_format(before_now(seconds=2))},
  530. project_id=project.id,
  531. )
  532. event2 = self.store_event(
  533. data={"timestamp": iso_format(before_now(seconds=1))},
  534. project_id=project2.id,
  535. )
  536. with self.feature("organizations:global-views"):
  537. response = self.get_success_response(
  538. query=f"issue:[{event.group.qualified_short_id},{event2.group.qualified_short_id}]"
  539. )
  540. assert len(response.data) == 2
  541. def test_lookup_by_short_id_ignores_project_list(self):
  542. organization = self.create_organization()
  543. project = self.create_project(organization=organization)
  544. project2 = self.create_project(organization=organization)
  545. group = self.create_group(project=project2)
  546. user = self.create_user()
  547. self.create_member(organization=organization, user=user)
  548. short_id = group.qualified_short_id
  549. self.login_as(user=user)
  550. response = self.get_success_response(
  551. organization.slug, project=project.id, query=short_id, shortIdLookup=1
  552. )
  553. assert len(response.data) == 1
  554. def test_lookup_by_short_id_no_perms(self):
  555. organization = self.create_organization()
  556. project = self.create_project(organization=organization)
  557. group = self.create_group(project=project)
  558. user = self.create_user()
  559. self.create_member(organization=organization, user=user, has_global_access=False)
  560. short_id = group.qualified_short_id
  561. self.login_as(user=user)
  562. response = self.get_success_response(organization.slug, query=short_id, shortIdLookup=1)
  563. assert len(response.data) == 0
  564. def test_lookup_by_group_id(self):
  565. self.login_as(user=self.user)
  566. response = self.get_success_response(group=self.group.id)
  567. assert len(response.data) == 1
  568. assert response.data[0]["id"] == str(self.group.id)
  569. group_2 = self.create_group()
  570. response = self.get_success_response(group=[self.group.id, group_2.id])
  571. assert {g["id"] for g in response.data} == {str(self.group.id), str(group_2.id)}
  572. def test_lookup_by_group_id_no_perms(self):
  573. organization = self.create_organization()
  574. project = self.create_project(organization=organization)
  575. group = self.create_group(project=project)
  576. user = self.create_user()
  577. self.create_member(organization=organization, user=user, has_global_access=False)
  578. self.login_as(user=user)
  579. response = self.get_response(group=[group.id])
  580. assert response.status_code == 403
  581. def test_lookup_by_first_release(self):
  582. self.login_as(self.user)
  583. project = self.project
  584. project2 = self.create_project(name="baz", organization=project.organization)
  585. release = Release.objects.create(organization=project.organization, version="12345")
  586. release.add_project(project)
  587. release.add_project(project2)
  588. event = self.store_event(
  589. data={"release": release.version, "timestamp": iso_format(before_now(seconds=2))},
  590. project_id=project.id,
  591. )
  592. event2 = self.store_event(
  593. data={"release": release.version, "timestamp": iso_format(before_now(seconds=1))},
  594. project_id=project2.id,
  595. )
  596. with self.feature("organizations:global-views"):
  597. response = self.get_success_response(
  598. **{"query": 'first-release:"%s"' % release.version}
  599. )
  600. issues = json.loads(response.content)
  601. assert len(issues) == 2
  602. assert int(issues[0]["id"]) == event2.group.id
  603. assert int(issues[1]["id"]) == event.group.id
  604. def test_lookup_by_release(self):
  605. self.login_as(self.user)
  606. project = self.project
  607. release = Release.objects.create(organization=project.organization, version="12345")
  608. release.add_project(project)
  609. event = self.store_event(
  610. data={
  611. "timestamp": iso_format(before_now(seconds=1)),
  612. "tags": {"sentry:release": release.version},
  613. },
  614. project_id=project.id,
  615. )
  616. response = self.get_success_response(release=release.version)
  617. issues = json.loads(response.content)
  618. assert len(issues) == 1
  619. assert int(issues[0]["id"]) == event.group.id
  620. def test_lookup_by_release_wildcard(self):
  621. self.login_as(self.user)
  622. project = self.project
  623. release = Release.objects.create(organization=project.organization, version="12345")
  624. release.add_project(project)
  625. event = self.store_event(
  626. data={
  627. "timestamp": iso_format(before_now(seconds=1)),
  628. "tags": {"sentry:release": release.version},
  629. },
  630. project_id=project.id,
  631. )
  632. response = self.get_success_response(release=release.version[:3] + "*")
  633. issues = json.loads(response.content)
  634. assert len(issues) == 1
  635. assert int(issues[0]["id"]) == event.group.id
  636. def test_lookup_by_regressed_in_release(self):
  637. self.login_as(self.user)
  638. project = self.project
  639. release = self.create_release()
  640. event = self.store_event(
  641. data={
  642. "timestamp": iso_format(before_now(seconds=1)),
  643. "tags": {"sentry:release": release.version},
  644. },
  645. project_id=project.id,
  646. )
  647. record_group_history(event.group, GroupHistoryStatus.REGRESSED, release=release)
  648. response = self.get_success_response(query=f"regressed_in_release:{release.version}")
  649. issues = json.loads(response.content)
  650. assert [int(issue["id"]) for issue in issues] == [event.group.id]
  651. def test_pending_delete_pending_merge_excluded(self):
  652. events = []
  653. for i in "abcd":
  654. events.append(
  655. self.store_event(
  656. data={
  657. "event_id": i * 32,
  658. "fingerprint": [i],
  659. "timestamp": iso_format(self.min_ago),
  660. },
  661. project_id=self.project.id,
  662. )
  663. )
  664. events[0].group.update(status=GroupStatus.PENDING_DELETION, substatus=None)
  665. events[2].group.update(status=GroupStatus.DELETION_IN_PROGRESS, substatus=None)
  666. events[3].group.update(status=GroupStatus.PENDING_MERGE, substatus=None)
  667. self.login_as(user=self.user)
  668. response = self.get_success_response()
  669. assert len(response.data) == 1
  670. assert response.data[0]["id"] == str(events[1].group.id)
  671. def test_filters_based_on_retention(self):
  672. self.login_as(user=self.user)
  673. self.create_group(last_seen=timezone.now() - timedelta(days=2))
  674. with self.options({"system.event-retention-days": 1}):
  675. response = self.get_success_response()
  676. assert len(response.data) == 0
  677. def test_token_auth(self):
  678. with assume_test_silo_mode(SiloMode.CONTROL):
  679. token = ApiToken.objects.create(user=self.user, scope_list=["event:read"])
  680. response = self.client.get(
  681. reverse("sentry-api-0-organization-group-index", args=[self.project.organization.slug]),
  682. format="json",
  683. HTTP_AUTHORIZATION=f"Bearer {token.token}",
  684. )
  685. assert response.status_code == 200, response.content
  686. def test_date_range(self):
  687. with self.options({"system.event-retention-days": 2}):
  688. event = self.store_event(
  689. data={"timestamp": iso_format(before_now(hours=5))}, project_id=self.project.id
  690. )
  691. group = event.group
  692. self.login_as(user=self.user)
  693. response = self.get_success_response(statsPeriod="6h")
  694. assert len(response.data) == 1
  695. assert response.data[0]["id"] == str(group.id)
  696. response = self.get_success_response(statsPeriod="1h")
  697. assert len(response.data) == 0
  698. @patch("sentry.analytics.record")
  699. def test_advanced_search_errors(self, mock_record):
  700. self.login_as(user=self.user)
  701. response = self.get_response(sort_by="date", query="!has:user")
  702. assert response.status_code == 200, response.data
  703. assert not any(
  704. c[0][0] == "advanced_search.feature_gated" for c in mock_record.call_args_list
  705. )
  706. with self.feature({"organizations:advanced-search": False}):
  707. response = self.get_response(sort_by="date", query="!has:user")
  708. assert response.status_code == 400, response.data
  709. assert (
  710. "You need access to the advanced search feature to use negative "
  711. "search" == response.data["detail"]
  712. )
  713. mock_record.assert_called_with(
  714. "advanced_search.feature_gated",
  715. user_id=self.user.id,
  716. default_user_id=self.user.id,
  717. organization_id=self.organization.id,
  718. )
  719. # This seems like a random override, but this test needed a way to override
  720. # the orderby being sent to snuba for a certain call. This function has a simple
  721. # return value and can be used to set variables in the snuba payload.
  722. @patch("sentry.utils.snuba.get_query_params_to_update_for_projects")
  723. def test_assigned_to_pagination(self, patched_params_update):
  724. old_sample_size = options.get("snuba.search.hits-sample-size")
  725. assert options.set("snuba.search.hits-sample-size", 1)
  726. days = reversed(range(4))
  727. self.login_as(user=self.user)
  728. groups = []
  729. for day in days:
  730. patched_params_update.side_effect = [
  731. (self.organization.id, {"project": [self.project.id]})
  732. ]
  733. group = self.store_event(
  734. data={
  735. "timestamp": iso_format(before_now(days=day)),
  736. "fingerprint": [f"group-{day}"],
  737. },
  738. project_id=self.project.id,
  739. ).group
  740. groups.append(group)
  741. assigned_groups = groups[:2]
  742. for ag in assigned_groups:
  743. ag.update(
  744. status=GroupStatus.RESOLVED, resolved_at=before_now(seconds=5), substatus=None
  745. )
  746. GroupAssignee.objects.assign(ag, self.user)
  747. # This side_effect is meant to override the `calculate_hits` snuba query specifically.
  748. # If this test is failing it's because the -last_seen override is being applied to
  749. # different snuba query.
  750. def _my_patched_params(query_params, **kwargs):
  751. if query_params.aggregations == [
  752. ["uniq", "group_id", "total"],
  753. ["multiply(toUInt64(max(timestamp)), 1000)", "", "last_seen"],
  754. ]:
  755. return (
  756. self.organization.id,
  757. {"project": [self.project.id], "orderby": ["-last_seen"]},
  758. )
  759. else:
  760. return (self.organization.id, {"project": [self.project.id]})
  761. patched_params_update.side_effect = _my_patched_params
  762. response = self.get_response(limit=1, query=f"assigned:{self.user.email}")
  763. assert len(response.data) == 1
  764. assert response.data[0]["id"] == str(assigned_groups[1].id)
  765. header_links = parse_link_header(response["Link"])
  766. cursor = [link for link in header_links.values() if link["rel"] == "next"][0]["cursor"]
  767. response = self.get_response(limit=1, cursor=cursor, query=f"assigned:{self.user.email}")
  768. assert len(response.data) == 1
  769. assert response.data[0]["id"] == str(assigned_groups[0].id)
  770. assert options.set("snuba.search.hits-sample-size", old_sample_size)
  771. def test_assigned_me_none(self):
  772. self.login_as(user=self.user)
  773. groups = []
  774. for i in range(5):
  775. group = self.store_event(
  776. data={
  777. "timestamp": iso_format(before_now(minutes=10, days=i)),
  778. "fingerprint": [f"group-{i}"],
  779. },
  780. project_id=self.project.id,
  781. ).group
  782. groups.append(group)
  783. assigned_groups = groups[:2]
  784. for ag in assigned_groups:
  785. GroupAssignee.objects.assign(ag, self.user)
  786. response = self.get_response(limit=10, query="assigned:me")
  787. assert [row["id"] for row in response.data] == [str(g.id) for g in assigned_groups]
  788. response = self.get_response(limit=10, query="assigned:[me, none]")
  789. assert len(response.data) == 5
  790. GroupAssignee.objects.assign(assigned_groups[1], self.create_user("other@user.com"))
  791. response = self.get_response(limit=10, query="assigned:[me, none]")
  792. assert len(response.data) == 4
  793. def test_seen_stats(self):
  794. self.store_event(
  795. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  796. project_id=self.project.id,
  797. )
  798. before_now_300_seconds = iso_format(before_now(seconds=300))
  799. before_now_350_seconds = iso_format(before_now(seconds=350))
  800. event2 = self.store_event(
  801. data={"timestamp": before_now_300_seconds, "fingerprint": ["group-2"]},
  802. project_id=self.project.id,
  803. )
  804. group2 = event2.group
  805. group2.first_seen = before_now_350_seconds
  806. group2.times_seen = 55
  807. group2.save()
  808. before_now_250_seconds = iso_format(before_now(seconds=250))
  809. self.store_event(
  810. data={
  811. "timestamp": before_now_250_seconds,
  812. "fingerprint": ["group-2"],
  813. "tags": {"server": "example.com", "trace": "meow", "message": "foo"},
  814. },
  815. project_id=self.project.id,
  816. )
  817. self.store_event(
  818. data={
  819. "timestamp": iso_format(before_now(seconds=200)),
  820. "fingerprint": ["group-1"],
  821. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  822. },
  823. project_id=self.project.id,
  824. )
  825. before_now_150_seconds = iso_format(before_now(seconds=150))
  826. self.store_event(
  827. data={
  828. "timestamp": before_now_150_seconds,
  829. "fingerprint": ["group-2"],
  830. "tags": {"trace": "ribbit", "server": "example.com"},
  831. },
  832. project_id=self.project.id,
  833. )
  834. before_now_100_seconds = iso_format(before_now(seconds=100))
  835. self.store_event(
  836. data={
  837. "timestamp": before_now_100_seconds,
  838. "fingerprint": ["group-2"],
  839. "tags": {"message": "foo", "trace": "meow"},
  840. },
  841. project_id=self.project.id,
  842. )
  843. self.login_as(user=self.user)
  844. response = self.get_response(sort_by="date", limit=10, query="server:example.com")
  845. assert response.status_code == 200
  846. assert len(response.data) == 2
  847. assert int(response.data[0]["id"]) == group2.id
  848. assert response.data[0]["lifetime"] is not None
  849. assert response.data[0]["filtered"] is not None
  850. assert response.data[0]["filtered"]["stats"] is not None
  851. assert response.data[0]["lifetime"]["stats"] is None
  852. assert response.data[0]["filtered"]["stats"] != response.data[0]["stats"]
  853. assert response.data[0]["lifetime"]["firstSeen"] == parse_datetime(
  854. before_now_350_seconds # Should match overridden value, not event value
  855. ).replace(tzinfo=timezone.utc)
  856. assert response.data[0]["lifetime"]["lastSeen"] == parse_datetime(
  857. before_now_100_seconds
  858. ).replace(tzinfo=timezone.utc)
  859. assert response.data[0]["lifetime"]["count"] == "55"
  860. assert response.data[0]["filtered"]["count"] == "2"
  861. assert response.data[0]["filtered"]["firstSeen"] == parse_datetime(
  862. before_now_250_seconds
  863. ).replace(tzinfo=timezone.utc)
  864. assert response.data[0]["filtered"]["lastSeen"] == parse_datetime(
  865. before_now_150_seconds
  866. ).replace(tzinfo=timezone.utc)
  867. # Empty filter test:
  868. response = self.get_response(sort_by="date", limit=10, query="")
  869. assert response.status_code == 200
  870. assert len(response.data) == 2
  871. assert int(response.data[0]["id"]) == group2.id
  872. assert response.data[0]["lifetime"] is not None
  873. assert response.data[0]["filtered"] is None
  874. assert response.data[0]["lifetime"]["stats"] is None
  875. assert response.data[0]["lifetime"]["count"] == "55"
  876. assert response.data[0]["lifetime"]["firstSeen"] == parse_datetime(
  877. before_now_350_seconds # Should match overridden value, not event value
  878. ).replace(tzinfo=timezone.utc)
  879. assert response.data[0]["lifetime"]["lastSeen"] == parse_datetime(
  880. before_now_100_seconds
  881. ).replace(tzinfo=timezone.utc)
  882. def test_semver_seen_stats(self):
  883. release_1 = self.create_release(version="test@1.2.3")
  884. release_2 = self.create_release(version="test@1.2.4")
  885. release_3 = self.create_release(version="test@1.2.5")
  886. release_1_e_1 = self.store_event(
  887. data={
  888. "timestamp": iso_format(before_now(minutes=5)),
  889. "fingerprint": ["group-1"],
  890. "release": release_1.version,
  891. },
  892. project_id=self.project.id,
  893. )
  894. group_1 = release_1_e_1.group
  895. release_2_e_1 = self.store_event(
  896. data={
  897. "timestamp": iso_format(before_now(minutes=3)),
  898. "fingerprint": ["group-1"],
  899. "release": release_2.version,
  900. },
  901. project_id=self.project.id,
  902. )
  903. release_3_e_1 = self.store_event(
  904. data={
  905. "timestamp": iso_format(before_now(minutes=1)),
  906. "fingerprint": ["group-1"],
  907. "release": release_3.version,
  908. },
  909. project_id=self.project.id,
  910. )
  911. group_1.update(times_seen=3)
  912. self.login_as(user=self.user)
  913. response = self.get_success_response(
  914. sort_by="date", limit=10, query="release.version:1.2.3"
  915. )
  916. assert [int(row["id"]) for row in response.data] == [group_1.id]
  917. group_data = response.data[0]
  918. assert group_data["lifetime"]["firstSeen"] == release_1_e_1.datetime
  919. assert group_data["filtered"]["firstSeen"] == release_1_e_1.datetime
  920. assert group_data["lifetime"]["lastSeen"] == release_3_e_1.datetime
  921. assert group_data["filtered"]["lastSeen"] == release_1_e_1.datetime
  922. assert int(group_data["lifetime"]["count"]) == 3
  923. assert int(group_data["filtered"]["count"]) == 1
  924. response = self.get_success_response(
  925. sort_by="date", limit=10, query="release.version:>=1.2.3"
  926. )
  927. assert [int(row["id"]) for row in response.data] == [group_1.id]
  928. group_data = response.data[0]
  929. assert group_data["lifetime"]["firstSeen"] == release_1_e_1.datetime
  930. assert group_data["filtered"]["firstSeen"] == release_1_e_1.datetime
  931. assert group_data["lifetime"]["lastSeen"] == release_3_e_1.datetime
  932. assert group_data["filtered"]["lastSeen"] == release_3_e_1.datetime
  933. assert int(group_data["lifetime"]["count"]) == 3
  934. assert int(group_data["filtered"]["count"]) == 3
  935. response = self.get_success_response(
  936. sort_by="date", limit=10, query="release.version:=1.2.4"
  937. )
  938. assert [int(row["id"]) for row in response.data] == [group_1.id]
  939. group_data = response.data[0]
  940. assert group_data["lifetime"]["firstSeen"] == release_1_e_1.datetime
  941. assert group_data["filtered"]["firstSeen"] == release_2_e_1.datetime
  942. assert group_data["lifetime"]["lastSeen"] == release_3_e_1.datetime
  943. assert group_data["filtered"]["lastSeen"] == release_2_e_1.datetime
  944. assert int(group_data["lifetime"]["count"]) == 3
  945. assert int(group_data["filtered"]["count"]) == 1
  946. def test_inbox_search(self):
  947. self.store_event(
  948. data={
  949. "timestamp": iso_format(before_now(seconds=200)),
  950. "fingerprint": ["group-1"],
  951. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  952. },
  953. project_id=self.project.id,
  954. )
  955. event = self.store_event(
  956. data={
  957. "timestamp": iso_format(before_now(seconds=200)),
  958. "fingerprint": ["group-2"],
  959. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  960. },
  961. project_id=self.project.id,
  962. )
  963. self.store_event(
  964. data={
  965. "timestamp": iso_format(before_now(seconds=200)),
  966. "fingerprint": ["group-3"],
  967. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  968. },
  969. project_id=self.project.id,
  970. )
  971. add_group_to_inbox(event.group, GroupInboxReason.NEW)
  972. self.login_as(user=self.user)
  973. response = self.get_response(
  974. sort_by="date", limit=10, query="is:unresolved is:for_review", expand=["inbox"]
  975. )
  976. assert response.status_code == 200
  977. assert len(response.data) == 1
  978. assert int(response.data[0]["id"]) == event.group.id
  979. assert response.data[0]["inbox"] is not None
  980. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.NEW.value
  981. def test_inbox_search_outside_retention(self):
  982. self.login_as(user=self.user)
  983. response = self.get_response(
  984. sort="inbox",
  985. limit=10,
  986. query="is:unresolved is:for_review",
  987. collapse="stats",
  988. expand=["inbox", "owners"],
  989. start=iso_format(before_now(days=20)),
  990. end=iso_format(before_now(days=15)),
  991. )
  992. assert response.status_code == 200
  993. assert len(response.data) == 0
  994. def test_assigned_or_suggested_search(self):
  995. event = self.store_event(
  996. data={
  997. "timestamp": iso_format(before_now(seconds=180)),
  998. "fingerprint": ["group-1"],
  999. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  1000. },
  1001. project_id=self.project.id,
  1002. )
  1003. event1 = self.store_event(
  1004. data={
  1005. "timestamp": iso_format(before_now(seconds=185)),
  1006. "fingerprint": ["group-2"],
  1007. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  1008. },
  1009. project_id=self.project.id,
  1010. )
  1011. event2 = self.store_event(
  1012. data={
  1013. "timestamp": iso_format(before_now(seconds=190)),
  1014. "fingerprint": ["group-3"],
  1015. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  1016. },
  1017. project_id=self.project.id,
  1018. )
  1019. assigned_event = self.store_event(
  1020. data={
  1021. "timestamp": iso_format(before_now(seconds=195)),
  1022. "fingerprint": ["group-4"],
  1023. },
  1024. project_id=self.project.id,
  1025. )
  1026. assigned_to_other_event = self.store_event(
  1027. data={
  1028. "timestamp": iso_format(before_now(seconds=195)),
  1029. "fingerprint": ["group-5"],
  1030. },
  1031. project_id=self.project.id,
  1032. )
  1033. self.login_as(user=self.user)
  1034. response = self.get_response(sort_by="date", limit=10, query="assigned_or_suggested:me")
  1035. assert response.status_code == 200
  1036. assert len(response.data) == 0
  1037. GroupOwner.objects.create(
  1038. group=assigned_to_other_event.group,
  1039. project=assigned_to_other_event.group.project,
  1040. organization=assigned_to_other_event.group.project.organization,
  1041. type=0,
  1042. team_id=None,
  1043. user_id=self.user.id,
  1044. )
  1045. GroupOwner.objects.create(
  1046. group=event.group,
  1047. project=event.group.project,
  1048. organization=event.group.project.organization,
  1049. type=0,
  1050. team_id=None,
  1051. user_id=self.user.id,
  1052. )
  1053. response = self.get_response(sort_by="date", limit=10, query="assigned_or_suggested:me")
  1054. assert response.status_code == 200
  1055. assert len(response.data) == 2
  1056. assert int(response.data[0]["id"]) == event.group.id
  1057. assert int(response.data[1]["id"]) == assigned_to_other_event.group.id
  1058. # Because assigned_to_other_event is assigned to self.other_user, it should not show up in assigned_or_suggested search for anyone but self.other_user. (aka. they are now the only owner)
  1059. other_user = self.create_user("other@user.com", is_superuser=False)
  1060. GroupAssignee.objects.create(
  1061. group=assigned_to_other_event.group,
  1062. project=assigned_to_other_event.group.project,
  1063. user_id=other_user.id,
  1064. )
  1065. response = self.get_response(sort_by="date", limit=10, query="assigned_or_suggested:me")
  1066. assert response.status_code == 200
  1067. assert len(response.data) == 1
  1068. assert int(response.data[0]["id"]) == event.group.id
  1069. response = self.get_response(
  1070. sort_by="date", limit=10, query=f"assigned_or_suggested:{other_user.email}"
  1071. )
  1072. assert response.status_code == 200
  1073. assert len(response.data) == 1
  1074. assert int(response.data[0]["id"]) == assigned_to_other_event.group.id
  1075. GroupAssignee.objects.create(
  1076. group=assigned_event.group, project=assigned_event.group.project, user_id=self.user.id
  1077. )
  1078. response = self.get_response(
  1079. sort_by="date", limit=10, query=f"assigned_or_suggested:{self.user.email}"
  1080. )
  1081. assert response.status_code == 200
  1082. assert len(response.data) == 2
  1083. assert int(response.data[0]["id"]) == event.group.id
  1084. assert int(response.data[1]["id"]) == assigned_event.group.id
  1085. response = self.get_response(
  1086. sort_by="date", limit=10, query=f"assigned_or_suggested:#{self.team.slug}"
  1087. )
  1088. assert response.status_code == 200
  1089. assert len(response.data) == 0
  1090. GroupOwner.objects.create(
  1091. group=event.group,
  1092. project=event.group.project,
  1093. organization=event.group.project.organization,
  1094. type=0,
  1095. team_id=self.team.id,
  1096. user_id=None,
  1097. )
  1098. response = self.get_response(
  1099. sort_by="date", limit=10, query=f"assigned_or_suggested:#{self.team.slug}"
  1100. )
  1101. assert response.status_code == 200
  1102. assert len(response.data) == 1
  1103. assert int(response.data[0]["id"]) == event.group.id
  1104. response = self.get_response(
  1105. sort_by="date", limit=10, query="assigned_or_suggested:[me, none]"
  1106. )
  1107. assert response.status_code == 200
  1108. assert len(response.data) == 4
  1109. assert int(response.data[0]["id"]) == event.group.id
  1110. assert int(response.data[1]["id"]) == event1.group.id
  1111. assert int(response.data[2]["id"]) == event2.group.id
  1112. assert int(response.data[3]["id"]) == assigned_event.group.id
  1113. not_me = self.create_user(email="notme@sentry.io")
  1114. GroupOwner.objects.create(
  1115. group=event2.group,
  1116. project=event2.group.project,
  1117. organization=event2.group.project.organization,
  1118. type=0,
  1119. team_id=None,
  1120. user_id=not_me.id,
  1121. )
  1122. response = self.get_response(
  1123. sort_by="date", limit=10, query="assigned_or_suggested:[me, none]"
  1124. )
  1125. assert response.status_code == 200
  1126. assert len(response.data) == 3
  1127. assert int(response.data[0]["id"]) == event.group.id
  1128. assert int(response.data[1]["id"]) == event1.group.id
  1129. assert int(response.data[2]["id"]) == assigned_event.group.id
  1130. GroupOwner.objects.create(
  1131. group=event2.group,
  1132. project=event2.group.project,
  1133. organization=event2.group.project.organization,
  1134. type=0,
  1135. team_id=None,
  1136. user_id=self.user.id,
  1137. )
  1138. # Should now include event2 as it has shared ownership.
  1139. response = self.get_response(
  1140. sort_by="date", limit=10, query="assigned_or_suggested:[me, none]"
  1141. )
  1142. assert response.status_code == 200
  1143. assert len(response.data) == 4
  1144. assert int(response.data[0]["id"]) == event.group.id
  1145. assert int(response.data[1]["id"]) == event1.group.id
  1146. assert int(response.data[2]["id"]) == event2.group.id
  1147. assert int(response.data[3]["id"]) == assigned_event.group.id
  1148. # Assign group to another user and now it shouldn't show up in owner search for this team.
  1149. GroupAssignee.objects.create(
  1150. group=event.group,
  1151. project=event.group.project,
  1152. user_id=other_user.id,
  1153. )
  1154. response = self.get_response(
  1155. sort_by="date", limit=10, query=f"assigned_or_suggested:#{self.team.slug}"
  1156. )
  1157. assert response.status_code == 200
  1158. assert len(response.data) == 0
  1159. def test_semver(self):
  1160. release_1 = self.create_release(version="test@1.2.3")
  1161. release_2 = self.create_release(version="test@1.2.4")
  1162. release_3 = self.create_release(version="test@1.2.5")
  1163. release_1_g_1 = self.store_event(
  1164. data={
  1165. "timestamp": iso_format(before_now(minutes=1)),
  1166. "fingerprint": ["group-1"],
  1167. "release": release_1.version,
  1168. },
  1169. project_id=self.project.id,
  1170. ).group.id
  1171. release_1_g_2 = self.store_event(
  1172. data={
  1173. "timestamp": iso_format(before_now(minutes=2)),
  1174. "fingerprint": ["group-2"],
  1175. "release": release_1.version,
  1176. },
  1177. project_id=self.project.id,
  1178. ).group.id
  1179. release_2_g_1 = self.store_event(
  1180. data={
  1181. "timestamp": iso_format(before_now(minutes=3)),
  1182. "fingerprint": ["group-3"],
  1183. "release": release_2.version,
  1184. },
  1185. project_id=self.project.id,
  1186. ).group.id
  1187. release_2_g_2 = self.store_event(
  1188. data={
  1189. "timestamp": iso_format(before_now(minutes=4)),
  1190. "fingerprint": ["group-4"],
  1191. "release": release_2.version,
  1192. },
  1193. project_id=self.project.id,
  1194. ).group.id
  1195. release_3_g_1 = self.store_event(
  1196. data={
  1197. "timestamp": iso_format(before_now(minutes=5)),
  1198. "fingerprint": ["group-5"],
  1199. "release": release_3.version,
  1200. },
  1201. project_id=self.project.id,
  1202. ).group.id
  1203. release_3_g_2 = self.store_event(
  1204. data={
  1205. "timestamp": iso_format(before_now(minutes=6)),
  1206. "fingerprint": ["group-6"],
  1207. "release": release_3.version,
  1208. },
  1209. project_id=self.project.id,
  1210. ).group.id
  1211. self.login_as(user=self.user)
  1212. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_ALIAS}:>1.2.3")
  1213. assert response.status_code == 200, response.content
  1214. assert [int(r["id"]) for r in response.json()] == [
  1215. release_2_g_1,
  1216. release_2_g_2,
  1217. release_3_g_1,
  1218. release_3_g_2,
  1219. ]
  1220. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_ALIAS}:>=1.2.3")
  1221. assert response.status_code == 200, response.content
  1222. assert [int(r["id"]) for r in response.json()] == [
  1223. release_1_g_1,
  1224. release_1_g_2,
  1225. release_2_g_1,
  1226. release_2_g_2,
  1227. release_3_g_1,
  1228. release_3_g_2,
  1229. ]
  1230. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_ALIAS}:<1.2.4")
  1231. assert response.status_code == 200, response.content
  1232. assert [int(r["id"]) for r in response.json()] == [release_1_g_1, release_1_g_2]
  1233. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_ALIAS}:<1.0")
  1234. assert response.status_code == 200, response.content
  1235. assert [int(r["id"]) for r in response.json()] == []
  1236. response = self.get_response(sort_by="date", limit=10, query=f"!{SEMVER_ALIAS}:1.2.4")
  1237. assert response.status_code == 200, response.content
  1238. assert [int(r["id"]) for r in response.json()] == [
  1239. release_1_g_1,
  1240. release_1_g_2,
  1241. release_3_g_1,
  1242. release_3_g_2,
  1243. ]
  1244. def test_release_stage(self):
  1245. replaced_release = self.create_release(
  1246. version="replaced_release",
  1247. environments=[self.environment],
  1248. adopted=timezone.now(),
  1249. unadopted=timezone.now(),
  1250. )
  1251. adopted_release = self.create_release(
  1252. version="adopted_release",
  1253. environments=[self.environment],
  1254. adopted=timezone.now(),
  1255. )
  1256. self.create_release(version="not_adopted_release", environments=[self.environment])
  1257. adopted_release_g_1 = self.store_event(
  1258. data={
  1259. "timestamp": iso_format(before_now(minutes=1)),
  1260. "fingerprint": ["group-1"],
  1261. "release": adopted_release.version,
  1262. "environment": self.environment.name,
  1263. },
  1264. project_id=self.project.id,
  1265. ).group.id
  1266. adopted_release_g_2 = self.store_event(
  1267. data={
  1268. "timestamp": iso_format(before_now(minutes=2)),
  1269. "fingerprint": ["group-2"],
  1270. "release": adopted_release.version,
  1271. "environment": self.environment.name,
  1272. },
  1273. project_id=self.project.id,
  1274. ).group.id
  1275. replaced_release_g_1 = self.store_event(
  1276. data={
  1277. "timestamp": iso_format(before_now(minutes=3)),
  1278. "fingerprint": ["group-3"],
  1279. "release": replaced_release.version,
  1280. "environment": self.environment.name,
  1281. },
  1282. project_id=self.project.id,
  1283. ).group.id
  1284. replaced_release_g_2 = self.store_event(
  1285. data={
  1286. "timestamp": iso_format(before_now(minutes=4)),
  1287. "fingerprint": ["group-4"],
  1288. "release": replaced_release.version,
  1289. "environment": self.environment.name,
  1290. },
  1291. project_id=self.project.id,
  1292. ).group.id
  1293. self.login_as(user=self.user)
  1294. response = self.get_response(
  1295. sort_by="date",
  1296. limit=10,
  1297. query=f"{RELEASE_STAGE_ALIAS}:{ReleaseStages.ADOPTED}",
  1298. environment=self.environment.name,
  1299. )
  1300. assert response.status_code == 200, response.content
  1301. assert [int(r["id"]) for r in response.json()] == [
  1302. adopted_release_g_1,
  1303. adopted_release_g_2,
  1304. ]
  1305. response = self.get_response(
  1306. sort_by="date",
  1307. limit=10,
  1308. query=f"!{RELEASE_STAGE_ALIAS}:{ReleaseStages.LOW_ADOPTION}",
  1309. environment=self.environment.name,
  1310. )
  1311. assert response.status_code == 200, response.content
  1312. assert [int(r["id"]) for r in response.json()] == [
  1313. adopted_release_g_1,
  1314. adopted_release_g_2,
  1315. replaced_release_g_1,
  1316. replaced_release_g_2,
  1317. ]
  1318. response = self.get_response(
  1319. sort_by="date",
  1320. limit=10,
  1321. query=f"{RELEASE_STAGE_ALIAS}:[{ReleaseStages.ADOPTED}, {ReleaseStages.REPLACED}]",
  1322. environment=self.environment.name,
  1323. )
  1324. assert response.status_code == 200, response.content
  1325. assert [int(r["id"]) for r in response.json()] == [
  1326. adopted_release_g_1,
  1327. adopted_release_g_2,
  1328. replaced_release_g_1,
  1329. replaced_release_g_2,
  1330. ]
  1331. response = self.get_response(
  1332. sort_by="date",
  1333. limit=10,
  1334. query=f"!{RELEASE_STAGE_ALIAS}:[{ReleaseStages.LOW_ADOPTION}, {ReleaseStages.REPLACED}]",
  1335. environment=self.environment.name,
  1336. )
  1337. assert response.status_code == 200, response.content
  1338. assert [int(r["id"]) for r in response.json()] == [
  1339. adopted_release_g_1,
  1340. adopted_release_g_2,
  1341. ]
  1342. def test_semver_package(self):
  1343. release_1 = self.create_release(version="test@1.2.3")
  1344. release_2 = self.create_release(version="test2@1.2.4")
  1345. release_1_g_1 = self.store_event(
  1346. data={
  1347. "timestamp": iso_format(before_now(minutes=1)),
  1348. "fingerprint": ["group-1"],
  1349. "release": release_1.version,
  1350. },
  1351. project_id=self.project.id,
  1352. ).group.id
  1353. release_1_g_2 = self.store_event(
  1354. data={
  1355. "timestamp": iso_format(before_now(minutes=2)),
  1356. "fingerprint": ["group-2"],
  1357. "release": release_1.version,
  1358. },
  1359. project_id=self.project.id,
  1360. ).group.id
  1361. release_2_g_1 = self.store_event(
  1362. data={
  1363. "timestamp": iso_format(before_now(minutes=3)),
  1364. "fingerprint": ["group-3"],
  1365. "release": release_2.version,
  1366. },
  1367. project_id=self.project.id,
  1368. ).group.id
  1369. self.login_as(user=self.user)
  1370. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_PACKAGE_ALIAS}:test")
  1371. assert response.status_code == 200, response.content
  1372. assert [int(r["id"]) for r in response.json()] == [
  1373. release_1_g_1,
  1374. release_1_g_2,
  1375. ]
  1376. response = self.get_response(
  1377. sort_by="date", limit=10, query=f"{SEMVER_PACKAGE_ALIAS}:test2"
  1378. )
  1379. assert response.status_code == 200, response.content
  1380. assert [int(r["id"]) for r in response.json()] == [
  1381. release_2_g_1,
  1382. ]
  1383. def test_semver_build(self):
  1384. release_1 = self.create_release(version="test@1.2.3+123")
  1385. release_2 = self.create_release(version="test2@1.2.4+124")
  1386. release_1_g_1 = self.store_event(
  1387. data={
  1388. "timestamp": iso_format(before_now(minutes=1)),
  1389. "fingerprint": ["group-1"],
  1390. "release": release_1.version,
  1391. },
  1392. project_id=self.project.id,
  1393. ).group.id
  1394. release_1_g_2 = self.store_event(
  1395. data={
  1396. "timestamp": iso_format(before_now(minutes=2)),
  1397. "fingerprint": ["group-2"],
  1398. "release": release_1.version,
  1399. },
  1400. project_id=self.project.id,
  1401. ).group.id
  1402. release_2_g_1 = self.store_event(
  1403. data={
  1404. "timestamp": iso_format(before_now(minutes=3)),
  1405. "fingerprint": ["group-3"],
  1406. "release": release_2.version,
  1407. },
  1408. project_id=self.project.id,
  1409. ).group.id
  1410. self.login_as(user=self.user)
  1411. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_BUILD_ALIAS}:123")
  1412. assert response.status_code == 200, response.content
  1413. assert [int(r["id"]) for r in response.json()] == [
  1414. release_1_g_1,
  1415. release_1_g_2,
  1416. ]
  1417. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_BUILD_ALIAS}:124")
  1418. assert response.status_code == 200, response.content
  1419. assert [int(r["id"]) for r in response.json()] == [
  1420. release_2_g_1,
  1421. ]
  1422. response = self.get_response(sort_by="date", limit=10, query=f"{SEMVER_BUILD_ALIAS}:[124]")
  1423. assert response.status_code == 400, response.content
  1424. def test_aggregate_stats_regression_test(self):
  1425. self.store_event(
  1426. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1427. project_id=self.project.id,
  1428. )
  1429. self.login_as(user=self.user)
  1430. response = self.get_response(
  1431. sort_by="date", limit=10, query="times_seen:>0 last_seen:-1h date:-1h"
  1432. )
  1433. assert response.status_code == 200
  1434. assert len(response.data) == 1
  1435. def test_skipped_fields(self):
  1436. event = self.store_event(
  1437. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1438. project_id=self.project.id,
  1439. )
  1440. self.store_event(
  1441. data={
  1442. "timestamp": iso_format(before_now(seconds=200)),
  1443. "fingerprint": ["group-1"],
  1444. "tags": {"server": "example.com", "trace": "woof", "message": "foo"},
  1445. },
  1446. project_id=self.project.id,
  1447. )
  1448. query = "server:example.com"
  1449. query += " status:unresolved"
  1450. query += " first_seen:" + iso_format(before_now(seconds=500))
  1451. self.login_as(user=self.user)
  1452. response = self.get_response(sort_by="date", limit=10, query=query)
  1453. assert response.status_code == 200
  1454. assert len(response.data) == 1
  1455. assert int(response.data[0]["id"]) == event.group.id
  1456. assert response.data[0]["lifetime"] is not None
  1457. assert response.data[0]["filtered"] is not None
  1458. def test_inbox_fields(self):
  1459. event = self.store_event(
  1460. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1461. project_id=self.project.id,
  1462. )
  1463. add_group_to_inbox(event.group, GroupInboxReason.NEW)
  1464. query = "status:unresolved"
  1465. self.login_as(user=self.user)
  1466. response = self.get_response(sort_by="date", limit=10, query=query, expand=["inbox"])
  1467. assert response.status_code == 200
  1468. assert len(response.data) == 1
  1469. assert int(response.data[0]["id"]) == event.group.id
  1470. assert response.data[0]["inbox"] is not None
  1471. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.NEW.value
  1472. assert response.data[0]["inbox"]["reason_details"] is None
  1473. remove_group_from_inbox(event.group)
  1474. snooze_details = {
  1475. "until": None,
  1476. "count": 3,
  1477. "window": None,
  1478. "user_count": None,
  1479. "user_window": 5,
  1480. }
  1481. add_group_to_inbox(event.group, GroupInboxReason.UNIGNORED, snooze_details)
  1482. response = self.get_response(sort_by="date", limit=10, query=query, expand=["inbox"])
  1483. assert response.status_code == 200
  1484. assert len(response.data) == 1
  1485. assert int(response.data[0]["id"]) == event.group.id
  1486. assert response.data[0]["inbox"] is not None
  1487. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.UNIGNORED.value
  1488. assert response.data[0]["inbox"]["reason_details"] == snooze_details
  1489. @with_feature("organizations:escalating-issues")
  1490. def test_inbox_fields_issue_states(self):
  1491. event = self.store_event(
  1492. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1493. project_id=self.project.id,
  1494. )
  1495. add_group_to_inbox(event.group, GroupInboxReason.NEW)
  1496. query = "status:unresolved"
  1497. self.login_as(user=self.user)
  1498. response = self.get_response(sort_by="date", limit=10, query=query, expand=["inbox"])
  1499. assert response.status_code == 200
  1500. assert len(response.data) == 1
  1501. assert int(response.data[0]["id"]) == event.group.id
  1502. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.NEW.value
  1503. remove_group_from_inbox(event.group)
  1504. snooze_details = {
  1505. "until": None,
  1506. "count": 3,
  1507. "window": None,
  1508. "user_count": None,
  1509. "user_window": 5,
  1510. }
  1511. add_group_to_inbox(event.group, GroupInboxReason.ONGOING, snooze_details)
  1512. response = self.get_response(sort_by="date", limit=10, query=query, expand=["inbox"])
  1513. assert response.status_code == 200
  1514. assert len(response.data) == 1
  1515. assert int(response.data[0]["id"]) == event.group.id
  1516. assert response.data[0]["inbox"] is not None
  1517. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.ONGOING.value
  1518. assert response.data[0]["inbox"]["reason_details"] == snooze_details
  1519. def test_expand_string(self):
  1520. event = self.store_event(
  1521. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1522. project_id=self.project.id,
  1523. )
  1524. add_group_to_inbox(event.group, GroupInboxReason.NEW)
  1525. query = "status:unresolved"
  1526. self.login_as(user=self.user)
  1527. response = self.get_response(sort_by="date", limit=10, query=query, expand="inbox")
  1528. assert response.status_code == 200
  1529. assert len(response.data) == 1
  1530. assert int(response.data[0]["id"]) == event.group.id
  1531. assert response.data[0]["inbox"] is not None
  1532. assert response.data[0]["inbox"]["reason"] == GroupInboxReason.NEW.value
  1533. assert response.data[0]["inbox"]["reason_details"] is None
  1534. def test_expand_owners(self):
  1535. event = self.store_event(
  1536. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1537. project_id=self.project.id,
  1538. )
  1539. query = "status:unresolved"
  1540. self.login_as(user=self.user)
  1541. # Test with no owner
  1542. response = self.get_response(sort_by="date", limit=10, query=query, expand="owners")
  1543. assert response.status_code == 200
  1544. assert len(response.data) == 1
  1545. assert int(response.data[0]["id"]) == event.group.id
  1546. assert response.data[0]["owners"] is None
  1547. # Test with owners
  1548. GroupOwner.objects.create(
  1549. group=event.group,
  1550. project=event.project,
  1551. organization=event.project.organization,
  1552. type=GroupOwnerType.SUSPECT_COMMIT.value,
  1553. user_id=self.user.id,
  1554. )
  1555. GroupOwner.objects.create(
  1556. group=event.group,
  1557. project=event.project,
  1558. organization=event.project.organization,
  1559. type=GroupOwnerType.OWNERSHIP_RULE.value,
  1560. team=self.team,
  1561. )
  1562. GroupOwner.objects.create(
  1563. group=event.group,
  1564. project=event.project,
  1565. organization=event.project.organization,
  1566. type=GroupOwnerType.CODEOWNERS.value,
  1567. team=self.team,
  1568. )
  1569. GroupOwner.objects.create(
  1570. group=event.group,
  1571. project=event.project,
  1572. organization=event.project.organization,
  1573. type=GroupOwnerType.SUSPECT_COMMIT.value,
  1574. user_id=None,
  1575. team=None,
  1576. )
  1577. response = self.get_response(sort_by="date", limit=10, query=query, expand="owners")
  1578. assert response.status_code == 200
  1579. assert len(response.data) == 1
  1580. assert int(response.data[0]["id"]) == event.group.id
  1581. assert response.data[0]["owners"] is not None
  1582. assert len(response.data[0]["owners"]) == 3
  1583. assert response.data[0]["owners"][0]["owner"] == f"user:{self.user.id}"
  1584. assert response.data[0]["owners"][1]["owner"] == f"team:{self.team.id}"
  1585. assert response.data[0]["owners"][2]["owner"] == f"team:{self.team.id}"
  1586. assert (
  1587. response.data[0]["owners"][0]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.SUSPECT_COMMIT]
  1588. )
  1589. assert (
  1590. response.data[0]["owners"][1]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.OWNERSHIP_RULE]
  1591. )
  1592. assert response.data[0]["owners"][2]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.CODEOWNERS]
  1593. @override_settings(SENTRY_SELF_HOSTED=False)
  1594. def test_ratelimit(self):
  1595. self.login_as(user=self.user)
  1596. with freeze_time("2000-01-01"):
  1597. for i in range(10):
  1598. self.get_success_response()
  1599. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  1600. def test_filter_not_unresolved(self):
  1601. event = self.store_event(
  1602. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1603. project_id=self.project.id,
  1604. )
  1605. event.group.update(status=GroupStatus.RESOLVED, substatus=None)
  1606. self.login_as(user=self.user)
  1607. response = self.get_response(
  1608. sort_by="date", limit=10, query="!is:unresolved", expand="inbox", collapse="stats"
  1609. )
  1610. assert response.status_code == 200
  1611. assert [int(r["id"]) for r in response.data] == [event.group.id]
  1612. def test_collapse_stats(self):
  1613. event = self.store_event(
  1614. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1615. project_id=self.project.id,
  1616. )
  1617. self.login_as(user=self.user)
  1618. response = self.get_response(
  1619. sort_by="date", limit=10, query="is:unresolved", expand="inbox", collapse="stats"
  1620. )
  1621. assert response.status_code == 200
  1622. assert len(response.data) == 1
  1623. assert int(response.data[0]["id"]) == event.group.id
  1624. assert "stats" not in response.data[0]
  1625. assert "firstSeen" not in response.data[0]
  1626. assert "lastSeen" not in response.data[0]
  1627. assert "count" not in response.data[0]
  1628. assert "userCount" not in response.data[0]
  1629. assert "lifetime" not in response.data[0]
  1630. assert "filtered" not in response.data[0]
  1631. def test_collapse_lifetime(self):
  1632. event = self.store_event(
  1633. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1634. project_id=self.project.id,
  1635. )
  1636. self.login_as(user=self.user)
  1637. response = self.get_response(
  1638. sort_by="date", limit=10, query="is:unresolved", collapse="lifetime"
  1639. )
  1640. assert response.status_code == 200
  1641. assert len(response.data) == 1
  1642. assert int(response.data[0]["id"]) == event.group.id
  1643. assert "stats" in response.data[0]
  1644. assert "firstSeen" in response.data[0]
  1645. assert "lastSeen" in response.data[0]
  1646. assert "count" in response.data[0]
  1647. assert "lifetime" not in response.data[0]
  1648. assert "filtered" in response.data[0]
  1649. def test_collapse_filtered(self):
  1650. event = self.store_event(
  1651. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1652. project_id=self.project.id,
  1653. )
  1654. self.login_as(user=self.user)
  1655. response = self.get_response(
  1656. sort_by="date", limit=10, query="is:unresolved", collapse="filtered"
  1657. )
  1658. assert response.status_code == 200
  1659. assert len(response.data) == 1
  1660. assert int(response.data[0]["id"]) == event.group.id
  1661. assert "stats" in response.data[0]
  1662. assert "firstSeen" in response.data[0]
  1663. assert "lastSeen" in response.data[0]
  1664. assert "count" in response.data[0]
  1665. assert "lifetime" in response.data[0]
  1666. assert "filtered" not in response.data[0]
  1667. def test_collapse_lifetime_and_filtered(self):
  1668. event = self.store_event(
  1669. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1670. project_id=self.project.id,
  1671. )
  1672. self.login_as(user=self.user)
  1673. response = self.get_response(
  1674. sort_by="date", limit=10, query="is:unresolved", collapse=["filtered", "lifetime"]
  1675. )
  1676. assert response.status_code == 200
  1677. assert len(response.data) == 1
  1678. assert int(response.data[0]["id"]) == event.group.id
  1679. assert "stats" in response.data[0]
  1680. assert "firstSeen" in response.data[0]
  1681. assert "lastSeen" in response.data[0]
  1682. assert "count" in response.data[0]
  1683. assert "lifetime" not in response.data[0]
  1684. assert "filtered" not in response.data[0]
  1685. def test_collapse_base(self):
  1686. event = self.store_event(
  1687. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1688. project_id=self.project.id,
  1689. )
  1690. self.login_as(user=self.user)
  1691. response = self.get_response(
  1692. sort_by="date", limit=10, query="is:unresolved", collapse=["base"]
  1693. )
  1694. assert response.status_code == 200
  1695. assert len(response.data) == 1
  1696. assert int(response.data[0]["id"]) == event.group.id
  1697. assert "title" not in response.data[0]
  1698. assert "hasSeen" not in response.data[0]
  1699. assert "stats" in response.data[0]
  1700. assert "firstSeen" in response.data[0]
  1701. assert "lastSeen" in response.data[0]
  1702. assert "count" in response.data[0]
  1703. assert "lifetime" in response.data[0]
  1704. assert "filtered" in response.data[0]
  1705. def test_collapse_stats_group_snooze_bug(self):
  1706. # There was a bug where we tried to access attributes on seen_stats if this feature is active
  1707. # but seen_stats could be null when we collapse stats.
  1708. event = self.store_event(
  1709. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1710. project_id=self.project.id,
  1711. )
  1712. GroupSnooze.objects.create(
  1713. group=event.group,
  1714. user_count=10,
  1715. until=timezone.now() + timedelta(days=1),
  1716. count=10,
  1717. state={"times_seen": 0},
  1718. )
  1719. self.login_as(user=self.user)
  1720. # The presence of the group above with attached GroupSnooze would have previously caused this error.
  1721. response = self.get_response(
  1722. sort_by="date", limit=10, query="is:unresolved", expand="inbox", collapse="stats"
  1723. )
  1724. assert response.status_code == 200
  1725. assert len(response.data) == 1
  1726. assert int(response.data[0]["id"]) == event.group.id
  1727. def test_query_status_and_substatus_overlapping(self):
  1728. event = self.store_event(
  1729. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1730. project_id=self.project.id,
  1731. )
  1732. event.group.update(status=GroupStatus.UNRESOLVED, substatus=GroupSubStatus.ONGOING)
  1733. self.login_as(user=self.user)
  1734. get_query_response = functools.partial(
  1735. self.get_response, sort_by="date", limit=10, expand="inbox", collapse="stats"
  1736. )
  1737. response0 = get_query_response(
  1738. query="is:unresolved",
  1739. )
  1740. with Feature("organizations:escalating-issues"):
  1741. response1 = get_query_response(
  1742. query="is:ongoing"
  1743. ) # (status=unresolved, substatus=(ongoing))
  1744. response2 = get_query_response(
  1745. query="is:unresolved"
  1746. ) # (status=unresolved, substatus=*)
  1747. response3 = get_query_response(
  1748. query="is:unresolved is:ongoing !is:regressed"
  1749. ) # (status=unresolved, substatus=(ongoing, !regressed))
  1750. response4 = get_query_response(
  1751. query="is:unresolved is:ongoing !is:ignored"
  1752. ) # (status=unresolved, substatus=(ongoing, !ignored))
  1753. response5 = get_query_response(
  1754. query="!is:regressed is:unresolved"
  1755. ) # (status=unresolved, substatus=(!regressed))
  1756. response6 = get_query_response(
  1757. query="!is:until_escalating"
  1758. ) # (status=(!unresolved), substatus=(!until_escalating))
  1759. assert (
  1760. response0.status_code
  1761. == response1.status_code
  1762. == response2.status_code
  1763. == response3.status_code
  1764. == response4.status_code
  1765. == response5.status_code
  1766. == response6.status_code
  1767. == 200
  1768. )
  1769. assert (
  1770. [int(r["id"]) for r in response0.data]
  1771. == [int(r["id"]) for r in response1.data]
  1772. == [int(r["id"]) for r in response2.data]
  1773. == [int(r["id"]) for r in response3.data]
  1774. == [int(r["id"]) for r in response4.data]
  1775. == [int(r["id"]) for r in response5.data]
  1776. == [int(r["id"]) for r in response6.data]
  1777. == [event.group.id]
  1778. )
  1779. def test_query_status_and_substatus_nonoverlapping(self):
  1780. event = self.store_event(
  1781. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1782. project_id=self.project.id,
  1783. )
  1784. event.group.update(status=GroupStatus.UNRESOLVED, substatus=GroupSubStatus.ONGOING)
  1785. self.login_as(user=self.user)
  1786. get_query_response = functools.partial(
  1787. self.get_response, sort_by="date", limit=10, expand="inbox", collapse="stats"
  1788. )
  1789. with Feature("organizations:escalating-issues"):
  1790. response1 = get_query_response(query="is:escalating")
  1791. response2 = get_query_response(query="is:new")
  1792. response3 = get_query_response(query="is:regressed")
  1793. response4 = get_query_response(query="is:forever")
  1794. response5 = get_query_response(query="is:until_condition_met")
  1795. response6 = get_query_response(query="is:until_escalating")
  1796. response7 = get_query_response(query="is:resolved")
  1797. response8 = get_query_response(query="is:ignored")
  1798. response9 = get_query_response(query="is:muted")
  1799. response10 = get_query_response(query="!is:unresolved")
  1800. assert (
  1801. response1.status_code
  1802. == response2.status_code
  1803. == response3.status_code
  1804. == response4.status_code
  1805. == response5.status_code
  1806. == response6.status_code
  1807. == response7.status_code
  1808. == response8.status_code
  1809. == response9.status_code
  1810. == response10.status_code
  1811. == 200
  1812. )
  1813. assert (
  1814. [int(r["id"]) for r in response1.data]
  1815. == [int(r["id"]) for r in response2.data]
  1816. == [int(r["id"]) for r in response3.data]
  1817. == [int(r["id"]) for r in response4.data]
  1818. == [int(r["id"]) for r in response5.data]
  1819. == [int(r["id"]) for r in response6.data]
  1820. == [int(r["id"]) for r in response7.data]
  1821. == [int(r["id"]) for r in response8.data]
  1822. == [int(r["id"]) for r in response9.data]
  1823. == [int(r["id"]) for r in response10.data]
  1824. == []
  1825. )
  1826. @region_silo_test(stable=True)
  1827. class GroupUpdateTest(APITestCase, SnubaTestCase):
  1828. endpoint = "sentry-api-0-organization-group-index"
  1829. method = "put"
  1830. def setUp(self):
  1831. super().setUp()
  1832. self.min_ago = timezone.now() - timedelta(minutes=1)
  1833. def get_response(self, *args, **kwargs):
  1834. if not args:
  1835. org = self.project.organization.slug
  1836. else:
  1837. org = args[0]
  1838. return super().get_response(org, **kwargs)
  1839. def assertNoResolution(self, group):
  1840. assert not GroupResolution.objects.filter(group=group).exists()
  1841. def test_global_resolve(self):
  1842. group1 = self.create_group(status=GroupStatus.RESOLVED)
  1843. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  1844. group3 = self.create_group(status=GroupStatus.IGNORED)
  1845. group4 = self.create_group(
  1846. project=self.create_project(slug="foo"),
  1847. status=GroupStatus.UNRESOLVED,
  1848. )
  1849. self.login_as(user=self.user)
  1850. response = self.get_success_response(
  1851. qs_params={"status": "unresolved", "project": self.project.id}, status="resolved"
  1852. )
  1853. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1854. # the previously resolved entry should not be included
  1855. new_group1 = Group.objects.get(id=group1.id)
  1856. assert new_group1.status == GroupStatus.RESOLVED
  1857. assert new_group1.resolved_at is None
  1858. # this wont exist because it wasn't affected
  1859. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group1).exists()
  1860. new_group2 = Group.objects.get(id=group2.id)
  1861. assert new_group2.status == GroupStatus.RESOLVED
  1862. assert new_group2.resolved_at is not None
  1863. assert GroupSubscription.objects.filter(
  1864. user_id=self.user.id, group=new_group2, is_active=True
  1865. ).exists()
  1866. # the ignored entry should not be included
  1867. new_group3 = Group.objects.get(id=group3.id)
  1868. assert new_group3.status == GroupStatus.IGNORED
  1869. assert new_group3.resolved_at is None
  1870. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group3)
  1871. new_group4 = Group.objects.get(id=group4.id)
  1872. assert new_group4.status == GroupStatus.UNRESOLVED
  1873. assert new_group4.resolved_at is None
  1874. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group4)
  1875. assert not GroupHistory.objects.filter(
  1876. group=group1, status=GroupHistoryStatus.RESOLVED
  1877. ).exists()
  1878. assert GroupHistory.objects.filter(
  1879. group=group2, status=GroupHistoryStatus.RESOLVED
  1880. ).exists()
  1881. assert not GroupHistory.objects.filter(
  1882. group=group3, status=GroupHistoryStatus.RESOLVED
  1883. ).exists()
  1884. assert not GroupHistory.objects.filter(
  1885. group=group4, status=GroupHistoryStatus.RESOLVED
  1886. ).exists()
  1887. def test_resolve_member(self):
  1888. group = self.create_group(status=GroupStatus.UNRESOLVED)
  1889. member = self.create_user()
  1890. self.create_member(
  1891. organization=self.organization, teams=group.project.teams.all(), user=member
  1892. )
  1893. self.login_as(user=member)
  1894. response = self.get_success_response(
  1895. qs_params={"status": "unresolved", "project": self.project.id}, status="resolved"
  1896. )
  1897. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1898. assert response.status_code == 200
  1899. def test_resolve_ignored(self):
  1900. group = self.create_group(status=GroupStatus.IGNORED)
  1901. snooze = GroupSnooze.objects.create(
  1902. group=group, until=timezone.now() - timedelta(minutes=1)
  1903. )
  1904. member = self.create_user()
  1905. self.create_member(
  1906. organization=self.organization, teams=group.project.teams.all(), user=member
  1907. )
  1908. self.login_as(user=member)
  1909. response = self.get_success_response(
  1910. qs_params={"id": group.id, "project": self.project.id}, status="resolved"
  1911. )
  1912. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1913. assert not GroupSnooze.objects.filter(id=snooze.id).exists()
  1914. def test_bulk_resolve(self):
  1915. self.login_as(user=self.user)
  1916. for i in range(200):
  1917. self.store_event(
  1918. data={
  1919. "fingerprint": [i],
  1920. "timestamp": iso_format(self.min_ago - timedelta(seconds=i)),
  1921. },
  1922. project_id=self.project.id,
  1923. )
  1924. response = self.get_success_response(query="is:unresolved", sort_by="date", method="get")
  1925. assert len(response.data) == 100
  1926. response = self.get_success_response(qs_params={"status": "unresolved"}, status="resolved")
  1927. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1928. response = self.get_success_response(query="is:unresolved", sort_by="date", method="get")
  1929. assert len(response.data) == 0
  1930. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  1931. def test_resolve_with_integration(self, mock_sync_status_outbound):
  1932. self.login_as(user=self.user)
  1933. org = self.organization
  1934. with assume_test_silo_mode(SiloMode.CONTROL):
  1935. integration = Integration.objects.create(provider="example", name="Example")
  1936. integration.add_organization(org, self.user)
  1937. event = self.store_event(
  1938. data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id
  1939. )
  1940. group = event.group
  1941. with assume_test_silo_mode(SiloMode.CONTROL):
  1942. OrganizationIntegration.objects.filter(
  1943. integration_id=integration.id, organization_id=group.organization.id
  1944. ).update(
  1945. config={
  1946. "sync_comments": True,
  1947. "sync_status_outbound": True,
  1948. "sync_status_inbound": True,
  1949. "sync_assignee_outbound": True,
  1950. "sync_assignee_inbound": True,
  1951. }
  1952. )
  1953. external_issue = ExternalIssue.objects.get_or_create(
  1954. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  1955. )[0]
  1956. GroupLink.objects.get_or_create(
  1957. group_id=group.id,
  1958. project_id=group.project_id,
  1959. linked_type=GroupLink.LinkedType.issue,
  1960. linked_id=external_issue.id,
  1961. relationship=GroupLink.Relationship.references,
  1962. )[0]
  1963. response = self.get_success_response(sort_by="date", query="is:unresolved", method="get")
  1964. assert len(response.data) == 1
  1965. with self.tasks():
  1966. with self.feature({"organizations:integrations-issue-sync": True}):
  1967. response = self.get_success_response(
  1968. qs_params={"status": "unresolved"}, status="resolved"
  1969. )
  1970. group = Group.objects.get(id=group.id)
  1971. assert group.status == GroupStatus.RESOLVED
  1972. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1973. mock_sync_status_outbound.assert_called_once_with(
  1974. external_issue, True, group.project_id
  1975. )
  1976. response = self.get_success_response(sort_by="date", query="is:unresolved", method="get")
  1977. assert len(response.data) == 0
  1978. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  1979. def test_set_unresolved_with_integration(self, mock_sync_status_outbound):
  1980. release = self.create_release(project=self.project, version="abc")
  1981. group = self.create_group(status=GroupStatus.RESOLVED)
  1982. with assume_test_silo_mode(SiloMode.CONTROL):
  1983. org = self.organization
  1984. integration = Integration.objects.create(provider="example", name="Example")
  1985. integration.add_organization(org, self.user)
  1986. OrganizationIntegration.objects.filter(
  1987. integration_id=integration.id, organization_id=group.organization.id
  1988. ).update(
  1989. config={
  1990. "sync_comments": True,
  1991. "sync_status_outbound": True,
  1992. "sync_status_inbound": True,
  1993. "sync_assignee_outbound": True,
  1994. "sync_assignee_inbound": True,
  1995. }
  1996. )
  1997. GroupResolution.objects.create(group=group, release=release)
  1998. external_issue = ExternalIssue.objects.get_or_create(
  1999. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  2000. )[0]
  2001. GroupLink.objects.get_or_create(
  2002. group_id=group.id,
  2003. project_id=group.project_id,
  2004. linked_type=GroupLink.LinkedType.issue,
  2005. linked_id=external_issue.id,
  2006. relationship=GroupLink.Relationship.references,
  2007. )[0]
  2008. self.login_as(user=self.user)
  2009. with self.tasks():
  2010. with self.feature({"organizations:integrations-issue-sync": True}):
  2011. response = self.get_success_response(
  2012. qs_params={"id": group.id}, status="unresolved"
  2013. )
  2014. assert response.status_code == 200
  2015. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2016. group = Group.objects.get(id=group.id)
  2017. assert group.status == GroupStatus.UNRESOLVED
  2018. self.assertNoResolution(group)
  2019. assert GroupSubscription.objects.filter(
  2020. user_id=self.user.id, group=group, is_active=True
  2021. ).exists()
  2022. mock_sync_status_outbound.assert_called_once_with(
  2023. external_issue, False, group.project_id
  2024. )
  2025. def test_self_assign_issue(self):
  2026. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2027. user = self.user
  2028. with assume_test_silo_mode(SiloMode.CONTROL):
  2029. uo1 = UserOption.objects.create(
  2030. key="self_assign_issue", value="1", project_id=None, user=user
  2031. )
  2032. self.login_as(user=user)
  2033. response = self.get_success_response(qs_params={"id": group.id}, status="resolved")
  2034. assert response.data["assignedTo"]["id"] == str(user.id)
  2035. assert response.data["assignedTo"]["type"] == "user"
  2036. assert response.data["status"] == "resolved"
  2037. assert GroupAssignee.objects.filter(group=group, user_id=user.id).exists()
  2038. assert GroupSubscription.objects.filter(
  2039. user_id=user.id, group=group, is_active=True
  2040. ).exists()
  2041. with assume_test_silo_mode(SiloMode.CONTROL):
  2042. uo1.delete()
  2043. def test_self_assign_issue_next_release(self):
  2044. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2045. release.add_project(self.project)
  2046. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2047. with assume_test_silo_mode(SiloMode.CONTROL):
  2048. uo1 = UserOption.objects.create(
  2049. key="self_assign_issue", value="1", project_id=None, user=self.user
  2050. )
  2051. self.login_as(user=self.user)
  2052. response = self.get_success_response(
  2053. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2054. )
  2055. assert response.data["status"] == "resolved"
  2056. assert response.data["statusDetails"]["inNextRelease"]
  2057. assert response.data["assignedTo"]["id"] == str(self.user.id)
  2058. assert response.data["assignedTo"]["type"] == "user"
  2059. group = Group.objects.get(id=group.id)
  2060. assert group.status == GroupStatus.RESOLVED
  2061. assert GroupResolution.objects.filter(group=group, release=release).exists()
  2062. assert GroupSubscription.objects.filter(
  2063. user_id=self.user.id, group=group, is_active=True
  2064. ).exists()
  2065. activity = Activity.objects.get(
  2066. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2067. )
  2068. assert activity.data["version"] == ""
  2069. with assume_test_silo_mode(SiloMode.CONTROL):
  2070. uo1.delete()
  2071. def test_in_semver_projects_group_resolution_stores_current_release_version(self):
  2072. """
  2073. Test that ensures that when we resolve a group in the next release, then
  2074. GroupResolution.current_release_version is set to the latest release associated with a
  2075. Group, when the project follows semantic versioning scheme
  2076. """
  2077. release_1 = self.create_release(version="fake_package@21.1.0")
  2078. release_2 = self.create_release(version="fake_package@21.1.1")
  2079. release_3 = self.create_release(version="fake_package@21.1.2")
  2080. self.store_event(
  2081. data={
  2082. "timestamp": iso_format(before_now(seconds=10)),
  2083. "fingerprint": ["group-1"],
  2084. "release": release_2.version,
  2085. },
  2086. project_id=self.project.id,
  2087. )
  2088. group = self.store_event(
  2089. data={
  2090. "timestamp": iso_format(before_now(seconds=12)),
  2091. "fingerprint": ["group-1"],
  2092. "release": release_1.version,
  2093. },
  2094. project_id=self.project.id,
  2095. ).group
  2096. self.login_as(user=self.user)
  2097. response = self.get_success_response(
  2098. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2099. )
  2100. assert response.data["status"] == "resolved"
  2101. assert response.data["statusDetails"]["inNextRelease"]
  2102. # The current_release_version should be to the latest (in semver) release associated with
  2103. # a group
  2104. grp_resolution = GroupResolution.objects.filter(group=group)
  2105. assert len(grp_resolution) == 1
  2106. grp_resolution = grp_resolution.first()
  2107. assert grp_resolution.current_release_version == release_2.version
  2108. # "resolvedInNextRelease" with semver releases is considered as "resolvedInRelease"
  2109. assert grp_resolution.type == GroupResolution.Type.in_release
  2110. assert grp_resolution.status == GroupResolution.Status.resolved
  2111. # Add release that is between 2 and 3 to ensure that any release after release 2 should
  2112. # not have a resolution
  2113. release_4 = self.create_release(version="fake_package@21.1.1+1")
  2114. for release in [release_1, release_2]:
  2115. assert GroupResolution.has_resolution(group=group, release=release)
  2116. for release in [release_3, release_4]:
  2117. assert not GroupResolution.has_resolution(group=group, release=release)
  2118. # Ensure that Activity has `current_release_version` set on `Resolved in next release`
  2119. activity = Activity.objects.filter(
  2120. group=grp_resolution.group,
  2121. type=ActivityType.SET_RESOLVED_IN_RELEASE.value,
  2122. ident=grp_resolution.id,
  2123. ).first()
  2124. assert "current_release_version" in activity.data
  2125. assert activity.data["current_release_version"] == release_2.version
  2126. def test_in_non_semver_projects_group_resolution_stores_current_release_version(self):
  2127. """
  2128. Test that ensures that when we resolve a group in the next release, then
  2129. GroupResolution.current_release_version is set to the most recent release associated with a
  2130. Group, when the project does not follow semantic versioning scheme
  2131. """
  2132. release_1 = self.create_release(
  2133. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2134. )
  2135. release_2 = self.create_release(version="foobar 2")
  2136. group = self.store_event(
  2137. data={
  2138. "timestamp": iso_format(before_now(seconds=12)),
  2139. "fingerprint": ["group-1"],
  2140. "release": release_1.version,
  2141. },
  2142. project_id=self.project.id,
  2143. ).group
  2144. self.login_as(user=self.user)
  2145. response = self.get_success_response(
  2146. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2147. )
  2148. assert response.data["status"] == "resolved"
  2149. assert response.data["statusDetails"]["inNextRelease"]
  2150. # Add a new release that is between 1 and 2, to make sure that if a the same issue/group
  2151. # occurs in that issue, then it should not have a resolution
  2152. release_3 = self.create_release(
  2153. date_added=timezone.now() - timedelta(minutes=30), version="foobar 3"
  2154. )
  2155. grp_resolution = GroupResolution.objects.filter(group=group)
  2156. assert len(grp_resolution) == 1
  2157. assert grp_resolution[0].current_release_version == release_1.version
  2158. assert GroupResolution.has_resolution(group=group, release=release_1)
  2159. for release in [release_2, release_3]:
  2160. assert not GroupResolution.has_resolution(group=group, release=release)
  2161. def test_in_non_semver_projects_store_actual_current_release_version_not_cached_version(self):
  2162. """
  2163. Test that ensures that the current_release_version is actually the latest version
  2164. associated with a group, not the cached version because currently
  2165. `group.get_last_release` fetches the latest release associated with a group and caches
  2166. that value, and we don't want to cache that value when resolving in next release in case a
  2167. new release appears to be associated with a group because if we store the cached rather
  2168. than the actual latest release, we might have unexpected results with the regression
  2169. algorithm
  2170. """
  2171. release_1 = self.create_release(
  2172. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2173. )
  2174. release_2 = self.create_release(version="foobar 2")
  2175. group = self.store_event(
  2176. data={
  2177. "timestamp": iso_format(before_now(seconds=12)),
  2178. "fingerprint": ["group-1"],
  2179. "release": release_1.version,
  2180. },
  2181. project_id=self.project.id,
  2182. ).group
  2183. # Call this function to cache the `last_seen` release to release_1
  2184. # i.e. Set the first last observed by Sentry
  2185. assert group.get_last_release() == release_1.version
  2186. self.login_as(user=self.user)
  2187. self.store_event(
  2188. data={
  2189. "timestamp": iso_format(before_now(seconds=0)),
  2190. "fingerprint": ["group-1"],
  2191. "release": release_2.version,
  2192. },
  2193. project_id=self.project.id,
  2194. )
  2195. # Cached (i.e. first last observed release by Sentry) is returned here since `use_cache`
  2196. # is set to its default of `True`
  2197. assert Group.objects.get(id=group.id).get_last_release() == release_1.version
  2198. response = self.get_success_response(
  2199. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2200. )
  2201. assert response.data["status"] == "resolved"
  2202. assert response.data["statusDetails"]["inNextRelease"]
  2203. # Changes here to release_2 and actual latest because `resolvedInNextRelease`,
  2204. # sets `use_cache` to False when fetching the last release associated with a group
  2205. assert Group.objects.get(id=group.id).get_last_release() == release_2.version
  2206. grp_resolution = GroupResolution.objects.filter(group=group)
  2207. assert len(grp_resolution) == 1
  2208. assert grp_resolution[0].current_release_version == release_2.version
  2209. def test_in_non_semver_projects_resolved_in_next_release_is_equated_to_in_release(self):
  2210. """
  2211. Test that ensures that if we basically know the next release when clicking on Resolved
  2212. In Next Release because that release exists, then we can short circuit setting
  2213. GroupResolution to type "inNextRelease", and then having `clear_expired_resolutions` run
  2214. once a new release is created to convert GroupResolution to in_release and set Activity.
  2215. Basically we treat "ResolvedInNextRelease" as "ResolvedInRelease" when there is a release
  2216. that was created after the last release associated with the group being resolved
  2217. """
  2218. release_1 = self.create_release(
  2219. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2220. )
  2221. release_2 = self.create_release(version="foobar 2")
  2222. self.create_release(version="foobar 3")
  2223. group = self.store_event(
  2224. data={
  2225. "timestamp": iso_format(before_now(seconds=12)),
  2226. "fingerprint": ["group-1"],
  2227. "release": release_1.version,
  2228. },
  2229. project_id=self.project.id,
  2230. ).group
  2231. self.login_as(user=self.user)
  2232. response = self.get_success_response(
  2233. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2234. )
  2235. assert response.data["status"] == "resolved"
  2236. assert response.data["statusDetails"]["inNextRelease"]
  2237. grp_resolution = GroupResolution.objects.filter(group=group)
  2238. assert len(grp_resolution) == 1
  2239. grp_resolution = grp_resolution[0]
  2240. assert grp_resolution.current_release_version == release_1.version
  2241. assert grp_resolution.release.id == release_2.id
  2242. assert grp_resolution.type == GroupResolution.Type.in_release
  2243. assert grp_resolution.status == GroupResolution.Status.resolved
  2244. activity = Activity.objects.filter(
  2245. group=grp_resolution.group,
  2246. type=ActivityType.SET_RESOLVED_IN_RELEASE.value,
  2247. ident=grp_resolution.id,
  2248. ).first()
  2249. assert activity.data["version"] == release_2.version
  2250. def test_selective_status_update(self):
  2251. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2252. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2253. group3 = self.create_group(status=GroupStatus.IGNORED)
  2254. group4 = self.create_group(
  2255. project=self.create_project(slug="foo"),
  2256. status=GroupStatus.UNRESOLVED,
  2257. )
  2258. self.login_as(user=self.user)
  2259. with self.feature("organizations:global-views"):
  2260. response = self.get_success_response(
  2261. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, status="resolved"
  2262. )
  2263. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  2264. new_group1 = Group.objects.get(id=group1.id)
  2265. assert new_group1.resolved_at is not None
  2266. assert new_group1.status == GroupStatus.RESOLVED
  2267. new_group2 = Group.objects.get(id=group2.id)
  2268. assert new_group2.resolved_at is not None
  2269. assert new_group2.status == GroupStatus.RESOLVED
  2270. assert GroupSubscription.objects.filter(
  2271. user_id=self.user.id, group=new_group2, is_active=True
  2272. ).exists()
  2273. new_group3 = Group.objects.get(id=group3.id)
  2274. assert new_group3.resolved_at is None
  2275. assert new_group3.status == GroupStatus.IGNORED
  2276. new_group4 = Group.objects.get(id=group4.id)
  2277. assert new_group4.resolved_at is None
  2278. assert new_group4.status == GroupStatus.UNRESOLVED
  2279. def test_set_resolved_in_current_release(self):
  2280. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2281. release.add_project(self.project)
  2282. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2283. self.login_as(user=self.user)
  2284. response = self.get_success_response(
  2285. qs_params={"id": group.id}, status="resolved", statusDetails={"inRelease": "latest"}
  2286. )
  2287. assert response.data["status"] == "resolved"
  2288. assert response.data["statusDetails"]["inRelease"] == release.version
  2289. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2290. group = Group.objects.get(id=group.id)
  2291. assert group.status == GroupStatus.RESOLVED
  2292. resolution = GroupResolution.objects.get(group=group)
  2293. assert resolution.release == release
  2294. assert resolution.type == GroupResolution.Type.in_release
  2295. assert resolution.status == GroupResolution.Status.resolved
  2296. assert resolution.actor_id == self.user.id
  2297. assert GroupSubscription.objects.filter(
  2298. user_id=self.user.id, group=group, is_active=True
  2299. ).exists()
  2300. activity = Activity.objects.get(
  2301. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2302. )
  2303. assert activity.data["version"] == release.version
  2304. assert GroupHistory.objects.filter(
  2305. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_RELEASE
  2306. ).exists()
  2307. def test_set_resolved_in_explicit_release(self):
  2308. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2309. release.add_project(self.project)
  2310. release2 = Release.objects.create(organization_id=self.project.organization_id, version="b")
  2311. release2.add_project(self.project)
  2312. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2313. self.login_as(user=self.user)
  2314. response = self.get_success_response(
  2315. qs_params={"id": group.id},
  2316. status="resolved",
  2317. statusDetails={"inRelease": release.version},
  2318. )
  2319. assert response.data["status"] == "resolved"
  2320. assert response.data["statusDetails"]["inRelease"] == release.version
  2321. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2322. assert "activity" in response.data
  2323. group = Group.objects.get(id=group.id)
  2324. assert group.status == GroupStatus.RESOLVED
  2325. resolution = GroupResolution.objects.get(group=group)
  2326. assert resolution.release == release
  2327. assert resolution.type == GroupResolution.Type.in_release
  2328. assert resolution.status == GroupResolution.Status.resolved
  2329. assert resolution.actor_id == self.user.id
  2330. assert GroupSubscription.objects.filter(
  2331. user_id=self.user.id, group=group, is_active=True
  2332. ).exists()
  2333. activity = Activity.objects.get(
  2334. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2335. )
  2336. assert activity.data["version"] == release.version
  2337. def test_in_semver_projects_set_resolved_in_explicit_release(self):
  2338. release_1 = self.create_release(version="fake_package@3.0.0")
  2339. release_2 = self.create_release(version="fake_package@2.0.0")
  2340. release_3 = self.create_release(version="fake_package@3.0.1")
  2341. group = self.store_event(
  2342. data={
  2343. "timestamp": iso_format(before_now(seconds=10)),
  2344. "fingerprint": ["group-1"],
  2345. "release": release_1.version,
  2346. },
  2347. project_id=self.project.id,
  2348. ).group
  2349. self.login_as(user=self.user)
  2350. response = self.get_success_response(
  2351. qs_params={"id": group.id},
  2352. status="resolved",
  2353. statusDetails={"inRelease": release_1.version},
  2354. )
  2355. assert response.data["status"] == "resolved"
  2356. assert response.data["statusDetails"]["inRelease"] == release_1.version
  2357. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2358. assert "activity" in response.data
  2359. group = Group.objects.get(id=group.id)
  2360. assert group.status == GroupStatus.RESOLVED
  2361. resolution = GroupResolution.objects.get(group=group)
  2362. assert resolution.release == release_1
  2363. assert resolution.type == GroupResolution.Type.in_release
  2364. assert resolution.status == GroupResolution.Status.resolved
  2365. assert resolution.actor_id == self.user.id
  2366. assert GroupSubscription.objects.filter(
  2367. user_id=self.user.id, group=group, is_active=True
  2368. ).exists()
  2369. activity = Activity.objects.get(
  2370. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2371. )
  2372. assert activity.data["version"] == release_1.version
  2373. assert GroupResolution.has_resolution(group=group, release=release_2)
  2374. assert not GroupResolution.has_resolution(group=group, release=release_3)
  2375. def test_set_resolved_in_next_release(self):
  2376. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2377. release.add_project(self.project)
  2378. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2379. self.login_as(user=self.user)
  2380. response = self.get_success_response(
  2381. qs_params={"id": group.id}, status="resolved", statusDetails={"inNextRelease": True}
  2382. )
  2383. assert response.data["status"] == "resolved"
  2384. assert response.data["statusDetails"]["inNextRelease"]
  2385. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2386. assert "activity" in response.data
  2387. group = Group.objects.get(id=group.id)
  2388. assert group.status == GroupStatus.RESOLVED
  2389. resolution = GroupResolution.objects.get(group=group)
  2390. assert resolution.release == release
  2391. assert resolution.type == GroupResolution.Type.in_next_release
  2392. assert resolution.status == GroupResolution.Status.pending
  2393. assert resolution.actor_id == self.user.id
  2394. assert GroupSubscription.objects.filter(
  2395. user_id=self.user.id, group=group, is_active=True
  2396. ).exists()
  2397. activity = Activity.objects.get(
  2398. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2399. )
  2400. assert activity.data["version"] == ""
  2401. def test_set_resolved_in_next_release_legacy(self):
  2402. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2403. release.add_project(self.project)
  2404. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2405. self.login_as(user=self.user)
  2406. response = self.get_success_response(
  2407. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2408. )
  2409. assert response.data["status"] == "resolved"
  2410. assert response.data["statusDetails"]["inNextRelease"]
  2411. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2412. assert "activity" in response.data
  2413. group = Group.objects.get(id=group.id)
  2414. assert group.status == GroupStatus.RESOLVED
  2415. resolution = GroupResolution.objects.get(group=group)
  2416. assert resolution.release == release
  2417. assert resolution.type == GroupResolution.Type.in_next_release
  2418. assert resolution.status == GroupResolution.Status.pending
  2419. assert resolution.actor_id == self.user.id
  2420. assert GroupSubscription.objects.filter(
  2421. user_id=self.user.id, group=group, is_active=True
  2422. ).exists()
  2423. assert GroupHistory.objects.filter(
  2424. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_RELEASE
  2425. ).exists()
  2426. activity = Activity.objects.get(
  2427. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2428. )
  2429. assert activity.data["version"] == ""
  2430. def test_set_resolved_in_explicit_commit_unreleased(self):
  2431. repo = self.create_repo(project=self.project, name=self.project.name)
  2432. commit = self.create_commit(project=self.project, repo=repo)
  2433. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2434. self.login_as(user=self.user)
  2435. response = self.get_success_response(
  2436. qs_params={"id": group.id},
  2437. status="resolved",
  2438. statusDetails={"inCommit": {"commit": commit.key, "repository": repo.name}},
  2439. )
  2440. assert response.data["status"] == "resolved"
  2441. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  2442. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2443. assert "activity" not in response.data
  2444. group = Group.objects.get(id=group.id)
  2445. assert group.status == GroupStatus.RESOLVED
  2446. link = GroupLink.objects.get(group_id=group.id)
  2447. assert link.linked_type == GroupLink.LinkedType.commit
  2448. assert link.relationship == GroupLink.Relationship.resolves
  2449. assert link.linked_id == commit.id
  2450. assert GroupSubscription.objects.filter(
  2451. user_id=self.user.id, group=group, is_active=True
  2452. ).exists()
  2453. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  2454. assert activity.data["commit"] == commit.id
  2455. assert GroupHistory.objects.filter(
  2456. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2457. ).exists()
  2458. def test_set_resolved_in_explicit_commit_released(self):
  2459. release = self.create_release(project=self.project)
  2460. repo = self.create_repo(project=self.project, name=self.project.name)
  2461. commit = self.create_commit(project=self.project, repo=repo, release=release)
  2462. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2463. self.login_as(user=self.user)
  2464. response = self.get_success_response(
  2465. qs_params={"id": group.id},
  2466. status="resolved",
  2467. statusDetails={"inCommit": {"commit": commit.key, "repository": repo.name}},
  2468. )
  2469. assert response.data["status"] == "resolved"
  2470. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  2471. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2472. assert "activity" in response.data
  2473. group = Group.objects.get(id=group.id)
  2474. assert group.status == GroupStatus.RESOLVED
  2475. link = GroupLink.objects.get(group_id=group.id)
  2476. assert link.project_id == self.project.id
  2477. assert link.linked_type == GroupLink.LinkedType.commit
  2478. assert link.relationship == GroupLink.Relationship.resolves
  2479. assert link.linked_id == commit.id
  2480. assert GroupSubscription.objects.filter(
  2481. user_id=self.user.id, group=group, is_active=True
  2482. ).exists()
  2483. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  2484. assert activity.data["commit"] == commit.id
  2485. resolution = GroupResolution.objects.get(group=group)
  2486. assert resolution.type == GroupResolution.Type.in_release
  2487. assert resolution.status == GroupResolution.Status.resolved
  2488. assert GroupHistory.objects.filter(
  2489. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2490. ).exists()
  2491. def test_set_resolved_in_explicit_commit_missing(self):
  2492. repo = self.create_repo(project=self.project, name=self.project.name)
  2493. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2494. self.login_as(user=self.user)
  2495. response = self.get_response(
  2496. qs_params={"id": group.id},
  2497. status="resolved",
  2498. statusDetails={"inCommit": {"commit": "a" * 40, "repository": repo.name}},
  2499. )
  2500. assert response.status_code == 400
  2501. assert (
  2502. response.data["statusDetails"]["inCommit"]["commit"][0]
  2503. == "Unable to find the given commit."
  2504. )
  2505. assert not GroupHistory.objects.filter(
  2506. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2507. ).exists()
  2508. def test_set_unresolved(self):
  2509. release = self.create_release(project=self.project, version="abc")
  2510. group = self.create_group(status=GroupStatus.RESOLVED)
  2511. GroupResolution.objects.create(group=group, release=release)
  2512. self.login_as(user=self.user)
  2513. response = self.get_success_response(qs_params={"id": group.id}, status="unresolved")
  2514. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2515. group = Group.objects.get(id=group.id)
  2516. assert group.status == GroupStatus.UNRESOLVED
  2517. assert GroupHistory.objects.filter(
  2518. group=group, status=GroupHistoryStatus.UNRESOLVED
  2519. ).exists()
  2520. self.assertNoResolution(group)
  2521. assert GroupSubscription.objects.filter(
  2522. user_id=self.user.id, group=group, is_active=True
  2523. ).exists()
  2524. def test_set_unresolved_on_snooze(self):
  2525. group = self.create_group(status=GroupStatus.IGNORED)
  2526. GroupSnooze.objects.create(group=group, until=timezone.now() - timedelta(days=1))
  2527. self.login_as(user=self.user)
  2528. response = self.get_success_response(qs_params={"id": group.id}, status="unresolved")
  2529. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2530. group = Group.objects.get(id=group.id)
  2531. assert group.status == GroupStatus.UNRESOLVED
  2532. assert GroupHistory.objects.filter(
  2533. group=group, status=GroupHistoryStatus.UNRESOLVED
  2534. ).exists()
  2535. def test_basic_ignore(self):
  2536. group = self.create_group(status=GroupStatus.RESOLVED)
  2537. snooze = GroupSnooze.objects.create(group=group, until=timezone.now())
  2538. self.login_as(user=self.user)
  2539. assert not GroupHistory.objects.filter(
  2540. group=group, status=GroupHistoryStatus.IGNORED
  2541. ).exists()
  2542. response = self.get_success_response(qs_params={"id": group.id}, status="ignored")
  2543. # existing snooze objects should be cleaned up
  2544. assert not GroupSnooze.objects.filter(id=snooze.id).exists()
  2545. group = Group.objects.get(id=group.id)
  2546. assert group.status == GroupStatus.IGNORED
  2547. assert GroupHistory.objects.filter(group=group, status=GroupHistoryStatus.IGNORED).exists()
  2548. assert response.data == {"status": "ignored", "statusDetails": {}, "inbox": None}
  2549. def test_snooze_duration(self):
  2550. group = self.create_group(status=GroupStatus.RESOLVED)
  2551. self.login_as(user=self.user)
  2552. response = self.get_success_response(
  2553. qs_params={"id": group.id}, status="ignored", ignoreDuration=30
  2554. )
  2555. snooze = GroupSnooze.objects.get(group=group)
  2556. snooze.until = snooze.until
  2557. now = timezone.now()
  2558. assert snooze.count is None
  2559. assert snooze.until is not None
  2560. assert snooze.until > now + timedelta(minutes=29)
  2561. assert snooze.until < now + timedelta(minutes=31)
  2562. assert snooze.user_count is None
  2563. assert snooze.user_window is None
  2564. assert snooze.window is None
  2565. response.data["statusDetails"]["ignoreUntil"] = response.data["statusDetails"][
  2566. "ignoreUntil"
  2567. ]
  2568. assert response.data["status"] == "ignored"
  2569. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2570. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2571. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2572. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2573. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2574. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2575. def test_snooze_count(self):
  2576. group = self.create_group(status=GroupStatus.RESOLVED, times_seen=1)
  2577. self.login_as(user=self.user)
  2578. response = self.get_success_response(
  2579. qs_params={"id": group.id}, status="ignored", ignoreCount=100
  2580. )
  2581. snooze = GroupSnooze.objects.get(group=group)
  2582. assert snooze.count == 100
  2583. assert snooze.until is None
  2584. assert snooze.user_count is None
  2585. assert snooze.user_window is None
  2586. assert snooze.window is None
  2587. assert snooze.state["times_seen"] == 1
  2588. assert response.data["status"] == "ignored"
  2589. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2590. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2591. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2592. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2593. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2594. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2595. def test_snooze_user_count(self):
  2596. for i in range(10):
  2597. event = self.store_event(
  2598. data={
  2599. "fingerprint": ["put-me-in-group-1"],
  2600. "user": {"id": str(i)},
  2601. "timestamp": iso_format(self.min_ago + timedelta(seconds=i)),
  2602. },
  2603. project_id=self.project.id,
  2604. )
  2605. assert event.group is not None
  2606. group = Group.objects.get(id=event.group.id)
  2607. group.status = GroupStatus.RESOLVED
  2608. group.substatus = None
  2609. group.save()
  2610. self.login_as(user=self.user)
  2611. response = self.get_success_response(
  2612. qs_params={"id": group.id}, status="ignored", ignoreUserCount=10
  2613. )
  2614. snooze = GroupSnooze.objects.get(group=group)
  2615. assert snooze.count is None
  2616. assert snooze.until is None
  2617. assert snooze.user_count == 10
  2618. assert snooze.user_window is None
  2619. assert snooze.window is None
  2620. assert snooze.state["users_seen"] == 10
  2621. assert response.data["status"] == "ignored"
  2622. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2623. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2624. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2625. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2626. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2627. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2628. def test_set_bookmarked(self):
  2629. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2630. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2631. group3 = self.create_group(status=GroupStatus.IGNORED)
  2632. group4 = self.create_group(
  2633. project=self.create_project(slug="foo"),
  2634. status=GroupStatus.UNRESOLVED,
  2635. )
  2636. self.login_as(user=self.user)
  2637. with self.feature("organizations:global-views"):
  2638. response = self.get_success_response(
  2639. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, isBookmarked="true"
  2640. )
  2641. assert response.data == {"isBookmarked": True}
  2642. bookmark1 = GroupBookmark.objects.filter(group=group1, user_id=self.user.id)
  2643. assert bookmark1.exists()
  2644. assert GroupSubscription.objects.filter(
  2645. user_id=self.user.id, group=group1, is_active=True
  2646. ).exists()
  2647. bookmark2 = GroupBookmark.objects.filter(group=group2, user_id=self.user.id)
  2648. assert bookmark2.exists()
  2649. assert GroupSubscription.objects.filter(
  2650. user_id=self.user.id, group=group2, is_active=True
  2651. ).exists()
  2652. bookmark3 = GroupBookmark.objects.filter(group=group3, user_id=self.user.id)
  2653. assert not bookmark3.exists()
  2654. bookmark4 = GroupBookmark.objects.filter(group=group4, user_id=self.user.id)
  2655. assert not bookmark4.exists()
  2656. def test_subscription(self):
  2657. group1 = self.create_group()
  2658. group2 = self.create_group()
  2659. group3 = self.create_group()
  2660. group4 = self.create_group(project=self.create_project(slug="foo"))
  2661. self.login_as(user=self.user)
  2662. with self.feature("organizations:global-views"):
  2663. response = self.get_success_response(
  2664. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, isSubscribed="true"
  2665. )
  2666. assert response.data == {"isSubscribed": True, "subscriptionDetails": {"reason": "unknown"}}
  2667. assert GroupSubscription.objects.filter(
  2668. group=group1, user_id=self.user.id, is_active=True
  2669. ).exists()
  2670. assert GroupSubscription.objects.filter(
  2671. group=group2, user_id=self.user.id, is_active=True
  2672. ).exists()
  2673. assert not GroupSubscription.objects.filter(group=group3, user_id=self.user.id).exists()
  2674. assert not GroupSubscription.objects.filter(group=group4, user_id=self.user.id).exists()
  2675. def test_set_public(self):
  2676. group1 = self.create_group()
  2677. group2 = self.create_group()
  2678. self.login_as(user=self.user)
  2679. response = self.get_success_response(
  2680. qs_params={"id": [group1.id, group2.id]}, isPublic="true"
  2681. )
  2682. assert response.data["isPublic"] is True
  2683. assert "shareId" in response.data
  2684. new_group1 = Group.objects.get(id=group1.id)
  2685. assert bool(new_group1.get_share_id())
  2686. new_group2 = Group.objects.get(id=group2.id)
  2687. assert bool(new_group2.get_share_id())
  2688. def test_set_private(self):
  2689. group1 = self.create_group()
  2690. group2 = self.create_group()
  2691. # Manually mark them as shared
  2692. for g in group1, group2:
  2693. GroupShare.objects.create(project_id=g.project_id, group=g)
  2694. assert bool(g.get_share_id())
  2695. self.login_as(user=self.user)
  2696. response = self.get_success_response(
  2697. qs_params={"id": [group1.id, group2.id]}, isPublic="false"
  2698. )
  2699. assert response.data == {"isPublic": False, "shareId": None}
  2700. new_group1 = Group.objects.get(id=group1.id)
  2701. assert not bool(new_group1.get_share_id())
  2702. new_group2 = Group.objects.get(id=group2.id)
  2703. assert not bool(new_group2.get_share_id())
  2704. def test_set_has_seen(self):
  2705. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2706. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2707. group3 = self.create_group(status=GroupStatus.IGNORED)
  2708. group4 = self.create_group(
  2709. project=self.create_project(slug="foo"),
  2710. status=GroupStatus.UNRESOLVED,
  2711. )
  2712. self.login_as(user=self.user)
  2713. with self.feature("organizations:global-views"):
  2714. response = self.get_success_response(
  2715. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, hasSeen="true"
  2716. )
  2717. assert response.data == {"hasSeen": True}
  2718. r1 = GroupSeen.objects.filter(group=group1, user_id=self.user.id)
  2719. assert r1.exists()
  2720. r2 = GroupSeen.objects.filter(group=group2, user_id=self.user.id)
  2721. assert r2.exists()
  2722. r3 = GroupSeen.objects.filter(group=group3, user_id=self.user.id)
  2723. assert not r3.exists()
  2724. r4 = GroupSeen.objects.filter(group=group4, user_id=self.user.id)
  2725. assert not r4.exists()
  2726. @patch("sentry.issues.merge.uuid4")
  2727. @patch("sentry.issues.merge.merge_groups")
  2728. @patch("sentry.eventstream.backend")
  2729. def test_merge(self, mock_eventstream, merge_groups, mock_uuid4):
  2730. eventstream_state = object()
  2731. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  2732. mock_uuid4.return_value = self.get_mock_uuid()
  2733. group1 = self.create_group(times_seen=1)
  2734. group2 = self.create_group(times_seen=50)
  2735. group3 = self.create_group(times_seen=2)
  2736. self.create_group()
  2737. self.login_as(user=self.user)
  2738. response = self.get_success_response(
  2739. qs_params={"id": [group1.id, group2.id, group3.id]}, merge="1"
  2740. )
  2741. assert response.data["merge"]["parent"] == str(group2.id)
  2742. assert sorted(response.data["merge"]["children"]) == sorted(
  2743. [str(group1.id), str(group3.id)]
  2744. )
  2745. mock_eventstream.start_merge.assert_called_once_with(
  2746. group1.project_id, [group3.id, group1.id], group2.id
  2747. )
  2748. assert len(merge_groups.mock_calls) == 1
  2749. merge_groups.delay.assert_any_call(
  2750. from_object_ids=[group3.id, group1.id],
  2751. to_object_id=group2.id,
  2752. transaction_id="abc123",
  2753. eventstream_state=eventstream_state,
  2754. )
  2755. @patch("sentry.issues.merge.uuid4")
  2756. @patch("sentry.issues.merge.merge_groups")
  2757. @patch("sentry.eventstream.backend")
  2758. def test_merge_performance_issues(self, mock_eventstream, merge_groups, mock_uuid4):
  2759. eventstream_state = object()
  2760. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  2761. mock_uuid4.return_value = self.get_mock_uuid()
  2762. group1 = self.create_group(times_seen=1, type=PerformanceSlowDBQueryGroupType.type_id)
  2763. group2 = self.create_group(times_seen=50, type=PerformanceSlowDBQueryGroupType.type_id)
  2764. group3 = self.create_group(times_seen=2, type=PerformanceSlowDBQueryGroupType.type_id)
  2765. self.create_group()
  2766. self.login_as(user=self.user)
  2767. response = self.get_error_response(
  2768. qs_params={"id": [group1.id, group2.id, group3.id]}, merge="1"
  2769. )
  2770. assert response.status_code == 400, response.content
  2771. def test_assign(self):
  2772. group1 = self.create_group(is_public=True)
  2773. group2 = self.create_group(is_public=True)
  2774. user = self.user
  2775. self.login_as(user=user)
  2776. response = self.get_success_response(qs_params={"id": group1.id}, assignedTo=user.username)
  2777. assert response.data["assignedTo"]["id"] == str(user.id)
  2778. assert response.data["assignedTo"]["type"] == "user"
  2779. assert GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  2780. assert GroupHistory.objects.filter(
  2781. group=group1, status=GroupHistoryStatus.ASSIGNED
  2782. ).exists()
  2783. assert not GroupAssignee.objects.filter(group=group2, user_id=user.id).exists()
  2784. assert (
  2785. Activity.objects.filter(
  2786. group=group1, user_id=user.id, type=ActivityType.ASSIGNED.value
  2787. ).count()
  2788. == 1
  2789. )
  2790. assert GroupSubscription.objects.filter(
  2791. user_id=user.id, group=group1, is_active=True
  2792. ).exists()
  2793. response = self.get_success_response(qs_params={"id": group1.id}, assignedTo="")
  2794. assert response.data["assignedTo"] is None
  2795. assert not GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  2796. assert GroupHistory.objects.filter(
  2797. group=group1, status=GroupHistoryStatus.UNASSIGNED
  2798. ).exists()
  2799. def test_assign_non_member(self):
  2800. group = self.create_group(is_public=True)
  2801. member = self.user
  2802. non_member = self.create_user("bar@example.com")
  2803. self.login_as(user=member)
  2804. response = self.get_response(qs_params={"id": group.id}, assignedTo=non_member.username)
  2805. assert not GroupHistory.objects.filter(
  2806. group=group, status=GroupHistoryStatus.ASSIGNED
  2807. ).exists()
  2808. assert response.status_code == 400, response.content
  2809. def test_assign_team(self):
  2810. self.login_as(user=self.user)
  2811. group = self.create_group()
  2812. other_member = self.create_user("bar@example.com")
  2813. team = self.create_team(
  2814. organization=group.project.organization, members=[self.user, other_member]
  2815. )
  2816. group.project.add_team(team)
  2817. assert not GroupHistory.objects.filter(
  2818. group=group, status=GroupHistoryStatus.ASSIGNED
  2819. ).exists()
  2820. response = self.get_success_response(
  2821. qs_params={"id": group.id}, assignedTo=f"team:{team.id}"
  2822. )
  2823. assert response.data["assignedTo"]["id"] == str(team.id)
  2824. assert response.data["assignedTo"]["type"] == "team"
  2825. assert GroupHistory.objects.filter(group=group, status=GroupHistoryStatus.ASSIGNED).exists()
  2826. assert GroupAssignee.objects.filter(group=group, team=team).exists()
  2827. assert Activity.objects.filter(group=group, type=ActivityType.ASSIGNED.value).count() == 1
  2828. assert GroupSubscription.objects.filter(group=group, is_active=True).count() == 2
  2829. response = self.get_success_response(qs_params={"id": group.id}, assignedTo="")
  2830. assert response.data["assignedTo"] is None
  2831. assert GroupHistory.objects.filter(
  2832. group=group, status=GroupHistoryStatus.UNASSIGNED
  2833. ).exists()
  2834. def test_discard(self):
  2835. group1 = self.create_group(is_public=True)
  2836. group2 = self.create_group(is_public=True)
  2837. group_hash = GroupHash.objects.create(hash="x" * 32, project=group1.project, group=group1)
  2838. user = self.user
  2839. self.login_as(user=user)
  2840. with self.tasks():
  2841. with self.feature("projects:discard-groups"):
  2842. response = self.get_response(qs_params={"id": group1.id}, discard=True)
  2843. assert response.status_code == 204
  2844. assert not Group.objects.filter(id=group1.id).exists()
  2845. assert Group.objects.filter(id=group2.id).exists()
  2846. assert GroupHash.objects.filter(id=group_hash.id).exists()
  2847. tombstone = GroupTombstone.objects.get(
  2848. id=GroupHash.objects.get(id=group_hash.id).group_tombstone_id
  2849. )
  2850. assert tombstone.message == group1.message
  2851. assert tombstone.culprit == group1.culprit
  2852. assert tombstone.project == group1.project
  2853. assert tombstone.data == group1.data
  2854. @override_settings(SENTRY_SELF_HOSTED=False)
  2855. def test_ratelimit(self):
  2856. self.login_as(user=self.user)
  2857. with freeze_time("2000-01-01"):
  2858. for i in range(5):
  2859. self.get_success_response()
  2860. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  2861. def test_set_inbox(self):
  2862. group1 = self.create_group()
  2863. group2 = self.create_group()
  2864. self.login_as(user=self.user)
  2865. response = self.get_success_response(qs_params={"id": [group1.id, group2.id]}, inbox="true")
  2866. assert response.data == {"inbox": True}
  2867. assert GroupInbox.objects.filter(group=group1).exists()
  2868. assert GroupInbox.objects.filter(group=group2).exists()
  2869. assert not GroupHistory.objects.filter(
  2870. group=group1, status=GroupHistoryStatus.REVIEWED
  2871. ).exists()
  2872. assert not GroupHistory.objects.filter(
  2873. group=group2, status=GroupHistoryStatus.REVIEWED
  2874. ).exists()
  2875. response = self.get_success_response(qs_params={"id": [group2.id]}, inbox="false")
  2876. assert response.data == {"inbox": False}
  2877. assert GroupInbox.objects.filter(group=group1).exists()
  2878. assert not GroupHistory.objects.filter(
  2879. group=group1, status=GroupHistoryStatus.REVIEWED
  2880. ).exists()
  2881. assert GroupHistory.objects.filter(
  2882. group=group2, status=GroupHistoryStatus.REVIEWED
  2883. ).exists()
  2884. assert not GroupInbox.objects.filter(group=group2).exists()
  2885. def test_set_resolved_inbox(self):
  2886. group1 = self.create_group()
  2887. group2 = self.create_group()
  2888. self.login_as(user=self.user)
  2889. response = self.get_success_response(
  2890. qs_params={"id": [group1.id, group2.id]}, status="resolved"
  2891. )
  2892. assert response.data["inbox"] is None
  2893. assert not GroupInbox.objects.filter(group=group1).exists()
  2894. assert not GroupInbox.objects.filter(group=group2).exists()
  2895. self.get_success_response(qs_params={"id": [group2.id]}, status="unresolved")
  2896. assert not GroupInbox.objects.filter(group=group1).exists()
  2897. assert not GroupInbox.objects.filter(group=group2).exists()
  2898. assert not GroupHistory.objects.filter(
  2899. group=group1, status=GroupHistoryStatus.UNRESOLVED
  2900. ).exists()
  2901. assert GroupHistory.objects.filter(
  2902. group=group2, status=GroupHistoryStatus.UNRESOLVED
  2903. ).exists()
  2904. @region_silo_test(stable=True)
  2905. class GroupDeleteTest(APITestCase, SnubaTestCase):
  2906. endpoint = "sentry-api-0-organization-group-index"
  2907. method = "delete"
  2908. def get_response(self, *args, **kwargs):
  2909. if not args:
  2910. org = self.project.organization.slug
  2911. else:
  2912. org = args[0]
  2913. return super().get_response(org, **kwargs)
  2914. @patch("sentry.eventstream.backend")
  2915. def test_delete_by_id(self, mock_eventstream):
  2916. eventstream_state = {"event_stream_state": uuid4()}
  2917. mock_eventstream.start_delete_groups = Mock(return_value=eventstream_state)
  2918. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2919. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2920. group3 = self.create_group(status=GroupStatus.IGNORED)
  2921. group4 = self.create_group(
  2922. project=self.create_project(slug="foo"),
  2923. status=GroupStatus.UNRESOLVED,
  2924. )
  2925. hashes = []
  2926. for g in group1, group2, group3, group4:
  2927. hash = uuid4().hex
  2928. hashes.append(hash)
  2929. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  2930. self.login_as(user=self.user)
  2931. with self.feature("organizations:global-views"):
  2932. response = self.get_response(
  2933. qs_params={"id": [group1.id, group2.id], "group4": group4.id}
  2934. )
  2935. mock_eventstream.start_delete_groups.assert_called_once_with(
  2936. group1.project_id, [group1.id, group2.id]
  2937. )
  2938. assert response.status_code == 204
  2939. assert Group.objects.get(id=group1.id).status == GroupStatus.PENDING_DELETION
  2940. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  2941. assert Group.objects.get(id=group2.id).status == GroupStatus.PENDING_DELETION
  2942. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  2943. assert Group.objects.get(id=group3.id).status != GroupStatus.PENDING_DELETION
  2944. assert GroupHash.objects.filter(group_id=group3.id).exists()
  2945. assert Group.objects.get(id=group4.id).status != GroupStatus.PENDING_DELETION
  2946. assert GroupHash.objects.filter(group_id=group4.id).exists()
  2947. Group.objects.filter(id__in=(group1.id, group2.id)).update(status=GroupStatus.UNRESOLVED)
  2948. with self.tasks():
  2949. with self.feature("organizations:global-views"):
  2950. response = self.get_response(
  2951. qs_params={"id": [group1.id, group2.id], "group4": group4.id}
  2952. )
  2953. # XXX(markus): Something is sending duplicated replacements to snuba --
  2954. # once from within tasks.deletions.groups and another time from
  2955. # sentry.deletions.defaults.groups
  2956. assert mock_eventstream.end_delete_groups.call_args_list == [
  2957. call(eventstream_state),
  2958. call(eventstream_state),
  2959. ]
  2960. assert response.status_code == 204
  2961. assert not Group.objects.filter(id=group1.id).exists()
  2962. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  2963. assert not Group.objects.filter(id=group2.id).exists()
  2964. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  2965. assert Group.objects.filter(id=group3.id).exists()
  2966. assert GroupHash.objects.filter(group_id=group3.id).exists()
  2967. assert Group.objects.filter(id=group4.id).exists()
  2968. assert GroupHash.objects.filter(group_id=group4.id).exists()
  2969. @patch("sentry.eventstream.backend")
  2970. def test_delete_performance_issue_by_id(self, mock_eventstream):
  2971. eventstream_state = {"event_stream_state": uuid4()}
  2972. mock_eventstream.start_delete_groups = Mock(return_value=eventstream_state)
  2973. group1 = self.create_group(
  2974. status=GroupStatus.RESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  2975. )
  2976. group2 = self.create_group(
  2977. status=GroupStatus.UNRESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  2978. )
  2979. hashes = []
  2980. for g in group1, group2:
  2981. hash = uuid4().hex
  2982. hashes.append(hash)
  2983. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  2984. self.login_as(user=self.user)
  2985. with self.feature("organizations:global-views"):
  2986. response = self.get_response(qs_params={"id": [group1.id, group2.id]})
  2987. assert response.status_code == 400
  2988. assert Group.objects.filter(id=group1.id).exists()
  2989. assert GroupHash.objects.filter(group_id=group1.id).exists()
  2990. assert Group.objects.filter(id=group2.id).exists()
  2991. assert GroupHash.objects.filter(group_id=group2.id).exists()
  2992. def test_bulk_delete(self):
  2993. groups = []
  2994. for i in range(10, 41):
  2995. groups.append(
  2996. self.create_group(
  2997. project=self.project,
  2998. status=GroupStatus.RESOLVED,
  2999. )
  3000. )
  3001. hashes = []
  3002. for group in groups:
  3003. hash = uuid4().hex
  3004. hashes.append(hash)
  3005. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  3006. self.login_as(user=self.user)
  3007. # if query is '' it defaults to is:unresolved
  3008. response = self.get_response(qs_params={"query": ""})
  3009. assert response.status_code == 204
  3010. for group in groups:
  3011. assert Group.objects.get(id=group.id).status == GroupStatus.PENDING_DELETION
  3012. assert not GroupHash.objects.filter(group_id=group.id).exists()
  3013. Group.objects.filter(id__in=[group.id for group in groups]).update(
  3014. status=GroupStatus.UNRESOLVED
  3015. )
  3016. with self.tasks():
  3017. response = self.get_response(qs_params={"query": ""})
  3018. assert response.status_code == 204
  3019. for group in groups:
  3020. assert not Group.objects.filter(id=group.id).exists()
  3021. assert not GroupHash.objects.filter(group_id=group.id).exists()
  3022. @override_settings(SENTRY_SELF_HOSTED=False)
  3023. def test_ratelimit(self):
  3024. self.login_as(user=self.user)
  3025. with freeze_time("2000-01-01"):
  3026. for i in range(5):
  3027. self.get_success_response()
  3028. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  3029. def test_bulk_delete_performance_issues(self):
  3030. groups = []
  3031. for i in range(10, 41):
  3032. groups.append(
  3033. self.create_group(
  3034. project=self.project,
  3035. status=GroupStatus.RESOLVED,
  3036. type=PerformanceSlowDBQueryGroupType.type_id,
  3037. )
  3038. )
  3039. hashes = []
  3040. for group in groups:
  3041. hash = uuid4().hex
  3042. hashes.append(hash)
  3043. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  3044. self.login_as(user=self.user)
  3045. # if query is '' it defaults to is:unresolved
  3046. response = self.get_response(qs_params={"query": ""})
  3047. assert response.status_code == 400
  3048. for group in groups:
  3049. assert Group.objects.filter(id=group.id).exists()
  3050. assert GroupHash.objects.filter(group_id=group.id).exists()