test_pagure_lib.py 205 KB

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