1
0

test_pagure_lib.py 204 KB

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