test_organization_group_index.py 145 KB

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