test_pagure_flask_ui_repo.py 257 KB

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