test_pagure_lib.py 210 KB

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