test_organization_group_index.py 140 KB

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