test_pagure_lib.py 198 KB

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