test_organization_group_index.py 132 KB

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