test_organization_group_index.py 142 KB

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