test_organization_group_index.py 140 KB

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