test_organization_group_index.py 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646
  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(stable=True)
  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}",
  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}",
  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}, {ReleaseStages.REPLACED}]",
  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}, {ReleaseStages.REPLACED}]",
  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_owners(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. # Test with no owner
  1538. response = self.get_response(sort_by="date", limit=10, query=query, expand="owners")
  1539. assert response.status_code == 200
  1540. assert len(response.data) == 1
  1541. assert int(response.data[0]["id"]) == event.group.id
  1542. assert response.data[0]["owners"] is None
  1543. # Test with owners
  1544. GroupOwner.objects.create(
  1545. group=event.group,
  1546. project=event.project,
  1547. organization=event.project.organization,
  1548. type=GroupOwnerType.SUSPECT_COMMIT.value,
  1549. user_id=self.user.id,
  1550. )
  1551. GroupOwner.objects.create(
  1552. group=event.group,
  1553. project=event.project,
  1554. organization=event.project.organization,
  1555. type=GroupOwnerType.OWNERSHIP_RULE.value,
  1556. team=self.team,
  1557. )
  1558. GroupOwner.objects.create(
  1559. group=event.group,
  1560. project=event.project,
  1561. organization=event.project.organization,
  1562. type=GroupOwnerType.CODEOWNERS.value,
  1563. team=self.team,
  1564. )
  1565. GroupOwner.objects.create(
  1566. group=event.group,
  1567. project=event.project,
  1568. organization=event.project.organization,
  1569. type=GroupOwnerType.SUSPECT_COMMIT.value,
  1570. user_id=None,
  1571. team=None,
  1572. )
  1573. response = self.get_response(sort_by="date", limit=10, query=query, expand="owners")
  1574. assert response.status_code == 200
  1575. assert len(response.data) == 1
  1576. assert int(response.data[0]["id"]) == event.group.id
  1577. assert response.data[0]["owners"] is not None
  1578. assert len(response.data[0]["owners"]) == 3
  1579. assert response.data[0]["owners"][0]["owner"] == f"user:{self.user.id}"
  1580. assert response.data[0]["owners"][1]["owner"] == f"team:{self.team.id}"
  1581. assert response.data[0]["owners"][2]["owner"] == f"team:{self.team.id}"
  1582. assert (
  1583. response.data[0]["owners"][0]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.SUSPECT_COMMIT]
  1584. )
  1585. assert (
  1586. response.data[0]["owners"][1]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.OWNERSHIP_RULE]
  1587. )
  1588. assert response.data[0]["owners"][2]["type"] == GROUP_OWNER_TYPE[GroupOwnerType.CODEOWNERS]
  1589. @override_settings(SENTRY_SELF_HOSTED=False)
  1590. def test_ratelimit(self):
  1591. self.login_as(user=self.user)
  1592. with freeze_time("2000-01-01"):
  1593. for i in range(10):
  1594. self.get_success_response()
  1595. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  1596. def test_filter_not_unresolved(self):
  1597. event = self.store_event(
  1598. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1599. project_id=self.project.id,
  1600. )
  1601. event.group.update(status=GroupStatus.RESOLVED, substatus=None)
  1602. self.login_as(user=self.user)
  1603. response = self.get_response(
  1604. sort_by="date", limit=10, query="!is:unresolved", expand="inbox", collapse="stats"
  1605. )
  1606. assert response.status_code == 200
  1607. assert [int(r["id"]) for r in response.data] == [event.group.id]
  1608. def test_collapse_stats(self):
  1609. event = self.store_event(
  1610. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1611. project_id=self.project.id,
  1612. )
  1613. self.login_as(user=self.user)
  1614. response = self.get_response(
  1615. sort_by="date", limit=10, query="is:unresolved", expand="inbox", collapse="stats"
  1616. )
  1617. assert response.status_code == 200
  1618. assert len(response.data) == 1
  1619. assert int(response.data[0]["id"]) == event.group.id
  1620. assert "stats" not in response.data[0]
  1621. assert "firstSeen" not in response.data[0]
  1622. assert "lastSeen" not in response.data[0]
  1623. assert "count" not in response.data[0]
  1624. assert "userCount" not in response.data[0]
  1625. assert "lifetime" not in response.data[0]
  1626. assert "filtered" not in response.data[0]
  1627. def test_collapse_lifetime(self):
  1628. event = self.store_event(
  1629. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1630. project_id=self.project.id,
  1631. )
  1632. self.login_as(user=self.user)
  1633. response = self.get_response(
  1634. sort_by="date", limit=10, query="is:unresolved", collapse="lifetime"
  1635. )
  1636. assert response.status_code == 200
  1637. assert len(response.data) == 1
  1638. assert int(response.data[0]["id"]) == event.group.id
  1639. assert "stats" in response.data[0]
  1640. assert "firstSeen" in response.data[0]
  1641. assert "lastSeen" in response.data[0]
  1642. assert "count" in response.data[0]
  1643. assert "lifetime" not in response.data[0]
  1644. assert "filtered" in response.data[0]
  1645. def test_collapse_filtered(self):
  1646. event = self.store_event(
  1647. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1648. project_id=self.project.id,
  1649. )
  1650. self.login_as(user=self.user)
  1651. response = self.get_response(
  1652. sort_by="date", limit=10, query="is:unresolved", collapse="filtered"
  1653. )
  1654. assert response.status_code == 200
  1655. assert len(response.data) == 1
  1656. assert int(response.data[0]["id"]) == event.group.id
  1657. assert "stats" in response.data[0]
  1658. assert "firstSeen" in response.data[0]
  1659. assert "lastSeen" in response.data[0]
  1660. assert "count" in response.data[0]
  1661. assert "lifetime" in response.data[0]
  1662. assert "filtered" not in response.data[0]
  1663. def test_collapse_lifetime_and_filtered(self):
  1664. event = self.store_event(
  1665. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1666. project_id=self.project.id,
  1667. )
  1668. self.login_as(user=self.user)
  1669. response = self.get_response(
  1670. sort_by="date", limit=10, query="is:unresolved", collapse=["filtered", "lifetime"]
  1671. )
  1672. assert response.status_code == 200
  1673. assert len(response.data) == 1
  1674. assert int(response.data[0]["id"]) == event.group.id
  1675. assert "stats" in response.data[0]
  1676. assert "firstSeen" in response.data[0]
  1677. assert "lastSeen" in response.data[0]
  1678. assert "count" in response.data[0]
  1679. assert "lifetime" not in response.data[0]
  1680. assert "filtered" not in response.data[0]
  1681. def test_collapse_base(self):
  1682. event = self.store_event(
  1683. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1684. project_id=self.project.id,
  1685. )
  1686. self.login_as(user=self.user)
  1687. response = self.get_response(
  1688. sort_by="date", limit=10, query="is:unresolved", collapse=["base"]
  1689. )
  1690. assert response.status_code == 200
  1691. assert len(response.data) == 1
  1692. assert int(response.data[0]["id"]) == event.group.id
  1693. assert "title" not in response.data[0]
  1694. assert "hasSeen" not in response.data[0]
  1695. assert "stats" in response.data[0]
  1696. assert "firstSeen" in response.data[0]
  1697. assert "lastSeen" in response.data[0]
  1698. assert "count" in response.data[0]
  1699. assert "lifetime" in response.data[0]
  1700. assert "filtered" in response.data[0]
  1701. def test_collapse_stats_group_snooze_bug(self):
  1702. # There was a bug where we tried to access attributes on seen_stats if this feature is active
  1703. # but seen_stats could be null when we collapse stats.
  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. GroupSnooze.objects.create(
  1709. group=event.group,
  1710. user_count=10,
  1711. until=timezone.now() + timedelta(days=1),
  1712. count=10,
  1713. state={"times_seen": 0},
  1714. )
  1715. self.login_as(user=self.user)
  1716. # The presence of the group above with attached GroupSnooze would have previously caused this error.
  1717. response = self.get_response(
  1718. sort_by="date", limit=10, query="is:unresolved", expand="inbox", collapse="stats"
  1719. )
  1720. assert response.status_code == 200
  1721. assert len(response.data) == 1
  1722. assert int(response.data[0]["id"]) == event.group.id
  1723. @with_feature("organizations:issue-stream-performance")
  1724. def test_collapse_unhandled(self):
  1725. event = self.store_event(
  1726. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1727. project_id=self.project.id,
  1728. )
  1729. self.login_as(user=self.user)
  1730. response = self.get_response(
  1731. sort_by="date", limit=10, query="is:unresolved", collapse=["unhandled"]
  1732. )
  1733. assert response.status_code == 200
  1734. assert len(response.data) == 1
  1735. assert int(response.data[0]["id"]) == event.group.id
  1736. assert "isUnhandled" not in response.data[0]
  1737. def test_selected_saved_search(self):
  1738. saved_search = SavedSearch.objects.create(
  1739. name="Saved Search",
  1740. query="ZeroDivisionError",
  1741. organization=self.organization,
  1742. owner_id=self.user.id,
  1743. )
  1744. event = self.store_event(
  1745. data={
  1746. "timestamp": iso_format(before_now(seconds=500)),
  1747. "fingerprint": ["group-1"],
  1748. "message": "ZeroDivisionError",
  1749. },
  1750. project_id=self.project.id,
  1751. )
  1752. self.store_event(
  1753. data={
  1754. "timestamp": iso_format(before_now(seconds=500)),
  1755. "fingerprint": ["group-2"],
  1756. "message": "TypeError",
  1757. },
  1758. project_id=self.project.id,
  1759. )
  1760. self.login_as(user=self.user)
  1761. response = self.get_response(
  1762. sort_by="date",
  1763. limit=10,
  1764. query="is:unresolved",
  1765. collapse=["unhandled"],
  1766. savedSearch=0,
  1767. searchId=saved_search.id,
  1768. )
  1769. assert response.status_code == 200
  1770. assert len(response.data) == 1
  1771. assert int(response.data[0]["id"]) == event.group.id
  1772. def test_default_saved_search(self):
  1773. SavedSearch.objects.create(
  1774. name="Saved Search",
  1775. query="ZeroDivisionError",
  1776. organization=self.organization,
  1777. owner_id=self.user.id,
  1778. visibility=Visibility.OWNER_PINNED,
  1779. )
  1780. event = self.store_event(
  1781. data={
  1782. "timestamp": iso_format(before_now(seconds=500)),
  1783. "fingerprint": ["group-1"],
  1784. "message": "ZeroDivisionError",
  1785. },
  1786. project_id=self.project.id,
  1787. )
  1788. self.store_event(
  1789. data={
  1790. "timestamp": iso_format(before_now(seconds=500)),
  1791. "fingerprint": ["group-2"],
  1792. "message": "TypeError",
  1793. },
  1794. project_id=self.project.id,
  1795. )
  1796. self.login_as(user=self.user)
  1797. response = self.get_response(
  1798. sort_by="date",
  1799. limit=10,
  1800. query="is:unresolved",
  1801. collapse=["unhandled"],
  1802. savedSearch=0,
  1803. )
  1804. assert response.status_code == 200
  1805. assert len(response.data) == 1
  1806. assert int(response.data[0]["id"]) == event.group.id
  1807. def test_query_status_and_substatus_overlapping(self):
  1808. event = self.store_event(
  1809. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1810. project_id=self.project.id,
  1811. )
  1812. event.group.update(status=GroupStatus.UNRESOLVED, substatus=GroupSubStatus.ONGOING)
  1813. self.login_as(user=self.user)
  1814. get_query_response = functools.partial(
  1815. self.get_response, sort_by="date", limit=10, expand="inbox", collapse="stats"
  1816. )
  1817. response0 = get_query_response(
  1818. query="is:unresolved",
  1819. )
  1820. with Feature("organizations:escalating-issues"):
  1821. response1 = get_query_response(
  1822. query="is:ongoing"
  1823. ) # (status=unresolved, substatus=(ongoing))
  1824. response2 = get_query_response(
  1825. query="is:unresolved"
  1826. ) # (status=unresolved, substatus=*)
  1827. response3 = get_query_response(
  1828. query="is:unresolved is:ongoing !is:regressed"
  1829. ) # (status=unresolved, substatus=(ongoing, !regressed))
  1830. response4 = get_query_response(
  1831. query="is:unresolved is:ongoing !is:ignored"
  1832. ) # (status=unresolved, substatus=(ongoing, !ignored))
  1833. response5 = get_query_response(
  1834. query="!is:regressed is:unresolved"
  1835. ) # (status=unresolved, substatus=(!regressed))
  1836. response6 = get_query_response(
  1837. query="!is:until_escalating"
  1838. ) # (status=(!unresolved), substatus=(!until_escalating))
  1839. assert (
  1840. response0.status_code
  1841. == response1.status_code
  1842. == response2.status_code
  1843. == response3.status_code
  1844. == response4.status_code
  1845. == response5.status_code
  1846. == response6.status_code
  1847. == 200
  1848. )
  1849. assert (
  1850. [int(r["id"]) for r in response0.data]
  1851. == [int(r["id"]) for r in response1.data]
  1852. == [int(r["id"]) for r in response2.data]
  1853. == [int(r["id"]) for r in response3.data]
  1854. == [int(r["id"]) for r in response4.data]
  1855. == [int(r["id"]) for r in response5.data]
  1856. == [int(r["id"]) for r in response6.data]
  1857. == [event.group.id]
  1858. )
  1859. def test_query_status_and_substatus_nonoverlapping(self):
  1860. event = self.store_event(
  1861. data={"timestamp": iso_format(before_now(seconds=500)), "fingerprint": ["group-1"]},
  1862. project_id=self.project.id,
  1863. )
  1864. event.group.update(status=GroupStatus.UNRESOLVED, substatus=GroupSubStatus.ONGOING)
  1865. self.login_as(user=self.user)
  1866. get_query_response = functools.partial(
  1867. self.get_response, sort_by="date", limit=10, expand="inbox", collapse="stats"
  1868. )
  1869. with Feature("organizations:escalating-issues"):
  1870. response1 = get_query_response(query="is:escalating")
  1871. response2 = get_query_response(query="is:new")
  1872. response3 = get_query_response(query="is:regressed")
  1873. response4 = get_query_response(query="is:forever")
  1874. response5 = get_query_response(query="is:until_condition_met")
  1875. response6 = get_query_response(query="is:until_escalating")
  1876. response7 = get_query_response(query="is:resolved")
  1877. response8 = get_query_response(query="is:ignored")
  1878. response9 = get_query_response(query="is:muted")
  1879. response10 = get_query_response(query="!is:unresolved")
  1880. assert (
  1881. response1.status_code
  1882. == response2.status_code
  1883. == response3.status_code
  1884. == response4.status_code
  1885. == response5.status_code
  1886. == response6.status_code
  1887. == response7.status_code
  1888. == response8.status_code
  1889. == response9.status_code
  1890. == response10.status_code
  1891. == 200
  1892. )
  1893. assert (
  1894. [int(r["id"]) for r in response1.data]
  1895. == [int(r["id"]) for r in response2.data]
  1896. == [int(r["id"]) for r in response3.data]
  1897. == [int(r["id"]) for r in response4.data]
  1898. == [int(r["id"]) for r in response5.data]
  1899. == [int(r["id"]) for r in response6.data]
  1900. == [int(r["id"]) for r in response7.data]
  1901. == [int(r["id"]) for r in response8.data]
  1902. == [int(r["id"]) for r in response9.data]
  1903. == [int(r["id"]) for r in response10.data]
  1904. == []
  1905. )
  1906. @region_silo_test(stable=True)
  1907. class GroupUpdateTest(APITestCase, SnubaTestCase):
  1908. endpoint = "sentry-api-0-organization-group-index"
  1909. method = "put"
  1910. def setUp(self):
  1911. super().setUp()
  1912. self.min_ago = timezone.now() - timedelta(minutes=1)
  1913. def get_response(self, *args, **kwargs):
  1914. if not args:
  1915. org = self.project.organization.slug
  1916. else:
  1917. org = args[0]
  1918. return super().get_response(org, **kwargs)
  1919. def assertNoResolution(self, group):
  1920. assert not GroupResolution.objects.filter(group=group).exists()
  1921. def test_global_resolve(self):
  1922. group1 = self.create_group(status=GroupStatus.RESOLVED)
  1923. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  1924. group3 = self.create_group(status=GroupStatus.IGNORED)
  1925. group4 = self.create_group(
  1926. project=self.create_project(slug="foo"),
  1927. status=GroupStatus.UNRESOLVED,
  1928. )
  1929. self.login_as(user=self.user)
  1930. response = self.get_success_response(
  1931. qs_params={"status": "unresolved", "project": self.project.id}, status="resolved"
  1932. )
  1933. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1934. # the previously resolved entry should not be included
  1935. new_group1 = Group.objects.get(id=group1.id)
  1936. assert new_group1.status == GroupStatus.RESOLVED
  1937. assert new_group1.resolved_at is None
  1938. # this wont exist because it wasn't affected
  1939. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group1).exists()
  1940. new_group2 = Group.objects.get(id=group2.id)
  1941. assert new_group2.status == GroupStatus.RESOLVED
  1942. assert new_group2.resolved_at is not None
  1943. assert GroupSubscription.objects.filter(
  1944. user_id=self.user.id, group=new_group2, is_active=True
  1945. ).exists()
  1946. # the ignored entry should not be included
  1947. new_group3 = Group.objects.get(id=group3.id)
  1948. assert new_group3.status == GroupStatus.IGNORED
  1949. assert new_group3.resolved_at is None
  1950. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group3)
  1951. new_group4 = Group.objects.get(id=group4.id)
  1952. assert new_group4.status == GroupStatus.UNRESOLVED
  1953. assert new_group4.resolved_at is None
  1954. assert not GroupSubscription.objects.filter(user_id=self.user.id, group=new_group4)
  1955. assert not GroupHistory.objects.filter(
  1956. group=group1, status=GroupHistoryStatus.RESOLVED
  1957. ).exists()
  1958. assert GroupHistory.objects.filter(
  1959. group=group2, status=GroupHistoryStatus.RESOLVED
  1960. ).exists()
  1961. assert not GroupHistory.objects.filter(
  1962. group=group3, status=GroupHistoryStatus.RESOLVED
  1963. ).exists()
  1964. assert not GroupHistory.objects.filter(
  1965. group=group4, status=GroupHistoryStatus.RESOLVED
  1966. ).exists()
  1967. def test_resolve_member(self):
  1968. group = self.create_group(status=GroupStatus.UNRESOLVED)
  1969. member = self.create_user()
  1970. self.create_member(
  1971. organization=self.organization, teams=group.project.teams.all(), user=member
  1972. )
  1973. self.login_as(user=member)
  1974. response = self.get_success_response(
  1975. qs_params={"status": "unresolved", "project": self.project.id}, status="resolved"
  1976. )
  1977. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1978. assert response.status_code == 200
  1979. def test_resolve_ignored(self):
  1980. group = self.create_group(status=GroupStatus.IGNORED)
  1981. snooze = GroupSnooze.objects.create(
  1982. group=group, until=timezone.now() - timedelta(minutes=1)
  1983. )
  1984. member = self.create_user()
  1985. self.create_member(
  1986. organization=self.organization, teams=group.project.teams.all(), user=member
  1987. )
  1988. self.login_as(user=member)
  1989. response = self.get_success_response(
  1990. qs_params={"id": group.id, "project": self.project.id}, status="resolved"
  1991. )
  1992. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  1993. assert not GroupSnooze.objects.filter(id=snooze.id).exists()
  1994. def test_bulk_resolve(self):
  1995. self.login_as(user=self.user)
  1996. for i in range(200):
  1997. self.store_event(
  1998. data={
  1999. "fingerprint": [i],
  2000. "timestamp": iso_format(self.min_ago - timedelta(seconds=i)),
  2001. },
  2002. project_id=self.project.id,
  2003. )
  2004. response = self.get_success_response(query="is:unresolved", sort_by="date", method="get")
  2005. assert len(response.data) == 100
  2006. response = self.get_success_response(qs_params={"status": "unresolved"}, status="resolved")
  2007. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  2008. response = self.get_success_response(query="is:unresolved", sort_by="date", method="get")
  2009. assert len(response.data) == 0
  2010. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  2011. def test_resolve_with_integration(self, mock_sync_status_outbound):
  2012. self.login_as(user=self.user)
  2013. org = self.organization
  2014. with assume_test_silo_mode(SiloMode.CONTROL):
  2015. integration = Integration.objects.create(provider="example", name="Example")
  2016. integration.add_organization(org, self.user)
  2017. event = self.store_event(
  2018. data={"timestamp": iso_format(self.min_ago)}, project_id=self.project.id
  2019. )
  2020. group = event.group
  2021. with assume_test_silo_mode(SiloMode.CONTROL):
  2022. OrganizationIntegration.objects.filter(
  2023. integration_id=integration.id, organization_id=group.organization.id
  2024. ).update(
  2025. config={
  2026. "sync_comments": True,
  2027. "sync_status_outbound": True,
  2028. "sync_status_inbound": True,
  2029. "sync_assignee_outbound": True,
  2030. "sync_assignee_inbound": True,
  2031. }
  2032. )
  2033. external_issue = ExternalIssue.objects.get_or_create(
  2034. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  2035. )[0]
  2036. GroupLink.objects.get_or_create(
  2037. group_id=group.id,
  2038. project_id=group.project_id,
  2039. linked_type=GroupLink.LinkedType.issue,
  2040. linked_id=external_issue.id,
  2041. relationship=GroupLink.Relationship.references,
  2042. )[0]
  2043. response = self.get_success_response(sort_by="date", query="is:unresolved", method="get")
  2044. assert len(response.data) == 1
  2045. with self.tasks():
  2046. with self.feature({"organizations:integrations-issue-sync": True}):
  2047. response = self.get_success_response(
  2048. qs_params={"status": "unresolved"}, status="resolved"
  2049. )
  2050. group = Group.objects.get(id=group.id)
  2051. assert group.status == GroupStatus.RESOLVED
  2052. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  2053. mock_sync_status_outbound.assert_called_once_with(
  2054. external_issue, True, group.project_id
  2055. )
  2056. response = self.get_success_response(sort_by="date", query="is:unresolved", method="get")
  2057. assert len(response.data) == 0
  2058. @patch("sentry.integrations.example.integration.ExampleIntegration.sync_status_outbound")
  2059. def test_set_unresolved_with_integration(self, mock_sync_status_outbound):
  2060. release = self.create_release(project=self.project, version="abc")
  2061. group = self.create_group(status=GroupStatus.RESOLVED)
  2062. with assume_test_silo_mode(SiloMode.CONTROL):
  2063. org = self.organization
  2064. integration = Integration.objects.create(provider="example", name="Example")
  2065. integration.add_organization(org, self.user)
  2066. OrganizationIntegration.objects.filter(
  2067. integration_id=integration.id, organization_id=group.organization.id
  2068. ).update(
  2069. config={
  2070. "sync_comments": True,
  2071. "sync_status_outbound": True,
  2072. "sync_status_inbound": True,
  2073. "sync_assignee_outbound": True,
  2074. "sync_assignee_inbound": True,
  2075. }
  2076. )
  2077. GroupResolution.objects.create(group=group, release=release)
  2078. external_issue = ExternalIssue.objects.get_or_create(
  2079. organization_id=org.id, integration_id=integration.id, key="APP-%s" % group.id
  2080. )[0]
  2081. GroupLink.objects.get_or_create(
  2082. group_id=group.id,
  2083. project_id=group.project_id,
  2084. linked_type=GroupLink.LinkedType.issue,
  2085. linked_id=external_issue.id,
  2086. relationship=GroupLink.Relationship.references,
  2087. )[0]
  2088. self.login_as(user=self.user)
  2089. with self.tasks():
  2090. with self.feature({"organizations:integrations-issue-sync": True}):
  2091. response = self.get_success_response(
  2092. qs_params={"id": group.id}, status="unresolved"
  2093. )
  2094. assert response.status_code == 200
  2095. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2096. group = Group.objects.get(id=group.id)
  2097. assert group.status == GroupStatus.UNRESOLVED
  2098. self.assertNoResolution(group)
  2099. assert GroupSubscription.objects.filter(
  2100. user_id=self.user.id, group=group, is_active=True
  2101. ).exists()
  2102. mock_sync_status_outbound.assert_called_once_with(
  2103. external_issue, False, group.project_id
  2104. )
  2105. def test_self_assign_issue(self):
  2106. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2107. user = self.user
  2108. with assume_test_silo_mode(SiloMode.CONTROL):
  2109. uo1 = UserOption.objects.create(
  2110. key="self_assign_issue", value="1", project_id=None, user=user
  2111. )
  2112. self.login_as(user=user)
  2113. response = self.get_success_response(qs_params={"id": group.id}, status="resolved")
  2114. assert response.data["assignedTo"]["id"] == str(user.id)
  2115. assert response.data["assignedTo"]["type"] == "user"
  2116. assert response.data["status"] == "resolved"
  2117. assert GroupAssignee.objects.filter(group=group, user_id=user.id).exists()
  2118. assert GroupSubscription.objects.filter(
  2119. user_id=user.id, group=group, is_active=True
  2120. ).exists()
  2121. with assume_test_silo_mode(SiloMode.CONTROL):
  2122. uo1.delete()
  2123. def test_self_assign_issue_next_release(self):
  2124. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2125. release.add_project(self.project)
  2126. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2127. with assume_test_silo_mode(SiloMode.CONTROL):
  2128. uo1 = UserOption.objects.create(
  2129. key="self_assign_issue", value="1", project_id=None, user=self.user
  2130. )
  2131. self.login_as(user=self.user)
  2132. response = self.get_success_response(
  2133. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2134. )
  2135. assert response.data["status"] == "resolved"
  2136. assert response.data["statusDetails"]["inNextRelease"]
  2137. assert response.data["assignedTo"]["id"] == str(self.user.id)
  2138. assert response.data["assignedTo"]["type"] == "user"
  2139. group = Group.objects.get(id=group.id)
  2140. assert group.status == GroupStatus.RESOLVED
  2141. assert GroupResolution.objects.filter(group=group, release=release).exists()
  2142. assert GroupSubscription.objects.filter(
  2143. user_id=self.user.id, group=group, is_active=True
  2144. ).exists()
  2145. activity = Activity.objects.get(
  2146. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2147. )
  2148. assert activity.data["version"] == ""
  2149. with assume_test_silo_mode(SiloMode.CONTROL):
  2150. uo1.delete()
  2151. def test_in_semver_projects_group_resolution_stores_current_release_version(self):
  2152. """
  2153. Test that ensures that when we resolve a group in the next release, then
  2154. GroupResolution.current_release_version is set to the latest release associated with a
  2155. Group, when the project follows semantic versioning scheme
  2156. """
  2157. release_1 = self.create_release(version="fake_package@21.1.0")
  2158. release_2 = self.create_release(version="fake_package@21.1.1")
  2159. release_3 = self.create_release(version="fake_package@21.1.2")
  2160. self.store_event(
  2161. data={
  2162. "timestamp": iso_format(before_now(seconds=10)),
  2163. "fingerprint": ["group-1"],
  2164. "release": release_2.version,
  2165. },
  2166. project_id=self.project.id,
  2167. )
  2168. group = self.store_event(
  2169. data={
  2170. "timestamp": iso_format(before_now(seconds=12)),
  2171. "fingerprint": ["group-1"],
  2172. "release": release_1.version,
  2173. },
  2174. project_id=self.project.id,
  2175. ).group
  2176. self.login_as(user=self.user)
  2177. response = self.get_success_response(
  2178. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2179. )
  2180. assert response.data["status"] == "resolved"
  2181. assert response.data["statusDetails"]["inNextRelease"]
  2182. # The current_release_version should be to the latest (in semver) release associated with
  2183. # a group
  2184. grp_resolution = GroupResolution.objects.filter(group=group)
  2185. assert len(grp_resolution) == 1
  2186. grp_resolution = grp_resolution.first()
  2187. assert grp_resolution.current_release_version == release_2.version
  2188. # "resolvedInNextRelease" with semver releases is considered as "resolvedInRelease"
  2189. assert grp_resolution.type == GroupResolution.Type.in_release
  2190. assert grp_resolution.status == GroupResolution.Status.resolved
  2191. # Add release that is between 2 and 3 to ensure that any release after release 2 should
  2192. # not have a resolution
  2193. release_4 = self.create_release(version="fake_package@21.1.1+1")
  2194. for release in [release_1, release_2]:
  2195. assert GroupResolution.has_resolution(group=group, release=release)
  2196. for release in [release_3, release_4]:
  2197. assert not GroupResolution.has_resolution(group=group, release=release)
  2198. # Ensure that Activity has `current_release_version` set on `Resolved in next release`
  2199. activity = Activity.objects.filter(
  2200. group=grp_resolution.group,
  2201. type=ActivityType.SET_RESOLVED_IN_RELEASE.value,
  2202. ident=grp_resolution.id,
  2203. ).first()
  2204. assert "current_release_version" in activity.data
  2205. assert activity.data["current_release_version"] == release_2.version
  2206. def test_in_non_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 most recent release associated with a
  2210. Group, when the project does not follow semantic versioning scheme
  2211. """
  2212. release_1 = self.create_release(
  2213. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2214. )
  2215. release_2 = self.create_release(version="foobar 2")
  2216. group = self.store_event(
  2217. data={
  2218. "timestamp": iso_format(before_now(seconds=12)),
  2219. "fingerprint": ["group-1"],
  2220. "release": release_1.version,
  2221. },
  2222. project_id=self.project.id,
  2223. ).group
  2224. self.login_as(user=self.user)
  2225. response = self.get_success_response(
  2226. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2227. )
  2228. assert response.data["status"] == "resolved"
  2229. assert response.data["statusDetails"]["inNextRelease"]
  2230. # Add a new release that is between 1 and 2, to make sure that if a the same issue/group
  2231. # occurs in that issue, then it should not have a resolution
  2232. release_3 = self.create_release(
  2233. date_added=timezone.now() - timedelta(minutes=30), version="foobar 3"
  2234. )
  2235. grp_resolution = GroupResolution.objects.filter(group=group)
  2236. assert len(grp_resolution) == 1
  2237. assert grp_resolution[0].current_release_version == release_1.version
  2238. assert GroupResolution.has_resolution(group=group, release=release_1)
  2239. for release in [release_2, release_3]:
  2240. assert not GroupResolution.has_resolution(group=group, release=release)
  2241. def test_in_non_semver_projects_store_actual_current_release_version_not_cached_version(self):
  2242. """
  2243. Test that ensures that the current_release_version is actually the latest version
  2244. associated with a group, not the cached version because currently
  2245. `group.get_last_release` fetches the latest release associated with a group and caches
  2246. that value, and we don't want to cache that value when resolving in next release in case a
  2247. new release appears to be associated with a group because if we store the cached rather
  2248. than the actual latest release, we might have unexpected results with the regression
  2249. algorithm
  2250. """
  2251. release_1 = self.create_release(
  2252. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2253. )
  2254. release_2 = self.create_release(version="foobar 2")
  2255. group = self.store_event(
  2256. data={
  2257. "timestamp": iso_format(before_now(seconds=12)),
  2258. "fingerprint": ["group-1"],
  2259. "release": release_1.version,
  2260. },
  2261. project_id=self.project.id,
  2262. ).group
  2263. # Call this function to cache the `last_seen` release to release_1
  2264. # i.e. Set the first last observed by Sentry
  2265. assert group.get_last_release() == release_1.version
  2266. self.login_as(user=self.user)
  2267. self.store_event(
  2268. data={
  2269. "timestamp": iso_format(before_now(seconds=0)),
  2270. "fingerprint": ["group-1"],
  2271. "release": release_2.version,
  2272. },
  2273. project_id=self.project.id,
  2274. )
  2275. # Cached (i.e. first last observed release by Sentry) is returned here since `use_cache`
  2276. # is set to its default of `True`
  2277. assert Group.objects.get(id=group.id).get_last_release() == release_1.version
  2278. response = self.get_success_response(
  2279. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2280. )
  2281. assert response.data["status"] == "resolved"
  2282. assert response.data["statusDetails"]["inNextRelease"]
  2283. # Changes here to release_2 and actual latest because `resolvedInNextRelease`,
  2284. # sets `use_cache` to False when fetching the last release associated with a group
  2285. assert Group.objects.get(id=group.id).get_last_release() == release_2.version
  2286. grp_resolution = GroupResolution.objects.filter(group=group)
  2287. assert len(grp_resolution) == 1
  2288. assert grp_resolution[0].current_release_version == release_2.version
  2289. def test_in_non_semver_projects_resolved_in_next_release_is_equated_to_in_release(self):
  2290. """
  2291. Test that ensures that if we basically know the next release when clicking on Resolved
  2292. In Next Release because that release exists, then we can short circuit setting
  2293. GroupResolution to type "inNextRelease", and then having `clear_expired_resolutions` run
  2294. once a new release is created to convert GroupResolution to in_release and set Activity.
  2295. Basically we treat "ResolvedInNextRelease" as "ResolvedInRelease" when there is a release
  2296. that was created after the last release associated with the group being resolved
  2297. """
  2298. release_1 = self.create_release(
  2299. date_added=timezone.now() - timedelta(minutes=45), version="foobar 1"
  2300. )
  2301. release_2 = self.create_release(version="foobar 2")
  2302. self.create_release(version="foobar 3")
  2303. group = self.store_event(
  2304. data={
  2305. "timestamp": iso_format(before_now(seconds=12)),
  2306. "fingerprint": ["group-1"],
  2307. "release": release_1.version,
  2308. },
  2309. project_id=self.project.id,
  2310. ).group
  2311. self.login_as(user=self.user)
  2312. response = self.get_success_response(
  2313. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2314. )
  2315. assert response.data["status"] == "resolved"
  2316. assert response.data["statusDetails"]["inNextRelease"]
  2317. grp_resolution = GroupResolution.objects.filter(group=group)
  2318. assert len(grp_resolution) == 1
  2319. grp_resolution = grp_resolution[0]
  2320. assert grp_resolution.current_release_version == release_1.version
  2321. assert grp_resolution.release.id == release_2.id
  2322. assert grp_resolution.type == GroupResolution.Type.in_release
  2323. assert grp_resolution.status == GroupResolution.Status.resolved
  2324. activity = Activity.objects.filter(
  2325. group=grp_resolution.group,
  2326. type=ActivityType.SET_RESOLVED_IN_RELEASE.value,
  2327. ident=grp_resolution.id,
  2328. ).first()
  2329. assert activity.data["version"] == release_2.version
  2330. def test_selective_status_update(self):
  2331. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2332. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2333. group3 = self.create_group(status=GroupStatus.IGNORED)
  2334. group4 = self.create_group(
  2335. project=self.create_project(slug="foo"),
  2336. status=GroupStatus.UNRESOLVED,
  2337. )
  2338. self.login_as(user=self.user)
  2339. with self.feature("organizations:global-views"):
  2340. response = self.get_success_response(
  2341. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, status="resolved"
  2342. )
  2343. assert response.data == {"status": "resolved", "statusDetails": {}, "inbox": None}
  2344. new_group1 = Group.objects.get(id=group1.id)
  2345. assert new_group1.resolved_at is not None
  2346. assert new_group1.status == GroupStatus.RESOLVED
  2347. new_group2 = Group.objects.get(id=group2.id)
  2348. assert new_group2.resolved_at is not None
  2349. assert new_group2.status == GroupStatus.RESOLVED
  2350. assert GroupSubscription.objects.filter(
  2351. user_id=self.user.id, group=new_group2, is_active=True
  2352. ).exists()
  2353. new_group3 = Group.objects.get(id=group3.id)
  2354. assert new_group3.resolved_at is None
  2355. assert new_group3.status == GroupStatus.IGNORED
  2356. new_group4 = Group.objects.get(id=group4.id)
  2357. assert new_group4.resolved_at is None
  2358. assert new_group4.status == GroupStatus.UNRESOLVED
  2359. def test_set_resolved_in_current_release(self):
  2360. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2361. release.add_project(self.project)
  2362. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2363. self.login_as(user=self.user)
  2364. response = self.get_success_response(
  2365. qs_params={"id": group.id}, status="resolved", statusDetails={"inRelease": "latest"}
  2366. )
  2367. assert response.data["status"] == "resolved"
  2368. assert response.data["statusDetails"]["inRelease"] == release.version
  2369. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2370. group = Group.objects.get(id=group.id)
  2371. assert group.status == GroupStatus.RESOLVED
  2372. resolution = GroupResolution.objects.get(group=group)
  2373. assert resolution.release == release
  2374. assert resolution.type == GroupResolution.Type.in_release
  2375. assert resolution.status == GroupResolution.Status.resolved
  2376. assert resolution.actor_id == self.user.id
  2377. assert GroupSubscription.objects.filter(
  2378. user_id=self.user.id, group=group, is_active=True
  2379. ).exists()
  2380. activity = Activity.objects.get(
  2381. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2382. )
  2383. assert activity.data["version"] == release.version
  2384. assert GroupHistory.objects.filter(
  2385. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_RELEASE
  2386. ).exists()
  2387. def test_set_resolved_in_explicit_release(self):
  2388. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2389. release.add_project(self.project)
  2390. release2 = Release.objects.create(organization_id=self.project.organization_id, version="b")
  2391. release2.add_project(self.project)
  2392. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2393. self.login_as(user=self.user)
  2394. response = self.get_success_response(
  2395. qs_params={"id": group.id},
  2396. status="resolved",
  2397. statusDetails={"inRelease": release.version},
  2398. )
  2399. assert response.data["status"] == "resolved"
  2400. assert response.data["statusDetails"]["inRelease"] == release.version
  2401. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2402. assert "activity" in response.data
  2403. group = Group.objects.get(id=group.id)
  2404. assert group.status == GroupStatus.RESOLVED
  2405. resolution = GroupResolution.objects.get(group=group)
  2406. assert resolution.release == release
  2407. assert resolution.type == GroupResolution.Type.in_release
  2408. assert resolution.status == GroupResolution.Status.resolved
  2409. assert resolution.actor_id == self.user.id
  2410. assert GroupSubscription.objects.filter(
  2411. user_id=self.user.id, group=group, is_active=True
  2412. ).exists()
  2413. activity = Activity.objects.get(
  2414. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2415. )
  2416. assert activity.data["version"] == release.version
  2417. def test_in_semver_projects_set_resolved_in_explicit_release(self):
  2418. release_1 = self.create_release(version="fake_package@3.0.0")
  2419. release_2 = self.create_release(version="fake_package@2.0.0")
  2420. release_3 = self.create_release(version="fake_package@3.0.1")
  2421. group = self.store_event(
  2422. data={
  2423. "timestamp": iso_format(before_now(seconds=10)),
  2424. "fingerprint": ["group-1"],
  2425. "release": release_1.version,
  2426. },
  2427. project_id=self.project.id,
  2428. ).group
  2429. self.login_as(user=self.user)
  2430. response = self.get_success_response(
  2431. qs_params={"id": group.id},
  2432. status="resolved",
  2433. statusDetails={"inRelease": release_1.version},
  2434. )
  2435. assert response.data["status"] == "resolved"
  2436. assert response.data["statusDetails"]["inRelease"] == release_1.version
  2437. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2438. assert "activity" in response.data
  2439. group = Group.objects.get(id=group.id)
  2440. assert group.status == GroupStatus.RESOLVED
  2441. resolution = GroupResolution.objects.get(group=group)
  2442. assert resolution.release == release_1
  2443. assert resolution.type == GroupResolution.Type.in_release
  2444. assert resolution.status == GroupResolution.Status.resolved
  2445. assert resolution.actor_id == self.user.id
  2446. assert GroupSubscription.objects.filter(
  2447. user_id=self.user.id, group=group, is_active=True
  2448. ).exists()
  2449. activity = Activity.objects.get(
  2450. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2451. )
  2452. assert activity.data["version"] == release_1.version
  2453. assert GroupResolution.has_resolution(group=group, release=release_2)
  2454. assert not GroupResolution.has_resolution(group=group, release=release_3)
  2455. def test_set_resolved_in_next_release(self):
  2456. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2457. release.add_project(self.project)
  2458. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2459. self.login_as(user=self.user)
  2460. response = self.get_success_response(
  2461. qs_params={"id": group.id}, status="resolved", statusDetails={"inNextRelease": True}
  2462. )
  2463. assert response.data["status"] == "resolved"
  2464. assert response.data["statusDetails"]["inNextRelease"]
  2465. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2466. assert "activity" in response.data
  2467. group = Group.objects.get(id=group.id)
  2468. assert group.status == GroupStatus.RESOLVED
  2469. resolution = GroupResolution.objects.get(group=group)
  2470. assert resolution.release == release
  2471. assert resolution.type == GroupResolution.Type.in_next_release
  2472. assert resolution.status == GroupResolution.Status.pending
  2473. assert resolution.actor_id == self.user.id
  2474. assert GroupSubscription.objects.filter(
  2475. user_id=self.user.id, group=group, is_active=True
  2476. ).exists()
  2477. activity = Activity.objects.get(
  2478. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2479. )
  2480. assert activity.data["version"] == ""
  2481. def test_set_resolved_in_next_release_legacy(self):
  2482. release = Release.objects.create(organization_id=self.project.organization_id, version="a")
  2483. release.add_project(self.project)
  2484. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2485. self.login_as(user=self.user)
  2486. response = self.get_success_response(
  2487. qs_params={"id": group.id}, status="resolvedInNextRelease"
  2488. )
  2489. assert response.data["status"] == "resolved"
  2490. assert response.data["statusDetails"]["inNextRelease"]
  2491. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2492. assert "activity" in response.data
  2493. group = Group.objects.get(id=group.id)
  2494. assert group.status == GroupStatus.RESOLVED
  2495. resolution = GroupResolution.objects.get(group=group)
  2496. assert resolution.release == release
  2497. assert resolution.type == GroupResolution.Type.in_next_release
  2498. assert resolution.status == GroupResolution.Status.pending
  2499. assert resolution.actor_id == self.user.id
  2500. assert GroupSubscription.objects.filter(
  2501. user_id=self.user.id, group=group, is_active=True
  2502. ).exists()
  2503. assert GroupHistory.objects.filter(
  2504. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_RELEASE
  2505. ).exists()
  2506. activity = Activity.objects.get(
  2507. group=group, type=ActivityType.SET_RESOLVED_IN_RELEASE.value
  2508. )
  2509. assert activity.data["version"] == ""
  2510. def test_set_resolved_in_explicit_commit_unreleased(self):
  2511. repo = self.create_repo(project=self.project, name=self.project.name)
  2512. commit = self.create_commit(project=self.project, repo=repo)
  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},
  2517. status="resolved",
  2518. statusDetails={"inCommit": {"commit": commit.key, "repository": repo.name}},
  2519. )
  2520. assert response.data["status"] == "resolved"
  2521. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  2522. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2523. assert "activity" not in response.data
  2524. group = Group.objects.get(id=group.id)
  2525. assert group.status == GroupStatus.RESOLVED
  2526. link = GroupLink.objects.get(group_id=group.id)
  2527. assert link.linked_type == GroupLink.LinkedType.commit
  2528. assert link.relationship == GroupLink.Relationship.resolves
  2529. assert link.linked_id == commit.id
  2530. assert GroupSubscription.objects.filter(
  2531. user_id=self.user.id, group=group, is_active=True
  2532. ).exists()
  2533. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  2534. assert activity.data["commit"] == commit.id
  2535. assert GroupHistory.objects.filter(
  2536. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2537. ).exists()
  2538. def test_set_resolved_in_explicit_commit_released(self):
  2539. release = self.create_release(project=self.project)
  2540. repo = self.create_repo(project=self.project, name=self.project.name)
  2541. commit = self.create_commit(project=self.project, repo=repo, release=release)
  2542. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2543. self.login_as(user=self.user)
  2544. response = self.get_success_response(
  2545. qs_params={"id": group.id},
  2546. status="resolved",
  2547. statusDetails={"inCommit": {"commit": commit.key, "repository": repo.name}},
  2548. )
  2549. assert response.data["status"] == "resolved"
  2550. assert response.data["statusDetails"]["inCommit"]["id"] == commit.key
  2551. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2552. assert "activity" in response.data
  2553. group = Group.objects.get(id=group.id)
  2554. assert group.status == GroupStatus.RESOLVED
  2555. link = GroupLink.objects.get(group_id=group.id)
  2556. assert link.project_id == self.project.id
  2557. assert link.linked_type == GroupLink.LinkedType.commit
  2558. assert link.relationship == GroupLink.Relationship.resolves
  2559. assert link.linked_id == commit.id
  2560. assert GroupSubscription.objects.filter(
  2561. user_id=self.user.id, group=group, is_active=True
  2562. ).exists()
  2563. activity = Activity.objects.get(group=group, type=ActivityType.SET_RESOLVED_IN_COMMIT.value)
  2564. assert activity.data["commit"] == commit.id
  2565. resolution = GroupResolution.objects.get(group=group)
  2566. assert resolution.type == GroupResolution.Type.in_release
  2567. assert resolution.status == GroupResolution.Status.resolved
  2568. assert GroupHistory.objects.filter(
  2569. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2570. ).exists()
  2571. def test_set_resolved_in_explicit_commit_missing(self):
  2572. repo = self.create_repo(project=self.project, name=self.project.name)
  2573. group = self.create_group(status=GroupStatus.UNRESOLVED)
  2574. self.login_as(user=self.user)
  2575. response = self.get_response(
  2576. qs_params={"id": group.id},
  2577. status="resolved",
  2578. statusDetails={"inCommit": {"commit": "a" * 40, "repository": repo.name}},
  2579. )
  2580. assert response.status_code == 400
  2581. assert (
  2582. response.data["statusDetails"]["inCommit"]["commit"][0]
  2583. == "Unable to find the given commit."
  2584. )
  2585. assert not GroupHistory.objects.filter(
  2586. group=group, status=GroupHistoryStatus.SET_RESOLVED_IN_COMMIT
  2587. ).exists()
  2588. def test_set_unresolved(self):
  2589. release = self.create_release(project=self.project, version="abc")
  2590. group = self.create_group(status=GroupStatus.RESOLVED)
  2591. GroupResolution.objects.create(group=group, release=release)
  2592. self.login_as(user=self.user)
  2593. response = self.get_success_response(qs_params={"id": group.id}, status="unresolved")
  2594. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2595. group = Group.objects.get(id=group.id)
  2596. assert group.status == GroupStatus.UNRESOLVED
  2597. assert GroupHistory.objects.filter(
  2598. group=group, status=GroupHistoryStatus.UNRESOLVED
  2599. ).exists()
  2600. self.assertNoResolution(group)
  2601. assert GroupSubscription.objects.filter(
  2602. user_id=self.user.id, group=group, is_active=True
  2603. ).exists()
  2604. def test_set_unresolved_on_snooze(self):
  2605. group = self.create_group(status=GroupStatus.IGNORED)
  2606. GroupSnooze.objects.create(group=group, until=timezone.now() - timedelta(days=1))
  2607. self.login_as(user=self.user)
  2608. response = self.get_success_response(qs_params={"id": group.id}, status="unresolved")
  2609. assert response.data == {"status": "unresolved", "statusDetails": {}}
  2610. group = Group.objects.get(id=group.id)
  2611. assert group.status == GroupStatus.UNRESOLVED
  2612. assert GroupHistory.objects.filter(
  2613. group=group, status=GroupHistoryStatus.UNRESOLVED
  2614. ).exists()
  2615. def test_basic_ignore(self):
  2616. group = self.create_group(status=GroupStatus.RESOLVED)
  2617. snooze = GroupSnooze.objects.create(group=group, until=timezone.now())
  2618. self.login_as(user=self.user)
  2619. assert not GroupHistory.objects.filter(
  2620. group=group, status=GroupHistoryStatus.IGNORED
  2621. ).exists()
  2622. response = self.get_success_response(qs_params={"id": group.id}, status="ignored")
  2623. # existing snooze objects should be cleaned up
  2624. assert not GroupSnooze.objects.filter(id=snooze.id).exists()
  2625. group = Group.objects.get(id=group.id)
  2626. assert group.status == GroupStatus.IGNORED
  2627. assert GroupHistory.objects.filter(group=group, status=GroupHistoryStatus.IGNORED).exists()
  2628. assert response.data == {"status": "ignored", "statusDetails": {}, "inbox": None}
  2629. def test_snooze_duration(self):
  2630. group = self.create_group(status=GroupStatus.RESOLVED)
  2631. self.login_as(user=self.user)
  2632. response = self.get_success_response(
  2633. qs_params={"id": group.id}, status="ignored", ignoreDuration=30
  2634. )
  2635. snooze = GroupSnooze.objects.get(group=group)
  2636. snooze.until = snooze.until
  2637. now = timezone.now()
  2638. assert snooze.count is None
  2639. assert snooze.until is not None
  2640. assert snooze.until > now + timedelta(minutes=29)
  2641. assert snooze.until < now + timedelta(minutes=31)
  2642. assert snooze.user_count is None
  2643. assert snooze.user_window is None
  2644. assert snooze.window is None
  2645. response.data["statusDetails"]["ignoreUntil"] = response.data["statusDetails"][
  2646. "ignoreUntil"
  2647. ]
  2648. assert response.data["status"] == "ignored"
  2649. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2650. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2651. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2652. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2653. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2654. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2655. def test_snooze_count(self):
  2656. group = self.create_group(status=GroupStatus.RESOLVED, times_seen=1)
  2657. self.login_as(user=self.user)
  2658. response = self.get_success_response(
  2659. qs_params={"id": group.id}, status="ignored", ignoreCount=100
  2660. )
  2661. snooze = GroupSnooze.objects.get(group=group)
  2662. assert snooze.count == 100
  2663. assert snooze.until is None
  2664. assert snooze.user_count is None
  2665. assert snooze.user_window is None
  2666. assert snooze.window is None
  2667. assert snooze.state["times_seen"] == 1
  2668. assert response.data["status"] == "ignored"
  2669. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2670. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2671. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2672. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2673. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2674. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2675. def test_snooze_user_count(self):
  2676. for i in range(10):
  2677. event = self.store_event(
  2678. data={
  2679. "fingerprint": ["put-me-in-group-1"],
  2680. "user": {"id": str(i)},
  2681. "timestamp": iso_format(self.min_ago + timedelta(seconds=i)),
  2682. },
  2683. project_id=self.project.id,
  2684. )
  2685. assert event.group is not None
  2686. group = Group.objects.get(id=event.group.id)
  2687. group.status = GroupStatus.RESOLVED
  2688. group.substatus = None
  2689. group.save()
  2690. self.login_as(user=self.user)
  2691. response = self.get_success_response(
  2692. qs_params={"id": group.id}, status="ignored", ignoreUserCount=10
  2693. )
  2694. snooze = GroupSnooze.objects.get(group=group)
  2695. assert snooze.count is None
  2696. assert snooze.until is None
  2697. assert snooze.user_count == 10
  2698. assert snooze.user_window is None
  2699. assert snooze.window is None
  2700. assert snooze.state["users_seen"] == 10
  2701. assert response.data["status"] == "ignored"
  2702. assert response.data["statusDetails"]["ignoreCount"] == snooze.count
  2703. assert response.data["statusDetails"]["ignoreWindow"] == snooze.window
  2704. assert response.data["statusDetails"]["ignoreUserCount"] == snooze.user_count
  2705. assert response.data["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  2706. assert response.data["statusDetails"]["ignoreUntil"] == snooze.until
  2707. assert response.data["statusDetails"]["actor"]["id"] == str(self.user.id)
  2708. def test_set_bookmarked(self):
  2709. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2710. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2711. group3 = self.create_group(status=GroupStatus.IGNORED)
  2712. group4 = self.create_group(
  2713. project=self.create_project(slug="foo"),
  2714. status=GroupStatus.UNRESOLVED,
  2715. )
  2716. self.login_as(user=self.user)
  2717. with self.feature("organizations:global-views"):
  2718. response = self.get_success_response(
  2719. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, isBookmarked="true"
  2720. )
  2721. assert response.data == {"isBookmarked": True}
  2722. bookmark1 = GroupBookmark.objects.filter(group=group1, user_id=self.user.id)
  2723. assert bookmark1.exists()
  2724. assert GroupSubscription.objects.filter(
  2725. user_id=self.user.id, group=group1, is_active=True
  2726. ).exists()
  2727. bookmark2 = GroupBookmark.objects.filter(group=group2, user_id=self.user.id)
  2728. assert bookmark2.exists()
  2729. assert GroupSubscription.objects.filter(
  2730. user_id=self.user.id, group=group2, is_active=True
  2731. ).exists()
  2732. bookmark3 = GroupBookmark.objects.filter(group=group3, user_id=self.user.id)
  2733. assert not bookmark3.exists()
  2734. bookmark4 = GroupBookmark.objects.filter(group=group4, user_id=self.user.id)
  2735. assert not bookmark4.exists()
  2736. def test_subscription(self):
  2737. group1 = self.create_group()
  2738. group2 = self.create_group()
  2739. group3 = self.create_group()
  2740. group4 = self.create_group(project=self.create_project(slug="foo"))
  2741. self.login_as(user=self.user)
  2742. with self.feature("organizations:global-views"):
  2743. response = self.get_success_response(
  2744. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, isSubscribed="true"
  2745. )
  2746. assert response.data == {"isSubscribed": True, "subscriptionDetails": {"reason": "unknown"}}
  2747. assert GroupSubscription.objects.filter(
  2748. group=group1, user_id=self.user.id, is_active=True
  2749. ).exists()
  2750. assert GroupSubscription.objects.filter(
  2751. group=group2, user_id=self.user.id, is_active=True
  2752. ).exists()
  2753. assert not GroupSubscription.objects.filter(group=group3, user_id=self.user.id).exists()
  2754. assert not GroupSubscription.objects.filter(group=group4, user_id=self.user.id).exists()
  2755. def test_set_public(self):
  2756. group1 = self.create_group()
  2757. group2 = self.create_group()
  2758. self.login_as(user=self.user)
  2759. response = self.get_success_response(
  2760. qs_params={"id": [group1.id, group2.id]}, isPublic="true"
  2761. )
  2762. assert response.data["isPublic"] is True
  2763. assert "shareId" in response.data
  2764. new_group1 = Group.objects.get(id=group1.id)
  2765. assert bool(new_group1.get_share_id())
  2766. new_group2 = Group.objects.get(id=group2.id)
  2767. assert bool(new_group2.get_share_id())
  2768. def test_set_private(self):
  2769. group1 = self.create_group()
  2770. group2 = self.create_group()
  2771. # Manually mark them as shared
  2772. for g in group1, group2:
  2773. GroupShare.objects.create(project_id=g.project_id, group=g)
  2774. assert bool(g.get_share_id())
  2775. self.login_as(user=self.user)
  2776. response = self.get_success_response(
  2777. qs_params={"id": [group1.id, group2.id]}, isPublic="false"
  2778. )
  2779. assert response.data == {"isPublic": False, "shareId": None}
  2780. new_group1 = Group.objects.get(id=group1.id)
  2781. assert not bool(new_group1.get_share_id())
  2782. new_group2 = Group.objects.get(id=group2.id)
  2783. assert not bool(new_group2.get_share_id())
  2784. def test_set_has_seen(self):
  2785. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2786. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  2787. group3 = self.create_group(status=GroupStatus.IGNORED)
  2788. group4 = self.create_group(
  2789. project=self.create_project(slug="foo"),
  2790. status=GroupStatus.UNRESOLVED,
  2791. )
  2792. self.login_as(user=self.user)
  2793. with self.feature("organizations:global-views"):
  2794. response = self.get_success_response(
  2795. qs_params={"id": [group1.id, group2.id], "group4": group4.id}, hasSeen="true"
  2796. )
  2797. assert response.data == {"hasSeen": True}
  2798. r1 = GroupSeen.objects.filter(group=group1, user_id=self.user.id)
  2799. assert r1.exists()
  2800. r2 = GroupSeen.objects.filter(group=group2, user_id=self.user.id)
  2801. assert r2.exists()
  2802. r3 = GroupSeen.objects.filter(group=group3, user_id=self.user.id)
  2803. assert not r3.exists()
  2804. r4 = GroupSeen.objects.filter(group=group4, user_id=self.user.id)
  2805. assert not r4.exists()
  2806. @patch("sentry.issues.merge.uuid4")
  2807. @patch("sentry.issues.merge.merge_groups")
  2808. @patch("sentry.eventstream.backend")
  2809. def test_merge(self, mock_eventstream, merge_groups, mock_uuid4):
  2810. eventstream_state = object()
  2811. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  2812. mock_uuid4.return_value = self.get_mock_uuid()
  2813. group1 = self.create_group(times_seen=1)
  2814. group2 = self.create_group(times_seen=50)
  2815. group3 = self.create_group(times_seen=2)
  2816. self.create_group()
  2817. self.login_as(user=self.user)
  2818. response = self.get_success_response(
  2819. qs_params={"id": [group1.id, group2.id, group3.id]}, merge="1"
  2820. )
  2821. assert response.data["merge"]["parent"] == str(group2.id)
  2822. assert sorted(response.data["merge"]["children"]) == sorted(
  2823. [str(group1.id), str(group3.id)]
  2824. )
  2825. mock_eventstream.start_merge.assert_called_once_with(
  2826. group1.project_id, [group3.id, group1.id], group2.id
  2827. )
  2828. assert len(merge_groups.mock_calls) == 1
  2829. merge_groups.delay.assert_any_call(
  2830. from_object_ids=[group3.id, group1.id],
  2831. to_object_id=group2.id,
  2832. transaction_id="abc123",
  2833. eventstream_state=eventstream_state,
  2834. )
  2835. @patch("sentry.issues.merge.uuid4")
  2836. @patch("sentry.issues.merge.merge_groups")
  2837. @patch("sentry.eventstream.backend")
  2838. def test_merge_performance_issues(self, mock_eventstream, merge_groups, mock_uuid4):
  2839. eventstream_state = object()
  2840. mock_eventstream.start_merge = Mock(return_value=eventstream_state)
  2841. mock_uuid4.return_value = self.get_mock_uuid()
  2842. group1 = self.create_group(times_seen=1, type=PerformanceSlowDBQueryGroupType.type_id)
  2843. group2 = self.create_group(times_seen=50, type=PerformanceSlowDBQueryGroupType.type_id)
  2844. group3 = self.create_group(times_seen=2, type=PerformanceSlowDBQueryGroupType.type_id)
  2845. self.create_group()
  2846. self.login_as(user=self.user)
  2847. response = self.get_error_response(
  2848. qs_params={"id": [group1.id, group2.id, group3.id]}, merge="1"
  2849. )
  2850. assert response.status_code == 400, response.content
  2851. def test_assign(self):
  2852. group1 = self.create_group(is_public=True)
  2853. group2 = self.create_group(is_public=True)
  2854. user = self.user
  2855. self.login_as(user=user)
  2856. response = self.get_success_response(qs_params={"id": group1.id}, assignedTo=user.username)
  2857. assert response.data["assignedTo"]["id"] == str(user.id)
  2858. assert response.data["assignedTo"]["type"] == "user"
  2859. assert GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  2860. assert GroupHistory.objects.filter(
  2861. group=group1, status=GroupHistoryStatus.ASSIGNED
  2862. ).exists()
  2863. assert not GroupAssignee.objects.filter(group=group2, user_id=user.id).exists()
  2864. assert (
  2865. Activity.objects.filter(
  2866. group=group1, user_id=user.id, type=ActivityType.ASSIGNED.value
  2867. ).count()
  2868. == 1
  2869. )
  2870. assert GroupSubscription.objects.filter(
  2871. user_id=user.id, group=group1, is_active=True
  2872. ).exists()
  2873. response = self.get_success_response(qs_params={"id": group1.id}, assignedTo="")
  2874. assert response.data["assignedTo"] is None
  2875. assert not GroupAssignee.objects.filter(group=group1, user_id=user.id).exists()
  2876. assert GroupHistory.objects.filter(
  2877. group=group1, status=GroupHistoryStatus.UNASSIGNED
  2878. ).exists()
  2879. def test_assign_non_member(self):
  2880. group = self.create_group(is_public=True)
  2881. member = self.user
  2882. non_member = self.create_user("bar@example.com")
  2883. self.login_as(user=member)
  2884. response = self.get_response(qs_params={"id": group.id}, assignedTo=non_member.username)
  2885. assert not GroupHistory.objects.filter(
  2886. group=group, status=GroupHistoryStatus.ASSIGNED
  2887. ).exists()
  2888. assert response.status_code == 400, response.content
  2889. def test_assign_team(self):
  2890. self.login_as(user=self.user)
  2891. group = self.create_group()
  2892. other_member = self.create_user("bar@example.com")
  2893. team = self.create_team(
  2894. organization=group.project.organization, members=[self.user, other_member]
  2895. )
  2896. group.project.add_team(team)
  2897. assert not GroupHistory.objects.filter(
  2898. group=group, status=GroupHistoryStatus.ASSIGNED
  2899. ).exists()
  2900. response = self.get_success_response(
  2901. qs_params={"id": group.id}, assignedTo=f"team:{team.id}"
  2902. )
  2903. assert response.data["assignedTo"]["id"] == str(team.id)
  2904. assert response.data["assignedTo"]["type"] == "team"
  2905. assert GroupHistory.objects.filter(group=group, status=GroupHistoryStatus.ASSIGNED).exists()
  2906. assert GroupAssignee.objects.filter(group=group, team=team).exists()
  2907. assert Activity.objects.filter(group=group, type=ActivityType.ASSIGNED.value).count() == 1
  2908. assert GroupSubscription.objects.filter(group=group, is_active=True).count() == 2
  2909. response = self.get_success_response(qs_params={"id": group.id}, assignedTo="")
  2910. assert response.data["assignedTo"] is None
  2911. assert GroupHistory.objects.filter(
  2912. group=group, status=GroupHistoryStatus.UNASSIGNED
  2913. ).exists()
  2914. def test_discard(self):
  2915. group1 = self.create_group(is_public=True)
  2916. group2 = self.create_group(is_public=True)
  2917. group_hash = GroupHash.objects.create(hash="x" * 32, project=group1.project, group=group1)
  2918. user = self.user
  2919. self.login_as(user=user)
  2920. with self.tasks():
  2921. with self.feature("projects:discard-groups"):
  2922. response = self.get_response(qs_params={"id": group1.id}, discard=True)
  2923. assert response.status_code == 204
  2924. assert not Group.objects.filter(id=group1.id).exists()
  2925. assert Group.objects.filter(id=group2.id).exists()
  2926. assert GroupHash.objects.filter(id=group_hash.id).exists()
  2927. tombstone = GroupTombstone.objects.get(
  2928. id=GroupHash.objects.get(id=group_hash.id).group_tombstone_id
  2929. )
  2930. assert tombstone.message == group1.message
  2931. assert tombstone.culprit == group1.culprit
  2932. assert tombstone.project == group1.project
  2933. assert tombstone.data == group1.data
  2934. @override_settings(SENTRY_SELF_HOSTED=False)
  2935. def test_ratelimit(self):
  2936. self.login_as(user=self.user)
  2937. with freeze_time("2000-01-01"):
  2938. for i in range(5):
  2939. self.get_success_response()
  2940. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  2941. def test_set_inbox(self):
  2942. group1 = self.create_group()
  2943. group2 = self.create_group()
  2944. self.login_as(user=self.user)
  2945. response = self.get_success_response(qs_params={"id": [group1.id, group2.id]}, inbox="true")
  2946. assert response.data == {"inbox": True}
  2947. assert GroupInbox.objects.filter(group=group1).exists()
  2948. assert GroupInbox.objects.filter(group=group2).exists()
  2949. assert not GroupHistory.objects.filter(
  2950. group=group1, status=GroupHistoryStatus.REVIEWED
  2951. ).exists()
  2952. assert not GroupHistory.objects.filter(
  2953. group=group2, status=GroupHistoryStatus.REVIEWED
  2954. ).exists()
  2955. response = self.get_success_response(qs_params={"id": [group2.id]}, inbox="false")
  2956. assert response.data == {"inbox": False}
  2957. assert GroupInbox.objects.filter(group=group1).exists()
  2958. assert not GroupHistory.objects.filter(
  2959. group=group1, status=GroupHistoryStatus.REVIEWED
  2960. ).exists()
  2961. assert GroupHistory.objects.filter(
  2962. group=group2, status=GroupHistoryStatus.REVIEWED
  2963. ).exists()
  2964. assert not GroupInbox.objects.filter(group=group2).exists()
  2965. def test_set_resolved_inbox(self):
  2966. group1 = self.create_group()
  2967. group2 = self.create_group()
  2968. self.login_as(user=self.user)
  2969. response = self.get_success_response(
  2970. qs_params={"id": [group1.id, group2.id]}, status="resolved"
  2971. )
  2972. assert response.data["inbox"] is None
  2973. assert not GroupInbox.objects.filter(group=group1).exists()
  2974. assert not GroupInbox.objects.filter(group=group2).exists()
  2975. self.get_success_response(qs_params={"id": [group2.id]}, status="unresolved")
  2976. assert not GroupInbox.objects.filter(group=group1).exists()
  2977. assert not GroupInbox.objects.filter(group=group2).exists()
  2978. assert not GroupHistory.objects.filter(
  2979. group=group1, status=GroupHistoryStatus.UNRESOLVED
  2980. ).exists()
  2981. assert GroupHistory.objects.filter(
  2982. group=group2, status=GroupHistoryStatus.UNRESOLVED
  2983. ).exists()
  2984. @region_silo_test(stable=True)
  2985. class GroupDeleteTest(APITestCase, SnubaTestCase):
  2986. endpoint = "sentry-api-0-organization-group-index"
  2987. method = "delete"
  2988. def get_response(self, *args, **kwargs):
  2989. if not args:
  2990. org = self.project.organization.slug
  2991. else:
  2992. org = args[0]
  2993. return super().get_response(org, **kwargs)
  2994. @patch("sentry.eventstream.backend")
  2995. def test_delete_by_id(self, mock_eventstream):
  2996. eventstream_state = {"event_stream_state": uuid4()}
  2997. mock_eventstream.start_delete_groups = Mock(return_value=eventstream_state)
  2998. group1 = self.create_group(status=GroupStatus.RESOLVED)
  2999. group2 = self.create_group(status=GroupStatus.UNRESOLVED)
  3000. group3 = self.create_group(status=GroupStatus.IGNORED)
  3001. group4 = self.create_group(
  3002. project=self.create_project(slug="foo"),
  3003. status=GroupStatus.UNRESOLVED,
  3004. )
  3005. hashes = []
  3006. for g in group1, group2, group3, group4:
  3007. hash = uuid4().hex
  3008. hashes.append(hash)
  3009. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  3010. self.login_as(user=self.user)
  3011. with self.feature("organizations:global-views"):
  3012. response = self.get_response(
  3013. qs_params={"id": [group1.id, group2.id], "group4": group4.id}
  3014. )
  3015. mock_eventstream.start_delete_groups.assert_called_once_with(
  3016. group1.project_id, [group1.id, group2.id]
  3017. )
  3018. assert response.status_code == 204
  3019. assert Group.objects.get(id=group1.id).status == GroupStatus.PENDING_DELETION
  3020. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  3021. assert Group.objects.get(id=group2.id).status == GroupStatus.PENDING_DELETION
  3022. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  3023. assert Group.objects.get(id=group3.id).status != GroupStatus.PENDING_DELETION
  3024. assert GroupHash.objects.filter(group_id=group3.id).exists()
  3025. assert Group.objects.get(id=group4.id).status != GroupStatus.PENDING_DELETION
  3026. assert GroupHash.objects.filter(group_id=group4.id).exists()
  3027. Group.objects.filter(id__in=(group1.id, group2.id)).update(status=GroupStatus.UNRESOLVED)
  3028. with self.tasks():
  3029. with self.feature("organizations:global-views"):
  3030. response = self.get_response(
  3031. qs_params={"id": [group1.id, group2.id], "group4": group4.id}
  3032. )
  3033. # XXX(markus): Something is sending duplicated replacements to snuba --
  3034. # once from within tasks.deletions.groups and another time from
  3035. # sentry.deletions.defaults.groups
  3036. assert mock_eventstream.end_delete_groups.call_args_list == [
  3037. call(eventstream_state),
  3038. call(eventstream_state),
  3039. ]
  3040. assert response.status_code == 204
  3041. assert not Group.objects.filter(id=group1.id).exists()
  3042. assert not GroupHash.objects.filter(group_id=group1.id).exists()
  3043. assert not Group.objects.filter(id=group2.id).exists()
  3044. assert not GroupHash.objects.filter(group_id=group2.id).exists()
  3045. assert Group.objects.filter(id=group3.id).exists()
  3046. assert GroupHash.objects.filter(group_id=group3.id).exists()
  3047. assert Group.objects.filter(id=group4.id).exists()
  3048. assert GroupHash.objects.filter(group_id=group4.id).exists()
  3049. @patch("sentry.eventstream.backend")
  3050. def test_delete_performance_issue_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(
  3054. status=GroupStatus.RESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  3055. )
  3056. group2 = self.create_group(
  3057. status=GroupStatus.UNRESOLVED, type=PerformanceSlowDBQueryGroupType.type_id
  3058. )
  3059. hashes = []
  3060. for g in group1, group2:
  3061. hash = uuid4().hex
  3062. hashes.append(hash)
  3063. GroupHash.objects.create(project=g.project, hash=hash, group=g)
  3064. self.login_as(user=self.user)
  3065. with self.feature("organizations:global-views"):
  3066. response = self.get_response(qs_params={"id": [group1.id, group2.id]})
  3067. assert response.status_code == 400
  3068. assert Group.objects.filter(id=group1.id).exists()
  3069. assert GroupHash.objects.filter(group_id=group1.id).exists()
  3070. assert Group.objects.filter(id=group2.id).exists()
  3071. assert GroupHash.objects.filter(group_id=group2.id).exists()
  3072. def test_bulk_delete(self):
  3073. groups = []
  3074. for i in range(10, 41):
  3075. groups.append(
  3076. self.create_group(
  3077. project=self.project,
  3078. status=GroupStatus.RESOLVED,
  3079. )
  3080. )
  3081. hashes = []
  3082. for group in groups:
  3083. hash = uuid4().hex
  3084. hashes.append(hash)
  3085. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  3086. self.login_as(user=self.user)
  3087. # if query is '' it defaults to is:unresolved
  3088. response = self.get_response(qs_params={"query": ""})
  3089. assert response.status_code == 204
  3090. for group in groups:
  3091. assert Group.objects.get(id=group.id).status == GroupStatus.PENDING_DELETION
  3092. assert not GroupHash.objects.filter(group_id=group.id).exists()
  3093. Group.objects.filter(id__in=[group.id for group in groups]).update(
  3094. status=GroupStatus.UNRESOLVED
  3095. )
  3096. with self.tasks():
  3097. response = self.get_response(qs_params={"query": ""})
  3098. assert response.status_code == 204
  3099. for group in groups:
  3100. assert not Group.objects.filter(id=group.id).exists()
  3101. assert not GroupHash.objects.filter(group_id=group.id).exists()
  3102. @override_settings(SENTRY_SELF_HOSTED=False)
  3103. def test_ratelimit(self):
  3104. self.login_as(user=self.user)
  3105. with freeze_time("2000-01-01"):
  3106. for i in range(5):
  3107. self.get_success_response()
  3108. self.get_error_response(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
  3109. def test_bulk_delete_performance_issues(self):
  3110. groups = []
  3111. for i in range(10, 41):
  3112. groups.append(
  3113. self.create_group(
  3114. project=self.project,
  3115. status=GroupStatus.RESOLVED,
  3116. type=PerformanceSlowDBQueryGroupType.type_id,
  3117. )
  3118. )
  3119. hashes = []
  3120. for group in groups:
  3121. hash = uuid4().hex
  3122. hashes.append(hash)
  3123. GroupHash.objects.create(project=group.project, hash=hash, group=group)
  3124. self.login_as(user=self.user)
  3125. # if query is '' it defaults to is:unresolved
  3126. response = self.get_response(qs_params={"query": ""})
  3127. assert response.status_code == 400
  3128. for group in groups:
  3129. assert Group.objects.filter(id=group.id).exists()
  3130. assert GroupHash.objects.filter(group_id=group.id).exists()