test_organization_group_index.py 141 KB

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