1
0

test_pagure_lib.py 203 KB

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