test_organization_group_index.py 130 KB

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