test_pagure_lib.py 204 KB

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