test_organization_group_index.py 133 KB

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