test_pagure_lib.py 210 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. import pygit2
  14. import markdown
  15. from mock import patch, MagicMock, Mock
  16. sys.path.insert(
  17. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  18. )
  19. import pagure
  20. import pagure.lib.query
  21. import pagure.lib.model
  22. import tests
  23. class PagureLibtests_search_user(tests.Modeltests):
  24. """
  25. Test the pagure.lib search_user() method
  26. """
  27. maxDiff = None
  28. def test_search_user_all(self):
  29. """
  30. Test the method returns all the users for the given session
  31. """
  32. # Retrieve all users
  33. items = pagure.lib.query.search_user(self.session)
  34. self.assertEqual(2, len(items))
  35. self.assertEqual(2, items[0].id)
  36. self.assertEqual("foo", items[0].user)
  37. self.assertEqual("foo", items[0].username)
  38. self.assertEqual([], items[1].groups)
  39. self.assertEqual(1, items[1].id)
  40. self.assertEqual("pingou", items[1].user)
  41. self.assertEqual("pingou", items[1].username)
  42. self.assertEqual([], items[1].groups)
  43. def test_search_user_username(self):
  44. """
  45. Test the method returns the user for a given username
  46. """
  47. # Retrieve user by username
  48. item = pagure.lib.query.search_user(self.session, username="foo")
  49. self.assertEqual("foo", item.user)
  50. self.assertEqual("foo", item.username)
  51. self.assertEqual([], item.groups)
  52. item = pagure.lib.query.search_user(self.session, username="bar")
  53. self.assertEqual(None, item)
  54. def test_search_user_email(self):
  55. """
  56. Test the method returns a user for a given email address
  57. """
  58. # Retrieve user by email
  59. item = pagure.lib.query.search_user(self.session, email="foo@foo.com")
  60. self.assertEqual(None, item)
  61. item = pagure.lib.query.search_user(self.session, email="foo@bar.com")
  62. self.assertEqual("foo", item.user)
  63. self.assertEqual("foo", item.username)
  64. self.assertEqual([], item.groups)
  65. self.assertEqual(
  66. ["foo@bar.com"], [email.email for email in item.emails]
  67. )
  68. item = pagure.lib.query.search_user(
  69. self.session, email="foo@pingou.com"
  70. )
  71. self.assertEqual("pingou", item.user)
  72. self.assertEqual(
  73. sorted(["bar@pingou.com", "foo@pingou.com"]),
  74. sorted([email.email for email in item.emails]),
  75. )
  76. def test_search_user_token(self):
  77. """
  78. Test the method returns a user for a given token
  79. """
  80. # Retrieve user by token
  81. item = pagure.lib.query.search_user(self.session, token="aaa")
  82. self.assertEqual(None, item)
  83. item = pagure.lib.model.User(
  84. user="pingou2",
  85. fullname="PY C",
  86. token="aaabbb",
  87. default_email="bar@pingou.com",
  88. )
  89. self.session.add(item)
  90. self.session.commit()
  91. item = pagure.lib.query.search_user(self.session, token="aaabbb")
  92. self.assertEqual("pingou2", item.user)
  93. self.assertEqual("PY C", item.fullname)
  94. def test_search_user_pattern(self):
  95. """
  96. Test the method returns a user for a given pattern
  97. """
  98. # Retrieve user by pattern
  99. item = pagure.lib.query.search_user(self.session, pattern="a*")
  100. self.assertEqual([], item)
  101. item = pagure.lib.model.User(
  102. user="pingou2",
  103. fullname="PY C",
  104. token="aaabbb",
  105. default_email="bar@pingou.com",
  106. )
  107. self.session.add(item)
  108. self.session.commit()
  109. items = pagure.lib.query.search_user(self.session, pattern="p*")
  110. self.assertEqual(2, len(items))
  111. self.assertEqual(1, items[0].id)
  112. self.assertEqual("pingou", items[0].user)
  113. self.assertEqual("pingou", items[0].username)
  114. self.assertEqual([], items[0].groups)
  115. self.assertEqual(
  116. sorted(["bar@pingou.com", "foo@pingou.com"]),
  117. sorted([email.email for email in items[0].emails]),
  118. )
  119. self.assertEqual(3, items[1].id)
  120. self.assertEqual("pingou2", items[1].user)
  121. self.assertEqual("pingou2", items[1].username)
  122. self.assertEqual([], items[1].groups)
  123. self.assertEqual([], [email.email for email in items[1].emails])
  124. class PagureLibtests_search_projects(tests.Modeltests):
  125. """
  126. Test the pagure.lib search_projects() method
  127. """
  128. def setUp(self):
  129. super(PagureLibtests_search_projects, self).setUp()
  130. tests.create_projects(self.session)
  131. def test_search_projects_all(self):
  132. """
  133. Test the method returns all the projects for the given session
  134. """
  135. projects = pagure.lib.query.search_projects(self.session)
  136. self.assertEqual(len(projects), 3)
  137. self.assertEqual(projects[0].id, 1)
  138. self.assertEqual(projects[1].id, 2)
  139. def test_search_projects_username(self):
  140. """
  141. Test the method returns all the projects for the given username
  142. """
  143. projects = pagure.lib.query.search_projects(
  144. self.session, username="foo"
  145. )
  146. self.assertEqual(len(projects), 0)
  147. projects = pagure.lib.query.search_projects(
  148. self.session, username="pingou"
  149. )
  150. self.assertEqual(len(projects), 3)
  151. self.assertEqual(projects[0].id, 1)
  152. self.assertEqual(projects[1].id, 2)
  153. def test_search_projects_start(self):
  154. """
  155. Test the method returns all the projects for the given start
  156. """
  157. projects = pagure.lib.query.search_projects(self.session, start=1)
  158. self.assertEqual(len(projects), 2)
  159. self.assertEqual(projects[0].id, 2)
  160. def test_search_projects_limit(self):
  161. """
  162. Test the method returns all the projects for the given limit
  163. """
  164. projects = pagure.lib.query.search_projects(self.session, limit=1)
  165. self.assertEqual(len(projects), 1)
  166. self.assertEqual(projects[0].id, 1)
  167. def test_search_projects_count(self):
  168. """
  169. Test the method returns the count of the projects
  170. """
  171. projects = pagure.lib.query.search_projects(self.session, count=True)
  172. self.assertEqual(projects, 3)
  173. def test_search_projects_commit_access(self):
  174. """
  175. Test the method returns the project of user with only commit access
  176. """
  177. # Also check if the project shows up if a user doesn't
  178. # have admin access in the project
  179. # Check with commit access first
  180. project = pagure.lib.query.get_authorized_project(
  181. self.session, project_name="test"
  182. )
  183. msg = pagure.lib.query.add_user_to_project(
  184. self.session,
  185. project=project,
  186. new_user="foo",
  187. user="pingou",
  188. access="commit",
  189. )
  190. self.assertEqual(msg, "User added")
  191. self.session.commit()
  192. projects = pagure.lib.query.search_projects(
  193. self.session, username="foo"
  194. )
  195. self.assertEqual(len(projects), 1)
  196. def test_search_projects_ticket_access(self):
  197. """
  198. Test the method does not returns the project of user with only ticket access
  199. """
  200. # Now check with only ticket access
  201. project = pagure.lib.query.get_authorized_project(
  202. self.session, project_name="test"
  203. )
  204. msg = pagure.lib.query.add_user_to_project(
  205. self.session,
  206. project=project,
  207. new_user="foo",
  208. user="pingou",
  209. access="ticket",
  210. )
  211. self.assertEqual(msg, "User added")
  212. self.session.commit()
  213. projects = pagure.lib.query.search_projects(
  214. self.session, username="foo"
  215. )
  216. self.assertEqual(len(projects), 0)
  217. def test_search_project_forked(self):
  218. """
  219. Test the search_project for forked projects in pagure.lib.query.
  220. """
  221. # Create two forked repo
  222. item = pagure.lib.model.Project(
  223. user_id=2, # foo
  224. name="test",
  225. description="test project #1",
  226. is_fork=True,
  227. parent_id=1,
  228. hook_token="aaabbbttt",
  229. )
  230. self.session.add(item)
  231. item = pagure.lib.model.Project(
  232. user_id=2, # foo
  233. name="test2",
  234. description="test project #2",
  235. is_fork=True,
  236. parent_id=2,
  237. hook_token="aaabbbuuu",
  238. )
  239. self.session.add(item)
  240. # Since we have two forks, let's search them
  241. projects = pagure.lib.query.search_projects(self.session, fork=True)
  242. self.assertEqual(len(projects), 2)
  243. projects = pagure.lib.query.search_projects(self.session, fork=False)
  244. self.assertEqual(len(projects), 3)
  245. def test_search_projects_private(self):
  246. """
  247. Test the method for private projects
  248. """
  249. item = pagure.lib.model.Project(
  250. user_id=1, # pingou
  251. name="private_test",
  252. description="Private test project #1",
  253. hook_token="aaabbbcccpp",
  254. private=True,
  255. )
  256. self.session.add(item)
  257. self.session.commit()
  258. # non authenticated
  259. projects = pagure.lib.query.search_projects(self.session)
  260. self.assertEqual(len(projects), 3)
  261. self.assertEqual(
  262. [p.path for p in projects],
  263. ["test.git", "test2.git", "somenamespace/test3.git"],
  264. )
  265. # non authenticated
  266. projects = pagure.lib.query.search_projects(
  267. self.session, username="pingou"
  268. )
  269. self.assertEqual(len(projects), 3)
  270. self.assertEqual(
  271. [p.path for p in projects],
  272. ["test.git", "test2.git", "somenamespace/test3.git"],
  273. )
  274. # authenticated as pingou
  275. projects = pagure.lib.query.search_projects(
  276. self.session, username="pingou", private="pingou"
  277. )
  278. self.assertEqual(len(projects), 4)
  279. self.assertEqual(
  280. [p.path for p in projects],
  281. [
  282. "private_test.git",
  283. "test.git",
  284. "test2.git",
  285. "somenamespace/test3.git",
  286. ],
  287. )
  288. # authenticated as foo
  289. projects = pagure.lib.query.search_projects(
  290. self.session, username="pingou", private="foo"
  291. )
  292. self.assertEqual(len(projects), 3)
  293. self.assertEqual(
  294. [p.path for p in projects],
  295. ["test.git", "test2.git", "somenamespace/test3.git"],
  296. )
  297. def test_search_projects_tags(self):
  298. """
  299. Test the method returns all the projects for the given tags
  300. """
  301. # Add tags to the project
  302. project = pagure.lib.query._get_project(self.session, "test")
  303. tag = pagure.lib.model.Tag(tag="fedora")
  304. self.session.add(tag)
  305. self.session.commit()
  306. tp = pagure.lib.model.TagProject(project_id=project.id, tag="fedora")
  307. self.session.add(tp)
  308. self.session.commit()
  309. projects = pagure.lib.query.search_projects(
  310. self.session, tags="fedora"
  311. )
  312. self.assertEqual(len(projects), 1)
  313. self.assertEqual(projects[0].path, "test.git")
  314. def test_search_projects_pattern(self):
  315. """
  316. Test the method returns all the projects for the given pattern
  317. """
  318. projects = pagure.lib.query.search_projects(
  319. self.session, pattern="test*"
  320. )
  321. self.assertEqual(len(projects), 3)
  322. self.assertEqual(
  323. [p.path for p in projects],
  324. ["test.git", "test2.git", "somenamespace/test3.git"],
  325. )
  326. def test_search_projects_sort(self):
  327. """
  328. Test the method returns all the projects sorted by lastest and oldest
  329. """
  330. projects = pagure.lib.query.search_projects(
  331. self.session, pattern="*", sort="latest"
  332. )
  333. self.assertEqual(len(projects), 3)
  334. self.assertEqual(
  335. [p.path for p in projects],
  336. ["somenamespace/test3.git", "test2.git", "test.git"],
  337. )
  338. projects = pagure.lib.query.search_projects(
  339. self.session, pattern="*", sort="oldest"
  340. )
  341. self.assertEqual(len(projects), 3)
  342. self.assertEqual(
  343. [p.path for p in projects],
  344. ["test.git", "test2.git", "somenamespace/test3.git"],
  345. )
  346. class PagureLibtests(tests.Modeltests):
  347. """ Tests for pagure.lib """
  348. maxDiff = None
  349. def test_get_repotypes(self):
  350. """ Test the get_repotypes function of pagure.lib.query. """
  351. with patch.dict(
  352. pagure.config.config,
  353. {"ENABLE_TICKETS": False, "ENABLE_DOCS": False},
  354. ):
  355. self.assertEqual(
  356. set(("main", "requests")),
  357. set(pagure.lib.query.get_repotypes()),
  358. )
  359. with patch.dict(
  360. pagure.config.config,
  361. {"ENABLE_TICKETS": True, "ENABLE_DOCS": False},
  362. ):
  363. self.assertEqual(
  364. set(("main", "requests", "tickets")),
  365. set(pagure.lib.query.get_repotypes()),
  366. )
  367. with patch.dict(
  368. pagure.config.config,
  369. {"ENABLE_TICKETS": False, "ENABLE_DOCS": True},
  370. ):
  371. self.assertEqual(
  372. set(("main", "requests", "docs")),
  373. set(pagure.lib.query.get_repotypes()),
  374. )
  375. with patch.dict(
  376. pagure.config.config, {"ENABLE_TICKETS": True, "ENABLE_DOCS": True}
  377. ):
  378. self.assertEqual(
  379. set(("main", "requests", "tickets", "docs")),
  380. set(pagure.lib.query.get_repotypes()),
  381. )
  382. def test_get_next_id(self):
  383. """ Test the get_next_id function of pagure.lib.query. """
  384. tests.create_projects(self.session)
  385. self.assertEqual(1, pagure.lib.query.get_next_id(self.session, 1))
  386. @patch("pagure.lib.git.update_git")
  387. @patch("pagure.lib.notify.send_email")
  388. def test_new_issue(self, p_send_email, p_ugt):
  389. """ Test the new_issue of pagure.lib.query. """
  390. p_send_email.return_value = True
  391. p_ugt.return_value = True
  392. tests.create_projects(self.session)
  393. repo = pagure.lib.query._get_project(self.session, "test")
  394. # Set some priorities to the project
  395. repo.priorities = {"1": "High", "2": "Normal"}
  396. self.session.add(repo)
  397. self.session.commit()
  398. # Before
  399. issues = pagure.lib.query.search_issues(self.session, repo)
  400. self.assertEqual(len(issues), 0)
  401. self.assertEqual(repo.open_tickets, 0)
  402. self.assertEqual(repo.open_tickets_public, 0)
  403. # See where it fails
  404. self.assertRaises(
  405. pagure.exceptions.PagureException,
  406. pagure.lib.query.new_issue,
  407. session=self.session,
  408. repo=repo,
  409. title="Test issue",
  410. content="We should work on this",
  411. user="blah",
  412. )
  413. # Fails since we're trying to give a non-existant priority
  414. self.assertRaises(
  415. pagure.exceptions.PagureException,
  416. pagure.lib.query.new_issue,
  417. session=self.session,
  418. repo=repo,
  419. title="Test issue",
  420. content="We should work on this",
  421. user="pingou",
  422. priority=0,
  423. )
  424. # Add an extra user to project `foo`
  425. repo = pagure.lib.query._get_project(self.session, "test")
  426. msg = pagure.lib.query.add_user_to_project(
  427. session=self.session, project=repo, new_user="foo", user="pingou"
  428. )
  429. self.session.commit()
  430. self.assertEqual(msg, "User added")
  431. # Try adding again this extra user to project `foo`
  432. self.assertRaises(
  433. pagure.exceptions.PagureException,
  434. pagure.lib.query.add_user_to_project,
  435. session=self.session,
  436. project=repo,
  437. new_user="foo",
  438. user="pingou",
  439. )
  440. self.session.commit()
  441. self.assertEqual(msg, "User added")
  442. # Create issues to play with
  443. msg = pagure.lib.query.new_issue(
  444. session=self.session,
  445. repo=repo,
  446. title="Test issue",
  447. content="We should work on this",
  448. user="pingou",
  449. )
  450. self.session.commit()
  451. self.assertEqual(msg.title, "Test issue")
  452. self.assertEqual(repo.open_tickets, 1)
  453. self.assertEqual(repo.open_tickets_public, 1)
  454. msg = pagure.lib.query.new_issue(
  455. session=self.session,
  456. repo=repo,
  457. title="Test issue #2",
  458. content="We should work on this for the second time",
  459. user="foo",
  460. status="Open",
  461. )
  462. self.session.commit()
  463. self.assertEqual(msg.title, "Test issue #2")
  464. self.assertEqual(repo.open_tickets, 2)
  465. self.assertEqual(repo.open_tickets_public, 2)
  466. # After
  467. issues = pagure.lib.query.search_issues(self.session, repo)
  468. self.assertEqual(len(issues), 2)
  469. @patch("pagure.lib.git.update_git")
  470. @patch("pagure.lib.notify.send_email")
  471. def test_edit_issue(self, p_send_email, p_ugt):
  472. """ Test the edit_issue of pagure.lib.query. """
  473. p_send_email.return_value = True
  474. p_ugt.return_value = True
  475. self.test_new_issue()
  476. repo = pagure.lib.query._get_project(self.session, "test")
  477. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  478. self.assertEqual(repo.open_tickets, 2)
  479. self.assertEqual(repo.open_tickets_public, 2)
  480. # Edit the issue
  481. msg = pagure.lib.query.edit_issue(
  482. session=self.session, issue=issue, user="pingou"
  483. )
  484. self.session.commit()
  485. self.assertEqual(msg, None)
  486. msg = pagure.lib.query.edit_issue(
  487. session=self.session,
  488. issue=issue,
  489. user="pingou",
  490. title="Test issue #2",
  491. content="We should work on this for the second time",
  492. status="Open",
  493. )
  494. self.session.commit()
  495. self.assertEqual(msg, None)
  496. msg = pagure.lib.query.edit_issue(
  497. session=self.session,
  498. issue=issue,
  499. user="pingou",
  500. title="Foo issue #2",
  501. content="We should work on this period",
  502. status="Closed",
  503. close_status="Invalid",
  504. private=True,
  505. )
  506. self.session.commit()
  507. self.assertEqual(
  508. msg,
  509. [
  510. "Issue status updated to: Closed (was: Open)",
  511. "Issue close_status updated to: Invalid",
  512. "Issue private status set to: True",
  513. ],
  514. )
  515. msg = pagure.lib.query.edit_issue(
  516. session=self.session,
  517. issue=issue,
  518. user="pingou",
  519. title="Foo issue #2",
  520. content="Fixed!",
  521. status="Closed",
  522. close_status="Fixed",
  523. private=False,
  524. )
  525. self.session.commit()
  526. self.assertEqual(
  527. msg,
  528. [
  529. "Issue close_status updated to: Fixed (was: Invalid)",
  530. "Issue private status set to: False (was: True)",
  531. ],
  532. )
  533. repo = pagure.lib.query._get_project(self.session, "test")
  534. self.assertEqual(repo.open_tickets, 1)
  535. self.assertEqual(repo.open_tickets_public, 1)
  536. self.assertEqual(repo.issues[1].status, "Closed")
  537. self.assertEqual(repo.issues[1].close_status, "Fixed")
  538. # Edit the status: re-open the ticket
  539. msg = pagure.lib.query.edit_issue(
  540. session=self.session,
  541. issue=issue,
  542. user="pingou",
  543. status="Open",
  544. private=True,
  545. )
  546. self.session.commit()
  547. self.assertEqual(
  548. msg,
  549. [
  550. "Issue status updated to: Open (was: Closed)",
  551. "Issue private status set to: True",
  552. ],
  553. )
  554. repo = pagure.lib.query._get_project(self.session, "test")
  555. for issue in repo.issues:
  556. self.assertEqual(issue.status, "Open")
  557. self.assertEqual(issue.close_status, None)
  558. # 2 open but one of them is private
  559. self.assertEqual(repo.open_tickets, 2)
  560. self.assertEqual(repo.open_tickets_public, 1)
  561. # Edit the status: re-close the ticket
  562. msg = pagure.lib.query.edit_issue(
  563. session=self.session,
  564. issue=issue,
  565. user="pingou",
  566. status="Closed",
  567. close_status="Invalid",
  568. private=True,
  569. )
  570. self.session.commit()
  571. self.assertEqual(
  572. msg,
  573. [
  574. "Issue status updated to: Closed (was: Open)",
  575. "Issue close_status updated to: Invalid",
  576. ],
  577. )
  578. repo = pagure.lib.query._get_project(self.session, "test")
  579. self.assertEqual(repo.open_tickets, 1)
  580. self.assertEqual(repo.open_tickets_public, 1)
  581. self.assertEqual(repo.issues[1].status, "Closed")
  582. self.assertEqual(repo.issues[1].close_status, "Invalid")
  583. @patch("pagure.lib.git.update_git")
  584. @patch("pagure.lib.notify.send_email")
  585. def test_edit_issue_close_status(self, p_send_email, p_ugt):
  586. """ Test the edit_issue of pagure.lib.query. """
  587. p_send_email.return_value = True
  588. p_ugt.return_value = True
  589. self.test_new_issue()
  590. repo = pagure.lib.query._get_project(self.session, "test")
  591. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  592. self.assertEqual(issue.status, "Open")
  593. self.assertEqual(issue.close_status, None)
  594. repo = pagure.lib.query._get_project(self.session, "test")
  595. self.assertEqual(repo.open_tickets, 2)
  596. self.assertEqual(repo.open_tickets_public, 2)
  597. # Edit the issue, providing just a close_status should also close
  598. # the ticket
  599. msg = pagure.lib.query.edit_issue(
  600. session=self.session,
  601. issue=issue,
  602. user="pingou",
  603. close_status="Fixed",
  604. )
  605. self.session.commit()
  606. self.assertEqual(msg, ["Issue close_status updated to: Fixed"])
  607. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  608. self.assertEqual(issue.status, "Closed")
  609. self.assertEqual(issue.close_status, "Fixed")
  610. repo = pagure.lib.query._get_project(self.session, "test")
  611. self.assertEqual(repo.open_tickets, 1)
  612. self.assertEqual(repo.open_tickets_public, 1)
  613. # Edit the issue, editing the status to open, should reset the
  614. # close_status
  615. msg = pagure.lib.query.edit_issue(
  616. session=self.session, issue=issue, user="pingou", status="Open"
  617. )
  618. self.session.commit()
  619. self.assertEqual(msg, ["Issue status updated to: Open (was: Closed)"])
  620. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  621. self.assertEqual(issue.status, "Open")
  622. self.assertEqual(issue.close_status, None)
  623. repo = pagure.lib.query._get_project(self.session, "test")
  624. self.assertEqual(repo.open_tickets, 2)
  625. self.assertEqual(repo.open_tickets_public, 2)
  626. @patch("pagure.lib.git.update_git")
  627. @patch("pagure.lib.notify.send_email")
  628. def test_edit_issue_priority(self, p_send_email, p_ugt):
  629. """ Test the edit_issue of pagure.lib when changing the priority.
  630. """
  631. p_send_email.return_value = True
  632. p_ugt.return_value = True
  633. self.test_new_issue()
  634. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  635. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  636. # Set some priorities to the repo
  637. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  638. repo.priorities = {"1": "High", "2": "Normal"}
  639. self.session.add(repo)
  640. self.session.commit()
  641. self.assertEqual(repo.open_tickets, 2)
  642. self.assertEqual(repo.open_tickets_public, 2)
  643. # Edit the issue -- Wrong priority value: No changes
  644. msg = pagure.lib.query.edit_issue(
  645. session=self.session, issue=issue, user="pingou", priority=3
  646. )
  647. self.session.commit()
  648. self.assertEqual(msg, None)
  649. # Edit the issue -- Good priority
  650. msg = pagure.lib.query.edit_issue(
  651. session=self.session, issue=issue, user="pingou", priority=2
  652. )
  653. self.session.commit()
  654. self.assertEqual(msg, ["Issue priority set to: Normal"])
  655. # Edit the issue -- Update priority
  656. msg = pagure.lib.query.edit_issue(
  657. session=self.session, issue=issue, user="pingou", priority=1
  658. )
  659. self.session.commit()
  660. self.assertEqual(msg, ["Issue priority set to: High (was: Normal)"])
  661. @patch("pagure.lib.git.update_git")
  662. @patch("pagure.lib.notify.send_email")
  663. def test_edit_issue_depending(self, p_send_email, p_ugt):
  664. """ Test the edit_issue of pagure.lib when the issue depends on
  665. another.
  666. """
  667. p_send_email.return_value = True
  668. p_ugt.return_value = True
  669. tests.create_projects(self.session)
  670. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  671. # Create 3 issues
  672. msg = pagure.lib.query.new_issue(
  673. session=self.session,
  674. repo=repo,
  675. title="Test issue #1",
  676. content="We should work on this for the second time",
  677. user="foo",
  678. status="Open",
  679. )
  680. self.session.commit()
  681. self.assertEqual(msg.title, "Test issue #1")
  682. msg = pagure.lib.query.new_issue(
  683. session=self.session,
  684. repo=repo,
  685. title="Test issue #2",
  686. content="We should work on this for the second time",
  687. user="foo",
  688. status="Open",
  689. )
  690. self.session.commit()
  691. self.assertEqual(msg.title, "Test issue #2")
  692. msg = pagure.lib.query.new_issue(
  693. session=self.session,
  694. repo=repo,
  695. title="Test issue #3",
  696. content="We should work on this for the second time",
  697. user="foo",
  698. status="Open",
  699. )
  700. self.session.commit()
  701. self.assertEqual(msg.title, "Test issue #3")
  702. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  703. self.assertEqual(repo.open_tickets, 3)
  704. self.assertEqual(repo.open_tickets_public, 3)
  705. # Make issue #2 blocking on issue #1
  706. msgs = pagure.lib.query.update_blocked_issue(
  707. self.session, repo, issue, blocks=["1"], username="pingou"
  708. )
  709. self.assertEqual(msgs, ["Issue marked as blocking: #1"])
  710. # Make issue #2 depend on issue #3
  711. msgs = pagure.lib.query.update_dependency_issue(
  712. self.session, repo, issue, depends=["3"], username="pingou"
  713. )
  714. self.assertEqual(msgs, ["Issue marked as depending on: #3"])
  715. # Edit the issue #3
  716. issue = pagure.lib.query.search_issues(self.session, repo, issueid=3)
  717. msg = pagure.lib.query.edit_issue(
  718. session=self.session, issue=issue, user="pingou"
  719. )
  720. self.session.commit()
  721. self.assertEqual(msg, None)
  722. msg = pagure.lib.query.edit_issue(
  723. session=self.session,
  724. issue=issue,
  725. user="pingou",
  726. title="Foo issue #2",
  727. content="We should work on this period",
  728. status="Closed",
  729. close_status="Invalid",
  730. private=True,
  731. )
  732. self.session.commit()
  733. self.assertEqual(
  734. msg,
  735. [
  736. "Issue status updated to: Closed (was: Open)",
  737. "Issue close_status updated to: Invalid",
  738. "Issue private status set to: True",
  739. ],
  740. )
  741. self.assertEqual(repo.open_tickets, 2)
  742. self.assertEqual(repo.open_tickets_public, 2)
  743. @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
  744. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  745. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  746. def test_add_issue_dependency(self):
  747. """ Test the add_issue_dependency of pagure.lib.query. """
  748. self.test_new_issue()
  749. repo = pagure.lib.query._get_project(self.session, "test")
  750. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  751. issue_blocked = pagure.lib.query.search_issues(
  752. self.session, repo, issueid=2
  753. )
  754. # Before
  755. self.assertEqual(issue.parents, [])
  756. self.assertEqual(issue.children, [])
  757. self.assertEqual(issue_blocked.parents, [])
  758. self.assertEqual(issue_blocked.children, [])
  759. self.assertRaises(
  760. pagure.exceptions.PagureException,
  761. pagure.lib.query.add_issue_dependency,
  762. session=self.session,
  763. issue=issue,
  764. issue_blocked=issue,
  765. user="pingou",
  766. )
  767. msg = pagure.lib.query.add_issue_dependency(
  768. session=self.session,
  769. issue=issue,
  770. issue_blocked=issue_blocked,
  771. user="pingou",
  772. )
  773. self.session.commit()
  774. self.assertEqual(msg, "Issue marked as depending on: #2")
  775. # After
  776. self.assertEqual(len(issue.parents), 0)
  777. self.assertEqual(issue.parents, [])
  778. self.assertEqual(len(issue.children), 1)
  779. self.assertEqual(issue.children[0].id, 2)
  780. self.assertEqual(issue.depending_text, [])
  781. self.assertEqual(issue.blocking_text, [2])
  782. self.assertEqual(len(issue_blocked.children), 0)
  783. self.assertEqual(issue_blocked.children, [])
  784. self.assertEqual(len(issue_blocked.parents), 1)
  785. self.assertEqual(issue_blocked.parents[0].id, 1)
  786. self.assertEqual(issue_blocked.depending_text, [1])
  787. self.assertEqual(issue_blocked.blocking_text, [])
  788. @patch("pagure.lib.query.REDIS")
  789. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  790. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  791. def test_edit_comment(self, mock_redis):
  792. """ Test the edit_issue of pagure.lib.query. """
  793. mock_redis.return_value = True
  794. self.test_add_issue_comment()
  795. repo = pagure.lib.query._get_project(self.session, "test")
  796. self.assertEqual(repo.open_tickets, 2)
  797. self.assertEqual(repo.open_tickets_public, 2)
  798. self.assertEqual(mock_redis.publish.call_count, 0)
  799. # Before
  800. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  801. self.assertEqual(len(issue.comments), 1)
  802. self.assertEqual(issue.comments[0].comment, "Hey look a comment!")
  803. # Edit one of the
  804. msg = pagure.lib.query.edit_comment(
  805. session=self.session,
  806. parent=issue,
  807. comment=issue.comments[0],
  808. user="pingou",
  809. updated_comment="Edited comment",
  810. )
  811. self.session.commit()
  812. self.assertEqual(msg, "Comment updated")
  813. self.assertEqual(mock_redis.publish.call_count, 1)
  814. # After
  815. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  816. self.assertEqual(len(issue.comments), 1)
  817. self.assertEqual(issue.comments[0].comment, "Edited comment")
  818. @patch("pagure.lib.query.REDIS")
  819. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  820. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  821. def test_edit_comment_private(self, mock_redis):
  822. """ Test the edit_issue of pagure.lib.query. """
  823. self.test_add_issue_comment_private()
  824. repo = pagure.lib.query._get_project(self.session, "test")
  825. self.assertEqual(repo.open_tickets, 1)
  826. self.assertEqual(repo.open_tickets_public, 0)
  827. self.assertEqual(mock_redis.publish.call_count, 0)
  828. # Before
  829. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  830. self.assertEqual(len(issue.comments), 1)
  831. self.assertEqual(issue.comments[0].comment, "Hey look a comment!")
  832. # Edit one of the
  833. msg = pagure.lib.query.edit_comment(
  834. session=self.session,
  835. parent=issue,
  836. comment=issue.comments[0],
  837. user="pingou",
  838. updated_comment="Edited comment",
  839. )
  840. self.session.commit()
  841. self.assertEqual(msg, "Comment updated")
  842. self.assertEqual(mock_redis.publish.call_count, 1)
  843. # After
  844. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  845. self.assertEqual(len(issue.comments), 1)
  846. self.assertEqual(issue.comments[0].comment, "Edited comment")
  847. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  848. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  849. @patch("pagure.lib.query.REDIS")
  850. def test_add_tag_obj(self, mock_redis):
  851. """ Test the add_tag_obj of pagure.lib.query. """
  852. mock_redis.return_value = True
  853. self.test_edit_issue()
  854. repo = pagure.lib.query._get_project(self.session, "test")
  855. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  856. self.assertFalse(issue.private)
  857. self.assertFalse(issue.project.private)
  858. args = mock_redis.publish.call_args_list
  859. self.assertEqual(len(args), 4)
  860. # Add a tag to the issue
  861. msg = pagure.lib.query.add_tag_obj(
  862. session=self.session, obj=issue, tags="tag1", user="pingou"
  863. )
  864. self.session.commit()
  865. self.assertEqual(msg, "Issue tagged with: tag1")
  866. args = mock_redis.publish.call_args_list
  867. self.assertEqual(len(args), 5)
  868. # Get the arguments of the last call and get the second of these
  869. # arguments (the first one changing for each test run)
  870. self.assertJSONEqual(
  871. args[-1:][0][0][1],
  872. '{"added_tags_color": ["DeepSkyBlue"], "added_tags": ["tag1"]}',
  873. )
  874. # Try a second time
  875. msg = pagure.lib.query.add_tag_obj(
  876. session=self.session, obj=issue, tags="tag1", user="pingou"
  877. )
  878. self.session.commit()
  879. self.assertEqual(msg, "Nothing to add")
  880. issues = pagure.lib.query.search_issues(
  881. self.session, repo, tags="tag1"
  882. )
  883. self.assertEqual(len(issues), 1)
  884. self.assertEqual(issues[0].id, 1)
  885. self.assertEqual(issues[0].project_id, 1)
  886. self.assertEqual(issues[0].status, "Open")
  887. self.assertEqual([tag.tag for tag in issues[0].tags], ["tag1"])
  888. @patch("pagure.lib.git.update_git")
  889. @patch("pagure.lib.notify.send_email")
  890. def test_remove_tags(self, p_send_email, p_ugt):
  891. """ Test the remove_tags of pagure.lib.query. """
  892. p_send_email.return_value = True
  893. p_ugt.return_value = True
  894. self.test_add_tag_obj()
  895. repo = pagure.lib.query._get_project(self.session, "test")
  896. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  897. self.assertRaises(
  898. pagure.exceptions.PagureException,
  899. pagure.lib.query.remove_tags,
  900. session=self.session,
  901. project=repo,
  902. tags="foo",
  903. user="pingou",
  904. )
  905. msgs = pagure.lib.query.remove_tags(
  906. session=self.session, project=repo, tags="tag1", user="pingou"
  907. )
  908. self.assertEqual(msgs, ["Tag: tag1 has been deleted"])
  909. @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
  910. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  911. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  912. def test_remove_tags_obj(self):
  913. """ Test the remove_tags_obj of pagure.lib.query. """
  914. self.test_add_tag_obj()
  915. repo = pagure.lib.query._get_project(self.session, "test")
  916. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  917. msgs = pagure.lib.query.remove_tags_obj(
  918. session=self.session, obj=issue, tags="tag1", user="pingou"
  919. )
  920. self.assertEqual(msgs, "Issue **un**tagged with: tag1")
  921. @patch("pagure.lib.git.update_git")
  922. @patch("pagure.lib.notify.send_email")
  923. def test_remove_tags_obj_from_project(self, p_send_email, p_ugt):
  924. """ Test the remove_tags_obj of pagure.lib from a project. """
  925. p_send_email.return_value = True
  926. p_ugt.return_value = True
  927. tests.create_projects(self.session)
  928. # Add a tag to the project
  929. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  930. msg = pagure.lib.query.add_tag_obj(
  931. self.session, repo, tags=["pagure", "test"], user="pingou"
  932. )
  933. self.assertEqual(msg, "Project tagged with: pagure, test")
  934. self.session.commit()
  935. # Check the tags
  936. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  937. self.assertEqual(repo.tags_text, ["pagure", "test"])
  938. # Remove one of the the tag
  939. msgs = pagure.lib.query.remove_tags_obj(
  940. session=self.session, obj=repo, tags="test", user="pingou"
  941. )
  942. self.assertEqual(msgs, "Project **un**tagged with: test")
  943. self.session.commit()
  944. # Check the tags
  945. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  946. self.assertEqual(repo.tags_text, ["pagure"])
  947. @patch("pagure.lib.git.update_git")
  948. @patch("pagure.lib.notify.send_email")
  949. def test_edit_issue_tags(self, p_send_email, p_ugt):
  950. """ Test the edit_issue_tags of pagure.lib.query. """
  951. p_send_email.return_value = True
  952. p_ugt.return_value = True
  953. self.test_add_tag_obj()
  954. repo = pagure.lib.query._get_project(self.session, "test")
  955. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  956. self.assertRaises(
  957. pagure.exceptions.PagureException,
  958. pagure.lib.query.edit_issue_tags,
  959. session=self.session,
  960. project=repo,
  961. old_tag="foo",
  962. new_tag="bar",
  963. new_tag_description="lorem ipsum",
  964. new_tag_color="black",
  965. user="pingou",
  966. )
  967. self.assertRaises(
  968. pagure.exceptions.PagureException,
  969. pagure.lib.query.edit_issue_tags,
  970. session=self.session,
  971. project=repo,
  972. old_tag=None,
  973. new_tag="bar",
  974. new_tag_description="lorem ipsum",
  975. new_tag_color="black",
  976. user="pingou",
  977. )
  978. msgs = pagure.lib.query.edit_issue_tags(
  979. session=self.session,
  980. project=repo,
  981. old_tag="tag1",
  982. new_tag="tag2",
  983. new_tag_description="lorem ipsum",
  984. new_tag_color="black",
  985. user="pingou",
  986. )
  987. self.session.commit()
  988. self.assertEqual(
  989. msgs,
  990. ["Edited tag: tag1()[DeepSkyBlue] to tag2(lorem ipsum)[black]"],
  991. )
  992. # Try editing the tag without changing anything
  993. self.assertRaises(
  994. pagure.exceptions.PagureException,
  995. pagure.lib.query.edit_issue_tags,
  996. session=self.session,
  997. project=repo,
  998. old_tag="tag2",
  999. new_tag="tag2",
  1000. new_tag_description="lorem ipsum",
  1001. new_tag_color="black",
  1002. user="pingou",
  1003. )
  1004. # Add a new tag
  1005. msg = pagure.lib.query.add_tag_obj(
  1006. session=self.session, obj=issue, tags="tag3", user="pingou"
  1007. )
  1008. self.session.commit()
  1009. self.assertEqual(msg, "Issue tagged with: tag3")
  1010. self.assertEqual([tag.tag for tag in issue.tags], ["tag2", "tag3"])
  1011. # Attempt to rename an existing tag into another existing one
  1012. self.assertRaises(
  1013. pagure.exceptions.PagureException,
  1014. pagure.lib.query.edit_issue_tags,
  1015. session=self.session,
  1016. project=repo,
  1017. old_tag="tag2",
  1018. new_tag="tag3",
  1019. new_tag_description="lorem ipsum",
  1020. new_tag_color="red",
  1021. user="pingou",
  1022. )
  1023. # Rename an existing tag
  1024. msgs = pagure.lib.query.edit_issue_tags(
  1025. session=self.session,
  1026. project=repo,
  1027. old_tag="tag2",
  1028. new_tag="tag4",
  1029. new_tag_description="ipsum lorem",
  1030. new_tag_color="purple",
  1031. user="pingou",
  1032. )
  1033. self.session.commit()
  1034. self.assertEqual(
  1035. msgs,
  1036. [
  1037. "Edited tag: tag2(lorem ipsum)[black] to tag4(ipsum lorem)[purple]"
  1038. ],
  1039. )
  1040. @patch("pagure.lib.git.update_git")
  1041. @patch("pagure.lib.notify.send_email")
  1042. def test_search_issues(self, p_send_email, p_ugt):
  1043. """ Test the search_issues of pagure.lib.query. """
  1044. p_send_email.return_value = True
  1045. p_ugt.return_value = True
  1046. self.test_edit_issue()
  1047. repo = pagure.lib.query._get_project(self.session, "test")
  1048. # All issues
  1049. issues = pagure.lib.query.search_issues(self.session, repo)
  1050. self.assertEqual(len(issues), 2)
  1051. self.assertEqual(issues[1].id, 1)
  1052. self.assertEqual(issues[1].project_id, 1)
  1053. self.assertEqual(issues[1].status, "Open")
  1054. self.assertEqual(issues[1].tags, [])
  1055. self.assertEqual(issues[0].id, 2)
  1056. self.assertEqual(issues[0].project_id, 1)
  1057. self.assertEqual(issues[0].status, "Closed")
  1058. self.assertEqual(issues[0].close_status, "Invalid")
  1059. self.assertEqual(issues[0].tags, [])
  1060. # Issues by status
  1061. issues = pagure.lib.query.search_issues(
  1062. self.session, repo, status="Closed"
  1063. )
  1064. self.assertEqual(len(issues), 1)
  1065. self.assertEqual(issues[0].id, 2)
  1066. self.assertEqual(issues[0].project_id, 1)
  1067. self.assertEqual(issues[0].status, "Closed")
  1068. self.assertEqual(issues[0].close_status, "Invalid")
  1069. self.assertEqual(issues[0].tags, [])
  1070. # Issues closed
  1071. issues = pagure.lib.query.search_issues(
  1072. self.session, repo, closed=True
  1073. )
  1074. self.assertEqual(len(issues), 1)
  1075. self.assertEqual(issues[0].id, 2)
  1076. self.assertEqual(issues[0].project_id, 1)
  1077. self.assertEqual(issues[0].status, "Closed")
  1078. self.assertEqual(issues[0].close_status, "Invalid")
  1079. self.assertEqual(issues[0].tags, [])
  1080. # Issues by tag
  1081. issues = pagure.lib.query.search_issues(self.session, repo, tags="foo")
  1082. self.assertEqual(len(issues), 0)
  1083. issues = pagure.lib.query.search_issues(
  1084. self.session, repo, tags="!foo"
  1085. )
  1086. self.assertEqual(len(issues), 2)
  1087. # Issue by id
  1088. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  1089. self.assertEqual(issue.title, "Test issue")
  1090. self.assertEqual(issue.user.user, "pingou")
  1091. self.assertEqual(issue.tags, [])
  1092. # Issues by authors
  1093. issues = pagure.lib.query.search_issues(
  1094. self.session, repo, author="foo"
  1095. )
  1096. self.assertEqual(len(issues), 1)
  1097. self.assertEqual(issues[0].id, 2)
  1098. self.assertEqual(issues[0].project_id, 1)
  1099. self.assertEqual(issues[0].status, "Closed")
  1100. self.assertEqual(issues[0].close_status, "Invalid")
  1101. self.assertEqual(issues[0].tags, [])
  1102. # Issues by assignee
  1103. issues = pagure.lib.query.search_issues(
  1104. self.session, repo, assignee="foo"
  1105. )
  1106. self.assertEqual(len(issues), 0)
  1107. issues = pagure.lib.query.search_issues(
  1108. self.session, repo, assignee="!foo"
  1109. )
  1110. self.assertEqual(len(issues), 2)
  1111. issues = pagure.lib.query.search_issues(
  1112. self.session, repo, private="foo"
  1113. )
  1114. self.assertEqual(len(issues), 2)
  1115. @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
  1116. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1117. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1118. def test_add_issue_assignee(self):
  1119. """ Test the add_issue_assignee of pagure.lib.query. """
  1120. self.test_new_issue()
  1121. repo = pagure.lib.query._get_project(self.session, "test")
  1122. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  1123. # Before
  1124. issues = pagure.lib.query.search_issues(
  1125. self.session, repo, assignee="pingou"
  1126. )
  1127. self.assertEqual(len(issues), 0)
  1128. # Test when it fails
  1129. self.assertRaises(
  1130. pagure.exceptions.PagureException,
  1131. pagure.lib.query.add_issue_assignee,
  1132. session=self.session,
  1133. issue=issue,
  1134. assignee="foo@foobar.com",
  1135. user="foo@pingou.com",
  1136. )
  1137. self.assertRaises(
  1138. pagure.exceptions.PagureException,
  1139. pagure.lib.query.add_issue_assignee,
  1140. session=self.session,
  1141. issue=issue,
  1142. assignee="foo@bar.com",
  1143. user="foo@foopingou.com",
  1144. )
  1145. # Set the assignee by its email
  1146. msg = pagure.lib.query.add_issue_assignee(
  1147. session=self.session,
  1148. issue=issue,
  1149. assignee="foo@bar.com",
  1150. user="foo@pingou.com",
  1151. )
  1152. self.session.commit()
  1153. self.assertEqual(msg, "Issue assigned to foo@bar.com")
  1154. # Change the assignee to someone else by its username
  1155. msg = pagure.lib.query.add_issue_assignee(
  1156. session=self.session, issue=issue, assignee="pingou", user="pingou"
  1157. )
  1158. self.session.commit()
  1159. self.assertEqual(msg, "Issue assigned to pingou (was: foo)")
  1160. # After -- Searches by assignee
  1161. issues = pagure.lib.query.search_issues(
  1162. self.session, repo, assignee="pingou"
  1163. )
  1164. self.assertEqual(len(issues), 1)
  1165. self.assertEqual(issues[0].id, 2)
  1166. self.assertEqual(issues[0].project_id, 1)
  1167. self.assertEqual(issues[0].status, "Open")
  1168. self.assertEqual(issues[0].tags, [])
  1169. issues = pagure.lib.query.search_issues(
  1170. self.session, repo, assignee=True
  1171. )
  1172. self.assertEqual(len(issues), 1)
  1173. self.assertEqual(issues[0].id, 2)
  1174. self.assertEqual(issues[0].title, "Test issue #2")
  1175. self.assertEqual(issues[0].project_id, 1)
  1176. self.assertEqual(issues[0].status, "Open")
  1177. self.assertEqual(issues[0].tags, [])
  1178. issues = pagure.lib.query.search_issues(
  1179. self.session, repo, assignee=False
  1180. )
  1181. self.assertEqual(len(issues), 1)
  1182. self.assertEqual(issues[0].id, 1)
  1183. self.assertEqual(issues[0].title, "Test issue")
  1184. self.assertEqual(issues[0].project_id, 1)
  1185. self.assertEqual(issues[0].status, "Open")
  1186. self.assertEqual(issues[0].tags, [])
  1187. # Reset the assignee to no-one
  1188. msg = pagure.lib.query.add_issue_assignee(
  1189. session=self.session, issue=issue, assignee=None, user="pingou"
  1190. )
  1191. self.session.commit()
  1192. self.assertEqual(msg, "Assignee reset")
  1193. issues = pagure.lib.query.search_issues(
  1194. self.session, repo, assignee=False
  1195. )
  1196. self.assertEqual(len(issues), 2)
  1197. self.assertEqual(issues[0].id, 2)
  1198. self.assertEqual(issues[1].id, 1)
  1199. issues = pagure.lib.query.search_issues(
  1200. self.session, repo, assignee=True
  1201. )
  1202. self.assertEqual(len(issues), 0)
  1203. @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
  1204. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1205. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1206. def test_add_issue_comment(self):
  1207. """ Test the add_issue_comment of pagure.lib.query. """
  1208. self.test_new_issue()
  1209. repo = pagure.lib.query._get_project(self.session, "test")
  1210. # Before
  1211. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  1212. self.assertEqual(len(issue.comments), 0)
  1213. # Set the assignee by its email
  1214. msg = pagure.lib.query.add_issue_assignee(
  1215. session=self.session,
  1216. issue=issue,
  1217. assignee="foo@bar.com",
  1218. user="foo@pingou.com",
  1219. )
  1220. self.session.commit()
  1221. self.assertEqual(msg, "Issue assigned to foo@bar.com")
  1222. # Add a comment to that issue
  1223. msg = pagure.lib.query.add_issue_comment(
  1224. session=self.session,
  1225. issue=issue,
  1226. comment="Hey look a comment!",
  1227. user="foo",
  1228. )
  1229. self.session.commit()
  1230. self.assertEqual(msg, "Comment added")
  1231. # After
  1232. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  1233. self.assertEqual(len(issue.comments), 1)
  1234. self.assertEqual(issue.comments[0].comment, "Hey look a comment!")
  1235. self.assertEqual(issue.comments[0].user.user, "foo")
  1236. @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
  1237. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  1238. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1239. def test_add_issue_comment_private(self):
  1240. """ Test the add_issue_comment of pagure.lib.query. """
  1241. tests.create_projects(self.session)
  1242. project = pagure.lib.query._get_project(self.session, "test")
  1243. msg = pagure.lib.query.new_issue(
  1244. session=self.session,
  1245. repo=project,
  1246. title="Test issue #1",
  1247. content="We should work on this for the second time",
  1248. user="foo",
  1249. status="Open",
  1250. private=True,
  1251. )
  1252. self.session.commit()
  1253. self.assertEqual(msg.title, "Test issue #1")
  1254. self.assertEqual(project.open_tickets, 1)
  1255. self.assertEqual(project.open_tickets_public, 0)
  1256. # Before
  1257. issue = pagure.lib.query.search_issues(
  1258. self.session, project, issueid=1
  1259. )
  1260. self.assertEqual(len(issue.comments), 0)
  1261. # Add a comment to that issue
  1262. msg = pagure.lib.query.add_issue_comment(
  1263. session=self.session,
  1264. issue=issue,
  1265. comment="Hey look a comment!",
  1266. user="foo",
  1267. )
  1268. self.session.commit()
  1269. self.assertEqual(msg, "Comment added")
  1270. # After
  1271. issue = pagure.lib.query.search_issues(
  1272. self.session, project, issueid=1
  1273. )
  1274. self.assertEqual(len(issue.comments), 1)
  1275. self.assertEqual(issue.comments[0].comment, "Hey look a comment!")
  1276. self.assertEqual(issue.comments[0].user.user, "foo")
  1277. @patch("pagure.lib.notify.send_email")
  1278. def test_add_user_to_project(self, p_send_email):
  1279. """ Test the add_user_to_project of pagure.lib.query. """
  1280. p_send_email.return_value = True
  1281. tests.create_projects(self.session)
  1282. # Before
  1283. repo = pagure.lib.query._get_project(self.session, "test")
  1284. self.assertEqual(len(repo.users), 0)
  1285. # Add an user to a project
  1286. self.assertRaises(
  1287. pagure.exceptions.PagureException,
  1288. pagure.lib.query.add_user_to_project,
  1289. session=self.session,
  1290. project=repo,
  1291. new_user="foobar",
  1292. user="pingou",
  1293. )
  1294. msg = pagure.lib.query.add_user_to_project(
  1295. session=self.session, project=repo, new_user="foo", user="pingou"
  1296. )
  1297. self.session.commit()
  1298. self.assertEqual(msg, "User added")
  1299. # After
  1300. repo = pagure.lib.query._get_project(self.session, "test")
  1301. self.assertEqual(len(repo.users), 1)
  1302. self.assertEqual(repo.users[0].user, "foo")
  1303. self.assertEqual(repo.admins[0].user, "foo")
  1304. # Try adding the same user with the same access
  1305. self.assertRaises(
  1306. pagure.exceptions.PagureException,
  1307. pagure.lib.query.add_user_to_project,
  1308. session=self.session,
  1309. project=repo,
  1310. new_user="foo",
  1311. user="pingou",
  1312. access="admin",
  1313. )
  1314. # Update the access of the user
  1315. msg = pagure.lib.query.add_user_to_project(
  1316. session=self.session,
  1317. project=repo,
  1318. new_user="foo",
  1319. user="pingou",
  1320. access="commit",
  1321. )
  1322. self.session.commit()
  1323. self.assertEqual(msg, "User access updated")
  1324. self.assertEqual(len(repo.users), 1)
  1325. self.assertEqual(repo.users[0].user, "foo")
  1326. self.assertEqual(repo.committers[0].user, "foo")
  1327. def test_new_project(self):
  1328. """ Test the new_project of pagure.lib.query. """
  1329. gitfolder = os.path.join(self.path, "repos")
  1330. docfolder = os.path.join(gitfolder, "docs")
  1331. ticketfolder = os.path.join(gitfolder, "tickets")
  1332. requestfolder = os.path.join(gitfolder, "requests")
  1333. # Try creating a blacklisted project
  1334. self.assertRaises(
  1335. pagure.exceptions.ProjectBlackListedException,
  1336. pagure.lib.query.new_project,
  1337. session=self.session,
  1338. user="pingou",
  1339. name="static",
  1340. repospanner_region=None,
  1341. blacklist=["static"],
  1342. allowed_prefix=[],
  1343. description="description for static",
  1344. parent_id=None,
  1345. )
  1346. # Try creating a project that's blacklisted by wildcard match
  1347. self.assertRaises(
  1348. pagure.exceptions.ProjectBlackListedException,
  1349. pagure.lib.query.new_project,
  1350. session=self.session,
  1351. user="pingou",
  1352. namespace="space",
  1353. name="static",
  1354. repospanner_region=None,
  1355. blacklist=["space/*"],
  1356. allowed_prefix=["space"],
  1357. description="description for static",
  1358. parent_id=None,
  1359. )
  1360. # Try creating a 40 chars project
  1361. self.assertRaises(
  1362. pagure.exceptions.PagureException,
  1363. pagure.lib.query.new_project,
  1364. session=self.session,
  1365. user="pingou",
  1366. name="s" * 40,
  1367. namespace="pingou",
  1368. repospanner_region=None,
  1369. blacklist=["static"],
  1370. allowed_prefix=["pingou"],
  1371. description="description for 40 chars length project",
  1372. parent_id=None,
  1373. prevent_40_chars=True,
  1374. )
  1375. # Create a new project
  1376. pagure.config.config["GIT_FOLDER"] = gitfolder
  1377. task = pagure.lib.query.new_project(
  1378. session=self.session,
  1379. user="pingou",
  1380. name="testproject",
  1381. repospanner_region=None,
  1382. blacklist=[],
  1383. allowed_prefix=[],
  1384. description="description for testproject",
  1385. parent_id=None,
  1386. )
  1387. self.session.commit()
  1388. self.assertEqual(
  1389. task.get(),
  1390. {
  1391. "endpoint": "ui_ns.view_repo",
  1392. "repo": "testproject",
  1393. "namespace": None,
  1394. },
  1395. )
  1396. # Try creating an existing project using a different case
  1397. self.assertRaises(
  1398. pagure.exceptions.PagureException,
  1399. pagure.lib.query.new_project,
  1400. session=self.session,
  1401. user="pingou",
  1402. name="TestProject",
  1403. repospanner_region=None,
  1404. blacklist=[],
  1405. allowed_prefix=[],
  1406. description="description for testproject",
  1407. parent_id=None,
  1408. )
  1409. # Now test that creation fails if ignore_existing_repo is False
  1410. self.session.commit()
  1411. repo = pagure.lib.query.get_authorized_project(
  1412. self.session, "testproject"
  1413. )
  1414. self.assertEqual(repo.path, "testproject.git")
  1415. gitrepo = os.path.join(gitfolder, repo.path)
  1416. docrepo = os.path.join(docfolder, repo.path)
  1417. ticketrepo = os.path.join(ticketfolder, repo.path)
  1418. requestrepo = os.path.join(requestfolder, repo.path)
  1419. self.assertTrue(os.path.exists(gitrepo))
  1420. self.assertTrue(os.path.exists(docrepo))
  1421. self.assertTrue(os.path.exists(ticketrepo))
  1422. self.assertTrue(os.path.exists(requestrepo))
  1423. # Try re-creating it but all repos are existing
  1424. self.assertRaises(
  1425. pagure.exceptions.PagureException,
  1426. pagure.lib.query.new_project,
  1427. session=self.session,
  1428. user="pingou",
  1429. name="testproject",
  1430. repospanner_region=None,
  1431. blacklist=[],
  1432. allowed_prefix=[],
  1433. description="description for testproject",
  1434. parent_id=None,
  1435. )
  1436. self.session.rollback()
  1437. self.assertTrue(os.path.exists(gitrepo))
  1438. self.assertTrue(os.path.exists(docrepo))
  1439. self.assertTrue(os.path.exists(ticketrepo))
  1440. self.assertTrue(os.path.exists(requestrepo))
  1441. # Try re-creating it ignoring the existing repos- but repo in the DB
  1442. self.assertRaises(
  1443. pagure.exceptions.PagureException,
  1444. pagure.lib.query.new_project,
  1445. session=self.session,
  1446. user="pingou",
  1447. name="testproject",
  1448. repospanner_region=None,
  1449. blacklist=[],
  1450. allowed_prefix=[],
  1451. description="description for testproject",
  1452. parent_id=None,
  1453. )
  1454. self.session.rollback()
  1455. # Re-create it, ignoring the existing repos on disk
  1456. repo = pagure.lib.query._get_project(self.session, "testproject")
  1457. self.session.delete(repo)
  1458. self.session.commit()
  1459. task = pagure.lib.query.new_project(
  1460. session=self.session,
  1461. user="pingou",
  1462. name="testproject",
  1463. repospanner_region=None,
  1464. blacklist=[],
  1465. allowed_prefix=[],
  1466. description="description for testproject",
  1467. parent_id=None,
  1468. ignore_existing_repo=True,
  1469. )
  1470. self.session.commit()
  1471. self.assertEqual(
  1472. task.get(),
  1473. {
  1474. "endpoint": "ui_ns.view_repo",
  1475. "repo": "testproject",
  1476. "namespace": None,
  1477. },
  1478. )
  1479. # Delete the repo from the DB so we can try again
  1480. repo = pagure.lib.query._get_project(self.session, "testproject")
  1481. self.session.delete(repo)
  1482. self.session.commit()
  1483. self.assertTrue(os.path.exists(gitrepo))
  1484. self.assertTrue(os.path.exists(docrepo))
  1485. self.assertTrue(os.path.exists(ticketrepo))
  1486. self.assertTrue(os.path.exists(requestrepo))
  1487. # Drop the main git repo and try again
  1488. shutil.rmtree(gitrepo)
  1489. task = pagure.lib.query.new_project(
  1490. session=self.session,
  1491. user="pingou",
  1492. name="testproject",
  1493. repospanner_region=None,
  1494. blacklist=[],
  1495. allowed_prefix=[],
  1496. description="description for testproject",
  1497. parent_id=None,
  1498. )
  1499. self.assertIn("already exists", str(task.get(propagate=False)))
  1500. self.assertFalse(os.path.exists(gitrepo))
  1501. self.assertTrue(os.path.exists(docrepo))
  1502. self.assertTrue(os.path.exists(ticketrepo))
  1503. self.assertTrue(os.path.exists(requestrepo))
  1504. # Drop the doc repo and try again
  1505. shutil.rmtree(docrepo)
  1506. with self.assertRaises(pagure.exceptions.RepoExistsException):
  1507. task = pagure.lib.query.new_project(
  1508. session=self.session,
  1509. user="pingou",
  1510. name="testproject",
  1511. repospanner_region=None,
  1512. blacklist=[],
  1513. allowed_prefix=[],
  1514. description="description for testproject",
  1515. parent_id=None,
  1516. )
  1517. task.get()
  1518. self.assertFalse(os.path.exists(gitrepo))
  1519. self.assertFalse(os.path.exists(docrepo))
  1520. self.assertTrue(os.path.exists(ticketrepo))
  1521. self.assertTrue(os.path.exists(requestrepo))
  1522. # Drop the request repo and try again
  1523. shutil.rmtree(ticketrepo)
  1524. with self.assertRaises(pagure.exceptions.RepoExistsException):
  1525. task = pagure.lib.query.new_project(
  1526. session=self.session,
  1527. user="pingou",
  1528. name="testproject",
  1529. repospanner_region=None,
  1530. blacklist=[],
  1531. allowed_prefix=[],
  1532. description="description for testproject",
  1533. parent_id=None,
  1534. )
  1535. task.get()
  1536. self.assertFalse(os.path.exists(gitrepo))
  1537. self.assertFalse(os.path.exists(docrepo))
  1538. self.assertFalse(os.path.exists(ticketrepo))
  1539. self.assertTrue(os.path.exists(requestrepo))
  1540. # Re-Try creating a 40 chars project this time allowing it
  1541. task = pagure.lib.query.new_project(
  1542. session=self.session,
  1543. user="pingou",
  1544. name="pingou/" + "s" * 40,
  1545. repospanner_region=None,
  1546. blacklist=["static"],
  1547. allowed_prefix=["pingou"],
  1548. description="description for 40 chars length project",
  1549. parent_id=None,
  1550. )
  1551. self.session.commit()
  1552. self.assertEqual(
  1553. task.get(),
  1554. {
  1555. "endpoint": "ui_ns.view_repo",
  1556. "repo": "pingou/ssssssssssssssssssssssssssssssssssssssss",
  1557. "namespace": None,
  1558. },
  1559. )
  1560. def test_new_project_user_ns(self):
  1561. """ Test the new_project of pagure.lib with user_ns on. """
  1562. gitfolder = os.path.join(self.path, "repos")
  1563. docfolder = os.path.join(gitfolder, "docs")
  1564. ticketfolder = os.path.join(gitfolder, "tickets")
  1565. requestfolder = os.path.join(gitfolder, "requests")
  1566. # Create a new project with user_ns as True
  1567. pagure.config.config["GIT_FOLDER"] = gitfolder
  1568. task = pagure.lib.query.new_project(
  1569. session=self.session,
  1570. user="pingou",
  1571. name="testproject",
  1572. repospanner_region=None,
  1573. blacklist=[],
  1574. allowed_prefix=[],
  1575. description="description for testproject",
  1576. parent_id=None,
  1577. user_ns=True,
  1578. )
  1579. self.session.commit()
  1580. self.assertEqual(
  1581. task.get(),
  1582. {
  1583. "endpoint": "ui_ns.view_repo",
  1584. "repo": "testproject",
  1585. "namespace": "pingou",
  1586. },
  1587. )
  1588. repo = pagure.lib.query._get_project(
  1589. self.session, "testproject", namespace="pingou"
  1590. )
  1591. self.assertEqual(repo.path, "pingou/testproject.git")
  1592. gitrepo = os.path.join(gitfolder, repo.path)
  1593. docrepo = os.path.join(docfolder, repo.path)
  1594. ticketrepo = os.path.join(ticketfolder, repo.path)
  1595. requestrepo = os.path.join(requestfolder, repo.path)
  1596. for path in [gitrepo, docrepo, ticketrepo, requestrepo]:
  1597. self.assertTrue(os.path.exists(path))
  1598. shutil.rmtree(path)
  1599. # Create a new project with a namespace and user_ns as True
  1600. pagure.config.config["GIT_FOLDER"] = gitfolder
  1601. task = pagure.lib.query.new_project(
  1602. session=self.session,
  1603. user="pingou",
  1604. name="testproject2",
  1605. namespace="testns",
  1606. repospanner_region=None,
  1607. blacklist=[],
  1608. allowed_prefix=["testns"],
  1609. description="description for testproject2",
  1610. parent_id=None,
  1611. user_ns=True,
  1612. )
  1613. self.session.commit()
  1614. self.assertEqual(
  1615. task.get(),
  1616. {
  1617. "endpoint": "ui_ns.view_repo",
  1618. "repo": "testproject2",
  1619. "namespace": "testns",
  1620. },
  1621. )
  1622. repo = pagure.lib.query._get_project(
  1623. self.session, "testproject2", namespace="testns"
  1624. )
  1625. self.assertEqual(repo.path, "testns/testproject2.git")
  1626. gitrepo = os.path.join(gitfolder, repo.path)
  1627. docrepo = os.path.join(docfolder, repo.path)
  1628. ticketrepo = os.path.join(ticketfolder, repo.path)
  1629. requestrepo = os.path.join(requestfolder, repo.path)
  1630. for path in [gitrepo, docrepo, ticketrepo, requestrepo]:
  1631. self.assertTrue(os.path.exists(path))
  1632. shutil.rmtree(path)
  1633. @patch("pagure.lib.notify.log")
  1634. def test_update_project_settings(self, mock_log):
  1635. """ Test the update_project_settings of pagure.lib.query. """
  1636. tests.create_projects(self.session)
  1637. # Before
  1638. repo = pagure.lib.query._get_project(self.session, "test2")
  1639. self.assertTrue(repo.settings["issue_tracker"])
  1640. self.assertFalse(repo.settings["project_documentation"])
  1641. msg = pagure.lib.query.update_project_settings(
  1642. session=self.session,
  1643. repo=repo,
  1644. settings={
  1645. "issue_tracker": True,
  1646. "project_documentation": False,
  1647. "pull_requests": True,
  1648. "Only_assignee_can_merge_pull-request": False,
  1649. "Minimum_score_to_merge_pull-request": -1,
  1650. "Web-hooks": None,
  1651. "Enforce_signed-off_commits_in_pull-request": False,
  1652. "always_merge": False,
  1653. "issues_default_to_private": False,
  1654. "fedmsg_notifications": True,
  1655. "stomp_notifications": True,
  1656. "pull_request_access_only": False,
  1657. "mqtt_notifications": True,
  1658. },
  1659. user="pingou",
  1660. )
  1661. self.assertEqual(msg, "No settings to change")
  1662. mock_log.assert_not_called()
  1663. # Invalid `Minimum_score_to_merge_pull-request`
  1664. self.assertRaises(
  1665. pagure.exceptions.PagureException,
  1666. pagure.lib.query.update_project_settings,
  1667. session=self.session,
  1668. repo=repo,
  1669. settings={
  1670. "issue_tracker": False,
  1671. "project_documentation": True,
  1672. "pull_requests": False,
  1673. "Only_assignee_can_merge_pull-request": None,
  1674. "Minimum_score_to_merge_pull-request": "foo",
  1675. "Web-hooks": "https://pagure.io/foobar",
  1676. "Enforce_signed-off_commits_in_pull-request": False,
  1677. "issues_default_to_private": False,
  1678. "fedmsg_notifications": True,
  1679. "stomp_notifications": True,
  1680. "pull_request_access_only": False,
  1681. },
  1682. user="pingou",
  1683. )
  1684. msg = pagure.lib.query.update_project_settings(
  1685. session=self.session,
  1686. repo=repo,
  1687. settings={
  1688. "issue_tracker": False,
  1689. "project_documentation": True,
  1690. "pull_requests": False,
  1691. "Only_assignee_can_merge_pull-request": None,
  1692. "Minimum_score_to_merge_pull-request": None,
  1693. "Web-hooks": "https://pagure.io/foobar",
  1694. "Enforce_signed-off_commits_in_pull-request": False,
  1695. "issues_default_to_private": False,
  1696. "fedmsg_notifications": True,
  1697. "stomp_notifications": True,
  1698. "pull_request_access_only": False,
  1699. "mqtt_notifications": True,
  1700. },
  1701. user="pingou",
  1702. )
  1703. self.assertEqual(msg, "Edited successfully settings of repo: test2")
  1704. self.assertEqual(mock_log.call_count, 1)
  1705. args = mock_log.call_args
  1706. self.assertEqual(len(args), 2)
  1707. self.assertEqual(args[0][0].fullname, "test2")
  1708. self.assertEqual(
  1709. sorted(args[1]["msg"]["fields"]),
  1710. sorted(
  1711. [
  1712. "Web-hooks",
  1713. "project_documentation",
  1714. "issue_tracker",
  1715. "pull_requests",
  1716. ]
  1717. ),
  1718. )
  1719. self.assertEqual(args[1]["topic"], "project.edit")
  1720. # After
  1721. repo = pagure.lib.query._get_project(self.session, "test2")
  1722. self.assertFalse(repo.settings["issue_tracker"])
  1723. self.assertTrue(repo.settings["project_documentation"])
  1724. self.assertFalse(repo.settings["pull_requests"])
  1725. def test_search_issues_milestones_invalid(self):
  1726. """ Test the search_issues of pagure.lib.query. """
  1727. self.test_edit_issue()
  1728. repo = pagure.lib.query._get_project(self.session, "test")
  1729. self.assertEqual(len(repo.issues), 2)
  1730. issues = pagure.lib.query.search_issues(
  1731. self.session, repo, milestones="foo"
  1732. )
  1733. self.assertEqual(len(issues), 0)
  1734. issues = pagure.lib.query.search_issues(
  1735. self.session, repo, milestones="foo", no_milestones=True
  1736. )
  1737. self.assertEqual(len(issues), 2)
  1738. def test_search_issues_custom_search(self):
  1739. """ Test the search_issues of pagure.lib.query. """
  1740. self.test_edit_issue()
  1741. repo = pagure.lib.query._get_project(self.session, "test")
  1742. self.assertEqual(len(repo.issues), 2)
  1743. issues = pagure.lib.query.search_issues(
  1744. self.session, repo, custom_search={"foo": "*"}
  1745. )
  1746. self.assertEqual(len(issues), 0)
  1747. def test_search_issues_offset(self):
  1748. """ Test the search_issues of pagure.lib.query. """
  1749. self.test_edit_issue()
  1750. repo = pagure.lib.query._get_project(self.session, "test")
  1751. issues = pagure.lib.query.search_issues(self.session, repo)
  1752. self.assertEqual(len(issues), 2)
  1753. self.assertEqual([i.id for i in issues], [2, 1])
  1754. issues = pagure.lib.query.search_issues(self.session, repo, offset=1)
  1755. self.assertEqual(len(issues), 1)
  1756. self.assertEqual([i.id for i in issues], [1])
  1757. def test_search_issues_tags(self):
  1758. """ Test the search_issues of pagure.lib.query. """
  1759. self.test_edit_issue()
  1760. repo = pagure.lib.query._get_project(self.session, "test")
  1761. self.assertEqual(len(repo.issues), 2)
  1762. # Add `tag1` to one issues and `tag2` only to the other one
  1763. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  1764. msg = pagure.lib.query.add_tag_obj(
  1765. session=self.session, obj=issue, tags="tag1", user="pingou"
  1766. )
  1767. self.session.commit()
  1768. self.assertEqual(msg, "Issue tagged with: tag1")
  1769. issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
  1770. msg = pagure.lib.query.add_tag_obj(
  1771. session=self.session, obj=issue, tags="tag2", user="pingou"
  1772. )
  1773. self.session.commit()
  1774. self.assertEqual(msg, "Issue tagged with: tag2")
  1775. # Search all issues tagged with `tag1`
  1776. issues = pagure.lib.query.search_issues(
  1777. self.session, repo, tags="tag1"
  1778. )
  1779. self.assertEqual(len(issues), 1)
  1780. self.assertEqual(issues[0].id, 1)
  1781. self.assertEqual(issues[0].project_id, 1)
  1782. self.assertEqual([tag.tag for tag in issues[0].tags], ["tag1"])
  1783. # Search all issues *not* tagged with `tag1`
  1784. issues = pagure.lib.query.search_issues(
  1785. self.session, repo, tags="!tag1"
  1786. )
  1787. self.assertEqual(len(issues), 1)
  1788. self.assertEqual(issues[0].id, 2)
  1789. self.assertEqual(issues[0].project_id, 1)
  1790. self.assertEqual([tag.tag for tag in issues[0].tags], ["tag2"])
  1791. # Search all issues *not* tagged with `tag1` but tagged with `tag2`
  1792. issues = pagure.lib.query.search_issues(
  1793. self.session, repo, tags=["!tag1", "tag2"]
  1794. )
  1795. self.assertEqual(len(issues), 1)
  1796. self.assertEqual(issues[0].id, 2)
  1797. self.assertEqual(issues[0].project_id, 1)
  1798. self.assertEqual([tag.tag for tag in issues[0].tags], ["tag2"])
  1799. def test_get_tags_of_project(self):
  1800. """ Test the get_tags_of_project of pagure.lib.query. """
  1801. self.test_add_tag_obj()
  1802. repo = pagure.lib.query._get_project(self.session, "test")
  1803. tags = pagure.lib.query.get_tags_of_project(self.session, repo)
  1804. self.assertEqual([tag.tag for tag in tags], ["tag1"])
  1805. tags = pagure.lib.query.get_tags_of_project(
  1806. self.session, repo, pattern="T*"
  1807. )
  1808. self.assertEqual([tag.tag for tag in tags], ["tag1"])
  1809. repo = pagure.lib.query._get_project(self.session, "test2")
  1810. tags = pagure.lib.query.get_tags_of_project(self.session, repo)
  1811. self.assertEqual([tag.tag for tag in tags], [])
  1812. def test_get_issue_statuses(self):
  1813. """ Test the get_issue_statuses of pagure.lib.query. """
  1814. statuses = pagure.lib.query.get_issue_statuses(self.session)
  1815. self.assertEqual(sorted(statuses), ["Closed", "Open"])
  1816. @patch.dict(
  1817. pagure.config.config, {"ALLOWED_EMAIL_DOMAINS": ["fp.o"]}, clear=True
  1818. )
  1819. def test_set_up_user(self):
  1820. """ Test the set_up_user of pagure.lib.query. """
  1821. items = pagure.lib.query.search_user(self.session)
  1822. self.assertEqual(2, len(items))
  1823. self.assertEqual(2, items[0].id)
  1824. self.assertEqual("foo", items[0].user)
  1825. self.assertEqual(1, items[1].id)
  1826. self.assertEqual("pingou", items[1].user)
  1827. pagure.lib.query.set_up_user(
  1828. session=self.session,
  1829. username="skvidal",
  1830. fullname="Seth",
  1831. default_email="skvidal@fp.o",
  1832. keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
  1833. ssh_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDtgzSO9d1IrKdmyBFUvtAJPLgGOhp0lSySkWRSe+/+3KXYjSnsLnCJQlO5M7JfaXhtTHEow86rh4W9+FoJdzo5iocAwH5xPZ5ttHLy7VHgTzNMUeMgKpjy6bBOdPoGPPG4mo7QCMCRJdWBRDv4OSEMLU5jQAvC272YK2V8L918VQ== root@test",
  1834. )
  1835. self.session.commit()
  1836. items = pagure.lib.query.search_user(self.session)
  1837. self.assertEqual(3, len(items))
  1838. self.assertEqual(2, items[0].id)
  1839. self.assertEqual("foo", items[0].user)
  1840. self.assertEqual(1, items[1].id)
  1841. self.assertEqual("pingou", items[1].user)
  1842. self.assertEqual(3, items[2].id)
  1843. self.assertEqual("skvidal", items[2].user)
  1844. self.assertEqual("Seth", items[2].fullname)
  1845. self.assertEqual(
  1846. ["skvidal@fp.o"], [email.email for email in items[2].emails]
  1847. )
  1848. # Add the user a second time
  1849. pagure.lib.query.set_up_user(
  1850. session=self.session,
  1851. username="skvidal",
  1852. fullname="Seth V",
  1853. default_email="skvidal@fp.o",
  1854. keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
  1855. )
  1856. self.session.commit()
  1857. # Nothing changed
  1858. items = pagure.lib.query.search_user(self.session)
  1859. self.assertEqual(3, len(items))
  1860. self.assertEqual("skvidal", items[2].user)
  1861. self.assertEqual("Seth V", items[2].fullname)
  1862. self.assertEqual(
  1863. ["skvidal@fp.o"], [email.email for email in items[2].emails]
  1864. )
  1865. # Add the user a third time with a different email
  1866. pagure.lib.query.set_up_user(
  1867. session=self.session,
  1868. username="skvidal",
  1869. fullname="Seth",
  1870. default_email="svidal@fp.o",
  1871. keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
  1872. )
  1873. self.session.commit()
  1874. # Email added
  1875. items = pagure.lib.query.search_user(self.session)
  1876. self.assertEqual(3, len(items))
  1877. self.assertEqual("skvidal", items[2].user)
  1878. self.assertEqual(
  1879. sorted(["skvidal@fp.o", "svidal@fp.o"]),
  1880. sorted([email.email for email in items[2].emails]),
  1881. )
  1882. # add again with forbidden email domain
  1883. pagure.lib.query.set_up_user(
  1884. session=self.session,
  1885. username="skvidal",
  1886. fullname="Seth",
  1887. default_email="svidal@example.o",
  1888. keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
  1889. )
  1890. self.session.commit()
  1891. # Email should not be added
  1892. items = pagure.lib.query.search_user(self.session)
  1893. self.assertEqual(3, len(items))
  1894. self.assertEqual("skvidal", items[2].user)
  1895. self.assertEqual(
  1896. sorted(["skvidal@fp.o", "svidal@fp.o"]),
  1897. sorted([email.email for email in items[2].emails]),
  1898. )
  1899. @patch.dict(
  1900. pagure.config.config,
  1901. {"ALLOWED_EMAIL_DOMAINS": ["fp.o", "example.c"]},
  1902. clear=True,
  1903. )
  1904. def test_set_up_user_multiple_emaildomains(self):
  1905. """ Test the set_up_user of pagure.lib when there are
  1906. multimple domains configured in ALLOWED_EMAIL_DOMAINS"""
  1907. items = pagure.lib.query.search_user(self.session)
  1908. self.assertEqual(2, len(items))
  1909. self.assertEqual(2, items[0].id)
  1910. self.assertEqual("foo", items[0].user)
  1911. self.assertEqual(1, items[1].id)
  1912. self.assertEqual("pingou", items[1].user)
  1913. pagure.lib.query.set_up_user(
  1914. session=self.session,
  1915. username="skvidal",
  1916. fullname="Seth",
  1917. default_email="skvidal@fp.o",
  1918. keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
  1919. )
  1920. self.session.commit()
  1921. # Add the user a second time with a different email
  1922. pagure.lib.query.set_up_user(
  1923. session=self.session,
  1924. username="skvidal",
  1925. fullname="Seth",
  1926. default_email="skvidal@example.c",
  1927. keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
  1928. )
  1929. self.session.commit()
  1930. # Email added
  1931. items = pagure.lib.query.search_user(self.session)
  1932. self.assertEqual(3, len(items))
  1933. self.assertEqual("skvidal", items[2].user)
  1934. self.assertEqual(
  1935. sorted(["skvidal@fp.o", "skvidal@example.c"]),
  1936. sorted([email.email for email in items[2].emails]),
  1937. )
  1938. # add again with forbidden email domain
  1939. pagure.lib.query.set_up_user(
  1940. session=self.session,
  1941. username="skvidal",
  1942. fullname="Seth",
  1943. default_email="svidal@example.o",
  1944. keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
  1945. )
  1946. self.session.commit()
  1947. # Email should not be added
  1948. items = pagure.lib.query.search_user(self.session)
  1949. self.assertEqual(3, len(items))
  1950. self.assertEqual("skvidal", items[2].user)
  1951. self.assertEqual(
  1952. sorted(["skvidal@fp.o", "skvidal@example.c"]),
  1953. sorted([email.email for email in items[2].emails]),
  1954. )
  1955. def avatar_url_from_email(self):
  1956. """ Test the avatar_url_from_openid of pagure.lib.query. """
  1957. output = pagure.lib.query.avatar_url_from_email(
  1958. "pingou@fedoraproject.org"
  1959. )
  1960. self.assertEqual(
  1961. output,
  1962. "https://seccdn.libravatar.org/avatar/"
  1963. "b3ee7bb4de70b6522c2478df3b4cd6322b5ec5d62ac7ceb1128e3d4ff42f6928"
  1964. "?s=64&d=retro",
  1965. )
  1966. output = pagure.lib.query.avatar_url_from_email("zoé@çëfò.org")
  1967. self.assertEqual(
  1968. output,
  1969. "https://seccdn.libravatar.org/avatar/"
  1970. "8fa6110d1f6a7a013969f012e1149ff89bf1252d4f15d25edee31d4662878656"
  1971. "?s=64&d=retro",
  1972. )
  1973. def test_fork_project_with_branch(self):
  1974. """ Test the fork_project of pagure.lib.query. """
  1975. gitfolder = os.path.join(self.path, "repos")
  1976. docfolder = os.path.join(gitfolder, "docs")
  1977. ticketfolder = os.path.join(gitfolder, "tickets")
  1978. requestfolder = os.path.join(gitfolder, "requests")
  1979. pagure.config.config["GIT_FOLDER"] = gitfolder
  1980. projects = pagure.lib.query.search_projects(self.session)
  1981. self.assertEqual(len(projects), 0)
  1982. # Create a new project
  1983. task = pagure.lib.query.new_project(
  1984. session=self.session,
  1985. user="pingou",
  1986. name="testproject",
  1987. repospanner_region=None,
  1988. blacklist=[],
  1989. allowed_prefix=[],
  1990. description="description for testproject",
  1991. parent_id=None,
  1992. )
  1993. self.session.commit()
  1994. self.assertEqual(
  1995. task.get(),
  1996. {
  1997. "endpoint": "ui_ns.view_repo",
  1998. "repo": "testproject",
  1999. "namespace": None,
  2000. },
  2001. )
  2002. projects = pagure.lib.query.search_projects(self.session)
  2003. self.assertEqual(len(projects), 1)
  2004. project = pagure.lib.query._get_project(self.session, "testproject")
  2005. gitrepo = os.path.join(gitfolder, project.path)
  2006. docrepo = os.path.join(docfolder, project.path)
  2007. ticketrepo = os.path.join(ticketfolder, project.path)
  2008. requestrepo = os.path.join(requestfolder, project.path)
  2009. # Add content to the main repo into three branches
  2010. tests.add_content_git_repo(gitrepo, "master")
  2011. tests.add_content_git_repo(gitrepo, "feature1")
  2012. tests.add_content_git_repo(gitrepo, "feature2")
  2013. # Check the branches of the main repo
  2014. self.assertEqual(
  2015. sorted(pagure.lib.git.get_git_branches(project)),
  2016. ["feature1", "feature2", "master"],
  2017. )
  2018. # Fork
  2019. task = pagure.lib.query.fork_project(
  2020. session=self.session, user="foo", repo=project
  2021. )
  2022. self.session.commit()
  2023. self.assertEqual(
  2024. task.get(),
  2025. {
  2026. "endpoint": "ui_ns.view_repo",
  2027. "repo": "testproject",
  2028. "namespace": None,
  2029. "username": "foo",
  2030. },
  2031. )
  2032. projects = pagure.lib.query.search_projects(self.session)
  2033. self.assertEqual(len(projects), 2)
  2034. project = pagure.lib.query._get_project(
  2035. self.session, "testproject", user="foo"
  2036. )
  2037. # Check the branches of the fork
  2038. self.assertEqual(
  2039. sorted(pagure.lib.git.get_git_branches(project)),
  2040. ["feature1", "feature2", "master"],
  2041. )
  2042. def test_fork_project_preserves_tags(self):
  2043. """ Test the fork_project of pagure.lib.query pushes tags to the fork. """
  2044. gitfolder = os.path.join(self.path, "repos")
  2045. docfolder = os.path.join(gitfolder, "docs")
  2046. ticketfolder = os.path.join(gitfolder, "tickets")
  2047. requestfolder = os.path.join(gitfolder, "requests")
  2048. pagure.config.config["GIT_FOLDER"] = gitfolder
  2049. projects = pagure.lib.query.search_projects(self.session)
  2050. self.assertEqual(len(projects), 0)
  2051. # Create a new project
  2052. task = pagure.lib.query.new_project(
  2053. session=self.session,
  2054. user="pingou",
  2055. name="testproject",
  2056. repospanner_region=None,
  2057. blacklist=[],
  2058. allowed_prefix=[],
  2059. description="description for testproject",
  2060. parent_id=None,
  2061. )
  2062. self.session.commit()
  2063. self.assertEqual(
  2064. task.get(),
  2065. {
  2066. "endpoint": "ui_ns.view_repo",
  2067. "repo": "testproject",
  2068. "namespace": None,
  2069. },
  2070. )
  2071. projects = pagure.lib.query.search_projects(self.session)
  2072. self.assertEqual(len(projects), 1)
  2073. project = pagure.lib.query._get_project(self.session, "testproject")
  2074. gitrepo = os.path.join(gitfolder, project.path)
  2075. docrepo = os.path.join(docfolder, project.path)
  2076. ticketrepo = os.path.join(ticketfolder, project.path)
  2077. requestrepo = os.path.join(requestfolder, project.path)
  2078. # Add content to the main repo into three branches
  2079. tests.add_content_git_repo(gitrepo, "master")
  2080. # Add a tag
  2081. tagged_commit = (
  2082. pygit2.Repository(gitrepo).revparse_single("master").hex
  2083. )
  2084. tag_sha = tests.add_tag_git_repo(
  2085. gitrepo, "1.2.3", tagged_commit, "release 1.2.3"
  2086. )
  2087. # Fork
  2088. task = pagure.lib.query.fork_project(
  2089. session=self.session, user="foo", repo=project
  2090. )
  2091. self.session.commit()
  2092. self.assertEqual(
  2093. task.get(),
  2094. {
  2095. "endpoint": "ui_ns.view_repo",
  2096. "repo": "testproject",
  2097. "namespace": None,
  2098. "username": "foo",
  2099. },
  2100. )
  2101. projects = pagure.lib.query.search_projects(self.session)
  2102. self.assertEqual(len(projects), 2)
  2103. project = pagure.lib.query._get_project(
  2104. self.session, "testproject", user="foo"
  2105. )
  2106. # Check the tag is there
  2107. fork_obj = pygit2.Repository(project.repopath("main"))
  2108. tag = fork_obj.get(tag_sha)
  2109. self.assertEqual(fork_obj[tag.target].hex, tagged_commit)
  2110. self.assertEqual(tag.message, "release 1.2.3")
  2111. def test_fork_project_namespaced(self):
  2112. """ Test the fork_project of pagure.lib on a namespaced project. """
  2113. gitfolder = os.path.join(self.path, "repos")
  2114. docfolder = os.path.join(gitfolder, "docs")
  2115. ticketfolder = os.path.join(gitfolder, "tickets")
  2116. requestfolder = os.path.join(gitfolder, "requests")
  2117. projects = pagure.lib.query.search_projects(self.session)
  2118. self.assertEqual(len(projects), 0)
  2119. # Create a new project
  2120. task = pagure.lib.query.new_project(
  2121. session=self.session,
  2122. user="pingou",
  2123. name="testproject",
  2124. namespace="foonamespace",
  2125. repospanner_region=None,
  2126. blacklist=[],
  2127. allowed_prefix=["foonamespace"],
  2128. description="description for testproject",
  2129. parent_id=None,
  2130. )
  2131. self.session.commit()
  2132. self.assertEqual(
  2133. task.get(),
  2134. {
  2135. "endpoint": "ui_ns.view_repo",
  2136. "repo": "testproject",
  2137. "namespace": "foonamespace",
  2138. },
  2139. )
  2140. projects = pagure.lib.query.search_projects(self.session)
  2141. self.assertEqual(len(projects), 1)
  2142. repo = pagure.lib.query._get_project(
  2143. self.session, "testproject", namespace="foonamespace"
  2144. )
  2145. gitrepo = os.path.join(gitfolder, repo.path)
  2146. docrepo = os.path.join(docfolder, repo.path)
  2147. ticketrepo = os.path.join(ticketfolder, repo.path)
  2148. requestrepo = os.path.join(requestfolder, repo.path)
  2149. self.assertTrue(os.path.exists(gitrepo))
  2150. self.assertTrue(os.path.exists(docrepo))
  2151. self.assertTrue(os.path.exists(ticketrepo))
  2152. self.assertTrue(os.path.exists(requestrepo))
  2153. # Fork worked
  2154. task = pagure.lib.query.fork_project(
  2155. session=self.session, user="foo", repo=repo
  2156. )
  2157. self.session.commit()
  2158. self.assertEqual(
  2159. task.get(),
  2160. {
  2161. "endpoint": "ui_ns.view_repo",
  2162. "repo": "testproject",
  2163. "namespace": "foonamespace",
  2164. "username": "foo",
  2165. },
  2166. )
  2167. # Fork a fork
  2168. repo = pagure.lib.query._get_project(
  2169. self.session, "testproject", user="foo", namespace="foonamespace"
  2170. )
  2171. task = pagure.lib.query.fork_project(
  2172. session=self.session, user="pingou", repo=repo
  2173. )
  2174. self.session.commit()
  2175. self.assertEqual(
  2176. task.get(),
  2177. {
  2178. "endpoint": "ui_ns.view_repo",
  2179. "repo": "testproject",
  2180. "namespace": "foonamespace",
  2181. "username": "pingou",
  2182. },
  2183. )
  2184. @patch("pagure.lib.notify.send_email")
  2185. def test_new_pull_request(self, mockemail):
  2186. """ test new_pull_request of pagure.lib.query. """
  2187. mockemail.return_value = True
  2188. tests.create_projects(self.session)
  2189. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2190. # Create a forked repo
  2191. item = pagure.lib.model.Project(
  2192. user_id=1, # pingou
  2193. name="test",
  2194. description="test project #1",
  2195. is_fork=True,
  2196. parent_id=1,
  2197. hook_token="aaabbbrrr",
  2198. )
  2199. self.session.commit()
  2200. self.session.add(item)
  2201. # Add an extra user to project `foo`
  2202. repo = pagure.lib.query._get_project(self.session, "test")
  2203. self.assertEqual(repo.open_requests, 0)
  2204. msg = pagure.lib.query.add_user_to_project(
  2205. session=self.session, project=repo, new_user="foo", user="pingou"
  2206. )
  2207. self.session.commit()
  2208. self.assertEqual(msg, "User added")
  2209. repo = pagure.lib.query._get_project(self.session, "test")
  2210. forked_repo = pagure.lib.query._get_project(
  2211. self.session, "test", user="pingou"
  2212. )
  2213. # Fails for the lack of repo_from and remote_git
  2214. self.assertRaises(
  2215. pagure.exceptions.PagureException,
  2216. pagure.lib.query.new_pull_request,
  2217. session=self.session,
  2218. repo_from=None,
  2219. branch_from="master",
  2220. repo_to=repo,
  2221. branch_to="master",
  2222. title="test pull-request",
  2223. user="pingou",
  2224. )
  2225. # Let's pretend we turned on the CI hook for the project
  2226. project = pagure.lib.query._get_project(self.session, "test")
  2227. obj = pagure.hooks.pagure_ci.PagureCITable(
  2228. project_id=project.id, active=True
  2229. )
  2230. self.session.add(obj)
  2231. self.session.commit()
  2232. # Create the new PR
  2233. req = pagure.lib.query.new_pull_request(
  2234. session=self.session,
  2235. repo_from=forked_repo,
  2236. branch_from="master",
  2237. repo_to=repo,
  2238. branch_to="master",
  2239. title="test pull-request",
  2240. user="pingou",
  2241. )
  2242. self.session.commit()
  2243. self.assertEqual(req.id, 1)
  2244. self.assertEqual(req.title, "test pull-request")
  2245. self.assertEqual(repo.open_requests, 1)
  2246. @patch("pagure.lib.query.REDIS")
  2247. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2248. def test_add_pull_request_comment(self, mock_redis):
  2249. """ Test add_pull_request_comment of pagure.lib.query. """
  2250. mock_redis.return_value = True
  2251. self.test_new_pull_request()
  2252. request = pagure.lib.query.search_pull_requests(
  2253. self.session, requestid=1
  2254. )
  2255. msg = pagure.lib.query.add_pull_request_comment(
  2256. session=self.session,
  2257. request=request,
  2258. commit="commithash",
  2259. tree_id=None,
  2260. filename="file",
  2261. row=None,
  2262. comment="This is awesome, I got to remember it!",
  2263. user="foo",
  2264. notification=True,
  2265. )
  2266. self.assertEqual(msg, "Comment added")
  2267. self.session.commit()
  2268. self.assertEqual(len(request.discussion), 0)
  2269. self.assertEqual(len(request.comments), 1)
  2270. self.assertEqual(request.score, 0)
  2271. self.assertEqual(mock_redis.publish.call_count, 1)
  2272. @patch("pagure.lib.query.REDIS")
  2273. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2274. @patch("pagure.lib.query.PAGURE_CI", MagicMock(return_value=True))
  2275. def test_add_pull_request_comment_to_re_run_ci(self, mock_redis):
  2276. """ Test add_pull_request_comment of pagure.lib.query. """
  2277. mock_redis.return_value = True
  2278. self.test_new_pull_request()
  2279. self.assertEqual(mock_redis.publish.call_count, 0)
  2280. # Let's pretend we turned on the CI hook for the project
  2281. project = pagure.lib.query._get_project(self.session, "test")
  2282. if not project.ci_hook or not project.ci_hook.active:
  2283. obj = pagure.hooks.pagure_ci.PagureCITable(
  2284. project_id=project.id, active=True
  2285. )
  2286. self.session.add(obj)
  2287. self.session.commit()
  2288. request = pagure.lib.query.search_pull_requests(
  2289. self.session, requestid=1
  2290. )
  2291. msg = pagure.lib.query.add_pull_request_comment(
  2292. session=self.session,
  2293. request=request,
  2294. commit="commithash",
  2295. tree_id=None,
  2296. filename="file",
  2297. row=None,
  2298. comment="Pretty please pagure-ci rebuild",
  2299. user="foo",
  2300. notification=True,
  2301. trigger_ci=["pretty please pagure-ci rebuild"],
  2302. )
  2303. self.assertEqual(msg, "Comment added")
  2304. self.session.commit()
  2305. self.assertEqual(len(request.discussion), 0)
  2306. self.assertEqual(len(request.comments), 1)
  2307. self.assertEqual(request.score, 0)
  2308. self.assertEqual(mock_redis.publish.call_count, 1)
  2309. @patch("pagure.lib.notify.send_email")
  2310. def test_add_pull_request_flag(self, mockemail):
  2311. """ Test add_pull_request_flag of pagure.lib.query. """
  2312. mockemail.return_value = True
  2313. self.test_new_pull_request()
  2314. tests.create_tokens(self.session)
  2315. request = pagure.lib.query.search_pull_requests(
  2316. self.session, requestid=1
  2317. )
  2318. self.assertEqual(len(request.flags), 0)
  2319. msg = pagure.lib.query.add_pull_request_flag(
  2320. session=self.session,
  2321. request=request,
  2322. username="jenkins",
  2323. percent=100,
  2324. comment="Build passes",
  2325. status="success",
  2326. url="http://jenkins.cloud.fedoraproject.org",
  2327. uid="jenkins_build_pagure_34",
  2328. user="foo",
  2329. token="aaabbbcccddd",
  2330. )
  2331. self.assertEqual(msg, ("Flag added", "jenkins_build_pagure_34"))
  2332. self.session.commit()
  2333. self.assertEqual(len(request.flags), 1)
  2334. self.assertEqual(request.flags[0].token_id, "aaabbbcccddd")
  2335. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2336. def test_search_pull_requests(self):
  2337. """ Test search_pull_requests of pagure.lib.query. """
  2338. self.test_new_pull_request()
  2339. prs = pagure.lib.query.search_pull_requests(session=self.session)
  2340. self.assertEqual(len(prs), 1)
  2341. prs = pagure.lib.query.search_pull_requests(
  2342. session=self.session, project_id=1
  2343. )
  2344. self.assertEqual(len(prs), 1)
  2345. prs = pagure.lib.query.search_pull_requests(
  2346. session=self.session, project_id_from=4
  2347. )
  2348. self.assertEqual(len(prs), 1)
  2349. prs = pagure.lib.query.search_pull_requests(
  2350. session=self.session, status=False
  2351. )
  2352. self.assertEqual(len(prs), 0)
  2353. # All non-assigned PR
  2354. prs = pagure.lib.query.search_pull_requests(
  2355. session=self.session, assignee=False
  2356. )
  2357. self.assertEqual(len(prs), 1)
  2358. prs[0].assignee_id = 1
  2359. self.session.add(prs[0])
  2360. self.session.commit()
  2361. # All the PR assigned
  2362. prs = pagure.lib.query.search_pull_requests(
  2363. session=self.session, assignee=True
  2364. )
  2365. self.assertEqual(len(prs), 1)
  2366. # Basically the same as above but then for a specific user
  2367. prs = pagure.lib.query.search_pull_requests(
  2368. session=self.session, assignee="pingou"
  2369. )
  2370. self.assertEqual(len(prs), 1)
  2371. # All PR except those assigned to pingou
  2372. prs = pagure.lib.query.search_pull_requests(
  2373. session=self.session, assignee="!pingou"
  2374. )
  2375. self.assertEqual(len(prs), 0)
  2376. # All PR created by the specified author
  2377. prs = pagure.lib.query.search_pull_requests(
  2378. session=self.session, author="pingou"
  2379. )
  2380. self.assertEqual(len(prs), 1)
  2381. # Count the PR instead of listing them
  2382. prs = pagure.lib.query.search_pull_requests(
  2383. session=self.session, author="pingou", count=True
  2384. )
  2385. self.assertEqual(prs, 1)
  2386. dt = datetime.datetime.utcnow()
  2387. # Create the second PR
  2388. repo = pagure.lib.query._get_project(self.session, "test")
  2389. req = pagure.lib.query.new_pull_request(
  2390. session=self.session,
  2391. repo_from=repo,
  2392. branch_from="feature",
  2393. repo_to=repo,
  2394. branch_to="master",
  2395. title="test pull-request #2",
  2396. user="pingou",
  2397. )
  2398. self.session.commit()
  2399. self.assertEqual(req.id, 2)
  2400. self.assertEqual(req.title, "test pull-request #2")
  2401. self.assertEqual(repo.open_requests, 2)
  2402. # Ensure we have 2 PRs
  2403. prs = pagure.lib.query.search_pull_requests(
  2404. session=self.session, author="pingou"
  2405. )
  2406. self.assertEqual(len(prs), 2)
  2407. # Test the offset
  2408. prs = pagure.lib.query.search_pull_requests(
  2409. session=self.session, author="pingou", offset=1
  2410. )
  2411. self.assertEqual(len(prs), 1)
  2412. # Test the updated_after
  2413. # Test updated after before the second PR was created
  2414. prs = pagure.lib.query.search_pull_requests(
  2415. session=self.session, author="pingou", updated_after=dt
  2416. )
  2417. self.assertEqual(len(prs), 1)
  2418. # Test updated after, 1h ago
  2419. prs = pagure.lib.query.search_pull_requests(
  2420. session=self.session,
  2421. author="pingou",
  2422. updated_after=dt - datetime.timedelta(hours=1),
  2423. )
  2424. self.assertEqual(len(prs), 2)
  2425. @patch("pagure.lib.notify.send_email")
  2426. def test_close_pull_request(self, send_email):
  2427. """ Test close_pull_request of pagure.lib.query. """
  2428. send_email.return_value = True
  2429. self.test_new_pull_request()
  2430. repo = pagure.lib.query._get_project(self.session, "test")
  2431. self.assertEqual(repo.open_requests, 1)
  2432. request = pagure.lib.query.search_pull_requests(
  2433. self.session, requestid=1
  2434. )
  2435. pagure.lib.query.close_pull_request(
  2436. session=self.session, request=request, user="pingou", merged=True
  2437. )
  2438. self.session.commit()
  2439. repo = pagure.lib.query._get_project(self.session, "test")
  2440. self.assertEqual(repo.open_requests, 0)
  2441. prs = pagure.lib.query.search_pull_requests(
  2442. session=self.session, status=False
  2443. )
  2444. self.assertEqual(len(prs), 1)
  2445. # Does not change much, just the notification sent
  2446. pagure.lib.query.close_pull_request(
  2447. session=self.session, request=request, user="pingou", merged=False
  2448. )
  2449. self.session.commit()
  2450. prs = pagure.lib.query.search_pull_requests(
  2451. session=self.session, status=False
  2452. )
  2453. self.assertEqual(len(prs), 1)
  2454. @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
  2455. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  2456. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2457. def test_remove_issue_dependency(self):
  2458. """ Test remove_issue_dependency of pagure.lib.query. """
  2459. self.test_add_issue_dependency()
  2460. repo = pagure.lib.query._get_project(self.session, "test")
  2461. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2462. issue_blocked = pagure.lib.query.search_issues(
  2463. self.session, repo, issueid=2
  2464. )
  2465. # Before
  2466. self.assertEqual(len(issue.children), 1)
  2467. self.assertEqual(issue.children[0].id, 2)
  2468. self.assertEqual(len(issue.parents), 0)
  2469. self.assertEqual(issue.parents, [])
  2470. self.assertEqual(len(issue_blocked.children), 0)
  2471. self.assertEqual(issue_blocked.children, [])
  2472. self.assertEqual(len(issue_blocked.parents), 1)
  2473. self.assertEqual(issue_blocked.parents[0].id, 1)
  2474. self.assertRaises(
  2475. pagure.exceptions.PagureException,
  2476. pagure.lib.query.remove_issue_dependency,
  2477. session=self.session,
  2478. issue=issue,
  2479. issue_blocked=issue,
  2480. user="pingou",
  2481. )
  2482. # Wrong order of issues
  2483. msg = pagure.lib.query.remove_issue_dependency(
  2484. session=self.session,
  2485. issue=issue,
  2486. issue_blocked=issue_blocked,
  2487. user="pingou",
  2488. )
  2489. self.session.commit()
  2490. self.assertEqual(msg, None)
  2491. # Drop deps
  2492. msg = pagure.lib.query.remove_issue_dependency(
  2493. session=self.session,
  2494. issue=issue_blocked,
  2495. issue_blocked=issue,
  2496. user="pingou",
  2497. )
  2498. self.session.commit()
  2499. self.assertEqual(msg, "Issue **un**marked as depending on: #1")
  2500. # After
  2501. self.assertEqual(issue.parents, [])
  2502. self.assertEqual(issue.children, [])
  2503. self.assertEqual(issue_blocked.parents, [])
  2504. self.assertEqual(issue_blocked.children, [])
  2505. @patch("pagure.lib.git.update_git")
  2506. @patch("pagure.lib.notify.send_email")
  2507. def test_get_issue_comment(self, p_send_email, p_ugt):
  2508. """ Test the get_issue_comment of pagure.lib.query. """
  2509. p_send_email.return_value = True
  2510. p_ugt.return_value = True
  2511. self.test_add_issue_comment()
  2512. repo = pagure.lib.query._get_project(self.session, "test")
  2513. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2514. self.assertEqual(
  2515. pagure.lib.query.get_issue_comment(self.session, issue.uid, 10),
  2516. None,
  2517. )
  2518. comment = pagure.lib.query.get_issue_comment(
  2519. self.session, issue.uid, 1
  2520. )
  2521. self.assertEqual(comment.comment, "Hey look a comment!")
  2522. @patch("pagure.lib.git.update_git")
  2523. @patch("pagure.lib.notify.send_email")
  2524. def test_get_issue_by_uid(self, p_send_email, p_ugt):
  2525. """ Test the get_issue_by_uid of pagure.lib.query. """
  2526. p_send_email.return_value = True
  2527. p_ugt.return_value = True
  2528. self.test_new_issue()
  2529. repo = pagure.lib.query._get_project(self.session, "test")
  2530. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2531. self.assertEqual(
  2532. pagure.lib.query.get_issue_by_uid(self.session, "foobar"), None
  2533. )
  2534. new_issue = pagure.lib.query.get_issue_by_uid(self.session, issue.uid)
  2535. self.assertEqual(issue, new_issue)
  2536. @patch("pagure.lib.git.update_git")
  2537. @patch("pagure.lib.notify.send_email")
  2538. def test_update_tags(self, p_send_email, p_ugt):
  2539. """ Test the update_tags of pagure.lib.query. """
  2540. p_send_email.return_value = True
  2541. p_ugt.return_value = True
  2542. self.test_new_issue()
  2543. repo = pagure.lib.query._get_project(self.session, "test")
  2544. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2545. # before
  2546. self.assertEqual(repo.tags_colored, [])
  2547. self.assertEqual(issue.tags_text, [])
  2548. messages = pagure.lib.query.update_tags(
  2549. self.session, issue, "tag", "pingou"
  2550. )
  2551. self.assertEqual(messages, ["Issue tagged with: tag"])
  2552. # after
  2553. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2554. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2555. self.assertEqual([t.tag for t in repo.tags_colored], ["tag"])
  2556. self.assertEqual(issue.tags_text, ["tag"])
  2557. # Replace the tag by two others
  2558. messages = pagure.lib.query.update_tags(
  2559. self.session, issue, ["tag2", "tag3"], "pingou"
  2560. )
  2561. self.assertEqual(
  2562. messages,
  2563. ["Issue tagged with: tag2, tag3", "Issue **un**tagged with: tag"],
  2564. )
  2565. # after
  2566. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2567. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2568. self.assertEqual(
  2569. sorted([t.tag for t in repo.tags_colored]), ["tag", "tag2", "tag3"]
  2570. )
  2571. self.assertEqual(sorted(issue.tags_text), ["tag2", "tag3"])
  2572. @patch("pagure.lib.git.update_git")
  2573. @patch("pagure.lib.notify.send_email")
  2574. def test_update_dependency_issue(self, p_send_email, p_ugt):
  2575. """ Test the update_dependency_issue of pagure.lib.query. """
  2576. p_send_email.return_value = True
  2577. p_ugt.return_value = True
  2578. self.test_new_issue()
  2579. repo = pagure.lib.query._get_project(self.session, "test")
  2580. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2581. self.assertEqual(repo.open_tickets, 2)
  2582. self.assertEqual(repo.open_tickets_public, 2)
  2583. # Create issues to play with
  2584. msg = pagure.lib.query.new_issue(
  2585. session=self.session,
  2586. repo=repo,
  2587. title="Test issue #3",
  2588. content="We should work on this (3rd time!)",
  2589. user="pingou",
  2590. private=True,
  2591. )
  2592. self.session.commit()
  2593. self.assertEqual(msg.title, "Test issue #3")
  2594. self.assertEqual(repo.open_tickets, 3)
  2595. self.assertEqual(repo.open_tickets_public, 2)
  2596. # before
  2597. self.assertEqual(issue.tags_text, [])
  2598. self.assertEqual(issue.depending_text, [])
  2599. self.assertEqual(issue.blocking_text, [])
  2600. messages = pagure.lib.query.update_dependency_issue(
  2601. self.session, repo, issue, "2", "pingou"
  2602. )
  2603. self.assertEqual(messages, ["Issue marked as depending on: #2"])
  2604. messages = pagure.lib.query.update_dependency_issue(
  2605. self.session, repo, issue, ["3", "4", 5], "pingou"
  2606. )
  2607. self.assertEqual(
  2608. messages,
  2609. [
  2610. "Issue marked as depending on: #3",
  2611. "Issue marked as depending on: #4",
  2612. "Issue marked as depending on: #5",
  2613. "Issue **un**marked as depending on: #2",
  2614. ],
  2615. )
  2616. # after
  2617. self.assertEqual(issue.tags_text, [])
  2618. self.assertEqual(issue.depending_text, [3])
  2619. self.assertEqual(issue.blocking_text, [])
  2620. @patch("pagure.lib.git.update_git")
  2621. @patch("pagure.lib.notify.send_email")
  2622. def test_update_blocked_issue(self, p_send_email, p_ugt):
  2623. """ Test the update_blocked_issue of pagure.lib.query. """
  2624. p_send_email.return_value = True
  2625. p_ugt.return_value = True
  2626. self.test_new_issue()
  2627. repo = pagure.lib.query._get_project(self.session, "test")
  2628. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2629. # Create issues to play with
  2630. msg = pagure.lib.query.new_issue(
  2631. session=self.session,
  2632. repo=repo,
  2633. title="Test issue #3",
  2634. content="We should work on this (3rd time!)",
  2635. user="pingou",
  2636. private=True,
  2637. )
  2638. self.session.commit()
  2639. self.assertEqual(msg.title, "Test issue #3")
  2640. # before
  2641. self.assertEqual(issue.tags_text, [])
  2642. self.assertEqual(issue.depending_text, [])
  2643. self.assertEqual(issue.blocking_text, [])
  2644. messages = pagure.lib.query.update_blocked_issue(
  2645. self.session, repo, issue, "2", "pingou"
  2646. )
  2647. self.assertEqual(messages, ["Issue marked as blocking: #2"])
  2648. messages = pagure.lib.query.update_blocked_issue(
  2649. self.session, repo, issue, ["3", "4", 5], "pingou"
  2650. )
  2651. self.assertEqual(
  2652. messages,
  2653. [
  2654. "Issue marked as blocking: #3",
  2655. "Issue marked as blocking: #4",
  2656. "Issue marked as blocking: #5",
  2657. "Issue **un**marked as blocking: #2",
  2658. ],
  2659. )
  2660. # after
  2661. self.assertEqual(issue.tags_text, [])
  2662. self.assertEqual(issue.depending_text, [])
  2663. self.assertEqual(issue.blocking_text, [3])
  2664. @patch("pagure.lib.notify.send_email")
  2665. def test_add_pull_request_assignee(self, mockemail):
  2666. """ Test add_pull_request_assignee of pagure.lib.query. """
  2667. mockemail.return_value = True
  2668. self.test_new_pull_request()
  2669. request = pagure.lib.query.search_pull_requests(
  2670. self.session, requestid=1
  2671. )
  2672. self.assertRaises(
  2673. pagure.exceptions.PagureException,
  2674. pagure.lib.query.add_pull_request_assignee,
  2675. session=self.session,
  2676. request=request,
  2677. assignee="bar",
  2678. user="foo",
  2679. )
  2680. # Assign
  2681. msg = pagure.lib.query.add_pull_request_assignee(
  2682. session=self.session,
  2683. request=request,
  2684. assignee="pingou",
  2685. user="foo",
  2686. )
  2687. self.assertEqual(msg, "Request assigned")
  2688. # Reset
  2689. msg = pagure.lib.query.add_pull_request_assignee(
  2690. session=self.session, request=request, assignee=None, user="foo"
  2691. )
  2692. self.assertEqual(msg, "Request assignee reset")
  2693. # Try resetting again
  2694. msg = pagure.lib.query.add_pull_request_assignee(
  2695. session=self.session, request=request, assignee=None, user="foo"
  2696. )
  2697. self.assertEqual(msg, None)
  2698. def test_search_pending_email(self):
  2699. """ Test search_pending_email of pagure.lib.query. """
  2700. self.assertEqual(
  2701. pagure.lib.query.search_pending_email(self.session), None
  2702. )
  2703. user = pagure.lib.query.search_user(self.session, username="pingou")
  2704. email_pend = pagure.lib.model.UserEmailPending(
  2705. user_id=user.id, email="foo@fp.o", token="abcdef"
  2706. )
  2707. self.session.add(email_pend)
  2708. self.session.commit()
  2709. self.assertNotEqual(
  2710. pagure.lib.query.search_pending_email(self.session), None
  2711. )
  2712. self.assertNotEqual(
  2713. pagure.lib.query.search_pending_email(
  2714. self.session, token="abcdef"
  2715. ),
  2716. None,
  2717. )
  2718. pend = pagure.lib.query.search_pending_email(
  2719. self.session, token="abcdef"
  2720. )
  2721. self.assertEqual(pend.user.username, "pingou")
  2722. self.assertEqual(pend.email, "foo@fp.o")
  2723. self.assertEqual(pend.token, "abcdef")
  2724. pend = pagure.lib.query.search_pending_email(
  2725. self.session, email="foo@fp.o"
  2726. )
  2727. self.assertEqual(pend.user.username, "pingou")
  2728. self.assertEqual(pend.email, "foo@fp.o")
  2729. self.assertEqual(pend.token, "abcdef")
  2730. def test_generate_hook_token(self):
  2731. """ Test generate_hook_token of pagure.lib.query. """
  2732. tests.create_projects(self.session)
  2733. projects = pagure.lib.query.search_projects(self.session)
  2734. for proj in projects:
  2735. self.assertIn(
  2736. proj.hook_token, ["aaabbbccc", "aaabbbddd", "aaabbbeee"]
  2737. )
  2738. pagure.lib.query.generate_hook_token(self.session)
  2739. projects = pagure.lib.query.search_projects(self.session)
  2740. for proj in projects:
  2741. self.assertNotIn(
  2742. proj.hook_token, ["aaabbbccc", "aaabbbddd", "aaabbbeee"]
  2743. )
  2744. @patch("pagure.lib.notify.send_email")
  2745. def test_pull_request_score(self, mockemail):
  2746. """ Test PullRequest.score of pagure.lib.model. """
  2747. mockemail.return_value = True
  2748. self.test_new_pull_request()
  2749. request = pagure.lib.query.search_pull_requests(
  2750. self.session, requestid=1
  2751. )
  2752. msg = pagure.lib.query.add_pull_request_comment(
  2753. session=self.session,
  2754. request=request,
  2755. commit=None,
  2756. tree_id=None,
  2757. filename=None,
  2758. row=None,
  2759. comment="This looks great :thumbsup:",
  2760. user="foo",
  2761. )
  2762. self.session.commit()
  2763. self.assertEqual(msg, "Comment added")
  2764. msg = pagure.lib.query.add_pull_request_comment(
  2765. session=self.session,
  2766. request=request,
  2767. commit=None,
  2768. tree_id=None,
  2769. filename=None,
  2770. row=None,
  2771. comment="I disagree -1",
  2772. user="pingou",
  2773. )
  2774. self.session.commit()
  2775. self.assertEqual(msg, "Comment added")
  2776. msg = pagure.lib.query.add_pull_request_comment(
  2777. session=self.session,
  2778. request=request,
  2779. commit=None,
  2780. tree_id=None,
  2781. filename=None,
  2782. row=None,
  2783. comment="NM this looks great now +1000",
  2784. user="pingou",
  2785. )
  2786. self.session.commit()
  2787. self.assertEqual(msg, "Comment added")
  2788. self.assertEqual(len(request.discussion), 3)
  2789. self.assertEqual(request.score, 2)
  2790. def test_add_group(self):
  2791. """ Test the add_group method of pagure.lib.query. """
  2792. groups = pagure.lib.query.search_groups(self.session)
  2793. self.assertEqual(len(groups), 0)
  2794. self.assertEqual(groups, [])
  2795. # Invalid type
  2796. self.assertRaises(
  2797. pagure.exceptions.PagureException,
  2798. pagure.lib.query.add_group,
  2799. self.session,
  2800. group_name="foo",
  2801. display_name="foo group",
  2802. description=None,
  2803. group_type="bar",
  2804. user="pingou",
  2805. is_admin=True,
  2806. blacklist=[],
  2807. )
  2808. groups = pagure.lib.query.search_groups(self.session)
  2809. self.assertEqual(len(groups), 0)
  2810. self.assertEqual(groups, [])
  2811. # Invalid user
  2812. self.assertRaises(
  2813. pagure.exceptions.PagureException,
  2814. pagure.lib.query.add_group,
  2815. self.session,
  2816. group_name="foo",
  2817. display_name="foo group",
  2818. description=None,
  2819. group_type="user",
  2820. user="test",
  2821. is_admin=False,
  2822. blacklist=[],
  2823. )
  2824. groups = pagure.lib.query.search_groups(self.session)
  2825. self.assertEqual(len(groups), 0)
  2826. self.assertEqual(groups, [])
  2827. # Invalid group name
  2828. self.assertRaises(
  2829. pagure.exceptions.PagureException,
  2830. pagure.lib.query.add_group,
  2831. self.session,
  2832. group_name="foo group",
  2833. display_name="foo group",
  2834. description=None,
  2835. group_type="user",
  2836. user="test",
  2837. is_admin=False,
  2838. blacklist=[],
  2839. )
  2840. groups = pagure.lib.query.search_groups(self.session)
  2841. self.assertEqual(len(groups), 0)
  2842. self.assertEqual(groups, [])
  2843. msg = pagure.lib.query.add_group(
  2844. self.session,
  2845. group_name="foo",
  2846. display_name="foo group",
  2847. description=None,
  2848. group_type="bar",
  2849. user="pingou",
  2850. is_admin=False,
  2851. blacklist=[],
  2852. )
  2853. self.session.commit()
  2854. self.assertEqual(msg, "User `pingou` added to the group `foo`.")
  2855. groups = pagure.lib.query.search_groups(self.session)
  2856. self.assertEqual(len(groups), 1)
  2857. self.assertEqual(groups[0].group_name, "foo")
  2858. # Group with this name already exists
  2859. self.assertRaises(
  2860. pagure.exceptions.PagureException,
  2861. pagure.lib.query.add_group,
  2862. self.session,
  2863. group_name="foo",
  2864. display_name="foo group",
  2865. description=None,
  2866. group_type="bar",
  2867. user="pingou",
  2868. is_admin=False,
  2869. blacklist=[],
  2870. )
  2871. # Group with this display name already exists
  2872. self.assertRaises(
  2873. pagure.exceptions.PagureException,
  2874. pagure.lib.query.add_group,
  2875. self.session,
  2876. group_name="foo1",
  2877. display_name="foo group",
  2878. description=None,
  2879. group_type="bar",
  2880. user="pingou",
  2881. is_admin=False,
  2882. blacklist=[],
  2883. )
  2884. # Group with a blacklisted prefix
  2885. self.assertRaises(
  2886. pagure.exceptions.PagureException,
  2887. pagure.lib.query.add_group,
  2888. self.session,
  2889. group_name="forks",
  2890. display_name="foo group",
  2891. description=None,
  2892. group_type="bar",
  2893. user="pingou",
  2894. is_admin=False,
  2895. blacklist=["forks"],
  2896. )
  2897. def test_add_user_to_group(self):
  2898. """ Test the add_user_to_group method of pagure.lib.query. """
  2899. self.test_add_group()
  2900. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  2901. self.assertNotEqual(group, None)
  2902. self.assertEqual(group.group_name, "foo")
  2903. # Invalid new user
  2904. self.assertRaises(
  2905. pagure.exceptions.PagureException,
  2906. pagure.lib.query.add_user_to_group,
  2907. self.session,
  2908. username="foobar",
  2909. group=group,
  2910. user="foo",
  2911. is_admin=False,
  2912. )
  2913. # Invalid user
  2914. self.assertRaises(
  2915. pagure.exceptions.PagureException,
  2916. pagure.lib.query.add_user_to_group,
  2917. self.session,
  2918. username="foo",
  2919. group=group,
  2920. user="foobar",
  2921. is_admin=False,
  2922. )
  2923. # User not allowed
  2924. self.assertRaises(
  2925. pagure.exceptions.PagureException,
  2926. pagure.lib.query.add_user_to_group,
  2927. self.session,
  2928. username="foo",
  2929. group=group,
  2930. user="foo",
  2931. is_admin=False,
  2932. )
  2933. msg = pagure.lib.query.add_user_to_group(
  2934. self.session,
  2935. username="foo",
  2936. group=group,
  2937. user="pingou",
  2938. is_admin=False,
  2939. )
  2940. self.session.commit()
  2941. self.assertEqual(msg, "User `foo` added to the group `foo`.")
  2942. msg = pagure.lib.query.add_user_to_group(
  2943. self.session,
  2944. username="foo",
  2945. group=group,
  2946. user="pingou",
  2947. is_admin=False,
  2948. )
  2949. self.session.commit()
  2950. self.assertEqual(
  2951. msg, "User `foo` already in the group, nothing to change."
  2952. )
  2953. def test_is_group_member(self):
  2954. """ Test the is_group_member method of pagure.lib.query. """
  2955. self.test_add_group()
  2956. self.assertFalse(
  2957. pagure.lib.query.is_group_member(self.session, None, "foo")
  2958. )
  2959. self.assertFalse(
  2960. pagure.lib.query.is_group_member(self.session, "bar", "foo")
  2961. )
  2962. self.assertFalse(
  2963. pagure.lib.query.is_group_member(self.session, "foo", "foo")
  2964. )
  2965. self.assertTrue(
  2966. pagure.lib.query.is_group_member(self.session, "pingou", "foo")
  2967. )
  2968. def test_get_user_group(self):
  2969. """ Test the get_user_group method of pagure.lib.query. """
  2970. self.test_add_group()
  2971. item = pagure.lib.query.get_user_group(self.session, 1, 1)
  2972. self.assertEqual(item.user_id, 1)
  2973. self.assertEqual(item.group_id, 1)
  2974. item = pagure.lib.query.get_user_group(self.session, 1, 2)
  2975. self.assertEqual(item, None)
  2976. item = pagure.lib.query.get_user_group(self.session, 2, 1)
  2977. self.assertEqual(item, None)
  2978. def test_get_group_types(self):
  2979. """ Test the get_group_types method of pagure.lib.query. """
  2980. self.test_add_group()
  2981. groups = pagure.lib.query.get_group_types(self.session, "user")
  2982. self.assertEqual(len(groups), 1)
  2983. self.assertEqual(groups[0].group_type, "user")
  2984. groups = pagure.lib.query.get_group_types(self.session)
  2985. self.assertEqual(len(groups), 2)
  2986. self.assertEqual(groups[0].group_type, "admin")
  2987. self.assertEqual(groups[1].group_type, "user")
  2988. def test_search_groups(self):
  2989. """ Test the search_groups method of pagure.lib.query. """
  2990. self.assertEqual(pagure.lib.query.search_groups(self.session), [])
  2991. msg = pagure.lib.query.add_group(
  2992. self.session,
  2993. group_name="foo",
  2994. display_name="foo group",
  2995. description=None,
  2996. group_type="bar",
  2997. user="pingou",
  2998. is_admin=False,
  2999. blacklist=[],
  3000. )
  3001. self.session.commit()
  3002. self.assertEqual(msg, "User `pingou` added to the group `foo`.")
  3003. groups = pagure.lib.query.search_groups(self.session)
  3004. self.assertEqual(len(groups), 1)
  3005. self.assertEqual(groups[0].group_name, "foo")
  3006. msg = pagure.lib.query.add_group(
  3007. self.session,
  3008. group_name="bar",
  3009. display_name="bar group",
  3010. description=None,
  3011. group_type="admin",
  3012. user="pingou",
  3013. is_admin=True,
  3014. blacklist=[],
  3015. )
  3016. self.session.commit()
  3017. self.assertEqual(msg, "User `pingou` added to the group `bar`.")
  3018. groups = pagure.lib.query.search_groups(self.session)
  3019. self.assertEqual(len(groups), 2)
  3020. self.assertEqual(groups[0].group_name, "bar")
  3021. self.assertEqual(groups[1].group_name, "foo")
  3022. groups = pagure.lib.query.search_groups(
  3023. self.session, group_type="user"
  3024. )
  3025. self.assertEqual(len(groups), 1)
  3026. self.assertEqual(groups[0].group_name, "foo")
  3027. groups = pagure.lib.query.search_groups(
  3028. self.session, group_type="admin"
  3029. )
  3030. self.assertEqual(len(groups), 1)
  3031. self.assertEqual(groups[0].group_name, "bar")
  3032. groups = pagure.lib.query.search_groups(self.session, group_name="foo")
  3033. self.assertEqual(groups.group_name, "foo")
  3034. def test_delete_user_of_group(self):
  3035. """ Test the delete_user_of_group method of pagure.lib.query. """
  3036. self.test_add_user_to_group()
  3037. groups = pagure.lib.query.search_groups(self.session)
  3038. self.assertEqual(len(groups), 1)
  3039. self.assertEqual(groups[0].group_name, "foo")
  3040. # Invalid username
  3041. self.assertRaises(
  3042. pagure.exceptions.PagureException,
  3043. pagure.lib.query.delete_user_of_group,
  3044. self.session,
  3045. username="bar",
  3046. groupname="foo",
  3047. user="pingou",
  3048. is_admin=False,
  3049. )
  3050. # Invalid groupname
  3051. self.assertRaises(
  3052. pagure.exceptions.PagureException,
  3053. pagure.lib.query.delete_user_of_group,
  3054. self.session,
  3055. username="foo",
  3056. groupname="bar",
  3057. user="pingou",
  3058. is_admin=False,
  3059. )
  3060. # Invalid user
  3061. self.assertRaises(
  3062. pagure.exceptions.PagureException,
  3063. pagure.lib.query.delete_user_of_group,
  3064. self.session,
  3065. username="foo",
  3066. groupname="foo",
  3067. user="test",
  3068. is_admin=False,
  3069. )
  3070. # User not in the group
  3071. item = pagure.lib.model.User(
  3072. user="bar",
  3073. fullname="bar",
  3074. password="foo",
  3075. default_email="bar@bar.com",
  3076. )
  3077. self.session.add(item)
  3078. item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
  3079. self.session.add(item)
  3080. self.session.commit()
  3081. self.assertRaises(
  3082. pagure.exceptions.PagureException,
  3083. pagure.lib.query.delete_user_of_group,
  3084. self.session,
  3085. username="bar",
  3086. groupname="foo",
  3087. user="pingou",
  3088. is_admin=False,
  3089. )
  3090. # User is not allowed to remove the username
  3091. self.assertRaises(
  3092. pagure.exceptions.PagureException,
  3093. pagure.lib.query.delete_user_of_group,
  3094. self.session,
  3095. username="foo",
  3096. groupname="foo",
  3097. user="bar",
  3098. is_admin=False,
  3099. )
  3100. # Username is the creator of the group
  3101. self.assertRaises(
  3102. pagure.exceptions.PagureException,
  3103. pagure.lib.query.delete_user_of_group,
  3104. self.session,
  3105. username="pingou",
  3106. groupname="foo",
  3107. user="pingou",
  3108. is_admin=False,
  3109. )
  3110. # All good
  3111. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3112. self.assertEqual(len(group.users), 2)
  3113. pagure.lib.query.delete_user_of_group(
  3114. self.session,
  3115. username="foo",
  3116. groupname="foo",
  3117. user="pingou",
  3118. is_admin=False,
  3119. )
  3120. self.session.commit()
  3121. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3122. self.assertEqual(len(group.users), 1)
  3123. def test_edit_group_info(self):
  3124. """ Test the edit_group_info method of pagure.lib.query. """
  3125. self.test_add_group()
  3126. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3127. self.assertNotEqual(group, None)
  3128. self.assertEqual(group.group_name, "foo")
  3129. # Invalid new user
  3130. self.assertRaises(
  3131. pagure.exceptions.PagureException,
  3132. pagure.lib.query.edit_group_info,
  3133. self.session,
  3134. group=group,
  3135. display_name="edited name",
  3136. description=None,
  3137. user="foo",
  3138. is_admin=False,
  3139. )
  3140. # Invalid user
  3141. self.assertRaises(
  3142. pagure.exceptions.PagureException,
  3143. pagure.lib.query.edit_group_info,
  3144. self.session,
  3145. group=group,
  3146. display_name="edited name",
  3147. description=None,
  3148. user="foobar",
  3149. is_admin=False,
  3150. )
  3151. # User not allowed
  3152. self.assertRaises(
  3153. pagure.exceptions.PagureException,
  3154. pagure.lib.query.edit_group_info,
  3155. self.session,
  3156. group=group,
  3157. display_name="edited name",
  3158. description=None,
  3159. user="bar",
  3160. is_admin=False,
  3161. )
  3162. msg = pagure.lib.query.edit_group_info(
  3163. self.session,
  3164. group=group,
  3165. display_name="edited name",
  3166. description=None,
  3167. user="pingou",
  3168. is_admin=False,
  3169. )
  3170. self.session.commit()
  3171. self.assertEqual(msg, 'Group "edited name" (foo) edited')
  3172. msg = pagure.lib.query.edit_group_info(
  3173. self.session,
  3174. group=group,
  3175. display_name="edited name",
  3176. description=None,
  3177. user="pingou",
  3178. is_admin=False,
  3179. )
  3180. self.session.commit()
  3181. self.assertEqual(msg, "Nothing changed")
  3182. def test_add_group_to_project(self):
  3183. """ Test the add_group_to_project method of pagure.lib.query. """
  3184. tests.create_projects(self.session)
  3185. self.test_add_group()
  3186. project = pagure.lib.query._get_project(self.session, "test2")
  3187. # Group does not exist
  3188. self.assertRaises(
  3189. pagure.exceptions.PagureException,
  3190. pagure.lib.query.add_group_to_project,
  3191. session=self.session,
  3192. project=project,
  3193. new_group="bar",
  3194. user="foo",
  3195. )
  3196. # Group does not exist, but allow creating it
  3197. msg = pagure.lib.query.add_group_to_project(
  3198. session=self.session,
  3199. project=project,
  3200. new_group="bar",
  3201. user="pingou",
  3202. create=True,
  3203. )
  3204. self.session.commit()
  3205. self.assertEqual(msg, "Group added")
  3206. self.assertEqual(project.groups[0].group_name, "bar")
  3207. self.assertEqual(len(project.admin_groups), 1)
  3208. self.assertEqual(project.admin_groups[0].group_name, "bar")
  3209. # User does not exist
  3210. self.assertRaises(
  3211. pagure.exceptions.PagureException,
  3212. pagure.lib.query.add_group_to_project,
  3213. session=self.session,
  3214. project=project,
  3215. new_group="foo",
  3216. user="bar",
  3217. )
  3218. # User not allowed
  3219. self.assertRaises(
  3220. pagure.exceptions.PagureException,
  3221. pagure.lib.query.add_group_to_project,
  3222. session=self.session,
  3223. project=project,
  3224. new_group="foo",
  3225. user="foo",
  3226. )
  3227. # All good
  3228. msg = pagure.lib.query.add_group_to_project(
  3229. session=self.session,
  3230. project=project,
  3231. new_group="foo",
  3232. user="pingou",
  3233. )
  3234. self.session.commit()
  3235. self.assertEqual(msg, "Group added")
  3236. self.assertEqual(project.groups[0].group_name, "bar")
  3237. self.assertEqual(project.groups[1].group_name, "foo")
  3238. self.assertEqual(len(project.admin_groups), 2)
  3239. self.assertEqual(project.admin_groups[0].group_name, "bar")
  3240. self.assertEqual(project.admin_groups[1].group_name, "foo")
  3241. self.assertEqual(len(project.committer_groups), 2)
  3242. # Group already associated with the project
  3243. self.assertRaises(
  3244. pagure.exceptions.PagureException,
  3245. pagure.lib.query.add_group_to_project,
  3246. session=self.session,
  3247. project=project,
  3248. new_group="foo",
  3249. user="pingou",
  3250. )
  3251. # Update the access of group in the project
  3252. msg = pagure.lib.query.add_group_to_project(
  3253. session=self.session,
  3254. project=project,
  3255. new_group="foo",
  3256. user="pingou",
  3257. access="commit",
  3258. )
  3259. self.session.commit()
  3260. self.assertEqual(msg, "Group access updated")
  3261. self.assertEqual(project.groups[0].group_name, "bar")
  3262. self.assertEqual(project.groups[1].group_name, "foo")
  3263. self.assertEqual(len(project.admin_groups), 1)
  3264. self.assertEqual(project.admin_groups[0].group_name, "bar")
  3265. self.assertEqual(len(project.committer_groups), 2)
  3266. # Update the access of group in the project
  3267. msg = pagure.lib.query.add_group_to_project(
  3268. session=self.session,
  3269. project=project,
  3270. new_group="foo",
  3271. user="pingou",
  3272. access="ticket",
  3273. )
  3274. self.session.commit()
  3275. self.assertEqual(msg, "Group access updated")
  3276. self.assertEqual(project.groups[0].group_name, "bar")
  3277. self.assertEqual(project.groups[1].group_name, "foo")
  3278. self.assertEqual(len(project.admin_groups), 1)
  3279. self.assertEqual(project.admin_groups[0].group_name, "bar")
  3280. self.assertEqual(len(project.committer_groups), 1)
  3281. self.assertEqual(project.committer_groups[0].group_name, "bar")
  3282. def test_update_watch_status(self):
  3283. """ Test the update_watch_status method of pagure.lib.query. """
  3284. tests.create_projects(self.session)
  3285. project = pagure.lib.query._get_project(self.session, "test")
  3286. # User does not exist
  3287. self.assertRaises(
  3288. pagure.exceptions.PagureException,
  3289. pagure.lib.query.update_watch_status,
  3290. session=self.session,
  3291. project=project,
  3292. user="aavrug",
  3293. watch="1",
  3294. )
  3295. # Invalid watch status
  3296. self.assertRaises(
  3297. pagure.exceptions.PagureException,
  3298. pagure.lib.query.update_watch_status,
  3299. session=self.session,
  3300. project=project,
  3301. user="pingou",
  3302. watch="me fail",
  3303. )
  3304. # All good and when user selected reset watch option.
  3305. msg = pagure.lib.query.update_watch_status(
  3306. session=self.session, project=project, user="pingou", watch="-1"
  3307. )
  3308. self.session.commit()
  3309. self.assertEqual(msg, "Watch status is already reset")
  3310. # All good and when user selected watch issues option.
  3311. msg = pagure.lib.query.update_watch_status(
  3312. session=self.session, project=project, user="pingou", watch="1"
  3313. )
  3314. self.session.commit()
  3315. self.assertEqual(
  3316. msg, "You are now watching issues and PRs on this project"
  3317. )
  3318. # All good and when user selected unwatch option.
  3319. msg = pagure.lib.query.update_watch_status(
  3320. session=self.session, project=project, user="pingou", watch="0"
  3321. )
  3322. self.session.commit()
  3323. self.assertEqual(msg, "You are no longer watching this project")
  3324. # All good and when user seleted reset watch option.
  3325. msg = pagure.lib.query.update_watch_status(
  3326. session=self.session, project=project, user="pingou", watch="-1"
  3327. )
  3328. self.session.commit()
  3329. self.assertEqual(msg, "Watch status reset")
  3330. def test_get_watch_level_on_repo_invalid(self):
  3331. """ test the get_watch_level_on_repo method of pagure.lib.query. """
  3332. self.assertRaises(
  3333. RuntimeError,
  3334. pagure.lib.query.get_watch_level_on_repo,
  3335. session=self.session,
  3336. user="pingou",
  3337. repo=None,
  3338. repouser=None,
  3339. namespace=None,
  3340. )
  3341. def test_get_watch_level_on_repo(self):
  3342. """ Test the get_watch_level_on_repo method of pagure.lib.query. """
  3343. tests.create_projects(self.session)
  3344. self.test_add_group()
  3345. project = pagure.lib.query._get_project(self.session, "test")
  3346. project2 = pagure.lib.query._get_project(self.session, "test2")
  3347. # If user not logged in
  3348. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3349. session=self.session, user=None, repo="test"
  3350. )
  3351. self.assertEqual(watch_level, [])
  3352. # User does not exist
  3353. user = tests.FakeUser()
  3354. user.username = "aavrug"
  3355. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3356. session=self.session, user=user, repo="test"
  3357. )
  3358. self.assertEqual(watch_level, [])
  3359. # Invalid project
  3360. watch = pagure.lib.query.get_watch_level_on_repo(
  3361. session=self.session, user=user, repo="invalid"
  3362. )
  3363. self.assertFalse(watch)
  3364. pagure.lib.query.add_group_to_project(
  3365. session=self.session,
  3366. project=project,
  3367. new_group="foo",
  3368. user="pingou",
  3369. )
  3370. self.session.commit()
  3371. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3372. pagure.lib.query.add_user_to_group(
  3373. self.session,
  3374. username="foo",
  3375. group=group,
  3376. user="pingou",
  3377. is_admin=False,
  3378. )
  3379. self.session.commit()
  3380. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3381. # If user belongs to any group of that project
  3382. user.username = "foo"
  3383. msg = watch_level = pagure.lib.query.get_watch_level_on_repo(
  3384. session=self.session, user=user, repo="test"
  3385. )
  3386. self.assertEqual(watch_level, ["issues"])
  3387. # If user is the creator
  3388. user.username = "pingou"
  3389. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3390. session=self.session, user=user, repo="test"
  3391. )
  3392. self.assertEqual(watch_level, ["issues"])
  3393. # Entry into watchers table for issues and commits
  3394. msg = pagure.lib.query.update_watch_status(
  3395. session=self.session, project=project, user="pingou", watch="3"
  3396. )
  3397. self.session.commit()
  3398. self.assertEqual(
  3399. msg,
  3400. "You are now watching issues, PRs, and commits on this project",
  3401. )
  3402. # From watchers table
  3403. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3404. session=self.session, user=user, repo="test"
  3405. )
  3406. self.assertEqual(["issues", "commits"], watch_level)
  3407. # Make sure that when a user watches more than one repo explicitly
  3408. # they get the correct watch status
  3409. msg = pagure.lib.query.update_watch_status(
  3410. session=self.session, project=project2, user="pingou", watch="1"
  3411. )
  3412. self.session.commit()
  3413. self.assertEqual(
  3414. msg, "You are now watching issues and PRs on this project"
  3415. )
  3416. # From watchers table
  3417. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3418. session=self.session, user=user, repo="test2"
  3419. )
  3420. self.assertEqual(["issues"], watch_level)
  3421. # Entry into watchers table for just commits
  3422. msg = pagure.lib.query.update_watch_status(
  3423. session=self.session, project=project, user="pingou", watch="2"
  3424. )
  3425. self.session.commit()
  3426. self.assertEqual(msg, "You are now watching commits on this project")
  3427. # From watchers table
  3428. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3429. session=self.session, user=user, repo="test"
  3430. )
  3431. self.assertEqual(["commits"], watch_level)
  3432. # Entry into watchers table for issues
  3433. msg = pagure.lib.query.update_watch_status(
  3434. session=self.session, project=project, user="pingou", watch="1"
  3435. )
  3436. self.session.commit()
  3437. self.assertEqual(
  3438. msg, "You are now watching issues and PRs on this project"
  3439. )
  3440. # From watchers table
  3441. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3442. session=self.session, user=user, repo="test"
  3443. )
  3444. self.assertEqual(["issues"], watch_level)
  3445. # Entry into watchers table for no watching
  3446. msg = pagure.lib.query.update_watch_status(
  3447. session=self.session, project=project, user="pingou", watch="0"
  3448. )
  3449. self.session.commit()
  3450. self.assertEqual(msg, "You are no longer watching this project")
  3451. # From watchers table
  3452. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3453. session=self.session, user=user, repo="test"
  3454. )
  3455. self.assertEqual(watch_level, [])
  3456. # Add a contributor to the project
  3457. item = pagure.lib.model.User(
  3458. user="bar",
  3459. fullname="bar foo",
  3460. password="foo",
  3461. default_email="bar@bar.com",
  3462. )
  3463. self.session.add(item)
  3464. item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
  3465. self.session.add(item)
  3466. msg = pagure.lib.query.add_user_to_project(
  3467. session=self.session,
  3468. project=project,
  3469. new_user="bar",
  3470. user="pingou",
  3471. )
  3472. self.session.commit()
  3473. self.assertEqual(msg, "User added")
  3474. # Check if the new contributor is watching
  3475. user.username = "bar"
  3476. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3477. session=self.session, user=user, repo="test"
  3478. )
  3479. self.assertEqual(watch_level, ["issues"])
  3480. # wrong project
  3481. user.username = "bar"
  3482. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3483. session=self.session,
  3484. user=user,
  3485. repo="test",
  3486. namespace="somenamespace",
  3487. )
  3488. self.assertEqual(watch_level, [])
  3489. def test_user_watch_list(self):
  3490. """ test user watch list method of pagure.lib """
  3491. tests.create_projects(self.session)
  3492. # He should be watching
  3493. user = tests.FakeUser()
  3494. user.username = "pingou"
  3495. watch_list_objs = pagure.lib.query.user_watch_list(
  3496. session=self.session, user="pingou"
  3497. )
  3498. watch_list = [obj.name for obj in watch_list_objs]
  3499. self.assertEqual(watch_list, ["test", "test2", "test3"])
  3500. # Make pingou unwatch the test3 project
  3501. project = pagure.lib.query._get_project(
  3502. self.session, "test3", namespace="somenamespace"
  3503. )
  3504. msg = pagure.lib.query.update_watch_status(
  3505. session=self.session, project=project, user="pingou", watch="0"
  3506. )
  3507. self.session.commit()
  3508. self.assertEqual(msg, "You are no longer watching this project")
  3509. # Re-check the watch list
  3510. watch_list_objs = pagure.lib.query.user_watch_list(
  3511. session=self.session, user="pingou"
  3512. )
  3513. watch_list = [obj.name for obj in watch_list_objs]
  3514. self.assertEqual(watch_list, ["test", "test2"])
  3515. # He isn't in the db, thus not watching anything
  3516. user.username = "vivek"
  3517. watch_list_objs = pagure.lib.query.user_watch_list(
  3518. session=self.session, user="vivek"
  3519. )
  3520. watch_list = [obj.name for obj in watch_list_objs]
  3521. self.assertEqual(watch_list, [])
  3522. # He shouldn't be watching anything
  3523. user.username = "foo"
  3524. watch_list_objs = pagure.lib.query.user_watch_list(
  3525. session=self.session, user="foo"
  3526. )
  3527. watch_list = [obj.name for obj in watch_list_objs]
  3528. self.assertEqual(watch_list, [])
  3529. @patch("pagure.lib.tasks.update_git", MagicMock(return_value=True))
  3530. @patch("pagure.lib.tasks.sync_pull_ref", MagicMock(return_value=True))
  3531. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3532. @patch("pagure.pfmarkdown._commit_exists", MagicMock(return_value=True))
  3533. def test_text2markdown(self):
  3534. """ Test the text2markdown method in pagure.lib.query. """
  3535. pagure.config.config["TESTING"] = True
  3536. pagure.config.config["SERVER_NAME"] = "localhost.localdomain"
  3537. # This creates:
  3538. # project: test
  3539. # fork: pingou/test
  3540. # PR#1 to project test
  3541. self.test_new_pull_request()
  3542. # create an issue (will be #2) in 'test' project
  3543. repo = pagure.lib.query._get_project(self.session, "test")
  3544. iss = pagure.lib.query.new_issue(
  3545. issue_id=2,
  3546. session=self.session,
  3547. repo=repo,
  3548. title="test issue",
  3549. content="content test issue",
  3550. user="pingou",
  3551. )
  3552. self.session.commit()
  3553. self.assertEqual(iss.id, 2)
  3554. self.assertEqual(iss.title, "test issue")
  3555. # create PR#2 to project pingou/test
  3556. forked_repo = pagure.lib.query._get_project(
  3557. self.session, "test", user="pingou"
  3558. )
  3559. req = pagure.lib.query.new_pull_request(
  3560. requestid=2,
  3561. session=self.session,
  3562. repo_from=forked_repo,
  3563. branch_from="master",
  3564. repo_to=forked_repo,
  3565. branch_to="master",
  3566. title="test pull-request in fork",
  3567. user="pingou",
  3568. )
  3569. self.session.commit()
  3570. self.assertEqual(req.id, 2)
  3571. self.assertEqual(req.title, "test pull-request in fork")
  3572. # Create the project ns/test
  3573. item = pagure.lib.model.Project(
  3574. user_id=1, # pingou
  3575. name="test3",
  3576. namespace="ns",
  3577. description="test project #1",
  3578. hook_token="aaabbbcccdd",
  3579. )
  3580. item.close_status = [
  3581. "Invalid",
  3582. "Insufficient data",
  3583. "Fixed",
  3584. "Duplicate",
  3585. ]
  3586. self.session.add(item)
  3587. self.session.commit()
  3588. iss = pagure.lib.query.new_issue(
  3589. issue_id=4,
  3590. session=self.session,
  3591. repo=item,
  3592. title="test issue",
  3593. content="content test issue",
  3594. user="pingou",
  3595. )
  3596. self.session.commit()
  3597. self.assertEqual(iss.id, 4)
  3598. self.assertEqual(iss.title, "test issue")
  3599. # Fork ns/test to pingou
  3600. item = pagure.lib.model.Project(
  3601. user_id=1, # pingou
  3602. name="test",
  3603. namespace="ns",
  3604. description="Forked namespaced test project #1",
  3605. is_fork=True,
  3606. parent_id=item.id,
  3607. hook_token="aaabbbrrrbb",
  3608. )
  3609. self.session.add(item)
  3610. self.session.commit()
  3611. iss = pagure.lib.query.new_issue(
  3612. issue_id=7,
  3613. session=self.session,
  3614. repo=item,
  3615. title="test issue #7",
  3616. content="content test issue #7 in forked repo",
  3617. user="pingou",
  3618. )
  3619. self.session.commit()
  3620. self.assertEqual(iss.id, 7)
  3621. self.assertEqual(iss.title, "test issue #7")
  3622. iss = pagure.lib.query.new_issue(
  3623. issue_id=8,
  3624. session=self.session,
  3625. repo=item,
  3626. title="private issue #8",
  3627. content="Private content test issue #8 in forked repo",
  3628. user="pingou",
  3629. private=True,
  3630. )
  3631. self.session.commit()
  3632. self.assertEqual(iss.id, 8)
  3633. self.assertEqual(iss.title, "private issue #8")
  3634. # newer bleach allow to customize the protocol supported
  3635. import bleach
  3636. bleach_v = bleach.__version__.split(".")
  3637. for idx, val in enumerate(bleach_v):
  3638. try:
  3639. val = int(val)
  3640. except ValueError:
  3641. pass
  3642. bleach_v[idx] = val
  3643. # old markdown generate other html
  3644. import markdown
  3645. try:
  3646. markdown_v = markdown.__version__.version_info
  3647. except AttributeError: # pragma: no cover
  3648. markdown_v = markdown.__version_info__
  3649. # python-markdown >= 3.2.0 returns to the old behavior on img tag trailing slash
  3650. old_markdown = markdown_v < (2, 6, 0) or markdown_v >= (3, 2, 0)
  3651. mk_321 = markdown_v >= (3, 2, 0)
  3652. texts = [
  3653. "foo bar test#1 see?",
  3654. "foo bar pingou/test#2 I mean, really",
  3655. "foo bar fork/pingou/test#2 bouza!",
  3656. "foo bar forks/pingou/test#2 bouza!",
  3657. "foo bar ns/test3#4 bouza!",
  3658. "foo bar fork/user/ns/test#5 bouza!",
  3659. "foo bar fork/pingou/ns/test#7 bouza!",
  3660. "test#1 bazinga!",
  3661. "pingou opened the PR forks/pingou/test#2",
  3662. "fork/pingou/ns/test#8 is private",
  3663. "implicit link to #1",
  3664. "implicit link .#1. with non-whitespace, non-word characters",
  3665. "#2 - implicit link at start of line",
  3666. "#2. implicit link at start of line with no whitespace after",
  3667. "#regular header",
  3668. "#34 looks like an implicit link, but no issue 34",
  3669. "pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd",
  3670. "irc://pagure.io",
  3671. "ircs://pagure.io",
  3672. "http://pagure.io",
  3673. "https://pagure.io",
  3674. "<https://pagure.io/pagure>",
  3675. "~~foo~~",
  3676. "~~foo bar~~",
  3677. "~~[BZ#1435310](https://bugzilla.redhat.com/1435310)~~",
  3678. "~~[BZ#1435310](https://bugzilla.redhat.com/1435310) avc denial "
  3679. "during F26AH boot 'error_name=org.freedesktop.systemd1."
  3680. "NoSuchDynamicUser'~~",
  3681. "``~~foo bar~~``",
  3682. "~~foo bar~~ and ~~another ~~",
  3683. "lets mention @pingou",
  3684. "@pingou at start of line",
  3685. "but not someone@pingou.com",
  3686. "[![Fedora_infinity_small.png]"
  3687. "(/test/issue/raw/Fedora_infinity_small.png)]"
  3688. "(/test/issue/raw/Fedora_infinity_small.png)",
  3689. ]
  3690. expected = [
  3691. # 'foo bar test#1 see?',
  3692. '<div class="markdown"><p>foo bar <a href="/test/pull-request/1"'
  3693. ' title="[Open] test pull-request">test#1</a> see?</p></div>',
  3694. # 'foo bar pingou/test#2 I mean, really', -- unknown namespace
  3695. '<div class="markdown"><p>foo bar pingou/test#2 I mean, really</p></div>',
  3696. # 'foo bar fork/pingou/test#2 bouza!',
  3697. '<div class="markdown"><p>foo bar <a href="/fork/'
  3698. 'pingou/test/pull-request/2" title="[Open] test pull-request in fork">'
  3699. "pingou/test#2</a> bouza!</p></div>",
  3700. # 'foo bar forks/pingou/test#2 bouza!', -- the 's' doesn't matter
  3701. '<div class="markdown"><p>foo bar <a href="/fork/'
  3702. 'pingou/test/pull-request/2" title="[Open] test pull-request in fork">'
  3703. "pingou/test#2</a> bouza!</p></div>",
  3704. # 'foo bar ns/test3#4 bouza!',
  3705. '<div class="markdown"><p>foo bar <a href="/ns/test3/issue/4"'
  3706. ' title="[Open] test issue">ns/test3#4</a> bouza!</p></div>',
  3707. # 'foo bar fork/user/ns/test#5 bouza!', -- unknown fork
  3708. '<div class="markdown"><p>foo bar user/ns/test#5 bouza!</p></div>',
  3709. # 'foo bar fork/pingou/ns/test#7 bouza!',
  3710. '<div class="markdown"><p>foo bar <a href="/fork/pingou/ns/test/issue/7"'
  3711. ' title="[Open] test issue #7">'
  3712. "pingou/ns/test#7</a> bouza!</p></div>",
  3713. # 'test#1 bazinga!',
  3714. '<div class="markdown"><p><a href="/test/pull-request/1" '
  3715. 'title="[Open] test pull-request">test#1</a> bazinga!</p></div>',
  3716. # 'pingou opened the PR forks/pingou/test#2'
  3717. '<div class="markdown"><p>pingou opened the PR <a href="/fork/pingou/test/pull-request/2" '
  3718. 'title="[Open] test pull-request in fork">pingou/test#2</a></p></div>',
  3719. # 'fork/pingou/ns/test#8 is private',
  3720. '<div class="markdown"><p><a href="/fork/pingou/ns/test/issue/8" '
  3721. 'title="Private issue">pingou/ns/test#8</a> is private</p></div>',
  3722. # 'implicit link to #1',
  3723. '<div class="markdown"><p>implicit link to <a href="/test/pull-request/1" title="[Open] test pull-request">#1</a></p></div>',
  3724. # 'implicit link .#1. with non-whitespace, non-word characters',
  3725. '<div class="markdown"><p>implicit link .<a href="/test/pull-request/1" title="[Open] test pull-request">#1</a>. with non-whitespace, non-word characters</p></div>',
  3726. # '#2 - implicit link at start of line',
  3727. '<div class="markdown"><p><a href="/test/issue/2" title="[Open] test issue">#2</a> - implicit link at start of line</p></div>',
  3728. # '#2. implicit link at start of line with no whitespace after',
  3729. '<div class="markdown"><p><a href="/test/issue/2" title="[Open] test issue">#2</a>. implicit link at start of line with no whitespace after</p></div>',
  3730. # '#regular header',
  3731. '<div class="markdown"><h1>regular header</h1></div>',
  3732. # '#34 looks like an implicit link, but no issue 34',
  3733. '<div class="markdown"><h1>34 looks like an implicit link, but no issue 34</h1></div>',
  3734. # 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  3735. '<div class="markdown"><p>pingou committed on <a href="/test/c/'
  3736. '9364354a4555ba17aa60f0dc844d70b74eb1aecd" '
  3737. 'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
  3738. ">test#9364354a4555ba17aa60f0dc844d70b74eb1aecd</a></p></div>",
  3739. # 'irc://pagure.io'
  3740. '<div class="markdown"><p><a href="irc://pagure.io">irc://pagure.io</a></p></div>',
  3741. # 'ircs://pagure.io' - This is getting cleaned by python-bleach
  3742. # and the version 1.4.3 that we have won't let us adjust the
  3743. # list of supported protocols
  3744. # '<p><a href="ircs://pagure.io">ircs://pagure.io</a></p>',
  3745. '<div class="markdown"><p><a href="ircs://pagure.io">ircs://pagure.io</a></p></div>'
  3746. if tuple(bleach_v) >= (1, 5, 0)
  3747. else '<div class="markdown"><p><a>ircs://pagure.io</a></p></div>',
  3748. # 'http://pagure.io'
  3749. '<div class="markdown"><p><a href="http://pagure.io">http://pagure.io</a></p></div>',
  3750. # 'https://pagure.io'
  3751. '<div class="markdown"><p><a href="https://pagure.io">https://pagure.io</a></p></div>',
  3752. # '<https://pagure.io/pagure>'
  3753. '<div class="markdown"><p><a href="https://pagure.io/pagure">https://pagure.io/pagure'
  3754. "</a></p></div>",
  3755. # '~~foo~~'
  3756. '<div class="markdown"><p><del>foo</del></p></div>',
  3757. # '~~foo bar~~'
  3758. '<div class="markdown"><p><del>foo bar</del></p></div>',
  3759. # '~~[BZ#1435310](https://bugzilla.redhat.com/1435310)~~'
  3760. '<div class="markdown"><p><del><a href="https://bugzilla.redhat.com/1435310">'
  3761. "BZ#1435310</a></del></p></div>",
  3762. # '~~[BZ#1435310](https://bugzilla.redhat.com/1435310) avc
  3763. # denial during F26AH boot 'error_name=org.freedesktop.systemd1
  3764. # .NoSuchDynamicUser~~'
  3765. '<div class="markdown"><p><del><a href="https://bugzilla.redhat.com/1435310">'
  3766. "BZ#1435310</a> avc denial during F26AH boot 'error_name="
  3767. "org.freedesktop.systemd1.NoSuchDynamicUser'</del></p></div>",
  3768. # '``~~foo bar~~``'
  3769. '<div class="markdown"><p><code>~~foo bar~~</code></p></div>',
  3770. # '~~foo bar~~ and ~~another ~~',
  3771. '<div class="markdown"><p><del>foo bar</del> and <del>another </del></p></div>',
  3772. # 'lets mention @pingou',
  3773. '<div class="markdown"><p>lets mention <a href="http://localhost.localdomain/user/pingou">@pingou</a></p></div>',
  3774. # '@pingou at start of line',
  3775. '<div class="markdown"><p><a href="http://localhost.localdomain/user/pingou">@pingou</a> at start of line</p></div>',
  3776. # 'but not someone@pingou.com',
  3777. '<div class="markdown"><p>but not someone@pingou.com</p></div>',
  3778. ]
  3779. if mk_321:
  3780. print("**** Markdown 3.2.1+ behavior")
  3781. expected.append(
  3782. # '[![Fedora_infinity_small.png]'
  3783. # '(/test/issue/raw/Fedora_infinity_small.png)]'
  3784. # '(/test/issue/raw/Fedora_infinity_small.png)',
  3785. '<div class="markdown"><p><a href="/test/issue/raw/Fedora_infinity_small.png"><span>'
  3786. '<img alt="Fedora_infinity_small.png" class="lazyload" '
  3787. 'data-src="/test/issue/raw/Fedora_infinity_small.png" src="">'
  3788. "<noscript>"
  3789. '&lt;img alt="Fedora_infinity_small.png" '
  3790. 'src="/test/issue/raw/Fedora_infinity_small.png" /&gt;'
  3791. "</noscript></span></a></p></div>"
  3792. )
  3793. elif old_markdown:
  3794. print("**** Old markdown behavior")
  3795. expected.append(
  3796. # '[![Fedora_infinity_small.png]'
  3797. # '(/test/issue/raw/Fedora_infinity_small.png)]'
  3798. # '(/test/issue/raw/Fedora_infinity_small.png)',
  3799. '<div class="markdown"><p><a href="/test/issue/raw/Fedora_infinity_small.png"><span>'
  3800. '<img alt="Fedora_infinity_small.png" class="lazyload" '
  3801. 'data-src="/test/issue/raw/Fedora_infinity_small.png" src="">'
  3802. "<noscript>"
  3803. '<img alt="Fedora_infinity_small.png" '
  3804. 'src="/test/issue/raw/Fedora_infinity_small.png" />'
  3805. "</noscript></span></a></p></div>"
  3806. )
  3807. else:
  3808. print("**** Not old but no longer new markdown")
  3809. expected.append(
  3810. # '[![Fedora_infinity_small.png]'
  3811. # '(/test/issue/raw/Fedora_infinity_small.png)]'
  3812. # '(/test/issue/raw/Fedora_infinity_small.png)',
  3813. '<div class="markdown"><p><a href="/test/issue/raw/Fedora_infinity_small.png"><span>'
  3814. '<img alt="Fedora_infinity_small.png" class="lazyload" '
  3815. 'data-src="/test/issue/raw/Fedora_infinity_small.png" src="">'
  3816. "<noscript>"
  3817. '<img alt="Fedora_infinity_small.png" '
  3818. 'src="/test/issue/raw/Fedora_infinity_small.png">'
  3819. "</noscript></span></a></p></div>"
  3820. )
  3821. with self.app.application.app_context() as ctx:
  3822. ctx.g.session = self.session
  3823. with ctx.app.test_request_context() as reqctx:
  3824. reqctx.request.url_root = "http://localhost.localdomain/"
  3825. reqctx.request.url = (
  3826. "http://localhost.localdomain/test/issue/69"
  3827. )
  3828. reqctx.request.args = Mock()
  3829. reqctx.request.args.get = Mock(return_value=None)
  3830. for idx, text in enumerate(texts):
  3831. print(text)
  3832. html = pagure.lib.query.text2markdown(text)
  3833. self.assertEqual(html, expected[idx])
  3834. def test_text2markdown_exception(self):
  3835. """ Test the text2markdown method in pagure.lib.query. """
  3836. text = "test#1 bazinga!"
  3837. expected_html = "test#1 bazinga!"
  3838. with self.app.application.app_context() as ctx:
  3839. html = pagure.lib.query.text2markdown(text)
  3840. self.assertEqual(html, expected_html)
  3841. def test_text2markdown_empty_string(self):
  3842. """ Test the text2markdown method in pagure.lib.query. """
  3843. text = ""
  3844. expected_html = ""
  3845. html = pagure.lib.query.text2markdown(text)
  3846. self.assertEqual(html, expected_html)
  3847. def test_get_access_levels(self):
  3848. """ Test the get_access_levels method in pagure.lib """
  3849. acls = pagure.lib.query.get_access_levels(self.session)
  3850. self.assertEqual(
  3851. sorted(["admin", "collaborator", "commit", "ticket"]), sorted(acls)
  3852. )
  3853. def test_get_project_users(self):
  3854. """ Test the get_project_users method when combine is True
  3855. """
  3856. tests.create_projects(self.session)
  3857. project = pagure.lib.query.get_authorized_project(
  3858. self.session, project_name="test"
  3859. )
  3860. # Default value of combine is True
  3861. # which means the an admin is a user, committer as well
  3862. # and a committer is also a user
  3863. # and a user is just a user
  3864. users = project.get_project_users(access="admin")
  3865. # Only pingou is the admin as of now
  3866. # But, he is the creator and
  3867. # the creator of the project is not listed in user_projects
  3868. # table. Thus, get_projec_users won't return him as an admin
  3869. # He has all the access of an admin though
  3870. self.assertEqual(len(users), 0)
  3871. self.assertEqual(project.user.username, "pingou")
  3872. # Wrong access level, should raise Accesslevelnotfound exception
  3873. self.assertRaises(
  3874. pagure.exceptions.AccessLevelNotFound,
  3875. project.get_project_users,
  3876. access="owner",
  3877. )
  3878. # Let's add a new user to the project, 'foo'
  3879. # By default, if no access is specified, he becomes an admin
  3880. msg = pagure.lib.query.add_user_to_project(
  3881. self.session, project=project, new_user="foo", user="pingou"
  3882. )
  3883. self.session.commit()
  3884. # since, he is an admin, the msg should be 'User added'
  3885. self.assertEqual(msg, "User added")
  3886. project = pagure.lib.query.get_authorized_project(
  3887. self.session, project_name="test"
  3888. )
  3889. users = project.get_project_users(access="admin")
  3890. self.assertEqual(len(users), 1)
  3891. self.assertEqual(users[0].username, "foo")
  3892. # foo should be a committer as well, since he is an admin
  3893. users = project.get_project_users(access="commit")
  3894. self.assertEqual(len(users), 1)
  3895. self.assertEqual(users[0].username, "foo")
  3896. # the admin also has ticket access
  3897. users = project.get_project_users(access="ticket")
  3898. self.assertEqual(len(users), 1)
  3899. self.assertEqual(users[0].username, "foo")
  3900. # let's update the access of foo to 'committer'
  3901. msg = pagure.lib.query.add_user_to_project(
  3902. self.session,
  3903. project=project,
  3904. new_user="foo",
  3905. user="pingou",
  3906. access="commit",
  3907. )
  3908. self.session.commit()
  3909. self.assertEqual(msg, "User access updated")
  3910. project = pagure.lib.query.get_authorized_project(
  3911. self.session, project_name="test"
  3912. )
  3913. # No admin now, even though pingou the creator is there
  3914. users = project.get_project_users(access="admin")
  3915. self.assertEqual(len(users), 0)
  3916. users = project.get_project_users(access="commit")
  3917. # foo is the committer currently
  3918. self.assertEqual(len(users), 1)
  3919. self.assertEqual(users[0].username, "foo")
  3920. users = project.get_project_users(access="ticket")
  3921. # foo also has ticket rights
  3922. self.assertEqual(len(users), 1)
  3923. self.assertEqual(users[0].username, "foo")
  3924. # let's update the access of foo to 'ticket'
  3925. msg = pagure.lib.query.add_user_to_project(
  3926. self.session,
  3927. project=project,
  3928. new_user="foo",
  3929. user="pingou",
  3930. access="ticket",
  3931. )
  3932. self.session.commit()
  3933. self.assertEqual(msg, "User access updated")
  3934. project = pagure.lib.query.get_authorized_project(
  3935. self.session, project_name="test"
  3936. )
  3937. # No admin now, even though pingou the creator is there
  3938. users = project.get_project_users(access="admin")
  3939. self.assertEqual(len(users), 0)
  3940. users = project.get_project_users(access="commit")
  3941. # foo deosn't have commit rights now
  3942. self.assertEqual(len(users), 0)
  3943. users = project.get_project_users(access="ticket")
  3944. # foo does have tickets right though
  3945. self.assertEqual(len(users), 1)
  3946. self.assertEqual(users[0].username, "foo")
  3947. def test_get_project_users_combine_false(self):
  3948. """ Test the get_project_users method when combine is False
  3949. """
  3950. tests.create_projects(self.session)
  3951. project = pagure.lib.query.get_authorized_project(
  3952. self.session, project_name="test"
  3953. )
  3954. # Let's add a new user to the project, 'foo'
  3955. # By default, if no access is specified, he becomes an admin
  3956. msg = pagure.lib.query.add_user_to_project(
  3957. self.session, project=project, new_user="foo", user="pingou"
  3958. )
  3959. self.session.commit()
  3960. # since, he is an admin, the msg should be 'User added'
  3961. self.assertEqual(msg, "User added")
  3962. # only one admin
  3963. users = project.get_project_users(access="admin", combine=False)
  3964. self.assertEqual(len(users), 1)
  3965. self.assertEqual(users[0].username, "foo")
  3966. # No user with only commit access
  3967. users = project.get_project_users(access="commit", combine=False)
  3968. self.assertEqual(len(users), 0)
  3969. # No user with only ticket access
  3970. users = project.get_project_users(access="ticket", combine=False)
  3971. self.assertEqual(len(users), 0)
  3972. # Update the access level of foo user to commit
  3973. msg = pagure.lib.query.add_user_to_project(
  3974. self.session,
  3975. project=project,
  3976. new_user="foo",
  3977. user="pingou",
  3978. access="commit",
  3979. )
  3980. self.session.commit()
  3981. self.assertEqual(msg, "User access updated")
  3982. # He is just a committer
  3983. project = pagure.lib.query.get_authorized_project(
  3984. self.session, project_name="test"
  3985. )
  3986. users = project.get_project_users(access="admin", combine=False)
  3987. self.assertEqual(len(users), 0)
  3988. # He is just a committer
  3989. users = project.get_project_users(access="commit", combine=False)
  3990. self.assertEqual(len(users), 1)
  3991. self.assertEqual(users[0].username, "foo")
  3992. # He is just a committer
  3993. users = project.get_project_users(access="ticket", combine=False)
  3994. self.assertEqual(len(users), 0)
  3995. # Update the access level of foo user to ticket
  3996. msg = pagure.lib.query.add_user_to_project(
  3997. self.session,
  3998. project=project,
  3999. new_user="foo",
  4000. user="pingou",
  4001. access="ticket",
  4002. )
  4003. self.session.commit()
  4004. self.assertEqual(msg, "User access updated")
  4005. # He is just a ticketer
  4006. project = pagure.lib.query.get_authorized_project(
  4007. self.session, project_name="test"
  4008. )
  4009. users = project.get_project_users(access="admin", combine=False)
  4010. self.assertEqual(len(users), 0)
  4011. # He is just a ticketer
  4012. users = project.get_project_users(access="commit", combine=False)
  4013. self.assertEqual(len(users), 0)
  4014. # He is just a ticketer
  4015. users = project.get_project_users(access="ticket", combine=False)
  4016. self.assertEqual(len(users), 1)
  4017. self.assertEqual(users[0].username, "foo")
  4018. def test_get_project_groups(self):
  4019. """ Test the get_project_groups method when combine is True
  4020. """
  4021. # Create some projects
  4022. tests.create_projects(self.session)
  4023. # Create a group in database
  4024. msg = pagure.lib.query.add_group(
  4025. self.session,
  4026. group_name="JL",
  4027. display_name="Justice League",
  4028. description="Nope, it's not JLA anymore",
  4029. group_type="user",
  4030. user="foo",
  4031. is_admin=False,
  4032. blacklist=pagure.config.config.get("BLACKLISTED_PROJECTS"),
  4033. )
  4034. self.assertEqual(msg, "User `foo` added to the group `JL`.")
  4035. # Add the group to project we just created, test
  4036. # First add it as an admin
  4037. project = pagure.lib.query.get_authorized_project(
  4038. self.session, project_name="test"
  4039. )
  4040. msg = pagure.lib.query.add_group_to_project(
  4041. self.session, project=project, new_group="JL", user="pingou"
  4042. )
  4043. self.session.commit()
  4044. self.assertEqual(msg, "Group added")
  4045. # Now, the group is an admin in the project
  4046. # so, it must have access to everything
  4047. project = pagure.lib.query.get_authorized_project(
  4048. self.session, project_name="test"
  4049. )
  4050. groups = project.get_project_groups(access="admin")
  4051. self.assertEqual(len(groups), 1)
  4052. self.assertEqual(groups[0].display_name, "Justice League")
  4053. self.assertEqual(len(project.admin_groups), 1)
  4054. self.assertEqual(
  4055. project.admin_groups[0].display_name, "Justice League"
  4056. )
  4057. # The group should be committer as well
  4058. groups = project.get_project_groups(access="commit")
  4059. self.assertEqual(len(groups), 1)
  4060. self.assertEqual(groups[0].display_name, "Justice League")
  4061. self.assertEqual(len(project.committer_groups), 1)
  4062. self.assertEqual(
  4063. project.committer_groups[0].display_name, "Justice League"
  4064. )
  4065. # The group should be ticketer as well
  4066. groups = project.get_project_groups(access="ticket")
  4067. self.assertEqual(len(groups), 1)
  4068. self.assertEqual(groups[0].display_name, "Justice League")
  4069. self.assertEqual(len(project.groups), 1)
  4070. self.assertEqual(project.groups[0].display_name, "Justice League")
  4071. # Update the access level of the group, JL to commit
  4072. project = pagure.lib.query.get_authorized_project(
  4073. self.session, project_name="test"
  4074. )
  4075. msg = pagure.lib.query.add_group_to_project(
  4076. self.session,
  4077. project=project,
  4078. new_group="JL",
  4079. user="pingou",
  4080. access="commit",
  4081. )
  4082. self.session.commit()
  4083. self.assertEqual(msg, "Group access updated")
  4084. # It shouldn't be an admin
  4085. project = pagure.lib.query.get_authorized_project(
  4086. self.session, project_name="test"
  4087. )
  4088. groups = project.get_project_groups(access="admin")
  4089. self.assertEqual(len(groups), 0)
  4090. self.assertEqual(len(project.admin_groups), 0)
  4091. # It is a committer
  4092. groups = project.get_project_groups(access="commit")
  4093. self.assertEqual(len(groups), 1)
  4094. self.assertEqual(groups[0].display_name, "Justice League")
  4095. self.assertEqual(len(project.committer_groups), 1)
  4096. self.assertEqual(
  4097. project.committer_groups[0].display_name, "Justice League"
  4098. )
  4099. # The group should be ticketer as well
  4100. groups = project.get_project_groups(access="ticket")
  4101. self.assertEqual(len(groups), 1)
  4102. self.assertEqual(groups[0].display_name, "Justice League")
  4103. self.assertEqual(len(project.groups), 1)
  4104. self.assertEqual(project.groups[0].display_name, "Justice League")
  4105. # Update the access of group JL to ticket
  4106. msg = pagure.lib.query.add_group_to_project(
  4107. self.session,
  4108. project=project,
  4109. new_group="JL",
  4110. user="pingou",
  4111. access="ticket",
  4112. )
  4113. self.session.commit()
  4114. self.assertEqual(msg, "Group access updated")
  4115. # It is not an admin
  4116. project = pagure.lib.query.get_authorized_project(
  4117. self.session, project_name="test"
  4118. )
  4119. groups = project.get_project_groups(access="admin")
  4120. self.assertEqual(len(groups), 0)
  4121. self.assertEqual(len(project.admin_groups), 0)
  4122. # The group shouldn't be a committer
  4123. groups = project.get_project_groups(access="commit")
  4124. self.assertEqual(len(groups), 0)
  4125. self.assertEqual(len(project.committer_groups), 0)
  4126. # The group should be ticketer
  4127. groups = project.get_project_groups(access="ticket")
  4128. self.assertEqual(len(groups), 1)
  4129. self.assertEqual(groups[0].display_name, "Justice League")
  4130. self.assertEqual(len(project.groups), 1)
  4131. self.assertEqual(project.groups[0].display_name, "Justice League")
  4132. def test_get_project_groups_combine_false(self):
  4133. """ Test the get_project_groups method when combine is False
  4134. """
  4135. # Create some projects
  4136. tests.create_projects(self.session)
  4137. # Create a group in database
  4138. msg = pagure.lib.query.add_group(
  4139. self.session,
  4140. group_name="JL",
  4141. display_name="Justice League",
  4142. description="Nope, it's not JLA anymore",
  4143. group_type="user",
  4144. user="foo",
  4145. is_admin=False,
  4146. blacklist=pagure.config.config.get("BLACKLISTED_PROJECTS"),
  4147. )
  4148. self.assertEqual(msg, "User `foo` added to the group `JL`.")
  4149. # Add the group to project we just created, test
  4150. # First add it as an admin
  4151. project = pagure.lib.query.get_authorized_project(
  4152. self.session, project_name="test"
  4153. )
  4154. msg = pagure.lib.query.add_group_to_project(
  4155. self.session, project=project, new_group="JL", user="pingou"
  4156. )
  4157. self.session.commit()
  4158. self.assertEqual(msg, "Group added")
  4159. # Now, the group is an admin in the project
  4160. # so, it must have access to everything
  4161. project = pagure.lib.query.get_authorized_project(
  4162. self.session, project_name="test"
  4163. )
  4164. groups = project.get_project_groups(access="admin", combine=False)
  4165. self.assertEqual(len(groups), 1)
  4166. self.assertEqual(groups[0].display_name, "Justice League")
  4167. self.assertEqual(len(project.admin_groups), 1)
  4168. self.assertEqual(
  4169. project.admin_groups[0].display_name, "Justice League"
  4170. )
  4171. # The group shoudn't be a committer
  4172. groups = project.get_project_groups(access="commit", combine=False)
  4173. self.assertEqual(len(groups), 0)
  4174. # The group shoudn't be a ticketer
  4175. groups = project.get_project_groups(access="ticket", combine=False)
  4176. self.assertEqual(len(groups), 0)
  4177. # Update the access level of the group, JL to commit
  4178. project = pagure.lib.query.get_authorized_project(
  4179. self.session, project_name="test"
  4180. )
  4181. msg = pagure.lib.query.add_group_to_project(
  4182. self.session,
  4183. project=project,
  4184. new_group="JL",
  4185. user="pingou",
  4186. access="commit",
  4187. )
  4188. self.session.commit()
  4189. self.assertEqual(msg, "Group access updated")
  4190. # It shouldn't be an admin
  4191. project = pagure.lib.query.get_authorized_project(
  4192. self.session, project_name="test"
  4193. )
  4194. groups = project.get_project_groups(access="admin", combine=False)
  4195. self.assertEqual(len(groups), 0)
  4196. # It is a committer
  4197. groups = project.get_project_groups(access="commit", combine=False)
  4198. self.assertEqual(len(groups), 1)
  4199. self.assertEqual(groups[0].display_name, "Justice League")
  4200. self.assertEqual(len(project.committer_groups), 1)
  4201. self.assertEqual(
  4202. project.committer_groups[0].display_name, "Justice League"
  4203. )
  4204. # The group shouldn't be ticketer
  4205. groups = project.get_project_groups(access="ticket", combine=False)
  4206. self.assertEqual(len(groups), 0)
  4207. # Update the access of group JL to ticket
  4208. msg = pagure.lib.query.add_group_to_project(
  4209. self.session,
  4210. project=project,
  4211. new_group="JL",
  4212. user="pingou",
  4213. access="ticket",
  4214. )
  4215. self.session.commit()
  4216. self.assertEqual(msg, "Group access updated")
  4217. # It is not an admin
  4218. project = pagure.lib.query.get_authorized_project(
  4219. self.session, project_name="test"
  4220. )
  4221. groups = project.get_project_groups(access="admin", combine=False)
  4222. self.assertEqual(len(groups), 0)
  4223. # The group shouldn't be a committer
  4224. groups = project.get_project_groups(access="commit", combine=False)
  4225. self.assertEqual(len(groups), 0)
  4226. # The group should be ticketer
  4227. groups = project.get_project_groups(access="ticket", combine=False)
  4228. self.assertEqual(len(groups), 1)
  4229. self.assertEqual(groups[0].display_name, "Justice League")
  4230. self.assertEqual(len(project.groups), 1)
  4231. self.assertEqual(project.groups[0].display_name, "Justice League")
  4232. def test_get_obj_access_user(self):
  4233. """ Test the get_obj_access method of pagure.lib
  4234. for model.User object """
  4235. # Create the projects
  4236. tests.create_projects(self.session)
  4237. # Add a user object - make him an admin first
  4238. project = pagure.lib.query.get_authorized_project(
  4239. self.session, project_name="test"
  4240. )
  4241. msg = pagure.lib.query.add_user_to_project(
  4242. self.session, project=project, new_user="foo", user="pingou"
  4243. )
  4244. self.session.commit()
  4245. self.assertEqual(msg, "User added")
  4246. user = pagure.lib.query.get_user(self.session, key="foo")
  4247. # He should be an admin
  4248. access_obj = pagure.lib.query.get_obj_access(
  4249. self.session, project_obj=project, obj=user
  4250. )
  4251. self.assertEqual(access_obj.access, "admin")
  4252. # Update and check for commit access
  4253. msg = pagure.lib.query.add_user_to_project(
  4254. self.session,
  4255. project=project,
  4256. new_user="foo",
  4257. user="pingou",
  4258. access="commit",
  4259. )
  4260. self.session.commit()
  4261. self.assertEqual(msg, "User access updated")
  4262. project = pagure.lib.query.get_authorized_project(
  4263. self.session, project_name="test"
  4264. )
  4265. # He should be a committer
  4266. access_obj = pagure.lib.query.get_obj_access(
  4267. self.session, project_obj=project, obj=user
  4268. )
  4269. self.assertEqual(access_obj.access, "commit")
  4270. # Update and check for ticket access
  4271. msg = pagure.lib.query.add_user_to_project(
  4272. self.session,
  4273. project=project,
  4274. new_user="foo",
  4275. user="pingou",
  4276. access="ticket",
  4277. )
  4278. self.session.commit()
  4279. self.assertEqual(msg, "User access updated")
  4280. project = pagure.lib.query.get_authorized_project(
  4281. self.session, project_name="test"
  4282. )
  4283. # He should be a ticketer
  4284. access_obj = pagure.lib.query.get_obj_access(
  4285. self.session, project_obj=project, obj=user
  4286. )
  4287. self.assertEqual(access_obj.access, "ticket")
  4288. def test_get_obj_access_group(self):
  4289. """ Test the get_obj_access method of pagure.lib
  4290. for model.PagureGroup object """
  4291. # Create the projects
  4292. tests.create_projects(self.session)
  4293. # Create a group in database
  4294. msg = pagure.lib.query.add_group(
  4295. self.session,
  4296. group_name="JL",
  4297. display_name="Justice League",
  4298. description="Nope, it's not JLA anymore",
  4299. group_type="user",
  4300. user="foo",
  4301. is_admin=False,
  4302. blacklist=pagure.config.config.get("BLACKLISTED_PROJECTS"),
  4303. )
  4304. self.assertEqual(msg, "User `foo` added to the group `JL`.")
  4305. # Add a group object - make him an admin first
  4306. project = pagure.lib.query.get_authorized_project(
  4307. self.session, project_name="test"
  4308. )
  4309. msg = pagure.lib.query.add_group_to_project(
  4310. self.session, project=project, new_group="JL", user="pingou"
  4311. )
  4312. self.session.commit()
  4313. self.assertEqual(msg, "Group added")
  4314. group = pagure.lib.query.search_groups(self.session, group_name="JL")
  4315. # He should be an admin
  4316. access_obj = pagure.lib.query.get_obj_access(
  4317. self.session, project_obj=project, obj=group
  4318. )
  4319. self.assertEqual(access_obj.access, "admin")
  4320. # Update and check for commit access
  4321. msg = pagure.lib.query.add_group_to_project(
  4322. self.session,
  4323. project=project,
  4324. new_group="JL",
  4325. user="pingou",
  4326. access="commit",
  4327. )
  4328. self.session.commit()
  4329. self.assertEqual(msg, "Group access updated")
  4330. project = pagure.lib.query.get_authorized_project(
  4331. self.session, project_name="test"
  4332. )
  4333. # He should be a committer
  4334. access_obj = pagure.lib.query.get_obj_access(
  4335. self.session, project_obj=project, obj=group
  4336. )
  4337. self.assertEqual(access_obj.access, "commit")
  4338. # Update and check for ticket access
  4339. msg = pagure.lib.query.add_group_to_project(
  4340. self.session,
  4341. project=project,
  4342. new_group="JL",
  4343. user="pingou",
  4344. access="ticket",
  4345. )
  4346. self.session.commit()
  4347. self.assertEqual(msg, "Group access updated")
  4348. project = pagure.lib.query.get_authorized_project(
  4349. self.session, project_name="test"
  4350. )
  4351. # He should be a ticketer
  4352. access_obj = pagure.lib.query.get_obj_access(
  4353. self.session, project_obj=project, obj=group
  4354. )
  4355. self.assertEqual(access_obj.access, "ticket")
  4356. def test_set_watch_obj(self):
  4357. """ Test the set_watch_obj method in pagure.lib """
  4358. # Create the project ns/test
  4359. item = pagure.lib.model.Project(
  4360. user_id=1, # pingou
  4361. name="test3",
  4362. namespace="ns",
  4363. description="test project #1",
  4364. hook_token="aaabbbcccdd",
  4365. )
  4366. item.close_status = ["Invalid", "Insufficient data", "Fixed"]
  4367. self.session.add(item)
  4368. self.session.commit()
  4369. # Create the ticket
  4370. iss = pagure.lib.query.new_issue(
  4371. issue_id=4,
  4372. session=self.session,
  4373. repo=item,
  4374. title="test issue",
  4375. content="content test issue",
  4376. user="pingou",
  4377. )
  4378. self.session.commit()
  4379. self.assertEqual(iss.id, 4)
  4380. self.assertEqual(iss.title, "test issue")
  4381. # Unknown user
  4382. self.assertRaises(
  4383. pagure.exceptions.PagureException,
  4384. pagure.lib.query.set_watch_obj,
  4385. self.session,
  4386. "unknown",
  4387. iss,
  4388. True,
  4389. )
  4390. # Invalid object to watch - project
  4391. self.assertRaises(
  4392. pagure.exceptions.InvalidObjectException,
  4393. pagure.lib.query.set_watch_obj,
  4394. self.session,
  4395. "foo",
  4396. iss.project,
  4397. True,
  4398. )
  4399. # Invalid object to watch - string
  4400. self.assertRaises(
  4401. AttributeError,
  4402. pagure.lib.query.set_watch_obj,
  4403. self.session,
  4404. "foo",
  4405. "ticket",
  4406. True,
  4407. )
  4408. # Watch the ticket
  4409. out = pagure.lib.query.set_watch_obj(self.session, "foo", iss, True)
  4410. self.assertEqual(out, "You are now watching this issue")
  4411. # Un-watch the ticket
  4412. out = pagure.lib.query.set_watch_obj(self.session, "foo", iss, False)
  4413. self.assertEqual(out, "You are no longer watching this issue")
  4414. def test_tokenize_search_string(self):
  4415. """ Test the tokenize_search_string function. """
  4416. # These are the tests performed to make sure we tokenize correctly.
  4417. # This is in the form: input string, custom fields, remaining pattern
  4418. tests = [
  4419. ("test123", {}, "test123"),
  4420. ("test:key test123", {"test": "key"}, "test123"),
  4421. (
  4422. 'test:"key with spaces" test123',
  4423. {"test": "key with spaces"},
  4424. "test123",
  4425. ),
  4426. ("test123 test:key test456", {"test": "key"}, "test123 test456"),
  4427. (
  4428. 'test123 test:"key with spaces" key2:value12 test456',
  4429. {"test": "key with spaces", "key2": "value12"},
  4430. "test123 test456",
  4431. ),
  4432. ]
  4433. for inp, flds, rem in tests:
  4434. self.assertEqual(
  4435. pagure.lib.query.tokenize_search_string(inp), (flds, rem)
  4436. )
  4437. def test_save_report(self):
  4438. """ Test the save_report function. """
  4439. # Create the projects
  4440. tests.create_projects(self.session)
  4441. project = pagure.lib.query.get_authorized_project(
  4442. self.session, project_name="test"
  4443. )
  4444. self.assertEqual(project.reports, {})
  4445. name = "test report"
  4446. url = "?foo=bar&baz=biz"
  4447. pagure.lib.query.save_report(
  4448. self.session, repo=project, name=name, url=url, username=None
  4449. )
  4450. project = pagure.lib.query.get_authorized_project(
  4451. self.session, project_name="test"
  4452. )
  4453. self.assertEqual(
  4454. project.reports, {"test report": {"baz": "biz", "foo": "bar"}}
  4455. )
  4456. name = "test report #2"
  4457. url = "?foo=bar&foo=none&foo=baz"
  4458. pagure.lib.query.save_report(
  4459. self.session, repo=project, name=name, url=url, username=None
  4460. )
  4461. project = pagure.lib.query.get_authorized_project(
  4462. self.session, project_name="test"
  4463. )
  4464. self.assertEqual(
  4465. project.reports,
  4466. {
  4467. "test report": {"baz": "biz", "foo": "bar"},
  4468. "test report #2": {"foo": ["bar", "none", "baz"]},
  4469. },
  4470. )
  4471. def test_text2markdown_table(self):
  4472. """ Test the text2markdown function with a markdown table. """
  4473. try:
  4474. md_version = markdown.__version__.version_info
  4475. except AttributeError:
  4476. md_version = markdown.__version_info__
  4477. if md_version < (2, 6, 7):
  4478. raise unittest.case.SkipTest(
  4479. "Skipping on old markdown that do not strip the orientation row"
  4480. )
  4481. text = """
  4482. | Left-aligned | Center-aligned | Right-aligned |
  4483. | :--- | :---: | ---: |
  4484. | git status | git status | git status |
  4485. | git diff | git diff | git diff |
  4486. foo bar
  4487. """
  4488. expected = """<div class="markdown"><table>
  4489. <thead>
  4490. <tr>
  4491. <th align="left">Left-aligned</th>
  4492. <th align="center">Center-aligned</th>
  4493. <th align="right">Right-aligned</th>
  4494. </tr>
  4495. </thead>
  4496. <tbody>
  4497. <tr>
  4498. <td align="left">git status</td>
  4499. <td align="center">git status</td>
  4500. <td align="right">git status</td>
  4501. </tr>
  4502. <tr>
  4503. <td align="left">git diff</td>
  4504. <td align="center">git diff</td>
  4505. <td align="right">git diff</td>
  4506. </tr>
  4507. </tbody>
  4508. </table>
  4509. <p>foo bar</p></div>"""
  4510. with self.app.application.app_context():
  4511. html = pagure.lib.query.text2markdown(text)
  4512. self.assertEqual(html, expected)
  4513. def test_text2markdown_table_old_mk(self):
  4514. """ Test the text2markdown function with a markdown table using the old
  4515. format where the orientation instruction are provided next to the column
  4516. delimiter unlike what can be done with more recent version of markdown.
  4517. """
  4518. text = """
  4519. | Left-aligned | Center-aligned | Right-aligned |
  4520. |:--- |:--------------:| ---:|
  4521. | git status | git status | git status |
  4522. | git diff | git diff | git diff |
  4523. foo bar
  4524. """
  4525. expected = """<div class="markdown"><table>
  4526. <thead>
  4527. <tr>
  4528. <th align="left">Left-aligned</th>
  4529. <th align="center">Center-aligned</th>
  4530. <th align="right">Right-aligned</th>
  4531. </tr>
  4532. </thead>
  4533. <tbody>
  4534. <tr>
  4535. <td align="left">git status</td>
  4536. <td align="center">git status</td>
  4537. <td align="right">git status</td>
  4538. </tr>
  4539. <tr>
  4540. <td align="left">git diff</td>
  4541. <td align="center">git diff</td>
  4542. <td align="right">git diff</td>
  4543. </tr>
  4544. </tbody>
  4545. </table>
  4546. <p>foo bar</p></div>"""
  4547. with self.app.application.app_context():
  4548. html = pagure.lib.query.text2markdown(text)
  4549. self.assertEqual(html, expected)
  4550. def test_set_redis(self):
  4551. """ Test the set_redis function of pagure.lib.query. """
  4552. self.assertIsNone(pagure.lib.query.REDIS)
  4553. pagure.lib.query.set_redis("0.0.0.0", 6379, 0)
  4554. self.assertIsNotNone(pagure.lib.query.REDIS)
  4555. def test_set_pagure_ci(self):
  4556. """ Test the set_pagure_ci function of pagure.lib.query. """
  4557. # self.assertIn(pagure.lib.query.PAGURE_CI, [None, ['jenkins']])
  4558. pagure.lib.query.set_pagure_ci(True)
  4559. self.assertIsNotNone(pagure.lib.query.PAGURE_CI)
  4560. self.assertTrue(pagure.lib.query.PAGURE_CI)
  4561. def test_get_user_invalid_user(self):
  4562. """ Test the get_user function of pagure.lib.query. """
  4563. self.assertRaises(
  4564. pagure.exceptions.PagureException,
  4565. pagure.lib.query.get_user,
  4566. self.session,
  4567. "unknown",
  4568. )
  4569. def test_get_user_username(self):
  4570. """ Test the get_user function of pagure.lib.query. """
  4571. user = pagure.lib.query.get_user(self.session, "foo")
  4572. self.assertEqual(user.username, "foo")
  4573. def test_get_user_email(self):
  4574. """ Test the get_user function of pagure.lib.query. """
  4575. user = pagure.lib.query.get_user(self.session, "bar@pingou.com")
  4576. self.assertEqual(user.username, "pingou")
  4577. def test_is_valid_ssh_key_empty(self):
  4578. """ Test the is_valid_ssh_key function of pagure.lib.query. """
  4579. self.assertIsNone(pagure.lib.query.is_valid_ssh_key(""))
  4580. def test_is_valid_ssh_key_invalid(self):
  4581. """ Test the is_valid_ssh_key function of pagure.lib.query. """
  4582. self.assertEqual(pagure.lib.query.is_valid_ssh_key("nonsense"), False)
  4583. def test_is_valid_ssh_key(self):
  4584. """ Test the is_valid_ssh_key function of pagure.lib.query. """
  4585. keyinfo = pagure.lib.query.is_valid_ssh_key(
  4586. """ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDtgzSO9d1IrKdmyBFUvtAJPLgGOhp0lSySkWRSe+/+3KXYjSnsLnCJQlO5M7JfaXhtTHEow86rh4W9+FoJdzo5iocAwH5xPZ5ttHLy7VHgTzNMUeMgKpjy6bBOdPoGPPG4mo7QCMCRJdWBRDv4OSEMLU5jQAvC272YK2V8L918VQ== root@test"""
  4587. )
  4588. keys = keyinfo.split("\n")
  4589. self.assertEqual(len(keys), 2)
  4590. self.assertEqual(keys[1], "")
  4591. key = keys[0].split(" ")
  4592. self.assertEqual(key[0], "1024")
  4593. # We should always get the SHA256 version
  4594. self.assertEqual(
  4595. key[1], "SHA256:ztcRXIq7y/HNfwwscrexCyDF46/Pn/oHTkBZx87GwI8"
  4596. )
  4597. self.assertEqual(key[3], "(RSA)")
  4598. def test_create_deploykeys_ssh_keys_on_disk_empty(self):
  4599. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4600. pagure.lib.query. """
  4601. self.assertIsNone(
  4602. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(None, None)
  4603. )
  4604. self.assertFalse(
  4605. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4606. )
  4607. def test_create_deploykeys_ssh_keys_on_disk_nokey(self):
  4608. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4609. pagure.lib.query. """
  4610. tests.create_projects(self.session)
  4611. project = pagure.lib.query._get_project(self.session, "test")
  4612. self.assertIsNone(
  4613. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4614. project, self.path
  4615. )
  4616. )
  4617. self.assertTrue(
  4618. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4619. )
  4620. self.assertEqual(
  4621. os.listdir(os.path.join(self.path, "deploykeys", "test")), []
  4622. )
  4623. @patch(
  4624. "pagure.lib.query.is_valid_ssh_key", MagicMock(return_value="foo bar")
  4625. )
  4626. def test_create_deploykeys_ssh_keys_on_disk(self):
  4627. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4628. pagure.lib.query. """
  4629. tests.create_projects(self.session)
  4630. project = pagure.lib.query._get_project(self.session, "test")
  4631. # Add a deploy key to the project
  4632. pingou = pagure.lib.query.get_user(self.session, "pingou")
  4633. msg = pagure.lib.query.add_sshkey_to_project_or_user(
  4634. self.session,
  4635. project=project,
  4636. ssh_key="foo bar",
  4637. pushaccess=False,
  4638. creator=pingou,
  4639. )
  4640. self.assertEqual(msg, "SSH key added")
  4641. self.assertIsNone(
  4642. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4643. project, self.path
  4644. )
  4645. )
  4646. self.assertTrue(
  4647. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4648. )
  4649. self.assertEqual(
  4650. os.listdir(os.path.join(self.path, "deploykeys", "test")),
  4651. ["deploykey_test_1.pub"],
  4652. )
  4653. # Remove the deploykey
  4654. project = pagure.lib.query._get_project(self.session, "test")
  4655. self.session.delete(project.deploykeys[0])
  4656. self.session.commit()
  4657. # Remove the file on disk
  4658. self.assertIsNone(
  4659. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4660. project, self.path
  4661. )
  4662. )
  4663. self.assertTrue(
  4664. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4665. )
  4666. self.assertEqual(
  4667. os.listdir(os.path.join(self.path, "deploykeys", "test")), []
  4668. )
  4669. @patch(
  4670. "pagure.lib.query.is_valid_ssh_key",
  4671. MagicMock(return_value="\nfoo bar"),
  4672. )
  4673. def test_create_deploykeys_ssh_keys_on_disk_empty_first_key(self):
  4674. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4675. pagure.lib.query. """
  4676. tests.create_projects(self.session)
  4677. project = pagure.lib.query._get_project(self.session, "test")
  4678. # Add a deploy key to the project
  4679. new_key_obj = pagure.lib.model.SSHKey(
  4680. project_id=project.id,
  4681. pushaccess=False,
  4682. public_ssh_key="\n foo bar",
  4683. ssh_short_key="\n foo bar",
  4684. ssh_search_key="\n foo bar",
  4685. creator_user_id=1, # pingou
  4686. )
  4687. self.session.add(new_key_obj)
  4688. self.session.commit()
  4689. self.assertIsNone(
  4690. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4691. project, self.path
  4692. )
  4693. )
  4694. self.assertTrue(
  4695. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4696. )
  4697. self.assertEqual(
  4698. os.listdir(os.path.join(self.path, "deploykeys", "test")), []
  4699. )
  4700. def test_create_deploykeys_ssh_keys_on_disk_invalid(self):
  4701. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4702. pagure.lib.query. """
  4703. tests.create_projects(self.session)
  4704. project = pagure.lib.query._get_project(self.session, "test")
  4705. # Add a deploy key to the project
  4706. new_key_obj = pagure.lib.model.SSHKey(
  4707. project_id=project.id,
  4708. pushaccess=False,
  4709. public_ssh_key="foo bar",
  4710. ssh_short_key="foo bar",
  4711. ssh_search_key="foo bar",
  4712. creator_user_id=1, # pingou
  4713. )
  4714. self.session.add(new_key_obj)
  4715. self.session.commit()
  4716. self.assertIsNone(
  4717. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4718. project, self.path
  4719. )
  4720. )
  4721. self.assertTrue(
  4722. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4723. )
  4724. self.assertEqual(
  4725. os.listdir(os.path.join(self.path, "deploykeys", "test")), []
  4726. )
  4727. def test_create_user_ssh_keys_on_disk_none(self):
  4728. """ Test the create_user_ssh_keys_on_disk function of pagure.lib.query. """
  4729. self.assertIsNone(
  4730. pagure.lib.query.create_user_ssh_keys_on_disk(None, None)
  4731. )
  4732. def test_update_user_settings_invalid_user(self):
  4733. """ Test the update_user_settings function of pagure.lib.query. """
  4734. self.assertRaises(
  4735. pagure.exceptions.PagureException,
  4736. pagure.lib.query.update_user_settings,
  4737. session=self.session,
  4738. settings={},
  4739. user="invalid",
  4740. )
  4741. def test_update_user_settings_no_change(self):
  4742. """ Test the update_user_settings function of pagure.lib.query. """
  4743. # First update the setting
  4744. msg = pagure.lib.query.update_user_settings(
  4745. session=self.session,
  4746. settings={"cc_me_to_my_actions": True},
  4747. user="pingou",
  4748. )
  4749. self.assertEqual(msg, "Successfully edited your settings")
  4750. # Then change it back to its default
  4751. msg = pagure.lib.query.update_user_settings(
  4752. session=self.session, settings={}, user="pingou"
  4753. )
  4754. self.assertEqual(msg, "Successfully edited your settings")
  4755. def test_update_user_settings_no_data(self):
  4756. """ Test the update_user_settings function of pagure.lib.query. """
  4757. msg = pagure.lib.query.update_user_settings(
  4758. session=self.session,
  4759. settings={"cc_me_to_my_actions": False},
  4760. user="pingou",
  4761. )
  4762. self.assertEqual(msg, "No settings to change")
  4763. def test_update_user_settings(self):
  4764. """ Test the update_user_settings function of pagure.lib.query. """
  4765. msg = pagure.lib.query.update_user_settings(
  4766. session=self.session,
  4767. settings={"cc_me_to_my_actions": True},
  4768. user="pingou",
  4769. )
  4770. self.assertEqual(msg, "Successfully edited your settings")
  4771. @patch.dict(
  4772. pagure.config.config,
  4773. {"ALLOWED_EMAIL_DOMAINS": ["pingoured.fr"]},
  4774. clear=True,
  4775. )
  4776. def test_add_email_to_user_with_logs(self):
  4777. """ Test the add_email_to_user function of pagure.lib when there
  4778. are log entries associated to the email added.
  4779. """
  4780. user = pagure.lib.query.search_user(self.session, username="pingou")
  4781. # Add a couple of log entries associated with the new email
  4782. for i in range(3):
  4783. log = pagure.lib.model.PagureLog(
  4784. user_email="new_email@pingoured.fr",
  4785. log_type="commit",
  4786. ref_id=i,
  4787. )
  4788. self.session.add(log)
  4789. self.session.commit()
  4790. # Check emails before
  4791. self.assertEqual(len(user.emails), 2)
  4792. # Add the new_email to the user
  4793. pagure.lib.query.add_email_to_user(
  4794. self.session, user, "new_email@pingoured.fr"
  4795. )
  4796. self.session.commit()
  4797. # Check emails after
  4798. self.assertEqual(len(user.emails), 3)
  4799. # add another email address that is not in an allowed domain
  4800. self.assertRaises(
  4801. pagure.exceptions.PagureException,
  4802. pagure.lib.query.add_email_to_user,
  4803. session=self.session,
  4804. user=user,
  4805. user_email="foo@bar.com",
  4806. )
  4807. self.session.commit()
  4808. # Check emails after
  4809. self.assertEqual(len(user.emails), 3)
  4810. # redo tests with 2 allowed domains
  4811. pagure.config.config["ALLOWED_EMAIL_DOMAINS"] = [
  4812. "pingoured.fr",
  4813. "example.com",
  4814. ]
  4815. # Add the new_email to the user
  4816. pagure.lib.query.add_email_to_user(
  4817. self.session, user, "another_email@example.com"
  4818. )
  4819. self.session.commit()
  4820. # Check emails after
  4821. self.assertEqual(len(user.emails), 4)
  4822. # add another email address that is not in an allowed domain
  4823. self.assertRaises(
  4824. pagure.exceptions.PagureException,
  4825. pagure.lib.query.add_email_to_user,
  4826. session=self.session,
  4827. user=user,
  4828. user_email="another_foo@bar.com",
  4829. )
  4830. self.session.commit()
  4831. # Check emails after
  4832. self.assertEqual(len(user.emails), 4)
  4833. @patch(
  4834. "pagure.lib.query.is_valid_ssh_key", MagicMock(return_value="foo bar")
  4835. )
  4836. def test_update_user_ssh_valid_key(self):
  4837. """ Test the update_user_ssh function of pagure.lib.query. """
  4838. pagure.SESSION = self.session
  4839. pagure.lib.query.update_user_ssh(
  4840. self.session, user="pingou", ssh_key="foo key", keydir=self.path
  4841. )
  4842. self.session.commit()
  4843. self.assertTrue(os.path.exists(os.path.join(self.path, "keys_0")))
  4844. self.assertEqual(
  4845. os.listdir(os.path.join(self.path, "keys_0")), ["pingou.pub"]
  4846. )
  4847. def test_add_user_pending_email_existing_email(self):
  4848. """ Test the add_user_pending_email function of pagure.lib.query. """
  4849. user = pagure.lib.query.search_user(self.session, username="pingou")
  4850. self.assertRaises(
  4851. pagure.exceptions.PagureException,
  4852. pagure.lib.query.add_user_pending_email,
  4853. session=self.session,
  4854. userobj=user,
  4855. email="foo@bar.com",
  4856. )
  4857. @patch("pagure.lib.notify.notify_new_email", MagicMock(return_value=True))
  4858. @patch.dict(
  4859. pagure.config.config,
  4860. {"ALLOWED_EMAIL_DOMAINS": ["pingoured.fr"]},
  4861. clear=True,
  4862. )
  4863. def test_add_user_pending_email(self):
  4864. """ Test the add_user_pending_email function of pagure.lib.query. """
  4865. user = pagure.lib.query.search_user(self.session, username="pingou")
  4866. self.assertEqual(len(user.emails), 2)
  4867. self.assertEqual(len(user.emails_pending), 0)
  4868. pagure.lib.query.add_user_pending_email(
  4869. session=self.session, userobj=user, email="new_mail@pingoured.fr"
  4870. )
  4871. self.session.commit()
  4872. self.assertEqual(len(user.emails), 2)
  4873. self.assertEqual(len(user.emails_pending), 1)
  4874. # add another email address that is not allowed
  4875. self.assertRaises(
  4876. pagure.exceptions.PagureException,
  4877. pagure.lib.query.add_user_pending_email,
  4878. session=self.session,
  4879. userobj=user,
  4880. email="foo@bar.com",
  4881. )
  4882. self.session.commit()
  4883. self.assertEqual(len(user.emails), 2)
  4884. self.assertEqual(len(user.emails_pending), 1)
  4885. def test_resend_pending_email_someone_else_email(self):
  4886. """ Test the resend_pending_email function of pagure.lib.query. """
  4887. user = pagure.lib.query.search_user(self.session, username="pingou")
  4888. self.assertRaises(
  4889. pagure.exceptions.PagureException,
  4890. pagure.lib.query.resend_pending_email,
  4891. session=self.session,
  4892. userobj=user,
  4893. email="foo@bar.com",
  4894. )
  4895. def test_resend_pending_email_email_validated(self):
  4896. """ Test the resend_pending_email function of pagure.lib.query. """
  4897. user = pagure.lib.query.search_user(self.session, username="pingou")
  4898. self.assertRaises(
  4899. pagure.exceptions.PagureException,
  4900. pagure.lib.query.resend_pending_email,
  4901. session=self.session,
  4902. userobj=user,
  4903. email="foo@pingou.com",
  4904. )
  4905. def test_get_acls(self):
  4906. """ Test the get_acls function of pagure.lib.query. """
  4907. acls = pagure.lib.query.get_acls(self.session)
  4908. self.assertEqual(
  4909. sorted([a.name for a in acls]),
  4910. [
  4911. "commit",
  4912. "commit_flag",
  4913. "create_branch",
  4914. "create_project",
  4915. "fork_project",
  4916. "generate_acls_project",
  4917. "internal_access",
  4918. "issue_assign",
  4919. "issue_change_status",
  4920. "issue_comment",
  4921. "issue_create",
  4922. "issue_subscribe",
  4923. "issue_update",
  4924. "issue_update_custom_fields",
  4925. "issue_update_milestone",
  4926. "modify_project",
  4927. "pull_request_assign",
  4928. "pull_request_close",
  4929. "pull_request_comment",
  4930. "pull_request_create",
  4931. "pull_request_flag",
  4932. "pull_request_merge",
  4933. "pull_request_rebase",
  4934. "pull_request_subscribe",
  4935. "pull_request_update",
  4936. "tag_project",
  4937. "update_watch_status",
  4938. ],
  4939. )
  4940. def test_get_acls_restrict_one(self):
  4941. """ Test the get_acls function of pagure.lib.query. """
  4942. acls = pagure.lib.query.get_acls(
  4943. self.session, restrict="create_project"
  4944. )
  4945. self.assertEqual([a.name for a in acls], ["create_project"])
  4946. def test_get_acls_restrict_two(self):
  4947. """ Test the get_acls function of pagure.lib.query. """
  4948. acls = pagure.lib.query.get_acls(
  4949. self.session, restrict=["create_project", "issue_create"]
  4950. )
  4951. self.assertEqual(
  4952. [a.name for a in acls], ["create_project", "issue_create"]
  4953. )
  4954. def test_filter_img_src(self):
  4955. """ Test the filter_img_src function of pagure.lib.query. """
  4956. for name in ("alt", "height", "width", "class"):
  4957. self.assertTrue(pagure.lib.query.filter_img_src(name, "caption"))
  4958. self.assertTrue(
  4959. pagure.lib.query.filter_img_src("src", "/path/to/image")
  4960. )
  4961. self.assertTrue(
  4962. pagure.lib.query.filter_img_src(
  4963. "src", "http://localhost.localdomain/path/to/image"
  4964. )
  4965. )
  4966. self.assertFalse(
  4967. pagure.lib.query.filter_img_src(
  4968. "src", "http://foo.org/path/to/image"
  4969. )
  4970. )
  4971. self.assertFalse(
  4972. pagure.lib.query.filter_img_src(
  4973. "anything", "http://foo.org/path/to/image"
  4974. )
  4975. )
  4976. def test_clean_input(self):
  4977. """ Test the clean_input function of pagure.lib.query. """
  4978. text = '<a href="/path" title="click me!">Click here</a>'
  4979. output = pagure.lib.query.clean_input(text)
  4980. self.assertEqual(output, text)
  4981. def test_could_be_text(self):
  4982. """ Test the could_be_text function of pagure.lib.query. """
  4983. self.assertTrue(pagure.lib.query.could_be_text(b"foo"))
  4984. self.assertTrue(pagure.lib.query.could_be_text("fâö".encode("utf-8")))
  4985. self.assertFalse(
  4986. pagure.lib.query.could_be_text(b"\x89PNG\r\n\x1a\n\x00")
  4987. )
  4988. def test_set_custom_key_fields_empty(self):
  4989. """ Test the set_custom_key_fields function of pagure.lib.query. """
  4990. tests.create_projects(self.session)
  4991. project = pagure.lib.query._get_project(self.session, "test")
  4992. self.assertIsNotNone(project)
  4993. msg = pagure.lib.query.set_custom_key_fields(
  4994. session=self.session,
  4995. project=project,
  4996. fields=[],
  4997. types=[],
  4998. data=[],
  4999. notify=False,
  5000. )
  5001. self.session.commit()
  5002. self.assertEqual(msg, "List of custom fields updated")
  5003. def test_set_custom_key_fields(self):
  5004. """ Test the set_custom_key_fields function of pagure.lib.query. """
  5005. tests.create_projects(self.session)
  5006. project = pagure.lib.query._get_project(self.session, "test")
  5007. self.assertIsNotNone(project)
  5008. # Set a custom key
  5009. msg = pagure.lib.query.set_custom_key_fields(
  5010. session=self.session,
  5011. project=project,
  5012. fields=["upstream"],
  5013. types=["url"],
  5014. data=[None],
  5015. notify=False,
  5016. )
  5017. self.session.commit()
  5018. self.assertEqual(msg, "List of custom fields updated")
  5019. # Set another one, with notifications on
  5020. msg = pagure.lib.query.set_custom_key_fields(
  5021. session=self.session,
  5022. project=project,
  5023. fields=["bugzilla_url"],
  5024. types=["url"],
  5025. data=[None],
  5026. notify=["on"],
  5027. )
  5028. self.session.commit()
  5029. self.assertEqual(msg, "List of custom fields updated")
  5030. # Re-set the second one but with notifications off
  5031. msg = pagure.lib.query.set_custom_key_fields(
  5032. session=self.session,
  5033. project=project,
  5034. fields=["bugzilla_url"],
  5035. types=["url"],
  5036. data=[None],
  5037. notify=["off"],
  5038. )
  5039. self.session.commit()
  5040. self.assertEqual(msg, "List of custom fields updated")
  5041. @patch("pagure.lib.query.REDIS")
  5042. def test_set_custom_key_value_boolean(self, mock_redis):
  5043. """ Test the set_custom_key_value function of pagure.lib.query. """
  5044. mock_redis.return_value = True
  5045. tests.create_projects(self.session)
  5046. project = pagure.lib.query._get_project(self.session, "test")
  5047. self.assertIsNotNone(project)
  5048. # Set a custom key
  5049. msg = pagure.lib.query.set_custom_key_fields(
  5050. session=self.session,
  5051. project=project,
  5052. fields=["tested"],
  5053. types=["boolean"],
  5054. data=[None],
  5055. notify=False,
  5056. )
  5057. self.session.commit()
  5058. self.assertEqual(msg, "List of custom fields updated")
  5059. # Create issues
  5060. msg = pagure.lib.query.new_issue(
  5061. session=self.session,
  5062. repo=project,
  5063. title="Test issue",
  5064. content="We should work on this",
  5065. user="pingou",
  5066. )
  5067. self.session.commit()
  5068. self.assertEqual(msg.title, "Test issue")
  5069. issue = pagure.lib.query.search_issues(
  5070. self.session, project, issueid=1
  5071. )
  5072. self.assertEqual(len(project.issue_keys), 1)
  5073. self.assertEqual(project.issue_keys[0].key_type, "boolean")
  5074. msg = pagure.lib.query.set_custom_key_value(
  5075. session=self.session,
  5076. issue=issue,
  5077. key=project.issue_keys[0],
  5078. value=True,
  5079. )
  5080. self.session.commit()
  5081. self.assertEqual(msg, "Custom field tested adjusted to True")
  5082. # Update it a second time to trigger edit
  5083. msg = pagure.lib.query.set_custom_key_value(
  5084. session=self.session,
  5085. issue=issue,
  5086. key=project.issue_keys[0],
  5087. value=False,
  5088. )
  5089. if str(self.session.bind.engine.url).startswith("sqlite"):
  5090. self.assertEqual(msg, "Custom field tested reset (from 1)")
  5091. else:
  5092. self.assertEqual(msg, "Custom field tested reset (from true)")
  5093. self.assertEqual(mock_redis.publish.call_count, 2)
  5094. @patch("pagure.lib.query.REDIS")
  5095. def test_set_custom_key_value_boolean_private_issue(self, mock_redis):
  5096. """ Test the set_custom_key_value function of pagure.lib.query. """
  5097. mock_redis.return_value = True
  5098. tests.create_projects(self.session)
  5099. project = pagure.lib.query._get_project(self.session, "test")
  5100. self.assertIsNotNone(project)
  5101. # Set a custom key
  5102. msg = pagure.lib.query.set_custom_key_fields(
  5103. session=self.session,
  5104. project=project,
  5105. fields=["tested"],
  5106. types=["boolean"],
  5107. data=[None],
  5108. notify=False,
  5109. )
  5110. self.session.commit()
  5111. self.assertEqual(msg, "List of custom fields updated")
  5112. # Create issues
  5113. msg = pagure.lib.query.new_issue(
  5114. session=self.session,
  5115. repo=project,
  5116. title="Test issue",
  5117. content="We should work on this",
  5118. user="pingou",
  5119. private=True,
  5120. )
  5121. self.session.commit()
  5122. self.assertEqual(msg.title, "Test issue")
  5123. issue = pagure.lib.query.search_issues(
  5124. self.session, project, issueid=1
  5125. )
  5126. self.assertEqual(len(project.issue_keys), 1)
  5127. self.assertEqual(project.issue_keys[0].key_type, "boolean")
  5128. msg = pagure.lib.query.set_custom_key_value(
  5129. session=self.session,
  5130. issue=issue,
  5131. key=project.issue_keys[0],
  5132. value=True,
  5133. )
  5134. self.session.commit()
  5135. self.assertEqual(msg, "Custom field tested adjusted to True")
  5136. # Update it a second time to trigger edit
  5137. msg = pagure.lib.query.set_custom_key_value(
  5138. session=self.session,
  5139. issue=issue,
  5140. key=project.issue_keys[0],
  5141. value=False,
  5142. )
  5143. self.session.commit()
  5144. if str(self.session.bind.engine.url).startswith("sqlite"):
  5145. self.assertEqual(msg, "Custom field tested reset (from 1)")
  5146. else:
  5147. self.assertEqual(msg, "Custom field tested reset (from true)")
  5148. self.assertEqual(mock_redis.publish.call_count, 2)
  5149. @patch("pagure.lib.query.REDIS")
  5150. def test_set_custom_key_value_text(self, mock_redis):
  5151. """ Test the set_custom_key_value function of pagure.lib.query. """
  5152. mock_redis.return_value = True
  5153. tests.create_projects(self.session)
  5154. project = pagure.lib.query._get_project(self.session, "test")
  5155. self.assertIsNotNone(project)
  5156. # Set a custom key
  5157. msg = pagure.lib.query.set_custom_key_fields(
  5158. session=self.session,
  5159. project=project,
  5160. fields=["tested"],
  5161. types=["text"],
  5162. data=[None],
  5163. notify=False,
  5164. )
  5165. self.session.commit()
  5166. self.assertEqual(msg, "List of custom fields updated")
  5167. # Create issues
  5168. msg = pagure.lib.query.new_issue(
  5169. session=self.session,
  5170. repo=project,
  5171. title="Test issue",
  5172. content="We should work on this",
  5173. user="pingou",
  5174. )
  5175. self.session.commit()
  5176. self.assertEqual(msg.title, "Test issue")
  5177. issue = pagure.lib.query.search_issues(
  5178. self.session, project, issueid=1
  5179. )
  5180. self.assertEqual(len(project.issue_keys), 1)
  5181. self.assertEqual(project.issue_keys[0].key_type, "text")
  5182. msg = pagure.lib.query.set_custom_key_value(
  5183. session=self.session,
  5184. issue=issue,
  5185. key=project.issue_keys[0],
  5186. value="In progress",
  5187. )
  5188. self.session.commit()
  5189. self.assertEqual(msg, "Custom field tested adjusted to In progress")
  5190. # Update it a second time to trigger edit
  5191. msg = pagure.lib.query.set_custom_key_value(
  5192. session=self.session,
  5193. issue=issue,
  5194. key=project.issue_keys[0],
  5195. value="Done",
  5196. )
  5197. self.assertEqual(
  5198. msg, "Custom field tested adjusted to Done (was: In progress)"
  5199. )
  5200. self.assertEqual(mock_redis.publish.call_count, 2)
  5201. def test_log_action_invalid(self):
  5202. """ Test the log_action function of pagure.lib.query. """
  5203. obj = MagicMock
  5204. obj.isa = "invalid"
  5205. self.assertRaises(
  5206. pagure.exceptions.InvalidObjectException,
  5207. pagure.lib.query.log_action,
  5208. session=self.session,
  5209. action="foo",
  5210. obj=obj,
  5211. user_obj=None,
  5212. )
  5213. def test_search_token_no_acls(self):
  5214. """ Test the search_token function of pagure.lib.query. """
  5215. tests.create_projects(self.session)
  5216. tests.create_tokens(self.session)
  5217. tests.create_tokens_acl(self.session)
  5218. out = pagure.lib.query.search_token(self.session, [])
  5219. self.assertEqual(len(out), 1)
  5220. self.assertEqual(out[0].id, "aaabbbcccddd")
  5221. def test_search_token_single_acls(self):
  5222. """ Test the search_token function of pagure.lib.query. """
  5223. tests.create_projects(self.session)
  5224. tests.create_tokens(self.session)
  5225. tests.create_tokens_acl(self.session)
  5226. out = pagure.lib.query.search_token(self.session, "issue_create")
  5227. self.assertEqual(len(out), 1)
  5228. self.assertEqual(out[0].id, "aaabbbcccddd")
  5229. def test_search_token_single_acls_user(self):
  5230. """ Test the search_token function of pagure.lib.query. """
  5231. tests.create_projects(self.session)
  5232. tests.create_tokens(self.session)
  5233. tests.create_tokens_acl(self.session)
  5234. out = pagure.lib.query.search_token(
  5235. self.session, "issue_create", user="pingou"
  5236. )
  5237. self.assertEqual(len(out), 1)
  5238. self.assertEqual(out[0].id, "aaabbbcccddd")
  5239. out = pagure.lib.query.search_token(
  5240. self.session, "issue_create", user="foo"
  5241. )
  5242. self.assertEqual(len(out), 0)
  5243. def test_search_token_single_acls_active(self):
  5244. """ Test the search_token function of pagure.lib.query. """
  5245. tests.create_projects(self.session)
  5246. tests.create_tokens(self.session)
  5247. tests.create_tokens_acl(self.session)
  5248. out = pagure.lib.query.search_token(
  5249. self.session, "issue_create", active=True
  5250. )
  5251. self.assertEqual(len(out), 1)
  5252. self.assertEqual(out[0].id, "aaabbbcccddd")
  5253. out = pagure.lib.query.search_token(
  5254. self.session, "issue_create", expired=True
  5255. )
  5256. self.assertEqual(len(out), 0)
  5257. def test_update_read_only_mode(self):
  5258. """ Test the update_read_only_mode method of pagure.lib """
  5259. tests.create_projects(self.session)
  5260. project_obj = pagure.lib.query._get_project(self.session, "test")
  5261. # Default mode of project is read only
  5262. self.assertEqual(project_obj.read_only, True)
  5263. # Remove read only
  5264. pagure.lib.query.update_read_only_mode(
  5265. self.session, project_obj, False
  5266. )
  5267. self.session.commit()
  5268. project_obj = pagure.lib.query._get_project(self.session, "test")
  5269. self.assertEqual(project_obj.read_only, False)
  5270. # Try reversing it back
  5271. pagure.lib.query.update_read_only_mode(self.session, project_obj, True)
  5272. self.session.commit()
  5273. project_obj = pagure.lib.query._get_project(self.session, "test")
  5274. self.assertEqual(project_obj.read_only, True)
  5275. if __name__ == "__main__":
  5276. unittest.main(verbosity=2)