test_organization_group_index.py 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547
  1. import functools
  2. from datetime import timedelta
  3. from unittest.mock import Mock, 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.testutils import APITestCase, SnubaTestCase
  51. from sentry.testutils.helpers import parse_link_header
  52. from sentry.testutils.helpers.datetime import before_now, iso_format
  53. from sentry.testutils.helpers.features import Feature, with_feature
  54. from sentry.testutils.silo import exempt_from_silo_limits, region_silo_test
  55. from sentry.types.activity import ActivityType
  56. from sentry.types.group import GroupSubStatus
  57. from sentry.utils import json
  58. @region_silo_test(stable=True)
  59. class GroupListTest(APITestCase, SnubaTestCase):
  60. endpoint = "sentry-api-0-organization-group-index"
  61. def setUp(self):
  62. super().setUp()
  63. self.min_ago = before_now(minutes=1)
  64. def _parse_links(self, header):
  65. # links come in {url: {...attrs}}, but we need {rel: {...attrs}}
  66. links = {}
  67. for url, attrs in parse_link_header(header).items():
  68. links[attrs["rel"]] = attrs
  69. attrs["href"] = url
  70. return links
  71. def get_response(self, *args, **kwargs):
  72. if not args:
  73. org = self.project.organization.slug
  74. else:
  75. org = args[0]
  76. return super().get_response(org, **kwargs)
  77. def test_sort_by_date_with_tag(self):
  78. # XXX(dcramer): this tests a case where an ambiguous column name existed
  79. event = self.store_event(
  80. data={"event_id": "a" * 32, "timestamp": iso_format(before_now(seconds=1))},
  81. project_id=self.project.id,
  82. )
  83. group = event.group
  84. self.login_as(user=self.user)
  85. response = self.get_success_response(sort_by="date", query="is:unresolved")
  86. assert len(response.data) == 1
  87. assert response.data[0]["id"] == str(group.id)
  88. def test_query_for_archived(self):
  89. event = self.store_event(
  90. data={"event_id": "a" * 32, "timestamp": iso_format(before_now(seconds=1))},
  91. project_id=self.project.id,
  92. )
  93. group = event.group
  94. Group.objects.update_group_status(
  95. groups=[group],
  96. status=GroupStatus.IGNORED,
  97. substatus=None,
  98. activity_type=ActivityType.SET_IGNORED,
  99. )
  100. self.login_as(user=self.user)
  101. response = self.get_success_response(sort_by="date", query="is:archived")
  102. assert len(response.data) == 1
  103. assert response.data[0]["id"] == str(group.id)
  104. @with_feature("organizations:issue-list-better-priority-sort")
  105. def test_sort_by_better_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="betterPriority",
  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 exempt_from_silo_limits():
  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
  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. integration = Integration.objects.create(provider="example", name="Example")
  1935. integration.add_organization(org, self.user)
  1936. event = self.store_event(
  1937. data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id
  1938. )
  1939. group = event.group
  1940. OrganizationIntegration.objects.filter(
  1941. integration_id=integration.id, organization_id=group.organization.id
  1942. ).update(
  1943. config={
  1944. "sync_comments": True,
  1945. "sync_status_outbound": True,
  1946. "sync_status_inbound": True,
  1947. "sync_assignee_outbound": True,
  1948. "sync_assignee_inbound": True,
  1949. }
  1950. )
  1951. external_issue = ExternalIssue.objects.get_or_create(
  1952. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  1953. )[0]
  1954. GroupLink.objects.get_or_create(
  1955. group_id=group.id,
  1956. project_id=group.project_id,
  1957. linked_type=GroupLink.LinkedType.issue,
  1958. linked_id=external_issue.id,
  1959. relationship=GroupLink.Relationship.references,
  1960. )[0]
  1961. response = self.get_success_response(sort_by="date", query="is:unresolved", method="get")
  1962. assert len(response.data) == 1
  1963. with self.tasks():
  1964. with self.feature({"organizations:integrations-issue-sync": True}):
  1965. response = self.get_success_response(
  1966. qs_params={"status": "unresolved"}, status="resolved"
  1967. )
  1968. group = Group.objects.get(id=group.id)
  1969. assert group.status == GroupStatus.RESOLVED
  1970. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1971. mock_sync_status_outbound.assert_called_once_with(
  1972. external_issue, True, group.project_id
  1973. )
  1974. response = self.get_success_response(sort_by="date", query="is:unresolved", method="get")
  1975. assert len(response.data) == 0
  1976. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  1977. def test_set_unresolved_with_integration(self, mock_sync_status_outbound):
  1978. release = self.create_release(project=self.project, version="abc")
  1979. group = self.create_group(status=GroupStatus.RESOLVED)
  1980. org = self.organization
  1981. integration = Integration.objects.create(provider="example", name="Example")
  1982. integration.add_organization(org, self.user)
  1983. OrganizationIntegration.objects.filter(
  1984. integration_id=integration.id, organization_id=group.organization.id
  1985. ).update(
  1986. config={
  1987. "sync_comments": True,
  1988. "sync_status_outbound": True,
  1989. "sync_status_inbound": True,
  1990. "sync_assignee_outbound": True,
  1991. "sync_assignee_inbound": True,
  1992. }
  1993. )
  1994. GroupResolution.objects.create(group=group, release=release)
  1995. external_issue = ExternalIssue.objects.get_or_create(
  1996. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  1997. )[0]
  1998. GroupLink.objects.get_or_create(
  1999. group_id=group.id,
  2000. project_id=group.project_id,
  2001. linked_type=GroupLink.LinkedType.issue,
  2002. linked_id=external_issue.id,
  2003. relationship=GroupLink.Relationship.references,
  2004. )[0]
  2005. self.login_as(user=self.user)
  2006. with self.tasks():
  2007. with self.feature({"organizations:integrations-issue-sync": True}):
  2008. response = self.get_success_response(
  2009. qs_params={"id": group.id}, status="unresolved"
  2010. )
  2011. assert response.status_code == 200
  2012. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2013. group = Group.objects.get(id=group.id)
  2014. assert group.status == GroupStatus.UNRESOLVED
  2015. self.assertNoResolution(group)
  2016. assert GroupSubscription.objects.filter(
  2017. user_id=self.user.id, group=group, is_active=True
  2018. ).exists()
  2019. mock_sync_status_outbound.assert_called_once_with(
  2020. external_issue, False, group.project_id
  2021. )
  2022. def test_self_assign_issue(self):
  2023. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2024. user = self.user
  2025. uo1 = UserOption.objects.create(
  2026. key="self_assign_issue", value="1", project_id=None, user=user
  2027. )
  2028. self.login_as(user=user)
  2029. response = self.get_success_response(qs_params={"id": group.id}, status="resolved")
  2030. assert response.data["assignedTo"]["id"] == str(user.id)
  2031. assert response.data["assignedTo"]["type"] == "user"
  2032. assert response.data["status"] == "resolved"
  2033. assert GroupAssignee.objects.filter(group=group, user_id=user.id).exists()
  2034. assert GroupSubscription.objects.filter(
  2035. user_id=user.id, group=group, is_active=True
  2036. ).exists()
  2037. uo1.delete()
  2038. def test_self_assign_issue_next_release(self):
  2039. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2040. release.add_project(self.project)
  2041. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2042. uo1 = UserOption.objects.create(
  2043. key="self_assign_issue", value="1", project_id=None, user=self.user
  2044. )
  2045. self.login_as(user=self.user)
  2046. response = self.get_success_response(
  2047. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2048. )
  2049. assert response.data["status"] == "resolved"
  2050. assert response.data["statusDetails"]["inNextRelease"]
  2051. assert response.data["assignedTo"]["id"] == str(self.user.id)
  2052. assert response.data["assignedTo"]["type"] == "user"
  2053. group = Group.objects.get(id=group.id)
  2054. assert group.status == GroupStatus.RESOLVED
  2055. assert GroupResolution.objects.filter(group=group, release=release).exists()
  2056. assert GroupSubscription.objects.filter(
  2057. user_id=self.user.id, group=group, is_active=True
  2058. ).exists()
  2059. activity = Activity.objects.get(
  2060. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2061. )
  2062. assert activity.data["version"] == ""
  2063. uo1.delete()
  2064. def test_in_semver_projects_group_resolution_stores_current_release_version(self):
  2065. """
  2066. Test that ensures that when we resolve a group in the next release, then
  2067. GroupResolution.current_release_version is set to the latest release associated with a
  2068. Group, when the project follows semantic versioning scheme
  2069. """
  2070. release_1 = self.create_release(version="fake_package@21.1.0")
  2071. release_2 = self.create_release(version="fake_package@21.1.1")
  2072. release_3 = self.create_release(version="fake_package@21.1.2")
  2073. self.store_event(
  2074. data={
  2075. "timestamp": iso_format(before_now(seconds=10)),
  2076. "fingerprint": ["group-1"],
  2077. "release": release_2.version,
  2078. },
  2079. project_id=self.project.id,
  2080. )
  2081. group = self.store_event(
  2082. data={
  2083. "timestamp": iso_format(before_now(seconds=12)),
  2084. "fingerprint": ["group-1"],
  2085. "release": release_1.version,
  2086. },
  2087. project_id=self.project.id,
  2088. ).group
  2089. self.login_as(user=self.user)
  2090. response = self.get_success_response(
  2091. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2092. )
  2093. assert response.data["status"] == "resolved"
  2094. assert response.data["statusDetails"]["inNextRelease"]
  2095. # The current_release_version should be to the latest (in semver) release associated with
  2096. # a group
  2097. grp_resolution = GroupResolution.objects.filter(group=group)
  2098. assert len(grp_resolution) == 1
  2099. grp_resolution = grp_resolution.first()
  2100. assert grp_resolution.current_release_version == release_2.version
  2101. # "resolvedInNextRelease" with semver releases is considered as "resolvedInRelease"
  2102. assert grp_resolution.type == GroupResolution.Type.in_release
  2103. assert grp_resolution.status == GroupResolution.Status.resolved
  2104. # Add release that is between 2 and 3 to ensure that any release after release 2 should
  2105. # not have a resolution
  2106. release_4 = self.create_release(version="fake_package@21.1.1+1")
  2107. for release in [release_1, release_2]:
  2108. assert GroupResolution.has_resolution(group=group, release=release)
  2109. for release in [release_3, release_4]:
  2110. assert not GroupResolution.has_resolution(group=group, release=release)
  2111. # Ensure that Activity has `current_release_version` set on `Resolved in next release`
  2112. activity = Activity.objects.filter(
  2113. group=grp_resolution.group,
  2114. type=ActivityType.SET_RESOLVED_IN_RELEASE.value,
  2115. ident=grp_resolution.id,
  2116. ).first()
  2117. assert "current_release_version" in activity.data
  2118. assert activity.data["current_release_version"] == release_2.version
  2119. def test_in_non_semver_projects_group_resolution_stores_current_release_version(self):
  2120. """
  2121. Test that ensures that when we resolve a group in the next release, then
  2122. GroupResolution.current_release_version is set to the most recent release associated with a
  2123. Group, when the project does not follow semantic versioning scheme
  2124. """
  2125. release_1 = self.create_release(
  2126. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2127. )
  2128. release_2 = self.create_release(version="foobar 2")
  2129. group = self.store_event(
  2130. data={
  2131. "timestamp": iso_format(before_now(seconds=12)),
  2132. "fingerprint": ["group-1"],
  2133. "release": release_1.version,
  2134. },
  2135. project_id=self.project.id,
  2136. ).group
  2137. self.login_as(user=self.user)
  2138. response = self.get_success_response(
  2139. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2140. )
  2141. assert response.data["status"] == "resolved"
  2142. assert response.data["statusDetails"]["inNextRelease"]
  2143. # Add a new release that is between 1 and 2, to make sure that if a the same issue/group
  2144. # occurs in that issue, then it should not have a resolution
  2145. release_3 = self.create_release(
  2146. date_added=timezone.now() - timedelta(minutes=30), version="foobar 3"
  2147. )
  2148. grp_resolution = GroupResolution.objects.filter(group=group)
  2149. assert len(grp_resolution) == 1
  2150. assert grp_resolution[0].current_release_version == release_1.version
  2151. assert GroupResolution.has_resolution(group=group, release=release_1)
  2152. for release in [release_2, release_3]:
  2153. assert not GroupResolution.has_resolution(group=group, release=release)
  2154. def test_in_non_semver_projects_store_actual_current_release_version_not_cached_version(self):
  2155. """
  2156. Test that ensures that the current_release_version is actually the latest version
  2157. associated with a group, not the cached version because currently
  2158. `group.get_last_release` fetches the latest release associated with a group and caches
  2159. that value, and we don't want to cache that value when resolving in next release in case a
  2160. new release appears to be associated with a group because if we store the cached rather
  2161. than the actual latest release, we might have unexpected results with the regression
  2162. algorithm
  2163. """
  2164. release_1 = self.create_release(
  2165. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2166. )
  2167. release_2 = self.create_release(version="foobar 2")
  2168. group = self.store_event(
  2169. data={
  2170. "timestamp": iso_format(before_now(seconds=12)),
  2171. "fingerprint": ["group-1"],
  2172. "release": release_1.version,
  2173. },
  2174. project_id=self.project.id,
  2175. ).group
  2176. # Call this function to cache the `last_seen` release to release_1
  2177. # i.e. Set the first last observed by Sentry
  2178. assert group.get_last_release() == release_1.version
  2179. self.login_as(user=self.user)
  2180. self.store_event(
  2181. data={
  2182. "timestamp": iso_format(before_now(seconds=0)),
  2183. "fingerprint": ["group-1"],
  2184. "release": release_2.version,
  2185. },
  2186. project_id=self.project.id,
  2187. )
  2188. # Cached (i.e. first last observed release by Sentry) is returned here since `use_cache`
  2189. # is set to its default of `True`
  2190. assert Group.objects.get(id=group.id).get_last_release() == release_1.version
  2191. response = self.get_success_response(
  2192. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2193. )
  2194. assert response.data["status"] == "resolved"
  2195. assert response.data["statusDetails"]["inNextRelease"]
  2196. # Changes here to release_2 and actual latest because `resolvedInNextRelease`,
  2197. # sets `use_cache` to False when fetching the last release associated with a group
  2198. assert Group.objects.get(id=group.id).get_last_release() == release_2.version
  2199. grp_resolution = GroupResolution.objects.filter(group=group)
  2200. assert len(grp_resolution) == 1
  2201. assert grp_resolution[0].current_release_version == release_2.version
  2202. def test_in_non_semver_projects_resolved_in_next_release_is_equated_to_in_release(self):
  2203. """
  2204. Test that ensures that if we basically know the next release when clicking on Resolved
  2205. In Next Release because that release exists, then we can short circuit setting
  2206. GroupResolution to type "inNextRelease", and then having `clear_expired_resolutions` run
  2207. once a new release is created to convert GroupResolution to in_release and set Activity.
  2208. Basically we treat "ResolvedInNextRelease" as "ResolvedInRelease" when there is a release
  2209. that was created after the last release associated with the group being resolved
  2210. """
  2211. release_1 = self.create_release(
  2212. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2213. )
  2214. release_2 = self.create_release(version="foobar 2")
  2215. self.create_release(version="foobar 3")
  2216. group = self.store_event(
  2217. data={
  2218. "timestamp": iso_format(before_now(seconds=12)),
  2219. "fingerprint": ["group-1"],
  2220. "release": release_1.version,
  2221. },
  2222. project_id=self.project.id,
  2223. ).group
  2224. self.login_as(user=self.user)
  2225. response = self.get_success_response(
  2226. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2227. )
  2228. assert response.data["status"] == "resolved"
  2229. assert response.data["statusDetails"]["inNextRelease"]
  2230. grp_resolution = GroupResolution.objects.filter(group=group)
  2231. assert len(grp_resolution) == 1
  2232. grp_resolution = grp_resolution[0]
  2233. assert grp_resolution.current_release_version == release_1.version
  2234. assert grp_resolution.release.id == release_2.id
  2235. assert grp_resolution.type == GroupResolution.Type.in_release
  2236. assert grp_resolution.status == GroupResolution.Status.resolved
  2237. activity = Activity.objects.filter(
  2238. group=grp_resolution.group,
  2239. type=ActivityType.SET_RESOLVED_IN_RELEASE.value,
  2240. ident=grp_resolution.id,
  2241. ).first()
  2242. assert activity.data["version"] == release_2.version
  2243. def test_selective_status_update(self):
  2244. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2245. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2246. group3 = self.create_group(status=GroupStatus.IGNORED)
  2247. group4 = self.create_group(
  2248. project=self.create_project(slug="foo"),
  2249. status=GroupStatus.UNRESOLVED,
  2250. )
  2251. self.login_as(user=self.user)
  2252. with self.feature("organizations:global-views"):
  2253. response = self.get_success_response(
  2254. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, status="resolved"
  2255. )
  2256. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  2257. new_group1 = Group.objects.get(id=group1.id)
  2258. assert new_group1.resolved_at is not None
  2259. assert new_group1.status == GroupStatus.RESOLVED
  2260. new_group2 = Group.objects.get(id=group2.id)
  2261. assert new_group2.resolved_at is not None
  2262. assert new_group2.status == GroupStatus.RESOLVED
  2263. assert GroupSubscription.objects.filter(
  2264. user_id=self.user.id, group=new_group2, is_active=True
  2265. ).exists()
  2266. new_group3 = Group.objects.get(id=group3.id)
  2267. assert new_group3.resolved_at is None
  2268. assert new_group3.status == GroupStatus.IGNORED
  2269. new_group4 = Group.objects.get(id=group4.id)
  2270. assert new_group4.resolved_at is None
  2271. assert new_group4.status == GroupStatus.UNRESOLVED
  2272. def test_set_resolved_in_current_release(self):
  2273. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2274. release.add_project(self.project)
  2275. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2276. self.login_as(user=self.user)
  2277. response = self.get_success_response(
  2278. qs_params={"id": group.id}, status="resolved", statusDetails={"inRelease": "latest"}
  2279. )
  2280. assert response.data["status"] == "resolved"
  2281. assert response.data["statusDetails"]["inRelease"] == release.version
  2282. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2283. group = Group.objects.get(id=group.id)
  2284. assert group.status == GroupStatus.RESOLVED
  2285. resolution = GroupResolution.objects.get(group=group)
  2286. assert resolution.release == release
  2287. assert resolution.type == GroupResolution.Type.in_release
  2288. assert resolution.status == GroupResolution.Status.resolved
  2289. assert resolution.actor_id == self.user.id
  2290. assert GroupSubscription.objects.filter(
  2291. user_id=self.user.id, group=group, is_active=True
  2292. ).exists()
  2293. activity = Activity.objects.get(
  2294. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2295. )
  2296. assert activity.data["version"] == release.version
  2297. assert GroupHistory.objects.filter(
  2298. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_RELEASE
  2299. ).exists()
  2300. def test_set_resolved_in_explicit_release(self):
  2301. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2302. release.add_project(self.project)
  2303. release2 = Release.objects.create(organization_id=self.project.organization_id, version="b")
  2304. release2.add_project(self.project)
  2305. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2306. self.login_as(user=self.user)
  2307. response = self.get_success_response(
  2308. qs_params={"id": group.id},
  2309. status="resolved",
  2310. statusDetails={"inRelease": release.version},
  2311. )
  2312. assert response.data["status"] == "resolved"
  2313. assert response.data["statusDetails"]["inRelease"] == release.version
  2314. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2315. assert "activity" in response.data
  2316. group = Group.objects.get(id=group.id)
  2317. assert group.status == GroupStatus.RESOLVED
  2318. resolution = GroupResolution.objects.get(group=group)
  2319. assert resolution.release == release
  2320. assert resolution.type == GroupResolution.Type.in_release
  2321. assert resolution.status == GroupResolution.Status.resolved
  2322. assert resolution.actor_id == self.user.id
  2323. assert GroupSubscription.objects.filter(
  2324. user_id=self.user.id, group=group, is_active=True
  2325. ).exists()
  2326. activity = Activity.objects.get(
  2327. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2328. )
  2329. assert activity.data["version"] == release.version
  2330. def test_in_semver_projects_set_resolved_in_explicit_release(self):
  2331. release_1 = self.create_release(version="fake_package@3.0.0")
  2332. release_2 = self.create_release(version="fake_package@2.0.0")
  2333. release_3 = self.create_release(version="fake_package@3.0.1")
  2334. group = self.store_event(
  2335. data={
  2336. "timestamp": iso_format(before_now(seconds=10)),
  2337. "fingerprint": ["group-1"],
  2338. "release": release_1.version,
  2339. },
  2340. project_id=self.project.id,
  2341. ).group
  2342. self.login_as(user=self.user)
  2343. response = self.get_success_response(
  2344. qs_params={"id": group.id},
  2345. status="resolved",
  2346. statusDetails={"inRelease": release_1.version},
  2347. )
  2348. assert response.data["status"] == "resolved"
  2349. assert response.data["statusDetails"]["inRelease"] == release_1.version
  2350. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2351. assert "activity" in response.data
  2352. group = Group.objects.get(id=group.id)
  2353. assert group.status == GroupStatus.RESOLVED
  2354. resolution = GroupResolution.objects.get(group=group)
  2355. assert resolution.release == release_1
  2356. assert resolution.type == GroupResolution.Type.in_release
  2357. assert resolution.status == GroupResolution.Status.resolved
  2358. assert resolution.actor_id == self.user.id
  2359. assert GroupSubscription.objects.filter(
  2360. user_id=self.user.id, group=group, is_active=True
  2361. ).exists()
  2362. activity = Activity.objects.get(
  2363. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2364. )
  2365. assert activity.data["version"] == release_1.version
  2366. assert GroupResolution.has_resolution(group=group, release=release_2)
  2367. assert not GroupResolution.has_resolution(group=group, release=release_3)
  2368. def test_set_resolved_in_next_release(self):
  2369. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2370. release.add_project(self.project)
  2371. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2372. self.login_as(user=self.user)
  2373. response = self.get_success_response(
  2374. qs_params={"id": group.id}, status="resolved", statusDetails={"inNextRelease": True}
  2375. )
  2376. assert response.data["status"] == "resolved"
  2377. assert response.data["statusDetails"]["inNextRelease"]
  2378. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2379. assert "activity" in response.data
  2380. group = Group.objects.get(id=group.id)
  2381. assert group.status == GroupStatus.RESOLVED
  2382. resolution = GroupResolution.objects.get(group=group)
  2383. assert resolution.release == release
  2384. assert resolution.type == GroupResolution.Type.in_next_release
  2385. assert resolution.status == GroupResolution.Status.pending
  2386. assert resolution.actor_id == self.user.id
  2387. assert GroupSubscription.objects.filter(
  2388. user_id=self.user.id, group=group, is_active=True
  2389. ).exists()
  2390. activity = Activity.objects.get(
  2391. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2392. )
  2393. assert activity.data["version"] == ""
  2394. def test_set_resolved_in_next_release_legacy(self):
  2395. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2396. release.add_project(self.project)
  2397. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2398. self.login_as(user=self.user)
  2399. response = self.get_success_response(
  2400. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2401. )
  2402. assert response.data["status"] == "resolved"
  2403. assert response.data["statusDetails"]["inNextRelease"]
  2404. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2405. assert "activity" in response.data
  2406. group = Group.objects.get(id=group.id)
  2407. assert group.status == GroupStatus.RESOLVED
  2408. resolution = GroupResolution.objects.get(group=group)
  2409. assert resolution.release == release
  2410. assert resolution.type == GroupResolution.Type.in_next_release
  2411. assert resolution.status == GroupResolution.Status.pending
  2412. assert resolution.actor_id == self.user.id
  2413. assert GroupSubscription.objects.filter(
  2414. user_id=self.user.id, group=group, is_active=True
  2415. ).exists()
  2416. assert GroupHistory.objects.filter(
  2417. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_RELEASE
  2418. ).exists()
  2419. activity = Activity.objects.get(
  2420. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2421. )
  2422. assert activity.data["version"] == ""
  2423. def test_set_resolved_in_explicit_commit_unreleased(self):
  2424. repo = self.create_repo(project=self.project, name=self.project.name)
  2425. commit = self.create_commit(project=self.project, repo=repo)
  2426. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2427. self.login_as(user=self.user)
  2428. response = self.get_success_response(
  2429. qs_params={"id": group.id},
  2430. status="resolved",
  2431. statusDetails={"inCommit": {"commit": commit.key, "repository": repo.name}},
  2432. )
  2433. assert response.data["status"] == "resolved"
  2434. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  2435. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2436. assert "activity" not in response.data
  2437. group = Group.objects.get(id=group.id)
  2438. assert group.status == GroupStatus.RESOLVED
  2439. link = GroupLink.objects.get(group_id=group.id)
  2440. assert link.linked_type == GroupLink.LinkedType.commit
  2441. assert link.relationship == GroupLink.Relationship.resolves
  2442. assert link.linked_id == commit.id
  2443. assert GroupSubscription.objects.filter(
  2444. user_id=self.user.id, group=group, is_active=True
  2445. ).exists()
  2446. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  2447. assert activity.data["commit"] == commit.id
  2448. assert GroupHistory.objects.filter(
  2449. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2450. ).exists()
  2451. def test_set_resolved_in_explicit_commit_released(self):
  2452. release = self.create_release(project=self.project)
  2453. repo = self.create_repo(project=self.project, name=self.project.name)
  2454. commit = self.create_commit(project=self.project, repo=repo, release=release)
  2455. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2456. self.login_as(user=self.user)
  2457. response = self.get_success_response(
  2458. qs_params={"id": group.id},
  2459. status="resolved",
  2460. statusDetails={"inCommit": {"commit": commit.key, "repository": repo.name}},
  2461. )
  2462. assert response.data["status"] == "resolved"
  2463. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  2464. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2465. assert "activity" in response.data
  2466. group = Group.objects.get(id=group.id)
  2467. assert group.status == GroupStatus.RESOLVED
  2468. link = GroupLink.objects.get(group_id=group.id)
  2469. assert link.project_id == self.project.id
  2470. assert link.linked_type == GroupLink.LinkedType.commit
  2471. assert link.relationship == GroupLink.Relationship.resolves
  2472. assert link.linked_id == commit.id
  2473. assert GroupSubscription.objects.filter(
  2474. user_id=self.user.id, group=group, is_active=True
  2475. ).exists()
  2476. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  2477. assert activity.data["commit"] == commit.id
  2478. resolution = GroupResolution.objects.get(group=group)
  2479. assert resolution.type == GroupResolution.Type.in_release
  2480. assert resolution.status == GroupResolution.Status.resolved
  2481. assert GroupHistory.objects.filter(
  2482. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2483. ).exists()
  2484. def test_set_resolved_in_explicit_commit_missing(self):
  2485. repo = self.create_repo(project=self.project, name=self.project.name)
  2486. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2487. self.login_as(user=self.user)
  2488. response = self.get_response(
  2489. qs_params={"id": group.id},
  2490. status="resolved",
  2491. statusDetails={"inCommit": {"commit": "a" * 40, "repository": repo.name}},
  2492. )
  2493. assert response.status_code == 400
  2494. assert (
  2495. response.data["statusDetails"]["inCommit"]["commit"][0]
  2496. == "Unable to find the given commit."
  2497. )
  2498. assert not GroupHistory.objects.filter(
  2499. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2500. ).exists()
  2501. def test_set_unresolved(self):
  2502. release = self.create_release(project=self.project, version="abc")
  2503. group = self.create_group(status=GroupStatus.RESOLVED)
  2504. GroupResolution.objects.create(group=group, release=release)
  2505. self.login_as(user=self.user)
  2506. response = self.get_success_response(qs_params={"id": group.id}, status="unresolved")
  2507. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2508. group = Group.objects.get(id=group.id)
  2509. assert group.status == GroupStatus.UNRESOLVED
  2510. assert GroupHistory.objects.filter(
  2511. group=group, status=GroupHistoryStatus.UNRESOLVED
  2512. ).exists()
  2513. self.assertNoResolution(group)
  2514. assert GroupSubscription.objects.filter(
  2515. user_id=self.user.id, group=group, is_active=True
  2516. ).exists()
  2517. def test_set_unresolved_on_snooze(self):
  2518. group = self.create_group(status=GroupStatus.IGNORED)
  2519. GroupSnooze.objects.create(group=group, until=timezone.now() - timedelta(days=1))
  2520. self.login_as(user=self.user)
  2521. response = self.get_success_response(qs_params={"id": group.id}, status="unresolved")
  2522. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2523. group = Group.objects.get(id=group.id)
  2524. assert group.status == GroupStatus.UNRESOLVED
  2525. assert GroupHistory.objects.filter(
  2526. group=group, status=GroupHistoryStatus.UNRESOLVED
  2527. ).exists()
  2528. def test_basic_ignore(self):
  2529. group = self.create_group(status=GroupStatus.RESOLVED)
  2530. snooze = GroupSnooze.objects.create(group=group, until=timezone.now())
  2531. self.login_as(user=self.user)
  2532. assert not GroupHistory.objects.filter(
  2533. group=group, status=GroupHistoryStatus.IGNORED
  2534. ).exists()
  2535. response = self.get_success_response(qs_params={"id": group.id}, status="ignored")
  2536. # existing snooze objects should be cleaned up
  2537. assert not GroupSnooze.objects.filter(id=snooze.id).exists()
  2538. group = Group.objects.get(id=group.id)
  2539. assert group.status == GroupStatus.IGNORED
  2540. assert GroupHistory.objects.filter(group=group, status=GroupHistoryStatus.IGNORED).exists()
  2541. assert response.data == {"status": "ignored", "statusDetails": {}, "inbox": None}
  2542. def test_snooze_duration(self):
  2543. group = self.create_group(status=GroupStatus.RESOLVED)
  2544. self.login_as(user=self.user)
  2545. response = self.get_success_response(
  2546. qs_params={"id": group.id}, status="ignored", ignoreDuration=30
  2547. )
  2548. snooze = GroupSnooze.objects.get(group=group)
  2549. snooze.until = snooze.until
  2550. now = timezone.now()
  2551. assert snooze.count is None
  2552. assert snooze.until > now + timedelta(minutes=29)
  2553. assert snooze.until < now + timedelta(minutes=31)
  2554. assert snooze.user_count is None
  2555. assert snooze.user_window is None
  2556. assert snooze.window is None
  2557. response.data["statusDetails"]["ignoreUntil"] = response.data["statusDetails"][
  2558. "ignoreUntil"
  2559. ]
  2560. assert response.data["status"] == "ignored"
  2561. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2562. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2563. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2564. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2565. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2566. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2567. def test_snooze_count(self):
  2568. group = self.create_group(status=GroupStatus.RESOLVED, times_seen=1)
  2569. self.login_as(user=self.user)
  2570. response = self.get_success_response(
  2571. qs_params={"id": group.id}, status="ignored", ignoreCount=100
  2572. )
  2573. snooze = GroupSnooze.objects.get(group=group)
  2574. assert snooze.count == 100
  2575. assert snooze.until is None
  2576. assert snooze.user_count is None
  2577. assert snooze.user_window is None
  2578. assert snooze.window is None
  2579. assert snooze.state["times_seen"] == 1
  2580. assert response.data["status"] == "ignored"
  2581. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2582. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2583. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2584. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2585. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2586. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2587. def test_snooze_user_count(self):
  2588. event = {}
  2589. for i in range(10):
  2590. event = self.store_event(
  2591. data={
  2592. "fingerprint": ["put-me-in-group-1"],
  2593. "user": {"id": str(i)},
  2594. "timestamp": iso_format(self.min_ago + timedelta(seconds=i)),
  2595. },
  2596. project_id=self.project.id,
  2597. )
  2598. group = Group.objects.get(id=event.group.id)
  2599. group.status = GroupStatus.RESOLVED
  2600. group.substatus = None
  2601. group.save()
  2602. self.login_as(user=self.user)
  2603. response = self.get_success_response(
  2604. qs_params={"id": group.id}, status="ignored", ignoreUserCount=10
  2605. )
  2606. snooze = GroupSnooze.objects.get(group=group)
  2607. assert snooze.count is None
  2608. assert snooze.until is None
  2609. assert snooze.user_count == 10
  2610. assert snooze.user_window is None
  2611. assert snooze.window is None
  2612. assert snooze.state["users_seen"] == 10
  2613. assert response.data["status"] == "ignored"
  2614. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2615. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2616. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2617. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2618. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2619. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2620. def test_set_bookmarked(self):
  2621. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2622. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2623. group3 = self.create_group(status=GroupStatus.IGNORED)
  2624. group4 = self.create_group(
  2625. project=self.create_project(slug="foo"),
  2626. status=GroupStatus.UNRESOLVED,
  2627. )
  2628. self.login_as(user=self.user)
  2629. with self.feature("organizations:global-views"):
  2630. response = self.get_success_response(
  2631. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, isBookmarked="true"
  2632. )
  2633. assert response.data == {"isBookmarked": True}
  2634. bookmark1 = GroupBookmark.objects.filter(group=group1, user_id=self.user.id)
  2635. assert bookmark1.exists()
  2636. assert GroupSubscription.objects.filter(
  2637. user_id=self.user.id, group=group1, is_active=True
  2638. ).exists()
  2639. bookmark2 = GroupBookmark.objects.filter(group=group2, user_id=self.user.id)
  2640. assert bookmark2.exists()
  2641. assert GroupSubscription.objects.filter(
  2642. user_id=self.user.id, group=group2, is_active=True
  2643. ).exists()
  2644. bookmark3 = GroupBookmark.objects.filter(group=group3, user_id=self.user.id)
  2645. assert not bookmark3.exists()
  2646. bookmark4 = GroupBookmark.objects.filter(group=group4, user_id=self.user.id)
  2647. assert not bookmark4.exists()
  2648. def test_subscription(self):
  2649. group1 = self.create_group()
  2650. group2 = self.create_group()
  2651. group3 = self.create_group()
  2652. group4 = self.create_group(project=self.create_project(slug="foo"))
  2653. self.login_as(user=self.user)
  2654. with self.feature("organizations:global-views"):
  2655. response = self.get_success_response(
  2656. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, isSubscribed="true"
  2657. )
  2658. assert response.data == {"isSubscribed": True, "subscriptionDetails": {"reason": "unknown"}}
  2659. assert GroupSubscription.objects.filter(
  2660. group=group1, user_id=self.user.id, is_active=True
  2661. ).exists()
  2662. assert GroupSubscription.objects.filter(
  2663. group=group2, user_id=self.user.id, is_active=True
  2664. ).exists()
  2665. assert not GroupSubscription.objects.filter(group=group3, user_id=self.user.id).exists()
  2666. assert not GroupSubscription.objects.filter(group=group4, user_id=self.user.id).exists()
  2667. def test_set_public(self):
  2668. group1 = self.create_group()
  2669. group2 = self.create_group()
  2670. self.login_as(user=self.user)
  2671. response = self.get_success_response(
  2672. qs_params={"id": [group1.id, group2.id]}, isPublic="true"
  2673. )
  2674. assert response.data["isPublic"] is True
  2675. assert "shareId" in response.data
  2676. new_group1 = Group.objects.get(id=group1.id)
  2677. assert bool(new_group1.get_share_id())
  2678. new_group2 = Group.objects.get(id=group2.id)
  2679. assert bool(new_group2.get_share_id())
  2680. def test_set_private(self):
  2681. group1 = self.create_group()
  2682. group2 = self.create_group()
  2683. # Manually mark them as shared
  2684. for g in group1, group2:
  2685. GroupShare.objects.create(project_id=g.project_id, group=g)
  2686. assert bool(g.get_share_id())
  2687. self.login_as(user=self.user)
  2688. response = self.get_success_response(
  2689. qs_params={"id": [group1.id, group2.id]}, isPublic="false"
  2690. )
  2691. assert response.data == {"isPublic": False, "shareId": None}
  2692. new_group1 = Group.objects.get(id=group1.id)
  2693. assert not bool(new_group1.get_share_id())
  2694. new_group2 = Group.objects.get(id=group2.id)
  2695. assert not bool(new_group2.get_share_id())
  2696. def test_set_has_seen(self):
  2697. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2698. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2699. group3 = self.create_group(status=GroupStatus.IGNORED)
  2700. group4 = self.create_group(
  2701. project=self.create_project(slug="foo"),
  2702. status=GroupStatus.UNRESOLVED,
  2703. )
  2704. self.login_as(user=self.user)
  2705. with self.feature("organizations:global-views"):
  2706. response = self.get_success_response(
  2707. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, hasSeen="true"
  2708. )
  2709. assert response.data == {"hasSeen": True}
  2710. r1 = GroupSeen.objects.filter(group=group1, user_id=self.user.id)
  2711. assert r1.exists()
  2712. r2 = GroupSeen.objects.filter(group=group2, user_id=self.user.id)
  2713. assert r2.exists()
  2714. r3 = GroupSeen.objects.filter(group=group3, user_id=self.user.id)
  2715. assert not r3.exists()
  2716. r4 = GroupSeen.objects.filter(group=group4, user_id=self.user.id)
  2717. assert not r4.exists()
  2718. @patch("sentry.issues.merge.uuid4")
  2719. @patch("sentry.issues.merge.merge_groups")
  2720. @patch("sentry.issues.merge.eventstream")
  2721. def test_merge(self, mock_eventstream, merge_groups, mock_uuid4):
  2722. eventstream_state = object()
  2723. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  2724. mock_uuid4.return_value = self.get_mock_uuid()
  2725. group1 = self.create_group(times_seen=1)
  2726. group2 = self.create_group(times_seen=50)
  2727. group3 = self.create_group(times_seen=2)
  2728. self.create_group()
  2729. self.login_as(user=self.user)
  2730. response = self.get_success_response(
  2731. qs_params={"id": [group1.id, group2.id, group3.id]}, merge="1"
  2732. )
  2733. assert response.data["merge"]["parent"] == str(group2.id)
  2734. assert sorted(response.data["merge"]["children"]) == sorted(
  2735. [str(group1.id), str(group3.id)]
  2736. )
  2737. mock_eventstream.start_merge.assert_called_once_with(
  2738. group1.project_id, [group3.id, group1.id], group2.id
  2739. )
  2740. assert len(merge_groups.mock_calls) == 1
  2741. merge_groups.delay.assert_any_call(
  2742. from_object_ids=[group3.id, group1.id],
  2743. to_object_id=group2.id,
  2744. transaction_id="abc123",
  2745. eventstream_state=eventstream_state,
  2746. )
  2747. @patch("sentry.issues.merge.uuid4")
  2748. @patch("sentry.issues.merge.merge_groups")
  2749. @patch("sentry.issues.merge.eventstream")
  2750. def test_merge_performance_issues(self, mock_eventstream, merge_groups, mock_uuid4):
  2751. eventstream_state = object()
  2752. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  2753. mock_uuid4.return_value = self.get_mock_uuid()
  2754. group1 = self.create_group(times_seen=1, type=PerformanceSlowDBQueryGroupType.type_id)
  2755. group2 = self.create_group(times_seen=50, type=PerformanceSlowDBQueryGroupType.type_id)
  2756. group3 = self.create_group(times_seen=2, type=PerformanceSlowDBQueryGroupType.type_id)
  2757. self.create_group()
  2758. self.login_as(user=self.user)
  2759. response = self.get_error_response(
  2760. qs_params={"id": [group1.id, group2.id, group3.id]}, merge="1"
  2761. )
  2762. assert response.status_code == 400, response.content
  2763. def test_assign(self):
  2764. group1 = self.create_group(is_public=True)
  2765. group2 = self.create_group(is_public=True)
  2766. user = self.user
  2767. self.login_as(user=user)
  2768. response = self.get_success_response(qs_params={"id": group1.id}, assignedTo=user.username)
  2769. assert response.data["assignedTo"]["id"] == str(user.id)
  2770. assert response.data["assignedTo"]["type"] == "user"
  2771. assert GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  2772. assert GroupHistory.objects.filter(
  2773. group=group1, status=GroupHistoryStatus.ASSIGNED
  2774. ).exists()
  2775. assert not GroupAssignee.objects.filter(group=group2, user_id=user.id).exists()
  2776. assert (
  2777. Activity.objects.filter(
  2778. group=group1, user_id=user.id, type=ActivityType.ASSIGNED.value
  2779. ).count()
  2780. == 1
  2781. )
  2782. assert GroupSubscription.objects.filter(
  2783. user_id=user.id, group=group1, is_active=True
  2784. ).exists()
  2785. response = self.get_success_response(qs_params={"id": group1.id}, assignedTo="")
  2786. assert response.data["assignedTo"] is None
  2787. assert not GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  2788. assert GroupHistory.objects.filter(
  2789. group=group1, status=GroupHistoryStatus.UNASSIGNED
  2790. ).exists()
  2791. def test_assign_non_member(self):
  2792. group = self.create_group(is_public=True)
  2793. member = self.user
  2794. non_member = self.create_user("bar@example.com")
  2795. self.login_as(user=member)
  2796. response = self.get_response(qs_params={"id": group.id}, assignedTo=non_member.username)
  2797. assert not GroupHistory.objects.filter(
  2798. group=group, status=GroupHistoryStatus.ASSIGNED
  2799. ).exists()
  2800. assert response.status_code == 400, response.content
  2801. def test_assign_team(self):
  2802. self.login_as(user=self.user)
  2803. group = self.create_group()
  2804. other_member = self.create_user("bar@example.com")
  2805. team = self.create_team(
  2806. organization=group.project.organization, members=[self.user, other_member]
  2807. )
  2808. group.project.add_team(team)
  2809. assert not GroupHistory.objects.filter(
  2810. group=group, status=GroupHistoryStatus.ASSIGNED
  2811. ).exists()
  2812. response = self.get_success_response(
  2813. qs_params={"id": group.id}, assignedTo=f"team:{team.id}"
  2814. )
  2815. assert response.data["assignedTo"]["id"] == str(team.id)
  2816. assert response.data["assignedTo"]["type"] == "team"
  2817. assert GroupHistory.objects.filter(group=group, status=GroupHistoryStatus.ASSIGNED).exists()
  2818. assert GroupAssignee.objects.filter(group=group, team=team).exists()
  2819. assert Activity.objects.filter(group=group, type=ActivityType.ASSIGNED.value).count() == 1
  2820. assert GroupSubscription.objects.filter(group=group, is_active=True).count() == 2
  2821. response = self.get_success_response(qs_params={"id": group.id}, assignedTo="")
  2822. assert response.data["assignedTo"] is None
  2823. assert GroupHistory.objects.filter(
  2824. group=group, status=GroupHistoryStatus.UNASSIGNED
  2825. ).exists()
  2826. def test_discard(self):
  2827. group1 = self.create_group(is_public=True)
  2828. group2 = self.create_group(is_public=True)
  2829. group_hash = GroupHash.objects.create(hash="x" * 32, project=group1.project, group=group1)
  2830. user = self.user
  2831. self.login_as(user=user)
  2832. with self.tasks():
  2833. with self.feature("projects:discard-groups"):
  2834. response = self.get_response(qs_params={"id": group1.id}, discard=True)
  2835. assert response.status_code == 204
  2836. assert not Group.objects.filter(id=group1.id).exists()
  2837. assert Group.objects.filter(id=group2.id).exists()
  2838. assert GroupHash.objects.filter(id=group_hash.id).exists()
  2839. tombstone = GroupTombstone.objects.get(
  2840. id=GroupHash.objects.get(id=group_hash.id).group_tombstone_id
  2841. )
  2842. assert tombstone.message == group1.message
  2843. assert tombstone.culprit == group1.culprit
  2844. assert tombstone.project == group1.project
  2845. assert tombstone.data == group1.data
  2846. @override_settings(SENTRY_SELF_HOSTED=False)
  2847. def test_ratelimit(self):
  2848. self.login_as(user=self.user)
  2849. with freeze_time("2000-01-01"):
  2850. for i in range(5):
  2851. self.get_success_response()
  2852. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  2853. def test_set_inbox(self):
  2854. group1 = self.create_group()
  2855. group2 = self.create_group()
  2856. self.login_as(user=self.user)
  2857. response = self.get_success_response(qs_params={"id": [group1.id, group2.id]}, inbox="true")
  2858. assert response.data == {"inbox": True}
  2859. assert GroupInbox.objects.filter(group=group1).exists()
  2860. assert GroupInbox.objects.filter(group=group2).exists()
  2861. assert not GroupHistory.objects.filter(
  2862. group=group1, status=GroupHistoryStatus.REVIEWED
  2863. ).exists()
  2864. assert not GroupHistory.objects.filter(
  2865. group=group2, status=GroupHistoryStatus.REVIEWED
  2866. ).exists()
  2867. response = self.get_success_response(qs_params={"id": [group2.id]}, inbox="false")
  2868. assert response.data == {"inbox": False}
  2869. assert GroupInbox.objects.filter(group=group1).exists()
  2870. assert not GroupHistory.objects.filter(
  2871. group=group1, status=GroupHistoryStatus.REVIEWED
  2872. ).exists()
  2873. assert GroupHistory.objects.filter(
  2874. group=group2, status=GroupHistoryStatus.REVIEWED
  2875. ).exists()
  2876. assert not GroupInbox.objects.filter(group=group2).exists()
  2877. def test_set_resolved_inbox(self):
  2878. group1 = self.create_group()
  2879. group2 = self.create_group()
  2880. self.login_as(user=self.user)
  2881. response = self.get_success_response(
  2882. qs_params={"id": [group1.id, group2.id]}, status="resolved"
  2883. )
  2884. assert response.data["inbox"] is None
  2885. assert not GroupInbox.objects.filter(group=group1).exists()
  2886. assert not GroupInbox.objects.filter(group=group2).exists()
  2887. self.get_success_response(qs_params={"id": [group2.id]}, status="unresolved")
  2888. assert not GroupInbox.objects.filter(group=group1).exists()
  2889. assert not GroupInbox.objects.filter(group=group2).exists()
  2890. assert not GroupHistory.objects.filter(
  2891. group=group1, status=GroupHistoryStatus.UNRESOLVED
  2892. ).exists()
  2893. assert GroupHistory.objects.filter(
  2894. group=group2, status=GroupHistoryStatus.UNRESOLVED
  2895. ).exists()
  2896. @region_silo_test
  2897. class GroupDeleteTest(APITestCase, SnubaTestCase):
  2898. endpoint = "sentry-api-0-organization-group-index"
  2899. method = "delete"
  2900. def get_response(self, *args, **kwargs):
  2901. if not args:
  2902. org = self.project.organization.slug
  2903. else:
  2904. org = args[0]
  2905. return super().get_response(org, **kwargs)
  2906. @patch("sentry.api.helpers.group_index.delete.eventstream")
  2907. @patch("sentry.eventstream")
  2908. def test_delete_by_id(self, mock_eventstream_task, mock_eventstream_api):
  2909. eventstream_state = {"event_stream_state": uuid4()}
  2910. mock_eventstream_api.start_delete_groups = Mock(return_value=eventstream_state)
  2911. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2912. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2913. group3 = self.create_group(status=GroupStatus.IGNORED)
  2914. group4 = self.create_group(
  2915. project=self.create_project(slug="foo"),
  2916. status=GroupStatus.UNRESOLVED,
  2917. )
  2918. hashes = []
  2919. for g in group1, group2, group3, group4:
  2920. hash = uuid4().hex
  2921. hashes.append(hash)
  2922. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  2923. self.login_as(user=self.user)
  2924. with self.feature("organizations:global-views"):
  2925. response = self.get_response(
  2926. qs_params={"id": [group1.id, group2.id], "group4": group4.id}
  2927. )
  2928. mock_eventstream_api.start_delete_groups.assert_called_once_with(
  2929. group1.project_id, [group1.id, group2.id]
  2930. )
  2931. assert response.status_code == 204
  2932. assert Group.objects.get(id=group1.id).status == GroupStatus.PENDING_DELETION
  2933. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  2934. assert Group.objects.get(id=group2.id).status == GroupStatus.PENDING_DELETION
  2935. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  2936. assert Group.objects.get(id=group3.id).status != GroupStatus.PENDING_DELETION
  2937. assert GroupHash.objects.filter(group_id=group3.id).exists()
  2938. assert Group.objects.get(id=group4.id).status != GroupStatus.PENDING_DELETION
  2939. assert GroupHash.objects.filter(group_id=group4.id).exists()
  2940. Group.objects.filter(id__in=(group1.id, group2.id)).update(status=GroupStatus.UNRESOLVED)
  2941. with self.tasks():
  2942. with self.feature("organizations:global-views"):
  2943. response = self.get_response(
  2944. qs_params={"id": [group1.id, group2.id], "group4": group4.id}
  2945. )
  2946. mock_eventstream_task.end_delete_groups.assert_called_once_with(eventstream_state)
  2947. assert response.status_code == 204
  2948. assert not Group.objects.filter(id=group1.id).exists()
  2949. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  2950. assert not Group.objects.filter(id=group2.id).exists()
  2951. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  2952. assert Group.objects.filter(id=group3.id).exists()
  2953. assert GroupHash.objects.filter(group_id=group3.id).exists()
  2954. assert Group.objects.filter(id=group4.id).exists()
  2955. assert GroupHash.objects.filter(group_id=group4.id).exists()
  2956. @patch("sentry.api.helpers.group_index.delete.eventstream")
  2957. @patch("sentry.eventstream")
  2958. def test_delete_performance_issue_by_id(self, mock_eventstream_task, mock_eventstream_api):
  2959. eventstream_state = {"event_stream_state": uuid4()}
  2960. mock_eventstream_api.start_delete_groups = Mock(return_value=eventstream_state)
  2961. group1 = self.create_group(
  2962. status=GroupStatus.RESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  2963. )
  2964. group2 = self.create_group(
  2965. status=GroupStatus.UNRESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  2966. )
  2967. hashes = []
  2968. for g in group1, group2:
  2969. hash = uuid4().hex
  2970. hashes.append(hash)
  2971. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  2972. self.login_as(user=self.user)
  2973. with self.feature("organizations:global-views"):
  2974. response = self.get_response(qs_params={"id": [group1.id, group2.id]})
  2975. assert response.status_code == 400
  2976. assert Group.objects.filter(id=group1.id).exists()
  2977. assert GroupHash.objects.filter(group_id=group1.id).exists()
  2978. assert Group.objects.filter(id=group2.id).exists()
  2979. assert GroupHash.objects.filter(group_id=group2.id).exists()
  2980. def test_bulk_delete(self):
  2981. groups = []
  2982. for i in range(10, 41):
  2983. groups.append(
  2984. self.create_group(
  2985. project=self.project,
  2986. status=GroupStatus.RESOLVED,
  2987. )
  2988. )
  2989. hashes = []
  2990. for group in groups:
  2991. hash = uuid4().hex
  2992. hashes.append(hash)
  2993. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  2994. self.login_as(user=self.user)
  2995. # if query is '' it defaults to is:unresolved
  2996. response = self.get_response(qs_params={"query": ""})
  2997. assert response.status_code == 204
  2998. for group in groups:
  2999. assert Group.objects.get(id=group.id).status == GroupStatus.PENDING_DELETION
  3000. assert not GroupHash.objects.filter(group_id=group.id).exists()
  3001. Group.objects.filter(id__in=[group.id for group in groups]).update(
  3002. status=GroupStatus.UNRESOLVED
  3003. )
  3004. with self.tasks():
  3005. response = self.get_response(qs_params={"query": ""})
  3006. assert response.status_code == 204
  3007. for group in groups:
  3008. assert not Group.objects.filter(id=group.id).exists()
  3009. assert not GroupHash.objects.filter(group_id=group.id).exists()
  3010. @override_settings(SENTRY_SELF_HOSTED=False)
  3011. def test_ratelimit(self):
  3012. self.login_as(user=self.user)
  3013. with freeze_time("2000-01-01"):
  3014. for i in range(5):
  3015. self.get_success_response()
  3016. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  3017. def test_bulk_delete_performance_issues(self):
  3018. groups = []
  3019. for i in range(10, 41):
  3020. groups.append(
  3021. self.create_group(
  3022. project=self.project,
  3023. status=GroupStatus.RESOLVED,
  3024. type=PerformanceSlowDBQueryGroupType.type_id,
  3025. )
  3026. )
  3027. hashes = []
  3028. for group in groups:
  3029. hash = uuid4().hex
  3030. hashes.append(hash)
  3031. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  3032. self.login_as(user=self.user)
  3033. # if query is '' it defaults to is:unresolved
  3034. response = self.get_response(qs_params={"query": ""})
  3035. assert response.status_code == 400
  3036. for group in groups:
  3037. assert Group.objects.filter(id=group.id).exists()
  3038. assert GroupHash.objects.filter(group_id=group.id).exists()