test_organization_group_index.py 146 KB

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