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