1
0

test_pagure_flask_ui_repo.py 271 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2017 - 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 json
  10. import unittest
  11. import re
  12. import shutil
  13. import sys
  14. import tempfile
  15. import time
  16. import os
  17. cchardet = None
  18. try:
  19. import cchardet
  20. except ImportError:
  21. pass
  22. import pagure_messages
  23. import pygit2
  24. import six
  25. from fedora_messaging import testing
  26. from mock import ANY, patch, MagicMock
  27. sys.path.insert(
  28. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  29. )
  30. import pagure.lib.query
  31. import tests
  32. from pagure.lib.repo import PagureRepo
  33. from pagure.utils import __get_file_in_tree as get_file_in_tree
  34. class PagureFlaskRepotests(tests.Modeltests):
  35. """Tests for flask app controller of pagure"""
  36. def setUp(self):
  37. """Set up the environnment, ran before every tests."""
  38. super(PagureFlaskRepotests, self).setUp()
  39. pagure.config.config["VIRUS_SCAN_ATTACHMENTS"] = False
  40. pagure.config.config["UPLOAD_FOLDER_URL"] = "/releases/"
  41. pagure.config.config["UPLOAD_FOLDER_PATH"] = os.path.join(
  42. self.path, "releases"
  43. )
  44. @patch("pagure.decorators.admin_session_timedout")
  45. def test_add_user_when_user_mngt_off(self, ast):
  46. """Test the add_user endpoint when user management is turned off
  47. in the pagure instance"""
  48. pagure.config.config["ENABLE_USER_MNGT"] = False
  49. ast.return_value = False
  50. # No Git repo
  51. output = self.app.get("/foo/adduser")
  52. self.assertEqual(output.status_code, 404)
  53. tests.create_projects(self.session)
  54. tests.create_projects_git(os.path.join(self.path, "repos"))
  55. # User not logged in
  56. output = self.app.get("/test/adduser")
  57. self.assertEqual(output.status_code, 302)
  58. user = tests.FakeUser(username="pingou")
  59. with tests.user_set(self.app.application, user):
  60. output = self.app.get("/test/adduser")
  61. self.assertEqual(output.status_code, 404)
  62. # just get the csrf token
  63. pagure.config.config["ENABLE_USER_MNGT"] = True
  64. output = self.app.get("/test/adduser")
  65. output_text = output.get_data(as_text=True)
  66. csrf_token = output_text.split(
  67. 'name="csrf_token" type="hidden" value="'
  68. )[1].split('">')[0]
  69. pagure.config.config["ENABLE_USER_MNGT"] = False
  70. data = {"user": "ralph"}
  71. output = self.app.post("/test/adduser", data=data)
  72. self.assertEqual(output.status_code, 404)
  73. data["csrf_token"] = csrf_token
  74. output = self.app.post("/test/adduser", data=data)
  75. self.assertEqual(output.status_code, 404)
  76. data["user"] = "foo"
  77. tests.create_projects_git(os.path.join(self.path, "repos"))
  78. output = self.app.post(
  79. "/test/adduser", data=data, follow_redirects=True
  80. )
  81. self.assertEqual(output.status_code, 404)
  82. pagure.config.config["ENABLE_USER_MNGT"] = True
  83. @patch("pagure.decorators.admin_session_timedout")
  84. def test_add_deploykey(self, ast):
  85. """Test the add_deploykey endpoint."""
  86. ast.return_value = False
  87. # No git repo
  88. output = self.app.get("/foo/adddeploykey")
  89. self.assertEqual(output.status_code, 404)
  90. tests.create_projects(self.session)
  91. tests.create_projects_git(os.path.join(self.path, "repos"))
  92. # User not logged in
  93. output = self.app.get("/test/adddeploykey")
  94. self.assertEqual(output.status_code, 302)
  95. user = tests.FakeUser()
  96. with tests.user_set(self.app.application, user):
  97. output = self.app.get("/test/adddeploykey")
  98. self.assertEqual(output.status_code, 403)
  99. ast.return_value = True
  100. output = self.app.get("/test/adddeploykey")
  101. self.assertEqual(output.status_code, 302)
  102. # Redirect also happens for POST request
  103. output = self.app.post("/test/adddeploykey")
  104. self.assertEqual(output.status_code, 302)
  105. # Need to do this un-authentified since our fake user isn't in the DB
  106. # Check the message flashed during the redirect
  107. output = self.app.get("/", follow_redirects=True)
  108. self.assertEqual(output.status_code, 200)
  109. output_text = output.get_data(as_text=True)
  110. self.assertIn("Action canceled, try it " "again", output_text)
  111. ast.return_value = False
  112. user.username = "pingou"
  113. with tests.user_set(self.app.application, user):
  114. output = self.app.get("/test/adddeploykey")
  115. self.assertEqual(output.status_code, 200)
  116. output_text = output.get_data(as_text=True)
  117. self.assertIn("<strong>Add deploy key to the", output_text)
  118. csrf_token = output_text.split(
  119. 'name="csrf_token" type="hidden" value="'
  120. )[1].split('">')[0]
  121. data = {"ssh_key": "asdf", "pushaccess": "false"}
  122. # No CSRF token
  123. output = self.app.post("/test/adddeploykey", data=data)
  124. self.assertEqual(output.status_code, 200)
  125. output_text = output.get_data(as_text=True)
  126. self.assertIn("<strong>Add deploy key to the", output_text)
  127. data["csrf_token"] = csrf_token
  128. # First, invalid SSH key
  129. output = self.app.post("/test/adddeploykey", data=data)
  130. self.assertEqual(output.status_code, 200)
  131. output_text = output.get_data(as_text=True)
  132. self.assertIn("<strong>Add deploy key to the", output_text)
  133. self.assertIn("SSH key invalid", output_text)
  134. # Next up, multiple SSH keys
  135. data["ssh_key"] = (
  136. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q=="
  137. )
  138. output = self.app.post(
  139. "/test/adddeploykey", data=data, follow_redirects=True
  140. )
  141. self.assertEqual(output.status_code, 200)
  142. output_text = output.get_data(as_text=True)
  143. self.assertIn("Please add single SSH keys.", output_text)
  144. # Now, a valid SSH key
  145. data["ssh_key"] = (
  146. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q=="
  147. )
  148. output = self.app.post(
  149. "/test/adddeploykey", data=data, follow_redirects=True
  150. )
  151. self.assertEqual(output.status_code, 200)
  152. output_text = output.get_data(as_text=True)
  153. self.assertIn(
  154. "<title>Settings - test - Pagure</title>", output_text
  155. )
  156. self.assertIn(
  157. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  158. output_text,
  159. )
  160. self.assertIn("SSH key added", output_text)
  161. self.assertNotIn("Push Access", output_text)
  162. # And now, adding the same key
  163. output = self.app.post(
  164. "/test/adddeploykey", data=data, follow_redirects=True
  165. )
  166. self.assertEqual(output.status_code, 200)
  167. output_text = output.get_data(as_text=True)
  168. self.assertIn("SSH key already exists", output_text)
  169. # And next, a key with push access
  170. data["ssh_key"] = (
  171. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC9Xwc2RDzPBhlEDARfHldGjudIVoa04tqT1JVKGQmyllTFz7Rb8CngQL3e7zyNzotnhwYKHdoiLlPkVEiDee4dWMUe48ilqId+FJZQGhyv8fu4BoFdE1AJUVylzmltbLg14VqG5gjTpXgtlrEva9arKwBMHJjRYc8ScaSn3OgyQw=="
  172. )
  173. data["pushaccess"] = "true"
  174. output = self.app.post(
  175. "/test/adddeploykey", data=data, follow_redirects=True
  176. )
  177. self.assertEqual(output.status_code, 200)
  178. output_text = output.get_data(as_text=True)
  179. self.assertIn(
  180. "<title>Settings - test - Pagure</title>", output_text
  181. )
  182. self.assertIn(
  183. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  184. output_text,
  185. )
  186. self.assertIn("SSH key added", output_text)
  187. self.assertIn("Push Access", output_text)
  188. @patch("pagure.decorators.admin_session_timedout")
  189. @patch.dict("pagure.config.config", {"DEPLOY_KEY": False})
  190. def test_add_deploykey_disabled(self, ast):
  191. """Test the add_deploykey endpoint when it's disabled in the config."""
  192. ast.return_value = False
  193. tests.create_projects(self.session)
  194. tests.create_projects_git(os.path.join(self.path, "repos"))
  195. user = tests.FakeUser(username="pingou")
  196. with tests.user_set(self.app.application, user):
  197. output = self.app.get("/test/adddeploykey")
  198. self.assertEqual(output.status_code, 404)
  199. output = self.app.post("/test/adddeploykey")
  200. self.assertEqual(output.status_code, 404)
  201. @patch.dict(
  202. "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
  203. )
  204. @patch("pagure.decorators.admin_session_timedout")
  205. def test_add_user(self, ast):
  206. """Test the add_user endpoint."""
  207. ast.return_value = False
  208. # No git repo
  209. output = self.app.get("/foo/adduser")
  210. self.assertEqual(output.status_code, 404)
  211. tests.create_projects(self.session)
  212. tests.create_projects_git(os.path.join(self.path, "repos"))
  213. # User not logged in
  214. output = self.app.get("/test/adduser")
  215. self.assertEqual(output.status_code, 302)
  216. user = tests.FakeUser()
  217. with tests.user_set(self.app.application, user):
  218. output = self.app.get("/test/adduser")
  219. self.assertEqual(output.status_code, 403)
  220. ast.return_value = True
  221. output = self.app.get("/test/adduser")
  222. self.assertEqual(output.status_code, 302)
  223. # Redirect also happens for POST request
  224. output = self.app.post("/test/adduser")
  225. self.assertEqual(output.status_code, 302)
  226. # Need to do this un-authentified since our fake user isn't in the DB
  227. # Check the message flashed during the redirect
  228. output = self.app.get("/", follow_redirects=True)
  229. self.assertEqual(output.status_code, 200)
  230. output_text = output.get_data(as_text=True)
  231. self.assertIn("Action canceled, try it " "again", output_text)
  232. ast.return_value = False
  233. user.username = "pingou"
  234. with tests.user_set(self.app.application, user):
  235. output = self.app.get("/test/adduser")
  236. self.assertEqual(output.status_code, 200)
  237. output_text = output.get_data(as_text=True)
  238. self.assertIn("<strong>Add user to the", output_text)
  239. csrf_token = output_text.split(
  240. 'name="csrf_token" type="hidden" value="'
  241. )[1].split('">')[0]
  242. data = {"user": "ralph"}
  243. # Missing access and no CSRF
  244. output = self.app.post("/test/adduser", data=data)
  245. self.assertEqual(output.status_code, 200)
  246. output_text = output.get_data(as_text=True)
  247. self.assertIn(
  248. "<title>Add user - test - Pagure</title>", output_text
  249. )
  250. self.assertIn("<strong>Add user to the", output_text)
  251. # No CSRF
  252. output = self.app.post("/test/adduser", data=data)
  253. self.assertEqual(output.status_code, 200)
  254. output_text = output.get_data(as_text=True)
  255. self.assertIn(
  256. "<title>Add user - test - Pagure</title>", output_text
  257. )
  258. # Missing access
  259. data["csrf_token"] = csrf_token
  260. output = self.app.post("/test/adduser", data=data)
  261. self.assertEqual(output.status_code, 200)
  262. output_text = output.get_data(as_text=True)
  263. self.assertIn(
  264. "<title>Add user - test - Pagure</title>", output_text
  265. )
  266. self.assertIn("<strong>Add user to the", output_text)
  267. # Unknown user
  268. data["access"] = "commit"
  269. output = self.app.post("/test/adduser", data=data)
  270. self.assertEqual(output.status_code, 200)
  271. output_text = output.get_data(as_text=True)
  272. self.assertIn(
  273. "<title>Add user - test - Pagure</title>", output_text
  274. )
  275. self.assertIn("<strong>Add user to the", output_text)
  276. self.assertIn("No user &#34;ralph&#34; found", output_text)
  277. # All correct
  278. data["user"] = "foo"
  279. with testing.mock_sends(
  280. pagure_messages.ProjectUserAddedV1(
  281. topic="pagure.project.user.added",
  282. body={
  283. "project": {
  284. "id": 1,
  285. "name": "test",
  286. "fullname": "test",
  287. "url_path": "test",
  288. "full_url": "http://localhost.localdomain/test",
  289. "description": "test project #1",
  290. "namespace": None,
  291. "parent": None,
  292. "date_created": ANY,
  293. "date_modified": ANY,
  294. "user": {
  295. "name": "pingou",
  296. "fullname": "PY C",
  297. "url_path": "user/pingou",
  298. "full_url": "http://localhost.localdomain/user/pingou",
  299. },
  300. "access_users": {
  301. "owner": ["pingou"],
  302. "admin": [],
  303. "commit": ["foo"],
  304. "collaborator": [],
  305. "ticket": [],
  306. },
  307. "access_groups": {
  308. "admin": [],
  309. "commit": [],
  310. "collaborator": [],
  311. "ticket": [],
  312. },
  313. "tags": [],
  314. "priorities": {},
  315. "custom_keys": [],
  316. "close_status": [
  317. "Invalid",
  318. "Insufficient data",
  319. "Fixed",
  320. "Duplicate",
  321. ],
  322. "milestones": {},
  323. },
  324. "new_user": "foo",
  325. "access": "commit",
  326. "branches": None,
  327. "agent": "pingou",
  328. },
  329. )
  330. ):
  331. output = self.app.post(
  332. "/test/adduser", data=data, follow_redirects=True
  333. )
  334. self.assertEqual(output.status_code, 200)
  335. output_text = output.get_data(as_text=True)
  336. self.assertIn(
  337. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  338. output_text,
  339. )
  340. self.assertIn("User added", output_text)
  341. # Update access
  342. data["access"] = "ticket"
  343. data["user"] = "foo"
  344. with testing.mock_sends(
  345. pagure_messages.ProjectUserAccessUpdatedV1(
  346. topic="pagure.project.user.access.updated",
  347. body={
  348. "project": {
  349. "id": 1,
  350. "name": "test",
  351. "fullname": "test",
  352. "url_path": "test",
  353. "full_url": "http://localhost.localdomain/test",
  354. "description": "test project #1",
  355. "namespace": None,
  356. "parent": None,
  357. "date_created": ANY,
  358. "date_modified": ANY,
  359. "user": {
  360. "name": "pingou",
  361. "fullname": "PY C",
  362. "url_path": "user/pingou",
  363. "full_url": "http://localhost.localdomain/user/pingou",
  364. },
  365. "access_users": {
  366. "owner": ["pingou"],
  367. "admin": [],
  368. "commit": [],
  369. "collaborator": [],
  370. "ticket": ["foo"],
  371. },
  372. "access_groups": {
  373. "admin": [],
  374. "commit": [],
  375. "collaborator": [],
  376. "ticket": [],
  377. },
  378. "tags": [],
  379. "priorities": {},
  380. "custom_keys": [],
  381. "close_status": [
  382. "Invalid",
  383. "Insufficient data",
  384. "Fixed",
  385. "Duplicate",
  386. ],
  387. "milestones": {},
  388. },
  389. "new_user": "foo",
  390. "new_access": "ticket",
  391. "new_branches": None,
  392. "agent": "pingou",
  393. },
  394. )
  395. ):
  396. output = self.app.post(
  397. "/test/adduser", data=data, follow_redirects=True
  398. )
  399. self.assertEqual(output.status_code, 200)
  400. output_text = output.get_data(as_text=True)
  401. self.assertIn(
  402. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  403. output_text,
  404. )
  405. self.assertIn("User access updated", output_text)
  406. @patch("pagure.decorators.admin_session_timedout")
  407. def test_add_group_project_when_user_mngt_off(self, ast):
  408. """Test the add_group_project endpoint when user management is
  409. turned off in the pagure instance"""
  410. pagure.config.config["ENABLE_USER_MNGT"] = False
  411. ast.return_value = False
  412. # No Git repo
  413. output = self.app.get("/foo/addgroup")
  414. self.assertEqual(output.status_code, 404)
  415. tests.create_projects(self.session)
  416. tests.create_projects_git(os.path.join(self.path, "repos"))
  417. # User not logged in
  418. output = self.app.get("/test/addgroup")
  419. self.assertEqual(output.status_code, 302)
  420. msg = pagure.lib.query.add_group(
  421. self.session,
  422. group_name="foo",
  423. group_type="bar",
  424. display_name="foo group",
  425. description=None,
  426. user="pingou",
  427. is_admin=False,
  428. blacklist=pagure.config.config["BLACKLISTED_GROUPS"],
  429. )
  430. self.session.commit()
  431. self.assertEqual(msg, "User `pingou` added to the group `foo`.")
  432. user = tests.FakeUser(username="pingou")
  433. with tests.user_set(self.app.application, user):
  434. # just get the csrf token
  435. pagure.config.config["ENABLE_USER_MNGT"] = True
  436. output = self.app.get("/test/addgroup")
  437. output_text = output.get_data(as_text=True)
  438. csrf_token = output_text.split(
  439. 'name="csrf_token" type="hidden" value="'
  440. )[1].split('">')[0]
  441. pagure.config.config["ENABLE_USER_MNGT"] = False
  442. data = {"group": "ralph"}
  443. output = self.app.post("/test/addgroup", data=data)
  444. self.assertEqual(output.status_code, 404)
  445. data["csrf_token"] = csrf_token
  446. output = self.app.post("/test/addgroup", data=data)
  447. self.assertEqual(output.status_code, 404)
  448. data["group"] = "foo"
  449. output = self.app.post(
  450. "/test/addgroup", data=data, follow_redirects=True
  451. )
  452. self.assertEqual(output.status_code, 404)
  453. pagure.config.config["ENABLE_USER_MNGT"] = True
  454. @patch.dict("pagure.config.config", {"ENABLE_GROUP_MNGT": False})
  455. @patch("pagure.decorators.admin_session_timedout")
  456. def test_add_group_project_grp_mngt_off(self, ast):
  457. """Test the add_group_project endpoint when group management is
  458. turned off in the pagure instance"""
  459. ast.return_value = False
  460. tests.create_projects(self.session)
  461. tests.create_projects_git(os.path.join(self.path, "repos"))
  462. user = tests.FakeUser(username="pingou")
  463. with tests.user_set(self.app.application, user):
  464. data = {
  465. "group": "ralph",
  466. "access": "ticket",
  467. "csrf_token": self.get_csrf(),
  468. }
  469. output = self.app.post(
  470. "/test/addgroup", data=data, follow_redirects=True
  471. )
  472. self.assertEqual(output.status_code, 200)
  473. output_text = output.get_data(as_text=True)
  474. self.assertIn(
  475. "<title>Add group - test - Pagure</title>", output_text
  476. )
  477. self.assertIn("No group ralph found.", output_text)
  478. @patch.dict(
  479. "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
  480. )
  481. @patch("pagure.decorators.admin_session_timedout")
  482. def test_add_group_project(self, ast):
  483. """Test the add_group_project endpoint."""
  484. ast.return_value = False
  485. # No Git repo
  486. output = self.app.get("/foo/addgroup")
  487. self.assertEqual(output.status_code, 404)
  488. tests.create_projects(self.session)
  489. tests.create_projects_git(os.path.join(self.path, "repos"))
  490. # User not logged in
  491. output = self.app.get("/test/addgroup")
  492. self.assertEqual(output.status_code, 302)
  493. user = tests.FakeUser()
  494. with tests.user_set(self.app.application, user):
  495. output = self.app.get("/test/addgroup")
  496. self.assertEqual(output.status_code, 403)
  497. ast.return_value = True
  498. output = self.app.get("/test/addgroup")
  499. self.assertEqual(output.status_code, 302)
  500. # Redirect also happens for POST request
  501. output = self.app.post("/test/addgroup")
  502. self.assertEqual(output.status_code, 302)
  503. # Need to do this un-authentified since our fake user isn't in the DB
  504. # Check the message flashed during the redirect
  505. output = self.app.get("/", follow_redirects=True)
  506. self.assertEqual(output.status_code, 200)
  507. output_text = output.get_data(as_text=True)
  508. self.assertIn("Action canceled, try it " "again", output_text)
  509. ast.return_value = False
  510. msg = pagure.lib.query.add_group(
  511. self.session,
  512. group_name="foo",
  513. display_name="foo group",
  514. description=None,
  515. group_type="bar",
  516. user="pingou",
  517. is_admin=False,
  518. blacklist=pagure.config.config["BLACKLISTED_GROUPS"],
  519. )
  520. self.session.commit()
  521. self.assertEqual(msg, "User `pingou` added to the group `foo`.")
  522. user.username = "pingou"
  523. with tests.user_set(self.app.application, user):
  524. output = self.app.get("/test/addgroup")
  525. self.assertEqual(output.status_code, 200)
  526. output_text = output.get_data(as_text=True)
  527. self.assertIn("<strong>Add group to the", output_text)
  528. csrf_token = output_text.split(
  529. 'name="csrf_token" type="hidden" value="'
  530. )[1].split('">')[0]
  531. data = {"group": "ralph"}
  532. # Missing CSRF
  533. output = self.app.post("/test/addgroup", data=data)
  534. self.assertEqual(output.status_code, 200)
  535. output_text = output.get_data(as_text=True)
  536. self.assertIn(
  537. "<title>Add group - test - Pagure</title>", output_text
  538. )
  539. self.assertIn("<strong>Add group to the", output_text)
  540. # Missing access
  541. data["csrf_token"] = csrf_token
  542. output = self.app.post("/test/addgroup", data=data)
  543. self.assertEqual(output.status_code, 200)
  544. output_text = output.get_data(as_text=True)
  545. self.assertIn(
  546. "<title>Add group - test - Pagure</title>", output_text
  547. )
  548. self.assertIn("<strong>Add group to the", output_text)
  549. # All good
  550. data["access"] = "ticket"
  551. with testing.mock_sends(
  552. pagure_messages.ProjectGroupAddedV1(
  553. topic="pagure.project.group.added",
  554. body={
  555. "project": {
  556. "id": 1,
  557. "name": "test",
  558. "fullname": "test",
  559. "url_path": "test",
  560. "full_url": "http://localhost.localdomain/test",
  561. "description": "test project #1",
  562. "namespace": None,
  563. "parent": None,
  564. "date_created": ANY,
  565. "date_modified": ANY,
  566. "user": {
  567. "name": "pingou",
  568. "fullname": "PY C",
  569. "url_path": "user/pingou",
  570. "full_url": "http://localhost.localdomain/user/pingou",
  571. },
  572. "access_users": {
  573. "owner": ["pingou"],
  574. "admin": [],
  575. "commit": [],
  576. "collaborator": [],
  577. "ticket": [],
  578. },
  579. "access_groups": {
  580. "admin": [],
  581. "commit": [],
  582. "collaborator": [],
  583. "ticket": ["ralph"],
  584. },
  585. "tags": [],
  586. "priorities": {},
  587. "custom_keys": [],
  588. "close_status": [
  589. "Invalid",
  590. "Insufficient data",
  591. "Fixed",
  592. "Duplicate",
  593. ],
  594. "milestones": {},
  595. },
  596. "new_group": "ralph",
  597. "access": "ticket",
  598. "branches": None,
  599. "agent": "pingou",
  600. },
  601. )
  602. ):
  603. output = self.app.post(
  604. "/test/addgroup", data=data, follow_redirects=True
  605. )
  606. self.assertEqual(output.status_code, 200)
  607. output_text = output.get_data(as_text=True)
  608. self.assertIn(
  609. "<title>Settings - test - Pagure</title>", output_text
  610. )
  611. self.assertIn(
  612. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  613. output_text,
  614. )
  615. self.assertIn("Group added", output_text)
  616. # All good -- Update existing group/access
  617. data["access"] = "commit"
  618. with testing.mock_sends(
  619. pagure_messages.ProjectGroupAccessUpdatedV1(
  620. topic="pagure.project.group.access.updated",
  621. body={
  622. "project": {
  623. "id": 1,
  624. "name": "test",
  625. "fullname": "test",
  626. "url_path": "test",
  627. "full_url": "http://localhost.localdomain/test",
  628. "description": "test project #1",
  629. "namespace": None,
  630. "parent": None,
  631. "date_created": ANY,
  632. "date_modified": ANY,
  633. "user": {
  634. "name": "pingou",
  635. "fullname": "PY C",
  636. "url_path": "user/pingou",
  637. "full_url": "http://localhost.localdomain/user/pingou",
  638. },
  639. "access_users": {
  640. "owner": ["pingou"],
  641. "admin": [],
  642. "commit": [],
  643. "collaborator": [],
  644. "ticket": [],
  645. },
  646. "access_groups": {
  647. "admin": [],
  648. "commit": ["ralph"],
  649. "collaborator": [],
  650. "ticket": [],
  651. },
  652. "tags": [],
  653. "priorities": {},
  654. "custom_keys": [],
  655. "close_status": [
  656. "Invalid",
  657. "Insufficient data",
  658. "Fixed",
  659. "Duplicate",
  660. ],
  661. "milestones": {},
  662. },
  663. "new_group": "ralph",
  664. "new_access": "commit",
  665. "new_branches": None,
  666. "agent": "pingou",
  667. },
  668. )
  669. ):
  670. output = self.app.post(
  671. "/test/addgroup", data=data, follow_redirects=True
  672. )
  673. self.assertEqual(output.status_code, 200)
  674. output_text = output.get_data(as_text=True)
  675. self.assertIn(
  676. "<title>Settings - test - Pagure</title>", output_text
  677. )
  678. self.assertIn(
  679. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  680. output_text,
  681. )
  682. self.assertIn("Group access updated", output_text)
  683. @patch("pagure.decorators.admin_session_timedout")
  684. def test_remove_user_when_user_mngt_off(self, ast):
  685. """Test the remove_user endpoint when user management is turned
  686. off in the pagure instance"""
  687. pagure.config.config["ENABLE_USER_MNGT"] = False
  688. ast.return_value = False
  689. # Git repo not found
  690. output = self.app.post("/foo/dropuser/1")
  691. self.assertEqual(output.status_code, 404)
  692. user = tests.FakeUser(username="pingou")
  693. with tests.user_set(self.app.application, user):
  694. tests.create_projects(self.session)
  695. tests.create_projects_git(os.path.join(self.path, "repos"))
  696. output = self.app.post("/test/settings")
  697. output_text = output.get_data(as_text=True)
  698. csrf_token = output_text.split(
  699. 'name="csrf_token" type="hidden" value="'
  700. )[1].split('">')[0]
  701. data = {"csrf_token": csrf_token}
  702. output = self.app.post(
  703. "/test/dropuser/2", data=data, follow_redirects=True
  704. )
  705. self.assertEqual(output.status_code, 404)
  706. # User not logged in
  707. output = self.app.post("/test/dropuser/1")
  708. self.assertEqual(output.status_code, 302)
  709. # Add an user to a project
  710. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  711. msg = pagure.lib.query.add_user_to_project(
  712. session=self.session, project=repo, new_user="foo", user="pingou"
  713. )
  714. self.session.commit()
  715. self.assertEqual(msg, "User added")
  716. with tests.user_set(self.app.application, user):
  717. output = self.app.post("/test/dropuser/2", follow_redirects=True)
  718. self.assertEqual(output.status_code, 404)
  719. data = {"csrf_token": csrf_token}
  720. output = self.app.post(
  721. "/test/dropuser/2", data=data, follow_redirects=True
  722. )
  723. self.assertEqual(output.status_code, 404)
  724. pagure.config.config["ENABLE_USER_MNGT"] = True
  725. @patch("pagure.decorators.admin_session_timedout")
  726. def test_remove_deploykey(self, ast):
  727. """Test the remove_deploykey endpoint."""
  728. ast.return_value = False
  729. # Git repo not found
  730. output = self.app.post("/foo/dropdeploykey/1")
  731. self.assertEqual(output.status_code, 404)
  732. user = tests.FakeUser()
  733. with tests.user_set(self.app.application, user):
  734. output = self.app.post("/foo/dropdeploykey/1")
  735. self.assertEqual(output.status_code, 404)
  736. tests.create_projects(self.session)
  737. tests.create_projects_git(os.path.join(self.path, "repos"))
  738. output = self.app.post("/test/dropdeploykey/1")
  739. self.assertEqual(output.status_code, 403)
  740. ast.return_value = True
  741. output = self.app.post("/test/dropdeploykey/1")
  742. self.assertEqual(output.status_code, 302)
  743. ast.return_value = False
  744. # User not logged in
  745. output = self.app.post("/test/dropdeploykey/1")
  746. self.assertEqual(output.status_code, 302)
  747. user.username = "pingou"
  748. with tests.user_set(self.app.application, user):
  749. output = self.app.post("/test/settings")
  750. output_text = output.get_data(as_text=True)
  751. csrf_token = output_text.split(
  752. 'name="csrf_token" type="hidden" value="'
  753. )[1].split('">')[0]
  754. data = {"csrf_token": csrf_token}
  755. output = self.app.post(
  756. "/test/dropdeploykey/1", data=data, follow_redirects=True
  757. )
  758. self.assertEqual(output.status_code, 200)
  759. output_text = output.get_data(as_text=True)
  760. self.assertIn(
  761. "<title>Settings - test - Pagure</title>", output_text
  762. )
  763. self.assertIn(
  764. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  765. output_text,
  766. )
  767. self.assertIn("Deploy key does not exist in project", output_text)
  768. # Add a deploy key to a project
  769. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  770. pingou = pagure.lib.query.get_user(self.session, "pingou")
  771. msg = pagure.lib.query.add_sshkey_to_project_or_user(
  772. session=self.session,
  773. project=repo,
  774. ssh_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==",
  775. pushaccess=True,
  776. creator=pingou,
  777. )
  778. self.session.commit()
  779. self.assertEqual(msg, "SSH key added")
  780. with tests.user_set(self.app.application, user):
  781. output = self.app.post(
  782. "/test/dropdeploykey/1", follow_redirects=True
  783. )
  784. self.assertEqual(output.status_code, 200)
  785. output_text = output.get_data(as_text=True)
  786. self.assertIn(
  787. "<title>Settings - test - Pagure</title>", output_text
  788. )
  789. self.assertIn(
  790. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  791. output_text,
  792. )
  793. self.assertNotIn("Deploy key removed", output_text)
  794. data = {"csrf_token": csrf_token}
  795. output = self.app.post(
  796. "/test/dropdeploykey/1", data=data, follow_redirects=True
  797. )
  798. self.assertEqual(output.status_code, 200)
  799. output_text = output.get_data(as_text=True)
  800. self.assertIn(
  801. "<title>Settings - test - Pagure</title>", output_text
  802. )
  803. self.assertIn(
  804. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  805. output_text,
  806. )
  807. self.assertIn("Deploy key removed", output_text)
  808. @patch("pagure.decorators.admin_session_timedout")
  809. @patch.dict("pagure.config.config", {"DEPLOY_KEY": False})
  810. def test_remove_deploykey_disabled(self, ast):
  811. """Test the remove_deploykey endpoint when it's disabled in the
  812. config.
  813. """
  814. ast.return_value = False
  815. tests.create_projects(self.session)
  816. tests.create_projects_git(os.path.join(self.path, "repos"))
  817. user = tests.FakeUser(username="pingou")
  818. with tests.user_set(self.app.application, user):
  819. output = self.app.post("/test/dropdeploykey/1")
  820. self.assertEqual(output.status_code, 404)
  821. @patch.dict(
  822. "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
  823. )
  824. @patch("pagure.decorators.admin_session_timedout")
  825. def test_remove_user(self, ast):
  826. """Test the remove_user endpoint."""
  827. ast.return_value = False
  828. # Git repo not found
  829. output = self.app.post("/foo/dropuser/1")
  830. self.assertEqual(output.status_code, 404)
  831. user = tests.FakeUser()
  832. with tests.user_set(self.app.application, user):
  833. output = self.app.post("/foo/dropuser/1")
  834. self.assertEqual(output.status_code, 404)
  835. tests.create_projects(self.session)
  836. tests.create_projects_git(os.path.join(self.path, "repos"))
  837. output = self.app.post("/test/dropuser/1")
  838. self.assertEqual(output.status_code, 403)
  839. ast.return_value = True
  840. output = self.app.post("/test/dropuser/1")
  841. self.assertEqual(output.status_code, 302)
  842. ast.return_value = False
  843. # User not logged in
  844. output = self.app.post("/test/dropuser/1")
  845. self.assertEqual(output.status_code, 302)
  846. user.username = "pingou"
  847. with tests.user_set(self.app.application, user):
  848. output = self.app.post("/test/settings")
  849. output_text = output.get_data(as_text=True)
  850. csrf_token = output_text.split(
  851. 'name="csrf_token" type="hidden" value="'
  852. )[1].split('">')[0]
  853. data = {"csrf_token": csrf_token}
  854. output = self.app.post(
  855. "/test/dropuser/2", data=data, follow_redirects=True
  856. )
  857. self.assertEqual(output.status_code, 200)
  858. output_text = output.get_data(as_text=True)
  859. self.assertIn(
  860. "<title>Settings - test - Pagure</title>", output_text
  861. )
  862. self.assertIn(
  863. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  864. output_text,
  865. )
  866. self.assertIn(
  867. "User does not have any " "access on the repo", output_text
  868. )
  869. # Add an user to a project
  870. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  871. self.assertEqual(len(repo.users), 0)
  872. msg = pagure.lib.query.add_user_to_project(
  873. session=self.session, project=repo, new_user="foo", user="pingou"
  874. )
  875. self.session.commit()
  876. self.assertEqual(msg, "User added")
  877. self.assertEqual(len(repo.users), 1)
  878. with tests.user_set(self.app.application, user):
  879. output = self.app.post("/test/dropuser/2", follow_redirects=True)
  880. self.assertEqual(output.status_code, 200)
  881. output_text = output.get_data(as_text=True)
  882. self.assertIn(
  883. "<title>Settings - test - Pagure</title>", output_text
  884. )
  885. self.assertIn(
  886. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  887. output_text,
  888. )
  889. self.assertNotIn("User removed", output_text)
  890. self.assertIn('action="/test/dropuser/2">', output_text)
  891. repo = pagure.lib.query.get_authorized_project(
  892. self.session, "test"
  893. )
  894. self.assertEqual(len(repo.users), 1)
  895. data = {"csrf_token": csrf_token}
  896. with testing.mock_sends(
  897. pagure_messages.ProjectUserRemovedV1(
  898. topic="pagure.project.user.removed",
  899. body={
  900. "project": {
  901. "id": 1,
  902. "name": "test",
  903. "fullname": "test",
  904. "url_path": "test",
  905. "full_url": "http://localhost.localdomain/test",
  906. "description": "test project #1",
  907. "namespace": None,
  908. "parent": None,
  909. "date_created": ANY,
  910. "date_modified": ANY,
  911. "user": {
  912. "name": "pingou",
  913. "fullname": "PY C",
  914. "url_path": "user/pingou",
  915. "full_url": "http://localhost.localdomain/user/pingou",
  916. },
  917. "access_users": {
  918. "owner": ["pingou"],
  919. "admin": [],
  920. "commit": [],
  921. "collaborator": [],
  922. "ticket": [],
  923. },
  924. "access_groups": {
  925. "admin": [],
  926. "commit": [],
  927. "collaborator": [],
  928. "ticket": [],
  929. },
  930. "tags": [],
  931. "priorities": {},
  932. "custom_keys": [],
  933. "close_status": [
  934. "Invalid",
  935. "Insufficient data",
  936. "Fixed",
  937. "Duplicate",
  938. ],
  939. "milestones": {},
  940. },
  941. "removed_user": "foo",
  942. "agent": "pingou",
  943. },
  944. )
  945. ):
  946. output = self.app.post(
  947. "/test/dropuser/2", data=data, follow_redirects=True
  948. )
  949. self.assertEqual(output.status_code, 200)
  950. output_text = output.get_data(as_text=True)
  951. self.assertIn(
  952. "<title>Settings - test - Pagure</title>", output_text
  953. )
  954. self.assertIn(
  955. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  956. output_text,
  957. )
  958. self.assertIn("User removed", output_text)
  959. self.assertNotIn('action="/test/dropuser/2">', output_text)
  960. self.session.commit()
  961. repo = pagure.lib.query.get_authorized_project(
  962. self.session, "test"
  963. )
  964. self.assertEqual(len(repo.users), 0)
  965. @patch("pagure.decorators.admin_session_timedout")
  966. @patch("pagure.lib.notify.log")
  967. def test_remove_user_self(self, mock_log, ast):
  968. """Test the remove_user endpoint when removing themselves."""
  969. ast.return_value = False
  970. tests.create_projects(self.session)
  971. tests.create_projects_git(os.path.join(self.path, "repos"))
  972. # Add an user to a project
  973. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  974. self.assertEqual(len(repo.users), 0)
  975. msg = pagure.lib.query.add_user_to_project(
  976. session=self.session, project=repo, new_user="foo", user="pingou"
  977. )
  978. self.session.commit()
  979. self.assertEqual(msg, "User added")
  980. self.assertEqual(len(repo.users), 1)
  981. # Let user foo remove themselves
  982. user = tests.FakeUser(username="foo")
  983. with tests.user_set(self.app.application, user):
  984. csrf_token = self.get_csrf()
  985. data = {"csrf_token": csrf_token}
  986. output = self.app.post(
  987. "/test/dropuser/2", data=data, follow_redirects=True
  988. )
  989. self.assertEqual(output.status_code, 200)
  990. output_text = output.get_data(as_text=True)
  991. self.assertIn(
  992. "<title>Overview - test - Pagure</title>", output_text
  993. )
  994. self.assertIn(
  995. '<h3 class="mb-0">\n<a href="/test"><strong>test</strong>'
  996. "</a>\n </h3>",
  997. output_text,
  998. )
  999. self.assertIn("User removed", output_text)
  1000. self.session.commit()
  1001. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1002. self.assertEqual(len(repo.users), 0)
  1003. mock_log.assert_called_with(ANY, topic="project.user.removed", msg=ANY)
  1004. @patch("pagure.decorators.admin_session_timedout")
  1005. def test_remove_group_project_when_user_mngt_off(self, ast):
  1006. """Test the remove_group_project endpoint when user management is
  1007. turned off in the pagure instance"""
  1008. pagure.config.config["ENABLE_USER_MNGT"] = False
  1009. ast.return_value = False
  1010. # No Git repo
  1011. output = self.app.post("/foo/dropgroup/1")
  1012. self.assertEqual(output.status_code, 404)
  1013. tests.create_projects(self.session)
  1014. tests.create_projects_git(os.path.join(self.path, "repos"))
  1015. # User not logged in
  1016. output = self.app.post("/test/dropgroup/1")
  1017. self.assertEqual(output.status_code, 302)
  1018. user = tests.FakeUser()
  1019. user.username = "pingou"
  1020. with tests.user_set(self.app.application, user):
  1021. output = self.app.post("/test/settings")
  1022. output_text = output.get_data(as_text=True)
  1023. csrf_token = output_text.split(
  1024. 'name="csrf_token" type="hidden" value="'
  1025. )[1].split('">')[0]
  1026. data = {"csrf_token": csrf_token}
  1027. output = self.app.post(
  1028. "/test/dropgroup/2", data=data, follow_redirects=True
  1029. )
  1030. self.assertEqual(output.status_code, 404)
  1031. # Create the new group
  1032. msg = pagure.lib.query.add_group(
  1033. session=self.session,
  1034. group_name="testgrp",
  1035. group_type="user",
  1036. display_name="testgrp group",
  1037. description=None,
  1038. user="pingou",
  1039. is_admin=False,
  1040. blacklist=[],
  1041. )
  1042. self.assertEqual(msg, "User `pingou` added to the group `testgrp`.")
  1043. self.session.commit()
  1044. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1045. # Add the group to a project
  1046. msg = pagure.lib.query.add_group_to_project(
  1047. session=self.session,
  1048. project=repo,
  1049. new_group="testgrp",
  1050. user="pingou",
  1051. )
  1052. self.session.commit()
  1053. self.assertEqual(msg, "Group added")
  1054. with tests.user_set(self.app.application, user):
  1055. output = self.app.post("/test/dropgroup/1", follow_redirects=True)
  1056. self.assertEqual(output.status_code, 404)
  1057. data = {"csrf_token": csrf_token}
  1058. output = self.app.post(
  1059. "/test/dropgroup/1", data=data, follow_redirects=True
  1060. )
  1061. self.assertEqual(output.status_code, 404)
  1062. pagure.config.config["ENABLE_USER_MNGT"] = True
  1063. @patch.dict(
  1064. "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
  1065. )
  1066. @patch("pagure.decorators.admin_session_timedout")
  1067. def test_remove_group_project(self, ast):
  1068. """Test the remove_group_project endpoint."""
  1069. ast.return_value = False
  1070. # No Git repo
  1071. output = self.app.post("/foo/dropgroup/1")
  1072. self.assertEqual(output.status_code, 404)
  1073. user = tests.FakeUser()
  1074. with tests.user_set(self.app.application, user):
  1075. output = self.app.post("/foo/dropgroup/1")
  1076. self.assertEqual(output.status_code, 404)
  1077. tests.create_projects(self.session)
  1078. tests.create_projects_git(os.path.join(self.path, "repos"))
  1079. output = self.app.post("/test/dropgroup/1")
  1080. self.assertEqual(output.status_code, 403)
  1081. ast.return_value = True
  1082. output = self.app.post("/test/dropgroup/1")
  1083. self.assertEqual(output.status_code, 302)
  1084. ast.return_value = False
  1085. # User not logged in
  1086. output = self.app.post("/test/dropgroup/1")
  1087. self.assertEqual(output.status_code, 302)
  1088. user.username = "pingou"
  1089. with tests.user_set(self.app.application, user):
  1090. output = self.app.post("/test/settings")
  1091. output_text = output.get_data(as_text=True)
  1092. csrf_token = output_text.split(
  1093. 'name="csrf_token" type="hidden" value="'
  1094. )[1].split('">')[0]
  1095. data = {"csrf_token": csrf_token}
  1096. output = self.app.post(
  1097. "/test/dropgroup/2", data=data, follow_redirects=True
  1098. )
  1099. self.assertEqual(output.status_code, 200)
  1100. output_text = output.get_data(as_text=True)
  1101. self.assertIn(
  1102. "<title>Settings - test - Pagure</title>", output_text
  1103. )
  1104. self.assertIn(
  1105. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1106. output_text,
  1107. )
  1108. self.assertIn(
  1109. "" "Group does not seem to be part of this project",
  1110. output_text,
  1111. )
  1112. # Create the new group
  1113. msg = pagure.lib.query.add_group(
  1114. session=self.session,
  1115. group_name="testgrp",
  1116. group_type="user",
  1117. display_name="testgrp group",
  1118. description=None,
  1119. user="pingou",
  1120. is_admin=False,
  1121. blacklist=[],
  1122. )
  1123. self.assertEqual(msg, "User `pingou` added to the group `testgrp`.")
  1124. self.session.commit()
  1125. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1126. # Add the group to a project
  1127. msg = pagure.lib.query.add_group_to_project(
  1128. session=self.session,
  1129. project=repo,
  1130. new_group="testgrp",
  1131. user="pingou",
  1132. )
  1133. self.session.commit()
  1134. self.assertEqual(msg, "Group added")
  1135. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1136. self.assertEqual(len(repo.groups), 1)
  1137. with tests.user_set(self.app.application, user):
  1138. output = self.app.post("/test/dropgroup/1", follow_redirects=True)
  1139. self.assertEqual(output.status_code, 200)
  1140. output_text = output.get_data(as_text=True)
  1141. self.assertIn(
  1142. "<title>Settings - test - Pagure</title>", output_text
  1143. )
  1144. self.assertIn(
  1145. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1146. output_text,
  1147. )
  1148. self.assertIn('action="/test/dropgroup/1">', output_text)
  1149. self.assertNotIn("Group removed", output_text)
  1150. repo = pagure.lib.query.get_authorized_project(
  1151. self.session, "test"
  1152. )
  1153. self.assertEqual(len(repo.groups), 1)
  1154. data = {"csrf_token": csrf_token}
  1155. with testing.mock_sends(
  1156. pagure_messages.ProjectGroupRemovedV1(
  1157. topic="pagure.project.group.removed",
  1158. body={
  1159. "project": {
  1160. "id": 1,
  1161. "name": "test",
  1162. "fullname": "test",
  1163. "url_path": "test",
  1164. "full_url": "http://localhost.localdomain/test",
  1165. "description": "test project #1",
  1166. "namespace": None,
  1167. "parent": None,
  1168. "date_created": ANY,
  1169. "date_modified": ANY,
  1170. "user": {
  1171. "name": "pingou",
  1172. "fullname": "PY C",
  1173. "url_path": "user/pingou",
  1174. "full_url": "http://localhost.localdomain/user/pingou",
  1175. },
  1176. "access_users": {
  1177. "owner": ["pingou"],
  1178. "admin": [],
  1179. "commit": [],
  1180. "collaborator": [],
  1181. "ticket": [],
  1182. },
  1183. "access_groups": {
  1184. "admin": [],
  1185. "commit": [],
  1186. "collaborator": [],
  1187. "ticket": [],
  1188. },
  1189. "tags": [],
  1190. "priorities": {},
  1191. "custom_keys": [],
  1192. "close_status": [
  1193. "Invalid",
  1194. "Insufficient data",
  1195. "Fixed",
  1196. "Duplicate",
  1197. ],
  1198. "milestones": {},
  1199. },
  1200. "removed_groups": ["testgrp"],
  1201. "agent": "pingou",
  1202. },
  1203. )
  1204. ):
  1205. output = self.app.post(
  1206. "/test/dropgroup/1", data=data, follow_redirects=True
  1207. )
  1208. self.assertEqual(output.status_code, 200)
  1209. output_text = output.get_data(as_text=True)
  1210. self.assertIn(
  1211. "<title>Settings - test - Pagure</title>", output_text
  1212. )
  1213. self.assertIn(
  1214. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1215. output_text,
  1216. )
  1217. self.assertIn("Group removed", output_text)
  1218. self.assertNotIn('action="/test/dropgroup/1">', output_text)
  1219. self.session.commit()
  1220. repo = pagure.lib.query.get_authorized_project(
  1221. self.session, "test"
  1222. )
  1223. self.assertEqual(len(repo.groups), 0)
  1224. @patch("pagure.decorators.admin_session_timedout")
  1225. def test_update_project(self, ast):
  1226. """Test the update_project endpoint."""
  1227. ast.return_value = True
  1228. # Git repo not found
  1229. output = self.app.post("/foo/update")
  1230. self.assertEqual(output.status_code, 404)
  1231. user = tests.FakeUser()
  1232. with tests.user_set(self.app.application, user):
  1233. # Project does not exist
  1234. output = self.app.post("/foo/update")
  1235. self.assertEqual(output.status_code, 404)
  1236. tests.create_projects(self.session)
  1237. tests.create_projects_git(os.path.join(self.path, "repos"))
  1238. # Session timed-out
  1239. output = self.app.post("/test/update")
  1240. self.assertEqual(output.status_code, 302)
  1241. ast.return_value = False
  1242. # Not allowed
  1243. output = self.app.post("/test/update")
  1244. self.assertEqual(output.status_code, 403)
  1245. # User not logged in
  1246. output = self.app.post("/test/update")
  1247. self.assertEqual(output.status_code, 302)
  1248. user.username = "pingou"
  1249. with tests.user_set(self.app.application, user):
  1250. output = self.app.post("/test/update", follow_redirects=True)
  1251. self.assertEqual(output.status_code, 200)
  1252. output_text = output.get_data(as_text=True)
  1253. self.assertIn(
  1254. "<title>Settings - test - Pagure</title>", output_text
  1255. )
  1256. self.assertIn(
  1257. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1258. output_text,
  1259. )
  1260. csrf_token = output_text.split(
  1261. 'name="csrf_token" type="hidden" value="'
  1262. )[1].split('">')[0]
  1263. data = {
  1264. "description": "new description for test project #1",
  1265. "csrf_token": csrf_token,
  1266. }
  1267. output = self.app.post(
  1268. "/test/update", data=data, follow_redirects=True
  1269. )
  1270. self.assertEqual(output.status_code, 200)
  1271. output_text = output.get_data(as_text=True)
  1272. self.assertIn(
  1273. "<title>Settings - test - Pagure</title>", output_text
  1274. )
  1275. self.assertIn(
  1276. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1277. output_text,
  1278. )
  1279. self.assertIn(
  1280. '<input class="form-control" name="avatar_email" value="" />',
  1281. output_text,
  1282. )
  1283. self.assertIn("Project updated", output_text)
  1284. # Edit the avatar_email
  1285. data = {
  1286. "description": "new description for test project #1",
  1287. "avatar_email": "pingou@fp.o",
  1288. "csrf_token": csrf_token,
  1289. }
  1290. output = self.app.post(
  1291. "/test/update", data=data, follow_redirects=True
  1292. )
  1293. self.assertEqual(output.status_code, 200)
  1294. output_text = output.get_data(as_text=True)
  1295. self.assertIn(
  1296. "<title>Settings - test - Pagure</title>", output_text
  1297. )
  1298. self.assertIn(
  1299. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1300. output_text,
  1301. )
  1302. self.assertIn(
  1303. '<input class="form-control" name="avatar_email" value="pingou@fp.o" />',
  1304. output_text,
  1305. )
  1306. self.assertIn("Project updated", output_text)
  1307. # Reset the avatar_email
  1308. data = {
  1309. "description": "new description for test project #1",
  1310. "avatar_email": "",
  1311. "csrf_token": csrf_token,
  1312. }
  1313. output = self.app.post(
  1314. "/test/update", data=data, follow_redirects=True
  1315. )
  1316. self.assertEqual(output.status_code, 200)
  1317. output_text = output.get_data(as_text=True)
  1318. self.assertIn(
  1319. "<title>Settings - test - Pagure</title>", output_text
  1320. )
  1321. self.assertIn(
  1322. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1323. output_text,
  1324. )
  1325. self.assertIn(
  1326. '<input class="form-control" name="avatar_email" value="" />',
  1327. output_text,
  1328. )
  1329. self.assertIn("Project updated", output_text)
  1330. @patch("pagure.decorators.admin_session_timedout")
  1331. def test_update_project_update_tag(self, ast):
  1332. """Test the view_settings endpoint when updating the project's tags.
  1333. We had an issue where when you add an existing tag to a project we
  1334. were querying the wrong table in the database. It would thus not find
  1335. the tag, would try to add it, and (rightfully) complain about duplicated
  1336. content.
  1337. This test ensure we are behaving properly.
  1338. """
  1339. ast.return_value = False
  1340. tests.create_projects(self.session)
  1341. tests.create_projects_git(os.path.join(self.path, "repos"))
  1342. user = tests.FakeUser(username="pingou")
  1343. with tests.user_set(self.app.application, user):
  1344. output = self.app.get("/test/settings")
  1345. self.assertEqual(output.status_code, 200)
  1346. output_text = output.get_data(as_text=True)
  1347. self.assertIn(
  1348. "<title>Settings - test - Pagure</title>", output_text
  1349. )
  1350. self.assertIn(
  1351. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1352. output_text,
  1353. )
  1354. csrf_token = output_text.split(
  1355. 'name="csrf_token" type="hidden" value="'
  1356. )[1].split('">')[0]
  1357. # Add tag to a project so that they are added to the database
  1358. data = {
  1359. "csrf_token": csrf_token,
  1360. "description": "Test project",
  1361. "tags": "test,pagure,tag",
  1362. }
  1363. output = self.app.post(
  1364. "/test/update", data=data, follow_redirects=True
  1365. )
  1366. self.assertEqual(output.status_code, 200)
  1367. output_text = output.get_data(as_text=True)
  1368. self.assertIn(
  1369. "<title>Settings - test - Pagure</title>", output_text
  1370. )
  1371. self.assertIn(
  1372. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1373. output_text,
  1374. )
  1375. self.assertIn("Project updated", output_text)
  1376. # Remove two of the tags of the project, they will still be in
  1377. # the DB but not associated to this project
  1378. data = {
  1379. "csrf_token": csrf_token,
  1380. "description": "Test project",
  1381. "tags": "tag",
  1382. }
  1383. output = self.app.post(
  1384. "/test/update", data=data, follow_redirects=True
  1385. )
  1386. self.assertEqual(output.status_code, 200)
  1387. output_text = output.get_data(as_text=True)
  1388. self.assertIn(
  1389. "<title>Settings - test - Pagure</title>", output_text
  1390. )
  1391. self.assertIn(
  1392. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1393. output_text,
  1394. )
  1395. self.assertIn("Project updated", output_text)
  1396. # Try re-adding the two tags, this used to fail before we fixed
  1397. # it
  1398. data = {
  1399. "csrf_token": csrf_token,
  1400. "description": "Test project",
  1401. "tags": "test,pagure,tag",
  1402. }
  1403. output = self.app.post(
  1404. "/test/update", data=data, follow_redirects=True
  1405. )
  1406. self.assertEqual(output.status_code, 200)
  1407. output_text = output.get_data(as_text=True)
  1408. self.assertIn(
  1409. "<title>Settings - test - Pagure</title>", output_text
  1410. )
  1411. self.assertIn(
  1412. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1413. output_text,
  1414. )
  1415. self.assertIn("Project updated", output_text)
  1416. @patch.dict(
  1417. "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
  1418. )
  1419. @patch("pagure.decorators.admin_session_timedout")
  1420. def test_view_settings(self, ast):
  1421. """Test the view_settings endpoint."""
  1422. ast.return_value = False
  1423. # No Git repo
  1424. output = self.app.get("/foo/settings")
  1425. self.assertEqual(output.status_code, 404)
  1426. user = tests.FakeUser()
  1427. with tests.user_set(self.app.application, user):
  1428. output = self.app.get("/foo/settings")
  1429. self.assertEqual(output.status_code, 404)
  1430. tests.create_projects(self.session)
  1431. tests.create_projects_git(os.path.join(self.path, "repos"))
  1432. output = self.app.get("/test/settings")
  1433. self.assertEqual(output.status_code, 403)
  1434. # User not logged in
  1435. output = self.app.get("/test/settings")
  1436. self.assertEqual(output.status_code, 302)
  1437. user.username = "pingou"
  1438. with tests.user_set(self.app.application, user):
  1439. ast.return_value = True
  1440. output = self.app.get("/test/settings")
  1441. self.assertEqual(output.status_code, 302)
  1442. ast.return_value = False
  1443. output = self.app.get("/test/settings")
  1444. self.assertEqual(output.status_code, 200)
  1445. output_text = output.get_data(as_text=True)
  1446. self.assertIn(
  1447. "<title>Settings - test - Pagure</title>", output_text
  1448. )
  1449. self.assertIn(
  1450. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1451. output_text,
  1452. )
  1453. # Both checkbox checked before
  1454. self.assertIn(
  1455. '<input id="pull_requests" type="checkbox" value="y" '
  1456. 'name="pull_requests" checked=""/>',
  1457. output_text,
  1458. )
  1459. self.assertIn(
  1460. '<input id="issue_tracker" type="checkbox" value="y" '
  1461. 'name="issue_tracker" checked=""/>',
  1462. output_text,
  1463. )
  1464. csrf_token = output_text.split(
  1465. 'name="csrf_token" type="hidden" value="'
  1466. )[1].split('">')[0]
  1467. data = {}
  1468. output = self.app.post(
  1469. "/test/settings", data=data, follow_redirects=True
  1470. )
  1471. self.assertEqual(output.status_code, 200)
  1472. output_text = output.get_data(as_text=True)
  1473. self.assertIn(
  1474. "<title>Settings - test - Pagure</title>", output_text
  1475. )
  1476. self.assertIn(
  1477. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1478. output_text,
  1479. )
  1480. # Both checkbox are still checked
  1481. output = self.app.get("/test/settings", follow_redirects=True)
  1482. self.assertEqual(output.status_code, 200)
  1483. output_text = output.get_data(as_text=True)
  1484. self.assertIn(
  1485. "<title>Settings - test - Pagure</title>", output_text
  1486. )
  1487. self.assertIn(
  1488. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1489. output_text,
  1490. )
  1491. self.assertIn(
  1492. '<input id="pull_requests" type="checkbox" value="y" '
  1493. 'name="pull_requests" checked=""/>',
  1494. output_text,
  1495. )
  1496. self.assertIn(
  1497. '<input id="issue_tracker" type="checkbox" value="y" '
  1498. 'name="issue_tracker" checked=""/>',
  1499. output_text,
  1500. )
  1501. data = {"csrf_token": csrf_token}
  1502. output = self.app.post(
  1503. "/test/settings", data=data, follow_redirects=True
  1504. )
  1505. self.assertEqual(output.status_code, 200)
  1506. output_text = output.get_data(as_text=True)
  1507. self.assertIn(
  1508. "<title>Overview - test - Pagure</title>", output_text
  1509. )
  1510. self.assertIn(
  1511. "Edited successfully " "settings of repo: test", output_text
  1512. )
  1513. # Both checkbox are now un-checked
  1514. output = self.app.get("/test/settings", follow_redirects=True)
  1515. self.assertEqual(output.status_code, 200)
  1516. output_text = output.get_data(as_text=True)
  1517. self.assertIn(
  1518. "<title>Settings - test - Pagure</title>", output_text
  1519. )
  1520. self.assertIn(
  1521. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1522. output_text,
  1523. )
  1524. self.assertIn(
  1525. '<input id="pull_requests" type="checkbox" value="y" '
  1526. 'name="pull_requests" />',
  1527. output_text,
  1528. )
  1529. self.assertIn(
  1530. '<input id="issue_tracker" type="checkbox" value="y" '
  1531. 'name="issue_tracker" />',
  1532. output_text,
  1533. )
  1534. data = {
  1535. "csrf_token": csrf_token,
  1536. "pull_requests": "y",
  1537. "issue_tracker": "y",
  1538. "fedmsg_notifications": "y",
  1539. }
  1540. with testing.mock_sends(
  1541. pagure_messages.ProjectEditV1(
  1542. topic="pagure.project.edit",
  1543. body={
  1544. "project": {
  1545. "id": 1,
  1546. "name": "test",
  1547. "fullname": "test",
  1548. "url_path": "test",
  1549. "full_url": "http://localhost.localdomain/test",
  1550. "description": "test project #1",
  1551. "namespace": None,
  1552. "parent": None,
  1553. "date_created": ANY,
  1554. "date_modified": ANY,
  1555. "user": {
  1556. "name": "pingou",
  1557. "fullname": "PY C",
  1558. "url_path": "user/pingou",
  1559. "full_url": "http://localhost.localdomain/user/pingou",
  1560. },
  1561. "access_users": {
  1562. "owner": ["pingou"],
  1563. "admin": [],
  1564. "commit": [],
  1565. "collaborator": [],
  1566. "ticket": [],
  1567. },
  1568. "access_groups": {
  1569. "admin": [],
  1570. "commit": [],
  1571. "collaborator": [],
  1572. "ticket": [],
  1573. },
  1574. "tags": [],
  1575. "priorities": {},
  1576. "custom_keys": [],
  1577. "close_status": [
  1578. "Invalid",
  1579. "Insufficient data",
  1580. "Fixed",
  1581. "Duplicate",
  1582. ],
  1583. "milestones": {},
  1584. },
  1585. "fields": [
  1586. "fedmsg_notifications",
  1587. "issue_tracker",
  1588. "pull_requests",
  1589. ],
  1590. "agent": "pingou",
  1591. },
  1592. )
  1593. ):
  1594. output = self.app.post(
  1595. "/test/settings", data=data, follow_redirects=True
  1596. )
  1597. self.assertEqual(output.status_code, 200)
  1598. output_text = output.get_data(as_text=True)
  1599. self.assertIn(
  1600. "<title>Overview - test - Pagure</title>", output_text
  1601. )
  1602. self.assertIn(
  1603. "Edited successfully " "settings of repo: test", output_text
  1604. )
  1605. # Both checkbox are again checked
  1606. output = self.app.get("/test/settings", follow_redirects=True)
  1607. self.assertEqual(output.status_code, 200)
  1608. output_text = output.get_data(as_text=True)
  1609. self.assertIn(
  1610. "<title>Settings - test - Pagure</title>", output_text
  1611. )
  1612. self.assertIn(
  1613. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1614. output_text,
  1615. )
  1616. self.assertIn(
  1617. '<input id="pull_requests" type="checkbox" value="y" '
  1618. 'name="pull_requests" checked=""/>',
  1619. output_text,
  1620. )
  1621. self.assertIn(
  1622. '<input id="issue_tracker" type="checkbox" value="y" '
  1623. 'name="issue_tracker" checked=""/>',
  1624. output_text,
  1625. )
  1626. @patch(
  1627. "pagure.decorators.admin_session_timedout",
  1628. MagicMock(return_value=False),
  1629. )
  1630. def test_view_settings_custom_fields(self):
  1631. """Test the view_settings endpoint when the project has some custom
  1632. field for issues."""
  1633. tests.create_projects(self.session)
  1634. tests.create_projects_git(os.path.join(self.path, "repos"))
  1635. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1636. msg = pagure.lib.query.set_custom_key_fields(
  1637. self.session,
  1638. repo,
  1639. ["bugzilla", "upstream", "reviewstatus"],
  1640. ["link", "boolean", "list"],
  1641. [
  1642. "unused data for non-list type",
  1643. "",
  1644. "ack",
  1645. "nack",
  1646. "needs review",
  1647. ],
  1648. [None, None, None],
  1649. )
  1650. self.session.commit()
  1651. self.assertEqual(msg, "List of custom fields updated")
  1652. self.assertIsNotNone(repo.issue_keys)
  1653. user = tests.FakeUser(username="pingou")
  1654. with tests.user_set(self.app.application, user):
  1655. output = self.app.get("/test/settings")
  1656. self.assertEqual(output.status_code, 200)
  1657. @patch("pagure.decorators.admin_session_timedout")
  1658. def test_view_settings_pr_only(self, ast):
  1659. """Test the view_settings endpoint when turning on PR only."""
  1660. ast.return_value = False
  1661. tests.create_projects(self.session)
  1662. tests.create_projects_git(os.path.join(self.path, "repos"))
  1663. user = tests.FakeUser(username="pingou")
  1664. with tests.user_set(self.app.application, user):
  1665. output = self.app.get("/test/settings")
  1666. self.assertEqual(output.status_code, 200)
  1667. output_text = output.get_data(as_text=True)
  1668. self.assertIn(
  1669. "<title>Settings - test - Pagure</title>", output_text
  1670. )
  1671. self.assertIn(
  1672. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1673. output_text,
  1674. )
  1675. csrf_token = self.get_csrf(output=output)
  1676. data = {
  1677. "csrf_token": csrf_token,
  1678. "pull_requests": "y",
  1679. "issue_tracker": "y",
  1680. "pull_request_access_only": "y",
  1681. }
  1682. output = self.app.post(
  1683. "/test/settings", data=data, follow_redirects=True
  1684. )
  1685. self.assertEqual(output.status_code, 200)
  1686. output_text = output.get_data(as_text=True)
  1687. self.assertIn(
  1688. "<title>Overview - test - Pagure</title>", output_text
  1689. )
  1690. self.assertIn(
  1691. "Edited successfully " "settings of repo: test", output_text
  1692. )
  1693. # Both checkbox are again checked
  1694. output = self.app.get("/test/settings", follow_redirects=True)
  1695. self.assertEqual(output.status_code, 200)
  1696. output_text = output.get_data(as_text=True)
  1697. self.assertIn(
  1698. "<title>Settings - test - Pagure</title>", output_text
  1699. )
  1700. self.assertIn(
  1701. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1702. output_text,
  1703. )
  1704. self.assertIn(
  1705. '<input id="pull_requests" type="checkbox" value="y" '
  1706. 'name="pull_requests" checked=""/>',
  1707. output_text,
  1708. )
  1709. self.assertIn(
  1710. '<input id="issue_tracker" type="checkbox" value="y" '
  1711. 'name="issue_tracker" checked=""/>',
  1712. output_text,
  1713. )
  1714. self.assertIn(
  1715. '<input id="pull_request_access_only" type="checkbox" '
  1716. 'value="y" name="pull_request_access_only" checked=""/>',
  1717. output_text,
  1718. )
  1719. @patch("pagure.decorators.admin_session_timedout")
  1720. def test_fields_in_view_settings(self, ast):
  1721. """Test the default fields in view_settings endpoint."""
  1722. ast.return_value = False
  1723. # No Git repo
  1724. output = self.app.get("/foo/settings")
  1725. self.assertEqual(output.status_code, 404)
  1726. user = tests.FakeUser()
  1727. with tests.user_set(self.app.application, user):
  1728. output = self.app.get("/foo/settings")
  1729. self.assertEqual(output.status_code, 404)
  1730. item = pagure.lib.model.Project(
  1731. user_id=1, # pingou
  1732. name="test",
  1733. description="test project #1",
  1734. hook_token="aaabbbccc",
  1735. )
  1736. self.session.add(item)
  1737. self.session.commit()
  1738. tests.create_projects_git(os.path.join(self.path, "repos"))
  1739. output = self.app.get("/test/settings")
  1740. self.assertEqual(output.status_code, 403)
  1741. # User not logged in
  1742. output = self.app.get("/test/settings")
  1743. self.assertEqual(output.status_code, 302)
  1744. user.username = "pingou"
  1745. with tests.user_set(self.app.application, user):
  1746. ast.return_value = True
  1747. output = self.app.get("/test/settings")
  1748. self.assertEqual(output.status_code, 302)
  1749. ast.return_value = False
  1750. output = self.app.get("/test/settings")
  1751. self.assertEqual(output.status_code, 200)
  1752. output_text = output.get_data(as_text=True)
  1753. self.assertIn(
  1754. "<title>Settings - test - Pagure</title>", output_text
  1755. )
  1756. self.assertIn(
  1757. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  1758. output_text,
  1759. )
  1760. # Check that the priorities have their empty fields
  1761. self.assertIn(
  1762. """<div class="form-group settings-field-rows" id="priorities-list">
  1763. <div class="row hidden blank-field">
  1764. <div class="col-sm-2" >
  1765. <input type="text" name="priority_weigth"
  1766. value="" size="3" class="form-control"/>
  1767. </div>
  1768. <div class="col-sm-9">
  1769. <input type="text" name="priority_title"
  1770. value="" class="form-control"/>
  1771. </div>""",
  1772. output_text,
  1773. )
  1774. # Check that the milestones have their empty fields
  1775. self.assertIn(
  1776. """<div id="milestones">
  1777. <div class="row p-t-1 milestone" id="milestone_1">
  1778. <input type="hidden" name="milestones" value="1">
  1779. <div class="col-sm-4 p-r-0">
  1780. <input type="text" name="milestone_1_name"
  1781. value="" size="3" class="form-control"/>
  1782. </div>
  1783. <div class="col-sm-4 p-r-0">
  1784. <input type="text" name="milestone_1_date"
  1785. value="" class="form-control"/>
  1786. </div>
  1787. <div class="col-sm-2 p-r-0" >
  1788. <span class="fa fa-long-arrow-up milestone_order_up"
  1789. data-stone="1"></span>
  1790. <span class="fa fa-long-arrow-down milestone_order_bottom"
  1791. data-stone="1"></span>
  1792. </div>
  1793. <div class="col-sm-1 p-r-0" >
  1794. <input type="checkbox" name="milestone_1_active" />
  1795. </div>
  1796. </div>""",
  1797. output_text,
  1798. )
  1799. # Check that the close_status have its empty field
  1800. self.assertIn(
  1801. """<div class="form-group settings-field-rows" id="status-list">
  1802. <div class="row hidden blank-field">
  1803. <div class="col-sm-11" >
  1804. <input type="text" name="close_status"
  1805. value="" class="form-control"/>
  1806. </div>""",
  1807. output_text,
  1808. )
  1809. # Check that the custom fields have their empty fields
  1810. self.assertIn(
  1811. """<div class="form-group settings-field-rows" id="customfields-list">
  1812. <div class="row hidden blank-field">
  1813. <div class="col-sm-2 pr-0">
  1814. <input type="text" name="custom_keys"
  1815. value="" class="form-control"/>
  1816. </div>
  1817. <div class="col-sm-2 pr-0">
  1818. <select name="custom_keys_type" class="form-control custom-keys">
  1819. <option value="text">Text</option>
  1820. <option value="boolean">Boolean</option>
  1821. <option value="link">Link</option>
  1822. <option value="list">List</option>
  1823. <option value="date">Date</option>
  1824. </select>
  1825. </div>
  1826. <div class="col-sm-6 pr-0">
  1827. <input title="Comma separated list items" type="text" name="custom_keys_data" value="" class="form-control custom-keys-list hidden" id="custom_keys_list"/>
  1828. </div>
  1829. <div class="col-sm-1 pr-0">
  1830. <input type="checkbox" name="custom_keys_notify" title="Trigger email notification when updated" class="form-control"/>
  1831. </div>""",
  1832. output_text,
  1833. )
  1834. def test_view_forks(self):
  1835. """Test the view_forks endpoint."""
  1836. output = self.app.get("/foo/forks", follow_redirects=True)
  1837. self.assertEqual(output.status_code, 404)
  1838. tests.create_projects(self.session)
  1839. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1840. output = self.app.get("/test/forks", follow_redirects=True)
  1841. self.assertEqual(output.status_code, 200)
  1842. output_text = output.get_data(as_text=True)
  1843. self.assertIn("This project has not been forked.", output_text)
  1844. @patch.dict("pagure.config.config", {"CASE_SENSITIVE": True})
  1845. def test_view_repo_case_sensitive(self):
  1846. """Test the view_repo endpoint."""
  1847. tests.create_projects(self.session)
  1848. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1849. output = self.app.get("/test")
  1850. self.assertEqual(output.status_code, 200)
  1851. output_text = output.get_data(as_text=True)
  1852. self.assertIn("<p>This repo is brand new!</p>", output_text)
  1853. output = self.app.get("/TEST")
  1854. self.assertEqual(output.status_code, 404)
  1855. def test_view_repo_more_button_absent_no_auth(self):
  1856. """Test the view_repo endpoint and check if the "more" button is
  1857. absent when not logged in."""
  1858. tests.create_projects(self.session)
  1859. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1860. output = self.app.get("/test")
  1861. self.assertEqual(output.status_code, 200)
  1862. output_text = output.get_data(as_text=True)
  1863. self.assertNotIn(
  1864. '<span class="pull-xs-right"><a data-toggle="collapse" '
  1865. 'href="#moregiturls"',
  1866. output_text,
  1867. )
  1868. self.assertIn("<p>This repo is brand new!</p>", output_text)
  1869. self.assertIn("<title>Overview - test - Pagure</title>", output_text)
  1870. self.assertIn(
  1871. '<span class="d-none d-md-inline">Stats</span>', output_text
  1872. )
  1873. self.perfMaxWalks(0, 0)
  1874. self.perfReset()
  1875. def test_view_repo_more_button_present(self):
  1876. """Test the view_repo endpoint and check if the "more" button is
  1877. present when it should be."""
  1878. tests.create_projects(self.session)
  1879. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1880. pingou = pagure.lib.query.get_user(self.session, "pingou")
  1881. pagure.lib.query.add_sshkey_to_project_or_user(
  1882. session=self.session,
  1883. user=pingou,
  1884. ssh_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==",
  1885. pushaccess=True,
  1886. creator=pingou,
  1887. )
  1888. self.session.commit()
  1889. user = tests.FakeUser(username="pingou")
  1890. with tests.user_set(self.app.application, user):
  1891. output = self.app.get("/test")
  1892. self.assertEqual(output.status_code, 200)
  1893. output_text = output.get_data(as_text=True)
  1894. self.assertIn(
  1895. '<input class="form-control bg-white select-on-focus" type="text" '
  1896. 'value="ssh://git@localhost.localdomain/tickets/test.git" readonly>',
  1897. output_text,
  1898. )
  1899. self.assertIn("<p>This repo is brand new!</p>", output_text)
  1900. self.assertIn(
  1901. "<title>Overview - test - Pagure</title>", output_text
  1902. )
  1903. self.assertIn(
  1904. '<span class="d-none d-md-inline">Stats</span>', output_text
  1905. )
  1906. self.perfMaxWalks(0, 0)
  1907. self.perfReset()
  1908. def test_view_repo_more_button_absent_no_access(self):
  1909. """Test the view_repo endpoint and check if the "more" button is
  1910. absent if the user doesn't have access to the project."""
  1911. tests.create_projects(self.session)
  1912. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1913. user = tests.FakeUser(username="foo")
  1914. with tests.user_set(self.app.application, user):
  1915. output = self.app.get("/test")
  1916. self.assertEqual(output.status_code, 200)
  1917. output_text = output.get_data(as_text=True)
  1918. self.assertNotIn(
  1919. '<input class="form-control bg-white select-on-focus" type="text" '
  1920. 'value="ssh://git@localhost.localdomain/tickets/test.git" readonly>',
  1921. output_text,
  1922. )
  1923. self.assertIn("<p>This repo is brand new!</p>", output_text)
  1924. self.assertIn(
  1925. "<title>Overview - test - Pagure</title>", output_text
  1926. )
  1927. self.assertIn(
  1928. '<span class="d-none d-md-inline">Stats</span>', output_text
  1929. )
  1930. self.perfMaxWalks(0, 0)
  1931. self.perfReset()
  1932. def test_view_repo_ssh_key_not_uploaded_no_ssh_url(self):
  1933. """Test viewing repo when user hasn't uploaded SSH key yet
  1934. and thus should see a message instead of url for SSH cloning."""
  1935. tests.create_projects(self.session)
  1936. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1937. user = tests.FakeUser(username="pingou")
  1938. with tests.user_set(self.app.application, user):
  1939. output = self.app.get("/test")
  1940. self.assertEqual(output.status_code, 200)
  1941. output_text = output.get_data(as_text=True)
  1942. self.assertIn(
  1943. "You need to upload SSH key to be able to clone over SSH",
  1944. output_text,
  1945. )
  1946. def test_view_repo(self):
  1947. """Test the view_repo endpoint."""
  1948. output = self.app.get("/foo")
  1949. # No project registered in the DB
  1950. self.assertEqual(output.status_code, 404)
  1951. tests.create_projects(self.session)
  1952. output = self.app.get("/test")
  1953. # No git repo associated
  1954. self.assertEqual(output.status_code, 404)
  1955. self.perfMaxWalks(0, 0)
  1956. self.perfReset()
  1957. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  1958. output = self.app.get("/test")
  1959. self.assertEqual(output.status_code, 200)
  1960. output_text = output.get_data(as_text=True)
  1961. self.assertIn("<p>This repo is brand new!</p>", output_text)
  1962. self.assertIn("<title>Overview - test - Pagure</title>", output_text)
  1963. self.assertIn(
  1964. '<a class="nav-link" href="/test/stats">\n '
  1965. '<i class="fa fa-line-chart fa-fw text-muted"></i>\n '
  1966. '<span class="d-none d-md-inline">Stats</span>\n </a>',
  1967. output_text,
  1968. )
  1969. self.perfMaxWalks(0, 0)
  1970. self.perfReset()
  1971. output = self.app.get("/test/")
  1972. self.assertEqual(output.status_code, 200)
  1973. output_text = output.get_data(as_text=True)
  1974. self.assertIn("<p>This repo is brand new!</p>", output_text)
  1975. self.assertIn("<title>Overview - test - Pagure</title>", output_text)
  1976. self.perfMaxWalks(0, 0)
  1977. self.perfReset()
  1978. # Add some content to the git repo
  1979. tests.add_content_git_repo(
  1980. os.path.join(self.path, "repos", "test.git")
  1981. )
  1982. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  1983. tests.add_readme_git_repo(
  1984. os.path.join(self.path, "repos", "test.git"), "README.txt"
  1985. )
  1986. tests.add_readme_git_repo(
  1987. os.path.join(self.path, "repos", "test.git"), "README.dummy"
  1988. )
  1989. self.perfReset()
  1990. # Authenticated, the Fork button appears
  1991. user = tests.FakeUser(username="pingou")
  1992. with tests.user_set(self.app.application, user):
  1993. output = self.app.get("/test")
  1994. self.assertEqual(output.status_code, 200)
  1995. output_text = output.get_data(as_text=True)
  1996. self.assertIn(
  1997. '<i class="fa fa-code-fork fa-fw"></i> Fork</button>',
  1998. output_text,
  1999. )
  2000. self.assertFalse("<p>This repo is brand new!</p>" in output_text)
  2001. self.assertNotIn("Forked from", output_text)
  2002. self.assertNotIn("README.txt", output_text)
  2003. self.assertNotIn("README.dummy", output_text)
  2004. self.assertIn(
  2005. "<title>Overview - test - Pagure</title>", output_text
  2006. )
  2007. self.perfMaxWalks(3, 8) # Target: (1, 3)
  2008. self.perfReset()
  2009. # Non-authenticated, the Fork button does not appear
  2010. output = self.app.get("/test")
  2011. self.assertEqual(output.status_code, 200)
  2012. output_text = output.get_data(as_text=True)
  2013. self.assertNotIn(
  2014. '<i class="fa fa-code-fork"></i>Fork</button>', output_text
  2015. )
  2016. self.assertNotIn("<p>This repo is brand new!</p>", output_text)
  2017. self.assertNotIn("Forked from", output_text)
  2018. self.assertNotIn("README.txt", output_text)
  2019. self.assertNotIn("README.dummy", output_text)
  2020. self.assertIn("<title>Overview - test - Pagure</title>", output_text)
  2021. self.perfMaxWalks(3, 8) # Target: (1, 3)
  2022. self.perfReset()
  2023. # Turn that repo into a fork
  2024. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2025. repo.parent_id = 2
  2026. repo.is_fork = True
  2027. self.session.add(repo)
  2028. self.session.commit()
  2029. # View the repo in the UI
  2030. output = self.app.get("/test")
  2031. self.assertEqual(output.status_code, 404)
  2032. # Add some content to the git repo
  2033. tests.add_content_git_repo(
  2034. os.path.join(self.path, "repos", "forks", "pingou", "test.git")
  2035. )
  2036. tests.add_readme_git_repo(
  2037. os.path.join(self.path, "repos", "forks", "pingou", "test.git")
  2038. )
  2039. # Authenticated and already have a fork, the View Fork button appears
  2040. user = tests.FakeUser(username="pingou")
  2041. with tests.user_set(self.app.application, user):
  2042. output = self.app.get("/fork/pingou/test")
  2043. self.assertEqual(output.status_code, 200)
  2044. output_text = output.get_data(as_text=True)
  2045. self.assertNotIn("<p>This repo is brand new!</p>", output_text)
  2046. self.assertIn(
  2047. "<title>Overview - test - Pagure</title>", output_text
  2048. )
  2049. self.assertIn("Forked from", output_text)
  2050. self.assertNotIn(
  2051. '<i class="fa fa-code-fork fa-fw"></i> Fork</button>',
  2052. output_text,
  2053. )
  2054. self.assertIn(
  2055. '<i class="fa fa-code-fork fa-fw"></i> View Upstream',
  2056. output_text,
  2057. )
  2058. self.perfMaxWalks(1, 3)
  2059. self.perfReset()
  2060. # Authenticated, the Fork button appears
  2061. user = tests.FakeUser(username="foo")
  2062. with tests.user_set(self.app.application, user):
  2063. output = self.app.get("/fork/pingou/test")
  2064. self.assertEqual(output.status_code, 200)
  2065. output_text = output.get_data(as_text=True)
  2066. self.assertNotIn("<p>This repo is brand new!</p>", output_text)
  2067. self.assertIn(
  2068. "<title>Overview - test - Pagure</title>", output_text
  2069. )
  2070. self.assertIn("Forked from", output_text)
  2071. self.assertNotIn(
  2072. '<i class="fa fa-code-fork fa-fw"></i> View Upstream',
  2073. output_text,
  2074. )
  2075. self.assertIn(
  2076. '<i class="fa fa-code-fork fa-fw"></i> Fork</button>',
  2077. output_text,
  2078. )
  2079. self.perfMaxWalks(1, 3)
  2080. self.perfReset()
  2081. # Non-authenticated, the Fork button does not appear
  2082. output = self.app.get("/fork/pingou/test")
  2083. self.assertEqual(output.status_code, 200)
  2084. output_text = output.get_data(as_text=True)
  2085. self.assertNotIn("<p>This repo is brand new!</p>", output_text)
  2086. self.assertIn("<title>Overview - test - Pagure</title>", output_text)
  2087. self.assertIn("Forked from", output_text)
  2088. self.assertNotIn(
  2089. '<i class="fa fa-code-fork"></i> View Fork', output_text
  2090. )
  2091. self.assertNotIn(
  2092. '<i class="fa fa-code-fork"></i>Fork</button>', output_text
  2093. )
  2094. self.perfMaxWalks(1, 3)
  2095. self.perfReset()
  2096. # Add a fork of a fork
  2097. item = pagure.lib.model.Project(
  2098. user_id=1, # pingou
  2099. name="test3",
  2100. description="test project #3",
  2101. is_fork=True,
  2102. parent_id=1,
  2103. hook_token="aaabbbmmm",
  2104. )
  2105. self.session.add(item)
  2106. self.session.commit()
  2107. tests.add_content_git_repo(
  2108. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  2109. )
  2110. tests.add_readme_git_repo(
  2111. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  2112. )
  2113. tests.add_commit_git_repo(
  2114. os.path.join(self.path, "repos", "forks", "pingou", "test3.git"),
  2115. ncommits=10,
  2116. )
  2117. output = self.app.get("/fork/pingou/test3")
  2118. self.assertEqual(output.status_code, 200)
  2119. output_text = output.get_data(as_text=True)
  2120. self.assertNotIn("<p>This repo is brand new!</p>", output_text)
  2121. self.assertIn("<title>Overview - test3 - Pagure</title>", output_text)
  2122. self.assertIn("Forked from", output_text)
  2123. self.perfMaxWalks(3, 18) # Ideal: (1, 3)
  2124. self.perfReset()
  2125. def test_view_repo_empty(self):
  2126. """Test the view_repo endpoint on a repo w/o master branch."""
  2127. tests.create_projects(self.session)
  2128. # Create a git repo to play with
  2129. gitrepo = os.path.join(self.path, "repos", "test.git")
  2130. pygit2.init_repository(gitrepo, bare=True)
  2131. # Create a fork of this repo
  2132. newpath = tempfile.mkdtemp(prefix="pagure-viewrepo-test")
  2133. new_repo = pygit2.clone_repository(gitrepo, newpath)
  2134. # Edit the sources file again
  2135. with open(os.path.join(newpath, "sources"), "w") as stream:
  2136. stream.write("foo\n bar\nbaz\n boose")
  2137. new_repo.index.add("sources")
  2138. new_repo.index.write()
  2139. # Commits the files added
  2140. tree = new_repo.index.write_tree()
  2141. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  2142. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  2143. new_repo.create_commit(
  2144. "refs/heads/feature",
  2145. author,
  2146. committer,
  2147. "A commit on branch feature",
  2148. tree,
  2149. [],
  2150. )
  2151. refname = "refs/heads/feature"
  2152. ori_remote = new_repo.remotes[0]
  2153. PagureRepo.push(ori_remote, refname)
  2154. output = self.app.get("/test")
  2155. self.assertEqual(output.status_code, 200)
  2156. output_text = output.get_data(as_text=True)
  2157. self.assertIn("<p>This repo is brand new!</p>", output_text)
  2158. self.assertNotIn("Forked from", output_text)
  2159. self.assertIn("<title>Overview - test - Pagure</title>", output_text)
  2160. self.assertEqual(output_text.count('<span class="commitid">'), 0)
  2161. shutil.rmtree(newpath)
  2162. '''
  2163. def test_view_repo_branch(self):
  2164. """ Test the view_repo_branch endpoint. """
  2165. output = self.app.get('/foo/branch/master')
  2166. # No project registered in the DB
  2167. self.assertEqual(output.status_code, 404)
  2168. tests.create_projects(self.session)
  2169. output = self.app.get('/test/branch/master')
  2170. # No git repo associated
  2171. self.assertEqual(output.status_code, 404)
  2172. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  2173. output = self.app.get('/test/branch/master')
  2174. self.assertEqual(output.status_code, 404)
  2175. # Add some content to the git repo
  2176. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  2177. 'test.git'))
  2178. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  2179. # Turn that repo into a fork
  2180. repo = pagure.lib.query.get_authorized_project(self.session, 'test')
  2181. repo.parent_id = 2
  2182. repo.is_fork = True
  2183. self.session.add(repo)
  2184. self.session.commit()
  2185. # Add some content to the git repo
  2186. tests.add_content_git_repo(
  2187. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  2188. tests.add_readme_git_repo(
  2189. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  2190. output = self.app.get('/fork/pingou/test/')
  2191. self.assertEqual(output.status_code, 200)
  2192. output_text = output.get_data(as_text=True)
  2193. self.assertNotIn('<p>This repo is brand new!</p>', output_text)
  2194. self.assertIn('Forked from', output_text)
  2195. # Add a fork of a fork
  2196. item = pagure.lib.model.Project(
  2197. user_id=1, # pingou
  2198. name='test3',
  2199. description='test project #3',
  2200. is_fork=True,
  2201. parent_id=1,
  2202. hook_token='aaabbbnnn',
  2203. )
  2204. self.session.add(item)
  2205. self.session.commit()
  2206. tests.add_content_git_repo(
  2207. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  2208. tests.add_readme_git_repo(
  2209. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  2210. tests.add_commit_git_repo(
  2211. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'),
  2212. ncommits=10)
  2213. output = self.app.get('/fork/pingou/test3/')
  2214. self.assertEqual(output.status_code, 200)
  2215. output_text = output.get_data(as_text=True)
  2216. self.assertNotIn('<p>This repo is brand new!</p>', output_text)
  2217. self.assertIn('Forked from', output_text)
  2218. '''
  2219. def test_view_commits(self):
  2220. """Test the view_commits endpoint."""
  2221. output = self.app.get("/foo/commits")
  2222. # No project registered in the DB
  2223. self.assertEqual(output.status_code, 404)
  2224. tests.create_projects(self.session)
  2225. output = self.app.get("/test/commits")
  2226. # No git repo associated
  2227. self.assertEqual(output.status_code, 404)
  2228. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2229. output = self.app.get("/test/commits")
  2230. self.assertEqual(output.status_code, 200)
  2231. output_text = output.get_data(as_text=True)
  2232. self.assertIn("<p>This repo is brand new!</p>", output_text)
  2233. self.assertIn("<title>Commits - test - Pagure</title>", output_text)
  2234. # Add some content to the git repo
  2235. tests.add_content_git_repo(
  2236. os.path.join(self.path, "repos", "test.git")
  2237. )
  2238. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2239. output = self.app.get("/test/commits")
  2240. self.assertEqual(output.status_code, 200)
  2241. output_text = output.get_data(as_text=True)
  2242. self.assertNotIn("<p>This repo is brand new!</p>", output_text)
  2243. self.assertNotIn("Forked from", output_text)
  2244. self.assertIn("<title>Commits - test - Pagure</title>", output_text)
  2245. output = self.app.get("/test/commits/master")
  2246. self.assertEqual(output.status_code, 200)
  2247. output_text = output.get_data(as_text=True)
  2248. self.assertNotIn("<p>This repo is brand new!</p>", output_text)
  2249. self.assertNotIn("Forked from", output_text)
  2250. self.assertIn("<title>Commits - test - Pagure</title>", output_text)
  2251. # Turn that repo into a fork
  2252. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  2253. repo.parent_id = 2
  2254. repo.is_fork = True
  2255. self.session.add(repo)
  2256. self.session.commit()
  2257. # View the repo in the UI
  2258. output = self.app.get("/test/commits")
  2259. self.assertEqual(output.status_code, 404)
  2260. # Add some content to the git repo
  2261. tests.add_content_git_repo(
  2262. os.path.join(self.path, "repos", "forks", "pingou", "test.git")
  2263. )
  2264. tests.add_readme_git_repo(
  2265. os.path.join(self.path, "repos", "forks", "pingou", "test.git")
  2266. )
  2267. output = self.app.get("/fork/pingou/test/commits?page=abc")
  2268. self.assertEqual(output.status_code, 200)
  2269. output_text = output.get_data(as_text=True)
  2270. self.assertNotIn("<p>This repo is brand new!</p>", output_text)
  2271. self.assertIn("<title>Commits - test - Pagure</title>", output_text)
  2272. self.assertIn("Forked from", output_text)
  2273. # Add a fork of a fork
  2274. item = pagure.lib.model.Project(
  2275. user_id=1, # pingou
  2276. name="test3",
  2277. description="test project #3",
  2278. is_fork=True,
  2279. parent_id=1,
  2280. hook_token="aaabbbooo",
  2281. )
  2282. self.session.add(item)
  2283. self.session.commit()
  2284. tests.add_content_git_repo(
  2285. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  2286. )
  2287. tests.add_readme_git_repo(
  2288. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  2289. )
  2290. tests.add_commit_git_repo(
  2291. os.path.join(self.path, "repos", "forks", "pingou", "test3.git"),
  2292. ncommits=10,
  2293. )
  2294. # list is empty
  2295. output = self.app.get("/fork/pingou/test3/commits/fobranch")
  2296. self.assertEqual(output.status_code, 200)
  2297. output_text = output.get_data(as_text=True)
  2298. self.assertIn(
  2299. '<div class="list-group my-2">\n\n\n </div>', output_text
  2300. )
  2301. self.assertIn(
  2302. 'Commits <span class="badge badge-secondary"> 0</span>',
  2303. output_text,
  2304. )
  2305. output = self.app.get("/fork/pingou/test3/commits")
  2306. self.assertEqual(output.status_code, 200)
  2307. output_text = output.get_data(as_text=True)
  2308. self.assertNotIn("<p>This repo is brand new!</p>", output_text)
  2309. self.assertIn("<title>Commits - test3 - Pagure</title>", output_text)
  2310. self.assertIn("Forked from", output_text)
  2311. def test_view_commits_from_tag(self):
  2312. """Test the view_commits endpoint given a tag."""
  2313. tests.create_projects(self.session)
  2314. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2315. # Add a README to the git repo - First commit
  2316. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2317. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  2318. first_commit = repo.revparse_single("HEAD")
  2319. tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
  2320. repo.create_tag(
  2321. "0.0.1",
  2322. first_commit.oid.hex,
  2323. pygit2.GIT_OBJ_COMMIT,
  2324. tagger,
  2325. "Release 0.0.1",
  2326. )
  2327. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2328. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  2329. latest_commit = repo.revparse_single("HEAD")
  2330. output = self.app.get("/test/commits/0.0.1")
  2331. self.assertEqual(output.status_code, 200)
  2332. output_text = output.get_data(as_text=True)
  2333. self.assertIn(first_commit.oid.hex, output_text)
  2334. self.assertNotIn(latest_commit.oid.hex, output_text)
  2335. self.assertIn("<title>Commits - test - Pagure</title>", output_text)
  2336. self.assertEqual(output_text.count('<span id="commit-actions">'), 1)
  2337. def test_view_commits_from_blob(self):
  2338. """Test the view_commits endpoint given a blob."""
  2339. tests.create_projects(self.session)
  2340. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2341. tests.add_content_to_git(os.path.join(self.path, "repos", "test.git"))
  2342. # Retrieve the blob of the `sources` file in head
  2343. repo_obj = pygit2.Repository(
  2344. os.path.join(self.path, "repos", "test.git")
  2345. )
  2346. commit = repo_obj[repo_obj.head.target]
  2347. content = get_file_in_tree(
  2348. repo_obj, commit.tree, ["sources"], bail_on_tree=True
  2349. )
  2350. output = self.app.get("/test/commits/%s" % content.oid.hex)
  2351. self.assertEqual(output.status_code, 404)
  2352. output_text = output.get_data(as_text=True)
  2353. self.assertIn("Invalid branch/identifier provided", output_text)
  2354. def test_view_commit_from_tag(self):
  2355. """Test the view_commit endpoint given a tag."""
  2356. tests.create_projects(self.session)
  2357. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2358. # Add a README to the git repo - First commit
  2359. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2360. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  2361. first_commit = repo.revparse_single("HEAD")
  2362. tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
  2363. repo.create_tag(
  2364. "0.0.1",
  2365. first_commit.oid.hex,
  2366. pygit2.GIT_OBJ_COMMIT,
  2367. tagger,
  2368. "Release 0.0.1",
  2369. )
  2370. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2371. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  2372. project = pagure.lib.query.get_authorized_project(self.session, "test")
  2373. tags = pagure.lib.git.get_git_tags_objects(project)
  2374. tag_id = tags[0]["object"].oid
  2375. commit_id = tags[0]["object"].peel(pygit2.Commit).hex
  2376. output = self.app.get("/test/c/%s" % tag_id)
  2377. self.assertEqual(output.status_code, 302)
  2378. output = self.app.get("/test/c/%s" % tag_id, follow_redirects=True)
  2379. self.assertEqual(output.status_code, 200)
  2380. output_text = output.get_data(as_text=True)
  2381. self.assertIn(first_commit.oid.hex, output_text)
  2382. self.assertIn(
  2383. "<title>Commit - test - %s - Pagure</title>" % commit_id,
  2384. output_text,
  2385. )
  2386. def test_compare_commits(self):
  2387. """Test the compare_commits endpoint."""
  2388. # First two commits comparison
  2389. def compare_first_two(c1, c2):
  2390. # View commits comparison
  2391. output = self.app.get("/test/c/%s..%s" % (c2.oid.hex, c1.oid.hex))
  2392. self.assertEqual(output.status_code, 200)
  2393. output_text = output.get_data(as_text=True)
  2394. self.assertIn(
  2395. "<title>Diff from %s to %s - test\n - Pagure</title>"
  2396. % (c2.oid.hex, c1.oid.hex),
  2397. output_text,
  2398. )
  2399. self.assertIn(
  2400. ' <span class="badge-light border border-secondary badge">%s</span>\n ..\n <span class="badge-light border border-secondary badge">%s</span>\n'
  2401. % (c2.oid.hex, c1.oid.hex),
  2402. output_text,
  2403. )
  2404. self.assertNotIn('id="show_hidden_commits"', output_text)
  2405. self.assertIn(
  2406. '<pre class="alert-danger"><code>- Row 0</code></pre>',
  2407. output_text,
  2408. )
  2409. # View inverse commits comparison
  2410. output = self.app.get("/test/c/%s..%s" % (c1.oid.hex, c2.oid.hex))
  2411. self.assertEqual(output.status_code, 200)
  2412. output_text = output.get_data(as_text=True)
  2413. self.assertIn(
  2414. "<title>Diff from %s to %s - test\n - Pagure</title>"
  2415. % (c1.oid.hex, c2.oid.hex),
  2416. output_text,
  2417. )
  2418. self.assertNotIn('id="show_hidden_commits"', output_text)
  2419. self.assertIn(
  2420. ' <span class="badge-light border border-secondary badge">%s</span>\n ..\n <span class="badge-light border border-secondary badge">%s</span>\n'
  2421. % (c1.oid.hex, c2.oid.hex),
  2422. output_text,
  2423. )
  2424. self.assertIn(
  2425. '<pre class="alert-success"><code>+ Row 0</code></pre>',
  2426. output_text,
  2427. )
  2428. def compare_all(c1, c3):
  2429. # View commits comparison
  2430. output = self.app.get("/test/c/%s..%s" % (c1.oid.hex, c3.oid.hex))
  2431. self.assertEqual(output.status_code, 200)
  2432. output_text = output.get_data(as_text=True)
  2433. self.assertIn(
  2434. "<title>Diff from %s to %s - test\n - Pagure</title>"
  2435. % (c1.oid.hex, c3.oid.hex),
  2436. output_text,
  2437. )
  2438. self.assertIn(
  2439. ' <span class="badge-light border border-secondary badge">%s</span>\n ..\n <span class="badge-light border border-secondary badge">%s</span>\n'
  2440. % (c1.oid.hex, c3.oid.hex),
  2441. output_text,
  2442. )
  2443. self.assertIn(
  2444. '<pre class="alert-success"><code>+ Row 0</code></pre>',
  2445. output_text,
  2446. )
  2447. self.assertEqual(
  2448. output_text.count(
  2449. '<pre class="alert-success"><code>+ Row 0</code></pre>'
  2450. ),
  2451. 2,
  2452. )
  2453. self.assertIn('<a class="pointer">1 more commits...', output_text)
  2454. self.assertIn(
  2455. 'title="View file as of 4829cfa">Šource</a>', output_text
  2456. )
  2457. self.assertIn(
  2458. '<div class="btn btn-outline-success disabled opacity-100 border-0 font-weight-bold">\n'
  2459. " file added\n",
  2460. output_text,
  2461. )
  2462. # View inverse commits comparison
  2463. output = self.app.get("/test/c/%s..%s" % (c3.oid.hex, c1.oid.hex))
  2464. self.assertEqual(output.status_code, 200)
  2465. output_text = output.get_data(as_text=True)
  2466. self.assertIn(
  2467. "<title>Diff from %s to %s - test\n - Pagure</title>"
  2468. % (c3.oid.hex, c1.oid.hex),
  2469. output_text,
  2470. )
  2471. self.assertIn(
  2472. ' <span class="badge-light border border-secondary badge">%s</span>\n ..\n <span class="badge-light border border-secondary badge">%s</span>\n'
  2473. % (c3.oid.hex, c1.oid.hex),
  2474. output_text,
  2475. )
  2476. self.assertIn(
  2477. '<pre class="text-muted"><code>@@ -1,2 +1,1 @@</code></pre>',
  2478. output_text,
  2479. )
  2480. self.assertIn(
  2481. '<pre class="alert-danger"><code>- Row 0</code></pre>',
  2482. output_text,
  2483. )
  2484. self.assertIn('<a class="pointer">1 more commits...', output_text)
  2485. self.assertIn(
  2486. 'title="View file as of 0000000">Šource</a>', output_text
  2487. )
  2488. self.assertIn(
  2489. '<div class="btn btn-outline-danger disabled opacity-100 border-0 font-weight-bold">\n'
  2490. " file removed\n",
  2491. output_text,
  2492. )
  2493. def compare_with_symlink(c3, c4):
  2494. # View comparison of commits with symlink
  2495. # we only test that the patch itself renders correctly,
  2496. # the rest of the logic is already tested in the other functions
  2497. output = self.app.get("/test/c/%s..%s" % (c3.oid.hex, c4.oid.hex))
  2498. self.assertEqual(output.status_code, 200)
  2499. output_text = output.get_data(as_text=True)
  2500. print(output_text)
  2501. self.assertIn(
  2502. "<title>Diff from %s to %s - test\n - Pagure</title>"
  2503. % (c3.oid.hex, c4.oid.hex),
  2504. output_text,
  2505. )
  2506. self.assertIn(
  2507. '<pre class="alert-success"><code>+ Source </code></pre>',
  2508. output_text,
  2509. )
  2510. output = self.app.get("/foo/bar")
  2511. # No project registered in the DB
  2512. self.assertEqual(output.status_code, 404)
  2513. tests.create_projects(self.session)
  2514. output = self.app.get("/test/bar")
  2515. # No git repo associated
  2516. self.assertEqual(output.status_code, 404)
  2517. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2518. output = self.app.get("/test/bar")
  2519. self.assertEqual(output.status_code, 404)
  2520. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  2521. # Add one commit to git repo
  2522. tests.add_commit_git_repo(
  2523. os.path.join(self.path, "repos", "test.git"), ncommits=1
  2524. )
  2525. c1 = repo.revparse_single("HEAD")
  2526. # Add another commit to git repo
  2527. tests.add_commit_git_repo(
  2528. os.path.join(self.path, "repos", "test.git"), ncommits=1
  2529. )
  2530. c2 = repo.revparse_single("HEAD")
  2531. # Add one more commit to git repo
  2532. tests.add_commit_git_repo(
  2533. os.path.join(self.path, "repos", "test.git"),
  2534. ncommits=1,
  2535. filename="Šource",
  2536. )
  2537. c3 = repo.revparse_single("HEAD")
  2538. tests.add_commit_git_repo(
  2539. os.path.join(self.path, "repos", "test.git"),
  2540. ncommits=1,
  2541. filename="Source-sl",
  2542. symlink_to="Source",
  2543. )
  2544. c4 = repo.revparse_single("HEAD")
  2545. compare_first_two(c1, c2)
  2546. compare_all(c1, c3)
  2547. compare_with_symlink(c3, c4)
  2548. user = tests.FakeUser()
  2549. # Set user logged in
  2550. with tests.user_set(self.app.application, user):
  2551. compare_first_two(c1, c2)
  2552. compare_all(c1, c3)
  2553. compare_with_symlink(c3, c4)
  2554. def test_view_file(self):
  2555. """Test the view_file endpoint."""
  2556. output = self.app.get("/foo/blob/foo/f/sources")
  2557. # No project registered in the DB
  2558. self.assertEqual(output.status_code, 404)
  2559. tests.create_projects(self.session)
  2560. output = self.app.get("/test/blob/foo/f/sources")
  2561. # No git repo associated
  2562. self.assertEqual(output.status_code, 404)
  2563. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2564. output = self.app.get("/test/blob/foo/f/sources")
  2565. self.assertEqual(output.status_code, 404)
  2566. # Add some content to the git repo
  2567. tests.add_content_git_repo(
  2568. os.path.join(self.path, "repos", "test.git")
  2569. )
  2570. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2571. tests.add_binary_git_repo(
  2572. os.path.join(self.path, "repos", "test.git"), "test.jpg"
  2573. )
  2574. tests.add_binary_git_repo(
  2575. os.path.join(self.path, "repos", "test.git"), "test_binary"
  2576. )
  2577. output = self.app.get("/test/blob/master/foofile")
  2578. self.assertEqual(output.status_code, 404)
  2579. # View in a branch
  2580. output = self.app.get("/test/blob/master/f/sources")
  2581. self.assertEqual(output.status_code, 200)
  2582. output_text = output.get_data(as_text=True)
  2583. self.assertIn(
  2584. '<pre class="syntaxhighlightblock">'
  2585. '<code class="lang-plaintext">foo\n bar</code></pre>',
  2586. output_text,
  2587. )
  2588. # Empty files should also be displayed
  2589. tests.add_content_to_git(
  2590. os.path.join(self.path, "repos", "test.git"),
  2591. filename="emptyfile.md",
  2592. content="",
  2593. )
  2594. output = self.app.get("/test/blob/master/f/emptyfile.md")
  2595. self.assertEqual(output.status_code, 200)
  2596. output_text = output.get_data(as_text=True)
  2597. self.assertIn(
  2598. '<a class="btn btn-secondary btn-sm" '
  2599. 'href="/test/raw/master/f/emptyfile.md" '
  2600. 'title="View as raw">Raw</a>',
  2601. output_text,
  2602. )
  2603. self.assertIn(
  2604. '<div class="m-2">\n' " \n </div>", output_text
  2605. )
  2606. # View what's supposed to be an image
  2607. output = self.app.get("/test/blob/master/f/test.jpg")
  2608. self.assertEqual(output.status_code, 200)
  2609. self.assertNotIn(b"<html", output.data)
  2610. # View by commit id
  2611. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  2612. commit = repo.revparse_single("HEAD")
  2613. output = self.app.get("/test/blob/%s/f/test.jpg" % commit.oid.hex)
  2614. self.assertEqual(output.status_code, 200)
  2615. self.assertNotIn(b"<html", output.data)
  2616. # View by image name -- somehow we support this
  2617. output = self.app.get("/test/blob/master/f/test.jpg")
  2618. self.assertEqual(output.status_code, 200)
  2619. self.assertNotIn(b"<html", output.data)
  2620. # View binary file
  2621. output = self.app.get("/test/blob/master/f/test_binary")
  2622. self.assertEqual(output.status_code, 200)
  2623. self.assertNotIn(b"<html", output.data)
  2624. # View folder
  2625. output = self.app.get("/test/blob/master/f/folder1")
  2626. self.assertEqual(output.status_code, 200)
  2627. output_text = output.get_data(as_text=True)
  2628. self.assertIn(
  2629. '<li class="active breadcrumb-item">\n '
  2630. '<span class="fa fa-folder" data-glyph="">\n '
  2631. "</span>&nbsp; folder1\n </li>",
  2632. output_text,
  2633. )
  2634. self.assertIn("<title>Tree - test - Pagure</title>", output_text)
  2635. self.assertIn(
  2636. '<a href="/test/blob/master/f/folder1/folder2">', output_text
  2637. )
  2638. # Verify the nav links correctly when viewing a nested folder/file.
  2639. output = self.app.get("/test/blob/master/f/folder1/folder2/file")
  2640. self.assertEqual(output.status_code, 200)
  2641. output_text = output.get_data(as_text=True)
  2642. self.assertIn(
  2643. '<li class="breadcrumb-item"><a href="/test/blob/master/f/folder1/folder2">'
  2644. '\n <span class="fa fa-folder"></span>&nbsp; folder2</a>\n'
  2645. " </li>",
  2646. output_text,
  2647. )
  2648. # View by image name -- with a non-existant file
  2649. output = self.app.get("/test/blob/sources/f/testfoo.jpg")
  2650. self.assertEqual(output.status_code, 404)
  2651. output = self.app.get("/test/blob/master/f/folder1/testfoo.jpg")
  2652. self.assertEqual(output.status_code, 404)
  2653. # View file with a non-ascii name
  2654. tests.add_commit_git_repo(
  2655. os.path.join(self.path, "repos", "test.git"),
  2656. ncommits=1,
  2657. filename="Šource",
  2658. )
  2659. output = self.app.get("/test/blob/master/f/Šource")
  2660. self.assertEqual(output.status_code, 200)
  2661. output_text = output.get_data(as_text=True)
  2662. self.assertEqual(
  2663. output.headers["Content-Type"].lower(), "text/html; charset=utf-8"
  2664. )
  2665. self.assertIn("</span>&nbsp; Šource", output_text)
  2666. self.assertIn(
  2667. '<pre class="syntaxhighlightblock">'
  2668. '<code class="lang-plaintext">Row 0\n</code></pre>',
  2669. output_text,
  2670. )
  2671. # Add a fork of a fork
  2672. item = pagure.lib.model.Project(
  2673. user_id=1, # pingou
  2674. name="test3",
  2675. description="test project #3",
  2676. is_fork=True,
  2677. parent_id=1,
  2678. hook_token="aaabbbppp",
  2679. )
  2680. self.session.add(item)
  2681. self.session.commit()
  2682. tests.add_content_git_repo(
  2683. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  2684. )
  2685. tests.add_readme_git_repo(
  2686. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  2687. )
  2688. tests.add_commit_git_repo(
  2689. os.path.join(self.path, "repos", "forks", "pingou", "test3.git"),
  2690. ncommits=10,
  2691. )
  2692. # Verify the nav links correctly when viewing a file/folder in a fork.
  2693. output = self.app.get(
  2694. "/fork/pingou/test3/blob/master/f/folder1/folder2/file"
  2695. )
  2696. self.assertEqual(output.status_code, 200)
  2697. output_text = output.get_data(as_text=True)
  2698. self.assertIn(
  2699. '<li class="breadcrumb-item">'
  2700. '<a href="/fork/pingou/test3/blob/master/f/folder1/folder2">'
  2701. '\n <span class="fa fa-folder"></span>'
  2702. "&nbsp; folder2</a>\n </li>",
  2703. output_text,
  2704. )
  2705. output = self.app.get("/fork/pingou/test3/blob/master/f/sources")
  2706. self.assertEqual(output.status_code, 200)
  2707. output_text = output.get_data(as_text=True)
  2708. self.assertIn(
  2709. '<pre class="syntaxhighlightblock">'
  2710. '<code class="lang-plaintext">foo\n barRow 0\n'
  2711. "Row 1\nRow 2\nRow 3\nRow 4\nRow 5\nRow 6\nRow 7\nRow 8\n"
  2712. "Row 9\n</code></pre>",
  2713. output_text,
  2714. )
  2715. @patch(
  2716. "pagure.lib.encoding_utils.decode",
  2717. MagicMock(side_effect=pagure.exceptions.PagureException),
  2718. )
  2719. def test_view_file_with_wrong_encoding(self):
  2720. """Test the view_file endpoint."""
  2721. tests.create_projects(self.session)
  2722. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2723. # Add some content to the git repo
  2724. tests.add_content_git_repo(
  2725. os.path.join(self.path, "repos", "test.git")
  2726. )
  2727. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2728. tests.add_binary_git_repo(
  2729. os.path.join(self.path, "repos", "test.git"), "test.jpg"
  2730. )
  2731. tests.add_binary_git_repo(
  2732. os.path.join(self.path, "repos", "test.git"), "test_binary"
  2733. )
  2734. # View file
  2735. output = self.app.get("/test/blob/master/f/sources")
  2736. self.assertEqual(output.status_code, 200)
  2737. output_text = output.get_data(as_text=True)
  2738. self.assertEqual("foo\n bar", output_text)
  2739. def test_view_raw_file(self):
  2740. """Test the view_raw_file endpoint."""
  2741. output = self.app.get("/foo/raw/foo/sources")
  2742. # No project registered in the DB
  2743. self.assertEqual(output.status_code, 404)
  2744. tests.create_projects(self.session)
  2745. output = self.app.get("/test/raw/foo/sources")
  2746. # No git repo associated
  2747. self.assertEqual(output.status_code, 404)
  2748. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2749. output = self.app.get("/test/raw/foo/sources")
  2750. self.assertEqual(output.status_code, 404)
  2751. # Add some content to the git repo
  2752. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2753. # View first commit
  2754. output = self.app.get("/test/raw/master")
  2755. self.assertEqual(output.status_code, 200)
  2756. output_text = output.get_data(as_text=True)
  2757. self.assertEqual(
  2758. output.headers["Content-Type"].lower(),
  2759. "text/plain; charset=ascii",
  2760. )
  2761. self.assertIn(":Author: Pierre-Yves Chibon", output_text)
  2762. # Add some more content to the repo
  2763. tests.add_content_git_repo(
  2764. os.path.join(self.path, "repos", "test.git")
  2765. )
  2766. tests.add_binary_git_repo(
  2767. os.path.join(self.path, "repos", "test.git"), "test.jpg"
  2768. )
  2769. tests.add_binary_git_repo(
  2770. os.path.join(self.path, "repos", "test.git"), "test_binary"
  2771. )
  2772. output = self.app.get("/test/raw/master/f/foofile")
  2773. self.assertEqual(output.status_code, 404)
  2774. # View in a branch
  2775. output = self.app.get("/test/raw/master/f/sources")
  2776. self.assertEqual(
  2777. output.headers["Content-Type"].lower(),
  2778. "text/plain; charset=ascii",
  2779. )
  2780. self.assertEqual(output.status_code, 200)
  2781. output_text = output.get_data(as_text=True)
  2782. self.assertIn("foo\n bar", output_text)
  2783. # View what's supposed to be an image
  2784. output = self.app.get("/test/raw/master/f/test.jpg")
  2785. self.assertEqual(output.status_code, 200)
  2786. output_text = output.get_data()
  2787. self.assertTrue(output_text.startswith(b"\x00\x00\x01\x00"))
  2788. # View by commit id
  2789. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  2790. commit = repo.revparse_single("HEAD")
  2791. output = self.app.get("/test/raw/%s/f/test.jpg" % commit.oid.hex)
  2792. self.assertEqual(output.status_code, 200)
  2793. output_text = output.get_data()
  2794. self.assertTrue(output_text.startswith(b"\x00\x00\x01\x00"))
  2795. # View by image name -- somehow we support this
  2796. output = self.app.get("/test/raw/master/f/test.jpg")
  2797. self.assertEqual(output.status_code, 200)
  2798. output_text = output.get_data()
  2799. self.assertTrue(output_text.startswith(b"\x00\x00\x01\x00"))
  2800. # View binary file
  2801. output = self.app.get("/test/raw/master/f/test_binary")
  2802. self.assertEqual(output.status_code, 200)
  2803. output_text = output.get_data()
  2804. self.assertEqual(
  2805. output.headers["Content-Type"].lower(), "application/octet-stream"
  2806. )
  2807. self.assertTrue(output_text.startswith(b"\x00\x00\x01\x00"))
  2808. # View folder
  2809. output = self.app.get("/test/raw/master/f/folder1")
  2810. self.assertEqual(output.status_code, 404)
  2811. # View by image name -- with a non-existant file
  2812. output = self.app.get("/test/raw/master/f/testfoo.jpg")
  2813. self.assertEqual(output.status_code, 404)
  2814. output = self.app.get("/test/raw/master/f/folder1/testfoo.jpg")
  2815. self.assertEqual(output.status_code, 404)
  2816. output = self.app.get("/test/raw/master/f/")
  2817. self.assertEqual(output.status_code, 404)
  2818. output = self.app.get("/test/raw/master")
  2819. self.assertEqual(output.status_code, 200)
  2820. output_text = output.get_data(as_text=True)
  2821. self.assertEqual(
  2822. output.headers["Content-Type"].lower(),
  2823. "text/plain; charset=ascii",
  2824. )
  2825. self.assertTrue(
  2826. output_text.startswith("diff --git a/test_binary b/test_binary\n")
  2827. )
  2828. output = self.app.get("/test/raw/%s" % commit.oid.hex)
  2829. self.assertEqual(output.status_code, 200)
  2830. output_text = output.get_data(as_text=True)
  2831. self.assertTrue(
  2832. output_text.startswith("diff --git a/test_binary b/test_binary\n")
  2833. )
  2834. # Add a fork of a fork
  2835. item = pagure.lib.model.Project(
  2836. user_id=1, # pingou
  2837. name="test3",
  2838. description="test project #3",
  2839. is_fork=True,
  2840. parent_id=1,
  2841. hook_token="aaabbbqqq",
  2842. )
  2843. self.session.add(item)
  2844. self.session.commit()
  2845. tests.add_content_git_repo(
  2846. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  2847. )
  2848. tests.add_readme_git_repo(
  2849. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  2850. )
  2851. tests.add_commit_git_repo(
  2852. os.path.join(self.path, "repos", "forks", "pingou", "test3.git"),
  2853. ncommits=10,
  2854. )
  2855. output = self.app.get("/fork/pingou/test3/raw/master/f/sources")
  2856. self.assertEqual(output.status_code, 200)
  2857. output_text = output.get_data(as_text=True)
  2858. self.assertEqual(
  2859. output.headers["Content-Type"].lower(),
  2860. "text/plain; charset=ascii",
  2861. )
  2862. self.assertIn("foo\n bar", output_text)
  2863. def test_view_commit(self):
  2864. """Test the view_commit endpoint."""
  2865. output = self.app.get("/foo/c/bar")
  2866. # No project registered in the DB
  2867. self.assertEqual(output.status_code, 404)
  2868. tests.create_projects(self.session)
  2869. output = self.app.get("/test/c/bar")
  2870. # No git repo associated
  2871. self.assertEqual(output.status_code, 404)
  2872. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  2873. output = self.app.get("/test/c/bar")
  2874. self.assertEqual(output.status_code, 404)
  2875. # Add a README to the git repo - First commit
  2876. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  2877. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  2878. commit = repo.revparse_single("HEAD")
  2879. # View first commit
  2880. output = self.app.get("/test/c/%s" % commit.oid.hex)
  2881. self.assertEqual(output.status_code, 200)
  2882. output_text = output.get_data(as_text=True)
  2883. self.assertIn("#commit-overview-collapse", output_text)
  2884. self.assertIn("Merged by Alice Author", output_text)
  2885. self.assertIn("Committed by Cecil Committer", output_text)
  2886. self.assertIn(
  2887. '<div class="btn btn-outline-success disabled opacity-100 '
  2888. 'border-0 font-weight-bold">file added</div>',
  2889. output_text,
  2890. )
  2891. # View first commit - with the old URL scheme disabled - default
  2892. output = self.app.get(
  2893. "/test/%s" % commit.oid.hex, follow_redirects=True
  2894. )
  2895. self.assertEqual(output.status_code, 404)
  2896. output_text = output.get_data(as_text=True)
  2897. self.assertIn("<p>Project not found</p>", output_text)
  2898. # Add some content to the git repo
  2899. tests.add_content_git_repo(
  2900. os.path.join(self.path, "repos", "test.git")
  2901. )
  2902. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  2903. commit = repo.revparse_single("HEAD")
  2904. # Add a symlink to test that it displays correctly
  2905. tests.add_commit_git_repo(
  2906. os.path.join(self.path, "repos", "test.git"),
  2907. ncommits=1,
  2908. filename="sources-sl",
  2909. symlink_to="sources",
  2910. )
  2911. commit_sl = repo.revparse_single("HEAD")
  2912. # View another commit
  2913. output = self.app.get("/test/c/%s" % commit.oid.hex)
  2914. self.assertEqual(output.status_code, 200)
  2915. output_text = output.get_data(as_text=True)
  2916. self.assertIn("#commit-overview-collapse", output_text)
  2917. self.assertIn("Authored by Alice Author", output_text)
  2918. self.assertIn("Committed by Cecil Committer", output_text)
  2919. # Make sure that diff containing symlink displays the header correctly
  2920. output = self.app.get("/test/c/%s" % commit_sl.oid.hex)
  2921. self.assertEqual(output.status_code, 200)
  2922. output_text = output.get_data(as_text=True)
  2923. # check the link to the file
  2924. self.assertIn(">sources-sl</a>", output_text)
  2925. # check that the header contains "file added" and a +1 for one added line
  2926. self.assertIn(">file added</span>", output_text)
  2927. self.assertIn(">+1</span>", output_text)
  2928. # View the commit when branch name is provided
  2929. output = self.app.get("/test/c/%s?branch=master" % commit.oid.hex)
  2930. self.assertEqual(output.status_code, 200)
  2931. output_text = output.get_data(as_text=True)
  2932. self.assertIn(
  2933. '<a class=\n "nav-link nowrap\n active"\n '
  2934. 'href="/test/commits/master">\n <i class="fa fa-list-alt '
  2935. 'text-muted fa-fw" data-glyph="spreadsheet"></i>&nbsp;Commits'
  2936. "\n </a>",
  2937. output_text,
  2938. )
  2939. # View the commit when branch name is wrong, show the commit
  2940. output = self.app.get("/test/c/%s?branch=abcxyz" % commit.oid.hex)
  2941. self.assertEqual(output.status_code, 200)
  2942. output_text = output.get_data(as_text=True)
  2943. self.assertIn(
  2944. '<a class=\n "nav-link nowrap\n active"\n '
  2945. 'href="/test/commits">\n <i class="fa fa-list-alt '
  2946. 'text-muted fa-fw" data-glyph="spreadsheet"></i>&nbsp;Commits'
  2947. "\n </a>\n",
  2948. output_text,
  2949. )
  2950. # Add a fork of a fork
  2951. item = pagure.lib.model.Project(
  2952. user_id=1, # pingou
  2953. name="test3",
  2954. description="test project #3",
  2955. is_fork=True,
  2956. parent_id=1,
  2957. hook_token="aaabbbkkk",
  2958. )
  2959. self.session.add(item)
  2960. self.session.commit()
  2961. forkedgit = os.path.join(
  2962. self.path, "repos", "forks", "pingou", "test3.git"
  2963. )
  2964. tests.add_content_git_repo(forkedgit)
  2965. tests.add_readme_git_repo(forkedgit)
  2966. repo = pygit2.Repository(forkedgit)
  2967. commit = repo.revparse_single("HEAD")
  2968. # Commit does not exist in anothe repo :)
  2969. output = self.app.get("/test/c/%s" % commit.oid.hex)
  2970. self.assertEqual(output.status_code, 404)
  2971. # View commit of fork
  2972. output = self.app.get("/fork/pingou/test3/c/%s" % commit.oid.hex)
  2973. self.assertEqual(output.status_code, 200)
  2974. output_text = output.get_data(as_text=True)
  2975. self.assertIn("#commit-overview-collapse", output_text)
  2976. self.assertIn("Authored by Alice Author", output_text)
  2977. self.assertIn("Committed by Cecil Committer", output_text)
  2978. # Try the old URL scheme with a short hash
  2979. output = self.app.get(
  2980. "/fork/pingou/test3/%s" % commit.oid.hex[:10],
  2981. follow_redirects=True,
  2982. )
  2983. self.assertEqual(output.status_code, 404)
  2984. output_text = output.get_data(as_text=True)
  2985. self.assertIn("<p>Project not found</p>", output_text)
  2986. # View the commit of the fork when branch name is provided
  2987. output = self.app.get(
  2988. "/fork/pingou/test3/c/%s?branch=master" % commit.oid.hex
  2989. )
  2990. self.assertEqual(output.status_code, 200)
  2991. output_text = output.get_data(as_text=True)
  2992. self.assertIn(
  2993. '<a class=\n "nav-link nowrap\n active"\n '
  2994. 'href="/fork/pingou/test3/commits/master">\n '
  2995. '<i class="fa fa-list-alt '
  2996. 'text-muted fa-fw" data-glyph="spreadsheet"></i>&nbsp;Commits'
  2997. "\n </a>\n",
  2998. output_text,
  2999. )
  3000. # View the commit of the fork when branch name is wrong
  3001. output = self.app.get(
  3002. "/fork/pingou/test3/c/%s?branch=abcxyz" % commit.oid.hex
  3003. )
  3004. self.assertEqual(output.status_code, 200)
  3005. output_text = output.get_data(as_text=True)
  3006. self.assertIn(
  3007. '<a class=\n "nav-link nowrap\n active"\n '
  3008. 'href="/fork/pingou/test3/commits">\n <i class="fa fa-list-alt '
  3009. 'text-muted fa-fw" data-glyph="spreadsheet"></i>&nbsp;Commits'
  3010. "\n </a>",
  3011. output_text,
  3012. )
  3013. def test_view_commit_with_full_link(self):
  3014. """Test the view_commit endpoint when the commit message includes
  3015. an url."""
  3016. tests.create_projects(self.session)
  3017. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  3018. folder = os.path.join(self.path, "repos", "test.git")
  3019. # Add a README to the git repo - First commit
  3020. tests.add_readme_git_repo(folder)
  3021. tests.create_projects_git(folder, bare=True)
  3022. # Add a commit with an url in the commit message
  3023. tests.add_content_to_git(
  3024. folder,
  3025. branch="master",
  3026. filename="sources",
  3027. content="foo",
  3028. message="Test commit message\n\n"
  3029. "Fixes http://example.com/pagure/issue/2",
  3030. )
  3031. # Add a README to the git repo - First commit
  3032. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  3033. commit = repo.revparse_single("HEAD")
  3034. # View first commit
  3035. output = self.app.get("/test/c/%s" % commit.oid.hex)
  3036. self.assertEqual(output.status_code, 200)
  3037. output_text = output.get_data(as_text=True)
  3038. self.assertIn("#commit-overview-collapse", output_text)
  3039. self.assertIn(
  3040. '<pre class="commit_message_body">\n '
  3041. "Test commit message\n \n "
  3042. 'Fixes <a href="http://example.com/pagure/issue/2" '
  3043. 'rel="nofollow">http://example.com/pagure/issue/2</a>\n '
  3044. "</pre>",
  3045. output_text,
  3046. )
  3047. self.assertIn(
  3048. '<div class="btn btn-outline-success disabled opacity-100 '
  3049. 'border-0 font-weight-bold">file added</div>',
  3050. output_text,
  3051. )
  3052. def test_view_commit_with_short_link(self):
  3053. """Test the view_commit endpoint when the commit message includes
  3054. an url."""
  3055. tests.create_projects(self.session)
  3056. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  3057. folder = os.path.join(self.path, "repos", "test.git")
  3058. # Add a README to the git repo - First commit
  3059. tests.add_readme_git_repo(folder)
  3060. tests.create_projects_git(folder, bare=True)
  3061. # Add a commit with an url in the commit message
  3062. tests.add_content_to_git(
  3063. folder,
  3064. branch="master",
  3065. filename="sources",
  3066. content="foo",
  3067. message="Test commit message\n\nFixes #2",
  3068. )
  3069. # Add a README to the git repo - First commit
  3070. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  3071. commit = repo.revparse_single("HEAD")
  3072. # View first commit
  3073. output = self.app.get("/test/c/%s" % commit.oid.hex)
  3074. self.assertEqual(output.status_code, 200)
  3075. output_text = output.get_data(as_text=True)
  3076. self.assertIn("#commit-overview-collapse", output_text)
  3077. self.assertIn(
  3078. '<pre class="commit_message_body">\n '
  3079. "Test commit message\n \n "
  3080. "Fixes #2\n </pre>",
  3081. output_text,
  3082. )
  3083. self.assertIn(
  3084. '<div class="btn btn-outline-success disabled opacity-100 '
  3085. 'border-0 font-weight-bold">file added</div>',
  3086. output_text,
  3087. )
  3088. def test_view_commit_patch(self):
  3089. """Test the view_commit_patch endpoint."""
  3090. # No project registered in the DB
  3091. output = self.app.get("/foo/c/bar.patch")
  3092. self.assertEqual(output.status_code, 404)
  3093. tests.create_projects(self.session)
  3094. output = self.app.get("/test/c/bar.patch")
  3095. # No git repo associated
  3096. self.assertEqual(output.status_code, 404)
  3097. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  3098. output = self.app.get("/test/c/bar.patch")
  3099. self.assertEqual(output.status_code, 404)
  3100. # Add a README to the git repo - First commit
  3101. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  3102. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  3103. commit = repo.revparse_single("HEAD")
  3104. # View first commit
  3105. output = self.app.get("/test/c/%s.patch" % commit.oid.hex)
  3106. self.assertEqual(output.status_code, 200)
  3107. output_text = output.get_data(as_text=True)
  3108. self.assertIn(
  3109. r"""diff --git a/README.rst b/README.rst
  3110. new file mode 100644
  3111. index 0000000..fb7093d
  3112. --- /dev/null
  3113. +++ b/README.rst
  3114. @@ -0,0 +1,16 @@
  3115. +Pagure
  3116. +======
  3117. +
  3118. +:Author: Pierre-Yves Chibon <pingou@pingoured.fr>
  3119. +
  3120. +
  3121. +Pagure is a light-weight git-centered forge based on pygit2.
  3122. +
  3123. +Currently, Pagure offers a web-interface for git repositories, a ticket
  3124. +system and possibilities to create new projects, fork existing ones and
  3125. +create/merge pull-requests across or within projects.
  3126. +
  3127. +
  3128. +Homepage: https://github.com/pypingou/pagure
  3129. +
  3130. +Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-))
  3131. """,
  3132. output_text,
  3133. )
  3134. self.assertIn("Subject: Add a README file", output_text)
  3135. # Add some content to the git repo
  3136. tests.add_content_git_repo(
  3137. os.path.join(self.path, "repos", "test.git")
  3138. )
  3139. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  3140. commit = repo.revparse_single("HEAD")
  3141. # View another commit
  3142. output = self.app.get("/test/c/%s.patch" % commit.oid.hex)
  3143. self.assertEqual(output.status_code, 200)
  3144. output_text = output.get_data(as_text=True)
  3145. self.assertIn(
  3146. "Subject: Add some directory and a file for more testing",
  3147. output_text,
  3148. )
  3149. self.assertIn(
  3150. r"""diff --git a/folder1/folder2/file b/folder1/folder2/file
  3151. new file mode 100644
  3152. index 0000000..11980b1
  3153. --- /dev/null
  3154. +++ b/folder1/folder2/file
  3155. @@ -0,0 +1,3 @@
  3156. +foo
  3157. + bar
  3158. +baz
  3159. \ No newline at end of file
  3160. """,
  3161. output_text,
  3162. )
  3163. # Add a fork of a fork
  3164. item = pagure.lib.model.Project(
  3165. user_id=1, # pingou
  3166. name="test3",
  3167. description="test project #3",
  3168. is_fork=True,
  3169. parent_id=1,
  3170. hook_token="aaabbblll",
  3171. )
  3172. self.session.add(item)
  3173. self.session.commit()
  3174. forkedgit = os.path.join(
  3175. self.path, "repos", "forks", "pingou", "test3.git"
  3176. )
  3177. tests.add_content_git_repo(forkedgit)
  3178. tests.add_readme_git_repo(forkedgit)
  3179. repo = pygit2.Repository(forkedgit)
  3180. commit = repo.revparse_single("HEAD")
  3181. # Commit does not exist in anothe repo :)
  3182. output = self.app.get("/test/c/%s.patch" % commit.oid.hex)
  3183. self.assertEqual(output.status_code, 404)
  3184. # View commit of fork
  3185. output = self.app.get("/fork/pingou/test3/c/%s.patch" % commit.oid.hex)
  3186. self.assertEqual(output.status_code, 200)
  3187. output_text = output.get_data(as_text=True)
  3188. self.assertIn(
  3189. r"""diff --git a/README.rst b/README.rst
  3190. new file mode 100644
  3191. index 0000000..fb7093d
  3192. --- /dev/null
  3193. +++ b/README.rst
  3194. @@ -0,0 +1,16 @@
  3195. +Pagure
  3196. +======
  3197. +
  3198. +:Author: Pierre-Yves Chibon <pingou@pingoured.fr>
  3199. +
  3200. +
  3201. +Pagure is a light-weight git-centered forge based on pygit2.
  3202. +
  3203. +Currently, Pagure offers a web-interface for git repositories, a ticket
  3204. +system and possibilities to create new projects, fork existing ones and
  3205. +create/merge pull-requests across or within projects.
  3206. +
  3207. +
  3208. +Homepage: https://github.com/pypingou/pagure
  3209. +
  3210. +Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-))
  3211. """,
  3212. output_text,
  3213. )
  3214. def test_view_commit_diff(self):
  3215. """Test the view_commit_diff endpoint."""
  3216. # No project registered in the DB
  3217. output = self.app.get("/foo/c/bar.diff")
  3218. self.assertEqual(output.status_code, 404)
  3219. tests.create_projects(self.session)
  3220. output = self.app.get("/test/c/bar.diff")
  3221. # No git repo associated
  3222. self.assertEqual(output.status_code, 404)
  3223. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  3224. output = self.app.get("/test/c/bar.diff")
  3225. self.assertEqual(output.status_code, 404)
  3226. # Add a README to the git repo - First commit
  3227. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  3228. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  3229. commit = repo.revparse_single("HEAD")
  3230. # View first commit
  3231. output = self.app.get("/test/c/%s.diff" % commit.oid.hex)
  3232. self.assertEqual(output.status_code, 200)
  3233. output_text = output.get_data(as_text=True)
  3234. self.assertEqual(
  3235. r"""diff --git a/README.rst b/README.rst
  3236. new file mode 100644
  3237. index 0000000..fb7093d
  3238. --- /dev/null
  3239. +++ b/README.rst
  3240. @@ -0,0 +1,16 @@
  3241. +Pagure
  3242. +======
  3243. +
  3244. +:Author: Pierre-Yves Chibon <pingou@pingoured.fr>
  3245. +
  3246. +
  3247. +Pagure is a light-weight git-centered forge based on pygit2.
  3248. +
  3249. +Currently, Pagure offers a web-interface for git repositories, a ticket
  3250. +system and possibilities to create new projects, fork existing ones and
  3251. +create/merge pull-requests across or within projects.
  3252. +
  3253. +
  3254. +Homepage: https://github.com/pypingou/pagure
  3255. +
  3256. +Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-))
  3257. """,
  3258. output_text,
  3259. )
  3260. def test_view_tree(self):
  3261. """Test the view_tree endpoint."""
  3262. output = self.app.get("/foo/tree/")
  3263. # No project registered in the DB
  3264. self.assertEqual(output.status_code, 404)
  3265. tests.create_projects(self.session)
  3266. output = self.app.get("/test/tree/")
  3267. # No git repo associated
  3268. self.assertEqual(output.status_code, 404)
  3269. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  3270. output = self.app.get("/test/tree/")
  3271. self.assertEqual(output.status_code, 200)
  3272. output_text = output.get_data(as_text=True)
  3273. self.assertIn(
  3274. """<ol class="breadcrumb p-0 bg-transparent mb-0">
  3275. <li class="breadcrumb-item">
  3276. <a href="/test/tree">
  3277. <span class="fa fa-random">
  3278. </span>&nbsp; None
  3279. </a>
  3280. </li>
  3281. </ol>""",
  3282. output_text,
  3283. )
  3284. self.assertIn("No content found in this repository", output_text)
  3285. # Add a README to the git repo - First commit
  3286. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  3287. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  3288. commit = repo.revparse_single("HEAD")
  3289. # View first commit
  3290. output = self.app.get("/test/tree/%s" % commit.oid.hex)
  3291. self.assertEqual(output.status_code, 200)
  3292. output_text = output.get_data(as_text=True)
  3293. self.assertIn("<title>Tree - test - Pagure</title>", output_text)
  3294. self.assertIn("README.rst", output_text)
  3295. self.assertFalse("No content found in this repository" in output_text)
  3296. # View tree, no identifier:
  3297. output = self.app.get("/test/tree/")
  3298. self.assertEqual(output.status_code, 200)
  3299. output_text = output.get_data(as_text=True)
  3300. self.assertIn("<title>Tree - test - Pagure</title>", output_text)
  3301. self.assertIn("README.rst", output_text)
  3302. self.assertNotIn("No content found in this repository", output_text)
  3303. self.assertNotIn(
  3304. "&#39;None&#39; not found in the git repository, going back to: "
  3305. "master",
  3306. output_text,
  3307. )
  3308. # View tree, invalid identifier:
  3309. output = self.app.get("/test/tree/invalid")
  3310. self.assertEqual(output.status_code, 200)
  3311. output_text = output.get_data(as_text=True)
  3312. self.assertIn("<title>Tree - test - Pagure</title>", output_text)
  3313. self.assertIn("README.rst", output_text)
  3314. self.assertNotIn("No content found in this repository", output_text)
  3315. self.assertIn(
  3316. "&#39;invalid&#39; not found in the git repository, going back "
  3317. "to: master",
  3318. output_text,
  3319. )
  3320. # View tree by branch
  3321. output = self.app.get("/test/tree/master")
  3322. self.assertEqual(output.status_code, 200)
  3323. output_text = output.get_data(as_text=True)
  3324. self.assertIn("<title>Tree - test - Pagure</title>", output_text)
  3325. self.assertIn("README.rst", output_text)
  3326. self.assertNotIn("No content found in this repository", output_text)
  3327. # Add a fork of a fork
  3328. item = pagure.lib.model.Project(
  3329. user_id=1, # pingou
  3330. name="test3",
  3331. description="test project #3",
  3332. is_fork=True,
  3333. parent_id=1,
  3334. hook_token="aaabbbfff",
  3335. )
  3336. self.session.add(item)
  3337. self.session.commit()
  3338. forkedgit = os.path.join(
  3339. self.path, "repos", "forks", "pingou", "test3.git"
  3340. )
  3341. tests.add_content_git_repo(forkedgit)
  3342. output = self.app.get("/fork/pingou/test3/tree/")
  3343. self.assertEqual(output.status_code, 200)
  3344. output_text = output.get_data(as_text=True)
  3345. self.assertIn("<title>Tree - test3 - Pagure</title>", output_text)
  3346. self.assertIn(
  3347. '<a href="/fork/pingou/test3/blob/master/f/folder1">', output_text
  3348. )
  3349. self.assertIn(
  3350. '<a href="/fork/pingou/test3/blob/master/f/sources">', output_text
  3351. )
  3352. self.assertNotIn("No content found in this repository", output_text)
  3353. output = self.app.get(
  3354. "/fork/pingou/test3/blob/master/f/folder1/folder2"
  3355. )
  3356. self.assertEqual(output.status_code, 200)
  3357. output_text = output.get_data(as_text=True)
  3358. self.assertIn(
  3359. '<a href="/fork/pingou/test3/blob/master/'
  3360. 'f/folder1/folder2/file%C5%A0">',
  3361. output_text,
  3362. )
  3363. @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
  3364. @patch("pagure.lib.notify.send_email")
  3365. @patch("pagure.decorators.admin_session_timedout")
  3366. def test_delete_repo_when_turned_off(self, ast, send_email):
  3367. """Test the delete_repo endpoint when deletion of a repo is
  3368. turned off in the pagure instance"""
  3369. ast.return_value = False
  3370. send_email.return_value = True
  3371. # No Git repo
  3372. output = self.app.post("/foo/delete")
  3373. self.assertEqual(output.status_code, 404)
  3374. user = tests.FakeUser(username="pingou")
  3375. with tests.user_set(self.app.application, user):
  3376. tests.create_projects(self.session)
  3377. tests.create_projects_git(os.path.join(self.path, "repos"))
  3378. output = self.app.post("/test/delete", follow_redirects=True)
  3379. self.assertEqual(output.status_code, 404)
  3380. # User not logged in
  3381. output = self.app.post("/test/delete")
  3382. self.assertEqual(output.status_code, 302)
  3383. with tests.user_set(self.app.application, user):
  3384. # Only git repo
  3385. output = self.app.post("/test/delete", follow_redirects=True)
  3386. self.assertEqual(output.status_code, 404)
  3387. # Only git and doc repo
  3388. tests.create_projects_git(os.path.join(self.path, "repos"))
  3389. tests.create_projects_git(os.path.join(self.path, "docs"))
  3390. output = self.app.post("/test/delete", follow_redirects=True)
  3391. self.assertEqual(output.status_code, 404)
  3392. # Create all the git repos
  3393. tests.create_projects_git(os.path.join(self.path, "repos"))
  3394. tests.create_projects_git(os.path.join(self.path, "docs"))
  3395. tests.create_projects_git(
  3396. os.path.join(self.path, "tickets"), bare=True
  3397. )
  3398. tests.create_projects_git(
  3399. os.path.join(self.path, "requests"), bare=True
  3400. )
  3401. # Check repo was created
  3402. output = self.app.get("/", follow_redirects=True)
  3403. self.assertEqual(output.status_code, 200)
  3404. output_text = output.get_data(as_text=True)
  3405. self.assertIn(
  3406. '<span class="btn btn-outline-secondary disabled opacity-100 '
  3407. 'border-0 ml-auto font-weight-bold">3 Projects</span>',
  3408. output_text,
  3409. )
  3410. self.assertNotIn(
  3411. '<span class="d-none d-md-inline">Forks&nbsp;</span>',
  3412. output_text,
  3413. )
  3414. # add issues
  3415. repo = pagure.lib.query.get_authorized_project(
  3416. self.session, "test"
  3417. )
  3418. msg = pagure.lib.query.new_issue(
  3419. session=self.session,
  3420. repo=repo,
  3421. title="Test issue",
  3422. content="We should work on this",
  3423. user="pingou",
  3424. )
  3425. self.session.commit()
  3426. self.assertEqual(msg.title, "Test issue")
  3427. msg = pagure.lib.query.new_issue(
  3428. session=self.session,
  3429. repo=repo,
  3430. title="Test issue #2",
  3431. content="We should work on this, really",
  3432. user="pingou",
  3433. )
  3434. self.session.commit()
  3435. self.assertEqual(msg.title, "Test issue #2")
  3436. # Add a comment to an issue
  3437. issue = pagure.lib.query.search_issues(
  3438. self.session, repo, issueid=1
  3439. )
  3440. msg = pagure.lib.query.add_issue_comment(
  3441. session=self.session,
  3442. issue=issue,
  3443. comment="Hey look a comment!",
  3444. user="foo",
  3445. )
  3446. self.session.commit()
  3447. self.assertEqual(msg, "Comment added")
  3448. # add pull-requests
  3449. req = pagure.lib.query.new_pull_request(
  3450. session=self.session,
  3451. repo_from=repo,
  3452. branch_from="feature",
  3453. repo_to=repo,
  3454. branch_to="master",
  3455. title="test pull-request",
  3456. user="pingou",
  3457. )
  3458. self.session.commit()
  3459. self.assertEqual(req.id, 3)
  3460. self.assertEqual(req.title, "test pull-request")
  3461. req = pagure.lib.query.new_pull_request(
  3462. session=self.session,
  3463. repo_from=repo,
  3464. branch_from="feature2",
  3465. repo_to=repo,
  3466. branch_to="master",
  3467. title="test pull-request",
  3468. user="pingou",
  3469. )
  3470. self.session.commit()
  3471. self.assertEqual(req.id, 4)
  3472. self.assertEqual(req.title, "test pull-request")
  3473. # Add comment on a pull-request
  3474. request = pagure.lib.query.search_pull_requests(
  3475. self.session, requestid=3
  3476. )
  3477. msg = pagure.lib.query.add_pull_request_comment(
  3478. session=self.session,
  3479. request=request,
  3480. commit="commithash",
  3481. tree_id=None,
  3482. filename="file",
  3483. row=None,
  3484. comment="This is awesome, I got to remember it!",
  3485. user="foo",
  3486. )
  3487. self.assertEqual(msg, "Comment added")
  3488. # Check before deleting the project
  3489. output = self.app.get("/", follow_redirects=True)
  3490. self.assertEqual(output.status_code, 200)
  3491. output_text = output.get_data(as_text=True)
  3492. self.assertIn(
  3493. '<span class="btn btn-outline-secondary disabled opacity-100 '
  3494. 'border-0 ml-auto font-weight-bold">3 Projects</span>',
  3495. output_text,
  3496. )
  3497. self.assertNotIn(
  3498. '<span class="d-none d-md-inline">Forks&nbsp;</span>',
  3499. output_text,
  3500. )
  3501. output = self.app.post("/test/delete", follow_redirects=True)
  3502. self.assertEqual(output.status_code, 404)
  3503. repo = pagure.lib.query.get_authorized_project(
  3504. self.session, "test"
  3505. )
  3506. self.assertNotEqual(repo, None)
  3507. repo = pagure.lib.query.get_authorized_project(
  3508. self.session, "test2"
  3509. )
  3510. self.assertNotEqual(repo, None)
  3511. # Add a fork of a fork
  3512. item = pagure.lib.model.Project(
  3513. user_id=1, # pingou
  3514. name="test3",
  3515. description="test project #3",
  3516. is_fork=True,
  3517. parent_id=2,
  3518. hook_token="aaabbbjjj",
  3519. )
  3520. self.session.add(item)
  3521. self.session.commit()
  3522. tests.add_content_git_repo(
  3523. os.path.join(
  3524. self.path, "repos", "forks", "pingou", "test3.git"
  3525. )
  3526. )
  3527. tests.add_content_git_repo(
  3528. os.path.join(self.path, "docs", "pingou", "test3.git")
  3529. )
  3530. tests.add_content_git_repo(
  3531. os.path.join(self.path, "tickets", "pingou", "test3.git")
  3532. )
  3533. # Check before deleting the fork
  3534. output = self.app.get("/", follow_redirects=True)
  3535. self.assertEqual(output.status_code, 200)
  3536. output_text = output.get_data(as_text=True)
  3537. self.assertIn(
  3538. '<span class="btn btn-outline-secondary disabled opacity-100 '
  3539. 'border-0 ml-auto font-weight-bold">3 Projects</span>',
  3540. output_text,
  3541. )
  3542. self.assertIn(
  3543. """<span>
  3544. <i class="fa fa-fw text-muted fa-code-fork"></i>
  3545. <span class="d-none d-md-inline">Forks&nbsp;</span>
  3546. </span>
  3547. <div class="ml-auto">
  3548. <span class="badge badge-secondary">
  3549. 1
  3550. </span>
  3551. </div>""",
  3552. output_text,
  3553. )
  3554. output = self.app.post(
  3555. "/fork/pingou/test3/delete", follow_redirects=True
  3556. )
  3557. self.assertEqual(output.status_code, 404)
  3558. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3559. @patch("pagure.decorators.admin_session_timedout")
  3560. def test_delete_repo(self, ast):
  3561. """Test the delete_repo endpoint."""
  3562. ast.return_value = False
  3563. # No Git repo
  3564. output = self.app.post("/foo/delete")
  3565. self.assertEqual(output.status_code, 404)
  3566. user = tests.FakeUser()
  3567. with tests.user_set(self.app.application, user):
  3568. tests.create_projects(self.session)
  3569. tests.create_projects_git(os.path.join(self.path, "repos"))
  3570. # No project registered in the DB (no git repo)
  3571. output = self.app.post("/foo/delete")
  3572. self.assertEqual(output.status_code, 404)
  3573. # User not allowed
  3574. output = self.app.post("/test/delete")
  3575. self.assertEqual(output.status_code, 403)
  3576. # User not logged in
  3577. output = self.app.post("/test/delete")
  3578. self.assertEqual(output.status_code, 302)
  3579. user = tests.FakeUser(username="pingou")
  3580. with tests.user_set(self.app.application, user):
  3581. tests.create_projects_git(os.path.join(self.path, "repos"))
  3582. ast.return_value = True
  3583. output = self.app.post("/test/delete")
  3584. self.assertEqual(output.status_code, 302)
  3585. ast.return_value = False
  3586. output = self.app.post("/test/delete", follow_redirects=True)
  3587. self.assertEqual(output.status_code, 200)
  3588. output_text = output.get_data(as_text=True)
  3589. self.assertIn(
  3590. """<span>
  3591. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  3592. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3593. </span>
  3594. <div class="ml-auto">
  3595. <span class="badge badge-secondary">
  3596. 2
  3597. </span>
  3598. </div>""",
  3599. output_text,
  3600. )
  3601. self.assertIn(
  3602. """<span>
  3603. <i class="fa fa-fw text-muted fa-code-fork"></i>
  3604. <span class="d-none d-md-inline">Forks&nbsp;</span>
  3605. </span>
  3606. <div class="ml-auto">
  3607. <span class="badge badge-secondary">
  3608. 0
  3609. </span>
  3610. </div>""",
  3611. output_text,
  3612. )
  3613. # Only git repo
  3614. item = pagure.lib.model.Project(
  3615. user_id=1, # pingou
  3616. name="test",
  3617. description="test project #1",
  3618. hook_token="aaabbbggg",
  3619. )
  3620. self.session.add(item)
  3621. self.session.commit()
  3622. tests.create_projects_git(os.path.join(self.path, "repos"))
  3623. output = self.app.post("/test/delete", follow_redirects=True)
  3624. self.assertEqual(output.status_code, 200)
  3625. output_text = output.get_data(as_text=True)
  3626. self.assertIn(
  3627. """<span>
  3628. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  3629. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3630. </span>
  3631. <div class="ml-auto">
  3632. <span class="badge badge-secondary">
  3633. 2
  3634. </span>
  3635. </div>""",
  3636. output_text,
  3637. )
  3638. self.assertIn(
  3639. """<span>
  3640. <i class="fa fa-fw text-muted fa-code-fork"></i>
  3641. <span class="d-none d-md-inline">Forks&nbsp;</span>
  3642. </span>
  3643. <div class="ml-auto">
  3644. <span class="badge badge-secondary">
  3645. 0
  3646. </span>
  3647. </div>""",
  3648. output_text,
  3649. )
  3650. # Only git and doc repo
  3651. item = pagure.lib.model.Project(
  3652. user_id=1, # pingou
  3653. name="test",
  3654. description="test project #1",
  3655. hook_token="aaabbbhhh",
  3656. )
  3657. self.session.add(item)
  3658. self.session.commit()
  3659. tests.create_projects_git(os.path.join(self.path, "repos"))
  3660. tests.create_projects_git(os.path.join(self.path, "docs"))
  3661. output = self.app.post("/test/delete", follow_redirects=True)
  3662. self.assertEqual(output.status_code, 200)
  3663. output_text = output.get_data(as_text=True)
  3664. self.assertIn(
  3665. """<span>
  3666. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  3667. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3668. </span>
  3669. <div class="ml-auto">
  3670. <span class="badge badge-secondary">
  3671. 2
  3672. </span>
  3673. </div>""",
  3674. output_text,
  3675. )
  3676. self.assertIn(
  3677. """<span>
  3678. <i class="fa fa-fw text-muted fa-code-fork"></i>
  3679. <span class="d-none d-md-inline">Forks&nbsp;</span>
  3680. </span>
  3681. <div class="ml-auto">
  3682. <span class="badge badge-secondary">
  3683. 0
  3684. </span>
  3685. </div>""",
  3686. output_text,
  3687. )
  3688. # All repo there
  3689. item = pagure.lib.model.Project(
  3690. user_id=1, # pingou
  3691. name="test",
  3692. description="test project #1",
  3693. hook_token="aaabbbiii",
  3694. )
  3695. self.session.add(item)
  3696. self.session.commit()
  3697. # Create all the git repos
  3698. tests.create_projects_git(os.path.join(self.path, "repos"))
  3699. tests.create_projects_git(os.path.join(self.path, "docs"))
  3700. tests.create_projects_git(
  3701. os.path.join(self.path, "tickets"), bare=True
  3702. )
  3703. tests.create_projects_git(
  3704. os.path.join(self.path, "requests"), bare=True
  3705. )
  3706. # Check repo was created
  3707. output = self.app.get("/", follow_redirects=True)
  3708. self.assertEqual(output.status_code, 200)
  3709. output_text = output.get_data(as_text=True)
  3710. self.assertIn(
  3711. """<span>
  3712. <i class="fa fa-calendar-o fa-rotate-270 fa-fw text-muted"></i>
  3713. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3714. </span>
  3715. <div class="ml-auto">
  3716. <span class="badge badge-secondary">
  3717. 3
  3718. </span>
  3719. </div>""",
  3720. output_text,
  3721. )
  3722. self.assertNotIn(
  3723. """<span class="d-none d-md-inline">Forks&nbsp;</span>""",
  3724. output_text,
  3725. )
  3726. # add issues
  3727. repo = pagure.lib.query.get_authorized_project(
  3728. self.session, "test"
  3729. )
  3730. msg = pagure.lib.query.new_issue(
  3731. session=self.session,
  3732. repo=repo,
  3733. title="Test issue",
  3734. content="We should work on this",
  3735. user="pingou",
  3736. )
  3737. self.session.commit()
  3738. self.assertEqual(msg.title, "Test issue")
  3739. msg = pagure.lib.query.new_issue(
  3740. session=self.session,
  3741. repo=repo,
  3742. title="Test issue #2",
  3743. content="We should work on this, really",
  3744. user="pingou",
  3745. )
  3746. self.session.commit()
  3747. self.assertEqual(msg.title, "Test issue #2")
  3748. # Add a comment to an issue
  3749. issue = pagure.lib.query.search_issues(
  3750. self.session, repo, issueid=1
  3751. )
  3752. msg = pagure.lib.query.add_issue_comment(
  3753. session=self.session,
  3754. issue=issue,
  3755. comment="Hey look a comment!",
  3756. user="foo",
  3757. )
  3758. self.session.commit()
  3759. self.assertEqual(msg, "Comment added")
  3760. # add pull-requests
  3761. req = pagure.lib.query.new_pull_request(
  3762. session=self.session,
  3763. repo_from=repo,
  3764. branch_from="feature",
  3765. repo_to=repo,
  3766. branch_to="master",
  3767. title="test pull-request",
  3768. user="pingou",
  3769. )
  3770. self.session.commit()
  3771. self.assertEqual(req.id, 3)
  3772. self.assertEqual(req.title, "test pull-request")
  3773. req = pagure.lib.query.new_pull_request(
  3774. session=self.session,
  3775. repo_from=repo,
  3776. branch_from="feature2",
  3777. repo_to=repo,
  3778. branch_to="master",
  3779. title="test pull-request",
  3780. user="pingou",
  3781. )
  3782. self.session.commit()
  3783. self.assertEqual(req.id, 4)
  3784. self.assertEqual(req.title, "test pull-request")
  3785. # Add comment on a pull-request
  3786. request = pagure.lib.query.search_pull_requests(
  3787. self.session, requestid=3
  3788. )
  3789. msg = pagure.lib.query.add_pull_request_comment(
  3790. session=self.session,
  3791. request=request,
  3792. commit="commithash",
  3793. tree_id=None,
  3794. filename="file",
  3795. row=None,
  3796. comment="This is awesome, I got to remember it!",
  3797. user="foo",
  3798. )
  3799. self.assertEqual(msg, "Comment added")
  3800. # Check before deleting the project
  3801. output = self.app.get("/", follow_redirects=True)
  3802. self.assertEqual(output.status_code, 200)
  3803. output_text = output.get_data(as_text=True)
  3804. self.assertIn(
  3805. """<span>
  3806. <i class="fa fa-calendar-o fa-rotate-270 fa-fw text-muted"></i>
  3807. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3808. </span>
  3809. <div class="ml-auto">
  3810. <span class="badge badge-secondary">
  3811. 3
  3812. </span>
  3813. </div>""",
  3814. output_text,
  3815. )
  3816. self.assertNotIn(
  3817. """<span class="d-none d-md-inline">Forks&nbsp;</span>""",
  3818. output_text,
  3819. )
  3820. output = self.app.post("/test/delete", follow_redirects=True)
  3821. self.assertEqual(output.status_code, 200)
  3822. output_text = output.get_data(as_text=True)
  3823. self.assertIn(
  3824. """<span>
  3825. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  3826. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3827. </span>
  3828. <div class="ml-auto">
  3829. <span class="badge badge-secondary">
  3830. 2
  3831. </span>
  3832. </div>""",
  3833. output_text,
  3834. )
  3835. self.assertIn(
  3836. """<span>
  3837. <i class="fa fa-fw text-muted fa-code-fork"></i>
  3838. <span class="d-none d-md-inline">Forks&nbsp;</span>
  3839. </span>
  3840. <div class="ml-auto">
  3841. <span class="badge badge-secondary">
  3842. 0
  3843. </span>
  3844. </div>""",
  3845. output_text,
  3846. )
  3847. repo = pagure.lib.query.get_authorized_project(
  3848. self.session, "test"
  3849. )
  3850. self.assertEqual(repo, None)
  3851. repo = pagure.lib.query.get_authorized_project(
  3852. self.session, "test2"
  3853. )
  3854. self.assertNotEqual(repo, None)
  3855. # Add a fork of a fork
  3856. item = pagure.lib.model.Project(
  3857. user_id=1, # pingou
  3858. name="test3",
  3859. description="test project #3",
  3860. is_fork=True,
  3861. parent_id=2,
  3862. hook_token="aaabbbjjj",
  3863. )
  3864. self.session.add(item)
  3865. self.session.commit()
  3866. tests.add_content_git_repo(
  3867. os.path.join(
  3868. self.path, "repos", "forks", "pingou", "test3.git"
  3869. )
  3870. )
  3871. tests.add_content_git_repo(
  3872. os.path.join(self.path, "docs", "pingou", "test3.git")
  3873. )
  3874. tests.add_content_git_repo(
  3875. os.path.join(self.path, "tickets", "pingou", "test3.git")
  3876. )
  3877. # Check before deleting the fork
  3878. output = self.app.get("/", follow_redirects=True)
  3879. self.assertEqual(output.status_code, 200)
  3880. output_text = output.get_data(as_text=True)
  3881. self.assertIn(
  3882. """<span>
  3883. <i class="fa fa-calendar-o fa-rotate-270 fa-fw text-muted"></i>
  3884. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3885. </span>
  3886. <div class="ml-auto">
  3887. <span class="badge badge-secondary">
  3888. 2
  3889. </span>
  3890. </div>""",
  3891. output_text,
  3892. )
  3893. self.assertIn(
  3894. """<span>
  3895. <i class="fa fa-fw text-muted fa-code-fork"></i>
  3896. <span class="d-none d-md-inline">Forks&nbsp;</span>
  3897. </span>
  3898. <div class="ml-auto">
  3899. <span class="badge badge-secondary">
  3900. 1
  3901. </span>
  3902. </div>""",
  3903. output_text,
  3904. )
  3905. output = self.app.post(
  3906. "/fork/pingou/test3/delete", follow_redirects=True
  3907. )
  3908. self.assertEqual(output.status_code, 200)
  3909. output_text = output.get_data(as_text=True)
  3910. self.assertIn(
  3911. """<span>
  3912. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  3913. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3914. </span>
  3915. <div class="ml-auto">
  3916. <span class="badge badge-secondary">
  3917. 2
  3918. </span>
  3919. </div>""",
  3920. output_text,
  3921. )
  3922. self.assertIn(
  3923. """<span>
  3924. <i class="fa fa-fw text-muted fa-code-fork"></i>
  3925. <span class="d-none d-md-inline">Forks&nbsp;</span>
  3926. </span>
  3927. <div class="ml-auto">
  3928. <span class="badge badge-secondary">
  3929. 0
  3930. </span>
  3931. </div>""",
  3932. output_text,
  3933. )
  3934. @patch.dict("pagure.config.config", {"TICKETS_FOLDER": None})
  3935. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  3936. @patch(
  3937. "pagure.decorators.admin_session_timedout",
  3938. MagicMock(return_value=False),
  3939. )
  3940. def test_delete_repo_no_ticket(self):
  3941. """Test the delete_repo endpoint when tickets aren't enabled in
  3942. this pagure instance."""
  3943. tests.create_projects(self.session)
  3944. tests.create_projects_git(os.path.join(self.path, "repos"))
  3945. user = tests.FakeUser(username="pingou")
  3946. with tests.user_set(self.app.application, user):
  3947. # Check before deleting the project
  3948. output = self.app.get("/", follow_redirects=True)
  3949. self.assertEqual(output.status_code, 200)
  3950. output_text = output.get_data(as_text=True)
  3951. self.assertIn(
  3952. """<span>
  3953. <i class="fa fa-calendar-o fa-rotate-270 fa-fw text-muted"></i>
  3954. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3955. </span>
  3956. <div class="ml-auto">
  3957. <span class="badge badge-secondary">
  3958. 3
  3959. </span>
  3960. </div>""",
  3961. output_text,
  3962. )
  3963. self.assertNotIn(
  3964. """<span class="d-none d-md-inline">Forks&nbsp;</span>""",
  3965. output_text,
  3966. )
  3967. # Delete the project
  3968. output = self.app.post("/test/delete", follow_redirects=True)
  3969. self.assertEqual(output.status_code, 200)
  3970. output_text = output.get_data(as_text=True)
  3971. # Check deletion worked
  3972. self.assertIn(
  3973. """<span>
  3974. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  3975. <span class="d-none d-md-inline">Projects&nbsp;</span>
  3976. </span>
  3977. <div class="ml-auto">
  3978. <span class="badge badge-secondary">
  3979. 2
  3980. </span>
  3981. </div>""",
  3982. output_text,
  3983. )
  3984. self.assertIn(
  3985. """<span>
  3986. <i class="fa fa-fw text-muted fa-code-fork"></i>
  3987. <span class="d-none d-md-inline">Forks&nbsp;</span>
  3988. </span>
  3989. <div class="ml-auto">
  3990. <span class="badge badge-secondary">
  3991. 0
  3992. </span>
  3993. </div>""",
  3994. output_text,
  3995. )
  3996. @patch("pagure.lib.notify.send_email")
  3997. @patch("pagure.decorators.admin_session_timedout")
  3998. def test_delete_repo_with_users(self, ast, send_email):
  3999. """Test the delete_repo endpoint."""
  4000. ast.return_value = False
  4001. send_email.return_value = True
  4002. user = tests.FakeUser()
  4003. user = tests.FakeUser(username="pingou")
  4004. with tests.user_set(self.app.application, user):
  4005. # Create new project
  4006. item = pagure.lib.model.Project(
  4007. user_id=1, # pingou
  4008. name="test",
  4009. description="test project #1",
  4010. hook_token="aaabbbiii",
  4011. )
  4012. self.session.add(item)
  4013. self.session.commit()
  4014. # Create all the git repos
  4015. tests.create_projects_git(os.path.join(self.path, "repos"))
  4016. tests.create_projects_git(
  4017. os.path.join(self.path, "docs"), bare=True
  4018. )
  4019. tests.create_projects_git(
  4020. os.path.join(self.path, "tickets"), bare=True
  4021. )
  4022. tests.create_projects_git(
  4023. os.path.join(self.path, "requests"), bare=True
  4024. )
  4025. # Check repo was created
  4026. output = self.app.get("/", follow_redirects=True)
  4027. self.assertEqual(output.status_code, 200)
  4028. output_text = output.get_data(as_text=True)
  4029. self.assertIn(
  4030. '<span class="btn btn-outline-secondary disabled opacity-100 '
  4031. 'border-0 ml-auto font-weight-bold">1 Projects</span>',
  4032. output_text,
  4033. )
  4034. self.assertNotIn(
  4035. '<span class="d-none d-md-inline">Forks&nbsp;</span>',
  4036. output_text,
  4037. )
  4038. # add user
  4039. repo = pagure.lib.query.get_authorized_project(
  4040. self.session, "test"
  4041. )
  4042. msg = pagure.lib.query.add_user_to_project(
  4043. session=self.session,
  4044. project=repo,
  4045. new_user="foo",
  4046. user="pingou",
  4047. )
  4048. self.session.commit()
  4049. self.assertEqual(msg, "User added")
  4050. # Check before deleting the project
  4051. output = self.app.get("/", follow_redirects=True)
  4052. self.assertEqual(output.status_code, 200)
  4053. output_text = output.get_data(as_text=True)
  4054. self.assertIn(
  4055. '<span class="btn btn-outline-secondary disabled opacity-100 '
  4056. 'border-0 ml-auto font-weight-bold">1 Projects</span>',
  4057. output_text,
  4058. )
  4059. self.assertNotIn(
  4060. '<span class="d-none d-md-inline">Forks&nbsp;</span>',
  4061. output_text,
  4062. )
  4063. repo = pagure.lib.query.get_authorized_project(
  4064. self.session, "test"
  4065. )
  4066. self.assertNotEqual(repo, None)
  4067. repo = pagure.lib.query.get_authorized_project(
  4068. self.session, "test2"
  4069. )
  4070. self.assertEqual(repo, None)
  4071. # Delete the project
  4072. output = self.app.post("/test/delete", follow_redirects=True)
  4073. self.assertEqual(output.status_code, 200)
  4074. output_text = output.get_data(as_text=True)
  4075. self.assertIn(
  4076. """<span>
  4077. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  4078. <span class="d-none d-md-inline">Projects&nbsp;</span>
  4079. </span>
  4080. <div class="ml-auto">
  4081. <span class="badge badge-secondary">
  4082. 0
  4083. </span>
  4084. </div>""",
  4085. output_text,
  4086. )
  4087. self.assertIn(
  4088. """<span>
  4089. <i class="fa fa-fw text-muted fa-code-fork"></i>
  4090. <span class="d-none d-md-inline">Forks&nbsp;</span>
  4091. </span>
  4092. <div class="ml-auto">
  4093. <span class="badge badge-secondary">
  4094. 0
  4095. </span>
  4096. </div>""",
  4097. output_text,
  4098. )
  4099. # Check after
  4100. repo = pagure.lib.query.get_authorized_project(
  4101. self.session, "test"
  4102. )
  4103. self.assertEqual(repo, None)
  4104. repo = pagure.lib.query.get_authorized_project(
  4105. self.session, "test2"
  4106. )
  4107. self.assertEqual(repo, None)
  4108. @patch("pagure.lib.notify.send_email")
  4109. @patch("pagure.decorators.admin_session_timedout")
  4110. def test_delete_repo_with_group(self, ast, send_email):
  4111. """Test the delete_repo endpoint."""
  4112. ast.return_value = False
  4113. send_email.return_value = True
  4114. user = tests.FakeUser()
  4115. user = tests.FakeUser(username="pingou")
  4116. with tests.user_set(self.app.application, user):
  4117. # Create new project
  4118. item = pagure.lib.model.Project(
  4119. user_id=1, # pingou
  4120. name="test",
  4121. description="test project #1",
  4122. hook_token="aaabbbiii",
  4123. )
  4124. self.session.add(item)
  4125. self.session.commit()
  4126. # Create all the git repos
  4127. tests.create_projects_git(os.path.join(self.path, "repos"))
  4128. tests.create_projects_git(
  4129. os.path.join(self.path, "docs"), bare=True
  4130. )
  4131. tests.create_projects_git(
  4132. os.path.join(self.path, "tickets"), bare=True
  4133. )
  4134. tests.create_projects_git(
  4135. os.path.join(self.path, "requests"), bare=True
  4136. )
  4137. # Check repo was created
  4138. output = self.app.get("/", follow_redirects=True)
  4139. self.assertEqual(output.status_code, 200)
  4140. output_text = output.get_data(as_text=True)
  4141. self.assertIn(
  4142. '<span class="btn btn-outline-secondary disabled opacity-100 '
  4143. 'border-0 ml-auto font-weight-bold">1 Projects</span>',
  4144. output_text,
  4145. )
  4146. self.assertNotIn(
  4147. '<span class="d-none d-md-inline">Forks&nbsp;</span>',
  4148. output_text,
  4149. )
  4150. # Create group
  4151. msg = pagure.lib.query.add_group(
  4152. self.session,
  4153. group_name="foo",
  4154. display_name="foo group",
  4155. description=None,
  4156. group_type="bar",
  4157. user="pingou",
  4158. is_admin=False,
  4159. blacklist=[],
  4160. )
  4161. self.session.commit()
  4162. self.assertEqual(msg, "User `pingou` added to the group `foo`.")
  4163. # Add group to the project
  4164. repo = pagure.lib.query.get_authorized_project(
  4165. self.session, "test"
  4166. )
  4167. msg = pagure.lib.query.add_group_to_project(
  4168. session=self.session,
  4169. project=repo,
  4170. new_group="foo",
  4171. user="pingou",
  4172. )
  4173. self.session.commit()
  4174. self.assertEqual(msg, "Group added")
  4175. # check if group where we expect it
  4176. repo = pagure.lib.query.get_authorized_project(
  4177. self.session, "test"
  4178. )
  4179. self.assertEqual(len(repo.projects_groups), 1)
  4180. # Check before deleting the project
  4181. output = self.app.get("/", follow_redirects=True)
  4182. self.assertEqual(output.status_code, 200)
  4183. output_text = output.get_data(as_text=True)
  4184. self.assertIn(
  4185. '<span class="btn btn-outline-secondary disabled opacity-100 '
  4186. 'border-0 ml-auto font-weight-bold">1 Projects</span>',
  4187. output_text,
  4188. )
  4189. self.assertNotIn(
  4190. '<span class="d-none d-md-inline">Forks&nbsp;</span>',
  4191. output_text,
  4192. )
  4193. repo = pagure.lib.query.get_authorized_project(
  4194. self.session, "test"
  4195. )
  4196. self.assertNotEqual(repo, None)
  4197. # Delete the project
  4198. output = self.app.post("/test/delete", follow_redirects=True)
  4199. self.assertEqual(output.status_code, 200)
  4200. output_text = output.get_data(as_text=True)
  4201. self.assertIn(
  4202. """<span>
  4203. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  4204. <span class="d-none d-md-inline">Projects&nbsp;</span>
  4205. </span>
  4206. <div class="ml-auto">
  4207. <span class="badge badge-secondary">
  4208. 0
  4209. </span>
  4210. </div>""",
  4211. output_text,
  4212. )
  4213. self.assertIn(
  4214. """<span>
  4215. <i class="fa fa-fw text-muted fa-code-fork"></i>
  4216. <span class="d-none d-md-inline">Forks&nbsp;</span>
  4217. </span>
  4218. <div class="ml-auto">
  4219. <span class="badge badge-secondary">
  4220. 0
  4221. </span>
  4222. </div>""",
  4223. output_text,
  4224. )
  4225. # Check after
  4226. repo = pagure.lib.query.get_authorized_project(
  4227. self.session, "test"
  4228. )
  4229. self.assertEqual(repo, None)
  4230. @patch("pagure.lib.notify.send_email")
  4231. @patch("pagure.decorators.admin_session_timedout")
  4232. def test_delete_repo_with_coloredtag(self, ast, send_email):
  4233. """Test the delete_repo endpoint."""
  4234. ast.return_value = False
  4235. send_email.return_value = True
  4236. user = tests.FakeUser()
  4237. user = tests.FakeUser(username="pingou")
  4238. with tests.user_set(self.app.application, user):
  4239. # Create new project
  4240. item = pagure.lib.model.Project(
  4241. user_id=1, # pingou
  4242. name="test",
  4243. description="test project #1",
  4244. hook_token="aaabbbiii",
  4245. )
  4246. self.session.add(item)
  4247. self.session.commit()
  4248. # Create all the git repos
  4249. tests.create_projects_git(os.path.join(self.path, "repos"))
  4250. tests.create_projects_git(
  4251. os.path.join(self.path, "docs"), bare=True
  4252. )
  4253. tests.create_projects_git(
  4254. os.path.join(self.path, "tickets"), bare=True
  4255. )
  4256. tests.create_projects_git(
  4257. os.path.join(self.path, "requests"), bare=True
  4258. )
  4259. # Check repo was created
  4260. output = self.app.get("/", follow_redirects=True)
  4261. self.assertEqual(output.status_code, 200)
  4262. output_text = output.get_data(as_text=True)
  4263. self.assertIn(
  4264. '<span class="btn btn-outline-secondary disabled opacity-100 '
  4265. 'border-0 ml-auto font-weight-bold">1 Projects</span>',
  4266. output_text,
  4267. )
  4268. self.assertNotIn(
  4269. '<span class="d-none d-md-inline">Forks&nbsp;</span>',
  4270. output_text,
  4271. )
  4272. # Create the issue
  4273. repo = pagure.lib.query.get_authorized_project(
  4274. self.session, "test"
  4275. )
  4276. msg = pagure.lib.query.new_issue(
  4277. session=self.session,
  4278. repo=repo,
  4279. title="Test issue",
  4280. content="We should work on this",
  4281. user="pingou",
  4282. )
  4283. self.session.commit()
  4284. self.assertEqual(msg.title, "Test issue")
  4285. # Add a tag to the issue
  4286. repo = pagure.lib.query.get_authorized_project(
  4287. self.session, "test"
  4288. )
  4289. issue = pagure.lib.query.search_issues(
  4290. self.session, repo, issueid=1
  4291. )
  4292. msg = pagure.lib.query.add_tag_obj(
  4293. session=self.session, obj=issue, tags="tag1", user="pingou"
  4294. )
  4295. self.session.commit()
  4296. self.assertEqual(msg, "Issue tagged with: tag1")
  4297. # Check before deleting the project
  4298. output = self.app.get("/", follow_redirects=True)
  4299. self.assertEqual(output.status_code, 200)
  4300. output_text = output.get_data(as_text=True)
  4301. self.assertIn(
  4302. '<span class="btn btn-outline-secondary disabled opacity-100 '
  4303. 'border-0 ml-auto font-weight-bold">1 Projects</span>',
  4304. output_text,
  4305. )
  4306. self.assertNotIn(
  4307. '<span class="d-none d-md-inline">Forks&nbsp;</span>',
  4308. output_text,
  4309. )
  4310. repo = pagure.lib.query.get_authorized_project(
  4311. self.session, "test"
  4312. )
  4313. self.assertNotEqual(repo, None)
  4314. repo = pagure.lib.query.get_authorized_project(
  4315. self.session, "test2"
  4316. )
  4317. self.assertEqual(repo, None)
  4318. # Delete the project
  4319. output = self.app.post("/test/delete", follow_redirects=True)
  4320. self.assertEqual(output.status_code, 200)
  4321. output_text = output.get_data(as_text=True)
  4322. self.assertIn(
  4323. """<span>
  4324. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  4325. <span class="d-none d-md-inline">Projects&nbsp;</span>
  4326. </span>
  4327. <div class="ml-auto">
  4328. <span class="badge badge-secondary">
  4329. 0
  4330. </span>
  4331. </div>""",
  4332. output_text,
  4333. )
  4334. self.assertIn(
  4335. """<span>
  4336. <i class="fa fa-fw text-muted fa-code-fork"></i>
  4337. <span class="d-none d-md-inline">Forks&nbsp;</span>
  4338. </span>
  4339. <div class="ml-auto">
  4340. <span class="badge badge-secondary">
  4341. 0
  4342. </span>
  4343. </div>""",
  4344. output_text,
  4345. )
  4346. # Check after
  4347. repo = pagure.lib.query.get_authorized_project(
  4348. self.session, "test"
  4349. )
  4350. self.assertEqual(repo, None)
  4351. repo = pagure.lib.query.get_authorized_project(
  4352. self.session, "test2"
  4353. )
  4354. self.assertEqual(repo, None)
  4355. @patch("pagure.decorators.admin_session_timedout")
  4356. def test_new_repo_hook_token(self, ast):
  4357. """Test the new_repo_hook_token endpoint."""
  4358. ast.return_value = False
  4359. tests.create_projects(self.session)
  4360. tests.create_projects_git(os.path.join(self.path, "repos"))
  4361. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  4362. self.assertEqual(repo.hook_token, "aaabbbccc")
  4363. user = tests.FakeUser()
  4364. with tests.user_set(self.app.application, user):
  4365. pagure.config.config["WEBHOOK"] = True
  4366. output = self.app.get("/new/")
  4367. self.assertEqual(output.status_code, 200)
  4368. output_text = output.get_data(as_text=True)
  4369. self.assertIn("<strong>Create new Project</strong>", output_text)
  4370. csrf_token = output_text.split(
  4371. 'name="csrf_token" type="hidden" value="'
  4372. )[1].split('">')[0]
  4373. output = self.app.post("/foo/hook_token")
  4374. self.assertEqual(output.status_code, 404)
  4375. output = self.app.post("/test/hook_token")
  4376. self.assertEqual(output.status_code, 403)
  4377. ast.return_value = True
  4378. output = self.app.post("/test/hook_token")
  4379. self.assertEqual(output.status_code, 302)
  4380. ast.return_value = False
  4381. pagure.config.config["WEBHOOK"] = False
  4382. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  4383. self.assertEqual(repo.hook_token, "aaabbbccc")
  4384. user.username = "pingou"
  4385. with tests.user_set(self.app.application, user):
  4386. pagure.config.config["WEBHOOK"] = True
  4387. output = self.app.post("/test/hook_token")
  4388. self.assertEqual(output.status_code, 400)
  4389. data = {"csrf_token": csrf_token}
  4390. repo = pagure.lib.query.get_authorized_project(
  4391. self.session, "test"
  4392. )
  4393. self.assertEqual(repo.hook_token, "aaabbbccc")
  4394. output = self.app.post(
  4395. "/test/hook_token", data=data, follow_redirects=True
  4396. )
  4397. self.assertEqual(output.status_code, 200)
  4398. output_text = output.get_data(as_text=True)
  4399. self.assertIn("New hook token generated", output_text)
  4400. pagure.config.config["WEBHOOK"] = False
  4401. self.session.commit()
  4402. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  4403. self.assertNotEqual(repo.hook_token, "aaabbbccc")
  4404. def test_view_tags(self):
  4405. """Test the view_tags endpoint."""
  4406. output = self.app.get("/foo/releases")
  4407. # No project registered in the DB
  4408. self.assertEqual(output.status_code, 404)
  4409. tests.create_projects(self.session)
  4410. output = self.app.get("/test/releases")
  4411. # No git repo associated
  4412. self.assertEqual(output.status_code, 404)
  4413. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  4414. output = self.app.get("/test/releases")
  4415. self.assertEqual(output.status_code, 200)
  4416. output_text = output.get_data(as_text=True)
  4417. self.assertIn("This project has not been tagged.", output_text)
  4418. # Add a README to the git repo - First commit
  4419. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  4420. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  4421. first_commit = repo.revparse_single("HEAD")
  4422. tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
  4423. repo.create_tag(
  4424. "0.0.1",
  4425. first_commit.oid.hex,
  4426. pygit2.GIT_OBJ_COMMIT,
  4427. tagger,
  4428. "Release 0.0.1",
  4429. )
  4430. output = self.app.get("/test/releases")
  4431. self.assertEqual(output.status_code, 200)
  4432. output_text = output.get_data(as_text=True)
  4433. self.assertIn("0.0.1", output_text)
  4434. self.assertIn('<section class="tag_list">', output_text)
  4435. self.assertEqual(
  4436. output_text.count(
  4437. '<i class="fa fa-calendar-o fa-rotate-270 text-muted"></i>'
  4438. ),
  4439. 1,
  4440. )
  4441. def test_edit_file_no_signed_off(self):
  4442. """Test the edit_file endpoint when signed-off isn't enforced."""
  4443. tests.create_projects(self.session)
  4444. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  4445. user = tests.FakeUser()
  4446. user.username = "pingou"
  4447. with tests.user_set(self.app.application, user):
  4448. # Add some content to the git repo
  4449. tests.add_content_git_repo(
  4450. os.path.join(self.path, "repos", "test.git")
  4451. )
  4452. output = self.app.get("/test/edit/master/f/sources")
  4453. self.assertEqual(output.status_code, 200)
  4454. output_text = output.get_data(as_text=True)
  4455. self.assertIn(
  4456. '<li><a href="/test/tree/master"><span class="fa fa-random">'
  4457. '</span>&nbsp; master</a></li><li class="active">'
  4458. '<span class="fa fa-file"></span>&nbsp; sources</li>',
  4459. output_text,
  4460. )
  4461. self.assertIn(
  4462. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  4463. output_text,
  4464. )
  4465. self.assertIn(
  4466. '<textarea rows="5" class="form-control" type="text" '
  4467. 'id="commit_message"\n name="commit_message" '
  4468. 'placeholder="An optional description of the change">'
  4469. "</textarea>",
  4470. output_text,
  4471. )
  4472. def test_edit_file_signed_off(self):
  4473. """Test the edit_file endpoint when signed-off is enforced."""
  4474. tests.create_projects(self.session)
  4475. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  4476. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  4477. settings = repo.settings
  4478. settings["Enforce_signed-off_commits_in_pull-request"] = True
  4479. repo.settings = settings
  4480. self.session.add(repo)
  4481. self.session.commit()
  4482. user = tests.FakeUser()
  4483. user.username = "pingou"
  4484. with tests.user_set(self.app.application, user):
  4485. # Add some content to the git repo
  4486. tests.add_content_git_repo(
  4487. os.path.join(self.path, "repos", "test.git")
  4488. )
  4489. output = self.app.get("/test/edit/master/f/sources")
  4490. self.assertEqual(output.status_code, 200)
  4491. output_text = output.get_data(as_text=True)
  4492. self.assertIn(
  4493. '<li><a href="/test/tree/master"><span class="fa fa-random">'
  4494. '</span>&nbsp; master</a></li><li class="active">'
  4495. '<span class="fa fa-file"></span>&nbsp; sources</li>',
  4496. output_text,
  4497. )
  4498. self.assertIn(
  4499. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  4500. output_text,
  4501. )
  4502. self.assertIn(
  4503. '<textarea rows="5" class="form-control" type="text" '
  4504. 'id="commit_message"\n name="commit_message" '
  4505. 'placeholder="An optional description of the change">'
  4506. "Signed-off-by: pingou <bar@pingou.com></textarea>",
  4507. output_text,
  4508. )
  4509. def test_edit_file(self):
  4510. """Test the edit_file endpoint."""
  4511. # No Git repo
  4512. output = self.app.get("/foo/edit/foo/f/sources")
  4513. self.assertEqual(output.status_code, 404)
  4514. # Add some content to the git repo
  4515. tests.add_content_git_repo(
  4516. os.path.join(self.path, "repos", "test.git")
  4517. )
  4518. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  4519. tests.add_binary_git_repo(
  4520. os.path.join(self.path, "repos", "test.git"), "test.jpg"
  4521. )
  4522. tests.add_binary_git_repo(
  4523. os.path.join(self.path, "repos", "test.git"), "test_binary"
  4524. )
  4525. user = tests.FakeUser()
  4526. with tests.user_set(self.app.application, user):
  4527. # No project registered in the DB
  4528. output = self.app.get("/foo/edit/foo/f/sources")
  4529. self.assertEqual(output.status_code, 404)
  4530. tests.create_projects(self.session)
  4531. tests.create_projects_git(
  4532. os.path.join(self.path, "repos"), bare=True
  4533. )
  4534. # No a repo admin
  4535. output = self.app.get("/test/edit/master/f/sources")
  4536. self.assertEqual(output.status_code, 403)
  4537. # User not logged in
  4538. output = self.app.get("/test/edit/foo/f/sources")
  4539. self.assertEqual(output.status_code, 302)
  4540. user.username = "pingou"
  4541. with tests.user_set(self.app.application, user):
  4542. output = self.app.get("/test/edit/master/foofile")
  4543. self.assertEqual(output.status_code, 404)
  4544. # Edit page
  4545. output = self.app.get("/test/edit/master/f/sources")
  4546. self.assertEqual(output.status_code, 200)
  4547. output_text = output.get_data(as_text=True)
  4548. self.assertIn(
  4549. '<li><a href="/test/tree/master"><span class="fa fa-random">'
  4550. '</span>&nbsp; master</a></li><li class="active">'
  4551. '<span class="fa fa-file"></span>&nbsp; sources</li>',
  4552. output_text,
  4553. )
  4554. self.assertIn(
  4555. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  4556. output_text,
  4557. )
  4558. # Verify the nav links correctly when editing a file.
  4559. output = self.app.get("/test/blob/master/f/folder1/folder2/file")
  4560. self.assertEqual(output.status_code, 200)
  4561. output_text = output.get_data(as_text=True)
  4562. self.assertIn(
  4563. '<li class="breadcrumb-item"><a href="/test/blob/master/f/folder1/folder2">'
  4564. '\n <span class="fa fa-folder"></span>&nbsp; folder2</a>\n'
  4565. " </li>",
  4566. output_text,
  4567. )
  4568. csrf_token = output_text.split(
  4569. 'name="csrf_token" type="hidden" value="'
  4570. )[1].split('">')[0]
  4571. # View what's supposed to be an image
  4572. output = self.app.get("/test/edit/master/f/test.jpg")
  4573. self.assertEqual(output.status_code, 400)
  4574. output_text = output.get_data(as_text=True)
  4575. self.assertIn("<p>Cannot edit binary files</p>", output_text)
  4576. # Check file before the commit:
  4577. output = self.app.get("/test/raw/master/f/sources")
  4578. self.assertEqual(output.status_code, 200)
  4579. output_text = output.get_data(as_text=True)
  4580. self.assertEqual(output_text, "foo\n bar")
  4581. # No CSRF Token
  4582. data = {
  4583. "content": "foo\n bar\n baz",
  4584. "commit_title": "test commit",
  4585. "commit_message": "Online commits from the gure.lib.get",
  4586. }
  4587. output = self.app.post("/test/edit/master/f/sources", data=data)
  4588. self.assertEqual(output.status_code, 200)
  4589. output_text = output.get_data(as_text=True)
  4590. self.assertIn("<title>Edit - test - Pagure</title>", output_text)
  4591. # Check that nothing changed
  4592. output = self.app.get("/test/raw/master/f/sources")
  4593. self.assertEqual(output.status_code, 200)
  4594. output_text = output.get_data(as_text=True)
  4595. self.assertEqual(output_text, "foo\n bar")
  4596. # Missing email
  4597. data["csrf_token"] = csrf_token
  4598. output = self.app.post("/test/edit/master/f/sources", data=data)
  4599. self.assertEqual(output.status_code, 200)
  4600. output_text = output.get_data(as_text=True)
  4601. self.assertIn("<title>Edit - test - Pagure</title>", output_text)
  4602. # Invalid email
  4603. data["email"] = "pingou@fp.o"
  4604. output = self.app.post("/test/edit/master/f/sources", data=data)
  4605. output_text = output.get_data(as_text=True)
  4606. self.assertIn("<title>Edit - test - Pagure</title>", output_text)
  4607. # Works
  4608. data["email"] = "bar@pingou.com"
  4609. data["branch"] = "master"
  4610. output = self.app.post(
  4611. "/test/edit/master/f/sources", data=data, follow_redirects=True
  4612. )
  4613. self.assertEqual(output.status_code, 200)
  4614. output_text = output.get_data(as_text=True)
  4615. self.assertIn(
  4616. "<title>Commits - test - Pagure</title>", output_text
  4617. )
  4618. self.assertIn("test commit", output_text)
  4619. # Check file after the commit:
  4620. output = self.app.get("/test/raw/master/f/sources")
  4621. self.assertEqual(output.status_code, 200)
  4622. output_text = output.get_data(as_text=True)
  4623. self.assertEqual(output_text, "foo\n bar\n baz")
  4624. # Add a fork of a fork
  4625. item = pagure.lib.model.Project(
  4626. user_id=1, # pingou
  4627. name="test3",
  4628. description="test project #3",
  4629. is_fork=True,
  4630. parent_id=1,
  4631. hook_token="aaabbbppp",
  4632. )
  4633. self.session.add(item)
  4634. self.session.commit()
  4635. tests.add_content_git_repo(
  4636. os.path.join(
  4637. self.path, "repos", "forks", "pingou", "test3.git"
  4638. )
  4639. )
  4640. tests.add_readme_git_repo(
  4641. os.path.join(
  4642. self.path, "repos", "forks", "pingou", "test3.git"
  4643. )
  4644. )
  4645. tests.add_commit_git_repo(
  4646. os.path.join(
  4647. self.path, "repos", "forks", "pingou", "test3.git"
  4648. ),
  4649. ncommits=10,
  4650. )
  4651. # Verify the nav links correctly when editing a file in a fork.
  4652. output = self.app.get(
  4653. "/fork/pingou/test3/edit/master/f/folder1/folder2/file"
  4654. )
  4655. self.assertEqual(output.status_code, 200)
  4656. output_text = output.get_data(as_text=True)
  4657. self.assertIn(
  4658. '<li><a\n href="/fork/pingou/test3/blob/master/f/folder1/folder2"\n'
  4659. ' ><span class="fa fa-folder"></span>&nbsp; folder2</a>\n'
  4660. " </li>",
  4661. output_text,
  4662. )
  4663. output = self.app.get("/fork/pingou/test3/edit/master/f/sources")
  4664. self.assertEqual(output.status_code, 200)
  4665. output_text = output.get_data(as_text=True)
  4666. self.assertIn(
  4667. '<li><a href="/fork/pingou/test3/tree/master">'
  4668. '<span class="fa fa-random">'
  4669. '</span>&nbsp; master</a></li><li class="active">'
  4670. '<span class="fa fa-file"></span>&nbsp; sources</li>',
  4671. output_text,
  4672. )
  4673. self.assertIn(
  4674. '<textarea id="textareaCode" name="content">foo\n barRow 0\n',
  4675. output_text,
  4676. )
  4677. # Empty the file - no `content` provided
  4678. data = {
  4679. "commit_title": "test commit",
  4680. "commit_message": "Online commits from the gure.lib.get",
  4681. "csrf_token": csrf_token,
  4682. "email": "bar@pingou.com",
  4683. "branch": "master",
  4684. }
  4685. output = self.app.post(
  4686. "/test/edit/master/f/sources", data=data, follow_redirects=True
  4687. )
  4688. self.assertEqual(output.status_code, 200)
  4689. output_text = output.get_data(as_text=True)
  4690. self.assertIn(
  4691. "<title>Commits - test - Pagure</title>", output_text
  4692. )
  4693. self.assertIn("test commit", output_text)
  4694. # Check file after the commit:
  4695. output = self.app.get("/test/raw/master/f/sources")
  4696. self.assertEqual(output.status_code, 404)
  4697. output_text = output.get_data(as_text=True)
  4698. self.assertIn("<p>No content found</p>", output_text)
  4699. def test_edit_file_default_email(self):
  4700. """Test the default email shown by the edit_file endpoint."""
  4701. tests.create_projects(self.session)
  4702. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  4703. # Add some content to the git repo
  4704. tests.add_content_git_repo(
  4705. os.path.join(self.path, "repos", "test.git")
  4706. )
  4707. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  4708. user = pagure.lib.query.search_user(self.session, username="pingou")
  4709. self.assertEqual(len(user.emails), 2)
  4710. self.assertEqual(user.default_email, "bar@pingou.com")
  4711. user = tests.FakeUser(username="pingou")
  4712. with tests.user_set(self.app.application, user):
  4713. # Edit page
  4714. output = self.app.get("/test/edit/master/f/sources")
  4715. self.assertEqual(output.status_code, 200)
  4716. output_text = output.get_data(as_text=True)
  4717. self.assertIn(
  4718. '<li><a href="/test/tree/master"><span class="fa fa-random">'
  4719. '</span>&nbsp; master</a></li><li class="active">'
  4720. '<span class="fa fa-file"></span>&nbsp; sources</li>',
  4721. output_text,
  4722. )
  4723. self.assertIn(
  4724. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  4725. output_text,
  4726. )
  4727. self.assertIn(
  4728. '<option value="bar@pingou.com" selected>bar@pingou.com'
  4729. "</option>",
  4730. output_text,
  4731. )
  4732. self.assertIn(
  4733. '<option value="foo@pingou.com" >foo@pingou.com</option>',
  4734. output_text,
  4735. )
  4736. @patch("pagure.decorators.admin_session_timedout")
  4737. def test_change_ref_head(self, ast):
  4738. """Test the change_ref_head endpoint."""
  4739. ast.return_value = True
  4740. # No Git repo
  4741. output = self.app.post("/foo/default/branch/")
  4742. self.assertEqual(output.status_code, 404)
  4743. user = tests.FakeUser()
  4744. with tests.user_set(self.app.application, user):
  4745. output = self.app.post("/foo/default/branch/")
  4746. self.assertEqual(output.status_code, 404)
  4747. ast.return_value = False
  4748. output = self.app.post("/foo/default/branch/")
  4749. self.assertEqual(output.status_code, 404)
  4750. tests.create_projects(self.session)
  4751. repos = tests.create_projects_git(os.path.join(self.path, "repos"))
  4752. output = self.app.post("/test/default/branch/")
  4753. self.assertEqual(output.status_code, 403)
  4754. # User no logged in
  4755. output = self.app.post("/test/default/branch/")
  4756. self.assertEqual(output.status_code, 302)
  4757. user.username = "pingou"
  4758. with tests.user_set(self.app.application, user):
  4759. output = self.app.post(
  4760. "/test/default/branch/", follow_redirects=True
  4761. ) # without git branch
  4762. self.assertEqual(output.status_code, 200)
  4763. output_text = output.get_data(as_text=True)
  4764. self.assertIn(
  4765. "<title>Settings - test - Pagure</title>", output_text
  4766. )
  4767. self.assertIn(
  4768. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  4769. output_text,
  4770. )
  4771. if self.get_wtforms_version() >= (2, 2):
  4772. self.assertIn(
  4773. '<select class="c-select" id="branches" name="branches" '
  4774. "required></select>",
  4775. output_text,
  4776. )
  4777. else:
  4778. self.assertIn(
  4779. '<select class="c-select" id="branches" name="branches">'
  4780. "</select>",
  4781. output_text,
  4782. )
  4783. csrf_token = output_text.split(
  4784. 'name="csrf_token" type="hidden" value="'
  4785. )[1].split('">')[0]
  4786. repo_obj = pygit2.Repository(repos[0])
  4787. tree = repo_obj.index.write_tree()
  4788. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  4789. committer = pygit2.Signature(
  4790. "Cecil Committer", "cecil@committers.tld"
  4791. )
  4792. repo_obj.create_commit(
  4793. "refs/heads/master", # the name of the reference to update
  4794. author,
  4795. committer,
  4796. "Add sources file for testing",
  4797. # binary string representing the tree object ID
  4798. tree,
  4799. # list of binary strings representing parents of the new commit
  4800. [],
  4801. )
  4802. repo_obj.create_branch("feature", repo_obj.head.peel())
  4803. data = {"branches": "feature", "csrf_token": csrf_token}
  4804. # changing head to feature branch
  4805. output = self.app.post(
  4806. "/test/default/branch/", data=data, follow_redirects=True
  4807. )
  4808. self.assertEqual(output.status_code, 200)
  4809. output_text = output.get_data(as_text=True)
  4810. self.assertIn(
  4811. "<title>Settings - test - Pagure</title>", output_text
  4812. )
  4813. self.assertIn(
  4814. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  4815. output_text,
  4816. )
  4817. if self.get_wtforms_version() >= (2, 2):
  4818. self.assertIn(
  4819. '<select class="c-select" id="branches" name="branches" '
  4820. "required>"
  4821. '<option selected value="feature">feature</option>'
  4822. '<option value="master">master</option>'
  4823. "</select>",
  4824. output_text,
  4825. )
  4826. else:
  4827. self.assertIn(
  4828. '<select class="c-select" id="branches" name="branches">'
  4829. '<option selected value="feature">feature</option>'
  4830. '<option value="master">master</option>'
  4831. "</select>",
  4832. output_text,
  4833. )
  4834. self.assertIn("Default branch updated " "to feature", output_text)
  4835. data = {"branches": "master", "csrf_token": csrf_token}
  4836. # changing head to master branch
  4837. output = self.app.post(
  4838. "/test/default/branch/", data=data, follow_redirects=True
  4839. )
  4840. self.assertEqual(output.status_code, 200)
  4841. output_text = output.get_data(as_text=True)
  4842. self.assertIn(
  4843. "<title>Settings - test - Pagure</title>", output_text
  4844. )
  4845. self.assertIn(
  4846. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  4847. output_text,
  4848. )
  4849. if self.get_wtforms_version() >= (2, 2):
  4850. self.assertIn(
  4851. '<select class="c-select" id="branches" name="branches" '
  4852. "required>"
  4853. '<option value="feature">feature</option>'
  4854. '<option selected value="master">master</option>'
  4855. "</select>",
  4856. output_text,
  4857. )
  4858. else:
  4859. self.assertIn(
  4860. '<select class="c-select" id="branches" name="branches">'
  4861. '<option value="feature">feature</option>'
  4862. '<option selected value="master">master</option>'
  4863. "</select>",
  4864. output_text,
  4865. )
  4866. self.assertIn("Default branch updated " "to master", output_text)
  4867. def test_new_release(self):
  4868. """Test the new_release endpoint."""
  4869. # No Git repo
  4870. output = self.app.post("/foo/upload/")
  4871. self.assertEqual(output.status_code, 404)
  4872. user = tests.FakeUser()
  4873. with tests.user_set(self.app.application, user):
  4874. output = self.app.post("/foo/upload/")
  4875. self.assertEqual(output.status_code, 404)
  4876. tests.create_projects(self.session)
  4877. repo = tests.create_projects_git(os.path.join(self.path, "repos"))
  4878. output = self.app.post("/test/upload/")
  4879. self.assertEqual(output.status_code, 403)
  4880. # User not logged in
  4881. output = self.app.post("/test/upload/")
  4882. self.assertEqual(output.status_code, 302)
  4883. user.username = "pingou"
  4884. with tests.user_set(self.app.application, user):
  4885. img = os.path.join(
  4886. os.path.abspath(os.path.dirname(__file__)), "placebo.png"
  4887. )
  4888. # Missing CSRF Token
  4889. with open(img, mode="rb") as stream:
  4890. data = {"filestream": stream}
  4891. output = self.app.post("/test/upload/", data=data)
  4892. self.assertEqual(output.status_code, 200)
  4893. output_text = output.get_data(as_text=True)
  4894. self.assertIn("<h2>Upload a new release</h2>", output_text)
  4895. csrf_token = output_text.split(
  4896. 'name="csrf_token" type="hidden" value="'
  4897. )[1].split('">')[0]
  4898. upload_dir = os.path.join(self.path, "releases")
  4899. self.assertEqual(os.listdir(upload_dir), [])
  4900. # Upload successful
  4901. with open(img, mode="rb") as stream:
  4902. data = {"filestream": stream, "csrf_token": csrf_token}
  4903. output = self.app.post(
  4904. "/test/upload/", data=data, follow_redirects=True
  4905. )
  4906. self.assertEqual(output.status_code, 200)
  4907. output_text = output.get_data(as_text=True)
  4908. self.assertIn("File", output_text)
  4909. self.assertIn("uploaded", output_text)
  4910. self.assertIn("This project has not been tagged.", output_text)
  4911. self.assertEqual(os.listdir(upload_dir), ["test"])
  4912. folder = os.path.join(upload_dir, "test")
  4913. checksum_file = os.path.join(folder, "CHECKSUMS")
  4914. # Check the checksums file
  4915. self.assertTrue(os.path.exists(checksum_file))
  4916. self.assertEqual(len(os.listdir(folder)), 2)
  4917. # Check the content of the checksums file
  4918. with open(checksum_file) as stream:
  4919. data = stream.readlines()
  4920. self.assertEqual(len(data), 3)
  4921. self.assertEqual(data[0], "# Generated and updated by pagure\n")
  4922. self.assertTrue(data[1].startswith("SHA256 ("))
  4923. self.assertTrue(
  4924. data[1].endswith(
  4925. "tests_placebo.png) = 8a06845923010b27bfd8e7e75acff"
  4926. "7badc40d1021b4994e01f5e11ca40bc3abe\n"
  4927. )
  4928. )
  4929. self.assertTrue(data[2].startswith("SHA512 ("))
  4930. self.assertTrue(
  4931. data[2].endswith(
  4932. "tests_placebo.png) = 65a4458df0acb29dc3c5ad4a3620e"
  4933. "98841d1fcf3f8df358f5348fdeddd1a86706491ac6e416768e"
  4934. "9f218aae8147d6ac524a59d3eb91fb925fdcb5c489e55ccbb\n"
  4935. )
  4936. )
  4937. # Try uploading the same file -- fails
  4938. with open(img, mode="rb") as stream:
  4939. data = {"filestream": stream, "csrf_token": csrf_token}
  4940. output = self.app.post(
  4941. "/test/upload/", data=data, follow_redirects=True
  4942. )
  4943. self.assertEqual(output.status_code, 200)
  4944. output_text = output.get_data(as_text=True)
  4945. self.assertIn(
  4946. "This tarball has already " "been uploaded", output_text
  4947. )
  4948. self.assertIn("This project has not been tagged.", output_text)
  4949. def test_new_release_two_files(self):
  4950. """Test the new_release endpoint when uploading two files."""
  4951. tests.create_projects(self.session)
  4952. repo = tests.create_projects_git(os.path.join(self.path, "repos"))
  4953. user = tests.FakeUser(username="pingou")
  4954. with tests.user_set(self.app.application, user):
  4955. img = os.path.join(
  4956. os.path.abspath(os.path.dirname(__file__)), "placebo.png"
  4957. )
  4958. img2 = os.path.join(
  4959. os.path.abspath(os.path.dirname(__file__)), "pagure.png"
  4960. )
  4961. csrf_token = self.get_csrf()
  4962. upload_dir = os.path.join(self.path, "releases")
  4963. self.assertEqual(os.listdir(upload_dir), [])
  4964. # Upload successful
  4965. with open(img, mode="rb") as stream:
  4966. with open(img2, mode="rb") as stream2:
  4967. data = {
  4968. "filestream": [stream, stream2],
  4969. "csrf_token": csrf_token,
  4970. }
  4971. output = self.app.post(
  4972. "/test/upload/", data=data, follow_redirects=True
  4973. )
  4974. self.assertEqual(output.status_code, 200)
  4975. output_text = output.get_data(as_text=True)
  4976. self.assertIn(
  4977. '<i class="fa fa-fw fa-info-circle"></i> File', output_text
  4978. )
  4979. self.assertIn("pagure.png&#34; uploaded</div>\n", output_text)
  4980. # self.assertTrue(0)
  4981. self.assertEqual(os.listdir(upload_dir), ["test"])
  4982. folder = os.path.join(upload_dir, "test")
  4983. checksum_file = os.path.join(folder, "CHECKSUMS")
  4984. # Check the checksums file
  4985. self.assertTrue(os.path.exists(checksum_file))
  4986. self.assertEqual(len(os.listdir(folder)), 3)
  4987. # Check the content of the checksums file
  4988. with open(checksum_file) as stream:
  4989. data = stream.readlines()
  4990. self.assertEqual(len(data), 5)
  4991. self.assertEqual(data[0], "# Generated and updated by pagure\n")
  4992. self.assertTrue(data[1].startswith("SHA256 ("))
  4993. self.assertTrue(
  4994. data[1].endswith(
  4995. "tests_placebo.png) = 8a06845923010b27bfd8e7e75acff"
  4996. "7badc40d1021b4994e01f5e11ca40bc3abe\n"
  4997. )
  4998. )
  4999. self.assertTrue(data[2].startswith("SHA512 ("))
  5000. self.assertTrue(
  5001. data[2].endswith(
  5002. "tests_placebo.png) = 65a4458df0acb29dc3c5ad4a3620e"
  5003. "98841d1fcf3f8df358f5348fdeddd1a86706491ac6e416768e"
  5004. "9f218aae8147d6ac524a59d3eb91fb925fdcb5c489e55ccbb\n"
  5005. )
  5006. )
  5007. self.assertTrue(data[3].startswith("SHA256 ("))
  5008. self.assertTrue(
  5009. data[3].endswith(
  5010. "tests_pagure.png) = 6498a2de405546200b6144da56fc25"
  5011. "d0a3976ae688dbfccaca609c8b4480523e\n"
  5012. )
  5013. )
  5014. self.assertTrue(data[4].startswith("SHA512 ("))
  5015. self.assertTrue(
  5016. data[4].endswith(
  5017. "tests_pagure.png) = 15458775e5d73cd74de7da7224597f6"
  5018. "7f8b23d62d3affb8abba4f5db74d33235642a0f744de2265cca7"
  5019. "d2b5866782c45e1fdeb32dd2822ae33e97995d4879afd\n"
  5020. )
  5021. )
  5022. @patch("pagure.decorators.admin_session_timedout")
  5023. def test_add_token_all_tokens(self, ast):
  5024. """Test the add_token endpoint."""
  5025. ast.return_value = False
  5026. tests.create_projects(self.session)
  5027. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5028. user = tests.FakeUser(username="pingou")
  5029. with tests.user_set(self.app.application, user):
  5030. output = self.app.get("/test/token/new/")
  5031. self.assertEqual(output.status_code, 200)
  5032. output_text = output.get_data(as_text=True)
  5033. self.assertIn("<strong>Create a new token</strong>", output_text)
  5034. self.assertEqual(
  5035. output_text.count('<label class="c-input c-checkbox">'),
  5036. len(pagure.config.config["ACLS"].keys()) - 1,
  5037. )
  5038. @patch.dict("pagure.config.config", {"USER_ACLS": ["create_project"]})
  5039. @patch("pagure.decorators.admin_session_timedout")
  5040. def test_add_token_one_token(self, ast):
  5041. """Test the add_token endpoint."""
  5042. ast.return_value = False
  5043. tests.create_projects(self.session)
  5044. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5045. user = tests.FakeUser(username="pingou")
  5046. with tests.user_set(self.app.application, user):
  5047. output = self.app.get("/test/token/new/")
  5048. self.assertEqual(output.status_code, 200)
  5049. output_text = output.get_data(as_text=True)
  5050. self.assertIn("<strong>Create a new token</strong>", output_text)
  5051. self.assertEqual(
  5052. output_text.count('<label class="c-input c-checkbox">'), 1
  5053. )
  5054. @patch("pagure.decorators.admin_session_timedout")
  5055. def test_add_token(self, ast):
  5056. """Test the add_token endpoint."""
  5057. ast.return_value = False
  5058. # No Git repo
  5059. output = self.app.get("/foo/token/new/")
  5060. self.assertEqual(output.status_code, 404)
  5061. user = tests.FakeUser()
  5062. with tests.user_set(self.app.application, user):
  5063. output = self.app.get("/foo/token/new/")
  5064. self.assertEqual(output.status_code, 404)
  5065. tests.create_projects(self.session)
  5066. tests.create_projects_git(
  5067. os.path.join(self.path, "repos"), bare=True
  5068. )
  5069. output = self.app.get("/test/token/new/")
  5070. self.assertEqual(output.status_code, 403)
  5071. # User not logged in
  5072. output = self.app.get("/test/token/new/")
  5073. self.assertEqual(output.status_code, 302)
  5074. user.username = "pingou"
  5075. with tests.user_set(self.app.application, user):
  5076. output = self.app.get("/test/token/new/")
  5077. self.assertEqual(output.status_code, 200)
  5078. output_text = output.get_data(as_text=True)
  5079. self.assertIn("<strong>Create a new token</strong>", output_text)
  5080. csrf_token = output_text.split(
  5081. 'name="csrf_token" type="hidden" value="'
  5082. )[1].split('">')[0]
  5083. data = {"csrf_token": csrf_token}
  5084. ast.return_value = True
  5085. # Test when the session timed-out
  5086. output = self.app.post("/test/token/new/", data=data)
  5087. self.assertEqual(output.status_code, 302)
  5088. output = self.app.get("/", follow_redirects=True)
  5089. self.assertEqual(output.status_code, 200)
  5090. output_text = output.get_data(as_text=True)
  5091. self.assertIn("Action canceled, try it " "again", output_text)
  5092. ast.return_value = False
  5093. # Missing acls
  5094. output = self.app.post("/test/token/new/", data=data)
  5095. self.assertEqual(output.status_code, 200)
  5096. output_text = output.get_data(as_text=True)
  5097. self.assertIn("<strong>Create a new token</strong>", output_text)
  5098. self.assertIn(
  5099. "You must select at least " "one permission.", output_text
  5100. )
  5101. data = {
  5102. "csrf_token": csrf_token,
  5103. "acls": ["issue_create"],
  5104. "description": "Test token",
  5105. }
  5106. # New token created
  5107. output = self.app.post(
  5108. "/test/token/new/", data=data, follow_redirects=True
  5109. )
  5110. self.assertEqual(output.status_code, 200)
  5111. output_text = output.get_data(as_text=True)
  5112. self.assertIn("Token created", output_text)
  5113. self.assertIn(
  5114. "<title>Settings - test - Pagure</title>", output_text
  5115. )
  5116. self.assertIn(
  5117. '<h5 class="pl-2 font-weight-bold text-muted">Project Settings</h5>',
  5118. output_text,
  5119. )
  5120. self.assertIn("<strong> Test token</strong>", output_text)
  5121. self.assertIn(
  5122. '<small class="font-weight-bold">Active until', output_text
  5123. )
  5124. @patch("pagure.decorators.admin_session_timedout")
  5125. def test_revoke_api_token(self, ast):
  5126. """Test the revoke_api_token endpoint."""
  5127. ast.return_value = False
  5128. # No Git repo
  5129. output = self.app.post("/foo/token/revoke/123")
  5130. self.assertEqual(output.status_code, 404)
  5131. user = tests.FakeUser()
  5132. with tests.user_set(self.app.application, user):
  5133. output = self.app.post("/foo/token/revoke/123")
  5134. self.assertEqual(output.status_code, 404)
  5135. tests.create_projects(self.session)
  5136. tests.create_projects_git(
  5137. os.path.join(self.path, "repos"), bare=True
  5138. )
  5139. output = self.app.post("/test/token/revoke/123")
  5140. self.assertEqual(output.status_code, 403)
  5141. # User not logged in
  5142. output = self.app.post("/test/token/revoke/123")
  5143. self.assertEqual(output.status_code, 302)
  5144. user.username = "pingou"
  5145. with tests.user_set(self.app.application, user):
  5146. output = self.app.get("/test/token/new")
  5147. self.assertEqual(output.status_code, 200)
  5148. output_text = output.get_data(as_text=True)
  5149. self.assertIn("<strong>Create a new token</strong>", output_text)
  5150. csrf_token = output_text.split(
  5151. 'name="csrf_token" type="hidden" value="'
  5152. )[1].split('">')[0]
  5153. data = {"csrf_token": csrf_token}
  5154. ast.return_value = True
  5155. # Test when the session timed-out
  5156. output = self.app.post("/test/token/revoke/123", data=data)
  5157. self.assertEqual(output.status_code, 302)
  5158. output = self.app.get("/", follow_redirects=True)
  5159. self.assertEqual(output.status_code, 200)
  5160. output_text = output.get_data(as_text=True)
  5161. self.assertIn("Action canceled, try it again", output_text)
  5162. ast.return_value = False
  5163. output = self.app.post("/test/token/revoke/123", data=data)
  5164. self.assertEqual(output.status_code, 404)
  5165. output_text = output.get_data(as_text=True)
  5166. self.assertIn("<p>Token not found</p>", output_text)
  5167. # Create a token to revoke
  5168. data = {"csrf_token": csrf_token, "acls": ["issue_create"]}
  5169. output = self.app.post(
  5170. "/test/token/new/", data=data, follow_redirects=True
  5171. )
  5172. self.assertEqual(output.status_code, 200)
  5173. output_text = output.get_data(as_text=True)
  5174. self.assertIn("Token created", output_text)
  5175. # Existing token will expire in 60 days
  5176. repo = pagure.lib.query.get_authorized_project(
  5177. self.session, "test"
  5178. )
  5179. self.assertEqual(
  5180. repo.tokens[0].expiration.date(),
  5181. datetime.datetime.utcnow().date()
  5182. + datetime.timedelta(days=(30 * 6)),
  5183. )
  5184. token = repo.tokens[0].id
  5185. output = self.app.post(
  5186. "/test/token/revoke/%s" % token,
  5187. data=data,
  5188. follow_redirects=True,
  5189. )
  5190. output_text = output.get_data(as_text=True)
  5191. self.assertIn(
  5192. "<title>Settings - test - Pagure</title>", output_text
  5193. )
  5194. self.assertIn("Token revoked", output_text)
  5195. self.assertEqual(output_text.count('title="Revoke token">'), 0)
  5196. self.assertEqual(output_text.count('title="Renew token">'), 1)
  5197. # Existing token has been expired
  5198. self.session.commit()
  5199. repo = pagure.lib.query.get_authorized_project(
  5200. self.session, "test"
  5201. )
  5202. self.assertEqual(
  5203. repo.tokens[0].expiration.date(), repo.tokens[0].created.date()
  5204. )
  5205. self.assertEqual(
  5206. repo.tokens[0].expiration.date(),
  5207. datetime.datetime.utcnow().date(),
  5208. )
  5209. @patch("pagure.decorators.admin_session_timedout")
  5210. def test_renew_api_token(self, ast):
  5211. """Test the renew_api_token endpoint."""
  5212. ast.return_value = False
  5213. # No Git repo
  5214. output = self.app.post("/foo/token/renew/123")
  5215. self.assertEqual(output.status_code, 404)
  5216. user = tests.FakeUser()
  5217. with tests.user_set(self.app.application, user):
  5218. # user logged in but still no git repo
  5219. output = self.app.post("/foo/token/renew/123")
  5220. self.assertEqual(output.status_code, 404)
  5221. tests.create_projects(self.session)
  5222. tests.create_projects_git(
  5223. os.path.join(self.path, "repos"), bare=True
  5224. )
  5225. # user logged in, git repo present, but user doesn't have access
  5226. output = self.app.post("/test/token/renew/123")
  5227. self.assertEqual(output.status_code, 403)
  5228. # User not logged in
  5229. output = self.app.post("/test/token/renew/123")
  5230. self.assertEqual(output.status_code, 302)
  5231. user.username = "pingou"
  5232. with tests.user_set(self.app.application, user):
  5233. output = self.app.get("/test/token/new")
  5234. self.assertEqual(output.status_code, 200)
  5235. output_text = output.get_data(as_text=True)
  5236. self.assertIn("<strong>Create a new token</strong>", output_text)
  5237. csrf_token = self.get_csrf(output=output)
  5238. data = {"csrf_token": csrf_token}
  5239. ast.return_value = True
  5240. # Test when the session timed-out
  5241. output = self.app.post("/test/token/renew/123", data=data)
  5242. self.assertEqual(output.status_code, 302)
  5243. output = self.app.get("/", follow_redirects=True)
  5244. self.assertEqual(output.status_code, 200)
  5245. output_text = output.get_data(as_text=True)
  5246. self.assertIn("Action canceled, try it again", output_text)
  5247. ast.return_value = False
  5248. output = self.app.post("/test/token/renew/123", data=data)
  5249. self.assertEqual(output.status_code, 404)
  5250. output_text = output.get_data(as_text=True)
  5251. self.assertIn("<p>Token not found</p>", output_text)
  5252. # Create a token to renew
  5253. data = {"csrf_token": csrf_token, "acls": ["issue_create"]}
  5254. output = self.app.post(
  5255. "/test/token/new/", data=data, follow_redirects=True
  5256. )
  5257. self.assertEqual(output.status_code, 200)
  5258. output_text = output.get_data(as_text=True)
  5259. self.assertIn("Token created", output_text)
  5260. # 1 token associated with the project, expires in 60 days
  5261. repo = pagure.lib.query.get_authorized_project(
  5262. self.session, "test"
  5263. )
  5264. self.assertEqual(len(repo.tokens), 1)
  5265. self.assertEqual(
  5266. repo.tokens[0].expiration.date(),
  5267. datetime.datetime.utcnow().date()
  5268. + datetime.timedelta(days=(30 * 6)),
  5269. )
  5270. token = repo.tokens[0].id
  5271. output = self.app.post(
  5272. "/test/token/renew/%s" % token,
  5273. data=data,
  5274. follow_redirects=True,
  5275. )
  5276. output_text = output.get_data(as_text=True)
  5277. self.assertIn(
  5278. "<title>Settings - test - Pagure</title>", output_text
  5279. )
  5280. self.assertIn("Token created", output_text)
  5281. self.assertEqual(output_text.count('title="Revoke token">'), 2)
  5282. self.assertEqual(output_text.count('title="Renew token">'), 2)
  5283. # Existing token has been renewed
  5284. self.session.commit()
  5285. repo = pagure.lib.query.get_authorized_project(
  5286. self.session, "test"
  5287. )
  5288. self.assertEqual(len(repo.tokens), 2)
  5289. self.assertEqual(
  5290. repo.tokens[0].expiration.date(),
  5291. repo.tokens[1].expiration.date(),
  5292. )
  5293. self.assertEqual(
  5294. repo.tokens[0].created.date(), repo.tokens[1].created.date()
  5295. )
  5296. self.assertEqual(repo.tokens[0].acls, repo.tokens[1].acls)
  5297. self.assertEqual(
  5298. repo.tokens[0].description, repo.tokens[1].description
  5299. )
  5300. def test_delete_branch(self):
  5301. """Test the delete_branch endpoint."""
  5302. # No Git repo
  5303. output = self.app.post("/foo/b/master/delete")
  5304. self.assertEqual(output.status_code, 404)
  5305. tests.create_projects(self.session)
  5306. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5307. # User not logged in
  5308. output = self.app.post("/test/b/master/delete")
  5309. self.assertEqual(output.status_code, 302)
  5310. user = tests.FakeUser()
  5311. with tests.user_set(self.app.application, user):
  5312. # Unknown repo
  5313. output = self.app.post("/foo/b/master/delete")
  5314. self.assertEqual(output.status_code, 404)
  5315. output = self.app.post("/test/b/master/delete")
  5316. self.assertEqual(output.status_code, 403)
  5317. user.username = "pingou"
  5318. with tests.user_set(self.app.application, user):
  5319. output = self.app.post("/test/b/master/delete")
  5320. self.assertEqual(output.status_code, 403)
  5321. output_text = output.get_data(as_text=True)
  5322. self.assertIn(
  5323. "<p>You are not allowed to delete the default branch: master</p>",
  5324. output_text,
  5325. )
  5326. output = self.app.post("/test/b/bar/delete")
  5327. self.assertEqual(output.status_code, 404)
  5328. output_text = output.get_data(as_text=True)
  5329. self.assertIn("<p>Branch not found</p>", output_text)
  5330. # Add a branch that we can delete
  5331. path = os.path.join(self.path, "repos", "test.git")
  5332. tests.add_content_git_repo(path)
  5333. repo = pygit2.Repository(path)
  5334. repo.create_branch("foo", repo.head.peel())
  5335. # Check before deletion
  5336. output = self.app.get("/test")
  5337. self.assertEqual(output.status_code, 200)
  5338. output = self.app.get("/test/branches")
  5339. output_text = output.get_data(as_text=True)
  5340. self.assertIn('<form id="delete_branch_form-foo"', output_text)
  5341. # Delete the branch
  5342. output = self.app.post("/test/b/foo/delete", follow_redirects=True)
  5343. self.assertEqual(output.status_code, 200)
  5344. output = self.app.get("/test/branches")
  5345. output_text = output.get_data(as_text=True)
  5346. self.assertNotIn('<form id="delete_branch_form-foo"', output_text)
  5347. # Add a branch with a '/' in its name that we can delete
  5348. path = os.path.join(self.path, "repos", "test.git")
  5349. tests.add_content_git_repo(path)
  5350. repo = pygit2.Repository(path)
  5351. repo.create_branch("feature/foo", repo.head.peel())
  5352. # Check before deletion
  5353. output = self.app.get("/test")
  5354. self.assertEqual(output.status_code, 200)
  5355. output = self.app.get("/test/branches")
  5356. output_text = output.get_data(as_text=True)
  5357. self.assertIn(
  5358. '<form id="delete_branch_form-feature__foo"', output_text
  5359. )
  5360. # Delete the branch
  5361. output = self.app.post(
  5362. "/test/b/feature/foo/delete", follow_redirects=True
  5363. )
  5364. self.assertEqual(output.status_code, 200)
  5365. output = self.app.get("/test/branches")
  5366. output_text = output.get_data(as_text=True)
  5367. self.assertNotIn(
  5368. '<form id="delete_branch_form-feature__foo"', output_text
  5369. )
  5370. def test_delete_branch_unicode(self):
  5371. """Test the delete_branch endpoint with an unicode branch."""
  5372. tests.create_projects(self.session)
  5373. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5374. user = tests.FakeUser(username="pingou")
  5375. with tests.user_set(self.app.application, user):
  5376. # Add a branch that we can delete
  5377. path = os.path.join(self.path, "repos", "test.git")
  5378. tests.add_content_git_repo(path)
  5379. repo = pygit2.Repository(path)
  5380. branchname = "☃️"
  5381. if six.PY2:
  5382. branchname = branchname.encode("utf-8")
  5383. repo.create_branch(branchname, repo.head.peel())
  5384. # Check before deletion
  5385. output = self.app.get("/test")
  5386. self.assertEqual(output.status_code, 200)
  5387. output = self.app.get("/test/branches")
  5388. output_text = output.get_data(as_text=True)
  5389. self.assertIn('<form id="delete_branch_form-☃️"', output_text)
  5390. # Delete the branch
  5391. output = self.app.post("/test/b/☃️/delete", follow_redirects=True)
  5392. self.assertEqual(output.status_code, 200)
  5393. # Check after deletion
  5394. output = self.app.get("/test/branches")
  5395. output_text = output.get_data(as_text=True)
  5396. self.assertNotIn('<form id="delete_branch_form-☃️"', output_text)
  5397. def test_delete_branch_master(self):
  5398. """Test the delete_branch endpoint with the master branch."""
  5399. tests.create_projects(self.session)
  5400. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5401. project = pagure.lib.query._get_project(self.session, "test")
  5402. user = tests.FakeUser(username="pingou")
  5403. with tests.user_set(self.app.application, user):
  5404. # Add a branch that we can delete
  5405. path = os.path.join(self.path, "repos", "test.git")
  5406. tests.add_content_git_repo(path)
  5407. repo = pygit2.Repository(path)
  5408. branchname = "main"
  5409. repo.create_branch(branchname, repo.head.peel())
  5410. # Make that branch be the default one:
  5411. pagure.lib.git.git_set_ref_head(project=project, branch=branchname)
  5412. # Check before deletion
  5413. output = self.app.get("/test")
  5414. self.assertEqual(output.status_code, 200)
  5415. output = self.app.get("/test/branches")
  5416. output_text = output.get_data(as_text=True)
  5417. self.assertIn('<form id="delete_branch_form-master"', output_text)
  5418. self.assertNotIn('<form id="delete_branch_form-main"', output_text)
  5419. # Delete the branch
  5420. output = self.app.post(
  5421. "/test/b/master/delete", follow_redirects=True
  5422. )
  5423. self.assertEqual(output.status_code, 200)
  5424. # Check after deletion
  5425. output = self.app.get("/test/branches")
  5426. output_text = output.get_data(as_text=True)
  5427. self.assertNotIn(
  5428. '<form id="delete_branch_form-master"', output_text
  5429. )
  5430. self.assertNotIn('<form id="delete_branch_form-main"', output_text)
  5431. @patch.dict("pagure.config.config", {"ALLOW_DELETE_BRANCH": False})
  5432. def test_delete_branch_disabled_in_ui(self):
  5433. """Test that the delete branch button doesn't show when the feature
  5434. is turned off."""
  5435. tests.create_projects(self.session)
  5436. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5437. # Add a branch that we can delete
  5438. path = os.path.join(self.path, "repos", "test.git")
  5439. tests.add_content_git_repo(path)
  5440. repo = pygit2.Repository(path)
  5441. repo.create_branch("foo", repo.head.peel())
  5442. user = tests.FakeUser(username="pingou")
  5443. with tests.user_set(self.app.application, user):
  5444. # Check that the UI doesn't offer the button
  5445. output = self.app.get("/test")
  5446. self.assertEqual(output.status_code, 200)
  5447. output_text = output.get_data(as_text=True)
  5448. self.assertNotIn('<form id="delete_branch_form-foo"', output_text)
  5449. self.assertNotIn(
  5450. "Are you sure you want to remove the branch", output_text
  5451. )
  5452. @patch.dict("pagure.config.config", {"ALLOW_DELETE_BRANCH": False})
  5453. def test_delete_branch_disabled(self):
  5454. """Test the delete_branch endpoint when it's disabled in the entire
  5455. instance."""
  5456. tests.create_projects(self.session)
  5457. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5458. # Add a branch that we can delete
  5459. path = os.path.join(self.path, "repos", "test.git")
  5460. tests.add_content_git_repo(path)
  5461. repo = pygit2.Repository(path)
  5462. repo.create_branch("foo", repo.head.peel())
  5463. user = tests.FakeUser(username="pingou")
  5464. with tests.user_set(self.app.application, user):
  5465. # Check if the delete branch button does not show
  5466. output = self.app.get("/test/branches")
  5467. self.assertEqual(output.status_code, 200)
  5468. self.assertNotIn(
  5469. 'title="Remove branch foo"', output.get_data(as_text=True)
  5470. )
  5471. # Delete the branch
  5472. output = self.app.post("/test/b/foo/delete", follow_redirects=True)
  5473. self.assertEqual(output.status_code, 404)
  5474. self.assertIn(
  5475. "This pagure instance does not allow branch deletion",
  5476. output.get_data(as_text=True),
  5477. )
  5478. @patch.dict("pagure.config.config", {"ALLOW_DELETE_BRANCH": False})
  5479. def test_delete_branch_disabled_fork(self):
  5480. """Test the delete_branch endpoint when it's disabled in the entire
  5481. instance."""
  5482. item = pagure.lib.model.Project(
  5483. user_id=2, # foo
  5484. name="test",
  5485. description="test project #1",
  5486. hook_token="aaabbb",
  5487. is_fork=True,
  5488. parent_id=1,
  5489. )
  5490. self.session.add(item)
  5491. self.session.commit()
  5492. tests.create_projects_git(
  5493. os.path.join(self.path, "repos", "forks", "foo"), bare=True
  5494. )
  5495. # Add a branch that we can delete
  5496. path = os.path.join(self.path, "repos", "forks", "foo", "test.git")
  5497. tests.add_content_git_repo(path)
  5498. repo = pygit2.Repository(path)
  5499. repo.create_branch("foo", repo.head.peel())
  5500. user = tests.FakeUser(username="foo")
  5501. with tests.user_set(self.app.application, user):
  5502. # Check if the delete branch button shows
  5503. output = self.app.get("/fork/foo/test/branches")
  5504. self.assertEqual(output.status_code, 200)
  5505. self.assertIn(
  5506. 'title="Remove branch foo"', output.get_data(as_text=True)
  5507. )
  5508. # Delete the branch
  5509. output = self.app.post(
  5510. "/fork/foo/test/b/foo/delete", follow_redirects=True
  5511. )
  5512. self.assertEqual(output.status_code, 200)
  5513. # Check if the delete branch button no longer appears
  5514. output = self.app.get("/fork/foo/test/branches")
  5515. self.assertEqual(output.status_code, 200)
  5516. self.assertNotIn(
  5517. 'title="Remove branch foo"', output.get_data(as_text=True)
  5518. )
  5519. def test_view_docs(self):
  5520. """Test the view_docs endpoint."""
  5521. output = self.app.get("/docs/foo/")
  5522. # No project registered in the DB
  5523. self.assertEqual(output.status_code, 404)
  5524. tests.create_projects(self.session)
  5525. output = self.app.get("/docs/test/")
  5526. # No git repo associated
  5527. self.assertEqual(output.status_code, 404)
  5528. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5529. output = self.app.get("/docs/test/")
  5530. self.assertEqual(output.status_code, 404)
  5531. def test_view_project_activity(self):
  5532. """Test the view_project_activity endpoint."""
  5533. tests.create_projects(self.session)
  5534. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5535. # Project Exists, but No DATAGREPPER_URL set
  5536. output = self.app.get("/test/activity/")
  5537. self.assertEqual(output.status_code, 404)
  5538. # Project Exists, and DATAGREPPER_URL set
  5539. pagure.config.config["DATAGREPPER_URL"] = "foo"
  5540. output = self.app.get("/test/activity/")
  5541. self.assertEqual(output.status_code, 200)
  5542. output_text = output.get_data(as_text=True)
  5543. self.assertIn("<title>Activity - test - Pagure</title>", output_text)
  5544. self.assertIn("No activity reported on the test project", output_text)
  5545. # project doesnt exist
  5546. output = self.app.get("/foo/activity/")
  5547. self.assertEqual(output.status_code, 404)
  5548. def test_goimport(self):
  5549. """Test the go-import tag."""
  5550. tests.create_projects(self.session)
  5551. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5552. output = self.app.get("/test/")
  5553. self.assertEqual(output.status_code, 200)
  5554. output_text = output.get_data(as_text=True)
  5555. self.assertIn(
  5556. '<meta name="go-import" '
  5557. 'content="localhost.localdomain/test git git://localhost.localdomain/test.git"'
  5558. ">",
  5559. output_text,
  5560. )
  5561. def test_watch_repo(self):
  5562. """Test the watch_repo endpoint."""
  5563. output = self.app.post("/watch/")
  5564. self.assertEqual(output.status_code, 405)
  5565. tests.create_projects(self.session)
  5566. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5567. user = tests.FakeUser()
  5568. user.username = "pingou"
  5569. with tests.user_set(self.app.application, user):
  5570. output = self.app.get("/new/")
  5571. self.assertEqual(output.status_code, 200)
  5572. output_text = output.get_data(as_text=True)
  5573. self.assertIn("<strong>Create new Project</strong>", output_text)
  5574. csrf_token = output_text.split(
  5575. 'name="csrf_token" type="hidden" value="'
  5576. )[1].split('">')[0]
  5577. data = {"csrf_token": csrf_token}
  5578. output = self.app.post(
  5579. "/foo/watch/settings/1", data=data, follow_redirects=True
  5580. )
  5581. self.assertEqual(output.status_code, 404)
  5582. output = self.app.post(
  5583. "/test/watch/settings/8", data=data, follow_redirects=True
  5584. )
  5585. self.assertEqual(output.status_code, 400)
  5586. output = self.app.post(
  5587. "/test/watch/settings/0", data=data, follow_redirects=True
  5588. )
  5589. output_text = output.get_data(as_text=True)
  5590. self.assertIn(
  5591. "You are no longer" " watching this project", output_text
  5592. )
  5593. output = self.app.post(
  5594. "/test/watch/settings/1", data=data, follow_redirects=True
  5595. )
  5596. output_text = output.get_data(as_text=True)
  5597. self.assertIn(
  5598. "You are now" " watching issues and PRs on this project",
  5599. output_text,
  5600. )
  5601. output = self.app.post(
  5602. "/test/watch/settings/2", data=data, follow_redirects=True
  5603. )
  5604. output_text = output.get_data(as_text=True)
  5605. self.assertIn(
  5606. "You are now" " watching commits on this project", output_text
  5607. )
  5608. output = self.app.post(
  5609. "/test/watch/settings/3", data=data, follow_redirects=True
  5610. )
  5611. output_text = output.get_data(as_text=True)
  5612. self.assertIn(
  5613. (
  5614. "You are now"
  5615. " watching issues, PRs, and commits on this project"
  5616. ),
  5617. output_text,
  5618. )
  5619. item = pagure.lib.model.Project(
  5620. user_id=2, # foo
  5621. name="test",
  5622. description="foo project #1",
  5623. hook_token="aaabbb",
  5624. is_fork=True,
  5625. parent_id=1,
  5626. )
  5627. self.session.add(item)
  5628. self.session.commit()
  5629. gitrepo = os.path.join(
  5630. self.path, "repos", "forks", "foo", "test.git"
  5631. )
  5632. pygit2.init_repository(gitrepo, bare=True)
  5633. output = self.app.post(
  5634. "/fork/foo/test/watch/settings/-1",
  5635. data=data,
  5636. follow_redirects=True,
  5637. )
  5638. output_text = output.get_data(as_text=True)
  5639. self.assertIn("Watch status is already reset", output_text)
  5640. output = self.app.post(
  5641. "/fork/foo/test/watch/settings/0",
  5642. data=data,
  5643. follow_redirects=True,
  5644. )
  5645. output_text = output.get_data(as_text=True)
  5646. self.assertIn(
  5647. "You are no longer" " watching this project", output_text
  5648. )
  5649. output = self.app.get("/test", data=data, follow_redirects=True)
  5650. output_text = output.get_data(as_text=True)
  5651. self.assertIn(
  5652. (
  5653. '<span class="btn btn-sm btn-primary font-weight-bold">1'
  5654. "</span>\n "
  5655. '<div class="dropdown-menu dropdown-menu-right watch-menu">'
  5656. ),
  5657. output_text,
  5658. )
  5659. output = self.app.post(
  5660. "/fork/foo/test/watch/settings/1",
  5661. data=data,
  5662. follow_redirects=True,
  5663. )
  5664. output_text = output.get_data(as_text=True)
  5665. self.assertIn(
  5666. "You are now" " watching issues and PRs on this project",
  5667. output_text,
  5668. )
  5669. output = self.app.get("/test", data=data, follow_redirects=True)
  5670. output_text = output.get_data(as_text=True)
  5671. self.assertIn(
  5672. (
  5673. '<span class="btn btn-sm btn-primary font-weight-bold">1'
  5674. "</span>\n "
  5675. '<div class="dropdown-menu dropdown-menu-right watch-menu">'
  5676. ),
  5677. output_text,
  5678. )
  5679. output = self.app.post(
  5680. "/fork/foo/test/watch/settings/2",
  5681. data=data,
  5682. follow_redirects=True,
  5683. )
  5684. output_text = output.get_data(as_text=True)
  5685. self.assertIn(
  5686. "You are now" " watching commits on this project", output_text
  5687. )
  5688. output = self.app.post(
  5689. "/fork/foo/test/watch/settings/3",
  5690. data=data,
  5691. follow_redirects=True,
  5692. )
  5693. output_text = output.get_data(as_text=True)
  5694. self.assertIn(
  5695. (
  5696. "You are now"
  5697. " watching issues, PRs, and commits on this project"
  5698. ),
  5699. output_text,
  5700. )
  5701. output = self.app.get("/test", data=data, follow_redirects=True)
  5702. output_text = output.get_data(as_text=True)
  5703. self.assertIn(
  5704. (
  5705. '<span class="btn btn-sm btn-primary font-weight-bold">1'
  5706. "</span>\n "
  5707. '<div class="dropdown-menu dropdown-menu-right watch-menu">'
  5708. ),
  5709. output_text,
  5710. )
  5711. project = pagure.lib.query._get_project(self.session, "test")
  5712. pagure.lib.query.add_user_to_project(
  5713. self.session,
  5714. project,
  5715. new_user="foo",
  5716. user="pingou",
  5717. access="commit",
  5718. )
  5719. self.session.commit()
  5720. output = self.app.get("/test", data=data, follow_redirects=True)
  5721. output_text = output.get_data(as_text=True)
  5722. self.assertIn(
  5723. (
  5724. '<span class="btn btn-sm btn-primary font-weight-bold">2'
  5725. "</span>\n "
  5726. '<div class="dropdown-menu dropdown-menu-right watch-menu">'
  5727. ),
  5728. output_text,
  5729. )
  5730. output = self.app.post(
  5731. "/fork/foo/test/watch/settings/-1",
  5732. data=data,
  5733. follow_redirects=True,
  5734. )
  5735. output_text = output.get_data(as_text=True)
  5736. self.assertIn("Watch status reset", output_text)
  5737. output = self.app.get("/test", data=data, follow_redirects=True)
  5738. output_text = output.get_data(as_text=True)
  5739. self.assertIn(
  5740. (
  5741. '<span class="btn btn-sm btn-primary font-weight-bold">2'
  5742. "</span>\n "
  5743. '<div class="dropdown-menu dropdown-menu-right watch-menu">'
  5744. ),
  5745. output_text,
  5746. )
  5747. def test_delete_report(self):
  5748. """Test the delete_report endpoint."""
  5749. output = self.app.post("/test/delete/report")
  5750. self.assertEqual(output.status_code, 404)
  5751. tests.create_projects(self.session)
  5752. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5753. user = tests.FakeUser()
  5754. user.username = "pingou"
  5755. with tests.user_set(self.app.application, user):
  5756. output = self.app.get("/new/")
  5757. self.assertEqual(output.status_code, 200)
  5758. output_text = output.get_data(as_text=True)
  5759. self.assertIn("<strong>Create new Project</strong>", output_text)
  5760. csrf_token = output_text.split(
  5761. 'name="csrf_token" type="hidden" value="'
  5762. )[1].split('">')[0]
  5763. # No report specified
  5764. data = {"csrf_token": csrf_token}
  5765. output = self.app.post(
  5766. "/test/delete/report", data=data, follow_redirects=True
  5767. )
  5768. self.assertEqual(output.status_code, 200)
  5769. output_text = output.get_data(as_text=True)
  5770. self.assertIn("Unknown report: None", output_text)
  5771. # Report specified not in the project's reports
  5772. data = {"csrf_token": csrf_token, "report": "foo"}
  5773. output = self.app.post(
  5774. "/test/delete/report", data=data, follow_redirects=True
  5775. )
  5776. self.assertEqual(output.status_code, 200)
  5777. output_text = output.get_data(as_text=True)
  5778. self.assertIn("Unknown report: foo", output_text)
  5779. # Create a report
  5780. project = pagure.lib.query.get_authorized_project(
  5781. self.session, project_name="test"
  5782. )
  5783. self.assertEqual(project.reports, {})
  5784. name = "test report"
  5785. url = "?foo=bar&baz=biz"
  5786. pagure.lib.query.save_report(
  5787. self.session, repo=project, name=name, url=url, username=None
  5788. )
  5789. self.session.commit()
  5790. project = pagure.lib.query.get_authorized_project(
  5791. self.session, project_name="test"
  5792. )
  5793. self.assertEqual(
  5794. project.reports, {"test report": {"baz": "biz", "foo": "bar"}}
  5795. )
  5796. # Missing CSRF
  5797. data = {"report": "test report"}
  5798. output = self.app.post(
  5799. "/test/delete/report", data=data, follow_redirects=True
  5800. )
  5801. self.assertEqual(output.status_code, 200)
  5802. output_text = output.get_data(as_text=True)
  5803. self.assertIn(
  5804. "<title>Settings - test - Pagure</title>", output_text
  5805. )
  5806. project = pagure.lib.query.get_authorized_project(
  5807. self.session, project_name="test"
  5808. )
  5809. self.assertEqual(
  5810. project.reports, {"test report": {"baz": "biz", "foo": "bar"}}
  5811. )
  5812. # Delete the report
  5813. data = {"csrf_token": csrf_token, "report": "test report"}
  5814. output = self.app.post(
  5815. "/test/delete/report", data=data, follow_redirects=True
  5816. )
  5817. self.assertEqual(output.status_code, 200)
  5818. output_text = output.get_data(as_text=True)
  5819. self.assertIn("List of reports updated", output_text)
  5820. self.session.commit()
  5821. project = pagure.lib.query.get_authorized_project(
  5822. self.session, project_name="test"
  5823. )
  5824. self.assertEqual(project.reports, {})
  5825. def test_delete_report_ns_project(self):
  5826. """Test the delete_report endpoint on a namespaced project."""
  5827. output = self.app.post("/foo/test/delete/report")
  5828. self.assertEqual(output.status_code, 404)
  5829. tests.create_projects(self.session)
  5830. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5831. user = tests.FakeUser()
  5832. user.username = "pingou"
  5833. with tests.user_set(self.app.application, user):
  5834. output = self.app.get("/new/")
  5835. self.assertEqual(output.status_code, 200)
  5836. output_text = output.get_data(as_text=True)
  5837. self.assertIn("<strong>Create new Project</strong>", output_text)
  5838. csrf_token = output_text.split(
  5839. 'name="csrf_token" type="hidden" value="'
  5840. )[1].split('">')[0]
  5841. item = pagure.lib.model.Project(
  5842. user_id=1, # pingou
  5843. namespace="foo",
  5844. name="test",
  5845. description="foo project #2",
  5846. hook_token="aaabbb",
  5847. )
  5848. self.session.add(item)
  5849. self.session.commit()
  5850. gitrepo = os.path.join(self.path, "repos", "foo", "test.git")
  5851. pygit2.init_repository(gitrepo, bare=True)
  5852. # No report specified
  5853. data = {"csrf_token": csrf_token}
  5854. output = self.app.post(
  5855. "/foo/test/delete/report", data=data, follow_redirects=True
  5856. )
  5857. self.assertEqual(output.status_code, 200)
  5858. output_text = output.get_data(as_text=True)
  5859. self.assertIn("Unknown report: None", output_text)
  5860. # Report specified not in the project's reports
  5861. data = {"csrf_token": csrf_token, "report": "foo"}
  5862. output = self.app.post(
  5863. "/foo/test/delete/report", data=data, follow_redirects=True
  5864. )
  5865. self.assertEqual(output.status_code, 200)
  5866. output_text = output.get_data(as_text=True)
  5867. self.assertIn("Unknown report: foo", output_text)
  5868. # Create a report
  5869. self.session.commit()
  5870. project = pagure.lib.query.get_authorized_project(
  5871. self.session, project_name="test", namespace="foo"
  5872. )
  5873. self.assertEqual(project.reports, {})
  5874. name = "test report"
  5875. url = "?foo=bar&baz=biz"
  5876. pagure.lib.query.save_report(
  5877. self.session, repo=project, name=name, url=url, username=None
  5878. )
  5879. self.session.commit()
  5880. project = pagure.lib.query.get_authorized_project(
  5881. self.session, project_name="test", namespace="foo"
  5882. )
  5883. self.assertEqual(
  5884. project.reports, {"test report": {"baz": "biz", "foo": "bar"}}
  5885. )
  5886. # Missing CSRF
  5887. data = {"report": "test report"}
  5888. output = self.app.post(
  5889. "/foo/test/delete/report", data=data, follow_redirects=True
  5890. )
  5891. self.assertEqual(output.status_code, 200)
  5892. output_text = output.get_data(as_text=True)
  5893. self.assertIn(
  5894. "<title>Settings - foo/test - Pagure</title>", output_text
  5895. )
  5896. project = pagure.lib.query.get_authorized_project(
  5897. self.session, project_name="test", namespace="foo"
  5898. )
  5899. self.assertEqual(
  5900. project.reports, {"test report": {"baz": "biz", "foo": "bar"}}
  5901. )
  5902. # Delete the report
  5903. data = {"csrf_token": csrf_token, "report": "test report"}
  5904. output = self.app.post(
  5905. "/foo/test/delete/report", data=data, follow_redirects=True
  5906. )
  5907. self.assertEqual(output.status_code, 200)
  5908. output_text = output.get_data(as_text=True)
  5909. self.assertIn("List of reports updated", output_text)
  5910. self.session.commit()
  5911. project = pagure.lib.query.get_authorized_project(
  5912. self.session, project_name="test", namespace="foo"
  5913. )
  5914. self.assertEqual(project.reports, {})
  5915. def test_open_pr_button_empty_repo(self):
  5916. """Test "Open Pull-Request" button on empty project."""
  5917. tests.create_projects(self.session)
  5918. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5919. output = self.app.get("/test")
  5920. self.assertEqual(output.status_code, 200)
  5921. output_text = output.get_data(as_text=True)
  5922. self.assertIn("<p>This repo is brand new!</p>", output_text)
  5923. self.assertNotIn(
  5924. 'href="/test/diff/master..master">Open Pull-Request', output_text
  5925. )
  5926. @patch.dict(
  5927. "pagure.config.config",
  5928. {"UPLOAD_FOLDER_PATH": None, "UPLOAD_FOLDER_URL": None},
  5929. )
  5930. def test_releases_upload_folder_vars_None(self):
  5931. """Test that /releases/ page of a repo displays correctly with
  5932. UPLOAD_FOLDER_PATH and UPLOAD_FOLDER_URL set to None
  5933. """
  5934. tests.create_projects(self.session)
  5935. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  5936. output = self.app.get("/test/releases")
  5937. self.assertEqual(output.status_code, 200)
  5938. self.assertIn(
  5939. "This project has not been tagged.", output.get_data(as_text=True)
  5940. )
  5941. class PagureFlaskRepoTestHooktests(tests.Modeltests):
  5942. """Tests for the web hook test function"""
  5943. def setUp(self):
  5944. """Set up the environnment, ran before every tests."""
  5945. super(PagureFlaskRepoTestHooktests, self).setUp()
  5946. tests.create_projects(self.session)
  5947. tests.create_projects_git(os.path.join(self.path, "repos"))
  5948. @patch(
  5949. "pagure.decorators.admin_session_timedout",
  5950. MagicMock(return_value=False),
  5951. )
  5952. def test_test_hook_no_project(self):
  5953. """Test the test_hook endpoint when the project doesn't exist."""
  5954. # No project
  5955. output = self.app.post("/foo/settings/test_hook")
  5956. self.assertEqual(output.status_code, 404)
  5957. @patch(
  5958. "pagure.decorators.admin_session_timedout",
  5959. MagicMock(return_value=False),
  5960. )
  5961. def test_test_hook_existing_project(self):
  5962. """Test the test_hook endpoint when the project doesn't exist."""
  5963. user = tests.FakeUser()
  5964. with tests.user_set(self.app.application, user):
  5965. output = self.app.post("/test/settings/test_hook")
  5966. self.assertEqual(output.status_code, 403)
  5967. @patch(
  5968. "pagure.decorators.admin_session_timedout",
  5969. MagicMock(return_value=False),
  5970. )
  5971. def test_test_hook_logged_out(self):
  5972. """Test the test_hook endpoint when the project isn't logged in."""
  5973. # User not logged in
  5974. output = self.app.post("/test/settings/test_hook")
  5975. self.assertEqual(output.status_code, 302)
  5976. @patch(
  5977. "pagure.decorators.admin_session_timedout",
  5978. MagicMock(return_value=False),
  5979. )
  5980. def test_test_hook_logged_in_no_csrf(self):
  5981. """Test the test_hook endpoint when the user is logged in."""
  5982. user = tests.FakeUser(username="pingou")
  5983. with tests.user_set(self.app.application, user):
  5984. output = self.app.post("/test/settings/test_hook")
  5985. self.assertEqual(output.status_code, 302)
  5986. self.assertEqual(output.status_code, 302)
  5987. @patch(
  5988. "pagure.decorators.admin_session_timedout",
  5989. MagicMock(return_value=False),
  5990. )
  5991. def test_test_hook_logged_in_csrf(self):
  5992. """Test the test_hook endpoint when the user is logged in."""
  5993. user = tests.FakeUser(username="pingou")
  5994. with tests.user_set(self.app.application, user):
  5995. data = {"csrf_token": self.get_csrf()}
  5996. output = self.app.post("/test/settings/test_hook", data=data)
  5997. self.assertEqual(output.status_code, 302)
  5998. class PagureFlaskRepoTestRegenerateGittests(tests.Modeltests):
  5999. """Tests for the regenerate git repo function"""
  6000. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  6001. @patch(
  6002. "pagure.decorators.admin_session_timedout",
  6003. MagicMock(return_value=False),
  6004. )
  6005. def setUp(self):
  6006. """Set up the environnment, ran before every tests."""
  6007. super(PagureFlaskRepoTestRegenerateGittests, self).setUp()
  6008. tests.create_projects(self.session)
  6009. tests.create_projects_git(os.path.join(self.path, "repos"))
  6010. user = tests.FakeUser()
  6011. with tests.user_set(self.app.application, user):
  6012. self.csrf_token = self.get_csrf()
  6013. def test_regenerate_git_invalid_project(self):
  6014. """Test the regenerate_git endpoint."""
  6015. user = tests.FakeUser()
  6016. with tests.user_set(self.app.application, user):
  6017. output = self.app.post("/foo/regenerate")
  6018. self.assertEqual(output.status_code, 404)
  6019. def test_regenerate_git_invalid_user(self):
  6020. """Test the regenerate_git endpoint."""
  6021. user = tests.FakeUser()
  6022. with tests.user_set(self.app.application, user):
  6023. output = self.app.post("/test/regenerate")
  6024. self.assertEqual(output.status_code, 403)
  6025. @patch(
  6026. "pagure.decorators.admin_session_timedout",
  6027. MagicMock(return_value=True),
  6028. )
  6029. def test_regenerate_git_user_session_timeout(self):
  6030. """Test the regenerate_git endpoint."""
  6031. user = tests.FakeUser()
  6032. with tests.user_set(self.app.application, user):
  6033. output = self.app.post("/test/regenerate")
  6034. self.assertEqual(output.status_code, 302)
  6035. def test_regenerate_git_no_csrf(self):
  6036. """Test the regenerate_git endpoint."""
  6037. user = tests.FakeUser(username="pingou")
  6038. with tests.user_set(self.app.application, user):
  6039. output = self.app.post("/test/regenerate")
  6040. self.assertEqual(output.status_code, 400)
  6041. def test_regenerate_git_missing_repo_type(self):
  6042. """Test the regenerate_git endpoint."""
  6043. user = tests.FakeUser(username="pingou")
  6044. with tests.user_set(self.app.application, user):
  6045. data = {"csrf_token": self.csrf_token}
  6046. output = self.app.post("/test/regenerate", data=data)
  6047. self.assertEqual(output.status_code, 400)
  6048. def test_regenerate_git_missing_invalid_regenerate(self):
  6049. """Test the regenerate_git endpoint."""
  6050. user = tests.FakeUser(username="pingou")
  6051. with tests.user_set(self.app.application, user):
  6052. data = {"csrf_token": self.csrf_token, "regenerate": "ticket"}
  6053. output = self.app.post("/test/regenerate", data=data)
  6054. self.assertEqual(output.status_code, 400)
  6055. @patch("pagure.lib.git._update_git")
  6056. def test_regenerate_git_tickets(self, upgit):
  6057. """Test the regenerate_git endpoint."""
  6058. upgit.return_value = True
  6059. user = tests.FakeUser(username="pingou")
  6060. with tests.user_set(self.app.application, user):
  6061. # Create an issue to play with
  6062. repo = pagure.lib.query.get_authorized_project(
  6063. self.session, "test"
  6064. )
  6065. msg = pagure.lib.query.new_issue(
  6066. session=self.session,
  6067. repo=repo,
  6068. title="Test issue",
  6069. content="We should work on this",
  6070. user="pingou",
  6071. )
  6072. self.session.commit()
  6073. self.assertEqual(msg.title, "Test issue")
  6074. data = {"csrf_token": self.csrf_token, "regenerate": "tickets"}
  6075. output = self.app.post(
  6076. "/test/regenerate", data=data, follow_redirects=True
  6077. )
  6078. self.assertEqual(output.status_code, 200)
  6079. output_text = output.get_data(as_text=True)
  6080. self.assertIn("Tickets git repo updating", output_text)
  6081. self.assertEqual(upgit.call_count, 2)
  6082. @patch("pagure.lib.git._update_git")
  6083. def test_regenerate_git_requests(self, upgit):
  6084. """Test the regenerate_git endpoint."""
  6085. # upgit.return_value = True
  6086. user = tests.FakeUser(username="pingou")
  6087. with tests.user_set(self.app.application, user):
  6088. # Create a request to play with
  6089. repo = pagure.lib.query.get_authorized_project(
  6090. self.session, "test"
  6091. )
  6092. msg = pagure.lib.query.new_pull_request(
  6093. session=self.session,
  6094. repo_from=repo,
  6095. branch_from="branch",
  6096. repo_to=repo,
  6097. branch_to="master",
  6098. title="Test pull-request",
  6099. user="pingou",
  6100. )
  6101. self.session.commit()
  6102. self.assertEqual(msg.title, "Test pull-request")
  6103. data = {"csrf_token": self.csrf_token, "regenerate": "requests"}
  6104. output = self.app.post(
  6105. "/test/regenerate", data=data, follow_redirects=True
  6106. )
  6107. self.assertEqual(output.status_code, 200)
  6108. output_text = output.get_data(as_text=True)
  6109. self.assertIn("Requests git repo updating", output_text)
  6110. self.assertEqual(upgit.call_count, 1)
  6111. class PagureFlaskRepoTestGitSSHURL(tests.Modeltests):
  6112. """Tests the display of the SSH url in the UI"""
  6113. def setUp(self):
  6114. """Set up the environnment, ran before every tests."""
  6115. super(PagureFlaskRepoTestGitSSHURL, self).setUp()
  6116. tests.create_projects(self.session)
  6117. tests.create_projects_git(os.path.join(self.path, "repos"))
  6118. pingou = pagure.lib.query.get_user(self.session, "pingou")
  6119. # Add a group and make pingou a member of it
  6120. item = pagure.lib.model.PagureGroup(
  6121. group_name="packager",
  6122. group_type="user",
  6123. display_name="User group",
  6124. user_id=1, # pingou
  6125. )
  6126. self.session.add(item)
  6127. self.session.commit()
  6128. pagure.lib.query.add_user_to_group(
  6129. self.session, pingou.username, item, pingou.username, True
  6130. )
  6131. # Add a SSH key for pingou so that he is allowed to push via ssh
  6132. msg = pagure.lib.query.add_sshkey_to_project_or_user(
  6133. session=self.session,
  6134. user=pingou,
  6135. ssh_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==",
  6136. pushaccess=True,
  6137. creator=pingou,
  6138. )
  6139. self.session.commit()
  6140. self.assertEqual(msg, "SSH key added")
  6141. def test_logged_out(self):
  6142. """Test the default behavior with the user logged out."""
  6143. output = self.app.get("/test")
  6144. self.assertEqual(output.status_code, 200)
  6145. output_text = output.get_data(as_text=True)
  6146. self.assertIn("<strong>Source Code</strong>", output_text)
  6147. self.assertIn(
  6148. '<div class="input-group-prepend"><span class="input-group-text">'
  6149. "GIT</span></div>",
  6150. output_text,
  6151. )
  6152. self.assertNotIn(
  6153. '<div class="input-group-prepend"><span class="input-group-text">'
  6154. "SSH</span></div>",
  6155. output_text,
  6156. )
  6157. def test_logged_in(self):
  6158. """Test the default behavior with the user logged in."""
  6159. user = tests.FakeUser(username="pingou")
  6160. with tests.user_set(self.app.application, user):
  6161. output = self.app.get("/test")
  6162. self.assertEqual(output.status_code, 200)
  6163. output_text = output.get_data(as_text=True)
  6164. self.assertIn("<strong>Source Code</strong>", output_text)
  6165. self.assertIn(
  6166. '<div class="input-group-prepend"><span class="input-group-text">'
  6167. "GIT</span></div>",
  6168. output_text,
  6169. )
  6170. self.assertIn(
  6171. '<div class="input-group-prepend"><span class="input-group-text">'
  6172. "SSH</span></div>",
  6173. output_text,
  6174. )
  6175. @patch.dict("pagure.config.config", {"SSH_ACCESS_GROUPS": ["packager"]})
  6176. def test_ssh_restricted_user_member(self):
  6177. """Test when ssh is restricted and the user has access."""
  6178. user = tests.FakeUser(username="pingou")
  6179. with tests.user_set(self.app.application, user):
  6180. output = self.app.get("/test")
  6181. self.assertEqual(output.status_code, 200)
  6182. output_text = output.get_data(as_text=True)
  6183. self.assertIn("<strong>Source Code</strong>", output_text)
  6184. self.assertIn(
  6185. '<div class="input-group-prepend"><span class="input-group-text">'
  6186. "GIT</span></div>",
  6187. output_text,
  6188. )
  6189. self.assertIn(
  6190. '<div class="input-group-prepend"><span class="input-group-text">'
  6191. "SSH</span></div>",
  6192. output_text,
  6193. )
  6194. @patch.dict("pagure.config.config", {"SSH_ACCESS_GROUPS": ["invalid"]})
  6195. def test_ssh_restricted_user_non_member(self):
  6196. """Test when ssh is restricted and the user does not have access."""
  6197. user = tests.FakeUser(username="pingou")
  6198. with tests.user_set(self.app.application, user):
  6199. output = self.app.get("/test")
  6200. self.assertEqual(output.status_code, 200)
  6201. output_text = output.get_data(as_text=True)
  6202. self.assertIn("<strong>Source Code</strong>", output_text)
  6203. self.assertIn(
  6204. '<div class="input-group-prepend"><span class="input-group-text">'
  6205. "GIT</span></div>",
  6206. output_text,
  6207. )
  6208. self.assertIn(
  6209. "Only members of the invalid group(s) can clone via ssh",
  6210. output_text,
  6211. )
  6212. self.assertNotIn(
  6213. '<div class="input-group-prepend"><span class="input-group-text">'
  6214. "SSH</span></div>",
  6215. output_text,
  6216. )
  6217. if __name__ == "__main__":
  6218. unittest.main(verbosity=2)