12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068 |
- # -*- coding: utf-8 -*-
- """
- (c) 2015-2018 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- """
- from __future__ import unicode_literals, absolute_import
- import datetime
- import unittest
- import shutil
- import sys
- import os
- import pygit2
- import markdown
- from mock import patch, MagicMock
- sys.path.insert(
- 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
- )
- import pagure
- import pagure.lib.query
- import pagure.lib.model
- import tests
- class PagureLibtests_search_user(tests.Modeltests):
- """
- Test the pagure.lib search_user() method
- """
- maxDiff = None
- def test_search_user_all(self):
- """
- Test the method returns all the users for the given session
- """
- # Retrieve all users
- items = pagure.lib.query.search_user(self.session)
- self.assertEqual(2, len(items))
- self.assertEqual(2, items[0].id)
- self.assertEqual("foo", items[0].user)
- self.assertEqual("foo", items[0].username)
- self.assertEqual([], items[1].groups)
- self.assertEqual(1, items[1].id)
- self.assertEqual("pingou", items[1].user)
- self.assertEqual("pingou", items[1].username)
- self.assertEqual([], items[1].groups)
- def test_search_user_username(self):
- """
- Test the method returns the user for a given username
- """
- # Retrieve user by username
- item = pagure.lib.query.search_user(self.session, username="foo")
- self.assertEqual("foo", item.user)
- self.assertEqual("foo", item.username)
- self.assertEqual([], item.groups)
- item = pagure.lib.query.search_user(self.session, username="bar")
- self.assertEqual(None, item)
- def test_search_user_email(self):
- """
- Test the method returns a user for a given email address
- """
- # Retrieve user by email
- item = pagure.lib.query.search_user(self.session, email="foo@foo.com")
- self.assertEqual(None, item)
- item = pagure.lib.query.search_user(self.session, email="foo@bar.com")
- self.assertEqual("foo", item.user)
- self.assertEqual("foo", item.username)
- self.assertEqual([], item.groups)
- self.assertEqual(
- ["foo@bar.com"], [email.email for email in item.emails]
- )
- item = pagure.lib.query.search_user(
- self.session, email="foo@pingou.com"
- )
- self.assertEqual("pingou", item.user)
- self.assertEqual(
- sorted(["bar@pingou.com", "foo@pingou.com"]),
- sorted([email.email for email in item.emails]),
- )
- def test_search_user_token(self):
- """
- Test the method returns a user for a given token
- """
- # Retrieve user by token
- item = pagure.lib.query.search_user(self.session, token="aaa")
- self.assertEqual(None, item)
- item = pagure.lib.model.User(
- user="pingou2",
- fullname="PY C",
- token="aaabbb",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- self.session.commit()
- item = pagure.lib.query.search_user(self.session, token="aaabbb")
- self.assertEqual("pingou2", item.user)
- self.assertEqual("PY C", item.fullname)
- def test_search_user_pattern(self):
- """
- Test the method returns a user for a given pattern
- """
- # Retrieve user by pattern
- item = pagure.lib.query.search_user(self.session, pattern="a*")
- self.assertEqual([], item)
- item = pagure.lib.model.User(
- user="pingou2",
- fullname="PY C",
- token="aaabbb",
- default_email="bar@pingou.com",
- )
- self.session.add(item)
- self.session.commit()
- items = pagure.lib.query.search_user(self.session, pattern="p*")
- self.assertEqual(2, len(items))
- self.assertEqual(1, items[0].id)
- self.assertEqual("pingou", items[0].user)
- self.assertEqual("pingou", items[0].username)
- self.assertEqual([], items[0].groups)
- self.assertEqual(
- sorted(["bar@pingou.com", "foo@pingou.com"]),
- sorted([email.email for email in items[0].emails]),
- )
- self.assertEqual(3, items[1].id)
- self.assertEqual("pingou2", items[1].user)
- self.assertEqual("pingou2", items[1].username)
- self.assertEqual([], items[1].groups)
- self.assertEqual([], [email.email for email in items[1].emails])
- class PagureLibtests_search_projects(tests.Modeltests):
- """
- Test the pagure.lib search_projects() method
- """
- def setUp(self):
- super(PagureLibtests_search_projects, self).setUp()
- tests.create_projects(self.session)
- def test_search_projects_all(self):
- """
- Test the method returns all the projects for the given session
- """
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 3)
- self.assertEqual(projects[0].id, 1)
- self.assertEqual(projects[1].id, 2)
- def test_search_projects_username(self):
- """
- Test the method returns all the projects for the given username
- """
- projects = pagure.lib.query.search_projects(
- self.session, username="foo"
- )
- self.assertEqual(len(projects), 0)
- projects = pagure.lib.query.search_projects(
- self.session, username="pingou"
- )
- self.assertEqual(len(projects), 3)
- self.assertEqual(projects[0].id, 1)
- self.assertEqual(projects[1].id, 2)
- def test_search_projects_start(self):
- """
- Test the method returns all the projects for the given start
- """
- projects = pagure.lib.query.search_projects(self.session, start=1)
- self.assertEqual(len(projects), 2)
- self.assertEqual(projects[0].id, 2)
- def test_search_projects_limit(self):
- """
- Test the method returns all the projects for the given limit
- """
- projects = pagure.lib.query.search_projects(self.session, limit=1)
- self.assertEqual(len(projects), 1)
- self.assertEqual(projects[0].id, 1)
- def test_search_projects_count(self):
- """
- Test the method returns the count of the projects
- """
- projects = pagure.lib.query.search_projects(self.session, count=True)
- self.assertEqual(projects, 3)
- def test_search_projects_commit_access(self):
- """
- Test the method returns the project of user with only commit access
- """
- # Also check if the project shows up if a user doesn't
- # have admin access in the project
- # Check with commit access first
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- msg = pagure.lib.query.add_user_to_project(
- self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="commit",
- )
- self.assertEqual(msg, "User added")
- self.session.commit()
- projects = pagure.lib.query.search_projects(
- self.session, username="foo"
- )
- self.assertEqual(len(projects), 1)
- def test_search_projects_ticket_access(self):
- """
- Test the method does not returns the project of user with only ticket access
- """
- # Now check with only ticket access
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- msg = pagure.lib.query.add_user_to_project(
- self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="ticket",
- )
- self.assertEqual(msg, "User added")
- self.session.commit()
- projects = pagure.lib.query.search_projects(
- self.session, username="foo"
- )
- self.assertEqual(len(projects), 0)
- def test_search_project_forked(self):
- """
- Test the search_project for forked projects in pagure.lib.query.
- """
- # Create two forked repo
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name="test",
- description="test project #1",
- is_fork=True,
- parent_id=1,
- hook_token="aaabbbttt",
- )
- self.session.add(item)
- item = pagure.lib.model.Project(
- user_id=2, # foo
- name="test2",
- description="test project #2",
- is_fork=True,
- parent_id=2,
- hook_token="aaabbbuuu",
- )
- self.session.add(item)
- # Since we have two forks, let's search them
- projects = pagure.lib.query.search_projects(self.session, fork=True)
- self.assertEqual(len(projects), 2)
- projects = pagure.lib.query.search_projects(self.session, fork=False)
- self.assertEqual(len(projects), 3)
- def test_search_projects_private(self):
- """
- Test the method for private projects
- """
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="private_test",
- description="Private test project #1",
- hook_token="aaabbbcccpp",
- private=True,
- )
- self.session.add(item)
- self.session.commit()
- # non authenticated
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 3)
- self.assertEqual(
- [p.path for p in projects],
- ["test.git", "test2.git", "somenamespace/test3.git"],
- )
- # non authenticated
- projects = pagure.lib.query.search_projects(
- self.session, username="pingou"
- )
- self.assertEqual(len(projects), 3)
- self.assertEqual(
- [p.path for p in projects],
- ["test.git", "test2.git", "somenamespace/test3.git"],
- )
- # authenticated as pingou
- projects = pagure.lib.query.search_projects(
- self.session, username="pingou", private="pingou"
- )
- self.assertEqual(len(projects), 4)
- self.assertEqual(
- [p.path for p in projects],
- [
- "private_test.git",
- "test.git",
- "test2.git",
- "somenamespace/test3.git",
- ],
- )
- # authenticated as foo
- projects = pagure.lib.query.search_projects(
- self.session, username="pingou", private="foo"
- )
- self.assertEqual(len(projects), 3)
- self.assertEqual(
- [p.path for p in projects],
- ["test.git", "test2.git", "somenamespace/test3.git"],
- )
- def test_search_projects_tags(self):
- """
- Test the method returns all the projects for the given tags
- """
- # Add tags to the project
- project = pagure.lib.query._get_project(self.session, "test")
- tag = pagure.lib.model.Tag(tag="fedora")
- self.session.add(tag)
- self.session.commit()
- tp = pagure.lib.model.TagProject(project_id=project.id, tag="fedora")
- self.session.add(tp)
- self.session.commit()
- projects = pagure.lib.query.search_projects(
- self.session, tags="fedora"
- )
- self.assertEqual(len(projects), 1)
- self.assertEqual(projects[0].path, "test.git")
- def test_search_projects_pattern(self):
- """
- Test the method returns all the projects for the given pattern
- """
- projects = pagure.lib.query.search_projects(
- self.session, pattern="test*"
- )
- self.assertEqual(len(projects), 3)
- self.assertEqual(
- [p.path for p in projects],
- ["test.git", "test2.git", "somenamespace/test3.git"],
- )
- def test_search_projects_sort(self):
- """
- Test the method returns all the projects sorted by lastest and oldest
- """
- projects = pagure.lib.query.search_projects(
- self.session, pattern="*", sort="latest"
- )
- self.assertEqual(len(projects), 3)
- self.assertEqual(
- [p.path for p in projects],
- ["somenamespace/test3.git", "test2.git", "test.git"],
- )
- projects = pagure.lib.query.search_projects(
- self.session, pattern="*", sort="oldest"
- )
- self.assertEqual(len(projects), 3)
- self.assertEqual(
- [p.path for p in projects],
- ["test.git", "test2.git", "somenamespace/test3.git"],
- )
- class PagureLibtests(tests.Modeltests):
- """ Tests for pagure.lib """
- maxDiff = None
- def test_get_repotypes(self):
- """ Test the get_repotypes function of pagure.lib.query. """
- with patch.dict(
- pagure.config.config,
- {"ENABLE_TICKETS": False, "ENABLE_DOCS": False},
- ):
- self.assertEqual(
- set(("main", "requests")),
- set(pagure.lib.query.get_repotypes()),
- )
- with patch.dict(
- pagure.config.config,
- {"ENABLE_TICKETS": True, "ENABLE_DOCS": False},
- ):
- self.assertEqual(
- set(("main", "requests", "tickets")),
- set(pagure.lib.query.get_repotypes()),
- )
- with patch.dict(
- pagure.config.config,
- {"ENABLE_TICKETS": False, "ENABLE_DOCS": True},
- ):
- self.assertEqual(
- set(("main", "requests", "docs")),
- set(pagure.lib.query.get_repotypes()),
- )
- with patch.dict(
- pagure.config.config, {"ENABLE_TICKETS": True, "ENABLE_DOCS": True}
- ):
- self.assertEqual(
- set(("main", "requests", "tickets", "docs")),
- set(pagure.lib.query.get_repotypes()),
- )
- def test_get_next_id(self):
- """ Test the get_next_id function of pagure.lib.query. """
- tests.create_projects(self.session)
- self.assertEqual(1, pagure.lib.query.get_next_id(self.session, 1))
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_new_issue(self, p_send_email, p_ugt):
- """ Test the new_issue of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- tests.create_projects(self.session)
- repo = pagure.lib.query._get_project(self.session, "test")
- # Set some priorities to the project
- repo.priorities = {"1": "High", "2": "Normal"}
- self.session.add(repo)
- self.session.commit()
- # Before
- issues = pagure.lib.query.search_issues(self.session, repo)
- self.assertEqual(len(issues), 0)
- self.assertEqual(repo.open_tickets, 0)
- self.assertEqual(repo.open_tickets_public, 0)
- # See where it fails
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.new_issue,
- session=self.session,
- repo=repo,
- title="Test issue",
- content="We should work on this",
- user="blah",
- )
- # Fails since we're trying to give a non-existant priority
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.new_issue,
- session=self.session,
- repo=repo,
- title="Test issue",
- content="We should work on this",
- user="pingou",
- priority=0,
- )
- # Add an extra user to project `foo`
- repo = pagure.lib.query._get_project(self.session, "test")
- msg = pagure.lib.query.add_user_to_project(
- session=self.session, project=repo, new_user="foo", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- # Try adding again this extra user to project `foo`
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_user_to_project,
- session=self.session,
- project=repo,
- new_user="foo",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- # Create issues to play with
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=repo,
- title="Test issue",
- content="We should work on this",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue")
- self.assertEqual(repo.open_tickets, 1)
- self.assertEqual(repo.open_tickets_public, 1)
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=repo,
- title="Test issue #2",
- content="We should work on this for the second time",
- user="foo",
- status="Open",
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue #2")
- self.assertEqual(repo.open_tickets, 2)
- self.assertEqual(repo.open_tickets_public, 2)
- # After
- issues = pagure.lib.query.search_issues(self.session, repo)
- self.assertEqual(len(issues), 2)
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_edit_issue(self, p_send_email, p_ugt):
- """ Test the edit_issue of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_new_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
- self.assertEqual(repo.open_tickets, 2)
- self.assertEqual(repo.open_tickets_public, 2)
- # Edit the issue
- msg = pagure.lib.query.edit_issue(
- session=self.session, issue=issue, user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, None)
- msg = pagure.lib.query.edit_issue(
- session=self.session,
- issue=issue,
- user="pingou",
- title="Test issue #2",
- content="We should work on this for the second time",
- status="Open",
- )
- self.session.commit()
- self.assertEqual(msg, None)
- msg = pagure.lib.query.edit_issue(
- session=self.session,
- issue=issue,
- user="pingou",
- title="Foo issue #2",
- content="We should work on this period",
- status="Closed",
- close_status="Invalid",
- private=True,
- )
- self.session.commit()
- self.assertEqual(
- msg,
- [
- "Issue status updated to: Closed (was: Open)",
- "Issue close_status updated to: Invalid",
- "Issue private status set to: True",
- ],
- )
- msg = pagure.lib.query.edit_issue(
- session=self.session,
- issue=issue,
- user="pingou",
- title="Foo issue #2",
- content="Fixed!",
- status="Closed",
- close_status="Fixed",
- private=False,
- )
- self.session.commit()
- self.assertEqual(
- msg,
- [
- "Issue close_status updated to: Fixed (was: Invalid)",
- "Issue private status set to: False (was: True)",
- ],
- )
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_tickets, 1)
- self.assertEqual(repo.open_tickets_public, 1)
- self.assertEqual(repo.issues[1].status, "Closed")
- self.assertEqual(repo.issues[1].close_status, "Fixed")
- # Edit the status: re-open the ticket
- msg = pagure.lib.query.edit_issue(
- session=self.session,
- issue=issue,
- user="pingou",
- status="Open",
- private=True,
- )
- self.session.commit()
- self.assertEqual(
- msg,
- [
- "Issue status updated to: Open (was: Closed)",
- "Issue private status set to: True",
- ],
- )
- repo = pagure.lib.query._get_project(self.session, "test")
- for issue in repo.issues:
- self.assertEqual(issue.status, "Open")
- self.assertEqual(issue.close_status, None)
- # 2 open but one of them is private
- self.assertEqual(repo.open_tickets, 2)
- self.assertEqual(repo.open_tickets_public, 1)
- # Edit the status: re-close the ticket
- msg = pagure.lib.query.edit_issue(
- session=self.session,
- issue=issue,
- user="pingou",
- status="Closed",
- close_status="Invalid",
- private=True,
- )
- self.session.commit()
- self.assertEqual(
- msg,
- [
- "Issue status updated to: Closed (was: Open)",
- "Issue close_status updated to: Invalid",
- ],
- )
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_tickets, 1)
- self.assertEqual(repo.open_tickets_public, 1)
- self.assertEqual(repo.issues[1].status, "Closed")
- self.assertEqual(repo.issues[1].close_status, "Invalid")
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_edit_issue_close_status(self, p_send_email, p_ugt):
- """ Test the edit_issue of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_new_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
- self.assertEqual(issue.status, "Open")
- self.assertEqual(issue.close_status, None)
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_tickets, 2)
- self.assertEqual(repo.open_tickets_public, 2)
- # Edit the issue, providing just a close_status should also close
- # the ticket
- msg = pagure.lib.query.edit_issue(
- session=self.session,
- issue=issue,
- user="pingou",
- close_status="Fixed",
- )
- self.session.commit()
- self.assertEqual(msg, ["Issue close_status updated to: Fixed"])
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
- self.assertEqual(issue.status, "Closed")
- self.assertEqual(issue.close_status, "Fixed")
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_tickets, 1)
- self.assertEqual(repo.open_tickets_public, 1)
- # Edit the issue, editing the status to open, should reset the
- # close_status
- msg = pagure.lib.query.edit_issue(
- session=self.session, issue=issue, user="pingou", status="Open"
- )
- self.session.commit()
- self.assertEqual(msg, ["Issue status updated to: Open (was: Closed)"])
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
- self.assertEqual(issue.status, "Open")
- self.assertEqual(issue.close_status, None)
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_tickets, 2)
- self.assertEqual(repo.open_tickets_public, 2)
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_edit_issue_priority(self, p_send_email, p_ugt):
- """ Test the edit_issue of pagure.lib when changing the priority.
- """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_new_issue()
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
- # Set some priorities to the repo
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- repo.priorities = {"1": "High", "2": "Normal"}
- self.session.add(repo)
- self.session.commit()
- self.assertEqual(repo.open_tickets, 2)
- self.assertEqual(repo.open_tickets_public, 2)
- # Edit the issue -- Wrong priority value: No changes
- msg = pagure.lib.query.edit_issue(
- session=self.session, issue=issue, user="pingou", priority=3
- )
- self.session.commit()
- self.assertEqual(msg, None)
- # Edit the issue -- Good priority
- msg = pagure.lib.query.edit_issue(
- session=self.session, issue=issue, user="pingou", priority=2
- )
- self.session.commit()
- self.assertEqual(msg, ["Issue priority set to: Normal"])
- # Edit the issue -- Update priority
- msg = pagure.lib.query.edit_issue(
- session=self.session, issue=issue, user="pingou", priority=1
- )
- self.session.commit()
- self.assertEqual(msg, ["Issue priority set to: High (was: Normal)"])
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_edit_issue_depending(self, p_send_email, p_ugt):
- """ Test the edit_issue of pagure.lib when the issue depends on
- another.
- """
- p_send_email.return_value = True
- p_ugt.return_value = True
- tests.create_projects(self.session)
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- # Create 3 issues
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=repo,
- title="Test issue #1",
- content="We should work on this for the second time",
- user="foo",
- status="Open",
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue #1")
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=repo,
- title="Test issue #2",
- content="We should work on this for the second time",
- user="foo",
- status="Open",
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue #2")
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=repo,
- title="Test issue #3",
- content="We should work on this for the second time",
- user="foo",
- status="Open",
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue #3")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
- self.assertEqual(repo.open_tickets, 3)
- self.assertEqual(repo.open_tickets_public, 3)
- # Make issue #2 blocking on issue #1
- msgs = pagure.lib.query.update_blocked_issue(
- self.session, repo, issue, blocks=["1"], username="pingou"
- )
- self.assertEqual(msgs, ["Issue marked as blocking: #1"])
- # Make issue #2 depend on issue #3
- msgs = pagure.lib.query.update_dependency_issue(
- self.session, repo, issue, depends=["3"], username="pingou"
- )
- self.assertEqual(msgs, ["Issue marked as depending on: #3"])
- # Edit the issue #3
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=3)
- msg = pagure.lib.query.edit_issue(
- session=self.session, issue=issue, user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, None)
- msg = pagure.lib.query.edit_issue(
- session=self.session,
- issue=issue,
- user="pingou",
- title="Foo issue #2",
- content="We should work on this period",
- status="Closed",
- close_status="Invalid",
- private=True,
- )
- self.session.commit()
- self.assertEqual(
- msg,
- [
- "Issue status updated to: Closed (was: Open)",
- "Issue close_status updated to: Invalid",
- "Issue private status set to: True",
- ],
- )
- self.assertEqual(repo.open_tickets, 2)
- self.assertEqual(repo.open_tickets_public, 2)
- @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_add_issue_dependency(self):
- """ Test the add_issue_dependency of pagure.lib.query. """
- self.test_new_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- issue_blocked = pagure.lib.query.search_issues(
- self.session, repo, issueid=2
- )
- # Before
- self.assertEqual(issue.parents, [])
- self.assertEqual(issue.children, [])
- self.assertEqual(issue_blocked.parents, [])
- self.assertEqual(issue_blocked.children, [])
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_issue_dependency,
- session=self.session,
- issue=issue,
- issue_blocked=issue,
- user="pingou",
- )
- msg = pagure.lib.query.add_issue_dependency(
- session=self.session,
- issue=issue,
- issue_blocked=issue_blocked,
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg, "Issue marked as depending on: #2")
- # After
- self.assertEqual(len(issue.parents), 0)
- self.assertEqual(issue.parents, [])
- self.assertEqual(len(issue.children), 1)
- self.assertEqual(issue.children[0].id, 2)
- self.assertEqual(issue.depending_text, [])
- self.assertEqual(issue.blocking_text, [2])
- self.assertEqual(len(issue_blocked.children), 0)
- self.assertEqual(issue_blocked.children, [])
- self.assertEqual(len(issue_blocked.parents), 1)
- self.assertEqual(issue_blocked.parents[0].id, 1)
- self.assertEqual(issue_blocked.depending_text, [1])
- self.assertEqual(issue_blocked.blocking_text, [])
- @patch("pagure.lib.query.REDIS")
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_edit_comment(self, mock_redis):
- """ Test the edit_issue of pagure.lib.query. """
- mock_redis.return_value = True
- self.test_add_issue_comment()
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_tickets, 2)
- self.assertEqual(repo.open_tickets_public, 2)
- self.assertEqual(mock_redis.publish.call_count, 0)
- # Before
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(len(issue.comments), 1)
- self.assertEqual(issue.comments[0].comment, "Hey look a comment!")
- # Edit one of the
- msg = pagure.lib.query.edit_comment(
- session=self.session,
- parent=issue,
- comment=issue.comments[0],
- user="pingou",
- updated_comment="Edited comment",
- )
- self.session.commit()
- self.assertEqual(msg, "Comment updated")
- self.assertEqual(mock_redis.publish.call_count, 1)
- # After
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(len(issue.comments), 1)
- self.assertEqual(issue.comments[0].comment, "Edited comment")
- @patch("pagure.lib.query.REDIS")
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_edit_comment_private(self, mock_redis):
- """ Test the edit_issue of pagure.lib.query. """
- self.test_add_issue_comment_private()
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_tickets, 1)
- self.assertEqual(repo.open_tickets_public, 0)
- self.assertEqual(mock_redis.publish.call_count, 0)
- # Before
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(len(issue.comments), 1)
- self.assertEqual(issue.comments[0].comment, "Hey look a comment!")
- # Edit one of the
- msg = pagure.lib.query.edit_comment(
- session=self.session,
- parent=issue,
- comment=issue.comments[0],
- user="pingou",
- updated_comment="Edited comment",
- )
- self.session.commit()
- self.assertEqual(msg, "Comment updated")
- self.assertEqual(mock_redis.publish.call_count, 1)
- # After
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(len(issue.comments), 1)
- self.assertEqual(issue.comments[0].comment, "Edited comment")
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- @patch("pagure.lib.query.REDIS")
- def test_add_tag_obj(self, mock_redis):
- """ Test the add_tag_obj of pagure.lib.query. """
- mock_redis.return_value = True
- self.test_edit_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertFalse(issue.private)
- self.assertFalse(issue.project.private)
- args = mock_redis.publish.call_args_list
- self.assertEqual(len(args), 4)
- # Add a tag to the issue
- msg = pagure.lib.query.add_tag_obj(
- session=self.session, obj=issue, tags="tag1", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Issue tagged with: tag1")
- args = mock_redis.publish.call_args_list
- self.assertEqual(len(args), 5)
- # Get the arguments of the last call and get the second of these
- # arguments (the first one changing for each test run)
- self.assertJSONEqual(
- args[-1:][0][0][1],
- '{"added_tags_color": ["DeepSkyBlue"], "added_tags": ["tag1"]}',
- )
- # Try a second time
- msg = pagure.lib.query.add_tag_obj(
- session=self.session, obj=issue, tags="tag1", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Nothing to add")
- issues = pagure.lib.query.search_issues(
- self.session, repo, tags="tag1"
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 1)
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual(issues[0].status, "Open")
- self.assertEqual([tag.tag for tag in issues[0].tags], ["tag1"])
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_remove_tags(self, p_send_email, p_ugt):
- """ Test the remove_tags of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_add_tag_obj()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.remove_tags,
- session=self.session,
- project=repo,
- tags="foo",
- user="pingou",
- )
- msgs = pagure.lib.query.remove_tags(
- session=self.session, project=repo, tags="tag1", user="pingou"
- )
- self.assertEqual(msgs, ["Tag: tag1 has been deleted"])
- @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_remove_tags_obj(self):
- """ Test the remove_tags_obj of pagure.lib.query. """
- self.test_add_tag_obj()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- msgs = pagure.lib.query.remove_tags_obj(
- session=self.session, obj=issue, tags="tag1", user="pingou"
- )
- self.assertEqual(msgs, "Issue **un**tagged with: tag1")
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_remove_tags_obj_from_project(self, p_send_email, p_ugt):
- """ Test the remove_tags_obj of pagure.lib from a project. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- tests.create_projects(self.session)
- # Add a tag to the project
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- msg = pagure.lib.query.add_tag_obj(
- self.session, repo, tags=["pagure", "test"], user="pingou"
- )
- self.assertEqual(msg, "Project tagged with: pagure, test")
- self.session.commit()
- # Check the tags
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(repo.tags_text, ["pagure", "test"])
- # Remove one of the the tag
- msgs = pagure.lib.query.remove_tags_obj(
- session=self.session, obj=repo, tags="test", user="pingou"
- )
- self.assertEqual(msgs, "Project **un**tagged with: test")
- self.session.commit()
- # Check the tags
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- self.assertEqual(repo.tags_text, ["pagure"])
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_edit_issue_tags(self, p_send_email, p_ugt):
- """ Test the edit_issue_tags of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_add_tag_obj()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.edit_issue_tags,
- session=self.session,
- project=repo,
- old_tag="foo",
- new_tag="bar",
- new_tag_description="lorem ipsum",
- new_tag_color="black",
- user="pingou",
- )
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.edit_issue_tags,
- session=self.session,
- project=repo,
- old_tag=None,
- new_tag="bar",
- new_tag_description="lorem ipsum",
- new_tag_color="black",
- user="pingou",
- )
- msgs = pagure.lib.query.edit_issue_tags(
- session=self.session,
- project=repo,
- old_tag="tag1",
- new_tag="tag2",
- new_tag_description="lorem ipsum",
- new_tag_color="black",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(
- msgs,
- ["Edited tag: tag1()[DeepSkyBlue] to tag2(lorem ipsum)[black]"],
- )
- # Try editing the tag without changing anything
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.edit_issue_tags,
- session=self.session,
- project=repo,
- old_tag="tag2",
- new_tag="tag2",
- new_tag_description="lorem ipsum",
- new_tag_color="black",
- user="pingou",
- )
- # Add a new tag
- msg = pagure.lib.query.add_tag_obj(
- session=self.session, obj=issue, tags="tag3", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Issue tagged with: tag3")
- self.assertEqual([tag.tag for tag in issue.tags], ["tag2", "tag3"])
- # Attempt to rename an existing tag into another existing one
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.edit_issue_tags,
- session=self.session,
- project=repo,
- old_tag="tag2",
- new_tag="tag3",
- new_tag_description="lorem ipsum",
- new_tag_color="red",
- user="pingou",
- )
- # Rename an existing tag
- msgs = pagure.lib.query.edit_issue_tags(
- session=self.session,
- project=repo,
- old_tag="tag2",
- new_tag="tag4",
- new_tag_description="ipsum lorem",
- new_tag_color="purple",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(
- msgs,
- [
- "Edited tag: tag2(lorem ipsum)[black] to tag4(ipsum lorem)[purple]"
- ],
- )
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_search_issues(self, p_send_email, p_ugt):
- """ Test the search_issues of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_edit_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- # All issues
- issues = pagure.lib.query.search_issues(self.session, repo)
- self.assertEqual(len(issues), 2)
- self.assertEqual(issues[1].id, 1)
- self.assertEqual(issues[1].project_id, 1)
- self.assertEqual(issues[1].status, "Open")
- self.assertEqual(issues[1].tags, [])
- self.assertEqual(issues[0].id, 2)
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual(issues[0].status, "Closed")
- self.assertEqual(issues[0].close_status, "Invalid")
- self.assertEqual(issues[0].tags, [])
- # Issues by status
- issues = pagure.lib.query.search_issues(
- self.session, repo, status="Closed"
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 2)
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual(issues[0].status, "Closed")
- self.assertEqual(issues[0].close_status, "Invalid")
- self.assertEqual(issues[0].tags, [])
- # Issues closed
- issues = pagure.lib.query.search_issues(
- self.session, repo, closed=True
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 2)
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual(issues[0].status, "Closed")
- self.assertEqual(issues[0].close_status, "Invalid")
- self.assertEqual(issues[0].tags, [])
- # Issues by tag
- issues = pagure.lib.query.search_issues(self.session, repo, tags="foo")
- self.assertEqual(len(issues), 0)
- issues = pagure.lib.query.search_issues(
- self.session, repo, tags="!foo"
- )
- self.assertEqual(len(issues), 2)
- # Issue by id
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(issue.title, "Test issue")
- self.assertEqual(issue.user.user, "pingou")
- self.assertEqual(issue.tags, [])
- # Issues by authors
- issues = pagure.lib.query.search_issues(
- self.session, repo, author="foo"
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 2)
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual(issues[0].status, "Closed")
- self.assertEqual(issues[0].close_status, "Invalid")
- self.assertEqual(issues[0].tags, [])
- # Issues by assignee
- issues = pagure.lib.query.search_issues(
- self.session, repo, assignee="foo"
- )
- self.assertEqual(len(issues), 0)
- issues = pagure.lib.query.search_issues(
- self.session, repo, assignee="!foo"
- )
- self.assertEqual(len(issues), 2)
- issues = pagure.lib.query.search_issues(
- self.session, repo, private="foo"
- )
- self.assertEqual(len(issues), 2)
- @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_add_issue_assignee(self):
- """ Test the add_issue_assignee of pagure.lib.query. """
- self.test_new_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
- # Before
- issues = pagure.lib.query.search_issues(
- self.session, repo, assignee="pingou"
- )
- self.assertEqual(len(issues), 0)
- # Test when it fails
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_issue_assignee,
- session=self.session,
- issue=issue,
- assignee="foo@foobar.com",
- user="foo@pingou.com",
- )
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_issue_assignee,
- session=self.session,
- issue=issue,
- assignee="foo@bar.com",
- user="foo@foopingou.com",
- )
- # Set the assignee by its email
- msg = pagure.lib.query.add_issue_assignee(
- session=self.session,
- issue=issue,
- assignee="foo@bar.com",
- user="foo@pingou.com",
- )
- self.session.commit()
- self.assertEqual(msg, "Issue assigned to foo@bar.com")
- # Change the assignee to someone else by its username
- msg = pagure.lib.query.add_issue_assignee(
- session=self.session, issue=issue, assignee="pingou", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Issue assigned to pingou (was: foo)")
- # After -- Searches by assignee
- issues = pagure.lib.query.search_issues(
- self.session, repo, assignee="pingou"
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 2)
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual(issues[0].status, "Open")
- self.assertEqual(issues[0].tags, [])
- issues = pagure.lib.query.search_issues(
- self.session, repo, assignee=True
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 2)
- self.assertEqual(issues[0].title, "Test issue #2")
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual(issues[0].status, "Open")
- self.assertEqual(issues[0].tags, [])
- issues = pagure.lib.query.search_issues(
- self.session, repo, assignee=False
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 1)
- self.assertEqual(issues[0].title, "Test issue")
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual(issues[0].status, "Open")
- self.assertEqual(issues[0].tags, [])
- # Reset the assignee to no-one
- msg = pagure.lib.query.add_issue_assignee(
- session=self.session, issue=issue, assignee=None, user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Assignee reset")
- issues = pagure.lib.query.search_issues(
- self.session, repo, assignee=False
- )
- self.assertEqual(len(issues), 2)
- self.assertEqual(issues[0].id, 2)
- self.assertEqual(issues[1].id, 1)
- issues = pagure.lib.query.search_issues(
- self.session, repo, assignee=True
- )
- self.assertEqual(len(issues), 0)
- @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_add_issue_comment(self):
- """ Test the add_issue_comment of pagure.lib.query. """
- self.test_new_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- # Before
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(len(issue.comments), 0)
- # Set the assignee by its email
- msg = pagure.lib.query.add_issue_assignee(
- session=self.session,
- issue=issue,
- assignee="foo@bar.com",
- user="foo@pingou.com",
- )
- self.session.commit()
- self.assertEqual(msg, "Issue assigned to foo@bar.com")
- # Add a comment to that issue
- msg = pagure.lib.query.add_issue_comment(
- session=self.session,
- issue=issue,
- comment="Hey look a comment!",
- user="foo",
- )
- self.session.commit()
- self.assertEqual(msg, "Comment added")
- # After
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(len(issue.comments), 1)
- self.assertEqual(issue.comments[0].comment, "Hey look a comment!")
- self.assertEqual(issue.comments[0].user.user, "foo")
- @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_add_issue_comment_private(self):
- """ Test the add_issue_comment of pagure.lib.query. """
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=project,
- title="Test issue #1",
- content="We should work on this for the second time",
- user="foo",
- status="Open",
- private=True,
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue #1")
- self.assertEqual(project.open_tickets, 1)
- self.assertEqual(project.open_tickets_public, 0)
- # Before
- issue = pagure.lib.query.search_issues(
- self.session, project, issueid=1
- )
- self.assertEqual(len(issue.comments), 0)
- # Add a comment to that issue
- msg = pagure.lib.query.add_issue_comment(
- session=self.session,
- issue=issue,
- comment="Hey look a comment!",
- user="foo",
- )
- self.session.commit()
- self.assertEqual(msg, "Comment added")
- # After
- issue = pagure.lib.query.search_issues(
- self.session, project, issueid=1
- )
- self.assertEqual(len(issue.comments), 1)
- self.assertEqual(issue.comments[0].comment, "Hey look a comment!")
- self.assertEqual(issue.comments[0].user.user, "foo")
- @patch("pagure.lib.notify.send_email")
- def test_add_user_to_project(self, p_send_email):
- """ Test the add_user_to_project of pagure.lib.query. """
- p_send_email.return_value = True
- tests.create_projects(self.session)
- # Before
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(len(repo.users), 0)
- # Add an user to a project
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_user_to_project,
- session=self.session,
- project=repo,
- new_user="foobar",
- user="pingou",
- )
- msg = pagure.lib.query.add_user_to_project(
- session=self.session, project=repo, new_user="foo", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- # After
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(len(repo.users), 1)
- self.assertEqual(repo.users[0].user, "foo")
- self.assertEqual(repo.admins[0].user, "foo")
- # Try adding the same user with the same access
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_user_to_project,
- session=self.session,
- project=repo,
- new_user="foo",
- user="pingou",
- access="admin",
- )
- # Update the access of the user
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=repo,
- new_user="foo",
- user="pingou",
- access="commit",
- )
- self.session.commit()
- self.assertEqual(msg, "User access updated")
- self.assertEqual(len(repo.users), 1)
- self.assertEqual(repo.users[0].user, "foo")
- self.assertEqual(repo.committers[0].user, "foo")
- def test_new_project(self):
- """ Test the new_project of pagure.lib.query. """
- gitfolder = os.path.join(self.path, "repos")
- docfolder = os.path.join(gitfolder, "docs")
- ticketfolder = os.path.join(gitfolder, "tickets")
- requestfolder = os.path.join(gitfolder, "requests")
- # Try creating a blacklisted project
- self.assertRaises(
- pagure.exceptions.ProjectBlackListedException,
- pagure.lib.query.new_project,
- session=self.session,
- user="pingou",
- name="static",
- repospanner_region=None,
- blacklist=["static"],
- allowed_prefix=[],
- description="description for static",
- parent_id=None,
- )
- # Try creating a project that's blacklisted by wildcard match
- self.assertRaises(
- pagure.exceptions.ProjectBlackListedException,
- pagure.lib.query.new_project,
- session=self.session,
- user="pingou",
- namespace="space",
- name="static",
- repospanner_region=None,
- blacklist=["space/*"],
- allowed_prefix=["space"],
- description="description for static",
- parent_id=None,
- )
- # Try creating a 40 chars project
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.new_project,
- session=self.session,
- user="pingou",
- name="s" * 40,
- namespace="pingou",
- repospanner_region=None,
- blacklist=["static"],
- allowed_prefix=["pingou"],
- description="description for 40 chars length project",
- parent_id=None,
- prevent_40_chars=True,
- )
- # Create a new project
- pagure.config.config["GIT_FOLDER"] = gitfolder
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": None,
- },
- )
- # Try creating an existing project using a different case
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.new_project,
- session=self.session,
- user="pingou",
- name="TestProject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- )
- # Now test that creation fails if ignore_existing_repo is False
- self.session.commit()
- repo = pagure.lib.query.get_authorized_project(
- self.session, "testproject"
- )
- self.assertEqual(repo.path, "testproject.git")
- gitrepo = os.path.join(gitfolder, repo.path)
- docrepo = os.path.join(docfolder, repo.path)
- ticketrepo = os.path.join(ticketfolder, repo.path)
- requestrepo = os.path.join(requestfolder, repo.path)
- self.assertTrue(os.path.exists(gitrepo))
- self.assertTrue(os.path.exists(docrepo))
- self.assertTrue(os.path.exists(ticketrepo))
- self.assertTrue(os.path.exists(requestrepo))
- # Try re-creating it but all repos are existing
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.new_project,
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- )
- self.session.rollback()
- self.assertTrue(os.path.exists(gitrepo))
- self.assertTrue(os.path.exists(docrepo))
- self.assertTrue(os.path.exists(ticketrepo))
- self.assertTrue(os.path.exists(requestrepo))
- # Try re-creating it ignoring the existing repos- but repo in the DB
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.new_project,
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- )
- self.session.rollback()
- # Re-create it, ignoring the existing repos on disk
- repo = pagure.lib.query._get_project(self.session, "testproject")
- self.session.delete(repo)
- self.session.commit()
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- ignore_existing_repo=True,
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": None,
- },
- )
- # Delete the repo from the DB so we can try again
- repo = pagure.lib.query._get_project(self.session, "testproject")
- self.session.delete(repo)
- self.session.commit()
- self.assertTrue(os.path.exists(gitrepo))
- self.assertTrue(os.path.exists(docrepo))
- self.assertTrue(os.path.exists(ticketrepo))
- self.assertTrue(os.path.exists(requestrepo))
- # Drop the main git repo and try again
- shutil.rmtree(gitrepo)
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- )
- self.assertIn("already exists", str(task.get(propagate=False)))
- self.assertFalse(os.path.exists(gitrepo))
- self.assertTrue(os.path.exists(docrepo))
- self.assertTrue(os.path.exists(ticketrepo))
- self.assertTrue(os.path.exists(requestrepo))
- # Drop the doc repo and try again
- shutil.rmtree(docrepo)
- with self.assertRaises(pagure.exceptions.RepoExistsException):
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- )
- task.get()
- self.assertFalse(os.path.exists(gitrepo))
- self.assertFalse(os.path.exists(docrepo))
- self.assertTrue(os.path.exists(ticketrepo))
- self.assertTrue(os.path.exists(requestrepo))
- # Drop the request repo and try again
- shutil.rmtree(ticketrepo)
- with self.assertRaises(pagure.exceptions.RepoExistsException):
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- )
- task.get()
- self.assertFalse(os.path.exists(gitrepo))
- self.assertFalse(os.path.exists(docrepo))
- self.assertFalse(os.path.exists(ticketrepo))
- self.assertTrue(os.path.exists(requestrepo))
- # Re-Try creating a 40 chars project this time allowing it
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="pingou/" + "s" * 40,
- repospanner_region=None,
- blacklist=["static"],
- allowed_prefix=["pingou"],
- description="description for 40 chars length project",
- parent_id=None,
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "pingou/ssssssssssssssssssssssssssssssssssssssss",
- "namespace": None,
- },
- )
- def test_new_project_user_ns(self):
- """ Test the new_project of pagure.lib with user_ns on. """
- gitfolder = os.path.join(self.path, "repos")
- docfolder = os.path.join(gitfolder, "docs")
- ticketfolder = os.path.join(gitfolder, "tickets")
- requestfolder = os.path.join(gitfolder, "requests")
- # Create a new project with user_ns as True
- pagure.config.config["GIT_FOLDER"] = gitfolder
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- user_ns=True,
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": "pingou",
- },
- )
- repo = pagure.lib.query._get_project(
- self.session, "testproject", namespace="pingou"
- )
- self.assertEqual(repo.path, "pingou/testproject.git")
- gitrepo = os.path.join(gitfolder, repo.path)
- docrepo = os.path.join(docfolder, repo.path)
- ticketrepo = os.path.join(ticketfolder, repo.path)
- requestrepo = os.path.join(requestfolder, repo.path)
- for path in [gitrepo, docrepo, ticketrepo, requestrepo]:
- self.assertTrue(os.path.exists(path))
- shutil.rmtree(path)
- # Create a new project with a namespace and user_ns as True
- pagure.config.config["GIT_FOLDER"] = gitfolder
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject2",
- namespace="testns",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=["testns"],
- description="description for testproject2",
- parent_id=None,
- user_ns=True,
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject2",
- "namespace": "testns",
- },
- )
- repo = pagure.lib.query._get_project(
- self.session, "testproject2", namespace="testns"
- )
- self.assertEqual(repo.path, "testns/testproject2.git")
- gitrepo = os.path.join(gitfolder, repo.path)
- docrepo = os.path.join(docfolder, repo.path)
- ticketrepo = os.path.join(ticketfolder, repo.path)
- requestrepo = os.path.join(requestfolder, repo.path)
- for path in [gitrepo, docrepo, ticketrepo, requestrepo]:
- self.assertTrue(os.path.exists(path))
- shutil.rmtree(path)
- @patch("pagure.lib.notify.log")
- def test_update_project_settings(self, mock_log):
- """ Test the update_project_settings of pagure.lib.query. """
- tests.create_projects(self.session)
- # Before
- repo = pagure.lib.query._get_project(self.session, "test2")
- self.assertTrue(repo.settings["issue_tracker"])
- self.assertFalse(repo.settings["project_documentation"])
- msg = pagure.lib.query.update_project_settings(
- session=self.session,
- repo=repo,
- settings={
- "issue_tracker": True,
- "project_documentation": False,
- "pull_requests": True,
- "Only_assignee_can_merge_pull-request": False,
- "Minimum_score_to_merge_pull-request": -1,
- "Web-hooks": None,
- "Enforce_signed-off_commits_in_pull-request": False,
- "always_merge": False,
- "issues_default_to_private": False,
- "fedmsg_notifications": True,
- "stomp_notifications": True,
- "pull_request_access_only": False,
- "mqtt_notifications": True,
- },
- user="pingou",
- )
- self.assertEqual(msg, "No settings to change")
- mock_log.assert_not_called()
- # Invalid `Minimum_score_to_merge_pull-request`
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.update_project_settings,
- session=self.session,
- repo=repo,
- settings={
- "issue_tracker": False,
- "project_documentation": True,
- "pull_requests": False,
- "Only_assignee_can_merge_pull-request": None,
- "Minimum_score_to_merge_pull-request": "foo",
- "Web-hooks": "https://pagure.io/foobar",
- "Enforce_signed-off_commits_in_pull-request": False,
- "issues_default_to_private": False,
- "fedmsg_notifications": True,
- "stomp_notifications": True,
- "pull_request_access_only": False,
- },
- user="pingou",
- )
- msg = pagure.lib.query.update_project_settings(
- session=self.session,
- repo=repo,
- settings={
- "issue_tracker": False,
- "project_documentation": True,
- "pull_requests": False,
- "Only_assignee_can_merge_pull-request": None,
- "Minimum_score_to_merge_pull-request": None,
- "Web-hooks": "https://pagure.io/foobar",
- "Enforce_signed-off_commits_in_pull-request": False,
- "issues_default_to_private": False,
- "fedmsg_notifications": True,
- "stomp_notifications": True,
- "pull_request_access_only": False,
- "mqtt_notifications": True,
- },
- user="pingou",
- )
- self.assertEqual(msg, "Edited successfully settings of repo: test2")
- self.assertEqual(mock_log.call_count, 1)
- args = mock_log.call_args
- self.assertEqual(len(args), 2)
- self.assertEqual(args[0][0].fullname, "test2")
- self.assertEqual(
- sorted(args[1]["msg"]["fields"]),
- sorted(
- [
- "Web-hooks",
- "project_documentation",
- "issue_tracker",
- "pull_requests",
- ]
- ),
- )
- self.assertEqual(args[1]["topic"], "project.edit")
- # After
- repo = pagure.lib.query._get_project(self.session, "test2")
- self.assertFalse(repo.settings["issue_tracker"])
- self.assertTrue(repo.settings["project_documentation"])
- self.assertFalse(repo.settings["pull_requests"])
- def test_search_issues_milestones_invalid(self):
- """ Test the search_issues of pagure.lib.query. """
- self.test_edit_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(len(repo.issues), 2)
- issues = pagure.lib.query.search_issues(
- self.session, repo, milestones="foo"
- )
- self.assertEqual(len(issues), 0)
- issues = pagure.lib.query.search_issues(
- self.session, repo, milestones="foo", no_milestones=True
- )
- self.assertEqual(len(issues), 2)
- def test_search_issues_custom_search(self):
- """ Test the search_issues of pagure.lib.query. """
- self.test_edit_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(len(repo.issues), 2)
- issues = pagure.lib.query.search_issues(
- self.session, repo, custom_search={"foo": "*"}
- )
- self.assertEqual(len(issues), 0)
- def test_search_issues_offset(self):
- """ Test the search_issues of pagure.lib.query. """
- self.test_edit_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issues = pagure.lib.query.search_issues(self.session, repo)
- self.assertEqual(len(issues), 2)
- self.assertEqual([i.id for i in issues], [2, 1])
- issues = pagure.lib.query.search_issues(self.session, repo, offset=1)
- self.assertEqual(len(issues), 1)
- self.assertEqual([i.id for i in issues], [1])
- def test_search_issues_tags(self):
- """ Test the search_issues of pagure.lib.query. """
- self.test_edit_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(len(repo.issues), 2)
- # Add `tag1` to one issues and `tag2` only to the other one
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- msg = pagure.lib.query.add_tag_obj(
- session=self.session, obj=issue, tags="tag1", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Issue tagged with: tag1")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=2)
- msg = pagure.lib.query.add_tag_obj(
- session=self.session, obj=issue, tags="tag2", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Issue tagged with: tag2")
- # Search all issues tagged with `tag1`
- issues = pagure.lib.query.search_issues(
- self.session, repo, tags="tag1"
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 1)
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual([tag.tag for tag in issues[0].tags], ["tag1"])
- # Search all issues *not* tagged with `tag1`
- issues = pagure.lib.query.search_issues(
- self.session, repo, tags="!tag1"
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 2)
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual([tag.tag for tag in issues[0].tags], ["tag2"])
- # Search all issues *not* tagged with `tag1` but tagged with `tag2`
- issues = pagure.lib.query.search_issues(
- self.session, repo, tags=["!tag1", "tag2"]
- )
- self.assertEqual(len(issues), 1)
- self.assertEqual(issues[0].id, 2)
- self.assertEqual(issues[0].project_id, 1)
- self.assertEqual([tag.tag for tag in issues[0].tags], ["tag2"])
- def test_get_tags_of_project(self):
- """ Test the get_tags_of_project of pagure.lib.query. """
- self.test_add_tag_obj()
- repo = pagure.lib.query._get_project(self.session, "test")
- tags = pagure.lib.query.get_tags_of_project(self.session, repo)
- self.assertEqual([tag.tag for tag in tags], ["tag1"])
- tags = pagure.lib.query.get_tags_of_project(
- self.session, repo, pattern="T*"
- )
- self.assertEqual([tag.tag for tag in tags], ["tag1"])
- repo = pagure.lib.query._get_project(self.session, "test2")
- tags = pagure.lib.query.get_tags_of_project(self.session, repo)
- self.assertEqual([tag.tag for tag in tags], [])
- def test_get_issue_statuses(self):
- """ Test the get_issue_statuses of pagure.lib.query. """
- statuses = pagure.lib.query.get_issue_statuses(self.session)
- self.assertEqual(sorted(statuses), ["Closed", "Open"])
- @patch.dict(
- pagure.config.config, {"ALLOWED_EMAIL_DOMAINS": ["fp.o"]}, clear=True
- )
- def test_set_up_user(self):
- """ Test the set_up_user of pagure.lib.query. """
- items = pagure.lib.query.search_user(self.session)
- self.assertEqual(2, len(items))
- self.assertEqual(2, items[0].id)
- self.assertEqual("foo", items[0].user)
- self.assertEqual(1, items[1].id)
- self.assertEqual("pingou", items[1].user)
- pagure.lib.query.set_up_user(
- session=self.session,
- username="skvidal",
- fullname="Seth",
- default_email="skvidal@fp.o",
- keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
- ssh_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDtgzSO9d1IrKdmyBFUvtAJPLgGOhp0lSySkWRSe+/+3KXYjSnsLnCJQlO5M7JfaXhtTHEow86rh4W9+FoJdzo5iocAwH5xPZ5ttHLy7VHgTzNMUeMgKpjy6bBOdPoGPPG4mo7QCMCRJdWBRDv4OSEMLU5jQAvC272YK2V8L918VQ== root@test",
- )
- self.session.commit()
- items = pagure.lib.query.search_user(self.session)
- self.assertEqual(3, len(items))
- self.assertEqual(2, items[0].id)
- self.assertEqual("foo", items[0].user)
- self.assertEqual(1, items[1].id)
- self.assertEqual("pingou", items[1].user)
- self.assertEqual(3, items[2].id)
- self.assertEqual("skvidal", items[2].user)
- self.assertEqual("Seth", items[2].fullname)
- self.assertEqual(
- ["skvidal@fp.o"], [email.email for email in items[2].emails]
- )
- # Add the user a second time
- pagure.lib.query.set_up_user(
- session=self.session,
- username="skvidal",
- fullname="Seth V",
- default_email="skvidal@fp.o",
- keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
- )
- self.session.commit()
- # Nothing changed
- items = pagure.lib.query.search_user(self.session)
- self.assertEqual(3, len(items))
- self.assertEqual("skvidal", items[2].user)
- self.assertEqual("Seth V", items[2].fullname)
- self.assertEqual(
- ["skvidal@fp.o"], [email.email for email in items[2].emails]
- )
- # Add the user a third time with a different email
- pagure.lib.query.set_up_user(
- session=self.session,
- username="skvidal",
- fullname="Seth",
- default_email="svidal@fp.o",
- keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
- )
- self.session.commit()
- # Email added
- items = pagure.lib.query.search_user(self.session)
- self.assertEqual(3, len(items))
- self.assertEqual("skvidal", items[2].user)
- self.assertEqual(
- sorted(["skvidal@fp.o", "svidal@fp.o"]),
- sorted([email.email for email in items[2].emails]),
- )
- # add again with forbidden email domain
- pagure.lib.query.set_up_user(
- session=self.session,
- username="skvidal",
- fullname="Seth",
- default_email="svidal@example.o",
- keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
- )
- self.session.commit()
- # Email should not be added
- items = pagure.lib.query.search_user(self.session)
- self.assertEqual(3, len(items))
- self.assertEqual("skvidal", items[2].user)
- self.assertEqual(
- sorted(["skvidal@fp.o", "svidal@fp.o"]),
- sorted([email.email for email in items[2].emails]),
- )
- @patch.dict(
- pagure.config.config,
- {"ALLOWED_EMAIL_DOMAINS": ["fp.o", "example.c"]},
- clear=True,
- )
- def test_set_up_user_multiple_emaildomains(self):
- """ Test the set_up_user of pagure.lib when there are
- multimple domains configured in ALLOWED_EMAIL_DOMAINS"""
- items = pagure.lib.query.search_user(self.session)
- self.assertEqual(2, len(items))
- self.assertEqual(2, items[0].id)
- self.assertEqual("foo", items[0].user)
- self.assertEqual(1, items[1].id)
- self.assertEqual("pingou", items[1].user)
- pagure.lib.query.set_up_user(
- session=self.session,
- username="skvidal",
- fullname="Seth",
- default_email="skvidal@fp.o",
- keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
- )
- self.session.commit()
- # Add the user a second time with a different email
- pagure.lib.query.set_up_user(
- session=self.session,
- username="skvidal",
- fullname="Seth",
- default_email="skvidal@example.c",
- keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
- )
- self.session.commit()
- # Email added
- items = pagure.lib.query.search_user(self.session)
- self.assertEqual(3, len(items))
- self.assertEqual("skvidal", items[2].user)
- self.assertEqual(
- sorted(["skvidal@fp.o", "skvidal@example.c"]),
- sorted([email.email for email in items[2].emails]),
- )
- # add again with forbidden email domain
- pagure.lib.query.set_up_user(
- session=self.session,
- username="skvidal",
- fullname="Seth",
- default_email="svidal@example.o",
- keydir=pagure.config.config.get("GITOLITE_KEYDIR", None),
- )
- self.session.commit()
- # Email should not be added
- items = pagure.lib.query.search_user(self.session)
- self.assertEqual(3, len(items))
- self.assertEqual("skvidal", items[2].user)
- self.assertEqual(
- sorted(["skvidal@fp.o", "skvidal@example.c"]),
- sorted([email.email for email in items[2].emails]),
- )
- def avatar_url_from_email(self):
- """ Test the avatar_url_from_openid of pagure.lib.query. """
- output = pagure.lib.query.avatar_url_from_email(
- "pingou@fedoraproject.org"
- )
- self.assertEqual(
- output,
- "https://seccdn.libravatar.org/avatar/"
- "b3ee7bb4de70b6522c2478df3b4cd6322b5ec5d62ac7ceb1128e3d4ff42f6928"
- "?s=64&d=retro",
- )
- output = pagure.lib.query.avatar_url_from_email("zoé@çëfò.org")
- self.assertEqual(
- output,
- "https://seccdn.libravatar.org/avatar/"
- "8fa6110d1f6a7a013969f012e1149ff89bf1252d4f15d25edee31d4662878656"
- "?s=64&d=retro",
- )
- def test_fork_project_with_branch(self):
- """ Test the fork_project of pagure.lib.query. """
- gitfolder = os.path.join(self.path, "repos")
- docfolder = os.path.join(gitfolder, "docs")
- ticketfolder = os.path.join(gitfolder, "tickets")
- requestfolder = os.path.join(gitfolder, "requests")
- pagure.config.config["GIT_FOLDER"] = gitfolder
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 0)
- # Create a new project
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": None,
- },
- )
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 1)
- project = pagure.lib.query._get_project(self.session, "testproject")
- gitrepo = os.path.join(gitfolder, project.path)
- docrepo = os.path.join(docfolder, project.path)
- ticketrepo = os.path.join(ticketfolder, project.path)
- requestrepo = os.path.join(requestfolder, project.path)
- # Add content to the main repo into three branches
- tests.add_content_git_repo(gitrepo, "master")
- tests.add_content_git_repo(gitrepo, "feature1")
- tests.add_content_git_repo(gitrepo, "feature2")
- # Check the branches of the main repo
- self.assertEqual(
- sorted(pagure.lib.git.get_git_branches(project)),
- ["feature1", "feature2", "master"],
- )
- # Fork
- task = pagure.lib.query.fork_project(
- session=self.session, user="foo", repo=project
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": None,
- "username": "foo",
- },
- )
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 2)
- project = pagure.lib.query._get_project(
- self.session, "testproject", user="foo"
- )
- # Check the branches of the fork
- self.assertEqual(
- sorted(pagure.lib.git.get_git_branches(project)),
- ["feature1", "feature2", "master"],
- )
- def test_fork_project_preserves_tags(self):
- """ Test the fork_project of pagure.lib.query pushes tags to the fork. """
- gitfolder = os.path.join(self.path, "repos")
- docfolder = os.path.join(gitfolder, "docs")
- ticketfolder = os.path.join(gitfolder, "tickets")
- requestfolder = os.path.join(gitfolder, "requests")
- pagure.config.config["GIT_FOLDER"] = gitfolder
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 0)
- # Create a new project
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=[],
- description="description for testproject",
- parent_id=None,
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": None,
- },
- )
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 1)
- project = pagure.lib.query._get_project(self.session, "testproject")
- gitrepo = os.path.join(gitfolder, project.path)
- docrepo = os.path.join(docfolder, project.path)
- ticketrepo = os.path.join(ticketfolder, project.path)
- requestrepo = os.path.join(requestfolder, project.path)
- # Add content to the main repo into three branches
- tests.add_content_git_repo(gitrepo, "master")
- # Add a tag
- tagged_commit = (
- pygit2.Repository(gitrepo).revparse_single("master").hex
- )
- tag_sha = tests.add_tag_git_repo(
- gitrepo, "1.2.3", tagged_commit, "release 1.2.3"
- )
- # Fork
- task = pagure.lib.query.fork_project(
- session=self.session, user="foo", repo=project
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": None,
- "username": "foo",
- },
- )
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 2)
- project = pagure.lib.query._get_project(
- self.session, "testproject", user="foo"
- )
- # Check the tag is there
- fork_obj = pygit2.Repository(project.repopath("main"))
- tag = fork_obj.get(tag_sha)
- self.assertEqual(fork_obj[tag.target].hex, tagged_commit)
- self.assertEqual(tag.message, "release 1.2.3")
- def test_fork_project_namespaced(self):
- """ Test the fork_project of pagure.lib on a namespaced project. """
- gitfolder = os.path.join(self.path, "repos")
- docfolder = os.path.join(gitfolder, "docs")
- ticketfolder = os.path.join(gitfolder, "tickets")
- requestfolder = os.path.join(gitfolder, "requests")
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 0)
- # Create a new project
- task = pagure.lib.query.new_project(
- session=self.session,
- user="pingou",
- name="testproject",
- namespace="foonamespace",
- repospanner_region=None,
- blacklist=[],
- allowed_prefix=["foonamespace"],
- description="description for testproject",
- parent_id=None,
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": "foonamespace",
- },
- )
- projects = pagure.lib.query.search_projects(self.session)
- self.assertEqual(len(projects), 1)
- repo = pagure.lib.query._get_project(
- self.session, "testproject", namespace="foonamespace"
- )
- gitrepo = os.path.join(gitfolder, repo.path)
- docrepo = os.path.join(docfolder, repo.path)
- ticketrepo = os.path.join(ticketfolder, repo.path)
- requestrepo = os.path.join(requestfolder, repo.path)
- self.assertTrue(os.path.exists(gitrepo))
- self.assertTrue(os.path.exists(docrepo))
- self.assertTrue(os.path.exists(ticketrepo))
- self.assertTrue(os.path.exists(requestrepo))
- # Fork worked
- task = pagure.lib.query.fork_project(
- session=self.session, user="foo", repo=repo
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": "foonamespace",
- "username": "foo",
- },
- )
- # Fork a fork
- repo = pagure.lib.query._get_project(
- self.session, "testproject", user="foo", namespace="foonamespace"
- )
- task = pagure.lib.query.fork_project(
- session=self.session, user="pingou", repo=repo
- )
- self.session.commit()
- self.assertEqual(
- task.get(),
- {
- "endpoint": "ui_ns.view_repo",
- "repo": "testproject",
- "namespace": "foonamespace",
- "username": "pingou",
- },
- )
- @patch("pagure.lib.notify.send_email")
- def test_new_pull_request(self, mockemail):
- """ test new_pull_request of pagure.lib.query. """
- mockemail.return_value = True
- tests.create_projects(self.session)
- # Create a forked repo
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- description="test project #1",
- is_fork=True,
- parent_id=1,
- hook_token="aaabbbrrr",
- )
- self.session.commit()
- self.session.add(item)
- # Add an extra user to project `foo`
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_requests, 0)
- msg = pagure.lib.query.add_user_to_project(
- session=self.session, project=repo, new_user="foo", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- repo = pagure.lib.query._get_project(self.session, "test")
- forked_repo = pagure.lib.query._get_project(
- self.session, "test", user="pingou"
- )
- # Fails for the lack of repo_from and remote_git
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.new_pull_request,
- session=self.session,
- repo_from=None,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="test pull-request",
- user="pingou",
- )
- # Let's pretend we turned on the CI hook for the project
- project = pagure.lib.query._get_project(self.session, "test")
- obj = pagure.hooks.pagure_ci.PagureCITable(
- project_id=project.id, active=True
- )
- self.session.add(obj)
- self.session.commit()
- # Create the new PR
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=repo,
- branch_to="master",
- title="test pull-request",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(req.id, 1)
- self.assertEqual(req.title, "test pull-request")
- self.assertEqual(repo.open_requests, 1)
- @patch("pagure.lib.query.REDIS")
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_add_pull_request_comment(self, mock_redis):
- """ Test add_pull_request_comment of pagure.lib.query. """
- mock_redis.return_value = True
- self.test_new_pull_request()
- request = pagure.lib.query.search_pull_requests(
- self.session, requestid=1
- )
- msg = pagure.lib.query.add_pull_request_comment(
- session=self.session,
- request=request,
- commit="commithash",
- tree_id=None,
- filename="file",
- row=None,
- comment="This is awesome, I got to remember it!",
- user="foo",
- notification=True,
- )
- self.assertEqual(msg, "Comment added")
- self.session.commit()
- self.assertEqual(len(request.discussion), 0)
- self.assertEqual(len(request.comments), 1)
- self.assertEqual(request.score, 0)
- self.assertEqual(mock_redis.publish.call_count, 1)
- @patch("pagure.lib.query.REDIS")
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- @patch("pagure.lib.query.PAGURE_CI", MagicMock(return_value=True))
- def test_add_pull_request_comment_to_re_run_ci(self, mock_redis):
- """ Test add_pull_request_comment of pagure.lib.query. """
- mock_redis.return_value = True
- self.test_new_pull_request()
- self.assertEqual(mock_redis.publish.call_count, 0)
- # Let's pretend we turned on the CI hook for the project
- project = pagure.lib.query._get_project(self.session, "test")
- if not project.ci_hook or not project.ci_hook.active:
- obj = pagure.hooks.pagure_ci.PagureCITable(
- project_id=project.id, active=True
- )
- self.session.add(obj)
- self.session.commit()
- request = pagure.lib.query.search_pull_requests(
- self.session, requestid=1
- )
- msg = pagure.lib.query.add_pull_request_comment(
- session=self.session,
- request=request,
- commit="commithash",
- tree_id=None,
- filename="file",
- row=None,
- comment="Pretty please pagure-ci rebuild",
- user="foo",
- notification=True,
- trigger_ci=["pretty please pagure-ci rebuild"],
- )
- self.assertEqual(msg, "Comment added")
- self.session.commit()
- self.assertEqual(len(request.discussion), 0)
- self.assertEqual(len(request.comments), 1)
- self.assertEqual(request.score, 0)
- self.assertEqual(mock_redis.publish.call_count, 1)
- @patch("pagure.lib.notify.send_email")
- def test_add_pull_request_flag(self, mockemail):
- """ Test add_pull_request_flag of pagure.lib.query. """
- mockemail.return_value = True
- self.test_new_pull_request()
- tests.create_tokens(self.session)
- request = pagure.lib.query.search_pull_requests(
- self.session, requestid=1
- )
- self.assertEqual(len(request.flags), 0)
- msg = pagure.lib.query.add_pull_request_flag(
- session=self.session,
- request=request,
- username="jenkins",
- percent=100,
- comment="Build passes",
- status="success",
- url="http://jenkins.cloud.fedoraproject.org",
- uid="jenkins_build_pagure_34",
- user="foo",
- token="aaabbbcccddd",
- )
- self.assertEqual(msg, ("Flag added", "jenkins_build_pagure_34"))
- self.session.commit()
- self.assertEqual(len(request.flags), 1)
- self.assertEqual(request.flags[0].token_id, "aaabbbcccddd")
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_search_pull_requests(self):
- """ Test search_pull_requests of pagure.lib.query. """
- self.test_new_pull_request()
- prs = pagure.lib.query.search_pull_requests(session=self.session)
- self.assertEqual(len(prs), 1)
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, project_id=1
- )
- self.assertEqual(len(prs), 1)
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, project_id_from=4
- )
- self.assertEqual(len(prs), 1)
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, status=False
- )
- self.assertEqual(len(prs), 0)
- # All non-assigned PR
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, assignee=False
- )
- self.assertEqual(len(prs), 1)
- prs[0].assignee_id = 1
- self.session.add(prs[0])
- self.session.commit()
- # All the PR assigned
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, assignee=True
- )
- self.assertEqual(len(prs), 1)
- # Basically the same as above but then for a specific user
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, assignee="pingou"
- )
- self.assertEqual(len(prs), 1)
- # All PR except those assigned to pingou
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, assignee="!pingou"
- )
- self.assertEqual(len(prs), 0)
- # All PR created by the specified author
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, author="pingou"
- )
- self.assertEqual(len(prs), 1)
- # Count the PR instead of listing them
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, author="pingou", count=True
- )
- self.assertEqual(prs, 1)
- dt = datetime.datetime.utcnow()
- # Create the second PR
- repo = pagure.lib.query._get_project(self.session, "test")
- req = pagure.lib.query.new_pull_request(
- session=self.session,
- repo_from=repo,
- branch_from="feature",
- repo_to=repo,
- branch_to="master",
- title="test pull-request #2",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(req.id, 2)
- self.assertEqual(req.title, "test pull-request #2")
- self.assertEqual(repo.open_requests, 2)
- # Ensure we have 2 PRs
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, author="pingou"
- )
- self.assertEqual(len(prs), 2)
- # Test the offset
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, author="pingou", offset=1
- )
- self.assertEqual(len(prs), 1)
- # Test the updated_after
- # Test updated after before the second PR was created
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, author="pingou", updated_after=dt
- )
- self.assertEqual(len(prs), 1)
- # Test updated after, 1h ago
- prs = pagure.lib.query.search_pull_requests(
- session=self.session,
- author="pingou",
- updated_after=dt - datetime.timedelta(hours=1),
- )
- self.assertEqual(len(prs), 2)
- @patch("pagure.lib.notify.send_email")
- def test_close_pull_request(self, send_email):
- """ Test close_pull_request of pagure.lib.query. """
- send_email.return_value = True
- self.test_new_pull_request()
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_requests, 1)
- request = pagure.lib.query.search_pull_requests(
- self.session, requestid=1
- )
- pagure.lib.query.close_pull_request(
- session=self.session, request=request, user="pingou", merged=True
- )
- self.session.commit()
- repo = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(repo.open_requests, 0)
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, status=False
- )
- self.assertEqual(len(prs), 1)
- # Does not change much, just the notification sent
- pagure.lib.query.close_pull_request(
- session=self.session, request=request, user="pingou", merged=False
- )
- self.session.commit()
- prs = pagure.lib.query.search_pull_requests(
- session=self.session, status=False
- )
- self.assertEqual(len(prs), 1)
- @patch("pagure.lib.query.REDIS", MagicMock(return_value=True))
- @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_remove_issue_dependency(self):
- """ Test remove_issue_dependency of pagure.lib.query. """
- self.test_add_issue_dependency()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- issue_blocked = pagure.lib.query.search_issues(
- self.session, repo, issueid=2
- )
- # Before
- self.assertEqual(len(issue.children), 1)
- self.assertEqual(issue.children[0].id, 2)
- self.assertEqual(len(issue.parents), 0)
- self.assertEqual(issue.parents, [])
- self.assertEqual(len(issue_blocked.children), 0)
- self.assertEqual(issue_blocked.children, [])
- self.assertEqual(len(issue_blocked.parents), 1)
- self.assertEqual(issue_blocked.parents[0].id, 1)
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.remove_issue_dependency,
- session=self.session,
- issue=issue,
- issue_blocked=issue,
- user="pingou",
- )
- # Wrong order of issues
- msg = pagure.lib.query.remove_issue_dependency(
- session=self.session,
- issue=issue,
- issue_blocked=issue_blocked,
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg, None)
- # Drop deps
- msg = pagure.lib.query.remove_issue_dependency(
- session=self.session,
- issue=issue_blocked,
- issue_blocked=issue,
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg, "Issue **un**marked as depending on: #1")
- # After
- self.assertEqual(issue.parents, [])
- self.assertEqual(issue.children, [])
- self.assertEqual(issue_blocked.parents, [])
- self.assertEqual(issue_blocked.children, [])
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_get_issue_comment(self, p_send_email, p_ugt):
- """ Test the get_issue_comment of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_add_issue_comment()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(
- pagure.lib.query.get_issue_comment(self.session, issue.uid, 10),
- None,
- )
- comment = pagure.lib.query.get_issue_comment(
- self.session, issue.uid, 1
- )
- self.assertEqual(comment.comment, "Hey look a comment!")
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_get_issue_by_uid(self, p_send_email, p_ugt):
- """ Test the get_issue_by_uid of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_new_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(
- pagure.lib.query.get_issue_by_uid(self.session, "foobar"), None
- )
- new_issue = pagure.lib.query.get_issue_by_uid(self.session, issue.uid)
- self.assertEqual(issue, new_issue)
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_update_tags(self, p_send_email, p_ugt):
- """ Test the update_tags of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_new_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- # before
- self.assertEqual(repo.tags_colored, [])
- self.assertEqual(issue.tags_text, [])
- messages = pagure.lib.query.update_tags(
- self.session, issue, "tag", "pingou"
- )
- self.assertEqual(messages, ["Issue tagged with: tag"])
- # after
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual([t.tag for t in repo.tags_colored], ["tag"])
- self.assertEqual(issue.tags_text, ["tag"])
- # Replace the tag by two others
- messages = pagure.lib.query.update_tags(
- self.session, issue, ["tag2", "tag3"], "pingou"
- )
- self.assertEqual(
- messages,
- ["Issue tagged with: tag2, tag3", "Issue **un**tagged with: tag"],
- )
- # after
- repo = pagure.lib.query.get_authorized_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(
- sorted([t.tag for t in repo.tags_colored]), ["tag", "tag2", "tag3"]
- )
- self.assertEqual(sorted(issue.tags_text), ["tag2", "tag3"])
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_update_dependency_issue(self, p_send_email, p_ugt):
- """ Test the update_dependency_issue of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_new_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- self.assertEqual(repo.open_tickets, 2)
- self.assertEqual(repo.open_tickets_public, 2)
- # Create issues to play with
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=repo,
- title="Test issue #3",
- content="We should work on this (3rd time!)",
- user="pingou",
- private=True,
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue #3")
- self.assertEqual(repo.open_tickets, 3)
- self.assertEqual(repo.open_tickets_public, 2)
- # before
- self.assertEqual(issue.tags_text, [])
- self.assertEqual(issue.depending_text, [])
- self.assertEqual(issue.blocking_text, [])
- messages = pagure.lib.query.update_dependency_issue(
- self.session, repo, issue, "2", "pingou"
- )
- self.assertEqual(messages, ["Issue marked as depending on: #2"])
- messages = pagure.lib.query.update_dependency_issue(
- self.session, repo, issue, ["3", "4", 5], "pingou"
- )
- self.assertEqual(
- messages,
- [
- "Issue marked as depending on: #3",
- "Issue marked as depending on: #4",
- "Issue marked as depending on: #5",
- "Issue **un**marked as depending on: #2",
- ],
- )
- # after
- self.assertEqual(issue.tags_text, [])
- self.assertEqual(issue.depending_text, [3])
- self.assertEqual(issue.blocking_text, [])
- @patch("pagure.lib.git.update_git")
- @patch("pagure.lib.notify.send_email")
- def test_update_blocked_issue(self, p_send_email, p_ugt):
- """ Test the update_blocked_issue of pagure.lib.query. """
- p_send_email.return_value = True
- p_ugt.return_value = True
- self.test_new_issue()
- repo = pagure.lib.query._get_project(self.session, "test")
- issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
- # Create issues to play with
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=repo,
- title="Test issue #3",
- content="We should work on this (3rd time!)",
- user="pingou",
- private=True,
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue #3")
- # before
- self.assertEqual(issue.tags_text, [])
- self.assertEqual(issue.depending_text, [])
- self.assertEqual(issue.blocking_text, [])
- messages = pagure.lib.query.update_blocked_issue(
- self.session, repo, issue, "2", "pingou"
- )
- self.assertEqual(messages, ["Issue marked as blocking: #2"])
- messages = pagure.lib.query.update_blocked_issue(
- self.session, repo, issue, ["3", "4", 5], "pingou"
- )
- self.assertEqual(
- messages,
- [
- "Issue marked as blocking: #3",
- "Issue marked as blocking: #4",
- "Issue marked as blocking: #5",
- "Issue **un**marked as blocking: #2",
- ],
- )
- # after
- self.assertEqual(issue.tags_text, [])
- self.assertEqual(issue.depending_text, [])
- self.assertEqual(issue.blocking_text, [3])
- @patch("pagure.lib.notify.send_email")
- def test_add_pull_request_assignee(self, mockemail):
- """ Test add_pull_request_assignee of pagure.lib.query. """
- mockemail.return_value = True
- self.test_new_pull_request()
- request = pagure.lib.query.search_pull_requests(
- self.session, requestid=1
- )
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_pull_request_assignee,
- session=self.session,
- request=request,
- assignee="bar",
- user="foo",
- )
- # Assign
- msg = pagure.lib.query.add_pull_request_assignee(
- session=self.session,
- request=request,
- assignee="pingou",
- user="foo",
- )
- self.assertEqual(msg, "Request assigned")
- # Reset
- msg = pagure.lib.query.add_pull_request_assignee(
- session=self.session, request=request, assignee=None, user="foo"
- )
- self.assertEqual(msg, "Request assignee reset")
- # Try resetting again
- msg = pagure.lib.query.add_pull_request_assignee(
- session=self.session, request=request, assignee=None, user="foo"
- )
- self.assertEqual(msg, None)
- def test_search_pending_email(self):
- """ Test search_pending_email of pagure.lib.query. """
- self.assertEqual(
- pagure.lib.query.search_pending_email(self.session), None
- )
- user = pagure.lib.query.search_user(self.session, username="pingou")
- email_pend = pagure.lib.model.UserEmailPending(
- user_id=user.id, email="foo@fp.o", token="abcdef"
- )
- self.session.add(email_pend)
- self.session.commit()
- self.assertNotEqual(
- pagure.lib.query.search_pending_email(self.session), None
- )
- self.assertNotEqual(
- pagure.lib.query.search_pending_email(
- self.session, token="abcdef"
- ),
- None,
- )
- pend = pagure.lib.query.search_pending_email(
- self.session, token="abcdef"
- )
- self.assertEqual(pend.user.username, "pingou")
- self.assertEqual(pend.email, "foo@fp.o")
- self.assertEqual(pend.token, "abcdef")
- pend = pagure.lib.query.search_pending_email(
- self.session, email="foo@fp.o"
- )
- self.assertEqual(pend.user.username, "pingou")
- self.assertEqual(pend.email, "foo@fp.o")
- self.assertEqual(pend.token, "abcdef")
- def test_generate_hook_token(self):
- """ Test generate_hook_token of pagure.lib.query. """
- tests.create_projects(self.session)
- projects = pagure.lib.query.search_projects(self.session)
- for proj in projects:
- self.assertIn(
- proj.hook_token, ["aaabbbccc", "aaabbbddd", "aaabbbeee"]
- )
- pagure.lib.query.generate_hook_token(self.session)
- projects = pagure.lib.query.search_projects(self.session)
- for proj in projects:
- self.assertNotIn(
- proj.hook_token, ["aaabbbccc", "aaabbbddd", "aaabbbeee"]
- )
- @patch("pagure.lib.notify.send_email")
- def test_pull_request_score(self, mockemail):
- """ Test PullRequest.score of pagure.lib.model. """
- mockemail.return_value = True
- self.test_new_pull_request()
- request = pagure.lib.query.search_pull_requests(
- self.session, requestid=1
- )
- msg = pagure.lib.query.add_pull_request_comment(
- session=self.session,
- request=request,
- commit=None,
- tree_id=None,
- filename=None,
- row=None,
- comment="This looks great :thumbsup:",
- user="foo",
- )
- self.session.commit()
- self.assertEqual(msg, "Comment added")
- msg = pagure.lib.query.add_pull_request_comment(
- session=self.session,
- request=request,
- commit=None,
- tree_id=None,
- filename=None,
- row=None,
- comment="I disagree -1",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg, "Comment added")
- msg = pagure.lib.query.add_pull_request_comment(
- session=self.session,
- request=request,
- commit=None,
- tree_id=None,
- filename=None,
- row=None,
- comment="NM this looks great now +1000",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg, "Comment added")
- self.assertEqual(len(request.discussion), 3)
- self.assertEqual(request.score, 2)
- def test_add_group(self):
- """ Test the add_group method of pagure.lib.query. """
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- self.assertEqual(groups, [])
- # Invalid type
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group,
- self.session,
- group_name="foo",
- display_name="foo group",
- description=None,
- group_type="bar",
- user="pingou",
- is_admin=True,
- blacklist=[],
- )
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- self.assertEqual(groups, [])
- # Invalid user
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group,
- self.session,
- group_name="foo",
- display_name="foo group",
- description=None,
- group_type="user",
- user="test",
- is_admin=False,
- blacklist=[],
- )
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- self.assertEqual(groups, [])
- # Invalid group name
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group,
- self.session,
- group_name="foo group",
- display_name="foo group",
- description=None,
- group_type="user",
- user="test",
- is_admin=False,
- blacklist=[],
- )
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 0)
- self.assertEqual(groups, [])
- msg = pagure.lib.query.add_group(
- self.session,
- group_name="foo",
- display_name="foo group",
- description=None,
- group_type="bar",
- user="pingou",
- is_admin=False,
- blacklist=[],
- )
- self.session.commit()
- self.assertEqual(msg, "User `pingou` added to the group `foo`.")
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].group_name, "foo")
- # Group with this name already exists
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group,
- self.session,
- group_name="foo",
- display_name="foo group",
- description=None,
- group_type="bar",
- user="pingou",
- is_admin=False,
- blacklist=[],
- )
- # Group with this display name already exists
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group,
- self.session,
- group_name="foo1",
- display_name="foo group",
- description=None,
- group_type="bar",
- user="pingou",
- is_admin=False,
- blacklist=[],
- )
- # Group with a blacklisted prefix
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group,
- self.session,
- group_name="forks",
- display_name="foo group",
- description=None,
- group_type="bar",
- user="pingou",
- is_admin=False,
- blacklist=["forks"],
- )
- def test_add_user_to_group(self):
- """ Test the add_user_to_group method of pagure.lib.query. """
- self.test_add_group()
- group = pagure.lib.query.search_groups(self.session, group_name="foo")
- self.assertNotEqual(group, None)
- self.assertEqual(group.group_name, "foo")
- # Invalid new user
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_user_to_group,
- self.session,
- username="foobar",
- group=group,
- user="foo",
- is_admin=False,
- )
- # Invalid user
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_user_to_group,
- self.session,
- username="foo",
- group=group,
- user="foobar",
- is_admin=False,
- )
- # User not allowed
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_user_to_group,
- self.session,
- username="foo",
- group=group,
- user="foo",
- is_admin=False,
- )
- msg = pagure.lib.query.add_user_to_group(
- self.session,
- username="foo",
- group=group,
- user="pingou",
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "User `foo` added to the group `foo`.")
- msg = pagure.lib.query.add_user_to_group(
- self.session,
- username="foo",
- group=group,
- user="pingou",
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(
- msg, "User `foo` already in the group, nothing to change."
- )
- def test_is_group_member(self):
- """ Test the is_group_member method of pagure.lib.query. """
- self.test_add_group()
- self.assertFalse(
- pagure.lib.query.is_group_member(self.session, None, "foo")
- )
- self.assertFalse(
- pagure.lib.query.is_group_member(self.session, "bar", "foo")
- )
- self.assertFalse(
- pagure.lib.query.is_group_member(self.session, "foo", "foo")
- )
- self.assertTrue(
- pagure.lib.query.is_group_member(self.session, "pingou", "foo")
- )
- def test_get_user_group(self):
- """ Test the get_user_group method of pagure.lib.query. """
- self.test_add_group()
- item = pagure.lib.query.get_user_group(self.session, 1, 1)
- self.assertEqual(item.user_id, 1)
- self.assertEqual(item.group_id, 1)
- item = pagure.lib.query.get_user_group(self.session, 1, 2)
- self.assertEqual(item, None)
- item = pagure.lib.query.get_user_group(self.session, 2, 1)
- self.assertEqual(item, None)
- def test_get_group_types(self):
- """ Test the get_group_types method of pagure.lib.query. """
- self.test_add_group()
- groups = pagure.lib.query.get_group_types(self.session, "user")
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].group_type, "user")
- groups = pagure.lib.query.get_group_types(self.session)
- self.assertEqual(len(groups), 2)
- self.assertEqual(groups[0].group_type, "admin")
- self.assertEqual(groups[1].group_type, "user")
- def test_search_groups(self):
- """ Test the search_groups method of pagure.lib.query. """
- self.assertEqual(pagure.lib.query.search_groups(self.session), [])
- msg = pagure.lib.query.add_group(
- self.session,
- group_name="foo",
- display_name="foo group",
- description=None,
- group_type="bar",
- user="pingou",
- is_admin=False,
- blacklist=[],
- )
- self.session.commit()
- self.assertEqual(msg, "User `pingou` added to the group `foo`.")
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].group_name, "foo")
- msg = pagure.lib.query.add_group(
- self.session,
- group_name="bar",
- display_name="bar group",
- description=None,
- group_type="admin",
- user="pingou",
- is_admin=True,
- blacklist=[],
- )
- self.session.commit()
- self.assertEqual(msg, "User `pingou` added to the group `bar`.")
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 2)
- self.assertEqual(groups[0].group_name, "bar")
- self.assertEqual(groups[1].group_name, "foo")
- groups = pagure.lib.query.search_groups(
- self.session, group_type="user"
- )
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].group_name, "foo")
- groups = pagure.lib.query.search_groups(
- self.session, group_type="admin"
- )
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].group_name, "bar")
- groups = pagure.lib.query.search_groups(self.session, group_name="foo")
- self.assertEqual(groups.group_name, "foo")
- def test_delete_user_of_group(self):
- """ Test the delete_user_of_group method of pagure.lib.query. """
- self.test_add_user_to_group()
- groups = pagure.lib.query.search_groups(self.session)
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].group_name, "foo")
- # Invalid username
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.delete_user_of_group,
- self.session,
- username="bar",
- groupname="foo",
- user="pingou",
- is_admin=False,
- )
- # Invalid groupname
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.delete_user_of_group,
- self.session,
- username="foo",
- groupname="bar",
- user="pingou",
- is_admin=False,
- )
- # Invalid user
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.delete_user_of_group,
- self.session,
- username="foo",
- groupname="foo",
- user="test",
- is_admin=False,
- )
- # User not in the group
- item = pagure.lib.model.User(
- user="bar",
- fullname="bar",
- password="foo",
- default_email="bar@bar.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
- self.session.add(item)
- self.session.commit()
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.delete_user_of_group,
- self.session,
- username="bar",
- groupname="foo",
- user="pingou",
- is_admin=False,
- )
- # User is not allowed to remove the username
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.delete_user_of_group,
- self.session,
- username="foo",
- groupname="foo",
- user="bar",
- is_admin=False,
- )
- # Username is the creator of the group
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.delete_user_of_group,
- self.session,
- username="pingou",
- groupname="foo",
- user="pingou",
- is_admin=False,
- )
- # All good
- group = pagure.lib.query.search_groups(self.session, group_name="foo")
- self.assertEqual(len(group.users), 2)
- pagure.lib.query.delete_user_of_group(
- self.session,
- username="foo",
- groupname="foo",
- user="pingou",
- is_admin=False,
- )
- self.session.commit()
- group = pagure.lib.query.search_groups(self.session, group_name="foo")
- self.assertEqual(len(group.users), 1)
- def test_edit_group_info(self):
- """ Test the edit_group_info method of pagure.lib.query. """
- self.test_add_group()
- group = pagure.lib.query.search_groups(self.session, group_name="foo")
- self.assertNotEqual(group, None)
- self.assertEqual(group.group_name, "foo")
- # Invalid new user
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.edit_group_info,
- self.session,
- group=group,
- display_name="edited name",
- description=None,
- user="foo",
- is_admin=False,
- )
- # Invalid user
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.edit_group_info,
- self.session,
- group=group,
- display_name="edited name",
- description=None,
- user="foobar",
- is_admin=False,
- )
- # User not allowed
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.edit_group_info,
- self.session,
- group=group,
- display_name="edited name",
- description=None,
- user="bar",
- is_admin=False,
- )
- msg = pagure.lib.query.edit_group_info(
- self.session,
- group=group,
- display_name="edited name",
- description=None,
- user="pingou",
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, 'Group "edited name" (foo) edited')
- msg = pagure.lib.query.edit_group_info(
- self.session,
- group=group,
- display_name="edited name",
- description=None,
- user="pingou",
- is_admin=False,
- )
- self.session.commit()
- self.assertEqual(msg, "Nothing changed")
- def test_add_group_to_project(self):
- """ Test the add_group_to_project method of pagure.lib.query. """
- tests.create_projects(self.session)
- self.test_add_group()
- project = pagure.lib.query._get_project(self.session, "test2")
- # Group does not exist
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group_to_project,
- session=self.session,
- project=project,
- new_group="bar",
- user="foo",
- )
- # Group does not exist, but allow creating it
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="bar",
- user="pingou",
- create=True,
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- self.assertEqual(project.groups[0].group_name, "bar")
- self.assertEqual(len(project.admin_groups), 1)
- self.assertEqual(project.admin_groups[0].group_name, "bar")
- # User does not exist
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group_to_project,
- session=self.session,
- project=project,
- new_group="foo",
- user="bar",
- )
- # User not allowed
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group_to_project,
- session=self.session,
- project=project,
- new_group="foo",
- user="foo",
- )
- # All good
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="foo",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- self.assertEqual(project.groups[0].group_name, "bar")
- self.assertEqual(project.groups[1].group_name, "foo")
- self.assertEqual(len(project.admin_groups), 2)
- self.assertEqual(project.admin_groups[0].group_name, "bar")
- self.assertEqual(project.admin_groups[1].group_name, "foo")
- self.assertEqual(len(project.committer_groups), 2)
- # Group already associated with the project
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_group_to_project,
- session=self.session,
- project=project,
- new_group="foo",
- user="pingou",
- )
- # Update the access of group in the project
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="foo",
- user="pingou",
- access="commit",
- )
- self.session.commit()
- self.assertEqual(msg, "Group access updated")
- self.assertEqual(project.groups[0].group_name, "bar")
- self.assertEqual(project.groups[1].group_name, "foo")
- self.assertEqual(len(project.admin_groups), 1)
- self.assertEqual(project.admin_groups[0].group_name, "bar")
- self.assertEqual(len(project.committer_groups), 2)
- # Update the access of group in the project
- msg = pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="foo",
- user="pingou",
- access="ticket",
- )
- self.session.commit()
- self.assertEqual(msg, "Group access updated")
- self.assertEqual(project.groups[0].group_name, "bar")
- self.assertEqual(project.groups[1].group_name, "foo")
- self.assertEqual(len(project.admin_groups), 1)
- self.assertEqual(project.admin_groups[0].group_name, "bar")
- self.assertEqual(len(project.committer_groups), 1)
- self.assertEqual(project.committer_groups[0].group_name, "bar")
- def test_update_watch_status(self):
- """ Test the update_watch_status method of pagure.lib.query. """
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- # User does not exist
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.update_watch_status,
- session=self.session,
- project=project,
- user="aavrug",
- watch="1",
- )
- # Invalid watch status
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.update_watch_status,
- session=self.session,
- project=project,
- user="pingou",
- watch="me fail",
- )
- # All good and when user selected reset watch option.
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project, user="pingou", watch="-1"
- )
- self.session.commit()
- self.assertEqual(msg, "Watch status is already reset")
- # All good and when user selected watch issues option.
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project, user="pingou", watch="1"
- )
- self.session.commit()
- self.assertEqual(
- msg, "You are now watching issues and PRs on this project"
- )
- # All good and when user selected unwatch option.
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project, user="pingou", watch="0"
- )
- self.session.commit()
- self.assertEqual(msg, "You are no longer watching this project")
- # All good and when user seleted reset watch option.
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project, user="pingou", watch="-1"
- )
- self.session.commit()
- self.assertEqual(msg, "Watch status reset")
- def test_get_watch_level_on_repo_invalid(self):
- """ test the get_watch_level_on_repo method of pagure.lib.query. """
- self.assertRaises(
- RuntimeError,
- pagure.lib.query.get_watch_level_on_repo,
- session=self.session,
- user="pingou",
- repo=None,
- repouser=None,
- namespace=None,
- )
- def test_get_watch_level_on_repo(self):
- """ Test the get_watch_level_on_repo method of pagure.lib.query. """
- tests.create_projects(self.session)
- self.test_add_group()
- project = pagure.lib.query._get_project(self.session, "test")
- project2 = pagure.lib.query._get_project(self.session, "test2")
- # If user not logged in
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=None, repo="test"
- )
- self.assertEqual(watch_level, [])
- # User does not exist
- user = tests.FakeUser()
- user.username = "aavrug"
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="test"
- )
- self.assertEqual(watch_level, [])
- # Invalid project
- watch = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="invalid"
- )
- self.assertFalse(watch)
- pagure.lib.query.add_group_to_project(
- session=self.session,
- project=project,
- new_group="foo",
- user="pingou",
- )
- self.session.commit()
- group = pagure.lib.query.search_groups(self.session, group_name="foo")
- pagure.lib.query.add_user_to_group(
- self.session,
- username="foo",
- group=group,
- user="pingou",
- is_admin=False,
- )
- self.session.commit()
- group = pagure.lib.query.search_groups(self.session, group_name="foo")
- # If user belongs to any group of that project
- user.username = "foo"
- msg = watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="test"
- )
- self.assertEqual(watch_level, ["issues"])
- # If user is the creator
- user.username = "pingou"
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="test"
- )
- self.assertEqual(watch_level, ["issues"])
- # Entry into watchers table for issues and commits
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project, user="pingou", watch="3"
- )
- self.session.commit()
- self.assertEqual(
- msg,
- "You are now watching issues, PRs, and commits on this project",
- )
- # From watchers table
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="test"
- )
- self.assertEqual(["issues", "commits"], watch_level)
- # Make sure that when a user watches more than one repo explicitly
- # they get the correct watch status
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project2, user="pingou", watch="1"
- )
- self.session.commit()
- self.assertEqual(
- msg, "You are now watching issues and PRs on this project"
- )
- # From watchers table
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="test2"
- )
- self.assertEqual(["issues"], watch_level)
- # Entry into watchers table for just commits
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project, user="pingou", watch="2"
- )
- self.session.commit()
- self.assertEqual(msg, "You are now watching commits on this project")
- # From watchers table
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="test"
- )
- self.assertEqual(["commits"], watch_level)
- # Entry into watchers table for issues
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project, user="pingou", watch="1"
- )
- self.session.commit()
- self.assertEqual(
- msg, "You are now watching issues and PRs on this project"
- )
- # From watchers table
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="test"
- )
- self.assertEqual(["issues"], watch_level)
- # Entry into watchers table for no watching
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project, user="pingou", watch="0"
- )
- self.session.commit()
- self.assertEqual(msg, "You are no longer watching this project")
- # From watchers table
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="test"
- )
- self.assertEqual(watch_level, [])
- # Add a contributor to the project
- item = pagure.lib.model.User(
- user="bar",
- fullname="bar foo",
- password="foo",
- default_email="bar@bar.com",
- )
- self.session.add(item)
- item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
- self.session.add(item)
- msg = pagure.lib.query.add_user_to_project(
- session=self.session,
- project=project,
- new_user="bar",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- # Check if the new contributor is watching
- user.username = "bar"
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session, user=user, repo="test"
- )
- self.assertEqual(watch_level, ["issues"])
- # wrong project
- user.username = "bar"
- watch_level = pagure.lib.query.get_watch_level_on_repo(
- session=self.session,
- user=user,
- repo="test",
- namespace="somenamespace",
- )
- self.assertEqual(watch_level, [])
- def test_user_watch_list(self):
- """ test user watch list method of pagure.lib """
- tests.create_projects(self.session)
- # He should be watching
- user = tests.FakeUser()
- user.username = "pingou"
- watch_list_objs = pagure.lib.query.user_watch_list(
- session=self.session, user="pingou"
- )
- watch_list = [obj.name for obj in watch_list_objs]
- self.assertEqual(watch_list, ["test", "test2", "test3"])
- # Make pingou unwatch the test3 project
- project = pagure.lib.query._get_project(
- self.session, "test3", namespace="somenamespace"
- )
- msg = pagure.lib.query.update_watch_status(
- session=self.session, project=project, user="pingou", watch="0"
- )
- self.session.commit()
- self.assertEqual(msg, "You are no longer watching this project")
- # Re-check the watch list
- watch_list_objs = pagure.lib.query.user_watch_list(
- session=self.session, user="pingou"
- )
- watch_list = [obj.name for obj in watch_list_objs]
- self.assertEqual(watch_list, ["test", "test2"])
- # He isn't in the db, thus not watching anything
- user.username = "vivek"
- watch_list_objs = pagure.lib.query.user_watch_list(
- session=self.session, user="vivek"
- )
- watch_list = [obj.name for obj in watch_list_objs]
- self.assertEqual(watch_list, [])
- # He shouldn't be watching anything
- user.username = "foo"
- watch_list_objs = pagure.lib.query.user_watch_list(
- session=self.session, user="foo"
- )
- watch_list = [obj.name for obj in watch_list_objs]
- self.assertEqual(watch_list, [])
- @patch("flask.request.url", "http://localhost.localdomain/test/issue/69")
- @patch("flask.request.url_root", "http://localhost.localdomain/")
- @patch("flask.request.args.get", return_value=None)
- @patch("flask.request")
- @patch("flask.g")
- @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
- def test_text2markdown(self, g, req, reqget):
- """ Test the text2markdown method in pagure.lib.query. """
- pagure.config.config["TESTING"] = True
- pagure.config.config["SERVER_NAME"] = "localhost.localdomain"
- # This creates:
- # project: test
- # fork: pingou/test
- # PR#1 to project test
- self.test_new_pull_request()
- # create an issue (will be #2) in 'test' project
- repo = pagure.lib.query._get_project(self.session, "test")
- iss = pagure.lib.query.new_issue(
- issue_id=2,
- session=self.session,
- repo=repo,
- title="test issue",
- content="content test issue",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(iss.id, 2)
- self.assertEqual(iss.title, "test issue")
- # create PR#2 to project pingou/test
- forked_repo = pagure.lib.query._get_project(
- self.session, "test", user="pingou"
- )
- req = pagure.lib.query.new_pull_request(
- requestid=2,
- session=self.session,
- repo_from=forked_repo,
- branch_from="master",
- repo_to=forked_repo,
- branch_to="master",
- title="test pull-request in fork",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(req.id, 2)
- self.assertEqual(req.title, "test pull-request in fork")
- # Create the project ns/test
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test3",
- namespace="ns",
- description="test project #1",
- hook_token="aaabbbcccdd",
- )
- item.close_status = [
- "Invalid",
- "Insufficient data",
- "Fixed",
- "Duplicate",
- ]
- self.session.add(item)
- self.session.commit()
- iss = pagure.lib.query.new_issue(
- issue_id=4,
- session=self.session,
- repo=item,
- title="test issue",
- content="content test issue",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(iss.id, 4)
- self.assertEqual(iss.title, "test issue")
- # Fork ns/test to pingou
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test",
- namespace="ns",
- description="Forked namespaced test project #1",
- is_fork=True,
- parent_id=item.id,
- hook_token="aaabbbrrrbb",
- )
- self.session.add(item)
- self.session.commit()
- iss = pagure.lib.query.new_issue(
- issue_id=7,
- session=self.session,
- repo=item,
- title="test issue #7",
- content="content test issue #7 in forked repo",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(iss.id, 7)
- self.assertEqual(iss.title, "test issue #7")
- iss = pagure.lib.query.new_issue(
- issue_id=8,
- session=self.session,
- repo=item,
- title="private issue #8",
- content="Private content test issue #8 in forked repo",
- user="pingou",
- private=True,
- )
- self.session.commit()
- self.assertEqual(iss.id, 8)
- self.assertEqual(iss.title, "private issue #8")
- # newer bleach allow to customize the protocol supported
- import bleach
- bleach_v = bleach.__version__.split(".")
- for idx, val in enumerate(bleach_v):
- try:
- val = int(val)
- except ValueError:
- pass
- bleach_v[idx] = val
- # old markdown generate other html
- import markdown
- try:
- markdown_v = markdown.__version__.version_info
- except AttributeError: # pragma: no cover
- markdown_v = markdown.__version_info__
- # python-markdown >= 3.2.0 returns to the old behavior on img tag trailing slash
- old_markdown = markdown_v < (2, 6, 0) or markdown_v >= (3, 2, 0)
- mk_321 = markdown_v >= (3, 2, 0)
- texts = [
- "foo bar test#1 see?",
- "foo bar pingou/test#2 I mean, really",
- "foo bar fork/pingou/test#2 bouza!",
- "foo bar forks/pingou/test#2 bouza!",
- "foo bar ns/test3#4 bouza!",
- "foo bar fork/user/ns/test#5 bouza!",
- "foo bar fork/pingou/ns/test#7 bouza!",
- "test#1 bazinga!",
- "pingou opened the PR forks/pingou/test#2",
- "fork/pingou/ns/test#8 is private",
- "implicit link to #1",
- "implicit link .#1. with non-whitespace, non-word characters",
- "#2 - implicit link at start of line",
- "#2. implicit link at start of line with no whitespace after",
- "#regular header",
- "#34 looks like an implicit link, but no issue 34",
- "pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd",
- "irc://pagure.io",
- "ircs://pagure.io",
- "http://pagure.io",
- "https://pagure.io",
- "<https://pagure.io/pagure>",
- "~~foo~~",
- "~~foo bar~~",
- "~~[BZ#1435310](https://bugzilla.redhat.com/1435310)~~",
- "~~[BZ#1435310](https://bugzilla.redhat.com/1435310) avc denial "
- "during F26AH boot 'error_name=org.freedesktop.systemd1."
- "NoSuchDynamicUser'~~",
- "``~~foo bar~~``",
- "~~foo bar~~ and ~~another ~~",
- "lets mention @pingou",
- "@pingou at start of line",
- "but not someone@pingou.com",
- "[![Fedora_infinity_small.png]"
- "(/test/issue/raw/Fedora_infinity_small.png)]"
- "(/test/issue/raw/Fedora_infinity_small.png)",
- ]
- expected = [
- # 'foo bar test#1 see?',
- '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/test/pull-request/1"'
- ' title="[Open] test pull-request">test#1</a> see?</p></div>',
- # 'foo bar pingou/test#2 I mean, really', -- unknown namespace
- '<div class="markdown"><p>foo bar pingou/test#2 I mean, really</p></div>',
- # 'foo bar fork/pingou/test#2 bouza!',
- '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/fork/'
- 'pingou/test/pull-request/2" title="[Open] test pull-request in fork">'
- "pingou/test#2</a> bouza!</p></div>",
- # 'foo bar forks/pingou/test#2 bouza!', -- the 's' doesn't matter
- '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/fork/'
- 'pingou/test/pull-request/2" title="[Open] test pull-request in fork">'
- "pingou/test#2</a> bouza!</p></div>",
- # 'foo bar ns/test3#4 bouza!',
- '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/ns/test3/issue/4"'
- ' title="[Open] test issue">ns/test3#4</a> bouza!</p></div>',
- # 'foo bar fork/user/ns/test#5 bouza!', -- unknown fork
- '<div class="markdown"><p>foo bar user/ns/test#5 bouza!</p></div>',
- # 'foo bar fork/pingou/ns/test#7 bouza!',
- '<div class="markdown"><p>foo bar <a href="http://localhost.localdomain/'
- 'fork/pingou/ns/test/issue/7" title="[Open] test issue #7">'
- "pingou/ns/test#7</a> bouza!</p></div>",
- # 'test#1 bazinga!',
- '<div class="markdown"><p><a href="http://localhost.localdomain/test/pull-request/1" '
- 'title="[Open] test pull-request">test#1</a> bazinga!</p></div>',
- # 'pingou opened the PR forks/pingou/test#2'
- '<div class="markdown"><p>pingou opened the PR <a href="http://localhost.localdomain/'
- 'fork/pingou/test/pull-request/2" '
- 'title="[Open] test pull-request in fork">pingou/test#2</a></p></div>',
- # 'fork/pingou/ns/test#8 is private',
- '<div class="markdown"><p><a href="http://localhost.localdomain/fork/pingou/ns/test/issue/8" '
- 'title="Private issue">pingou/ns/test#8</a> is private</p></div>',
- # 'implicit link to #1',
- '<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>',
- # 'implicit link .#1. with non-whitespace, non-word characters',
- '<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>',
- # '#2 - implicit link at start of line',
- '<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>',
- # '#2. implicit link at start of line with no whitespace after',
- '<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>',
- # '#regular header',
- '<div class="markdown"><h1>regular header</h1></div>',
- # '#34 looks like an implicit link, but no issue 34',
- '<div class="markdown"><h1>34 looks like an implicit link, but no issue 34</h1></div>',
- # 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
- '<div class="markdown"><p>pingou committed on <a href="http://localhost.localdomain/'
- 'test/c/9364354a4555ba17aa60f0dc844d70b74eb1aecd" '
- 'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
- ">test#9364354a4555ba17aa60f0dc844d70b74eb1aecd</a></p></div>",
- # 'irc://pagure.io'
- '<div class="markdown"><p><a href="irc://pagure.io">irc://pagure.io</a></p></div>',
- # 'ircs://pagure.io' - This is getting cleaned by python-bleach
- # and the version 1.4.3 that we have won't let us adjust the
- # list of supported protocols
- # '<p><a href="ircs://pagure.io">ircs://pagure.io</a></p>',
- '<div class="markdown"><p><a href="ircs://pagure.io">ircs://pagure.io</a></p></div>'
- if tuple(bleach_v) >= (1, 5, 0)
- else '<div class="markdown"><p><a>ircs://pagure.io</a></p></div>',
- # 'http://pagure.io'
- '<div class="markdown"><p><a href="http://pagure.io">http://pagure.io</a></p></div>',
- # 'https://pagure.io'
- '<div class="markdown"><p><a href="https://pagure.io">https://pagure.io</a></p></div>',
- # '<https://pagure.io/pagure>'
- '<div class="markdown"><p><a href="https://pagure.io/pagure">https://pagure.io/pagure'
- "</a></p></div>",
- # '~~foo~~'
- '<div class="markdown"><p><del>foo</del></p></div>',
- # '~~foo bar~~'
- '<div class="markdown"><p><del>foo bar</del></p></div>',
- # '~~[BZ#1435310](https://bugzilla.redhat.com/1435310)~~'
- '<div class="markdown"><p><del><a href="https://bugzilla.redhat.com/1435310">'
- "BZ#1435310</a></del></p></div>",
- # '~~[BZ#1435310](https://bugzilla.redhat.com/1435310) avc
- # denial during F26AH boot 'error_name=org.freedesktop.systemd1
- # .NoSuchDynamicUser~~'
- '<div class="markdown"><p><del><a href="https://bugzilla.redhat.com/1435310">'
- "BZ#1435310</a> avc denial during F26AH boot 'error_name="
- "org.freedesktop.systemd1.NoSuchDynamicUser'</del></p></div>",
- # '``~~foo bar~~``'
- '<div class="markdown"><p><code>~~foo bar~~</code></p></div>',
- # '~~foo bar~~ and ~~another ~~',
- '<div class="markdown"><p><del>foo bar</del> and <del>another </del></p></div>',
- # 'lets mention @pingou',
- '<div class="markdown"><p>lets mention <a href="http://localhost.localdomain/user/pingou">@pingou</a></p></div>',
- # '@pingou at start of line',
- '<div class="markdown"><p><a href="http://localhost.localdomain/user/pingou">@pingou</a> at start of line</p></div>',
- # 'but not someone@pingou.com',
- '<div class="markdown"><p>but not someone@pingou.com</p></div>',
- ]
- if mk_321:
- print("**** Markdown 3.2.1+ behavior")
- expected.append(
- # '[![Fedora_infinity_small.png]'
- # '(/test/issue/raw/Fedora_infinity_small.png)]'
- # '(/test/issue/raw/Fedora_infinity_small.png)',
- '<div class="markdown"><p><a href="/test/issue/raw/Fedora_infinity_small.png"><span>'
- '<img alt="Fedora_infinity_small.png" class="lazyload" '
- 'data-src="/test/issue/raw/Fedora_infinity_small.png" src="">'
- "<noscript>"
- '<img alt="Fedora_infinity_small.png" '
- 'src="/test/issue/raw/Fedora_infinity_small.png" />'
- "</noscript></span></a></p></div>"
- )
- elif old_markdown:
- print("**** Old markdown behavior")
- expected.append(
- # '[![Fedora_infinity_small.png]'
- # '(/test/issue/raw/Fedora_infinity_small.png)]'
- # '(/test/issue/raw/Fedora_infinity_small.png)',
- '<div class="markdown"><p><a href="/test/issue/raw/Fedora_infinity_small.png"><span>'
- '<img alt="Fedora_infinity_small.png" class="lazyload" '
- 'data-src="/test/issue/raw/Fedora_infinity_small.png" src="">'
- "<noscript>"
- '<img alt="Fedora_infinity_small.png" '
- 'src="/test/issue/raw/Fedora_infinity_small.png" />'
- "</noscript></span></a></p></div>"
- )
- else:
- print("**** Not old but no longer new markdown")
- expected.append(
- # '[![Fedora_infinity_small.png]'
- # '(/test/issue/raw/Fedora_infinity_small.png)]'
- # '(/test/issue/raw/Fedora_infinity_small.png)',
- '<div class="markdown"><p><a href="/test/issue/raw/Fedora_infinity_small.png"><span>'
- '<img alt="Fedora_infinity_small.png" class="lazyload" '
- 'data-src="/test/issue/raw/Fedora_infinity_small.png" src="">'
- "<noscript>"
- '<img alt="Fedora_infinity_small.png" '
- 'src="/test/issue/raw/Fedora_infinity_small.png">'
- "</noscript></span></a></p></div>"
- )
- with self.app.application.app_context():
- g.session = self.session
- for idx, text in enumerate(texts):
- html = pagure.lib.query.text2markdown(text)
- self.assertEqual(html, expected[idx])
- def test_text2markdown_exception(self):
- """ Test the text2markdown method in pagure.lib.query. """
- text = "test#1 bazinga!"
- expected_html = "test#1 bazinga!"
- html = pagure.lib.query.text2markdown(text)
- self.assertEqual(html, expected_html)
- def test_text2markdown_empty_string(self):
- """ Test the text2markdown method in pagure.lib.query. """
- text = ""
- expected_html = ""
- html = pagure.lib.query.text2markdown(text)
- self.assertEqual(html, expected_html)
- def test_get_access_levels(self):
- """ Test the get_access_levels method in pagure.lib """
- acls = pagure.lib.query.get_access_levels(self.session)
- self.assertEqual(sorted(["admin", "commit", "ticket"]), sorted(acls))
- def test_get_project_users(self):
- """ Test the get_project_users method when combine is True
- """
- tests.create_projects(self.session)
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- # Default value of combine is True
- # which means the an admin is a user, committer as well
- # and a committer is also a user
- # and a user is just a user
- users = project.get_project_users(access="admin")
- # Only pingou is the admin as of now
- # But, he is the creator and
- # the creator of the project is not listed in user_projects
- # table. Thus, get_projec_users won't return him as an admin
- # He has all the access of an admin though
- self.assertEqual(len(users), 0)
- self.assertEqual(project.user.username, "pingou")
- # Wrong access level, should raise Accesslevelnotfound exception
- self.assertRaises(
- pagure.exceptions.AccessLevelNotFound,
- project.get_project_users,
- access="owner",
- )
- # Let's add a new user to the project, 'foo'
- # By default, if no access is specified, he becomes an admin
- msg = pagure.lib.query.add_user_to_project(
- self.session, project=project, new_user="foo", user="pingou"
- )
- self.session.commit()
- # since, he is an admin, the msg should be 'User added'
- self.assertEqual(msg, "User added")
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- users = project.get_project_users(access="admin")
- self.assertEqual(len(users), 1)
- self.assertEqual(users[0].username, "foo")
- # foo should be a committer as well, since he is an admin
- users = project.get_project_users(access="commit")
- self.assertEqual(len(users), 1)
- self.assertEqual(users[0].username, "foo")
- # the admin also has ticket access
- users = project.get_project_users(access="ticket")
- self.assertEqual(len(users), 1)
- self.assertEqual(users[0].username, "foo")
- # let's update the access of foo to 'committer'
- msg = pagure.lib.query.add_user_to_project(
- self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="commit",
- )
- self.session.commit()
- self.assertEqual(msg, "User access updated")
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- # No admin now, even though pingou the creator is there
- users = project.get_project_users(access="admin")
- self.assertEqual(len(users), 0)
- users = project.get_project_users(access="commit")
- # foo is the committer currently
- self.assertEqual(len(users), 1)
- self.assertEqual(users[0].username, "foo")
- users = project.get_project_users(access="ticket")
- # foo also has ticket rights
- self.assertEqual(len(users), 1)
- self.assertEqual(users[0].username, "foo")
- # let's update the access of foo to 'ticket'
- msg = pagure.lib.query.add_user_to_project(
- self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="ticket",
- )
- self.session.commit()
- self.assertEqual(msg, "User access updated")
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- # No admin now, even though pingou the creator is there
- users = project.get_project_users(access="admin")
- self.assertEqual(len(users), 0)
- users = project.get_project_users(access="commit")
- # foo deosn't have commit rights now
- self.assertEqual(len(users), 0)
- users = project.get_project_users(access="ticket")
- # foo does have tickets right though
- self.assertEqual(len(users), 1)
- self.assertEqual(users[0].username, "foo")
- def test_get_project_users_combine_false(self):
- """ Test the get_project_users method when combine is False
- """
- tests.create_projects(self.session)
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- # Let's add a new user to the project, 'foo'
- # By default, if no access is specified, he becomes an admin
- msg = pagure.lib.query.add_user_to_project(
- self.session, project=project, new_user="foo", user="pingou"
- )
- self.session.commit()
- # since, he is an admin, the msg should be 'User added'
- self.assertEqual(msg, "User added")
- # only one admin
- users = project.get_project_users(access="admin", combine=False)
- self.assertEqual(len(users), 1)
- self.assertEqual(users[0].username, "foo")
- # No user with only commit access
- users = project.get_project_users(access="commit", combine=False)
- self.assertEqual(len(users), 0)
- # No user with only ticket access
- users = project.get_project_users(access="ticket", combine=False)
- self.assertEqual(len(users), 0)
- # Update the access level of foo user to commit
- msg = pagure.lib.query.add_user_to_project(
- self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="commit",
- )
- self.session.commit()
- self.assertEqual(msg, "User access updated")
- # He is just a committer
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- users = project.get_project_users(access="admin", combine=False)
- self.assertEqual(len(users), 0)
- # He is just a committer
- users = project.get_project_users(access="commit", combine=False)
- self.assertEqual(len(users), 1)
- self.assertEqual(users[0].username, "foo")
- # He is just a committer
- users = project.get_project_users(access="ticket", combine=False)
- self.assertEqual(len(users), 0)
- # Update the access level of foo user to ticket
- msg = pagure.lib.query.add_user_to_project(
- self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="ticket",
- )
- self.session.commit()
- self.assertEqual(msg, "User access updated")
- # He is just a ticketer
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- users = project.get_project_users(access="admin", combine=False)
- self.assertEqual(len(users), 0)
- # He is just a ticketer
- users = project.get_project_users(access="commit", combine=False)
- self.assertEqual(len(users), 0)
- # He is just a ticketer
- users = project.get_project_users(access="ticket", combine=False)
- self.assertEqual(len(users), 1)
- self.assertEqual(users[0].username, "foo")
- def test_get_project_groups(self):
- """ Test the get_project_groups method when combine is True
- """
- # Create some projects
- tests.create_projects(self.session)
- # Create a group in database
- msg = pagure.lib.query.add_group(
- self.session,
- group_name="JL",
- display_name="Justice League",
- description="Nope, it's not JLA anymore",
- group_type="user",
- user="foo",
- is_admin=False,
- blacklist=pagure.config.config.get("BLACKLISTED_PROJECTS"),
- )
- self.assertEqual(msg, "User `foo` added to the group `JL`.")
- # Add the group to project we just created, test
- # First add it as an admin
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- msg = pagure.lib.query.add_group_to_project(
- self.session, project=project, new_group="JL", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- # Now, the group is an admin in the project
- # so, it must have access to everything
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- groups = project.get_project_groups(access="admin")
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].display_name, "Justice League")
- self.assertEqual(len(project.admin_groups), 1)
- self.assertEqual(
- project.admin_groups[0].display_name, "Justice League"
- )
- # The group should be committer as well
- groups = project.get_project_groups(access="commit")
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].display_name, "Justice League")
- self.assertEqual(len(project.committer_groups), 1)
- self.assertEqual(
- project.committer_groups[0].display_name, "Justice League"
- )
- # The group should be ticketer as well
- groups = project.get_project_groups(access="ticket")
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].display_name, "Justice League")
- self.assertEqual(len(project.groups), 1)
- self.assertEqual(project.groups[0].display_name, "Justice League")
- # Update the access level of the group, JL to commit
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- msg = pagure.lib.query.add_group_to_project(
- self.session,
- project=project,
- new_group="JL",
- user="pingou",
- access="commit",
- )
- self.session.commit()
- self.assertEqual(msg, "Group access updated")
- # It shouldn't be an admin
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- groups = project.get_project_groups(access="admin")
- self.assertEqual(len(groups), 0)
- self.assertEqual(len(project.admin_groups), 0)
- # It is a committer
- groups = project.get_project_groups(access="commit")
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].display_name, "Justice League")
- self.assertEqual(len(project.committer_groups), 1)
- self.assertEqual(
- project.committer_groups[0].display_name, "Justice League"
- )
- # The group should be ticketer as well
- groups = project.get_project_groups(access="ticket")
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].display_name, "Justice League")
- self.assertEqual(len(project.groups), 1)
- self.assertEqual(project.groups[0].display_name, "Justice League")
- # Update the access of group JL to ticket
- msg = pagure.lib.query.add_group_to_project(
- self.session,
- project=project,
- new_group="JL",
- user="pingou",
- access="ticket",
- )
- self.session.commit()
- self.assertEqual(msg, "Group access updated")
- # It is not an admin
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- groups = project.get_project_groups(access="admin")
- self.assertEqual(len(groups), 0)
- self.assertEqual(len(project.admin_groups), 0)
- # The group shouldn't be a committer
- groups = project.get_project_groups(access="commit")
- self.assertEqual(len(groups), 0)
- self.assertEqual(len(project.committer_groups), 0)
- # The group should be ticketer
- groups = project.get_project_groups(access="ticket")
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].display_name, "Justice League")
- self.assertEqual(len(project.groups), 1)
- self.assertEqual(project.groups[0].display_name, "Justice League")
- def test_get_project_groups_combine_false(self):
- """ Test the get_project_groups method when combine is False
- """
- # Create some projects
- tests.create_projects(self.session)
- # Create a group in database
- msg = pagure.lib.query.add_group(
- self.session,
- group_name="JL",
- display_name="Justice League",
- description="Nope, it's not JLA anymore",
- group_type="user",
- user="foo",
- is_admin=False,
- blacklist=pagure.config.config.get("BLACKLISTED_PROJECTS"),
- )
- self.assertEqual(msg, "User `foo` added to the group `JL`.")
- # Add the group to project we just created, test
- # First add it as an admin
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- msg = pagure.lib.query.add_group_to_project(
- self.session, project=project, new_group="JL", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- # Now, the group is an admin in the project
- # so, it must have access to everything
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- groups = project.get_project_groups(access="admin", combine=False)
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].display_name, "Justice League")
- self.assertEqual(len(project.admin_groups), 1)
- self.assertEqual(
- project.admin_groups[0].display_name, "Justice League"
- )
- # The group shoudn't be a committer
- groups = project.get_project_groups(access="commit", combine=False)
- self.assertEqual(len(groups), 0)
- # The group shoudn't be a ticketer
- groups = project.get_project_groups(access="ticket", combine=False)
- self.assertEqual(len(groups), 0)
- # Update the access level of the group, JL to commit
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- msg = pagure.lib.query.add_group_to_project(
- self.session,
- project=project,
- new_group="JL",
- user="pingou",
- access="commit",
- )
- self.session.commit()
- self.assertEqual(msg, "Group access updated")
- # It shouldn't be an admin
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- groups = project.get_project_groups(access="admin", combine=False)
- self.assertEqual(len(groups), 0)
- # It is a committer
- groups = project.get_project_groups(access="commit", combine=False)
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].display_name, "Justice League")
- self.assertEqual(len(project.committer_groups), 1)
- self.assertEqual(
- project.committer_groups[0].display_name, "Justice League"
- )
- # The group shouldn't be ticketer
- groups = project.get_project_groups(access="ticket", combine=False)
- self.assertEqual(len(groups), 0)
- # Update the access of group JL to ticket
- msg = pagure.lib.query.add_group_to_project(
- self.session,
- project=project,
- new_group="JL",
- user="pingou",
- access="ticket",
- )
- self.session.commit()
- self.assertEqual(msg, "Group access updated")
- # It is not an admin
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- groups = project.get_project_groups(access="admin", combine=False)
- self.assertEqual(len(groups), 0)
- # The group shouldn't be a committer
- groups = project.get_project_groups(access="commit", combine=False)
- self.assertEqual(len(groups), 0)
- # The group should be ticketer
- groups = project.get_project_groups(access="ticket", combine=False)
- self.assertEqual(len(groups), 1)
- self.assertEqual(groups[0].display_name, "Justice League")
- self.assertEqual(len(project.groups), 1)
- self.assertEqual(project.groups[0].display_name, "Justice League")
- def test_get_obj_access_user(self):
- """ Test the get_obj_access method of pagure.lib
- for model.User object """
- # Create the projects
- tests.create_projects(self.session)
- # Add a user object - make him an admin first
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- msg = pagure.lib.query.add_user_to_project(
- self.session, project=project, new_user="foo", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "User added")
- user = pagure.lib.query.get_user(self.session, key="foo")
- # He should be an admin
- access_obj = pagure.lib.query.get_obj_access(
- self.session, project_obj=project, obj=user
- )
- self.assertEqual(access_obj.access, "admin")
- # Update and check for commit access
- msg = pagure.lib.query.add_user_to_project(
- self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="commit",
- )
- self.session.commit()
- self.assertEqual(msg, "User access updated")
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- # He should be a committer
- access_obj = pagure.lib.query.get_obj_access(
- self.session, project_obj=project, obj=user
- )
- self.assertEqual(access_obj.access, "commit")
- # Update and check for ticket access
- msg = pagure.lib.query.add_user_to_project(
- self.session,
- project=project,
- new_user="foo",
- user="pingou",
- access="ticket",
- )
- self.session.commit()
- self.assertEqual(msg, "User access updated")
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- # He should be a ticketer
- access_obj = pagure.lib.query.get_obj_access(
- self.session, project_obj=project, obj=user
- )
- self.assertEqual(access_obj.access, "ticket")
- def test_get_obj_access_group(self):
- """ Test the get_obj_access method of pagure.lib
- for model.PagureGroup object """
- # Create the projects
- tests.create_projects(self.session)
- # Create a group in database
- msg = pagure.lib.query.add_group(
- self.session,
- group_name="JL",
- display_name="Justice League",
- description="Nope, it's not JLA anymore",
- group_type="user",
- user="foo",
- is_admin=False,
- blacklist=pagure.config.config.get("BLACKLISTED_PROJECTS"),
- )
- self.assertEqual(msg, "User `foo` added to the group `JL`.")
- # Add a group object - make him an admin first
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- msg = pagure.lib.query.add_group_to_project(
- self.session, project=project, new_group="JL", user="pingou"
- )
- self.session.commit()
- self.assertEqual(msg, "Group added")
- group = pagure.lib.query.search_groups(self.session, group_name="JL")
- # He should be an admin
- access_obj = pagure.lib.query.get_obj_access(
- self.session, project_obj=project, obj=group
- )
- self.assertEqual(access_obj.access, "admin")
- # Update and check for commit access
- msg = pagure.lib.query.add_group_to_project(
- self.session,
- project=project,
- new_group="JL",
- user="pingou",
- access="commit",
- )
- self.session.commit()
- self.assertEqual(msg, "Group access updated")
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- # He should be a committer
- access_obj = pagure.lib.query.get_obj_access(
- self.session, project_obj=project, obj=group
- )
- self.assertEqual(access_obj.access, "commit")
- # Update and check for ticket access
- msg = pagure.lib.query.add_group_to_project(
- self.session,
- project=project,
- new_group="JL",
- user="pingou",
- access="ticket",
- )
- self.session.commit()
- self.assertEqual(msg, "Group access updated")
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- # He should be a ticketer
- access_obj = pagure.lib.query.get_obj_access(
- self.session, project_obj=project, obj=group
- )
- self.assertEqual(access_obj.access, "ticket")
- def test_set_watch_obj(self):
- """ Test the set_watch_obj method in pagure.lib """
- # Create the project ns/test
- item = pagure.lib.model.Project(
- user_id=1, # pingou
- name="test3",
- namespace="ns",
- description="test project #1",
- hook_token="aaabbbcccdd",
- )
- item.close_status = ["Invalid", "Insufficient data", "Fixed"]
- self.session.add(item)
- self.session.commit()
- # Create the ticket
- iss = pagure.lib.query.new_issue(
- issue_id=4,
- session=self.session,
- repo=item,
- title="test issue",
- content="content test issue",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(iss.id, 4)
- self.assertEqual(iss.title, "test issue")
- # Unknown user
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.set_watch_obj,
- self.session,
- "unknown",
- iss,
- True,
- )
- # Invalid object to watch - project
- self.assertRaises(
- pagure.exceptions.InvalidObjectException,
- pagure.lib.query.set_watch_obj,
- self.session,
- "foo",
- iss.project,
- True,
- )
- # Invalid object to watch - string
- self.assertRaises(
- AttributeError,
- pagure.lib.query.set_watch_obj,
- self.session,
- "foo",
- "ticket",
- True,
- )
- # Watch the ticket
- out = pagure.lib.query.set_watch_obj(self.session, "foo", iss, True)
- self.assertEqual(out, "You are now watching this issue")
- # Un-watch the ticket
- out = pagure.lib.query.set_watch_obj(self.session, "foo", iss, False)
- self.assertEqual(out, "You are no longer watching this issue")
- def test_tokenize_search_string(self):
- """ Test the tokenize_search_string function. """
- # These are the tests performed to make sure we tokenize correctly.
- # This is in the form: input string, custom fields, remaining pattern
- tests = [
- ("test123", {}, "test123"),
- ("test:key test123", {"test": "key"}, "test123"),
- (
- 'test:"key with spaces" test123',
- {"test": "key with spaces"},
- "test123",
- ),
- ("test123 test:key test456", {"test": "key"}, "test123 test456"),
- (
- 'test123 test:"key with spaces" key2:value12 test456',
- {"test": "key with spaces", "key2": "value12"},
- "test123 test456",
- ),
- ]
- for inp, flds, rem in tests:
- self.assertEqual(
- pagure.lib.query.tokenize_search_string(inp), (flds, rem)
- )
- def test_save_report(self):
- """ Test the save_report function. """
- # Create the projects
- tests.create_projects(self.session)
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- self.assertEqual(project.reports, {})
- name = "test report"
- url = "?foo=bar&baz=biz"
- pagure.lib.query.save_report(
- self.session, repo=project, name=name, url=url, username=None
- )
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- self.assertEqual(
- project.reports, {"test report": {"baz": "biz", "foo": "bar"}}
- )
- name = "test report #2"
- url = "?foo=bar&foo=none&foo=baz"
- pagure.lib.query.save_report(
- self.session, repo=project, name=name, url=url, username=None
- )
- project = pagure.lib.query.get_authorized_project(
- self.session, project_name="test"
- )
- self.assertEqual(
- project.reports,
- {
- "test report": {"baz": "biz", "foo": "bar"},
- "test report #2": {"foo": ["bar", "none", "baz"]},
- },
- )
- def test_text2markdown_table(self):
- """ Test the text2markdown function with a markdown table. """
- try:
- md_version = markdown.__version__.version_info
- except AttributeError:
- md_version = markdown.__version_info__
- if md_version < (2, 6, 7):
- raise unittest.case.SkipTest(
- "Skipping on old markdown that do not strip the orientation row"
- )
- text = """
- | Left-aligned | Center-aligned | Right-aligned |
- | :--- | :---: | ---: |
- | git status | git status | git status |
- | git diff | git diff | git diff |
- foo bar
- """
- expected = """<div class="markdown"><table>
- <thead>
- <tr>
- <th align="left">Left-aligned</th>
- <th align="center">Center-aligned</th>
- <th align="right">Right-aligned</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td align="left">git status</td>
- <td align="center">git status</td>
- <td align="right">git status</td>
- </tr>
- <tr>
- <td align="left">git diff</td>
- <td align="center">git diff</td>
- <td align="right">git diff</td>
- </tr>
- </tbody>
- </table>
- <p>foo bar</p></div>"""
- with self.app.application.app_context():
- html = pagure.lib.query.text2markdown(text)
- self.assertEqual(html, expected)
- def test_text2markdown_table_old_mk(self):
- """ Test the text2markdown function with a markdown table using the old
- format where the orientation instruction are provided next to the column
- delimiter unlike what can be done with more recent version of markdown.
- """
- text = """
- | Left-aligned | Center-aligned | Right-aligned |
- |:--- |:--------------:| ---:|
- | git status | git status | git status |
- | git diff | git diff | git diff |
- foo bar
- """
- expected = """<div class="markdown"><table>
- <thead>
- <tr>
- <th align="left">Left-aligned</th>
- <th align="center">Center-aligned</th>
- <th align="right">Right-aligned</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td align="left">git status</td>
- <td align="center">git status</td>
- <td align="right">git status</td>
- </tr>
- <tr>
- <td align="left">git diff</td>
- <td align="center">git diff</td>
- <td align="right">git diff</td>
- </tr>
- </tbody>
- </table>
- <p>foo bar</p></div>"""
- with self.app.application.app_context():
- html = pagure.lib.query.text2markdown(text)
- self.assertEqual(html, expected)
- def test_set_redis(self):
- """ Test the set_redis function of pagure.lib.query. """
- self.assertIsNone(pagure.lib.query.REDIS)
- pagure.lib.query.set_redis("0.0.0.0", 6379, 0)
- self.assertIsNotNone(pagure.lib.query.REDIS)
- def test_set_pagure_ci(self):
- """ Test the set_pagure_ci function of pagure.lib.query. """
- # self.assertIn(pagure.lib.query.PAGURE_CI, [None, ['jenkins']])
- pagure.lib.query.set_pagure_ci(True)
- self.assertIsNotNone(pagure.lib.query.PAGURE_CI)
- self.assertTrue(pagure.lib.query.PAGURE_CI)
- def test_get_user_invalid_user(self):
- """ Test the get_user function of pagure.lib.query. """
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.get_user,
- self.session,
- "unknown",
- )
- def test_get_user_username(self):
- """ Test the get_user function of pagure.lib.query. """
- user = pagure.lib.query.get_user(self.session, "foo")
- self.assertEqual(user.username, "foo")
- def test_get_user_email(self):
- """ Test the get_user function of pagure.lib.query. """
- user = pagure.lib.query.get_user(self.session, "bar@pingou.com")
- self.assertEqual(user.username, "pingou")
- def test_is_valid_ssh_key_empty(self):
- """ Test the is_valid_ssh_key function of pagure.lib.query. """
- self.assertIsNone(pagure.lib.query.is_valid_ssh_key(""))
- def test_is_valid_ssh_key_invalid(self):
- """ Test the is_valid_ssh_key function of pagure.lib.query. """
- self.assertEqual(pagure.lib.query.is_valid_ssh_key("nonsense"), False)
- def test_is_valid_ssh_key(self):
- """ Test the is_valid_ssh_key function of pagure.lib.query. """
- keyinfo = pagure.lib.query.is_valid_ssh_key(
- """ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDtgzSO9d1IrKdmyBFUvtAJPLgGOhp0lSySkWRSe+/+3KXYjSnsLnCJQlO5M7JfaXhtTHEow86rh4W9+FoJdzo5iocAwH5xPZ5ttHLy7VHgTzNMUeMgKpjy6bBOdPoGPPG4mo7QCMCRJdWBRDv4OSEMLU5jQAvC272YK2V8L918VQ== root@test"""
- )
- keys = keyinfo.split("\n")
- self.assertEqual(len(keys), 2)
- self.assertEqual(keys[1], "")
- key = keys[0].split(" ")
- self.assertEqual(key[0], "1024")
- # We should always get the SHA256 version
- self.assertEqual(
- key[1], "SHA256:ztcRXIq7y/HNfwwscrexCyDF46/Pn/oHTkBZx87GwI8"
- )
- self.assertEqual(key[3], "(RSA)")
- def test_create_deploykeys_ssh_keys_on_disk_empty(self):
- """ Test the create_deploykeys_ssh_keys_on_disk function of
- pagure.lib.query. """
- self.assertIsNone(
- pagure.lib.query.create_deploykeys_ssh_keys_on_disk(None, None)
- )
- self.assertFalse(
- os.path.exists(os.path.join(self.path, "deploykeys", "test"))
- )
- def test_create_deploykeys_ssh_keys_on_disk_nokey(self):
- """ Test the create_deploykeys_ssh_keys_on_disk function of
- pagure.lib.query. """
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- self.assertIsNone(
- pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
- project, self.path
- )
- )
- self.assertTrue(
- os.path.exists(os.path.join(self.path, "deploykeys", "test"))
- )
- self.assertEqual(
- os.listdir(os.path.join(self.path, "deploykeys", "test")), []
- )
- @patch(
- "pagure.lib.query.is_valid_ssh_key", MagicMock(return_value="foo bar")
- )
- def test_create_deploykeys_ssh_keys_on_disk(self):
- """ Test the create_deploykeys_ssh_keys_on_disk function of
- pagure.lib.query. """
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- # Add a deploy key to the project
- pingou = pagure.lib.query.get_user(self.session, "pingou")
- msg = pagure.lib.query.add_sshkey_to_project_or_user(
- self.session,
- project=project,
- ssh_key="foo bar",
- pushaccess=False,
- creator=pingou,
- )
- self.assertEqual(msg, "SSH key added")
- self.assertIsNone(
- pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
- project, self.path
- )
- )
- self.assertTrue(
- os.path.exists(os.path.join(self.path, "deploykeys", "test"))
- )
- self.assertEqual(
- os.listdir(os.path.join(self.path, "deploykeys", "test")),
- ["deploykey_test_1.pub"],
- )
- # Remove the deploykey
- project = pagure.lib.query._get_project(self.session, "test")
- self.session.delete(project.deploykeys[0])
- self.session.commit()
- # Remove the file on disk
- self.assertIsNone(
- pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
- project, self.path
- )
- )
- self.assertTrue(
- os.path.exists(os.path.join(self.path, "deploykeys", "test"))
- )
- self.assertEqual(
- os.listdir(os.path.join(self.path, "deploykeys", "test")), []
- )
- @patch(
- "pagure.lib.query.is_valid_ssh_key",
- MagicMock(return_value="\nfoo bar"),
- )
- def test_create_deploykeys_ssh_keys_on_disk_empty_first_key(self):
- """ Test the create_deploykeys_ssh_keys_on_disk function of
- pagure.lib.query. """
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- # Add a deploy key to the project
- new_key_obj = pagure.lib.model.SSHKey(
- project_id=project.id,
- pushaccess=False,
- public_ssh_key="\n foo bar",
- ssh_short_key="\n foo bar",
- ssh_search_key="\n foo bar",
- creator_user_id=1, # pingou
- )
- self.session.add(new_key_obj)
- self.session.commit()
- self.assertIsNone(
- pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
- project, self.path
- )
- )
- self.assertTrue(
- os.path.exists(os.path.join(self.path, "deploykeys", "test"))
- )
- self.assertEqual(
- os.listdir(os.path.join(self.path, "deploykeys", "test")), []
- )
- def test_create_deploykeys_ssh_keys_on_disk_invalid(self):
- """ Test the create_deploykeys_ssh_keys_on_disk function of
- pagure.lib.query. """
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- # Add a deploy key to the project
- new_key_obj = pagure.lib.model.SSHKey(
- project_id=project.id,
- pushaccess=False,
- public_ssh_key="foo bar",
- ssh_short_key="foo bar",
- ssh_search_key="foo bar",
- creator_user_id=1, # pingou
- )
- self.session.add(new_key_obj)
- self.session.commit()
- self.assertIsNone(
- pagure.lib.query.create_deploykeys_ssh_keys_on_disk(
- project, self.path
- )
- )
- self.assertTrue(
- os.path.exists(os.path.join(self.path, "deploykeys", "test"))
- )
- self.assertEqual(
- os.listdir(os.path.join(self.path, "deploykeys", "test")), []
- )
- def test_create_user_ssh_keys_on_disk_none(self):
- """ Test the create_user_ssh_keys_on_disk function of pagure.lib.query. """
- self.assertIsNone(
- pagure.lib.query.create_user_ssh_keys_on_disk(None, None)
- )
- def test_update_user_settings_invalid_user(self):
- """ Test the update_user_settings function of pagure.lib.query. """
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.update_user_settings,
- session=self.session,
- settings={},
- user="invalid",
- )
- def test_update_user_settings_no_change(self):
- """ Test the update_user_settings function of pagure.lib.query. """
- # First update the setting
- msg = pagure.lib.query.update_user_settings(
- session=self.session,
- settings={"cc_me_to_my_actions": True},
- user="pingou",
- )
- self.assertEqual(msg, "Successfully edited your settings")
- # Then change it back to its default
- msg = pagure.lib.query.update_user_settings(
- session=self.session, settings={}, user="pingou"
- )
- self.assertEqual(msg, "Successfully edited your settings")
- def test_update_user_settings_no_data(self):
- """ Test the update_user_settings function of pagure.lib.query. """
- msg = pagure.lib.query.update_user_settings(
- session=self.session,
- settings={"cc_me_to_my_actions": False},
- user="pingou",
- )
- self.assertEqual(msg, "No settings to change")
- def test_update_user_settings(self):
- """ Test the update_user_settings function of pagure.lib.query. """
- msg = pagure.lib.query.update_user_settings(
- session=self.session,
- settings={"cc_me_to_my_actions": True},
- user="pingou",
- )
- self.assertEqual(msg, "Successfully edited your settings")
- @patch.dict(
- pagure.config.config,
- {"ALLOWED_EMAIL_DOMAINS": ["pingoured.fr"]},
- clear=True,
- )
- def test_add_email_to_user_with_logs(self):
- """ Test the add_email_to_user function of pagure.lib when there
- are log entries associated to the email added.
- """
- user = pagure.lib.query.search_user(self.session, username="pingou")
- # Add a couple of log entries associated with the new email
- for i in range(3):
- log = pagure.lib.model.PagureLog(
- user_email="new_email@pingoured.fr",
- log_type="commit",
- ref_id=i,
- )
- self.session.add(log)
- self.session.commit()
- # Check emails before
- self.assertEqual(len(user.emails), 2)
- # Add the new_email to the user
- pagure.lib.query.add_email_to_user(
- self.session, user, "new_email@pingoured.fr"
- )
- self.session.commit()
- # Check emails after
- self.assertEqual(len(user.emails), 3)
- # add another email address that is not in an allowed domain
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_email_to_user,
- session=self.session,
- user=user,
- user_email="foo@bar.com",
- )
- self.session.commit()
- # Check emails after
- self.assertEqual(len(user.emails), 3)
- # redo tests with 2 allowed domains
- pagure.config.config["ALLOWED_EMAIL_DOMAINS"] = [
- "pingoured.fr",
- "example.com",
- ]
- # Add the new_email to the user
- pagure.lib.query.add_email_to_user(
- self.session, user, "another_email@example.com"
- )
- self.session.commit()
- # Check emails after
- self.assertEqual(len(user.emails), 4)
- # add another email address that is not in an allowed domain
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_email_to_user,
- session=self.session,
- user=user,
- user_email="another_foo@bar.com",
- )
- self.session.commit()
- # Check emails after
- self.assertEqual(len(user.emails), 4)
- @patch(
- "pagure.lib.query.is_valid_ssh_key", MagicMock(return_value="foo bar")
- )
- def test_update_user_ssh_valid_key(self):
- """ Test the update_user_ssh function of pagure.lib.query. """
- pagure.SESSION = self.session
- pagure.lib.query.update_user_ssh(
- self.session, user="pingou", ssh_key="foo key", keydir=self.path
- )
- self.session.commit()
- self.assertTrue(os.path.exists(os.path.join(self.path, "keys_0")))
- self.assertEqual(
- os.listdir(os.path.join(self.path, "keys_0")), ["pingou.pub"]
- )
- def test_add_user_pending_email_existing_email(self):
- """ Test the add_user_pending_email function of pagure.lib.query. """
- user = pagure.lib.query.search_user(self.session, username="pingou")
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_user_pending_email,
- session=self.session,
- userobj=user,
- email="foo@bar.com",
- )
- @patch("pagure.lib.notify.notify_new_email", MagicMock(return_value=True))
- @patch.dict(
- pagure.config.config,
- {"ALLOWED_EMAIL_DOMAINS": ["pingoured.fr"]},
- clear=True,
- )
- def test_add_user_pending_email(self):
- """ Test the add_user_pending_email function of pagure.lib.query. """
- user = pagure.lib.query.search_user(self.session, username="pingou")
- self.assertEqual(len(user.emails), 2)
- self.assertEqual(len(user.emails_pending), 0)
- pagure.lib.query.add_user_pending_email(
- session=self.session, userobj=user, email="new_mail@pingoured.fr"
- )
- self.session.commit()
- self.assertEqual(len(user.emails), 2)
- self.assertEqual(len(user.emails_pending), 1)
- # add another email address that is not allowed
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.add_user_pending_email,
- session=self.session,
- userobj=user,
- email="foo@bar.com",
- )
- self.session.commit()
- self.assertEqual(len(user.emails), 2)
- self.assertEqual(len(user.emails_pending), 1)
- def test_resend_pending_email_someone_else_email(self):
- """ Test the resend_pending_email function of pagure.lib.query. """
- user = pagure.lib.query.search_user(self.session, username="pingou")
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.resend_pending_email,
- session=self.session,
- userobj=user,
- email="foo@bar.com",
- )
- def test_resend_pending_email_email_validated(self):
- """ Test the resend_pending_email function of pagure.lib.query. """
- user = pagure.lib.query.search_user(self.session, username="pingou")
- self.assertRaises(
- pagure.exceptions.PagureException,
- pagure.lib.query.resend_pending_email,
- session=self.session,
- userobj=user,
- email="foo@pingou.com",
- )
- def test_get_acls(self):
- """ Test the get_acls function of pagure.lib.query. """
- acls = pagure.lib.query.get_acls(self.session)
- self.assertEqual(
- sorted([a.name for a in acls]),
- [
- "commit",
- "commit_flag",
- "create_branch",
- "create_project",
- "fork_project",
- "generate_acls_project",
- "internal_access",
- "issue_assign",
- "issue_change_status",
- "issue_comment",
- "issue_create",
- "issue_subscribe",
- "issue_update",
- "issue_update_custom_fields",
- "issue_update_milestone",
- "modify_project",
- "pull_request_assign",
- "pull_request_close",
- "pull_request_comment",
- "pull_request_create",
- "pull_request_flag",
- "pull_request_merge",
- "pull_request_rebase",
- "pull_request_subscribe",
- "pull_request_update",
- "tag_project",
- "update_watch_status",
- ],
- )
- def test_get_acls_restrict_one(self):
- """ Test the get_acls function of pagure.lib.query. """
- acls = pagure.lib.query.get_acls(
- self.session, restrict="create_project"
- )
- self.assertEqual([a.name for a in acls], ["create_project"])
- def test_get_acls_restrict_two(self):
- """ Test the get_acls function of pagure.lib.query. """
- acls = pagure.lib.query.get_acls(
- self.session, restrict=["create_project", "issue_create"]
- )
- self.assertEqual(
- [a.name for a in acls], ["create_project", "issue_create"]
- )
- def test_filter_img_src(self):
- """ Test the filter_img_src function of pagure.lib.query. """
- for name in ("alt", "height", "width", "class"):
- self.assertTrue(pagure.lib.query.filter_img_src(name, "caption"))
- self.assertTrue(
- pagure.lib.query.filter_img_src("src", "/path/to/image")
- )
- self.assertTrue(
- pagure.lib.query.filter_img_src(
- "src", "http://localhost.localdomain/path/to/image"
- )
- )
- self.assertFalse(
- pagure.lib.query.filter_img_src(
- "src", "http://foo.org/path/to/image"
- )
- )
- self.assertFalse(
- pagure.lib.query.filter_img_src(
- "anything", "http://foo.org/path/to/image"
- )
- )
- def test_clean_input(self):
- """ Test the clean_input function of pagure.lib.query. """
- text = '<a href="/path" title="click me!">Click here</a>'
- output = pagure.lib.query.clean_input(text)
- self.assertEqual(output, text)
- def test_could_be_text(self):
- """ Test the could_be_text function of pagure.lib.query. """
- self.assertTrue(pagure.lib.query.could_be_text(b"foo"))
- self.assertTrue(pagure.lib.query.could_be_text("fâö".encode("utf-8")))
- self.assertFalse(
- pagure.lib.query.could_be_text(b"\x89PNG\r\n\x1a\n\x00")
- )
- def test_set_custom_key_fields_empty(self):
- """ Test the set_custom_key_fields function of pagure.lib.query. """
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- self.assertIsNotNone(project)
- msg = pagure.lib.query.set_custom_key_fields(
- session=self.session,
- project=project,
- fields=[],
- types=[],
- data=[],
- notify=False,
- )
- self.session.commit()
- self.assertEqual(msg, "List of custom fields updated")
- def test_set_custom_key_fields(self):
- """ Test the set_custom_key_fields function of pagure.lib.query. """
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- self.assertIsNotNone(project)
- # Set a custom key
- msg = pagure.lib.query.set_custom_key_fields(
- session=self.session,
- project=project,
- fields=["upstream"],
- types=["url"],
- data=[None],
- notify=False,
- )
- self.session.commit()
- self.assertEqual(msg, "List of custom fields updated")
- # Set another one, with notifications on
- msg = pagure.lib.query.set_custom_key_fields(
- session=self.session,
- project=project,
- fields=["bugzilla_url"],
- types=["url"],
- data=[None],
- notify=["on"],
- )
- self.session.commit()
- self.assertEqual(msg, "List of custom fields updated")
- # Re-set the second one but with notifications off
- msg = pagure.lib.query.set_custom_key_fields(
- session=self.session,
- project=project,
- fields=["bugzilla_url"],
- types=["url"],
- data=[None],
- notify=["off"],
- )
- self.session.commit()
- self.assertEqual(msg, "List of custom fields updated")
- @patch("pagure.lib.query.REDIS")
- def test_set_custom_key_value_boolean(self, mock_redis):
- """ Test the set_custom_key_value function of pagure.lib.query. """
- mock_redis.return_value = True
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- self.assertIsNotNone(project)
- # Set a custom key
- msg = pagure.lib.query.set_custom_key_fields(
- session=self.session,
- project=project,
- fields=["tested"],
- types=["boolean"],
- data=[None],
- notify=False,
- )
- self.session.commit()
- self.assertEqual(msg, "List of custom fields updated")
- # Create issues
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=project,
- title="Test issue",
- content="We should work on this",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue")
- issue = pagure.lib.query.search_issues(
- self.session, project, issueid=1
- )
- self.assertEqual(len(project.issue_keys), 1)
- self.assertEqual(project.issue_keys[0].key_type, "boolean")
- msg = pagure.lib.query.set_custom_key_value(
- session=self.session,
- issue=issue,
- key=project.issue_keys[0],
- value=True,
- )
- self.session.commit()
- self.assertEqual(msg, "Custom field tested adjusted to True")
- # Update it a second time to trigger edit
- msg = pagure.lib.query.set_custom_key_value(
- session=self.session,
- issue=issue,
- key=project.issue_keys[0],
- value=False,
- )
- if str(self.session.bind.engine.url).startswith("sqlite"):
- self.assertEqual(msg, "Custom field tested reset (from 1)")
- else:
- self.assertEqual(msg, "Custom field tested reset (from true)")
- self.assertEqual(mock_redis.publish.call_count, 2)
- @patch("pagure.lib.query.REDIS")
- def test_set_custom_key_value_boolean_private_issue(self, mock_redis):
- """ Test the set_custom_key_value function of pagure.lib.query. """
- mock_redis.return_value = True
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- self.assertIsNotNone(project)
- # Set a custom key
- msg = pagure.lib.query.set_custom_key_fields(
- session=self.session,
- project=project,
- fields=["tested"],
- types=["boolean"],
- data=[None],
- notify=False,
- )
- self.session.commit()
- self.assertEqual(msg, "List of custom fields updated")
- # Create issues
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=project,
- title="Test issue",
- content="We should work on this",
- user="pingou",
- private=True,
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue")
- issue = pagure.lib.query.search_issues(
- self.session, project, issueid=1
- )
- self.assertEqual(len(project.issue_keys), 1)
- self.assertEqual(project.issue_keys[0].key_type, "boolean")
- msg = pagure.lib.query.set_custom_key_value(
- session=self.session,
- issue=issue,
- key=project.issue_keys[0],
- value=True,
- )
- self.session.commit()
- self.assertEqual(msg, "Custom field tested adjusted to True")
- # Update it a second time to trigger edit
- msg = pagure.lib.query.set_custom_key_value(
- session=self.session,
- issue=issue,
- key=project.issue_keys[0],
- value=False,
- )
- self.session.commit()
- if str(self.session.bind.engine.url).startswith("sqlite"):
- self.assertEqual(msg, "Custom field tested reset (from 1)")
- else:
- self.assertEqual(msg, "Custom field tested reset (from true)")
- self.assertEqual(mock_redis.publish.call_count, 2)
- @patch("pagure.lib.query.REDIS")
- def test_set_custom_key_value_text(self, mock_redis):
- """ Test the set_custom_key_value function of pagure.lib.query. """
- mock_redis.return_value = True
- tests.create_projects(self.session)
- project = pagure.lib.query._get_project(self.session, "test")
- self.assertIsNotNone(project)
- # Set a custom key
- msg = pagure.lib.query.set_custom_key_fields(
- session=self.session,
- project=project,
- fields=["tested"],
- types=["text"],
- data=[None],
- notify=False,
- )
- self.session.commit()
- self.assertEqual(msg, "List of custom fields updated")
- # Create issues
- msg = pagure.lib.query.new_issue(
- session=self.session,
- repo=project,
- title="Test issue",
- content="We should work on this",
- user="pingou",
- )
- self.session.commit()
- self.assertEqual(msg.title, "Test issue")
- issue = pagure.lib.query.search_issues(
- self.session, project, issueid=1
- )
- self.assertEqual(len(project.issue_keys), 1)
- self.assertEqual(project.issue_keys[0].key_type, "text")
- msg = pagure.lib.query.set_custom_key_value(
- session=self.session,
- issue=issue,
- key=project.issue_keys[0],
- value="In progress",
- )
- self.session.commit()
- self.assertEqual(msg, "Custom field tested adjusted to In progress")
- # Update it a second time to trigger edit
- msg = pagure.lib.query.set_custom_key_value(
- session=self.session,
- issue=issue,
- key=project.issue_keys[0],
- value="Done",
- )
- self.assertEqual(
- msg, "Custom field tested adjusted to Done (was: In progress)"
- )
- self.assertEqual(mock_redis.publish.call_count, 2)
- def test_log_action_invalid(self):
- """ Test the log_action function of pagure.lib.query. """
- obj = MagicMock
- obj.isa = "invalid"
- self.assertRaises(
- pagure.exceptions.InvalidObjectException,
- pagure.lib.query.log_action,
- session=self.session,
- action="foo",
- obj=obj,
- user_obj=None,
- )
- def test_search_token_no_acls(self):
- """ Test the search_token function of pagure.lib.query. """
- tests.create_projects(self.session)
- tests.create_tokens(self.session)
- tests.create_tokens_acl(self.session)
- out = pagure.lib.query.search_token(self.session, [])
- self.assertEqual(len(out), 1)
- self.assertEqual(out[0].id, "aaabbbcccddd")
- def test_search_token_single_acls(self):
- """ Test the search_token function of pagure.lib.query. """
- tests.create_projects(self.session)
- tests.create_tokens(self.session)
- tests.create_tokens_acl(self.session)
- out = pagure.lib.query.search_token(self.session, "issue_create")
- self.assertEqual(len(out), 1)
- self.assertEqual(out[0].id, "aaabbbcccddd")
- def test_search_token_single_acls_user(self):
- """ Test the search_token function of pagure.lib.query. """
- tests.create_projects(self.session)
- tests.create_tokens(self.session)
- tests.create_tokens_acl(self.session)
- out = pagure.lib.query.search_token(
- self.session, "issue_create", user="pingou"
- )
- self.assertEqual(len(out), 1)
- self.assertEqual(out[0].id, "aaabbbcccddd")
- out = pagure.lib.query.search_token(
- self.session, "issue_create", user="foo"
- )
- self.assertEqual(len(out), 0)
- def test_search_token_single_acls_active(self):
- """ Test the search_token function of pagure.lib.query. """
- tests.create_projects(self.session)
- tests.create_tokens(self.session)
- tests.create_tokens_acl(self.session)
- out = pagure.lib.query.search_token(
- self.session, "issue_create", active=True
- )
- self.assertEqual(len(out), 1)
- self.assertEqual(out[0].id, "aaabbbcccddd")
- out = pagure.lib.query.search_token(
- self.session, "issue_create", expired=True
- )
- self.assertEqual(len(out), 0)
- def test_update_read_only_mode(self):
- """ Test the update_read_only_mode method of pagure.lib """
- tests.create_projects(self.session)
- project_obj = pagure.lib.query._get_project(self.session, "test")
- # Default mode of project is read only
- self.assertEqual(project_obj.read_only, True)
- # Remove read only
- pagure.lib.query.update_read_only_mode(
- self.session, project_obj, False
- )
- self.session.commit()
- project_obj = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(project_obj.read_only, False)
- # Try reversing it back
- pagure.lib.query.update_read_only_mode(self.session, project_obj, True)
- self.session.commit()
- project_obj = pagure.lib.query._get_project(self.session, "test")
- self.assertEqual(project_obj.read_only, True)
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|