test_pagure_lib.py 210 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068
  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
  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. # Create a forked repo
  2190. item = pagure.lib.model.Project(
  2191. user_id=1, # pingou
  2192. name="test",
  2193. description="test project #1",
  2194. is_fork=True,
  2195. parent_id=1,
  2196. hook_token="aaabbbrrr",
  2197. )
  2198. self.session.commit()
  2199. self.session.add(item)
  2200. # Add an extra user to project `foo`
  2201. repo = pagure.lib.query._get_project(self.session, "test")
  2202. self.assertEqual(repo.open_requests, 0)
  2203. msg = pagure.lib.query.add_user_to_project(
  2204. session=self.session, project=repo, new_user="foo", user="pingou"
  2205. )
  2206. self.session.commit()
  2207. self.assertEqual(msg, "User added")
  2208. repo = pagure.lib.query._get_project(self.session, "test")
  2209. forked_repo = pagure.lib.query._get_project(
  2210. self.session, "test", user="pingou"
  2211. )
  2212. # Fails for the lack of repo_from and remote_git
  2213. self.assertRaises(
  2214. pagure.exceptions.PagureException,
  2215. pagure.lib.query.new_pull_request,
  2216. session=self.session,
  2217. repo_from=None,
  2218. branch_from="master",
  2219. repo_to=repo,
  2220. branch_to="master",
  2221. title="test pull-request",
  2222. user="pingou",
  2223. )
  2224. # Let's pretend we turned on the CI hook for the project
  2225. project = pagure.lib.query._get_project(self.session, "test")
  2226. obj = pagure.hooks.pagure_ci.PagureCITable(
  2227. project_id=project.id, active=True
  2228. )
  2229. self.session.add(obj)
  2230. self.session.commit()
  2231. # Create the new PR
  2232. req = pagure.lib.query.new_pull_request(
  2233. session=self.session,
  2234. repo_from=forked_repo,
  2235. branch_from="master",
  2236. repo_to=repo,
  2237. branch_to="master",
  2238. title="test pull-request",
  2239. user="pingou",
  2240. )
  2241. self.session.commit()
  2242. self.assertEqual(req.id, 1)
  2243. self.assertEqual(req.title, "test pull-request")
  2244. self.assertEqual(repo.open_requests, 1)
  2245. @patch("pagure.lib.query.REDIS")
  2246. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2247. def test_add_pull_request_comment(self, mock_redis):
  2248. """ Test add_pull_request_comment of pagure.lib.query. """
  2249. mock_redis.return_value = True
  2250. self.test_new_pull_request()
  2251. request = pagure.lib.query.search_pull_requests(
  2252. self.session, requestid=1
  2253. )
  2254. msg = pagure.lib.query.add_pull_request_comment(
  2255. session=self.session,
  2256. request=request,
  2257. commit="commithash",
  2258. tree_id=None,
  2259. filename="file",
  2260. row=None,
  2261. comment="This is awesome, I got to remember it!",
  2262. user="foo",
  2263. notification=True,
  2264. )
  2265. self.assertEqual(msg, "Comment added")
  2266. self.session.commit()
  2267. self.assertEqual(len(request.discussion), 0)
  2268. self.assertEqual(len(request.comments), 1)
  2269. self.assertEqual(request.score, 0)
  2270. self.assertEqual(mock_redis.publish.call_count, 1)
  2271. @patch("pagure.lib.query.REDIS")
  2272. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2273. @patch("pagure.lib.query.PAGURE_CI", MagicMock(return_value=True))
  2274. def test_add_pull_request_comment_to_re_run_ci(self, mock_redis):
  2275. """ Test add_pull_request_comment of pagure.lib.query. """
  2276. mock_redis.return_value = True
  2277. self.test_new_pull_request()
  2278. self.assertEqual(mock_redis.publish.call_count, 0)
  2279. # Let's pretend we turned on the CI hook for the project
  2280. project = pagure.lib.query._get_project(self.session, "test")
  2281. if not project.ci_hook or not project.ci_hook.active:
  2282. obj = pagure.hooks.pagure_ci.PagureCITable(
  2283. project_id=project.id, active=True
  2284. )
  2285. self.session.add(obj)
  2286. self.session.commit()
  2287. request = pagure.lib.query.search_pull_requests(
  2288. self.session, requestid=1
  2289. )
  2290. msg = pagure.lib.query.add_pull_request_comment(
  2291. session=self.session,
  2292. request=request,
  2293. commit="commithash",
  2294. tree_id=None,
  2295. filename="file",
  2296. row=None,
  2297. comment="Pretty please pagure-ci rebuild",
  2298. user="foo",
  2299. notification=True,
  2300. trigger_ci=["pretty please pagure-ci rebuild"],
  2301. )
  2302. self.assertEqual(msg, "Comment added")
  2303. self.session.commit()
  2304. self.assertEqual(len(request.discussion), 0)
  2305. self.assertEqual(len(request.comments), 1)
  2306. self.assertEqual(request.score, 0)
  2307. self.assertEqual(mock_redis.publish.call_count, 1)
  2308. @patch("pagure.lib.notify.send_email")
  2309. def test_add_pull_request_flag(self, mockemail):
  2310. """ Test add_pull_request_flag of pagure.lib.query. """
  2311. mockemail.return_value = True
  2312. self.test_new_pull_request()
  2313. tests.create_tokens(self.session)
  2314. request = pagure.lib.query.search_pull_requests(
  2315. self.session, requestid=1
  2316. )
  2317. self.assertEqual(len(request.flags), 0)
  2318. msg = pagure.lib.query.add_pull_request_flag(
  2319. session=self.session,
  2320. request=request,
  2321. username="jenkins",
  2322. percent=100,
  2323. comment="Build passes",
  2324. status="success",
  2325. url="http://jenkins.cloud.fedoraproject.org",
  2326. uid="jenkins_build_pagure_34",
  2327. user="foo",
  2328. token="aaabbbcccddd",
  2329. )
  2330. self.assertEqual(msg, ("Flag added", "jenkins_build_pagure_34"))
  2331. self.session.commit()
  2332. self.assertEqual(len(request.flags), 1)
  2333. self.assertEqual(request.flags[0].token_id, "aaabbbcccddd")
  2334. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2335. def test_search_pull_requests(self):
  2336. """ Test search_pull_requests of pagure.lib.query. """
  2337. self.test_new_pull_request()
  2338. prs = pagure.lib.query.search_pull_requests(session=self.session)
  2339. self.assertEqual(len(prs), 1)
  2340. prs = pagure.lib.query.search_pull_requests(
  2341. session=self.session, project_id=1
  2342. )
  2343. self.assertEqual(len(prs), 1)
  2344. prs = pagure.lib.query.search_pull_requests(
  2345. session=self.session, project_id_from=4
  2346. )
  2347. self.assertEqual(len(prs), 1)
  2348. prs = pagure.lib.query.search_pull_requests(
  2349. session=self.session, status=False
  2350. )
  2351. self.assertEqual(len(prs), 0)
  2352. # All non-assigned PR
  2353. prs = pagure.lib.query.search_pull_requests(
  2354. session=self.session, assignee=False
  2355. )
  2356. self.assertEqual(len(prs), 1)
  2357. prs[0].assignee_id = 1
  2358. self.session.add(prs[0])
  2359. self.session.commit()
  2360. # All the PR assigned
  2361. prs = pagure.lib.query.search_pull_requests(
  2362. session=self.session, assignee=True
  2363. )
  2364. self.assertEqual(len(prs), 1)
  2365. # Basically the same as above but then for a specific user
  2366. prs = pagure.lib.query.search_pull_requests(
  2367. session=self.session, assignee="pingou"
  2368. )
  2369. self.assertEqual(len(prs), 1)
  2370. # All PR except those assigned to pingou
  2371. prs = pagure.lib.query.search_pull_requests(
  2372. session=self.session, assignee="!pingou"
  2373. )
  2374. self.assertEqual(len(prs), 0)
  2375. # All PR created by the specified author
  2376. prs = pagure.lib.query.search_pull_requests(
  2377. session=self.session, author="pingou"
  2378. )
  2379. self.assertEqual(len(prs), 1)
  2380. # Count the PR instead of listing them
  2381. prs = pagure.lib.query.search_pull_requests(
  2382. session=self.session, author="pingou", count=True
  2383. )
  2384. self.assertEqual(prs, 1)
  2385. dt = datetime.datetime.utcnow()
  2386. # Create the second PR
  2387. repo = pagure.lib.query._get_project(self.session, "test")
  2388. req = pagure.lib.query.new_pull_request(
  2389. session=self.session,
  2390. repo_from=repo,
  2391. branch_from="feature",
  2392. repo_to=repo,
  2393. branch_to="master",
  2394. title="test pull-request #2",
  2395. user="pingou",
  2396. )
  2397. self.session.commit()
  2398. self.assertEqual(req.id, 2)
  2399. self.assertEqual(req.title, "test pull-request #2")
  2400. self.assertEqual(repo.open_requests, 2)
  2401. # Ensure we have 2 PRs
  2402. prs = pagure.lib.query.search_pull_requests(
  2403. session=self.session, author="pingou"
  2404. )
  2405. self.assertEqual(len(prs), 2)
  2406. # Test the offset
  2407. prs = pagure.lib.query.search_pull_requests(
  2408. session=self.session, author="pingou", offset=1
  2409. )
  2410. self.assertEqual(len(prs), 1)
  2411. # Test the updated_after
  2412. # Test updated after before the second PR was created
  2413. prs = pagure.lib.query.search_pull_requests(
  2414. session=self.session, author="pingou", updated_after=dt
  2415. )
  2416. self.assertEqual(len(prs), 1)
  2417. # Test updated after, 1h ago
  2418. prs = pagure.lib.query.search_pull_requests(
  2419. session=self.session,
  2420. author="pingou",
  2421. updated_after=dt - datetime.timedelta(hours=1),
  2422. )
  2423. self.assertEqual(len(prs), 2)
  2424. @patch("pagure.lib.notify.send_email")
  2425. def test_close_pull_request(self, send_email):
  2426. """ Test close_pull_request of pagure.lib.query. """
  2427. send_email.return_value = True
  2428. self.test_new_pull_request()
  2429. repo = pagure.lib.query._get_project(self.session, "test")
  2430. self.assertEqual(repo.open_requests, 1)
  2431. request = pagure.lib.query.search_pull_requests(
  2432. self.session, requestid=1
  2433. )
  2434. pagure.lib.query.close_pull_request(
  2435. session=self.session, request=request, user="pingou", merged=True
  2436. )
  2437. self.session.commit()
  2438. repo = pagure.lib.query._get_project(self.session, "test")
  2439. self.assertEqual(repo.open_requests, 0)
  2440. prs = pagure.lib.query.search_pull_requests(
  2441. session=self.session, status=False
  2442. )
  2443. self.assertEqual(len(prs), 1)
  2444. # Does not change much, just the notification sent
  2445. pagure.lib.query.close_pull_request(
  2446. session=self.session, request=request, user="pingou", merged=False
  2447. )
  2448. self.session.commit()
  2449. prs = pagure.lib.query.search_pull_requests(
  2450. session=self.session, status=False
  2451. )
  2452. self.assertEqual(len(prs), 1)
  2453. @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
  2454. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  2455. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  2456. def test_remove_issue_dependency(self):
  2457. """ Test remove_issue_dependency of pagure.lib.query. """
  2458. self.test_add_issue_dependency()
  2459. repo = pagure.lib.query._get_project(self.session, "test")
  2460. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2461. issue_blocked = pagure.lib.query.search_issues(
  2462. self.session, repo, issueid=2
  2463. )
  2464. # Before
  2465. self.assertEqual(len(issue.children), 1)
  2466. self.assertEqual(issue.children[0].id, 2)
  2467. self.assertEqual(len(issue.parents), 0)
  2468. self.assertEqual(issue.parents, [])
  2469. self.assertEqual(len(issue_blocked.children), 0)
  2470. self.assertEqual(issue_blocked.children, [])
  2471. self.assertEqual(len(issue_blocked.parents), 1)
  2472. self.assertEqual(issue_blocked.parents[0].id, 1)
  2473. self.assertRaises(
  2474. pagure.exceptions.PagureException,
  2475. pagure.lib.query.remove_issue_dependency,
  2476. session=self.session,
  2477. issue=issue,
  2478. issue_blocked=issue,
  2479. user="pingou",
  2480. )
  2481. # Wrong order of issues
  2482. msg = pagure.lib.query.remove_issue_dependency(
  2483. session=self.session,
  2484. issue=issue,
  2485. issue_blocked=issue_blocked,
  2486. user="pingou",
  2487. )
  2488. self.session.commit()
  2489. self.assertEqual(msg, None)
  2490. # Drop deps
  2491. msg = pagure.lib.query.remove_issue_dependency(
  2492. session=self.session,
  2493. issue=issue_blocked,
  2494. issue_blocked=issue,
  2495. user="pingou",
  2496. )
  2497. self.session.commit()
  2498. self.assertEqual(msg, "Issue **un**marked as depending on: #1")
  2499. # After
  2500. self.assertEqual(issue.parents, [])
  2501. self.assertEqual(issue.children, [])
  2502. self.assertEqual(issue_blocked.parents, [])
  2503. self.assertEqual(issue_blocked.children, [])
  2504. @patch("pagure.lib.git.update_git")
  2505. @patch("pagure.lib.notify.send_email")
  2506. def test_get_issue_comment(self, p_send_email, p_ugt):
  2507. """ Test the get_issue_comment of pagure.lib.query. """
  2508. p_send_email.return_value = True
  2509. p_ugt.return_value = True
  2510. self.test_add_issue_comment()
  2511. repo = pagure.lib.query._get_project(self.session, "test")
  2512. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2513. self.assertEqual(
  2514. pagure.lib.query.get_issue_comment(self.session, issue.uid, 10),
  2515. None,
  2516. )
  2517. comment = pagure.lib.query.get_issue_comment(
  2518. self.session, issue.uid, 1
  2519. )
  2520. self.assertEqual(comment.comment, "Hey look a comment!")
  2521. @patch("pagure.lib.git.update_git")
  2522. @patch("pagure.lib.notify.send_email")
  2523. def test_get_issue_by_uid(self, p_send_email, p_ugt):
  2524. """ Test the get_issue_by_uid of pagure.lib.query. """
  2525. p_send_email.return_value = True
  2526. p_ugt.return_value = True
  2527. self.test_new_issue()
  2528. repo = pagure.lib.query._get_project(self.session, "test")
  2529. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2530. self.assertEqual(
  2531. pagure.lib.query.get_issue_by_uid(self.session, "foobar"), None
  2532. )
  2533. new_issue = pagure.lib.query.get_issue_by_uid(self.session, issue.uid)
  2534. self.assertEqual(issue, new_issue)
  2535. @patch("pagure.lib.git.update_git")
  2536. @patch("pagure.lib.notify.send_email")
  2537. def test_update_tags(self, p_send_email, p_ugt):
  2538. """ Test the update_tags of pagure.lib.query. """
  2539. p_send_email.return_value = True
  2540. p_ugt.return_value = True
  2541. self.test_new_issue()
  2542. repo = pagure.lib.query._get_project(self.session, "test")
  2543. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2544. # before
  2545. self.assertEqual(repo.tags_colored, [])
  2546. self.assertEqual(issue.tags_text, [])
  2547. messages = pagure.lib.query.update_tags(
  2548. self.session, issue, "tag", "pingou"
  2549. )
  2550. self.assertEqual(messages, ["Issue tagged with: tag"])
  2551. # after
  2552. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2553. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2554. self.assertEqual([t.tag for t in repo.tags_colored], ["tag"])
  2555. self.assertEqual(issue.tags_text, ["tag"])
  2556. # Replace the tag by two others
  2557. messages = pagure.lib.query.update_tags(
  2558. self.session, issue, ["tag2", "tag3"], "pingou"
  2559. )
  2560. self.assertEqual(
  2561. messages,
  2562. ["Issue tagged with: tag2, tag3", "Issue **un**tagged with: tag"],
  2563. )
  2564. # after
  2565. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2566. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2567. self.assertEqual(
  2568. sorted([t.tag for t in repo.tags_colored]), ["tag", "tag2", "tag3"]
  2569. )
  2570. self.assertEqual(sorted(issue.tags_text), ["tag2", "tag3"])
  2571. @patch("pagure.lib.git.update_git")
  2572. @patch("pagure.lib.notify.send_email")
  2573. def test_update_dependency_issue(self, p_send_email, p_ugt):
  2574. """ Test the update_dependency_issue of pagure.lib.query. """
  2575. p_send_email.return_value = True
  2576. p_ugt.return_value = True
  2577. self.test_new_issue()
  2578. repo = pagure.lib.query._get_project(self.session, "test")
  2579. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2580. self.assertEqual(repo.open_tickets, 2)
  2581. self.assertEqual(repo.open_tickets_public, 2)
  2582. # Create issues to play with
  2583. msg = pagure.lib.query.new_issue(
  2584. session=self.session,
  2585. repo=repo,
  2586. title="Test issue #3",
  2587. content="We should work on this (3rd time!)",
  2588. user="pingou",
  2589. private=True,
  2590. )
  2591. self.session.commit()
  2592. self.assertEqual(msg.title, "Test issue #3")
  2593. self.assertEqual(repo.open_tickets, 3)
  2594. self.assertEqual(repo.open_tickets_public, 2)
  2595. # before
  2596. self.assertEqual(issue.tags_text, [])
  2597. self.assertEqual(issue.depending_text, [])
  2598. self.assertEqual(issue.blocking_text, [])
  2599. messages = pagure.lib.query.update_dependency_issue(
  2600. self.session, repo, issue, "2", "pingou"
  2601. )
  2602. self.assertEqual(messages, ["Issue marked as depending on: #2"])
  2603. messages = pagure.lib.query.update_dependency_issue(
  2604. self.session, repo, issue, ["3", "4", 5], "pingou"
  2605. )
  2606. self.assertEqual(
  2607. messages,
  2608. [
  2609. "Issue marked as depending on: #3",
  2610. "Issue marked as depending on: #4",
  2611. "Issue marked as depending on: #5",
  2612. "Issue **un**marked as depending on: #2",
  2613. ],
  2614. )
  2615. # after
  2616. self.assertEqual(issue.tags_text, [])
  2617. self.assertEqual(issue.depending_text, [3])
  2618. self.assertEqual(issue.blocking_text, [])
  2619. @patch("pagure.lib.git.update_git")
  2620. @patch("pagure.lib.notify.send_email")
  2621. def test_update_blocked_issue(self, p_send_email, p_ugt):
  2622. """ Test the update_blocked_issue of pagure.lib.query. """
  2623. p_send_email.return_value = True
  2624. p_ugt.return_value = True
  2625. self.test_new_issue()
  2626. repo = pagure.lib.query._get_project(self.session, "test")
  2627. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  2628. # Create issues to play with
  2629. msg = pagure.lib.query.new_issue(
  2630. session=self.session,
  2631. repo=repo,
  2632. title="Test issue #3",
  2633. content="We should work on this (3rd time!)",
  2634. user="pingou",
  2635. private=True,
  2636. )
  2637. self.session.commit()
  2638. self.assertEqual(msg.title, "Test issue #3")
  2639. # before
  2640. self.assertEqual(issue.tags_text, [])
  2641. self.assertEqual(issue.depending_text, [])
  2642. self.assertEqual(issue.blocking_text, [])
  2643. messages = pagure.lib.query.update_blocked_issue(
  2644. self.session, repo, issue, "2", "pingou"
  2645. )
  2646. self.assertEqual(messages, ["Issue marked as blocking: #2"])
  2647. messages = pagure.lib.query.update_blocked_issue(
  2648. self.session, repo, issue, ["3", "4", 5], "pingou"
  2649. )
  2650. self.assertEqual(
  2651. messages,
  2652. [
  2653. "Issue marked as blocking: #3",
  2654. "Issue marked as blocking: #4",
  2655. "Issue marked as blocking: #5",
  2656. "Issue **un**marked as blocking: #2",
  2657. ],
  2658. )
  2659. # after
  2660. self.assertEqual(issue.tags_text, [])
  2661. self.assertEqual(issue.depending_text, [])
  2662. self.assertEqual(issue.blocking_text, [3])
  2663. @patch("pagure.lib.notify.send_email")
  2664. def test_add_pull_request_assignee(self, mockemail):
  2665. """ Test add_pull_request_assignee of pagure.lib.query. """
  2666. mockemail.return_value = True
  2667. self.test_new_pull_request()
  2668. request = pagure.lib.query.search_pull_requests(
  2669. self.session, requestid=1
  2670. )
  2671. self.assertRaises(
  2672. pagure.exceptions.PagureException,
  2673. pagure.lib.query.add_pull_request_assignee,
  2674. session=self.session,
  2675. request=request,
  2676. assignee="bar",
  2677. user="foo",
  2678. )
  2679. # Assign
  2680. msg = pagure.lib.query.add_pull_request_assignee(
  2681. session=self.session,
  2682. request=request,
  2683. assignee="pingou",
  2684. user="foo",
  2685. )
  2686. self.assertEqual(msg, "Request assigned")
  2687. # Reset
  2688. msg = pagure.lib.query.add_pull_request_assignee(
  2689. session=self.session, request=request, assignee=None, user="foo"
  2690. )
  2691. self.assertEqual(msg, "Request assignee reset")
  2692. # Try resetting again
  2693. msg = pagure.lib.query.add_pull_request_assignee(
  2694. session=self.session, request=request, assignee=None, user="foo"
  2695. )
  2696. self.assertEqual(msg, None)
  2697. def test_search_pending_email(self):
  2698. """ Test search_pending_email of pagure.lib.query. """
  2699. self.assertEqual(
  2700. pagure.lib.query.search_pending_email(self.session), None
  2701. )
  2702. user = pagure.lib.query.search_user(self.session, username="pingou")
  2703. email_pend = pagure.lib.model.UserEmailPending(
  2704. user_id=user.id, email="foo@fp.o", token="abcdef"
  2705. )
  2706. self.session.add(email_pend)
  2707. self.session.commit()
  2708. self.assertNotEqual(
  2709. pagure.lib.query.search_pending_email(self.session), None
  2710. )
  2711. self.assertNotEqual(
  2712. pagure.lib.query.search_pending_email(
  2713. self.session, token="abcdef"
  2714. ),
  2715. None,
  2716. )
  2717. pend = pagure.lib.query.search_pending_email(
  2718. self.session, token="abcdef"
  2719. )
  2720. self.assertEqual(pend.user.username, "pingou")
  2721. self.assertEqual(pend.email, "foo@fp.o")
  2722. self.assertEqual(pend.token, "abcdef")
  2723. pend = pagure.lib.query.search_pending_email(
  2724. self.session, email="foo@fp.o"
  2725. )
  2726. self.assertEqual(pend.user.username, "pingou")
  2727. self.assertEqual(pend.email, "foo@fp.o")
  2728. self.assertEqual(pend.token, "abcdef")
  2729. def test_generate_hook_token(self):
  2730. """ Test generate_hook_token of pagure.lib.query. """
  2731. tests.create_projects(self.session)
  2732. projects = pagure.lib.query.search_projects(self.session)
  2733. for proj in projects:
  2734. self.assertIn(
  2735. proj.hook_token, ["aaabbbccc", "aaabbbddd", "aaabbbeee"]
  2736. )
  2737. pagure.lib.query.generate_hook_token(self.session)
  2738. projects = pagure.lib.query.search_projects(self.session)
  2739. for proj in projects:
  2740. self.assertNotIn(
  2741. proj.hook_token, ["aaabbbccc", "aaabbbddd", "aaabbbeee"]
  2742. )
  2743. @patch("pagure.lib.notify.send_email")
  2744. def test_pull_request_score(self, mockemail):
  2745. """ Test PullRequest.score of pagure.lib.model. """
  2746. mockemail.return_value = True
  2747. self.test_new_pull_request()
  2748. request = pagure.lib.query.search_pull_requests(
  2749. self.session, requestid=1
  2750. )
  2751. msg = pagure.lib.query.add_pull_request_comment(
  2752. session=self.session,
  2753. request=request,
  2754. commit=None,
  2755. tree_id=None,
  2756. filename=None,
  2757. row=None,
  2758. comment="This looks great :thumbsup:",
  2759. user="foo",
  2760. )
  2761. self.session.commit()
  2762. self.assertEqual(msg, "Comment added")
  2763. msg = pagure.lib.query.add_pull_request_comment(
  2764. session=self.session,
  2765. request=request,
  2766. commit=None,
  2767. tree_id=None,
  2768. filename=None,
  2769. row=None,
  2770. comment="I disagree -1",
  2771. user="pingou",
  2772. )
  2773. self.session.commit()
  2774. self.assertEqual(msg, "Comment added")
  2775. msg = pagure.lib.query.add_pull_request_comment(
  2776. session=self.session,
  2777. request=request,
  2778. commit=None,
  2779. tree_id=None,
  2780. filename=None,
  2781. row=None,
  2782. comment="NM this looks great now +1000",
  2783. user="pingou",
  2784. )
  2785. self.session.commit()
  2786. self.assertEqual(msg, "Comment added")
  2787. self.assertEqual(len(request.discussion), 3)
  2788. self.assertEqual(request.score, 2)
  2789. def test_add_group(self):
  2790. """ Test the add_group method of pagure.lib.query. """
  2791. groups = pagure.lib.query.search_groups(self.session)
  2792. self.assertEqual(len(groups), 0)
  2793. self.assertEqual(groups, [])
  2794. # Invalid type
  2795. self.assertRaises(
  2796. pagure.exceptions.PagureException,
  2797. pagure.lib.query.add_group,
  2798. self.session,
  2799. group_name="foo",
  2800. display_name="foo group",
  2801. description=None,
  2802. group_type="bar",
  2803. user="pingou",
  2804. is_admin=True,
  2805. blacklist=[],
  2806. )
  2807. groups = pagure.lib.query.search_groups(self.session)
  2808. self.assertEqual(len(groups), 0)
  2809. self.assertEqual(groups, [])
  2810. # Invalid user
  2811. self.assertRaises(
  2812. pagure.exceptions.PagureException,
  2813. pagure.lib.query.add_group,
  2814. self.session,
  2815. group_name="foo",
  2816. display_name="foo group",
  2817. description=None,
  2818. group_type="user",
  2819. user="test",
  2820. is_admin=False,
  2821. blacklist=[],
  2822. )
  2823. groups = pagure.lib.query.search_groups(self.session)
  2824. self.assertEqual(len(groups), 0)
  2825. self.assertEqual(groups, [])
  2826. # Invalid group name
  2827. self.assertRaises(
  2828. pagure.exceptions.PagureException,
  2829. pagure.lib.query.add_group,
  2830. self.session,
  2831. group_name="foo group",
  2832. display_name="foo group",
  2833. description=None,
  2834. group_type="user",
  2835. user="test",
  2836. is_admin=False,
  2837. blacklist=[],
  2838. )
  2839. groups = pagure.lib.query.search_groups(self.session)
  2840. self.assertEqual(len(groups), 0)
  2841. self.assertEqual(groups, [])
  2842. msg = pagure.lib.query.add_group(
  2843. self.session,
  2844. group_name="foo",
  2845. display_name="foo group",
  2846. description=None,
  2847. group_type="bar",
  2848. user="pingou",
  2849. is_admin=False,
  2850. blacklist=[],
  2851. )
  2852. self.session.commit()
  2853. self.assertEqual(msg, "User `pingou` added to the group `foo`.")
  2854. groups = pagure.lib.query.search_groups(self.session)
  2855. self.assertEqual(len(groups), 1)
  2856. self.assertEqual(groups[0].group_name, "foo")
  2857. # Group with this name already exists
  2858. self.assertRaises(
  2859. pagure.exceptions.PagureException,
  2860. pagure.lib.query.add_group,
  2861. self.session,
  2862. group_name="foo",
  2863. display_name="foo group",
  2864. description=None,
  2865. group_type="bar",
  2866. user="pingou",
  2867. is_admin=False,
  2868. blacklist=[],
  2869. )
  2870. # Group with this display name already exists
  2871. self.assertRaises(
  2872. pagure.exceptions.PagureException,
  2873. pagure.lib.query.add_group,
  2874. self.session,
  2875. group_name="foo1",
  2876. display_name="foo group",
  2877. description=None,
  2878. group_type="bar",
  2879. user="pingou",
  2880. is_admin=False,
  2881. blacklist=[],
  2882. )
  2883. # Group with a blacklisted prefix
  2884. self.assertRaises(
  2885. pagure.exceptions.PagureException,
  2886. pagure.lib.query.add_group,
  2887. self.session,
  2888. group_name="forks",
  2889. display_name="foo group",
  2890. description=None,
  2891. group_type="bar",
  2892. user="pingou",
  2893. is_admin=False,
  2894. blacklist=["forks"],
  2895. )
  2896. def test_add_user_to_group(self):
  2897. """ Test the add_user_to_group method of pagure.lib.query. """
  2898. self.test_add_group()
  2899. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  2900. self.assertNotEqual(group, None)
  2901. self.assertEqual(group.group_name, "foo")
  2902. # Invalid new user
  2903. self.assertRaises(
  2904. pagure.exceptions.PagureException,
  2905. pagure.lib.query.add_user_to_group,
  2906. self.session,
  2907. username="foobar",
  2908. group=group,
  2909. user="foo",
  2910. is_admin=False,
  2911. )
  2912. # Invalid user
  2913. self.assertRaises(
  2914. pagure.exceptions.PagureException,
  2915. pagure.lib.query.add_user_to_group,
  2916. self.session,
  2917. username="foo",
  2918. group=group,
  2919. user="foobar",
  2920. is_admin=False,
  2921. )
  2922. # User not allowed
  2923. self.assertRaises(
  2924. pagure.exceptions.PagureException,
  2925. pagure.lib.query.add_user_to_group,
  2926. self.session,
  2927. username="foo",
  2928. group=group,
  2929. user="foo",
  2930. is_admin=False,
  2931. )
  2932. msg = pagure.lib.query.add_user_to_group(
  2933. self.session,
  2934. username="foo",
  2935. group=group,
  2936. user="pingou",
  2937. is_admin=False,
  2938. )
  2939. self.session.commit()
  2940. self.assertEqual(msg, "User `foo` added to the group `foo`.")
  2941. msg = pagure.lib.query.add_user_to_group(
  2942. self.session,
  2943. username="foo",
  2944. group=group,
  2945. user="pingou",
  2946. is_admin=False,
  2947. )
  2948. self.session.commit()
  2949. self.assertEqual(
  2950. msg, "User `foo` already in the group, nothing to change."
  2951. )
  2952. def test_is_group_member(self):
  2953. """ Test the is_group_member method of pagure.lib.query. """
  2954. self.test_add_group()
  2955. self.assertFalse(
  2956. pagure.lib.query.is_group_member(self.session, None, "foo")
  2957. )
  2958. self.assertFalse(
  2959. pagure.lib.query.is_group_member(self.session, "bar", "foo")
  2960. )
  2961. self.assertFalse(
  2962. pagure.lib.query.is_group_member(self.session, "foo", "foo")
  2963. )
  2964. self.assertTrue(
  2965. pagure.lib.query.is_group_member(self.session, "pingou", "foo")
  2966. )
  2967. def test_get_user_group(self):
  2968. """ Test the get_user_group method of pagure.lib.query. """
  2969. self.test_add_group()
  2970. item = pagure.lib.query.get_user_group(self.session, 1, 1)
  2971. self.assertEqual(item.user_id, 1)
  2972. self.assertEqual(item.group_id, 1)
  2973. item = pagure.lib.query.get_user_group(self.session, 1, 2)
  2974. self.assertEqual(item, None)
  2975. item = pagure.lib.query.get_user_group(self.session, 2, 1)
  2976. self.assertEqual(item, None)
  2977. def test_get_group_types(self):
  2978. """ Test the get_group_types method of pagure.lib.query. """
  2979. self.test_add_group()
  2980. groups = pagure.lib.query.get_group_types(self.session, "user")
  2981. self.assertEqual(len(groups), 1)
  2982. self.assertEqual(groups[0].group_type, "user")
  2983. groups = pagure.lib.query.get_group_types(self.session)
  2984. self.assertEqual(len(groups), 2)
  2985. self.assertEqual(groups[0].group_type, "admin")
  2986. self.assertEqual(groups[1].group_type, "user")
  2987. def test_search_groups(self):
  2988. """ Test the search_groups method of pagure.lib.query. """
  2989. self.assertEqual(pagure.lib.query.search_groups(self.session), [])
  2990. msg = pagure.lib.query.add_group(
  2991. self.session,
  2992. group_name="foo",
  2993. display_name="foo group",
  2994. description=None,
  2995. group_type="bar",
  2996. user="pingou",
  2997. is_admin=False,
  2998. blacklist=[],
  2999. )
  3000. self.session.commit()
  3001. self.assertEqual(msg, "User `pingou` added to the group `foo`.")
  3002. groups = pagure.lib.query.search_groups(self.session)
  3003. self.assertEqual(len(groups), 1)
  3004. self.assertEqual(groups[0].group_name, "foo")
  3005. msg = pagure.lib.query.add_group(
  3006. self.session,
  3007. group_name="bar",
  3008. display_name="bar group",
  3009. description=None,
  3010. group_type="admin",
  3011. user="pingou",
  3012. is_admin=True,
  3013. blacklist=[],
  3014. )
  3015. self.session.commit()
  3016. self.assertEqual(msg, "User `pingou` added to the group `bar`.")
  3017. groups = pagure.lib.query.search_groups(self.session)
  3018. self.assertEqual(len(groups), 2)
  3019. self.assertEqual(groups[0].group_name, "bar")
  3020. self.assertEqual(groups[1].group_name, "foo")
  3021. groups = pagure.lib.query.search_groups(
  3022. self.session, group_type="user"
  3023. )
  3024. self.assertEqual(len(groups), 1)
  3025. self.assertEqual(groups[0].group_name, "foo")
  3026. groups = pagure.lib.query.search_groups(
  3027. self.session, group_type="admin"
  3028. )
  3029. self.assertEqual(len(groups), 1)
  3030. self.assertEqual(groups[0].group_name, "bar")
  3031. groups = pagure.lib.query.search_groups(self.session, group_name="foo")
  3032. self.assertEqual(groups.group_name, "foo")
  3033. def test_delete_user_of_group(self):
  3034. """ Test the delete_user_of_group method of pagure.lib.query. """
  3035. self.test_add_user_to_group()
  3036. groups = pagure.lib.query.search_groups(self.session)
  3037. self.assertEqual(len(groups), 1)
  3038. self.assertEqual(groups[0].group_name, "foo")
  3039. # Invalid username
  3040. self.assertRaises(
  3041. pagure.exceptions.PagureException,
  3042. pagure.lib.query.delete_user_of_group,
  3043. self.session,
  3044. username="bar",
  3045. groupname="foo",
  3046. user="pingou",
  3047. is_admin=False,
  3048. )
  3049. # Invalid groupname
  3050. self.assertRaises(
  3051. pagure.exceptions.PagureException,
  3052. pagure.lib.query.delete_user_of_group,
  3053. self.session,
  3054. username="foo",
  3055. groupname="bar",
  3056. user="pingou",
  3057. is_admin=False,
  3058. )
  3059. # Invalid user
  3060. self.assertRaises(
  3061. pagure.exceptions.PagureException,
  3062. pagure.lib.query.delete_user_of_group,
  3063. self.session,
  3064. username="foo",
  3065. groupname="foo",
  3066. user="test",
  3067. is_admin=False,
  3068. )
  3069. # User not in the group
  3070. item = pagure.lib.model.User(
  3071. user="bar",
  3072. fullname="bar",
  3073. password="foo",
  3074. default_email="bar@bar.com",
  3075. )
  3076. self.session.add(item)
  3077. item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
  3078. self.session.add(item)
  3079. self.session.commit()
  3080. self.assertRaises(
  3081. pagure.exceptions.PagureException,
  3082. pagure.lib.query.delete_user_of_group,
  3083. self.session,
  3084. username="bar",
  3085. groupname="foo",
  3086. user="pingou",
  3087. is_admin=False,
  3088. )
  3089. # User is not allowed to remove the username
  3090. self.assertRaises(
  3091. pagure.exceptions.PagureException,
  3092. pagure.lib.query.delete_user_of_group,
  3093. self.session,
  3094. username="foo",
  3095. groupname="foo",
  3096. user="bar",
  3097. is_admin=False,
  3098. )
  3099. # Username is the creator of the group
  3100. self.assertRaises(
  3101. pagure.exceptions.PagureException,
  3102. pagure.lib.query.delete_user_of_group,
  3103. self.session,
  3104. username="pingou",
  3105. groupname="foo",
  3106. user="pingou",
  3107. is_admin=False,
  3108. )
  3109. # All good
  3110. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3111. self.assertEqual(len(group.users), 2)
  3112. pagure.lib.query.delete_user_of_group(
  3113. self.session,
  3114. username="foo",
  3115. groupname="foo",
  3116. user="pingou",
  3117. is_admin=False,
  3118. )
  3119. self.session.commit()
  3120. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3121. self.assertEqual(len(group.users), 1)
  3122. def test_edit_group_info(self):
  3123. """ Test the edit_group_info method of pagure.lib.query. """
  3124. self.test_add_group()
  3125. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3126. self.assertNotEqual(group, None)
  3127. self.assertEqual(group.group_name, "foo")
  3128. # Invalid new user
  3129. self.assertRaises(
  3130. pagure.exceptions.PagureException,
  3131. pagure.lib.query.edit_group_info,
  3132. self.session,
  3133. group=group,
  3134. display_name="edited name",
  3135. description=None,
  3136. user="foo",
  3137. is_admin=False,
  3138. )
  3139. # Invalid user
  3140. self.assertRaises(
  3141. pagure.exceptions.PagureException,
  3142. pagure.lib.query.edit_group_info,
  3143. self.session,
  3144. group=group,
  3145. display_name="edited name",
  3146. description=None,
  3147. user="foobar",
  3148. is_admin=False,
  3149. )
  3150. # User not allowed
  3151. self.assertRaises(
  3152. pagure.exceptions.PagureException,
  3153. pagure.lib.query.edit_group_info,
  3154. self.session,
  3155. group=group,
  3156. display_name="edited name",
  3157. description=None,
  3158. user="bar",
  3159. is_admin=False,
  3160. )
  3161. msg = pagure.lib.query.edit_group_info(
  3162. self.session,
  3163. group=group,
  3164. display_name="edited name",
  3165. description=None,
  3166. user="pingou",
  3167. is_admin=False,
  3168. )
  3169. self.session.commit()
  3170. self.assertEqual(msg, 'Group "edited name" (foo) edited')
  3171. msg = pagure.lib.query.edit_group_info(
  3172. self.session,
  3173. group=group,
  3174. display_name="edited name",
  3175. description=None,
  3176. user="pingou",
  3177. is_admin=False,
  3178. )
  3179. self.session.commit()
  3180. self.assertEqual(msg, "Nothing changed")
  3181. def test_add_group_to_project(self):
  3182. """ Test the add_group_to_project method of pagure.lib.query. """
  3183. tests.create_projects(self.session)
  3184. self.test_add_group()
  3185. project = pagure.lib.query._get_project(self.session, "test2")
  3186. # Group does not exist
  3187. self.assertRaises(
  3188. pagure.exceptions.PagureException,
  3189. pagure.lib.query.add_group_to_project,
  3190. session=self.session,
  3191. project=project,
  3192. new_group="bar",
  3193. user="foo",
  3194. )
  3195. # Group does not exist, but allow creating it
  3196. msg = pagure.lib.query.add_group_to_project(
  3197. session=self.session,
  3198. project=project,
  3199. new_group="bar",
  3200. user="pingou",
  3201. create=True,
  3202. )
  3203. self.session.commit()
  3204. self.assertEqual(msg, "Group added")
  3205. self.assertEqual(project.groups[0].group_name, "bar")
  3206. self.assertEqual(len(project.admin_groups), 1)
  3207. self.assertEqual(project.admin_groups[0].group_name, "bar")
  3208. # User does not exist
  3209. self.assertRaises(
  3210. pagure.exceptions.PagureException,
  3211. pagure.lib.query.add_group_to_project,
  3212. session=self.session,
  3213. project=project,
  3214. new_group="foo",
  3215. user="bar",
  3216. )
  3217. # User not allowed
  3218. self.assertRaises(
  3219. pagure.exceptions.PagureException,
  3220. pagure.lib.query.add_group_to_project,
  3221. session=self.session,
  3222. project=project,
  3223. new_group="foo",
  3224. user="foo",
  3225. )
  3226. # All good
  3227. msg = pagure.lib.query.add_group_to_project(
  3228. session=self.session,
  3229. project=project,
  3230. new_group="foo",
  3231. user="pingou",
  3232. )
  3233. self.session.commit()
  3234. self.assertEqual(msg, "Group added")
  3235. self.assertEqual(project.groups[0].group_name, "bar")
  3236. self.assertEqual(project.groups[1].group_name, "foo")
  3237. self.assertEqual(len(project.admin_groups), 2)
  3238. self.assertEqual(project.admin_groups[0].group_name, "bar")
  3239. self.assertEqual(project.admin_groups[1].group_name, "foo")
  3240. self.assertEqual(len(project.committer_groups), 2)
  3241. # Group already associated with the project
  3242. self.assertRaises(
  3243. pagure.exceptions.PagureException,
  3244. pagure.lib.query.add_group_to_project,
  3245. session=self.session,
  3246. project=project,
  3247. new_group="foo",
  3248. user="pingou",
  3249. )
  3250. # Update the access of group in the project
  3251. msg = pagure.lib.query.add_group_to_project(
  3252. session=self.session,
  3253. project=project,
  3254. new_group="foo",
  3255. user="pingou",
  3256. access="commit",
  3257. )
  3258. self.session.commit()
  3259. self.assertEqual(msg, "Group access updated")
  3260. self.assertEqual(project.groups[0].group_name, "bar")
  3261. self.assertEqual(project.groups[1].group_name, "foo")
  3262. self.assertEqual(len(project.admin_groups), 1)
  3263. self.assertEqual(project.admin_groups[0].group_name, "bar")
  3264. self.assertEqual(len(project.committer_groups), 2)
  3265. # Update the access of group in the project
  3266. msg = pagure.lib.query.add_group_to_project(
  3267. session=self.session,
  3268. project=project,
  3269. new_group="foo",
  3270. user="pingou",
  3271. access="ticket",
  3272. )
  3273. self.session.commit()
  3274. self.assertEqual(msg, "Group access updated")
  3275. self.assertEqual(project.groups[0].group_name, "bar")
  3276. self.assertEqual(project.groups[1].group_name, "foo")
  3277. self.assertEqual(len(project.admin_groups), 1)
  3278. self.assertEqual(project.admin_groups[0].group_name, "bar")
  3279. self.assertEqual(len(project.committer_groups), 1)
  3280. self.assertEqual(project.committer_groups[0].group_name, "bar")
  3281. def test_update_watch_status(self):
  3282. """ Test the update_watch_status method of pagure.lib.query. """
  3283. tests.create_projects(self.session)
  3284. project = pagure.lib.query._get_project(self.session, "test")
  3285. # User does not exist
  3286. self.assertRaises(
  3287. pagure.exceptions.PagureException,
  3288. pagure.lib.query.update_watch_status,
  3289. session=self.session,
  3290. project=project,
  3291. user="aavrug",
  3292. watch="1",
  3293. )
  3294. # Invalid watch status
  3295. self.assertRaises(
  3296. pagure.exceptions.PagureException,
  3297. pagure.lib.query.update_watch_status,
  3298. session=self.session,
  3299. project=project,
  3300. user="pingou",
  3301. watch="me fail",
  3302. )
  3303. # All good and when user selected reset watch option.
  3304. msg = pagure.lib.query.update_watch_status(
  3305. session=self.session, project=project, user="pingou", watch="-1"
  3306. )
  3307. self.session.commit()
  3308. self.assertEqual(msg, "Watch status is already reset")
  3309. # All good and when user selected watch issues option.
  3310. msg = pagure.lib.query.update_watch_status(
  3311. session=self.session, project=project, user="pingou", watch="1"
  3312. )
  3313. self.session.commit()
  3314. self.assertEqual(
  3315. msg, "You are now watching issues and PRs on this project"
  3316. )
  3317. # All good and when user selected unwatch option.
  3318. msg = pagure.lib.query.update_watch_status(
  3319. session=self.session, project=project, user="pingou", watch="0"
  3320. )
  3321. self.session.commit()
  3322. self.assertEqual(msg, "You are no longer watching this project")
  3323. # All good and when user seleted reset watch option.
  3324. msg = pagure.lib.query.update_watch_status(
  3325. session=self.session, project=project, user="pingou", watch="-1"
  3326. )
  3327. self.session.commit()
  3328. self.assertEqual(msg, "Watch status reset")
  3329. def test_get_watch_level_on_repo_invalid(self):
  3330. """ test the get_watch_level_on_repo method of pagure.lib.query. """
  3331. self.assertRaises(
  3332. RuntimeError,
  3333. pagure.lib.query.get_watch_level_on_repo,
  3334. session=self.session,
  3335. user="pingou",
  3336. repo=None,
  3337. repouser=None,
  3338. namespace=None,
  3339. )
  3340. def test_get_watch_level_on_repo(self):
  3341. """ Test the get_watch_level_on_repo method of pagure.lib.query. """
  3342. tests.create_projects(self.session)
  3343. self.test_add_group()
  3344. project = pagure.lib.query._get_project(self.session, "test")
  3345. project2 = pagure.lib.query._get_project(self.session, "test2")
  3346. # If user not logged in
  3347. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3348. session=self.session, user=None, repo="test"
  3349. )
  3350. self.assertEqual(watch_level, [])
  3351. # User does not exist
  3352. user = tests.FakeUser()
  3353. user.username = "aavrug"
  3354. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3355. session=self.session, user=user, repo="test"
  3356. )
  3357. self.assertEqual(watch_level, [])
  3358. # Invalid project
  3359. watch = pagure.lib.query.get_watch_level_on_repo(
  3360. session=self.session, user=user, repo="invalid"
  3361. )
  3362. self.assertFalse(watch)
  3363. pagure.lib.query.add_group_to_project(
  3364. session=self.session,
  3365. project=project,
  3366. new_group="foo",
  3367. user="pingou",
  3368. )
  3369. self.session.commit()
  3370. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3371. pagure.lib.query.add_user_to_group(
  3372. self.session,
  3373. username="foo",
  3374. group=group,
  3375. user="pingou",
  3376. is_admin=False,
  3377. )
  3378. self.session.commit()
  3379. group = pagure.lib.query.search_groups(self.session, group_name="foo")
  3380. # If user belongs to any group of that project
  3381. user.username = "foo"
  3382. msg = watch_level = pagure.lib.query.get_watch_level_on_repo(
  3383. session=self.session, user=user, repo="test"
  3384. )
  3385. self.assertEqual(watch_level, ["issues"])
  3386. # If user is the creator
  3387. user.username = "pingou"
  3388. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3389. session=self.session, user=user, repo="test"
  3390. )
  3391. self.assertEqual(watch_level, ["issues"])
  3392. # Entry into watchers table for issues and commits
  3393. msg = pagure.lib.query.update_watch_status(
  3394. session=self.session, project=project, user="pingou", watch="3"
  3395. )
  3396. self.session.commit()
  3397. self.assertEqual(
  3398. msg,
  3399. "You are now watching issues, PRs, and commits on this project",
  3400. )
  3401. # From watchers table
  3402. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3403. session=self.session, user=user, repo="test"
  3404. )
  3405. self.assertEqual(["issues", "commits"], watch_level)
  3406. # Make sure that when a user watches more than one repo explicitly
  3407. # they get the correct watch status
  3408. msg = pagure.lib.query.update_watch_status(
  3409. session=self.session, project=project2, user="pingou", watch="1"
  3410. )
  3411. self.session.commit()
  3412. self.assertEqual(
  3413. msg, "You are now watching issues and PRs on this project"
  3414. )
  3415. # From watchers table
  3416. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3417. session=self.session, user=user, repo="test2"
  3418. )
  3419. self.assertEqual(["issues"], watch_level)
  3420. # Entry into watchers table for just commits
  3421. msg = pagure.lib.query.update_watch_status(
  3422. session=self.session, project=project, user="pingou", watch="2"
  3423. )
  3424. self.session.commit()
  3425. self.assertEqual(msg, "You are now watching commits on this project")
  3426. # From watchers table
  3427. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3428. session=self.session, user=user, repo="test"
  3429. )
  3430. self.assertEqual(["commits"], watch_level)
  3431. # Entry into watchers table for issues
  3432. msg = pagure.lib.query.update_watch_status(
  3433. session=self.session, project=project, user="pingou", watch="1"
  3434. )
  3435. self.session.commit()
  3436. self.assertEqual(
  3437. msg, "You are now watching issues and PRs on this project"
  3438. )
  3439. # From watchers table
  3440. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3441. session=self.session, user=user, repo="test"
  3442. )
  3443. self.assertEqual(["issues"], watch_level)
  3444. # Entry into watchers table for no watching
  3445. msg = pagure.lib.query.update_watch_status(
  3446. session=self.session, project=project, user="pingou", watch="0"
  3447. )
  3448. self.session.commit()
  3449. self.assertEqual(msg, "You are no longer watching this project")
  3450. # From watchers table
  3451. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3452. session=self.session, user=user, repo="test"
  3453. )
  3454. self.assertEqual(watch_level, [])
  3455. # Add a contributor to the project
  3456. item = pagure.lib.model.User(
  3457. user="bar",
  3458. fullname="bar foo",
  3459. password="foo",
  3460. default_email="bar@bar.com",
  3461. )
  3462. self.session.add(item)
  3463. item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
  3464. self.session.add(item)
  3465. msg = pagure.lib.query.add_user_to_project(
  3466. session=self.session,
  3467. project=project,
  3468. new_user="bar",
  3469. user="pingou",
  3470. )
  3471. self.session.commit()
  3472. self.assertEqual(msg, "User added")
  3473. # Check if the new contributor is watching
  3474. user.username = "bar"
  3475. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3476. session=self.session, user=user, repo="test"
  3477. )
  3478. self.assertEqual(watch_level, ["issues"])
  3479. # wrong project
  3480. user.username = "bar"
  3481. watch_level = pagure.lib.query.get_watch_level_on_repo(
  3482. session=self.session,
  3483. user=user,
  3484. repo="test",
  3485. namespace="somenamespace",
  3486. )
  3487. self.assertEqual(watch_level, [])
  3488. def test_user_watch_list(self):
  3489. """ test user watch list method of pagure.lib """
  3490. tests.create_projects(self.session)
  3491. # He should be watching
  3492. user = tests.FakeUser()
  3493. user.username = "pingou"
  3494. watch_list_objs = pagure.lib.query.user_watch_list(
  3495. session=self.session, user="pingou"
  3496. )
  3497. watch_list = [obj.name for obj in watch_list_objs]
  3498. self.assertEqual(watch_list, ["test", "test2", "test3"])
  3499. # Make pingou unwatch the test3 project
  3500. project = pagure.lib.query._get_project(
  3501. self.session, "test3", namespace="somenamespace"
  3502. )
  3503. msg = pagure.lib.query.update_watch_status(
  3504. session=self.session, project=project, user="pingou", watch="0"
  3505. )
  3506. self.session.commit()
  3507. self.assertEqual(msg, "You are no longer watching this project")
  3508. # Re-check the watch list
  3509. watch_list_objs = pagure.lib.query.user_watch_list(
  3510. session=self.session, user="pingou"
  3511. )
  3512. watch_list = [obj.name for obj in watch_list_objs]
  3513. self.assertEqual(watch_list, ["test", "test2"])
  3514. # He isn't in the db, thus not watching anything
  3515. user.username = "vivek"
  3516. watch_list_objs = pagure.lib.query.user_watch_list(
  3517. session=self.session, user="vivek"
  3518. )
  3519. watch_list = [obj.name for obj in watch_list_objs]
  3520. self.assertEqual(watch_list, [])
  3521. # He shouldn't be watching anything
  3522. user.username = "foo"
  3523. watch_list_objs = pagure.lib.query.user_watch_list(
  3524. session=self.session, user="foo"
  3525. )
  3526. watch_list = [obj.name for obj in watch_list_objs]
  3527. self.assertEqual(watch_list, [])
  3528. @patch("flask.request.url", "http://localhost.localdomain/test/issue/69")
  3529. @patch("flask.request.url_root", "http://localhost.localdomain/")
  3530. @patch("flask.request.args.get", return_value=None)
  3531. @patch("flask.request")
  3532. @patch("flask.g")
  3533. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3534. def test_text2markdown(self, g, req, reqget):
  3535. """ Test the text2markdown method in pagure.lib.query. """
  3536. pagure.config.config["TESTING"] = True
  3537. pagure.config.config["SERVER_NAME"] = "localhost.localdomain"
  3538. # This creates:
  3539. # project: test
  3540. # fork: pingou/test
  3541. # PR#1 to project test
  3542. self.test_new_pull_request()
  3543. # create an issue (will be #2) in 'test' project
  3544. repo = pagure.lib.query._get_project(self.session, "test")
  3545. iss = pagure.lib.query.new_issue(
  3546. issue_id=2,
  3547. session=self.session,
  3548. repo=repo,
  3549. title="test issue",
  3550. content="content test issue",
  3551. user="pingou",
  3552. )
  3553. self.session.commit()
  3554. self.assertEqual(iss.id, 2)
  3555. self.assertEqual(iss.title, "test issue")
  3556. # create PR#2 to project pingou/test
  3557. forked_repo = pagure.lib.query._get_project(
  3558. self.session, "test", user="pingou"
  3559. )
  3560. req = pagure.lib.query.new_pull_request(
  3561. requestid=2,
  3562. session=self.session,
  3563. repo_from=forked_repo,
  3564. branch_from="master",
  3565. repo_to=forked_repo,
  3566. branch_to="master",
  3567. title="test pull-request in fork",
  3568. user="pingou",
  3569. )
  3570. self.session.commit()
  3571. self.assertEqual(req.id, 2)
  3572. self.assertEqual(req.title, "test pull-request in fork")
  3573. # Create the project ns/test
  3574. item = pagure.lib.model.Project(
  3575. user_id=1, # pingou
  3576. name="test3",
  3577. namespace="ns",
  3578. description="test project #1",
  3579. hook_token="aaabbbcccdd",
  3580. )
  3581. item.close_status = [
  3582. "Invalid",
  3583. "Insufficient data",
  3584. "Fixed",
  3585. "Duplicate",
  3586. ]
  3587. self.session.add(item)
  3588. self.session.commit()
  3589. iss = pagure.lib.query.new_issue(
  3590. issue_id=4,
  3591. session=self.session,
  3592. repo=item,
  3593. title="test issue",
  3594. content="content test issue",
  3595. user="pingou",
  3596. )
  3597. self.session.commit()
  3598. self.assertEqual(iss.id, 4)
  3599. self.assertEqual(iss.title, "test issue")
  3600. # Fork ns/test to pingou
  3601. item = pagure.lib.model.Project(
  3602. user_id=1, # pingou
  3603. name="test",
  3604. namespace="ns",
  3605. description="Forked namespaced test project #1",
  3606. is_fork=True,
  3607. parent_id=item.id,
  3608. hook_token="aaabbbrrrbb",
  3609. )
  3610. self.session.add(item)
  3611. self.session.commit()
  3612. iss = pagure.lib.query.new_issue(
  3613. issue_id=7,
  3614. session=self.session,
  3615. repo=item,
  3616. title="test issue #7",
  3617. content="content test issue #7 in forked repo",
  3618. user="pingou",
  3619. )
  3620. self.session.commit()
  3621. self.assertEqual(iss.id, 7)
  3622. self.assertEqual(iss.title, "test issue #7")
  3623. iss = pagure.lib.query.new_issue(
  3624. issue_id=8,
  3625. session=self.session,
  3626. repo=item,
  3627. title="private issue #8",
  3628. content="Private content test issue #8 in forked repo",
  3629. user="pingou",
  3630. private=True,
  3631. )
  3632. self.session.commit()
  3633. self.assertEqual(iss.id, 8)
  3634. self.assertEqual(iss.title, "private issue #8")
  3635. # newer bleach allow to customize the protocol supported
  3636. import bleach
  3637. bleach_v = bleach.__version__.split(".")
  3638. for idx, val in enumerate(bleach_v):
  3639. try:
  3640. val = int(val)
  3641. except ValueError:
  3642. pass
  3643. bleach_v[idx] = val
  3644. # old markdown generate other html
  3645. import markdown
  3646. try:
  3647. markdown_v = markdown.__version__.version_info
  3648. except AttributeError: # pragma: no cover
  3649. markdown_v = markdown.__version_info__
  3650. # python-markdown >= 3.2.0 returns to the old behavior on img tag trailing slash
  3651. old_markdown = markdown_v < (2, 6, 0) or markdown_v >= (3, 2, 0)
  3652. mk_321 = markdown_v >= (3, 2, 0)
  3653. texts = [
  3654. "foo bar test#1 see?",
  3655. "foo bar pingou/test#2 I mean, really",
  3656. "foo bar fork/pingou/test#2 bouza!",
  3657. "foo bar forks/pingou/test#2 bouza!",
  3658. "foo bar ns/test3#4 bouza!",
  3659. "foo bar fork/user/ns/test#5 bouza!",
  3660. "foo bar fork/pingou/ns/test#7 bouza!",
  3661. "test#1 bazinga!",
  3662. "pingou opened the PR forks/pingou/test#2",
  3663. "fork/pingou/ns/test#8 is private",
  3664. "implicit link to #1",
  3665. "implicit link .#1. with non-whitespace, non-word characters",
  3666. "#2 - implicit link at start of line",
  3667. "#2. implicit link at start of line with no whitespace after",
  3668. "#regular header",
  3669. "#34 looks like an implicit link, but no issue 34",
  3670. "pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd",
  3671. "irc://pagure.io",
  3672. "ircs://pagure.io",
  3673. "http://pagure.io",
  3674. "https://pagure.io",
  3675. "<https://pagure.io/pagure>",
  3676. "~~foo~~",
  3677. "~~foo bar~~",
  3678. "~~[BZ#1435310](https://bugzilla.redhat.com/1435310)~~",
  3679. "~~[BZ#1435310](https://bugzilla.redhat.com/1435310) avc denial "
  3680. "during F26AH boot 'error_name=org.freedesktop.systemd1."
  3681. "NoSuchDynamicUser'~~",
  3682. "``~~foo bar~~``",
  3683. "~~foo bar~~ and ~~another ~~",
  3684. "lets mention @pingou",
  3685. "@pingou at start of line",
  3686. "but not someone@pingou.com",
  3687. "[![Fedora_infinity_small.png]"
  3688. "(/test/issue/raw/Fedora_infinity_small.png)]"
  3689. "(/test/issue/raw/Fedora_infinity_small.png)",
  3690. ]
  3691. expected = [
  3692. # 'foo bar test#1 see?',
  3693. '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/test/pull-request/1"'
  3694. ' title="[Open] test pull-request">test#1</a> see?</p></div>',
  3695. # 'foo bar pingou/test#2 I mean, really', -- unknown namespace
  3696. '<div class="markdown"><p>foo bar pingou/test#2 I mean, really</p></div>',
  3697. # 'foo bar fork/pingou/test#2 bouza!',
  3698. '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/fork/'
  3699. 'pingou/test/pull-request/2" title="[Open] test pull-request in fork">'
  3700. "pingou/test#2</a> bouza!</p></div>",
  3701. # 'foo bar forks/pingou/test#2 bouza!', -- the 's' doesn't matter
  3702. '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/fork/'
  3703. 'pingou/test/pull-request/2" title="[Open] test pull-request in fork">'
  3704. "pingou/test#2</a> bouza!</p></div>",
  3705. # 'foo bar ns/test3#4 bouza!',
  3706. '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/ns/test3/issue/4"'
  3707. ' title="[Open] test issue">ns/test3#4</a> bouza!</p></div>',
  3708. # 'foo bar fork/user/ns/test#5 bouza!', -- unknown fork
  3709. '<div class="markdown"><p>foo bar user/ns/test#5 bouza!</p></div>',
  3710. # 'foo bar fork/pingou/ns/test#7 bouza!',
  3711. '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/'
  3712. 'fork/pingou/ns/test/issue/7" title="[Open] test issue #7">'
  3713. "pingou/ns/test#7</a> bouza!</p></div>",
  3714. # 'test#1 bazinga!',
  3715. '<div class="markdown"><p><a href="http://localhost.localdomain/test/pull-request/1" '
  3716. 'title="[Open] test pull-request">test#1</a> bazinga!</p></div>',
  3717. # 'pingou opened the PR forks/pingou/test#2'
  3718. '<div class="markdown"><p>pingou opened the PR <a href="http://localhost.localdomain/'
  3719. 'fork/pingou/test/pull-request/2" '
  3720. 'title="[Open] test pull-request in fork">pingou/test#2</a></p></div>',
  3721. # 'fork/pingou/ns/test#8 is private',
  3722. '<div class="markdown"><p><a href="http://localhost.localdomain/fork/pingou/ns/test/issue/8" '
  3723. 'title="Private issue">pingou/ns/test#8</a> is private</p></div>',
  3724. # 'implicit link to #1',
  3725. '<div class="markdown"><p>implicit link to <a href="http://localhost.localdomain/test/pull-request/1" title="[Open] test pull-request">#1</a></p></div>',
  3726. # 'implicit link .#1. with non-whitespace, non-word characters',
  3727. '<div class="markdown"><p>implicit link .<a href="http://localhost.localdomain/test/pull-request/1" title="[Open] test pull-request">#1</a>. with non-whitespace, non-word characters</p></div>',
  3728. # '#2 - implicit link at start of line',
  3729. '<div class="markdown"><p><a href="http://localhost.localdomain/test/issue/2" title="[Open] test issue">#2</a> - implicit link at start of line</p></div>',
  3730. # '#2. implicit link at start of line with no whitespace after',
  3731. '<div class="markdown"><p><a href="http://localhost.localdomain/test/issue/2" title="[Open] test issue">#2</a>. implicit link at start of line with no whitespace after</p></div>',
  3732. # '#regular header',
  3733. '<div class="markdown"><h1>regular header</h1></div>',
  3734. # '#34 looks like an implicit link, but no issue 34',
  3735. '<div class="markdown"><h1>34 looks like an implicit link, but no issue 34</h1></div>',
  3736. # 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  3737. '<div class="markdown"><p>pingou committed on <a href="http://localhost.localdomain/'
  3738. 'test/c/9364354a4555ba17aa60f0dc844d70b74eb1aecd" '
  3739. 'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
  3740. ">test#9364354a4555ba17aa60f0dc844d70b74eb1aecd</a></p></div>",
  3741. # 'irc://pagure.io'
  3742. '<div class="markdown"><p><a href="irc://pagure.io">irc://pagure.io</a></p></div>',
  3743. # 'ircs://pagure.io' - This is getting cleaned by python-bleach
  3744. # and the version 1.4.3 that we have won't let us adjust the
  3745. # list of supported protocols
  3746. # '<p><a href="ircs://pagure.io">ircs://pagure.io</a></p>',
  3747. '<div class="markdown"><p><a href="ircs://pagure.io">ircs://pagure.io</a></p></div>'
  3748. if tuple(bleach_v) >= (1, 5, 0)
  3749. else '<div class="markdown"><p><a>ircs://pagure.io</a></p></div>',
  3750. # 'http://pagure.io'
  3751. '<div class="markdown"><p><a href="http://pagure.io">http://pagure.io</a></p></div>',
  3752. # 'https://pagure.io'
  3753. '<div class="markdown"><p><a href="https://pagure.io">https://pagure.io</a></p></div>',
  3754. # '<https://pagure.io/pagure>'
  3755. '<div class="markdown"><p><a href="https://pagure.io/pagure">https://pagure.io/pagure'
  3756. "</a></p></div>",
  3757. # '~~foo~~'
  3758. '<div class="markdown"><p><del>foo</del></p></div>',
  3759. # '~~foo bar~~'
  3760. '<div class="markdown"><p><del>foo bar</del></p></div>',
  3761. # '~~[BZ#1435310](https://bugzilla.redhat.com/1435310)~~'
  3762. '<div class="markdown"><p><del><a href="https://bugzilla.redhat.com/1435310">'
  3763. "BZ#1435310</a></del></p></div>",
  3764. # '~~[BZ#1435310](https://bugzilla.redhat.com/1435310) avc
  3765. # denial during F26AH boot 'error_name=org.freedesktop.systemd1
  3766. # .NoSuchDynamicUser~~'
  3767. '<div class="markdown"><p><del><a href="https://bugzilla.redhat.com/1435310">'
  3768. "BZ#1435310</a> avc denial during F26AH boot 'error_name="
  3769. "org.freedesktop.systemd1.NoSuchDynamicUser'</del></p></div>",
  3770. # '``~~foo bar~~``'
  3771. '<div class="markdown"><p><code>~~foo bar~~</code></p></div>',
  3772. # '~~foo bar~~ and ~~another ~~',
  3773. '<div class="markdown"><p><del>foo bar</del> and <del>another </del></p></div>',
  3774. # 'lets mention @pingou',
  3775. '<div class="markdown"><p>lets mention <a href="http://localhost.localdomain/user/pingou">@pingou</a></p></div>',
  3776. # '@pingou at start of line',
  3777. '<div class="markdown"><p><a href="http://localhost.localdomain/user/pingou">@pingou</a> at start of line</p></div>',
  3778. # 'but not someone@pingou.com',
  3779. '<div class="markdown"><p>but not someone@pingou.com</p></div>',
  3780. ]
  3781. if mk_321:
  3782. print("**** Markdown 3.2.1+ behavior")
  3783. expected.append(
  3784. # '[![Fedora_infinity_small.png]'
  3785. # '(/test/issue/raw/Fedora_infinity_small.png)]'
  3786. # '(/test/issue/raw/Fedora_infinity_small.png)',
  3787. '<div class="markdown"><p><a href="/test/issue/raw/Fedora_infinity_small.png"><span>'
  3788. '<img alt="Fedora_infinity_small.png" class="lazyload" '
  3789. 'data-src="/test/issue/raw/Fedora_infinity_small.png" src="">'
  3790. "<noscript>"
  3791. '&lt;img alt="Fedora_infinity_small.png" '
  3792. 'src="/test/issue/raw/Fedora_infinity_small.png" /&gt;'
  3793. "</noscript></span></a></p></div>"
  3794. )
  3795. elif old_markdown:
  3796. print("**** Old markdown behavior")
  3797. expected.append(
  3798. # '[![Fedora_infinity_small.png]'
  3799. # '(/test/issue/raw/Fedora_infinity_small.png)]'
  3800. # '(/test/issue/raw/Fedora_infinity_small.png)',
  3801. '<div class="markdown"><p><a href="/test/issue/raw/Fedora_infinity_small.png"><span>'
  3802. '<img alt="Fedora_infinity_small.png" class="lazyload" '
  3803. 'data-src="/test/issue/raw/Fedora_infinity_small.png" src="">'
  3804. "<noscript>"
  3805. '<img alt="Fedora_infinity_small.png" '
  3806. 'src="/test/issue/raw/Fedora_infinity_small.png" />'
  3807. "</noscript></span></a></p></div>"
  3808. )
  3809. else:
  3810. print("**** Not old but no longer new markdown")
  3811. expected.append(
  3812. # '[![Fedora_infinity_small.png]'
  3813. # '(/test/issue/raw/Fedora_infinity_small.png)]'
  3814. # '(/test/issue/raw/Fedora_infinity_small.png)',
  3815. '<div class="markdown"><p><a href="/test/issue/raw/Fedora_infinity_small.png"><span>'
  3816. '<img alt="Fedora_infinity_small.png" class="lazyload" '
  3817. 'data-src="/test/issue/raw/Fedora_infinity_small.png" src="">'
  3818. "<noscript>"
  3819. '<img alt="Fedora_infinity_small.png" '
  3820. 'src="/test/issue/raw/Fedora_infinity_small.png">'
  3821. "</noscript></span></a></p></div>"
  3822. )
  3823. with self.app.application.app_context():
  3824. g.session = self.session
  3825. for idx, text in enumerate(texts):
  3826. html = pagure.lib.query.text2markdown(text)
  3827. self.assertEqual(html, expected[idx])
  3828. def test_text2markdown_exception(self):
  3829. """ Test the text2markdown method in pagure.lib.query. """
  3830. text = "test#1 bazinga!"
  3831. expected_html = "test#1 bazinga!"
  3832. html = pagure.lib.query.text2markdown(text)
  3833. self.assertEqual(html, expected_html)
  3834. def test_text2markdown_empty_string(self):
  3835. """ Test the text2markdown method in pagure.lib.query. """
  3836. text = ""
  3837. expected_html = ""
  3838. html = pagure.lib.query.text2markdown(text)
  3839. self.assertEqual(html, expected_html)
  3840. def test_get_access_levels(self):
  3841. """ Test the get_access_levels method in pagure.lib """
  3842. acls = pagure.lib.query.get_access_levels(self.session)
  3843. self.assertEqual(sorted(["admin", "commit", "ticket"]), sorted(acls))
  3844. def test_get_project_users(self):
  3845. """ Test the get_project_users method when combine is True
  3846. """
  3847. tests.create_projects(self.session)
  3848. project = pagure.lib.query.get_authorized_project(
  3849. self.session, project_name="test"
  3850. )
  3851. # Default value of combine is True
  3852. # which means the an admin is a user, committer as well
  3853. # and a committer is also a user
  3854. # and a user is just a user
  3855. users = project.get_project_users(access="admin")
  3856. # Only pingou is the admin as of now
  3857. # But, he is the creator and
  3858. # the creator of the project is not listed in user_projects
  3859. # table. Thus, get_projec_users won't return him as an admin
  3860. # He has all the access of an admin though
  3861. self.assertEqual(len(users), 0)
  3862. self.assertEqual(project.user.username, "pingou")
  3863. # Wrong access level, should raise Accesslevelnotfound exception
  3864. self.assertRaises(
  3865. pagure.exceptions.AccessLevelNotFound,
  3866. project.get_project_users,
  3867. access="owner",
  3868. )
  3869. # Let's add a new user to the project, 'foo'
  3870. # By default, if no access is specified, he becomes an admin
  3871. msg = pagure.lib.query.add_user_to_project(
  3872. self.session, project=project, new_user="foo", user="pingou"
  3873. )
  3874. self.session.commit()
  3875. # since, he is an admin, the msg should be 'User added'
  3876. self.assertEqual(msg, "User added")
  3877. project = pagure.lib.query.get_authorized_project(
  3878. self.session, project_name="test"
  3879. )
  3880. users = project.get_project_users(access="admin")
  3881. self.assertEqual(len(users), 1)
  3882. self.assertEqual(users[0].username, "foo")
  3883. # foo should be a committer as well, since he is an admin
  3884. users = project.get_project_users(access="commit")
  3885. self.assertEqual(len(users), 1)
  3886. self.assertEqual(users[0].username, "foo")
  3887. # the admin also has ticket access
  3888. users = project.get_project_users(access="ticket")
  3889. self.assertEqual(len(users), 1)
  3890. self.assertEqual(users[0].username, "foo")
  3891. # let's update the access of foo to 'committer'
  3892. msg = pagure.lib.query.add_user_to_project(
  3893. self.session,
  3894. project=project,
  3895. new_user="foo",
  3896. user="pingou",
  3897. access="commit",
  3898. )
  3899. self.session.commit()
  3900. self.assertEqual(msg, "User access updated")
  3901. project = pagure.lib.query.get_authorized_project(
  3902. self.session, project_name="test"
  3903. )
  3904. # No admin now, even though pingou the creator is there
  3905. users = project.get_project_users(access="admin")
  3906. self.assertEqual(len(users), 0)
  3907. users = project.get_project_users(access="commit")
  3908. # foo is the committer currently
  3909. self.assertEqual(len(users), 1)
  3910. self.assertEqual(users[0].username, "foo")
  3911. users = project.get_project_users(access="ticket")
  3912. # foo also has ticket rights
  3913. self.assertEqual(len(users), 1)
  3914. self.assertEqual(users[0].username, "foo")
  3915. # let's update the access of foo to 'ticket'
  3916. msg = pagure.lib.query.add_user_to_project(
  3917. self.session,
  3918. project=project,
  3919. new_user="foo",
  3920. user="pingou",
  3921. access="ticket",
  3922. )
  3923. self.session.commit()
  3924. self.assertEqual(msg, "User access updated")
  3925. project = pagure.lib.query.get_authorized_project(
  3926. self.session, project_name="test"
  3927. )
  3928. # No admin now, even though pingou the creator is there
  3929. users = project.get_project_users(access="admin")
  3930. self.assertEqual(len(users), 0)
  3931. users = project.get_project_users(access="commit")
  3932. # foo deosn't have commit rights now
  3933. self.assertEqual(len(users), 0)
  3934. users = project.get_project_users(access="ticket")
  3935. # foo does have tickets right though
  3936. self.assertEqual(len(users), 1)
  3937. self.assertEqual(users[0].username, "foo")
  3938. def test_get_project_users_combine_false(self):
  3939. """ Test the get_project_users method when combine is False
  3940. """
  3941. tests.create_projects(self.session)
  3942. project = pagure.lib.query.get_authorized_project(
  3943. self.session, project_name="test"
  3944. )
  3945. # Let's add a new user to the project, 'foo'
  3946. # By default, if no access is specified, he becomes an admin
  3947. msg = pagure.lib.query.add_user_to_project(
  3948. self.session, project=project, new_user="foo", user="pingou"
  3949. )
  3950. self.session.commit()
  3951. # since, he is an admin, the msg should be 'User added'
  3952. self.assertEqual(msg, "User added")
  3953. # only one admin
  3954. users = project.get_project_users(access="admin", combine=False)
  3955. self.assertEqual(len(users), 1)
  3956. self.assertEqual(users[0].username, "foo")
  3957. # No user with only commit access
  3958. users = project.get_project_users(access="commit", combine=False)
  3959. self.assertEqual(len(users), 0)
  3960. # No user with only ticket access
  3961. users = project.get_project_users(access="ticket", combine=False)
  3962. self.assertEqual(len(users), 0)
  3963. # Update the access level of foo user to commit
  3964. msg = pagure.lib.query.add_user_to_project(
  3965. self.session,
  3966. project=project,
  3967. new_user="foo",
  3968. user="pingou",
  3969. access="commit",
  3970. )
  3971. self.session.commit()
  3972. self.assertEqual(msg, "User access updated")
  3973. # He is just a committer
  3974. project = pagure.lib.query.get_authorized_project(
  3975. self.session, project_name="test"
  3976. )
  3977. users = project.get_project_users(access="admin", combine=False)
  3978. self.assertEqual(len(users), 0)
  3979. # He is just a committer
  3980. users = project.get_project_users(access="commit", combine=False)
  3981. self.assertEqual(len(users), 1)
  3982. self.assertEqual(users[0].username, "foo")
  3983. # He is just a committer
  3984. users = project.get_project_users(access="ticket", combine=False)
  3985. self.assertEqual(len(users), 0)
  3986. # Update the access level of foo user to ticket
  3987. msg = pagure.lib.query.add_user_to_project(
  3988. self.session,
  3989. project=project,
  3990. new_user="foo",
  3991. user="pingou",
  3992. access="ticket",
  3993. )
  3994. self.session.commit()
  3995. self.assertEqual(msg, "User access updated")
  3996. # He is just a ticketer
  3997. project = pagure.lib.query.get_authorized_project(
  3998. self.session, project_name="test"
  3999. )
  4000. users = project.get_project_users(access="admin", combine=False)
  4001. self.assertEqual(len(users), 0)
  4002. # He is just a ticketer
  4003. users = project.get_project_users(access="commit", combine=False)
  4004. self.assertEqual(len(users), 0)
  4005. # He is just a ticketer
  4006. users = project.get_project_users(access="ticket", combine=False)
  4007. self.assertEqual(len(users), 1)
  4008. self.assertEqual(users[0].username, "foo")
  4009. def test_get_project_groups(self):
  4010. """ Test the get_project_groups method when combine is True
  4011. """
  4012. # Create some projects
  4013. tests.create_projects(self.session)
  4014. # Create a group in database
  4015. msg = pagure.lib.query.add_group(
  4016. self.session,
  4017. group_name="JL",
  4018. display_name="Justice League",
  4019. description="Nope, it's not JLA anymore",
  4020. group_type="user",
  4021. user="foo",
  4022. is_admin=False,
  4023. blacklist=pagure.config.config.get("BLACKLISTED_PROJECTS"),
  4024. )
  4025. self.assertEqual(msg, "User `foo` added to the group `JL`.")
  4026. # Add the group to project we just created, test
  4027. # First add it as an admin
  4028. project = pagure.lib.query.get_authorized_project(
  4029. self.session, project_name="test"
  4030. )
  4031. msg = pagure.lib.query.add_group_to_project(
  4032. self.session, project=project, new_group="JL", user="pingou"
  4033. )
  4034. self.session.commit()
  4035. self.assertEqual(msg, "Group added")
  4036. # Now, the group is an admin in the project
  4037. # so, it must have access to everything
  4038. project = pagure.lib.query.get_authorized_project(
  4039. self.session, project_name="test"
  4040. )
  4041. groups = project.get_project_groups(access="admin")
  4042. self.assertEqual(len(groups), 1)
  4043. self.assertEqual(groups[0].display_name, "Justice League")
  4044. self.assertEqual(len(project.admin_groups), 1)
  4045. self.assertEqual(
  4046. project.admin_groups[0].display_name, "Justice League"
  4047. )
  4048. # The group should be committer as well
  4049. groups = project.get_project_groups(access="commit")
  4050. self.assertEqual(len(groups), 1)
  4051. self.assertEqual(groups[0].display_name, "Justice League")
  4052. self.assertEqual(len(project.committer_groups), 1)
  4053. self.assertEqual(
  4054. project.committer_groups[0].display_name, "Justice League"
  4055. )
  4056. # The group should be ticketer as well
  4057. groups = project.get_project_groups(access="ticket")
  4058. self.assertEqual(len(groups), 1)
  4059. self.assertEqual(groups[0].display_name, "Justice League")
  4060. self.assertEqual(len(project.groups), 1)
  4061. self.assertEqual(project.groups[0].display_name, "Justice League")
  4062. # Update the access level of the group, JL to commit
  4063. project = pagure.lib.query.get_authorized_project(
  4064. self.session, project_name="test"
  4065. )
  4066. msg = pagure.lib.query.add_group_to_project(
  4067. self.session,
  4068. project=project,
  4069. new_group="JL",
  4070. user="pingou",
  4071. access="commit",
  4072. )
  4073. self.session.commit()
  4074. self.assertEqual(msg, "Group access updated")
  4075. # It shouldn't be an admin
  4076. project = pagure.lib.query.get_authorized_project(
  4077. self.session, project_name="test"
  4078. )
  4079. groups = project.get_project_groups(access="admin")
  4080. self.assertEqual(len(groups), 0)
  4081. self.assertEqual(len(project.admin_groups), 0)
  4082. # It is a committer
  4083. groups = project.get_project_groups(access="commit")
  4084. self.assertEqual(len(groups), 1)
  4085. self.assertEqual(groups[0].display_name, "Justice League")
  4086. self.assertEqual(len(project.committer_groups), 1)
  4087. self.assertEqual(
  4088. project.committer_groups[0].display_name, "Justice League"
  4089. )
  4090. # The group should be ticketer as well
  4091. groups = project.get_project_groups(access="ticket")
  4092. self.assertEqual(len(groups), 1)
  4093. self.assertEqual(groups[0].display_name, "Justice League")
  4094. self.assertEqual(len(project.groups), 1)
  4095. self.assertEqual(project.groups[0].display_name, "Justice League")
  4096. # Update the access of group JL to ticket
  4097. msg = pagure.lib.query.add_group_to_project(
  4098. self.session,
  4099. project=project,
  4100. new_group="JL",
  4101. user="pingou",
  4102. access="ticket",
  4103. )
  4104. self.session.commit()
  4105. self.assertEqual(msg, "Group access updated")
  4106. # It is not an admin
  4107. project = pagure.lib.query.get_authorized_project(
  4108. self.session, project_name="test"
  4109. )
  4110. groups = project.get_project_groups(access="admin")
  4111. self.assertEqual(len(groups), 0)
  4112. self.assertEqual(len(project.admin_groups), 0)
  4113. # The group shouldn't be a committer
  4114. groups = project.get_project_groups(access="commit")
  4115. self.assertEqual(len(groups), 0)
  4116. self.assertEqual(len(project.committer_groups), 0)
  4117. # The group should be ticketer
  4118. groups = project.get_project_groups(access="ticket")
  4119. self.assertEqual(len(groups), 1)
  4120. self.assertEqual(groups[0].display_name, "Justice League")
  4121. self.assertEqual(len(project.groups), 1)
  4122. self.assertEqual(project.groups[0].display_name, "Justice League")
  4123. def test_get_project_groups_combine_false(self):
  4124. """ Test the get_project_groups method when combine is False
  4125. """
  4126. # Create some projects
  4127. tests.create_projects(self.session)
  4128. # Create a group in database
  4129. msg = pagure.lib.query.add_group(
  4130. self.session,
  4131. group_name="JL",
  4132. display_name="Justice League",
  4133. description="Nope, it's not JLA anymore",
  4134. group_type="user",
  4135. user="foo",
  4136. is_admin=False,
  4137. blacklist=pagure.config.config.get("BLACKLISTED_PROJECTS"),
  4138. )
  4139. self.assertEqual(msg, "User `foo` added to the group `JL`.")
  4140. # Add the group to project we just created, test
  4141. # First add it as an admin
  4142. project = pagure.lib.query.get_authorized_project(
  4143. self.session, project_name="test"
  4144. )
  4145. msg = pagure.lib.query.add_group_to_project(
  4146. self.session, project=project, new_group="JL", user="pingou"
  4147. )
  4148. self.session.commit()
  4149. self.assertEqual(msg, "Group added")
  4150. # Now, the group is an admin in the project
  4151. # so, it must have access to everything
  4152. project = pagure.lib.query.get_authorized_project(
  4153. self.session, project_name="test"
  4154. )
  4155. groups = project.get_project_groups(access="admin", combine=False)
  4156. self.assertEqual(len(groups), 1)
  4157. self.assertEqual(groups[0].display_name, "Justice League")
  4158. self.assertEqual(len(project.admin_groups), 1)
  4159. self.assertEqual(
  4160. project.admin_groups[0].display_name, "Justice League"
  4161. )
  4162. # The group shoudn't be a committer
  4163. groups = project.get_project_groups(access="commit", combine=False)
  4164. self.assertEqual(len(groups), 0)
  4165. # The group shoudn't be a ticketer
  4166. groups = project.get_project_groups(access="ticket", combine=False)
  4167. self.assertEqual(len(groups), 0)
  4168. # Update the access level of the group, JL to commit
  4169. project = pagure.lib.query.get_authorized_project(
  4170. self.session, project_name="test"
  4171. )
  4172. msg = pagure.lib.query.add_group_to_project(
  4173. self.session,
  4174. project=project,
  4175. new_group="JL",
  4176. user="pingou",
  4177. access="commit",
  4178. )
  4179. self.session.commit()
  4180. self.assertEqual(msg, "Group access updated")
  4181. # It shouldn't be an admin
  4182. project = pagure.lib.query.get_authorized_project(
  4183. self.session, project_name="test"
  4184. )
  4185. groups = project.get_project_groups(access="admin", combine=False)
  4186. self.assertEqual(len(groups), 0)
  4187. # It is a committer
  4188. groups = project.get_project_groups(access="commit", combine=False)
  4189. self.assertEqual(len(groups), 1)
  4190. self.assertEqual(groups[0].display_name, "Justice League")
  4191. self.assertEqual(len(project.committer_groups), 1)
  4192. self.assertEqual(
  4193. project.committer_groups[0].display_name, "Justice League"
  4194. )
  4195. # The group shouldn't be ticketer
  4196. groups = project.get_project_groups(access="ticket", combine=False)
  4197. self.assertEqual(len(groups), 0)
  4198. # Update the access of group JL to ticket
  4199. msg = pagure.lib.query.add_group_to_project(
  4200. self.session,
  4201. project=project,
  4202. new_group="JL",
  4203. user="pingou",
  4204. access="ticket",
  4205. )
  4206. self.session.commit()
  4207. self.assertEqual(msg, "Group access updated")
  4208. # It is not an admin
  4209. project = pagure.lib.query.get_authorized_project(
  4210. self.session, project_name="test"
  4211. )
  4212. groups = project.get_project_groups(access="admin", combine=False)
  4213. self.assertEqual(len(groups), 0)
  4214. # The group shouldn't be a committer
  4215. groups = project.get_project_groups(access="commit", combine=False)
  4216. self.assertEqual(len(groups), 0)
  4217. # The group should be ticketer
  4218. groups = project.get_project_groups(access="ticket", combine=False)
  4219. self.assertEqual(len(groups), 1)
  4220. self.assertEqual(groups[0].display_name, "Justice League")
  4221. self.assertEqual(len(project.groups), 1)
  4222. self.assertEqual(project.groups[0].display_name, "Justice League")
  4223. def test_get_obj_access_user(self):
  4224. """ Test the get_obj_access method of pagure.lib
  4225. for model.User object """
  4226. # Create the projects
  4227. tests.create_projects(self.session)
  4228. # Add a user object - make him an admin first
  4229. project = pagure.lib.query.get_authorized_project(
  4230. self.session, project_name="test"
  4231. )
  4232. msg = pagure.lib.query.add_user_to_project(
  4233. self.session, project=project, new_user="foo", user="pingou"
  4234. )
  4235. self.session.commit()
  4236. self.assertEqual(msg, "User added")
  4237. user = pagure.lib.query.get_user(self.session, key="foo")
  4238. # He should be an admin
  4239. access_obj = pagure.lib.query.get_obj_access(
  4240. self.session, project_obj=project, obj=user
  4241. )
  4242. self.assertEqual(access_obj.access, "admin")
  4243. # Update and check for commit access
  4244. msg = pagure.lib.query.add_user_to_project(
  4245. self.session,
  4246. project=project,
  4247. new_user="foo",
  4248. user="pingou",
  4249. access="commit",
  4250. )
  4251. self.session.commit()
  4252. self.assertEqual(msg, "User access updated")
  4253. project = pagure.lib.query.get_authorized_project(
  4254. self.session, project_name="test"
  4255. )
  4256. # He should be a committer
  4257. access_obj = pagure.lib.query.get_obj_access(
  4258. self.session, project_obj=project, obj=user
  4259. )
  4260. self.assertEqual(access_obj.access, "commit")
  4261. # Update and check for ticket access
  4262. msg = pagure.lib.query.add_user_to_project(
  4263. self.session,
  4264. project=project,
  4265. new_user="foo",
  4266. user="pingou",
  4267. access="ticket",
  4268. )
  4269. self.session.commit()
  4270. self.assertEqual(msg, "User access updated")
  4271. project = pagure.lib.query.get_authorized_project(
  4272. self.session, project_name="test"
  4273. )
  4274. # He should be a ticketer
  4275. access_obj = pagure.lib.query.get_obj_access(
  4276. self.session, project_obj=project, obj=user
  4277. )
  4278. self.assertEqual(access_obj.access, "ticket")
  4279. def test_get_obj_access_group(self):
  4280. """ Test the get_obj_access method of pagure.lib
  4281. for model.PagureGroup object """
  4282. # Create the projects
  4283. tests.create_projects(self.session)
  4284. # Create a group in database
  4285. msg = pagure.lib.query.add_group(
  4286. self.session,
  4287. group_name="JL",
  4288. display_name="Justice League",
  4289. description="Nope, it's not JLA anymore",
  4290. group_type="user",
  4291. user="foo",
  4292. is_admin=False,
  4293. blacklist=pagure.config.config.get("BLACKLISTED_PROJECTS"),
  4294. )
  4295. self.assertEqual(msg, "User `foo` added to the group `JL`.")
  4296. # Add a group object - make him an admin first
  4297. project = pagure.lib.query.get_authorized_project(
  4298. self.session, project_name="test"
  4299. )
  4300. msg = pagure.lib.query.add_group_to_project(
  4301. self.session, project=project, new_group="JL", user="pingou"
  4302. )
  4303. self.session.commit()
  4304. self.assertEqual(msg, "Group added")
  4305. group = pagure.lib.query.search_groups(self.session, group_name="JL")
  4306. # He should be an admin
  4307. access_obj = pagure.lib.query.get_obj_access(
  4308. self.session, project_obj=project, obj=group
  4309. )
  4310. self.assertEqual(access_obj.access, "admin")
  4311. # Update and check for commit access
  4312. msg = pagure.lib.query.add_group_to_project(
  4313. self.session,
  4314. project=project,
  4315. new_group="JL",
  4316. user="pingou",
  4317. access="commit",
  4318. )
  4319. self.session.commit()
  4320. self.assertEqual(msg, "Group access updated")
  4321. project = pagure.lib.query.get_authorized_project(
  4322. self.session, project_name="test"
  4323. )
  4324. # He should be a committer
  4325. access_obj = pagure.lib.query.get_obj_access(
  4326. self.session, project_obj=project, obj=group
  4327. )
  4328. self.assertEqual(access_obj.access, "commit")
  4329. # Update and check for ticket access
  4330. msg = pagure.lib.query.add_group_to_project(
  4331. self.session,
  4332. project=project,
  4333. new_group="JL",
  4334. user="pingou",
  4335. access="ticket",
  4336. )
  4337. self.session.commit()
  4338. self.assertEqual(msg, "Group access updated")
  4339. project = pagure.lib.query.get_authorized_project(
  4340. self.session, project_name="test"
  4341. )
  4342. # He should be a ticketer
  4343. access_obj = pagure.lib.query.get_obj_access(
  4344. self.session, project_obj=project, obj=group
  4345. )
  4346. self.assertEqual(access_obj.access, "ticket")
  4347. def test_set_watch_obj(self):
  4348. """ Test the set_watch_obj method in pagure.lib """
  4349. # Create the project ns/test
  4350. item = pagure.lib.model.Project(
  4351. user_id=1, # pingou
  4352. name="test3",
  4353. namespace="ns",
  4354. description="test project #1",
  4355. hook_token="aaabbbcccdd",
  4356. )
  4357. item.close_status = ["Invalid", "Insufficient data", "Fixed"]
  4358. self.session.add(item)
  4359. self.session.commit()
  4360. # Create the ticket
  4361. iss = pagure.lib.query.new_issue(
  4362. issue_id=4,
  4363. session=self.session,
  4364. repo=item,
  4365. title="test issue",
  4366. content="content test issue",
  4367. user="pingou",
  4368. )
  4369. self.session.commit()
  4370. self.assertEqual(iss.id, 4)
  4371. self.assertEqual(iss.title, "test issue")
  4372. # Unknown user
  4373. self.assertRaises(
  4374. pagure.exceptions.PagureException,
  4375. pagure.lib.query.set_watch_obj,
  4376. self.session,
  4377. "unknown",
  4378. iss,
  4379. True,
  4380. )
  4381. # Invalid object to watch - project
  4382. self.assertRaises(
  4383. pagure.exceptions.InvalidObjectException,
  4384. pagure.lib.query.set_watch_obj,
  4385. self.session,
  4386. "foo",
  4387. iss.project,
  4388. True,
  4389. )
  4390. # Invalid object to watch - string
  4391. self.assertRaises(
  4392. AttributeError,
  4393. pagure.lib.query.set_watch_obj,
  4394. self.session,
  4395. "foo",
  4396. "ticket",
  4397. True,
  4398. )
  4399. # Watch the ticket
  4400. out = pagure.lib.query.set_watch_obj(self.session, "foo", iss, True)
  4401. self.assertEqual(out, "You are now watching this issue")
  4402. # Un-watch the ticket
  4403. out = pagure.lib.query.set_watch_obj(self.session, "foo", iss, False)
  4404. self.assertEqual(out, "You are no longer watching this issue")
  4405. def test_tokenize_search_string(self):
  4406. """ Test the tokenize_search_string function. """
  4407. # These are the tests performed to make sure we tokenize correctly.
  4408. # This is in the form: input string, custom fields, remaining pattern
  4409. tests = [
  4410. ("test123", {}, "test123"),
  4411. ("test:key test123", {"test": "key"}, "test123"),
  4412. (
  4413. 'test:"key with spaces" test123',
  4414. {"test": "key with spaces"},
  4415. "test123",
  4416. ),
  4417. ("test123 test:key test456", {"test": "key"}, "test123 test456"),
  4418. (
  4419. 'test123 test:"key with spaces" key2:value12 test456',
  4420. {"test": "key with spaces", "key2": "value12"},
  4421. "test123 test456",
  4422. ),
  4423. ]
  4424. for inp, flds, rem in tests:
  4425. self.assertEqual(
  4426. pagure.lib.query.tokenize_search_string(inp), (flds, rem)
  4427. )
  4428. def test_save_report(self):
  4429. """ Test the save_report function. """
  4430. # Create the projects
  4431. tests.create_projects(self.session)
  4432. project = pagure.lib.query.get_authorized_project(
  4433. self.session, project_name="test"
  4434. )
  4435. self.assertEqual(project.reports, {})
  4436. name = "test report"
  4437. url = "?foo=bar&baz=biz"
  4438. pagure.lib.query.save_report(
  4439. self.session, repo=project, name=name, url=url, username=None
  4440. )
  4441. project = pagure.lib.query.get_authorized_project(
  4442. self.session, project_name="test"
  4443. )
  4444. self.assertEqual(
  4445. project.reports, {"test report": {"baz": "biz", "foo": "bar"}}
  4446. )
  4447. name = "test report #2"
  4448. url = "?foo=bar&foo=none&foo=baz"
  4449. pagure.lib.query.save_report(
  4450. self.session, repo=project, name=name, url=url, username=None
  4451. )
  4452. project = pagure.lib.query.get_authorized_project(
  4453. self.session, project_name="test"
  4454. )
  4455. self.assertEqual(
  4456. project.reports,
  4457. {
  4458. "test report": {"baz": "biz", "foo": "bar"},
  4459. "test report #2": {"foo": ["bar", "none", "baz"]},
  4460. },
  4461. )
  4462. def test_text2markdown_table(self):
  4463. """ Test the text2markdown function with a markdown table. """
  4464. try:
  4465. md_version = markdown.__version__.version_info
  4466. except AttributeError:
  4467. md_version = markdown.__version_info__
  4468. if md_version < (2, 6, 7):
  4469. raise unittest.case.SkipTest(
  4470. "Skipping on old markdown that do not strip the orientation row"
  4471. )
  4472. text = """
  4473. | Left-aligned | Center-aligned | Right-aligned |
  4474. | :--- | :---: | ---: |
  4475. | git status | git status | git status |
  4476. | git diff | git diff | git diff |
  4477. foo bar
  4478. """
  4479. expected = """<div class="markdown"><table>
  4480. <thead>
  4481. <tr>
  4482. <th align="left">Left-aligned</th>
  4483. <th align="center">Center-aligned</th>
  4484. <th align="right">Right-aligned</th>
  4485. </tr>
  4486. </thead>
  4487. <tbody>
  4488. <tr>
  4489. <td align="left">git status</td>
  4490. <td align="center">git status</td>
  4491. <td align="right">git status</td>
  4492. </tr>
  4493. <tr>
  4494. <td align="left">git diff</td>
  4495. <td align="center">git diff</td>
  4496. <td align="right">git diff</td>
  4497. </tr>
  4498. </tbody>
  4499. </table>
  4500. <p>foo bar</p></div>"""
  4501. with self.app.application.app_context():
  4502. html = pagure.lib.query.text2markdown(text)
  4503. self.assertEqual(html, expected)
  4504. def test_text2markdown_table_old_mk(self):
  4505. """ Test the text2markdown function with a markdown table using the old
  4506. format where the orientation instruction are provided next to the column
  4507. delimiter unlike what can be done with more recent version of markdown.
  4508. """
  4509. text = """
  4510. | Left-aligned | Center-aligned | Right-aligned |
  4511. |:--- |:--------------:| ---:|
  4512. | git status | git status | git status |
  4513. | git diff | git diff | git diff |
  4514. foo bar
  4515. """
  4516. expected = """<div class="markdown"><table>
  4517. <thead>
  4518. <tr>
  4519. <th align="left">Left-aligned</th>
  4520. <th align="center">Center-aligned</th>
  4521. <th align="right">Right-aligned</th>
  4522. </tr>
  4523. </thead>
  4524. <tbody>
  4525. <tr>
  4526. <td align="left">git status</td>
  4527. <td align="center">git status</td>
  4528. <td align="right">git status</td>
  4529. </tr>
  4530. <tr>
  4531. <td align="left">git diff</td>
  4532. <td align="center">git diff</td>
  4533. <td align="right">git diff</td>
  4534. </tr>
  4535. </tbody>
  4536. </table>
  4537. <p>foo bar</p></div>"""
  4538. with self.app.application.app_context():
  4539. html = pagure.lib.query.text2markdown(text)
  4540. self.assertEqual(html, expected)
  4541. def test_set_redis(self):
  4542. """ Test the set_redis function of pagure.lib.query. """
  4543. self.assertIsNone(pagure.lib.query.REDIS)
  4544. pagure.lib.query.set_redis("0.0.0.0", 6379, 0)
  4545. self.assertIsNotNone(pagure.lib.query.REDIS)
  4546. def test_set_pagure_ci(self):
  4547. """ Test the set_pagure_ci function of pagure.lib.query. """
  4548. # self.assertIn(pagure.lib.query.PAGURE_CI, [None, ['jenkins']])
  4549. pagure.lib.query.set_pagure_ci(True)
  4550. self.assertIsNotNone(pagure.lib.query.PAGURE_CI)
  4551. self.assertTrue(pagure.lib.query.PAGURE_CI)
  4552. def test_get_user_invalid_user(self):
  4553. """ Test the get_user function of pagure.lib.query. """
  4554. self.assertRaises(
  4555. pagure.exceptions.PagureException,
  4556. pagure.lib.query.get_user,
  4557. self.session,
  4558. "unknown",
  4559. )
  4560. def test_get_user_username(self):
  4561. """ Test the get_user function of pagure.lib.query. """
  4562. user = pagure.lib.query.get_user(self.session, "foo")
  4563. self.assertEqual(user.username, "foo")
  4564. def test_get_user_email(self):
  4565. """ Test the get_user function of pagure.lib.query. """
  4566. user = pagure.lib.query.get_user(self.session, "bar@pingou.com")
  4567. self.assertEqual(user.username, "pingou")
  4568. def test_is_valid_ssh_key_empty(self):
  4569. """ Test the is_valid_ssh_key function of pagure.lib.query. """
  4570. self.assertIsNone(pagure.lib.query.is_valid_ssh_key(""))
  4571. def test_is_valid_ssh_key_invalid(self):
  4572. """ Test the is_valid_ssh_key function of pagure.lib.query. """
  4573. self.assertEqual(pagure.lib.query.is_valid_ssh_key("nonsense"), False)
  4574. def test_is_valid_ssh_key(self):
  4575. """ Test the is_valid_ssh_key function of pagure.lib.query. """
  4576. keyinfo = pagure.lib.query.is_valid_ssh_key(
  4577. """ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDtgzSO9d1IrKdmyBFUvtAJPLgGOhp0lSySkWRSe+/+3KXYjSnsLnCJQlO5M7JfaXhtTHEow86rh4W9+FoJdzo5iocAwH5xPZ5ttHLy7VHgTzNMUeMgKpjy6bBOdPoGPPG4mo7QCMCRJdWBRDv4OSEMLU5jQAvC272YK2V8L918VQ== root@test"""
  4578. )
  4579. keys = keyinfo.split("\n")
  4580. self.assertEqual(len(keys), 2)
  4581. self.assertEqual(keys[1], "")
  4582. key = keys[0].split(" ")
  4583. self.assertEqual(key[0], "1024")
  4584. # We should always get the SHA256 version
  4585. self.assertEqual(
  4586. key[1], "SHA256:ztcRXIq7y/HNfwwscrexCyDF46/Pn/oHTkBZx87GwI8"
  4587. )
  4588. self.assertEqual(key[3], "(RSA)")
  4589. def test_create_deploykeys_ssh_keys_on_disk_empty(self):
  4590. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4591. pagure.lib.query. """
  4592. self.assertIsNone(
  4593. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(None, None)
  4594. )
  4595. self.assertFalse(
  4596. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4597. )
  4598. def test_create_deploykeys_ssh_keys_on_disk_nokey(self):
  4599. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4600. pagure.lib.query. """
  4601. tests.create_projects(self.session)
  4602. project = pagure.lib.query._get_project(self.session, "test")
  4603. self.assertIsNone(
  4604. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4605. project, self.path
  4606. )
  4607. )
  4608. self.assertTrue(
  4609. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4610. )
  4611. self.assertEqual(
  4612. os.listdir(os.path.join(self.path, "deploykeys", "test")), []
  4613. )
  4614. @patch(
  4615. "pagure.lib.query.is_valid_ssh_key", MagicMock(return_value="foo bar")
  4616. )
  4617. def test_create_deploykeys_ssh_keys_on_disk(self):
  4618. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4619. pagure.lib.query. """
  4620. tests.create_projects(self.session)
  4621. project = pagure.lib.query._get_project(self.session, "test")
  4622. # Add a deploy key to the project
  4623. pingou = pagure.lib.query.get_user(self.session, "pingou")
  4624. msg = pagure.lib.query.add_sshkey_to_project_or_user(
  4625. self.session,
  4626. project=project,
  4627. ssh_key="foo bar",
  4628. pushaccess=False,
  4629. creator=pingou,
  4630. )
  4631. self.assertEqual(msg, "SSH key added")
  4632. self.assertIsNone(
  4633. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4634. project, self.path
  4635. )
  4636. )
  4637. self.assertTrue(
  4638. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4639. )
  4640. self.assertEqual(
  4641. os.listdir(os.path.join(self.path, "deploykeys", "test")),
  4642. ["deploykey_test_1.pub"],
  4643. )
  4644. # Remove the deploykey
  4645. project = pagure.lib.query._get_project(self.session, "test")
  4646. self.session.delete(project.deploykeys[0])
  4647. self.session.commit()
  4648. # Remove the file on disk
  4649. self.assertIsNone(
  4650. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4651. project, self.path
  4652. )
  4653. )
  4654. self.assertTrue(
  4655. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4656. )
  4657. self.assertEqual(
  4658. os.listdir(os.path.join(self.path, "deploykeys", "test")), []
  4659. )
  4660. @patch(
  4661. "pagure.lib.query.is_valid_ssh_key",
  4662. MagicMock(return_value="\nfoo bar"),
  4663. )
  4664. def test_create_deploykeys_ssh_keys_on_disk_empty_first_key(self):
  4665. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4666. pagure.lib.query. """
  4667. tests.create_projects(self.session)
  4668. project = pagure.lib.query._get_project(self.session, "test")
  4669. # Add a deploy key to the project
  4670. new_key_obj = pagure.lib.model.SSHKey(
  4671. project_id=project.id,
  4672. pushaccess=False,
  4673. public_ssh_key="\n foo bar",
  4674. ssh_short_key="\n foo bar",
  4675. ssh_search_key="\n foo bar",
  4676. creator_user_id=1, # pingou
  4677. )
  4678. self.session.add(new_key_obj)
  4679. self.session.commit()
  4680. self.assertIsNone(
  4681. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4682. project, self.path
  4683. )
  4684. )
  4685. self.assertTrue(
  4686. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4687. )
  4688. self.assertEqual(
  4689. os.listdir(os.path.join(self.path, "deploykeys", "test")), []
  4690. )
  4691. def test_create_deploykeys_ssh_keys_on_disk_invalid(self):
  4692. """ Test the create_deploykeys_ssh_keys_on_disk function of
  4693. pagure.lib.query. """
  4694. tests.create_projects(self.session)
  4695. project = pagure.lib.query._get_project(self.session, "test")
  4696. # Add a deploy key to the project
  4697. new_key_obj = pagure.lib.model.SSHKey(
  4698. project_id=project.id,
  4699. pushaccess=False,
  4700. public_ssh_key="foo bar",
  4701. ssh_short_key="foo bar",
  4702. ssh_search_key="foo bar",
  4703. creator_user_id=1, # pingou
  4704. )
  4705. self.session.add(new_key_obj)
  4706. self.session.commit()
  4707. self.assertIsNone(
  4708. pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
  4709. project, self.path
  4710. )
  4711. )
  4712. self.assertTrue(
  4713. os.path.exists(os.path.join(self.path, "deploykeys", "test"))
  4714. )
  4715. self.assertEqual(
  4716. os.listdir(os.path.join(self.path, "deploykeys", "test")), []
  4717. )
  4718. def test_create_user_ssh_keys_on_disk_none(self):
  4719. """ Test the create_user_ssh_keys_on_disk function of pagure.lib.query. """
  4720. self.assertIsNone(
  4721. pagure.lib.query.create_user_ssh_keys_on_disk(None, None)
  4722. )
  4723. def test_update_user_settings_invalid_user(self):
  4724. """ Test the update_user_settings function of pagure.lib.query. """
  4725. self.assertRaises(
  4726. pagure.exceptions.PagureException,
  4727. pagure.lib.query.update_user_settings,
  4728. session=self.session,
  4729. settings={},
  4730. user="invalid",
  4731. )
  4732. def test_update_user_settings_no_change(self):
  4733. """ Test the update_user_settings function of pagure.lib.query. """
  4734. # First update the setting
  4735. msg = pagure.lib.query.update_user_settings(
  4736. session=self.session,
  4737. settings={"cc_me_to_my_actions": True},
  4738. user="pingou",
  4739. )
  4740. self.assertEqual(msg, "Successfully edited your settings")
  4741. # Then change it back to its default
  4742. msg = pagure.lib.query.update_user_settings(
  4743. session=self.session, settings={}, user="pingou"
  4744. )
  4745. self.assertEqual(msg, "Successfully edited your settings")
  4746. def test_update_user_settings_no_data(self):
  4747. """ Test the update_user_settings function of pagure.lib.query. """
  4748. msg = pagure.lib.query.update_user_settings(
  4749. session=self.session,
  4750. settings={"cc_me_to_my_actions": False},
  4751. user="pingou",
  4752. )
  4753. self.assertEqual(msg, "No settings to change")
  4754. def test_update_user_settings(self):
  4755. """ Test the update_user_settings function of pagure.lib.query. """
  4756. msg = pagure.lib.query.update_user_settings(
  4757. session=self.session,
  4758. settings={"cc_me_to_my_actions": True},
  4759. user="pingou",
  4760. )
  4761. self.assertEqual(msg, "Successfully edited your settings")
  4762. @patch.dict(
  4763. pagure.config.config,
  4764. {"ALLOWED_EMAIL_DOMAINS": ["pingoured.fr"]},
  4765. clear=True,
  4766. )
  4767. def test_add_email_to_user_with_logs(self):
  4768. """ Test the add_email_to_user function of pagure.lib when there
  4769. are log entries associated to the email added.
  4770. """
  4771. user = pagure.lib.query.search_user(self.session, username="pingou")
  4772. # Add a couple of log entries associated with the new email
  4773. for i in range(3):
  4774. log = pagure.lib.model.PagureLog(
  4775. user_email="new_email@pingoured.fr",
  4776. log_type="commit",
  4777. ref_id=i,
  4778. )
  4779. self.session.add(log)
  4780. self.session.commit()
  4781. # Check emails before
  4782. self.assertEqual(len(user.emails), 2)
  4783. # Add the new_email to the user
  4784. pagure.lib.query.add_email_to_user(
  4785. self.session, user, "new_email@pingoured.fr"
  4786. )
  4787. self.session.commit()
  4788. # Check emails after
  4789. self.assertEqual(len(user.emails), 3)
  4790. # add another email address that is not in an allowed domain
  4791. self.assertRaises(
  4792. pagure.exceptions.PagureException,
  4793. pagure.lib.query.add_email_to_user,
  4794. session=self.session,
  4795. user=user,
  4796. user_email="foo@bar.com",
  4797. )
  4798. self.session.commit()
  4799. # Check emails after
  4800. self.assertEqual(len(user.emails), 3)
  4801. # redo tests with 2 allowed domains
  4802. pagure.config.config["ALLOWED_EMAIL_DOMAINS"] = [
  4803. "pingoured.fr",
  4804. "example.com",
  4805. ]
  4806. # Add the new_email to the user
  4807. pagure.lib.query.add_email_to_user(
  4808. self.session, user, "another_email@example.com"
  4809. )
  4810. self.session.commit()
  4811. # Check emails after
  4812. self.assertEqual(len(user.emails), 4)
  4813. # add another email address that is not in an allowed domain
  4814. self.assertRaises(
  4815. pagure.exceptions.PagureException,
  4816. pagure.lib.query.add_email_to_user,
  4817. session=self.session,
  4818. user=user,
  4819. user_email="another_foo@bar.com",
  4820. )
  4821. self.session.commit()
  4822. # Check emails after
  4823. self.assertEqual(len(user.emails), 4)
  4824. @patch(
  4825. "pagure.lib.query.is_valid_ssh_key", MagicMock(return_value="foo bar")
  4826. )
  4827. def test_update_user_ssh_valid_key(self):
  4828. """ Test the update_user_ssh function of pagure.lib.query. """
  4829. pagure.SESSION = self.session
  4830. pagure.lib.query.update_user_ssh(
  4831. self.session, user="pingou", ssh_key="foo key", keydir=self.path
  4832. )
  4833. self.session.commit()
  4834. self.assertTrue(os.path.exists(os.path.join(self.path, "keys_0")))
  4835. self.assertEqual(
  4836. os.listdir(os.path.join(self.path, "keys_0")), ["pingou.pub"]
  4837. )
  4838. def test_add_user_pending_email_existing_email(self):
  4839. """ Test the add_user_pending_email function of pagure.lib.query. """
  4840. user = pagure.lib.query.search_user(self.session, username="pingou")
  4841. self.assertRaises(
  4842. pagure.exceptions.PagureException,
  4843. pagure.lib.query.add_user_pending_email,
  4844. session=self.session,
  4845. userobj=user,
  4846. email="foo@bar.com",
  4847. )
  4848. @patch("pagure.lib.notify.notify_new_email", MagicMock(return_value=True))
  4849. @patch.dict(
  4850. pagure.config.config,
  4851. {"ALLOWED_EMAIL_DOMAINS": ["pingoured.fr"]},
  4852. clear=True,
  4853. )
  4854. def test_add_user_pending_email(self):
  4855. """ Test the add_user_pending_email function of pagure.lib.query. """
  4856. user = pagure.lib.query.search_user(self.session, username="pingou")
  4857. self.assertEqual(len(user.emails), 2)
  4858. self.assertEqual(len(user.emails_pending), 0)
  4859. pagure.lib.query.add_user_pending_email(
  4860. session=self.session, userobj=user, email="new_mail@pingoured.fr"
  4861. )
  4862. self.session.commit()
  4863. self.assertEqual(len(user.emails), 2)
  4864. self.assertEqual(len(user.emails_pending), 1)
  4865. # add another email address that is not allowed
  4866. self.assertRaises(
  4867. pagure.exceptions.PagureException,
  4868. pagure.lib.query.add_user_pending_email,
  4869. session=self.session,
  4870. userobj=user,
  4871. email="foo@bar.com",
  4872. )
  4873. self.session.commit()
  4874. self.assertEqual(len(user.emails), 2)
  4875. self.assertEqual(len(user.emails_pending), 1)
  4876. def test_resend_pending_email_someone_else_email(self):
  4877. """ Test the resend_pending_email function of pagure.lib.query. """
  4878. user = pagure.lib.query.search_user(self.session, username="pingou")
  4879. self.assertRaises(
  4880. pagure.exceptions.PagureException,
  4881. pagure.lib.query.resend_pending_email,
  4882. session=self.session,
  4883. userobj=user,
  4884. email="foo@bar.com",
  4885. )
  4886. def test_resend_pending_email_email_validated(self):
  4887. """ Test the resend_pending_email function of pagure.lib.query. """
  4888. user = pagure.lib.query.search_user(self.session, username="pingou")
  4889. self.assertRaises(
  4890. pagure.exceptions.PagureException,
  4891. pagure.lib.query.resend_pending_email,
  4892. session=self.session,
  4893. userobj=user,
  4894. email="foo@pingou.com",
  4895. )
  4896. def test_get_acls(self):
  4897. """ Test the get_acls function of pagure.lib.query. """
  4898. acls = pagure.lib.query.get_acls(self.session)
  4899. self.assertEqual(
  4900. sorted([a.name for a in acls]),
  4901. [
  4902. "commit",
  4903. "commit_flag",
  4904. "create_branch",
  4905. "create_project",
  4906. "fork_project",
  4907. "generate_acls_project",
  4908. "internal_access",
  4909. "issue_assign",
  4910. "issue_change_status",
  4911. "issue_comment",
  4912. "issue_create",
  4913. "issue_subscribe",
  4914. "issue_update",
  4915. "issue_update_custom_fields",
  4916. "issue_update_milestone",
  4917. "modify_project",
  4918. "pull_request_assign",
  4919. "pull_request_close",
  4920. "pull_request_comment",
  4921. "pull_request_create",
  4922. "pull_request_flag",
  4923. "pull_request_merge",
  4924. "pull_request_rebase",
  4925. "pull_request_subscribe",
  4926. "pull_request_update",
  4927. "tag_project",
  4928. "update_watch_status",
  4929. ],
  4930. )
  4931. def test_get_acls_restrict_one(self):
  4932. """ Test the get_acls function of pagure.lib.query. """
  4933. acls = pagure.lib.query.get_acls(
  4934. self.session, restrict="create_project"
  4935. )
  4936. self.assertEqual([a.name for a in acls], ["create_project"])
  4937. def test_get_acls_restrict_two(self):
  4938. """ Test the get_acls function of pagure.lib.query. """
  4939. acls = pagure.lib.query.get_acls(
  4940. self.session, restrict=["create_project", "issue_create"]
  4941. )
  4942. self.assertEqual(
  4943. [a.name for a in acls], ["create_project", "issue_create"]
  4944. )
  4945. def test_filter_img_src(self):
  4946. """ Test the filter_img_src function of pagure.lib.query. """
  4947. for name in ("alt", "height", "width", "class"):
  4948. self.assertTrue(pagure.lib.query.filter_img_src(name, "caption"))
  4949. self.assertTrue(
  4950. pagure.lib.query.filter_img_src("src", "/path/to/image")
  4951. )
  4952. self.assertTrue(
  4953. pagure.lib.query.filter_img_src(
  4954. "src", "http://localhost.localdomain/path/to/image"
  4955. )
  4956. )
  4957. self.assertFalse(
  4958. pagure.lib.query.filter_img_src(
  4959. "src", "http://foo.org/path/to/image"
  4960. )
  4961. )
  4962. self.assertFalse(
  4963. pagure.lib.query.filter_img_src(
  4964. "anything", "http://foo.org/path/to/image"
  4965. )
  4966. )
  4967. def test_clean_input(self):
  4968. """ Test the clean_input function of pagure.lib.query. """
  4969. text = '<a href="/path" title="click me!">Click here</a>'
  4970. output = pagure.lib.query.clean_input(text)
  4971. self.assertEqual(output, text)
  4972. def test_could_be_text(self):
  4973. """ Test the could_be_text function of pagure.lib.query. """
  4974. self.assertTrue(pagure.lib.query.could_be_text(b"foo"))
  4975. self.assertTrue(pagure.lib.query.could_be_text("fâö".encode("utf-8")))
  4976. self.assertFalse(
  4977. pagure.lib.query.could_be_text(b"\x89PNG\r\n\x1a\n\x00")
  4978. )
  4979. def test_set_custom_key_fields_empty(self):
  4980. """ Test the set_custom_key_fields function of pagure.lib.query. """
  4981. tests.create_projects(self.session)
  4982. project = pagure.lib.query._get_project(self.session, "test")
  4983. self.assertIsNotNone(project)
  4984. msg = pagure.lib.query.set_custom_key_fields(
  4985. session=self.session,
  4986. project=project,
  4987. fields=[],
  4988. types=[],
  4989. data=[],
  4990. notify=False,
  4991. )
  4992. self.session.commit()
  4993. self.assertEqual(msg, "List of custom fields updated")
  4994. def test_set_custom_key_fields(self):
  4995. """ Test the set_custom_key_fields function of pagure.lib.query. """
  4996. tests.create_projects(self.session)
  4997. project = pagure.lib.query._get_project(self.session, "test")
  4998. self.assertIsNotNone(project)
  4999. # Set a custom key
  5000. msg = pagure.lib.query.set_custom_key_fields(
  5001. session=self.session,
  5002. project=project,
  5003. fields=["upstream"],
  5004. types=["url"],
  5005. data=[None],
  5006. notify=False,
  5007. )
  5008. self.session.commit()
  5009. self.assertEqual(msg, "List of custom fields updated")
  5010. # Set another one, with notifications on
  5011. msg = pagure.lib.query.set_custom_key_fields(
  5012. session=self.session,
  5013. project=project,
  5014. fields=["bugzilla_url"],
  5015. types=["url"],
  5016. data=[None],
  5017. notify=["on"],
  5018. )
  5019. self.session.commit()
  5020. self.assertEqual(msg, "List of custom fields updated")
  5021. # Re-set the second one but with notifications off
  5022. msg = pagure.lib.query.set_custom_key_fields(
  5023. session=self.session,
  5024. project=project,
  5025. fields=["bugzilla_url"],
  5026. types=["url"],
  5027. data=[None],
  5028. notify=["off"],
  5029. )
  5030. self.session.commit()
  5031. self.assertEqual(msg, "List of custom fields updated")
  5032. @patch("pagure.lib.query.REDIS")
  5033. def test_set_custom_key_value_boolean(self, mock_redis):
  5034. """ Test the set_custom_key_value function of pagure.lib.query. """
  5035. mock_redis.return_value = True
  5036. tests.create_projects(self.session)
  5037. project = pagure.lib.query._get_project(self.session, "test")
  5038. self.assertIsNotNone(project)
  5039. # Set a custom key
  5040. msg = pagure.lib.query.set_custom_key_fields(
  5041. session=self.session,
  5042. project=project,
  5043. fields=["tested"],
  5044. types=["boolean"],
  5045. data=[None],
  5046. notify=False,
  5047. )
  5048. self.session.commit()
  5049. self.assertEqual(msg, "List of custom fields updated")
  5050. # Create issues
  5051. msg = pagure.lib.query.new_issue(
  5052. session=self.session,
  5053. repo=project,
  5054. title="Test issue",
  5055. content="We should work on this",
  5056. user="pingou",
  5057. )
  5058. self.session.commit()
  5059. self.assertEqual(msg.title, "Test issue")
  5060. issue = pagure.lib.query.search_issues(
  5061. self.session, project, issueid=1
  5062. )
  5063. self.assertEqual(len(project.issue_keys), 1)
  5064. self.assertEqual(project.issue_keys[0].key_type, "boolean")
  5065. msg = pagure.lib.query.set_custom_key_value(
  5066. session=self.session,
  5067. issue=issue,
  5068. key=project.issue_keys[0],
  5069. value=True,
  5070. )
  5071. self.session.commit()
  5072. self.assertEqual(msg, "Custom field tested adjusted to True")
  5073. # Update it a second time to trigger edit
  5074. msg = pagure.lib.query.set_custom_key_value(
  5075. session=self.session,
  5076. issue=issue,
  5077. key=project.issue_keys[0],
  5078. value=False,
  5079. )
  5080. if str(self.session.bind.engine.url).startswith("sqlite"):
  5081. self.assertEqual(msg, "Custom field tested reset (from 1)")
  5082. else:
  5083. self.assertEqual(msg, "Custom field tested reset (from true)")
  5084. self.assertEqual(mock_redis.publish.call_count, 2)
  5085. @patch("pagure.lib.query.REDIS")
  5086. def test_set_custom_key_value_boolean_private_issue(self, mock_redis):
  5087. """ Test the set_custom_key_value function of pagure.lib.query. """
  5088. mock_redis.return_value = True
  5089. tests.create_projects(self.session)
  5090. project = pagure.lib.query._get_project(self.session, "test")
  5091. self.assertIsNotNone(project)
  5092. # Set a custom key
  5093. msg = pagure.lib.query.set_custom_key_fields(
  5094. session=self.session,
  5095. project=project,
  5096. fields=["tested"],
  5097. types=["boolean"],
  5098. data=[None],
  5099. notify=False,
  5100. )
  5101. self.session.commit()
  5102. self.assertEqual(msg, "List of custom fields updated")
  5103. # Create issues
  5104. msg = pagure.lib.query.new_issue(
  5105. session=self.session,
  5106. repo=project,
  5107. title="Test issue",
  5108. content="We should work on this",
  5109. user="pingou",
  5110. private=True,
  5111. )
  5112. self.session.commit()
  5113. self.assertEqual(msg.title, "Test issue")
  5114. issue = pagure.lib.query.search_issues(
  5115. self.session, project, issueid=1
  5116. )
  5117. self.assertEqual(len(project.issue_keys), 1)
  5118. self.assertEqual(project.issue_keys[0].key_type, "boolean")
  5119. msg = pagure.lib.query.set_custom_key_value(
  5120. session=self.session,
  5121. issue=issue,
  5122. key=project.issue_keys[0],
  5123. value=True,
  5124. )
  5125. self.session.commit()
  5126. self.assertEqual(msg, "Custom field tested adjusted to True")
  5127. # Update it a second time to trigger edit
  5128. msg = pagure.lib.query.set_custom_key_value(
  5129. session=self.session,
  5130. issue=issue,
  5131. key=project.issue_keys[0],
  5132. value=False,
  5133. )
  5134. self.session.commit()
  5135. if str(self.session.bind.engine.url).startswith("sqlite"):
  5136. self.assertEqual(msg, "Custom field tested reset (from 1)")
  5137. else:
  5138. self.assertEqual(msg, "Custom field tested reset (from true)")
  5139. self.assertEqual(mock_redis.publish.call_count, 2)
  5140. @patch("pagure.lib.query.REDIS")
  5141. def test_set_custom_key_value_text(self, mock_redis):
  5142. """ Test the set_custom_key_value function of pagure.lib.query. """
  5143. mock_redis.return_value = True
  5144. tests.create_projects(self.session)
  5145. project = pagure.lib.query._get_project(self.session, "test")
  5146. self.assertIsNotNone(project)
  5147. # Set a custom key
  5148. msg = pagure.lib.query.set_custom_key_fields(
  5149. session=self.session,
  5150. project=project,
  5151. fields=["tested"],
  5152. types=["text"],
  5153. data=[None],
  5154. notify=False,
  5155. )
  5156. self.session.commit()
  5157. self.assertEqual(msg, "List of custom fields updated")
  5158. # Create issues
  5159. msg = pagure.lib.query.new_issue(
  5160. session=self.session,
  5161. repo=project,
  5162. title="Test issue",
  5163. content="We should work on this",
  5164. user="pingou",
  5165. )
  5166. self.session.commit()
  5167. self.assertEqual(msg.title, "Test issue")
  5168. issue = pagure.lib.query.search_issues(
  5169. self.session, project, issueid=1
  5170. )
  5171. self.assertEqual(len(project.issue_keys), 1)
  5172. self.assertEqual(project.issue_keys[0].key_type, "text")
  5173. msg = pagure.lib.query.set_custom_key_value(
  5174. session=self.session,
  5175. issue=issue,
  5176. key=project.issue_keys[0],
  5177. value="In progress",
  5178. )
  5179. self.session.commit()
  5180. self.assertEqual(msg, "Custom field tested adjusted to In progress")
  5181. # Update it a second time to trigger edit
  5182. msg = pagure.lib.query.set_custom_key_value(
  5183. session=self.session,
  5184. issue=issue,
  5185. key=project.issue_keys[0],
  5186. value="Done",
  5187. )
  5188. self.assertEqual(
  5189. msg, "Custom field tested adjusted to Done (was: In progress)"
  5190. )
  5191. self.assertEqual(mock_redis.publish.call_count, 2)
  5192. def test_log_action_invalid(self):
  5193. """ Test the log_action function of pagure.lib.query. """
  5194. obj = MagicMock
  5195. obj.isa = "invalid"
  5196. self.assertRaises(
  5197. pagure.exceptions.InvalidObjectException,
  5198. pagure.lib.query.log_action,
  5199. session=self.session,
  5200. action="foo",
  5201. obj=obj,
  5202. user_obj=None,
  5203. )
  5204. def test_search_token_no_acls(self):
  5205. """ Test the search_token function of pagure.lib.query. """
  5206. tests.create_projects(self.session)
  5207. tests.create_tokens(self.session)
  5208. tests.create_tokens_acl(self.session)
  5209. out = pagure.lib.query.search_token(self.session, [])
  5210. self.assertEqual(len(out), 1)
  5211. self.assertEqual(out[0].id, "aaabbbcccddd")
  5212. def test_search_token_single_acls(self):
  5213. """ Test the search_token function of pagure.lib.query. """
  5214. tests.create_projects(self.session)
  5215. tests.create_tokens(self.session)
  5216. tests.create_tokens_acl(self.session)
  5217. out = pagure.lib.query.search_token(self.session, "issue_create")
  5218. self.assertEqual(len(out), 1)
  5219. self.assertEqual(out[0].id, "aaabbbcccddd")
  5220. def test_search_token_single_acls_user(self):
  5221. """ Test the search_token function of pagure.lib.query. """
  5222. tests.create_projects(self.session)
  5223. tests.create_tokens(self.session)
  5224. tests.create_tokens_acl(self.session)
  5225. out = pagure.lib.query.search_token(
  5226. self.session, "issue_create", user="pingou"
  5227. )
  5228. self.assertEqual(len(out), 1)
  5229. self.assertEqual(out[0].id, "aaabbbcccddd")
  5230. out = pagure.lib.query.search_token(
  5231. self.session, "issue_create", user="foo"
  5232. )
  5233. self.assertEqual(len(out), 0)
  5234. def test_search_token_single_acls_active(self):
  5235. """ Test the search_token function of pagure.lib.query. """
  5236. tests.create_projects(self.session)
  5237. tests.create_tokens(self.session)
  5238. tests.create_tokens_acl(self.session)
  5239. out = pagure.lib.query.search_token(
  5240. self.session, "issue_create", active=True
  5241. )
  5242. self.assertEqual(len(out), 1)
  5243. self.assertEqual(out[0].id, "aaabbbcccddd")
  5244. out = pagure.lib.query.search_token(
  5245. self.session, "issue_create", expired=True
  5246. )
  5247. self.assertEqual(len(out), 0)
  5248. def test_update_read_only_mode(self):
  5249. """ Test the update_read_only_mode method of pagure.lib """
  5250. tests.create_projects(self.session)
  5251. project_obj = pagure.lib.query._get_project(self.session, "test")
  5252. # Default mode of project is read only
  5253. self.assertEqual(project_obj.read_only, True)
  5254. # Remove read only
  5255. pagure.lib.query.update_read_only_mode(
  5256. self.session, project_obj, False
  5257. )
  5258. self.session.commit()
  5259. project_obj = pagure.lib.query._get_project(self.session, "test")
  5260. self.assertEqual(project_obj.read_only, False)
  5261. # Try reversing it back
  5262. pagure.lib.query.update_read_only_mode(self.session, project_obj, True)
  5263. self.session.commit()
  5264. project_obj = pagure.lib.query._get_project(self.session, "test")
  5265. self.assertEqual(project_obj.read_only, True)
  5266. if __name__ == "__main__":
  5267. unittest.main(verbosity=2)