test_pagure_flask_ui_repo.py 207 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import datetime
  10. import json
  11. import unittest
  12. import re
  13. import shutil
  14. import sys
  15. import tempfile
  16. import time
  17. import os
  18. import pygit2
  19. from mock import ANY, patch, MagicMock
  20. sys.path.insert(0, os.path.join(os.path.dirname(
  21. os.path.abspath(__file__)), '..'))
  22. import pagure
  23. import pagure.lib
  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. @patch('pagure.decorators.admin_session_timedout')
  36. def test_add_user_when_user_mngt_off(self, ast):
  37. """ Test the add_user endpoint when user management is turned off
  38. in the pagure instance """
  39. pagure.config.config['ENABLE_USER_MNGT'] = False
  40. ast.return_value = False
  41. # No Git repo
  42. output = self.app.get('/foo/adduser')
  43. self.assertEqual(output.status_code, 404)
  44. tests.create_projects(self.session)
  45. tests.create_projects_git(os.path.join(self.path, 'repos'))
  46. # User not logged in
  47. output = self.app.get('/test/adduser')
  48. self.assertEqual(output.status_code, 302)
  49. user = tests.FakeUser(username='pingou')
  50. with tests.user_set(self.app.application, user):
  51. output = self.app.get('/test/adduser')
  52. self.assertEqual(output.status_code, 404)
  53. #just get the csrf token
  54. pagure.config.config['ENABLE_USER_MNGT'] = True
  55. output = self.app.get('/test/adduser')
  56. csrf_token = output.data.split(
  57. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  58. pagure.config.config['ENABLE_USER_MNGT'] = False
  59. data = {
  60. 'user': 'ralph',
  61. }
  62. output = self.app.post('/test/adduser', data=data)
  63. self.assertEqual(output.status_code, 404)
  64. data['csrf_token'] = csrf_token
  65. output = self.app.post('/test/adduser', data=data)
  66. self.assertEqual(output.status_code, 404)
  67. data['user'] = 'foo'
  68. tests.create_projects_git(os.path.join(self.path, 'repos'))
  69. output = self.app.post(
  70. '/test/adduser', data=data, follow_redirects=True)
  71. self.assertEqual(output.status_code, 404)
  72. pagure.config.config['ENABLE_USER_MNGT'] = True
  73. @patch('pagure.decorators.admin_session_timedout')
  74. def test_add_deploykey(self, ast):
  75. """ Test the add_deploykey endpoint. """
  76. ast.return_value = False
  77. # No git repo
  78. output = self.app.get('/foo/adddeploykey')
  79. self.assertEqual(output.status_code, 404)
  80. tests.create_projects(self.session)
  81. tests.create_projects_git(os.path.join(self.path, 'repos'))
  82. # User not logged in
  83. output = self.app.get('/test/adddeploykey')
  84. self.assertEqual(output.status_code, 302)
  85. user = tests.FakeUser()
  86. with tests.user_set(self.app.application, user):
  87. output = self.app.get('/test/adddeploykey')
  88. self.assertEqual(output.status_code, 403)
  89. ast.return_value = True
  90. output = self.app.get('/test/adddeploykey')
  91. self.assertEqual(output.status_code, 302)
  92. # Redirect also happens for POST request
  93. output = self.app.post('/test/adddeploykey')
  94. self.assertEqual(output.status_code, 302)
  95. # Need to do this un-authentified since our fake user isn't in the DB
  96. # Check the message flashed during the redirect
  97. output = self.app.get('/')
  98. self.assertEqual(output.status_code, 200)
  99. self.assertIn(
  100. '</button>\n Action canceled, try it '
  101. 'again',output.data)
  102. ast.return_value = False
  103. user.username = 'pingou'
  104. with tests.user_set(self.app.application, user):
  105. output = self.app.get('/test/adddeploykey')
  106. self.assertEqual(output.status_code, 200)
  107. self.assertIn('<strong>Add deploy key to the', output.data)
  108. csrf_token = output.data.split(
  109. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  110. data = {
  111. 'ssh_key': 'asdf',
  112. 'pushaccess': 'false'
  113. }
  114. # No CSRF token
  115. output = self.app.post('/test/adddeploykey', data=data)
  116. self.assertEqual(output.status_code, 200)
  117. self.assertTrue('<strong>Add deploy key to the' in output.data)
  118. data['csrf_token'] = csrf_token
  119. # First, invalid SSH key
  120. output = self.app.post('/test/adddeploykey', data=data)
  121. self.assertEqual(output.status_code, 200)
  122. self.assertIn('<strong>Add deploy key to the', output.data)
  123. self.assertIn('Deploy key invalid', output.data)
  124. # Next up, multiple SSH keys
  125. data['ssh_key'] = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q=='
  126. output = self.app.post(
  127. '/test/adddeploykey', data=data, follow_redirects=True)
  128. self.assertEqual(output.status_code, 200)
  129. self.assertIn('Deploy key can only be single keys.', output.data)
  130. # Now, a valid SSH key
  131. data['ssh_key'] = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q=='
  132. output = self.app.post(
  133. '/test/adddeploykey', data=data, follow_redirects=True)
  134. self.assertEqual(output.status_code, 200)
  135. self.assertIn(
  136. '<title>Settings - test - Pagure</title>', output.data)
  137. self.assertIn('<h3>Settings for test</h3>', output.data)
  138. self.assertIn('Deploy key added', output.data)
  139. self.assertNotIn('PUSH ACCESS', output.data)
  140. # And now, adding the same key
  141. output = self.app.post(
  142. '/test/adddeploykey', data=data, follow_redirects=True)
  143. self.assertEqual(output.status_code, 200)
  144. self.assertIn('Deploy key already exists', output.data)
  145. # And next, a key with push access
  146. data['ssh_key'] = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC9Xwc2RDzPBhlEDARfHldGjudIVoa04tqT1JVKGQmyllTFz7Rb8CngQL3e7zyNzotnhwYKHdoiLlPkVEiDee4dWMUe48ilqId+FJZQGhyv8fu4BoFdE1AJUVylzmltbLg14VqG5gjTpXgtlrEva9arKwBMHJjRYc8ScaSn3OgyQw=='
  147. data['pushaccess'] = 'true'
  148. output = self.app.post(
  149. '/test/adddeploykey', data=data, follow_redirects=True)
  150. self.assertEqual(output.status_code, 200)
  151. self.assertIn(
  152. '<title>Settings - test - Pagure</title>', output.data)
  153. self.assertIn('<h3>Settings for test</h3>', output.data)
  154. self.assertIn('Deploy key added', output.data)
  155. self.assertIn('PUSH ACCESS', output.data)
  156. @patch('pagure.decorators.admin_session_timedout')
  157. @patch.dict('pagure.config.config', {'DEPLOY_KEY': False})
  158. def test_add_deploykey_disabled(self, ast):
  159. """ Test the add_deploykey endpoint when it's disabled in the config.
  160. """
  161. ast.return_value = False
  162. tests.create_projects(self.session)
  163. tests.create_projects_git(os.path.join(self.path, 'repos'))
  164. user = tests.FakeUser(username='pingou')
  165. with tests.user_set(self.app.application, user):
  166. output = self.app.get('/test/adddeploykey')
  167. self.assertEqual(output.status_code, 404)
  168. output = self.app.post('/test/adddeploykey')
  169. self.assertEqual(output.status_code, 404)
  170. @patch('pagure.decorators.admin_session_timedout')
  171. @patch('pagure.lib.notify.log')
  172. def test_add_user(self, mock_log, ast):
  173. """ Test the add_user endpoint. """
  174. ast.return_value = False
  175. # No git repo
  176. output = self.app.get('/foo/adduser')
  177. self.assertEqual(output.status_code, 404)
  178. tests.create_projects(self.session)
  179. tests.create_projects_git(os.path.join(self.path, 'repos'))
  180. # User not logged in
  181. output = self.app.get('/test/adduser')
  182. self.assertEqual(output.status_code, 302)
  183. user = tests.FakeUser()
  184. with tests.user_set(self.app.application, user):
  185. output = self.app.get('/test/adduser')
  186. self.assertEqual(output.status_code, 403)
  187. ast.return_value = True
  188. output = self.app.get('/test/adduser')
  189. self.assertEqual(output.status_code, 302)
  190. # Redirect also happens for POST request
  191. output = self.app.post('/test/adduser')
  192. self.assertEqual(output.status_code, 302)
  193. # Need to do this un-authentified since our fake user isn't in the DB
  194. # Check the message flashed during the redirect
  195. output = self.app.get('/')
  196. self.assertEqual(output.status_code, 200)
  197. self.assertIn(
  198. '</button>\n Action canceled, try it '
  199. 'again',output.data)
  200. ast.return_value = False
  201. user.username = 'pingou'
  202. with tests.user_set(self.app.application, user):
  203. output = self.app.get('/test/adduser')
  204. self.assertEqual(output.status_code, 200)
  205. self.assertIn('<strong>Add user to the', output.data)
  206. csrf_token = output.data.split(
  207. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  208. data = {
  209. 'user': 'ralph',
  210. }
  211. # Missing access and no CSRF
  212. output = self.app.post('/test/adduser', data=data)
  213. self.assertEqual(output.status_code, 200)
  214. self.assertIn(
  215. '<title>Add user - test - Pagure</title>', output.data)
  216. self.assertTrue('<strong>Add user to the' in output.data)
  217. # No CSRF
  218. output = self.app.post('/test/adduser', data=data)
  219. self.assertEqual(output.status_code, 200)
  220. self.assertIn(
  221. '<title>Add user - test - Pagure</title>', output.data)
  222. # Missing access
  223. data['csrf_token'] = csrf_token
  224. output = self.app.post('/test/adduser', data=data)
  225. self.assertEqual(output.status_code, 200)
  226. self.assertIn(
  227. '<title>Add user - test - Pagure</title>', output.data)
  228. self.assertIn('<strong>Add user to the', output.data)
  229. # Unknown user
  230. data['access'] = 'commit'
  231. output = self.app.post('/test/adduser', data=data)
  232. self.assertEqual(output.status_code, 200)
  233. self.assertIn(
  234. '<title>Add user - test - Pagure</title>', output.data)
  235. self.assertIn('<strong>Add user to the', output.data)
  236. self.assertIn(
  237. '</button>\n No user &#34;ralph&#34; found\n',
  238. output.data)
  239. # All correct
  240. data['user'] = 'foo'
  241. output = self.app.post(
  242. '/test/adduser', data=data, follow_redirects=True)
  243. self.assertEqual(output.status_code, 200)
  244. self.assertIn('<h3>Settings for test</h3>', output.data)
  245. self.assertIn(
  246. '</button>\n User added', output.data)
  247. mock_log.assert_called_with(ANY, topic='project.user.added', msg=ANY, redis=ANY)
  248. @patch('pagure.decorators.admin_session_timedout')
  249. def test_add_group_project_when_user_mngt_off(self, ast):
  250. """ Test the add_group_project endpoint when user management is
  251. turned off in the pagure instance"""
  252. pagure.config.config['ENABLE_USER_MNGT'] = False
  253. ast.return_value = False
  254. # No Git repo
  255. output = self.app.get('/foo/addgroup')
  256. self.assertEqual(output.status_code, 404)
  257. tests.create_projects(self.session)
  258. tests.create_projects_git(os.path.join(self.path, 'repos'))
  259. # User not logged in
  260. output = self.app.get('/test/addgroup')
  261. self.assertEqual(output.status_code, 302)
  262. msg = pagure.lib.add_group(
  263. self.session,
  264. group_name='foo',
  265. group_type='bar',
  266. display_name='foo group',
  267. description=None,
  268. user='pingou',
  269. is_admin=False,
  270. blacklist=pagure.config.config['BLACKLISTED_GROUPS'],
  271. )
  272. self.session.commit()
  273. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  274. user = tests.FakeUser(username='pingou')
  275. with tests.user_set(self.app.application, user):
  276. #just get the csrf token
  277. pagure.config.config['ENABLE_USER_MNGT'] = True
  278. output = self.app.get('/test/addgroup')
  279. csrf_token = output.data.split(
  280. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  281. pagure.config.config['ENABLE_USER_MNGT'] = False
  282. data = {
  283. 'group': 'ralph',
  284. }
  285. output = self.app.post('/test/addgroup', data=data)
  286. self.assertEqual(output.status_code, 404)
  287. data['csrf_token'] = csrf_token
  288. output = self.app.post('/test/addgroup', data=data)
  289. self.assertEqual(output.status_code, 404)
  290. data['group'] = 'foo'
  291. output = self.app.post(
  292. '/test/addgroup', data=data, follow_redirects=True)
  293. self.assertEqual(output.status_code, 404)
  294. pagure.config.config['ENABLE_USER_MNGT'] = True
  295. @patch.dict('pagure.config.config', {'ENABLE_GROUP_MNGT': False})
  296. @patch('pagure.decorators.admin_session_timedout')
  297. def test_add_group_project_grp_mngt_off(self, ast):
  298. """ Test the add_group_project endpoint when group management is
  299. turned off in the pagure instance"""
  300. ast.return_value = False
  301. tests.create_projects(self.session)
  302. tests.create_projects_git(os.path.join(self.path, 'repos'))
  303. user = tests.FakeUser(username='pingou')
  304. with tests.user_set(self.app.application, user):
  305. data = {
  306. 'group': 'ralph',
  307. 'access': 'ticket',
  308. 'csrf_token': self.get_csrf(),
  309. }
  310. output = self.app.post(
  311. '/test/addgroup', data=data, follow_redirects=True)
  312. self.assertEqual(output.status_code, 200)
  313. self.assertIn(
  314. u'<title>Add group - test - Pagure</title>',
  315. output.data)
  316. self.assertIn(u'No group ralph found.', output.data)
  317. @patch('pagure.decorators.admin_session_timedout')
  318. def test_add_group_project(self, ast):
  319. """ Test the add_group_project endpoint. """
  320. ast.return_value = False
  321. # No Git repo
  322. output = self.app.get('/foo/addgroup')
  323. self.assertEqual(output.status_code, 404)
  324. tests.create_projects(self.session)
  325. tests.create_projects_git(os.path.join(self.path, 'repos'))
  326. # User not logged in
  327. output = self.app.get('/test/addgroup')
  328. self.assertEqual(output.status_code, 302)
  329. user = tests.FakeUser()
  330. with tests.user_set(self.app.application, user):
  331. output = self.app.get('/test/addgroup')
  332. self.assertEqual(output.status_code, 403)
  333. ast.return_value = True
  334. output = self.app.get('/test/addgroup')
  335. self.assertEqual(output.status_code, 302)
  336. # Redirect also happens for POST request
  337. output = self.app.post('/test/addgroup')
  338. self.assertEqual(output.status_code, 302)
  339. # Need to do this un-authentified since our fake user isn't in the DB
  340. # Check the message flashed during the redirect
  341. output = self.app.get('/')
  342. self.assertEqual(output.status_code, 200)
  343. self.assertIn(
  344. '</button>\n Action canceled, try it '
  345. 'again', output.data)
  346. ast.return_value = False
  347. msg = pagure.lib.add_group(
  348. self.session,
  349. group_name='foo',
  350. display_name='foo group',
  351. description=None,
  352. group_type='bar',
  353. user='pingou',
  354. is_admin=False,
  355. blacklist=pagure.config.config['BLACKLISTED_GROUPS'],
  356. )
  357. self.session.commit()
  358. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  359. user.username = 'pingou'
  360. with tests.user_set(self.app.application, user):
  361. output = self.app.get('/test/addgroup')
  362. self.assertEqual(output.status_code, 200)
  363. self.assertTrue('<strong>Add group to the' in output.data)
  364. csrf_token = output.data.split(
  365. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  366. data = {
  367. 'group': 'ralph',
  368. }
  369. # Missing CSRF
  370. output = self.app.post('/test/addgroup', data=data)
  371. self.assertEqual(output.status_code, 200)
  372. self.assertIn(
  373. '<title>Add group - test - Pagure</title>', output.data)
  374. self.assertIn('<strong>Add group to the', output.data)
  375. # Missing access
  376. data['csrf_token'] = csrf_token
  377. output = self.app.post('/test/addgroup', data=data)
  378. self.assertEqual(output.status_code, 200)
  379. self.assertIn(
  380. '<title>Add group - test - Pagure</title>', output.data)
  381. self.assertIn('<strong>Add group to the', output.data)
  382. # All good
  383. data['access'] = 'ticket'
  384. output = self.app.post(
  385. '/test/addgroup', data=data, follow_redirects=True)
  386. self.assertEqual(output.status_code, 200)
  387. self.assertIn(
  388. '<title>Settings - test - Pagure</title>', output.data)
  389. self.assertIn('<h3>Settings for test</h3>', output.data)
  390. self.assertIn(
  391. '</button>\n Group added', output.data)
  392. @patch('pagure.decorators.admin_session_timedout')
  393. def test_remove_user_when_user_mngt_off(self, ast):
  394. """ Test the remove_user endpoint when user management is turned
  395. off in the pagure instance"""
  396. pagure.config.config['ENABLE_USER_MNGT'] = False
  397. ast.return_value = False
  398. # Git repo not found
  399. output = self.app.post('/foo/dropuser/1')
  400. self.assertEqual(output.status_code, 404)
  401. user = tests.FakeUser(username='pingou')
  402. with tests.user_set(self.app.application, user):
  403. tests.create_projects(self.session)
  404. tests.create_projects_git(os.path.join(self.path, 'repos'))
  405. output = self.app.post('/test/settings')
  406. csrf_token = output.data.split(
  407. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  408. data = {'csrf_token': csrf_token}
  409. output = self.app.post(
  410. '/test/dropuser/2', data=data, follow_redirects=True)
  411. self.assertEqual(output.status_code, 404)
  412. # User not logged in
  413. output = self.app.post('/test/dropuser/1')
  414. self.assertEqual(output.status_code, 302)
  415. # Add an user to a project
  416. repo = pagure.lib.get_authorized_project(self.session, 'test')
  417. msg = pagure.lib.add_user_to_project(
  418. session=self.session,
  419. project=repo,
  420. new_user='foo',
  421. user='pingou',
  422. )
  423. self.session.commit()
  424. self.assertEqual(msg, 'User added')
  425. with tests.user_set(self.app.application, user):
  426. output = self.app.post('/test/dropuser/2', follow_redirects=True)
  427. self.assertEqual(output.status_code, 404)
  428. data = {'csrf_token': csrf_token}
  429. output = self.app.post(
  430. '/test/dropuser/2', data=data, follow_redirects=True)
  431. self.assertEqual(output.status_code, 404)
  432. pagure.config.config['ENABLE_USER_MNGT'] = True
  433. @patch('pagure.decorators.admin_session_timedout')
  434. def test_remove_deploykey(self, ast):
  435. """ Test the remove_deploykey endpoint. """
  436. ast.return_value = False
  437. # Git repo not found
  438. output = self.app.post('/foo/dropdeploykey/1')
  439. self.assertEqual(output.status_code, 404)
  440. user = tests.FakeUser()
  441. with tests.user_set(self.app.application, user):
  442. output = self.app.post('/foo/dropdeploykey/1')
  443. self.assertEqual(output.status_code, 404)
  444. tests.create_projects(self.session)
  445. tests.create_projects_git(os.path.join(self.path, 'repos'))
  446. output = self.app.post('/test/dropdeploykey/1')
  447. self.assertEqual(output.status_code, 403)
  448. ast.return_value = True
  449. output = self.app.post('/test/dropdeploykey/1')
  450. self.assertEqual(output.status_code, 302)
  451. ast.return_value = False
  452. # User not logged in
  453. output = self.app.post('/test/dropdeploykey/1')
  454. self.assertEqual(output.status_code, 302)
  455. user.username = 'pingou'
  456. with tests.user_set(self.app.application, user):
  457. output = self.app.post('/test/settings')
  458. csrf_token = output.data.split(
  459. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  460. data = {'csrf_token': csrf_token}
  461. output = self.app.post(
  462. '/test/dropdeploykey/1', data=data, follow_redirects=True)
  463. self.assertEqual(output.status_code, 200)
  464. self.assertIn(
  465. '<title>Settings - test - Pagure</title>', output.data)
  466. self.assertIn('<h3>Settings for test</h3>', output.data)
  467. self.assertIn('Deploy key does not exist in project', output.data)
  468. # Add a deploy key to a project
  469. repo = pagure.lib.get_authorized_project(self.session, 'test')
  470. msg = pagure.lib.add_deploykey_to_project(
  471. session=self.session,
  472. project=repo,
  473. ssh_key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==',
  474. pushaccess=True,
  475. user='pingou',
  476. )
  477. self.session.commit()
  478. self.assertEqual(msg, 'Deploy key added')
  479. with tests.user_set(self.app.application, user):
  480. output = self.app.post('/test/dropdeploykey/1', follow_redirects=True)
  481. self.assertEqual(output.status_code, 200)
  482. self.assertIn(
  483. '<title>Settings - test - Pagure</title>', output.data)
  484. self.assertIn('<h3>Settings for test</h3>', output.data)
  485. self.assertNotIn('Deploy key removed', output.data)
  486. data = {'csrf_token': csrf_token}
  487. output = self.app.post(
  488. '/test/dropdeploykey/1', data=data, follow_redirects=True)
  489. self.assertEqual(output.status_code, 200)
  490. self.assertIn(
  491. '<title>Settings - test - Pagure</title>', output.data)
  492. self.assertIn('<h3>Settings for test</h3>', output.data)
  493. self.assertIn('Deploy key removed', output.data)
  494. @patch('pagure.decorators.admin_session_timedout')
  495. @patch.dict('pagure.config.config', {'DEPLOY_KEY': False})
  496. def test_remove_deploykey_disabled(self, ast):
  497. """ Test the remove_deploykey endpoint when it's disabled in the
  498. config.
  499. """
  500. ast.return_value = False
  501. tests.create_projects(self.session)
  502. tests.create_projects_git(os.path.join(self.path, 'repos'))
  503. user = tests.FakeUser(username='pingou')
  504. with tests.user_set(self.app.application, user):
  505. output = self.app.post('/test/dropdeploykey/1')
  506. self.assertEqual(output.status_code, 404)
  507. @patch('pagure.decorators.admin_session_timedout')
  508. @patch('pagure.lib.notify.log')
  509. def test_remove_user(self, mock_log, ast):
  510. """ Test the remove_user endpoint. """
  511. ast.return_value = False
  512. # Git repo not found
  513. output = self.app.post('/foo/dropuser/1')
  514. self.assertEqual(output.status_code, 404)
  515. user = tests.FakeUser()
  516. with tests.user_set(self.app.application, user):
  517. output = self.app.post('/foo/dropuser/1')
  518. self.assertEqual(output.status_code, 404)
  519. tests.create_projects(self.session)
  520. tests.create_projects_git(os.path.join(self.path, 'repos'))
  521. output = self.app.post('/test/dropuser/1')
  522. self.assertEqual(output.status_code, 403)
  523. ast.return_value = True
  524. output = self.app.post('/test/dropuser/1')
  525. self.assertEqual(output.status_code, 302)
  526. ast.return_value = False
  527. # User not logged in
  528. output = self.app.post('/test/dropuser/1')
  529. self.assertEqual(output.status_code, 302)
  530. user.username = 'pingou'
  531. with tests.user_set(self.app.application, user):
  532. output = self.app.post('/test/settings')
  533. csrf_token = output.data.split(
  534. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  535. data = {'csrf_token': csrf_token}
  536. output = self.app.post(
  537. '/test/dropuser/2', data=data, follow_redirects=True)
  538. self.assertEqual(output.status_code, 200)
  539. self.assertIn(
  540. '<title>Settings - test - Pagure</title>', output.data)
  541. self.assertIn('<h3>Settings for test</h3>', output.data)
  542. self.assertIn(
  543. '</button>\n User does not have any '
  544. 'access on the repo', output.data)
  545. # Add an user to a project
  546. repo = pagure.lib.get_authorized_project(self.session, 'test')
  547. self.assertEqual(len(repo.users), 0)
  548. msg = pagure.lib.add_user_to_project(
  549. session=self.session,
  550. project=repo,
  551. new_user='foo',
  552. user='pingou',
  553. )
  554. self.session.commit()
  555. self.assertEqual(msg, 'User added')
  556. self.assertEqual(len(repo.users), 1)
  557. with tests.user_set(self.app.application, user):
  558. output = self.app.post('/test/dropuser/2', follow_redirects=True)
  559. self.assertEqual(output.status_code, 200)
  560. self.assertIn(
  561. '<title>Settings - test - Pagure</title>', output.data)
  562. self.assertIn('<h3>Settings for test</h3>', output.data)
  563. self.assertNotIn(
  564. '</button>\n User removed', output.data)
  565. self.assertIn('action="/test/dropuser/2">', output.data)
  566. repo = pagure.lib.get_authorized_project(self.session, 'test')
  567. self.assertEqual(len(repo.users), 1)
  568. data = {'csrf_token': csrf_token}
  569. output = self.app.post(
  570. '/test/dropuser/2', data=data, follow_redirects=True)
  571. self.assertEqual(output.status_code, 200)
  572. self.assertIn(
  573. '<title>Settings - test - Pagure</title>', output.data)
  574. self.assertIn('<h3>Settings for test</h3>', output.data)
  575. self.assertIn(
  576. '</button>\n User removed', output.data)
  577. self.assertNotIn('action="/test/dropuser/2">', output.data)
  578. self.session.commit()
  579. repo = pagure.lib.get_authorized_project(self.session, 'test')
  580. self.assertEqual(len(repo.users), 0)
  581. mock_log.assert_called_with(ANY, topic='project.user.removed', msg=ANY)
  582. @patch('pagure.decorators.admin_session_timedout')
  583. @patch('pagure.lib.notify.log')
  584. def test_remove_user_self(self, mock_log, ast):
  585. """ Test the remove_user endpoint when removing themselves. """
  586. ast.return_value = False
  587. tests.create_projects(self.session)
  588. tests.create_projects_git(os.path.join(self.path, 'repos'))
  589. # Add an user to a project
  590. repo = pagure.lib.get_authorized_project(self.session, 'test')
  591. self.assertEqual(len(repo.users), 0)
  592. msg = pagure.lib.add_user_to_project(
  593. session=self.session,
  594. project=repo,
  595. new_user='foo',
  596. user='pingou',
  597. )
  598. self.session.commit()
  599. self.assertEqual(msg, 'User added')
  600. self.assertEqual(len(repo.users), 1)
  601. # Let user foo remove themselves
  602. user = tests.FakeUser(username='foo')
  603. with tests.user_set(self.app.application, user):
  604. csrf_token = self.get_csrf()
  605. data = {'csrf_token': csrf_token}
  606. output = self.app.post(
  607. '/test/dropuser/2', data=data, follow_redirects=True)
  608. self.assertEqual(output.status_code, 200)
  609. self.assertIn(
  610. u'<title>Overview - test - Pagure</title>', output.data)
  611. self.assertIn(
  612. u'<h2 class="repo-name m-b-0">\n<a href="/test">test</a>',
  613. output.data)
  614. self.assertIn(
  615. u'</button>\n User removed', output.data)
  616. self.session.commit()
  617. repo = pagure.lib.get_authorized_project(self.session, 'test')
  618. self.assertEqual(len(repo.users), 0)
  619. mock_log.assert_called_with(ANY, topic='project.user.removed', msg=ANY)
  620. @patch('pagure.decorators.admin_session_timedout')
  621. def test_remove_group_project_when_user_mngt_off(self, ast):
  622. """ Test the remove_group_project endpoint when user management is
  623. turned off in the pagure instance"""
  624. pagure.config.config['ENABLE_USER_MNGT'] = False
  625. ast.return_value = False
  626. # No Git repo
  627. output = self.app.post('/foo/dropgroup/1')
  628. self.assertEqual(output.status_code, 404)
  629. tests.create_projects(self.session)
  630. tests.create_projects_git(os.path.join(self.path, 'repos'))
  631. # User not logged in
  632. output = self.app.post('/test/dropgroup/1')
  633. self.assertEqual(output.status_code, 302)
  634. user = tests.FakeUser()
  635. user.username = 'pingou'
  636. with tests.user_set(self.app.application, user):
  637. output = self.app.post('/test/settings')
  638. csrf_token = output.data.split(
  639. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  640. data = {'csrf_token': csrf_token}
  641. output = self.app.post(
  642. '/test/dropgroup/2', data=data, follow_redirects=True)
  643. self.assertEqual(output.status_code, 404)
  644. # Create the new group
  645. msg = pagure.lib.add_group(
  646. session=self.session,
  647. group_name='testgrp',
  648. group_type='user',
  649. display_name='testgrp group',
  650. description=None,
  651. user='pingou',
  652. is_admin=False,
  653. blacklist=[],
  654. )
  655. self.assertEqual(msg, 'User `pingou` added to the group `testgrp`.')
  656. self.session.commit()
  657. repo = pagure.lib.get_authorized_project(self.session, 'test')
  658. # Add the group to a project
  659. msg = pagure.lib.add_group_to_project(
  660. session=self.session,
  661. project=repo,
  662. new_group='testgrp',
  663. user='pingou',
  664. )
  665. self.session.commit()
  666. self.assertEqual(msg, 'Group added')
  667. with tests.user_set(self.app.application, user):
  668. output = self.app.post('/test/dropgroup/1', follow_redirects=True)
  669. self.assertEqual(output.status_code, 404)
  670. data = {'csrf_token': csrf_token}
  671. output = self.app.post(
  672. '/test/dropgroup/1', data=data, follow_redirects=True)
  673. self.assertEqual(output.status_code, 404)
  674. pagure.config.config['ENABLE_USER_MNGT'] = True
  675. @patch('pagure.decorators.admin_session_timedout')
  676. def test_remove_group_project(self, ast):
  677. """ Test the remove_group_project endpoint. """
  678. ast.return_value = False
  679. # No Git repo
  680. output = self.app.post('/foo/dropgroup/1')
  681. self.assertEqual(output.status_code, 404)
  682. user = tests.FakeUser()
  683. with tests.user_set(self.app.application, user):
  684. output = self.app.post('/foo/dropgroup/1')
  685. self.assertEqual(output.status_code, 404)
  686. tests.create_projects(self.session)
  687. tests.create_projects_git(os.path.join(self.path, 'repos'))
  688. output = self.app.post('/test/dropgroup/1')
  689. self.assertEqual(output.status_code, 403)
  690. ast.return_value = True
  691. output = self.app.post('/test/dropgroup/1')
  692. self.assertEqual(output.status_code, 302)
  693. ast.return_value = False
  694. # User not logged in
  695. output = self.app.post('/test/dropgroup/1')
  696. self.assertEqual(output.status_code, 302)
  697. user.username = 'pingou'
  698. with tests.user_set(self.app.application, user):
  699. output = self.app.post('/test/settings')
  700. csrf_token = output.data.split(
  701. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  702. data = {'csrf_token': csrf_token}
  703. output = self.app.post(
  704. '/test/dropgroup/2', data=data, follow_redirects=True)
  705. self.assertEqual(output.status_code, 200)
  706. self.assertIn(
  707. '<title>Settings - test - Pagure</title>', output.data)
  708. self.assertIn('<h3>Settings for test</h3>', output.data)
  709. self.assertIn(
  710. '</button>\n '
  711. 'Group does not seem to be part of this project',
  712. output.data)
  713. # Create the new group
  714. msg = pagure.lib.add_group(
  715. session=self.session,
  716. group_name='testgrp',
  717. group_type='user',
  718. display_name='testgrp group',
  719. description=None,
  720. user='pingou',
  721. is_admin=False,
  722. blacklist=[],
  723. )
  724. self.assertEqual(msg, 'User `pingou` added to the group `testgrp`.')
  725. self.session.commit()
  726. repo = pagure.lib.get_authorized_project(self.session, 'test')
  727. # Add the group to a project
  728. msg = pagure.lib.add_group_to_project(
  729. session=self.session,
  730. project=repo,
  731. new_group='testgrp',
  732. user='pingou',
  733. )
  734. self.session.commit()
  735. self.assertEqual(msg, 'Group added')
  736. repo = pagure.lib.get_authorized_project(self.session, 'test')
  737. self.assertEqual(len(repo.groups), 1)
  738. with tests.user_set(self.app.application, user):
  739. output = self.app.post('/test/dropgroup/1', follow_redirects=True)
  740. self.assertEqual(output.status_code, 200)
  741. self.assertIn(
  742. '<title>Settings - test - Pagure</title>', output.data)
  743. self.assertIn('<h3>Settings for test</h3>', output.data)
  744. self.assertIn('action="/test/dropgroup/1">', output.data)
  745. self.assertNotIn(
  746. '</button>\n Group removed',
  747. output.data)
  748. repo = pagure.lib.get_authorized_project(self.session, 'test')
  749. self.assertEqual(len(repo.groups), 1)
  750. data = {'csrf_token': csrf_token}
  751. output = self.app.post(
  752. '/test/dropgroup/1', data=data, follow_redirects=True)
  753. self.assertEqual(output.status_code, 200)
  754. self.assertIn(
  755. '<title>Settings - test - Pagure</title>', output.data)
  756. self.assertIn('<h3>Settings for test</h3>', output.data)
  757. self.assertIn(
  758. '</button>\n Group removed',
  759. output.data)
  760. self.assertNotIn('action="/test/dropgroup/1">', output.data)
  761. self.session.commit()
  762. repo = pagure.lib.get_authorized_project(self.session, 'test')
  763. self.assertEqual(len(repo.groups), 0)
  764. @patch('pagure.decorators.admin_session_timedout')
  765. def test_update_project(self, ast):
  766. """ Test the update_project endpoint. """
  767. ast.return_value = True
  768. # Git repo not found
  769. output = self.app.post('/foo/update')
  770. self.assertEqual(output.status_code, 404)
  771. user = tests.FakeUser()
  772. with tests.user_set(self.app.application, user):
  773. # Project does not exist
  774. output = self.app.post('/foo/update')
  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. # Session timed-out
  779. output = self.app.post('/test/update')
  780. self.assertEqual(output.status_code, 302)
  781. ast.return_value = False
  782. # Not allowed
  783. output = self.app.post('/test/update')
  784. self.assertEqual(output.status_code, 403)
  785. # User not logged in
  786. output = self.app.post('/test/update')
  787. self.assertEqual(output.status_code, 302)
  788. user.username = 'pingou'
  789. with tests.user_set(self.app.application, user):
  790. output = self.app.post('/test/update', follow_redirects=True)
  791. self.assertEqual(output.status_code, 200)
  792. self.assertIn(
  793. '<title>Settings - test - Pagure</title>', output.data)
  794. self.assertIn('<h3>Settings for test</h3>', output.data)
  795. csrf_token = output.data.split(
  796. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  797. data = {
  798. 'description': 'new description for test project #1',
  799. 'csrf_token': csrf_token,
  800. }
  801. output = self.app.post(
  802. '/test/update', data=data, follow_redirects=True)
  803. self.assertEqual(output.status_code, 200)
  804. self.assertIn(
  805. '<title>Settings - test - Pagure</title>', output.data)
  806. self.assertIn('<h3>Settings for test</h3>', output.data)
  807. self.assertIn(
  808. '<input class="form-control" name="avatar_email" value="" />', output.data)
  809. self.assertIn(
  810. '</button>\n Project updated',
  811. output.data)
  812. # Edit the avatar_email
  813. data = {
  814. 'description': 'new description for test project #1',
  815. 'avatar_email': 'pingou@fp.o',
  816. 'csrf_token': csrf_token,
  817. }
  818. output = self.app.post(
  819. '/test/update', data=data, follow_redirects=True)
  820. self.assertEqual(output.status_code, 200)
  821. self.assertIn(
  822. '<title>Settings - test - Pagure</title>', output.data)
  823. self.assertIn('<h3>Settings for test</h3>', output.data)
  824. self.assertIn(
  825. '<input class="form-control" name="avatar_email" value="pingou@fp.o" />',
  826. output.data)
  827. self.assertIn(
  828. '</button>\n Project updated',
  829. output.data)
  830. # Reset the avatar_email
  831. data = {
  832. 'description': 'new description for test project #1',
  833. 'avatar_email': '',
  834. 'csrf_token': csrf_token,
  835. }
  836. output = self.app.post(
  837. '/test/update', data=data, follow_redirects=True)
  838. self.assertEqual(output.status_code, 200)
  839. self.assertIn(
  840. '<title>Settings - test - Pagure</title>', output.data)
  841. self.assertIn('<h3>Settings for test</h3>', output.data)
  842. self.assertIn(
  843. '<input class="form-control" name="avatar_email" value="" />', output.data)
  844. self.assertIn(
  845. '</button>\n Project updated',
  846. output.data)
  847. @patch('pagure.decorators.admin_session_timedout')
  848. def test_update_project_update_tag(self, ast):
  849. """ Test the view_settings endpoint when updating the project's tags.
  850. We had an issue where when you add an existing tag to a project we
  851. were querying the wrong table in the database. It would thus not find
  852. the tag, would try to add it, and (rightfully) complain about duplicated
  853. content.
  854. This test ensure we are behaving properly.
  855. """
  856. ast.return_value = False
  857. tests.create_projects(self.session)
  858. tests.create_projects_git(os.path.join(self.path, 'repos'))
  859. user = tests.FakeUser(username='pingou')
  860. with tests.user_set(self.app.application, user):
  861. output = self.app.get('/test/settings')
  862. self.assertEqual(output.status_code, 200)
  863. self.assertIn(
  864. '<title>Settings - test - Pagure</title>', output.data)
  865. self.assertIn('<h3>Settings for test</h3>', output.data)
  866. csrf_token = output.data.split(
  867. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  868. # Add tag to a project so that they are added to the database
  869. data = {
  870. 'csrf_token': csrf_token,
  871. 'description': 'Test project',
  872. 'tags': 'test,pagure,tag',
  873. }
  874. output = self.app.post(
  875. '/test/update', data=data, follow_redirects=True)
  876. self.assertEqual(output.status_code, 200)
  877. self.assertIn(
  878. '<title>Settings - test - Pagure</title>', output.data)
  879. self.assertIn('<h3>Settings for test</h3>', output.data)
  880. self.assertIn(
  881. '</button>\n Project updated',
  882. output.data)
  883. # Remove two of the tags of the project, they will still be in
  884. # the DB but not associated to this project
  885. data = {
  886. 'csrf_token': csrf_token,
  887. 'description': 'Test project',
  888. 'tags': 'tag',
  889. }
  890. output = self.app.post(
  891. '/test/update', data=data, follow_redirects=True)
  892. self.assertEqual(output.status_code, 200)
  893. self.assertIn(
  894. '<title>Settings - test - Pagure</title>', output.data)
  895. self.assertIn('<h3>Settings for test</h3>', output.data)
  896. self.assertIn(
  897. '</button>\n Project updated',
  898. output.data)
  899. # Try re-adding the two tags, this used to fail before we fixed
  900. # it
  901. data = {
  902. 'csrf_token': csrf_token,
  903. 'description': 'Test project',
  904. 'tags': 'test,pagure,tag',
  905. }
  906. output = self.app.post(
  907. '/test/update', data=data, follow_redirects=True)
  908. self.assertEqual(output.status_code, 200)
  909. self.assertIn(
  910. '<title>Settings - test - Pagure</title>', output.data)
  911. self.assertIn('<h3>Settings for test</h3>', output.data)
  912. self.assertIn(
  913. '</button>\n Project updated',
  914. output.data)
  915. @patch('pagure.decorators.admin_session_timedout')
  916. def test_view_settings(self, ast):
  917. """ Test the view_settings endpoint. """
  918. ast.return_value = False
  919. # No Git repo
  920. output = self.app.get('/foo/settings')
  921. self.assertEqual(output.status_code, 404)
  922. user = tests.FakeUser()
  923. with tests.user_set(self.app.application, user):
  924. output = self.app.get('/foo/settings')
  925. self.assertEqual(output.status_code, 404)
  926. tests.create_projects(self.session)
  927. tests.create_projects_git(os.path.join(self.path, 'repos'))
  928. output = self.app.get('/test/settings')
  929. self.assertEqual(output.status_code, 403)
  930. # User not logged in
  931. output = self.app.get('/test/settings')
  932. self.assertEqual(output.status_code, 302)
  933. user.username = 'pingou'
  934. with tests.user_set(self.app.application, user):
  935. ast.return_value = True
  936. output = self.app.get('/test/settings')
  937. self.assertEqual(output.status_code, 302)
  938. ast.return_value = False
  939. output = self.app.get('/test/settings')
  940. self.assertEqual(output.status_code, 200)
  941. self.assertIn(
  942. '<title>Settings - test - Pagure</title>', output.data)
  943. self.assertIn('<h3>Settings for test</h3>', output.data)
  944. # Both checkbox checked before
  945. self.assertIn(
  946. '<input id="pull_requests" type="checkbox" value="y" '
  947. 'name="pull_requests" checked=""/>', output.data)
  948. self.assertIn(
  949. '<input id="issue_tracker" type="checkbox" value="y" '
  950. 'name="issue_tracker" checked=""/>', output.data)
  951. csrf_token = output.data.split(
  952. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  953. data = {}
  954. output = self.app.post(
  955. '/test/settings', data=data, follow_redirects=True)
  956. self.assertEqual(output.status_code, 200)
  957. self.assertIn(
  958. '<title>Settings - test - Pagure</title>', output.data)
  959. self.assertIn('<h3>Settings for test</h3>', output.data)
  960. # Both checkbox are still checked
  961. output = self.app.get('/test/settings', follow_redirects=True)
  962. self.assertEqual(output.status_code, 200)
  963. self.assertIn(
  964. '<title>Settings - test - Pagure</title>', output.data)
  965. self.assertIn('<h3>Settings for test</h3>', output.data)
  966. self.assertIn(
  967. '<input id="pull_requests" type="checkbox" value="y" '
  968. 'name="pull_requests" checked=""/>', output.data)
  969. self.assertIn(
  970. '<input id="issue_tracker" type="checkbox" value="y" '
  971. 'name="issue_tracker" checked=""/>', output.data)
  972. data = {'csrf_token': csrf_token}
  973. output = self.app.post(
  974. '/test/settings', data=data, follow_redirects=True)
  975. self.assertEqual(output.status_code, 200)
  976. self.assertIn(
  977. '<title>Overview - test - Pagure</title>', output.data)
  978. self.assertIn(
  979. '<div class="projectinfo m-t-1 m-b-1">\n'
  980. 'test project #1 </div>', output.data)
  981. self.assertIn(
  982. '</button>\n Edited successfully '
  983. 'settings of repo: test', output.data)
  984. # Both checkbox are now un-checked
  985. output = self.app.get('/test/settings', follow_redirects=True)
  986. self.assertEqual(output.status_code, 200)
  987. self.assertIn(
  988. '<title>Settings - test - Pagure</title>', output.data)
  989. self.assertIn('<h3>Settings for test</h3>', output.data)
  990. self.assertIn(
  991. '<input id="pull_requests" type="checkbox" value="y" '
  992. 'name="pull_requests" />', output.data)
  993. self.assertIn(
  994. '<input id="issue_tracker" type="checkbox" value="y" '
  995. 'name="issue_tracker" />', output.data)
  996. data = {
  997. 'csrf_token': csrf_token,
  998. 'pull_requests': 'y',
  999. 'issue_tracker': 'y',
  1000. }
  1001. output = self.app.post(
  1002. '/test/settings', data=data, follow_redirects=True)
  1003. self.assertEqual(output.status_code, 200)
  1004. self.assertIn(
  1005. '<title>Overview - test - Pagure</title>', output.data)
  1006. self.assertIn(
  1007. '<div class="projectinfo m-t-1 m-b-1">\n'
  1008. 'test project #1 </div>', output.data)
  1009. self.assertIn(
  1010. '</button>\n Edited successfully '
  1011. 'settings of repo: test', output.data)
  1012. # Both checkbox are again checked
  1013. output = self.app.get('/test/settings', follow_redirects=True)
  1014. self.assertEqual(output.status_code, 200)
  1015. self.assertIn(
  1016. '<title>Settings - test - Pagure</title>', output.data)
  1017. self.assertIn('<h3>Settings for test</h3>', output.data)
  1018. self.assertIn(
  1019. '<input id="pull_requests" type="checkbox" value="y" '
  1020. 'name="pull_requests" checked=""/>', output.data)
  1021. self.assertIn(
  1022. '<input id="issue_tracker" type="checkbox" value="y" '
  1023. 'name="issue_tracker" checked=""/>', output.data)
  1024. @patch('pagure.lib.git.generate_gitolite_acls')
  1025. @patch('pagure.decorators.admin_session_timedout')
  1026. def test_view_settings_pr_only(self, ast, gen_acl):
  1027. """ Test the view_settings endpoint when turning on PR only. """
  1028. ast.return_value = False
  1029. tests.create_projects(self.session)
  1030. tests.create_projects_git(os.path.join(self.path, 'repos'))
  1031. user = tests.FakeUser(username='pingou')
  1032. with tests.user_set(self.app.application, user):
  1033. output = self.app.get('/test/settings')
  1034. self.assertEqual(output.status_code, 200)
  1035. self.assertIn(
  1036. '<title>Settings - test - Pagure</title>', output.data)
  1037. self.assertIn('<h3>Settings for test</h3>', output.data)
  1038. csrf_token = self.get_csrf(output=output)
  1039. data = {
  1040. 'csrf_token': csrf_token,
  1041. 'pull_requests': 'y',
  1042. 'issue_tracker': 'y',
  1043. 'pull_request_access_only': 'y',
  1044. }
  1045. output = self.app.post(
  1046. '/test/settings', data=data, follow_redirects=True)
  1047. self.assertEqual(output.status_code, 200)
  1048. self.assertIn(
  1049. '<title>Overview - test - Pagure</title>', output.data)
  1050. self.assertIn(
  1051. '<div class="projectinfo m-t-1 m-b-1">\n'
  1052. 'test project #1 </div>', output.data)
  1053. self.assertIn(
  1054. '</button>\n Edited successfully '
  1055. 'settings of repo: test', output.data)
  1056. # Both checkbox are again checked
  1057. output = self.app.get('/test/settings', follow_redirects=True)
  1058. self.assertEqual(output.status_code, 200)
  1059. self.assertIn(
  1060. '<title>Settings - test - Pagure</title>', output.data)
  1061. self.assertIn('<h3>Settings for test</h3>', output.data)
  1062. self.assertIn(
  1063. '<input id="pull_requests" type="checkbox" value="y" '
  1064. 'name="pull_requests" checked=""/>', output.data)
  1065. self.assertIn(
  1066. '<input id="issue_tracker" type="checkbox" value="y" '
  1067. 'name="issue_tracker" checked=""/>', output.data)
  1068. self.assertIn(
  1069. '<input id="pull_request_access_only" type="checkbox" '
  1070. 'value="y" name="pull_request_access_only" checked=""/>',
  1071. output.data)
  1072. repo = pagure.lib.get_authorized_project(self.session, 'test')
  1073. self.assertEqual(gen_acl.call_count, 1)
  1074. args = gen_acl.call_args
  1075. self.assertEqual(args[0], tuple())
  1076. self.assertEqual(args[1].keys(), ['project'])
  1077. self.assertEqual(args[1]['project'].fullname, 'test')
  1078. @patch('pagure.decorators.admin_session_timedout')
  1079. def test_fields_in_view_settings(self, ast):
  1080. """ Test the default fields in view_settings endpoint. """
  1081. ast.return_value = False
  1082. # No Git repo
  1083. output = self.app.get('/foo/settings')
  1084. self.assertEqual(output.status_code, 404)
  1085. user = tests.FakeUser()
  1086. with tests.user_set(self.app.application, user):
  1087. output = self.app.get('/foo/settings')
  1088. self.assertEqual(output.status_code, 404)
  1089. item = pagure.lib.model.Project(
  1090. user_id=1, # pingou
  1091. name='test',
  1092. description='test project #1',
  1093. hook_token='aaabbbccc',
  1094. )
  1095. self.session.add(item)
  1096. self.session.commit()
  1097. tests.create_projects_git(os.path.join(self.path, 'repos'))
  1098. output = self.app.get('/test/settings')
  1099. self.assertEqual(output.status_code, 403)
  1100. # User not logged in
  1101. output = self.app.get('/test/settings')
  1102. self.assertEqual(output.status_code, 302)
  1103. user.username = 'pingou'
  1104. with tests.user_set(self.app.application, user):
  1105. ast.return_value = True
  1106. output = self.app.get('/test/settings')
  1107. self.assertEqual(output.status_code, 302)
  1108. ast.return_value = False
  1109. output = self.app.get('/test/settings')
  1110. self.assertEqual(output.status_code, 200)
  1111. self.assertIn(
  1112. '<title>Settings - test - Pagure</title>', output.data)
  1113. self.assertIn('<h3>Settings for test</h3>', output.data)
  1114. # Check that the priorities have their empty fields
  1115. self.assertIn(
  1116. '''<div id="priorities">
  1117. <div class="row p-t-1">
  1118. <div class="col-sm-2 p-r-0">
  1119. <input type="text" name="priority_weigth"
  1120. value="" size="3" class="form-control"/>
  1121. </div>
  1122. <div class="col-sm-9 p-r-0">
  1123. <input type="text" name="priority_title"
  1124. value="" class="form-control"/>
  1125. </div>
  1126. </div>
  1127. </div>''', output.data)
  1128. # Check that the milestones have their empty fields
  1129. self.assertIn(
  1130. '''<div id="milestones">
  1131. <div class="row p-t-1 milestone" id="milestone_1">
  1132. <div class="col-sm-4 p-r-0">
  1133. <input type="text" name="milestones"
  1134. value="" size="3" class="form-control"/>
  1135. </div>
  1136. <div class="col-sm-4 p-r-0">
  1137. <input type="text" name="milestone_date_1"
  1138. value="" class="form-control"/>
  1139. </div>
  1140. <div class="col-sm-2 p-r-0" >
  1141. <span class="oi milestone_order_up"
  1142. data-stone="1"
  1143. data-glyph="arrow-thick-top"></span>
  1144. <span class="oi milestone_order_bottom"
  1145. data-stone="1"
  1146. data-glyph="arrow-thick-bottom"></span>
  1147. </div>
  1148. <div class="col-sm-1 p-r-0" >
  1149. <input type="checkbox" name="active_milestone_1" checked />
  1150. </div>
  1151. </div>''', output.data)
  1152. # Check that the close_status have its empty field
  1153. self.assertIn(
  1154. '''<div id="close_sstatus">
  1155. <div class="row p-t-1">
  1156. <div class="col-sm-12 p-r-0">
  1157. <input type="text" name="close_status"
  1158. value="" class="form-control"/>
  1159. </div>
  1160. </div>''', output.data)
  1161. # Check that the custom fields have their empty fields
  1162. self.assertIn(
  1163. '''<div id="custom_fields">
  1164. <div class="row p-t-1">
  1165. <div class="col-sm-3 p-r-0">
  1166. <input type="text" name="custom_keys"
  1167. value="" class="form-control"/>
  1168. </div>
  1169. <div class="col-sm-2 p-r-0">
  1170. <select name="custom_keys_type" class="form-control">
  1171. <option value="text" >Text</option>
  1172. <option value="boolean" >Boolean</option>
  1173. <option value="link" >Link</option>
  1174. <option value="list" >List</option>
  1175. </select>
  1176. </div>
  1177. <div class="col-sm-6 p-r-0">
  1178. <input title="Comma separated list items" type="text" name="custom_keys_data"
  1179. value="" class="form-control"/>
  1180. </div>
  1181. <div class="col-sm-1 p-r-0">
  1182. <input type="checkbox" name="custom_keys_notify-1" title="Trigger email notification when updated"
  1183. class="form-control"/>
  1184. </div>
  1185. </div>''', output.data)
  1186. def test_view_forks(self):
  1187. """ Test the view_forks endpoint. """
  1188. output = self.app.get('/foo/stats', follow_redirects=True)
  1189. self.assertEqual(output.status_code, 404)
  1190. tests.create_projects(self.session)
  1191. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1192. output = self.app.get('/test/stats', follow_redirects=True)
  1193. self.assertEqual(output.status_code, 200)
  1194. self.assertTrue('This project has not been forked.' in output.data)
  1195. @patch.dict('pagure.config.config', {'CASE_SENSITIVE': True})
  1196. def test_view_repo_case_sensitive(self):
  1197. """ Test the view_repo endpoint. """
  1198. tests.create_projects(self.session)
  1199. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1200. output = self.app.get('/test')
  1201. self.assertEqual(output.status_code, 200)
  1202. self.assertTrue('<p>This repo is brand new!</p>' in output.data)
  1203. output = self.app.get('/TEST')
  1204. self.assertEqual(output.status_code, 404)
  1205. def test_view_repo_more_button_absent_no_auth(self):
  1206. """ Test the view_repo endpoint and check if the "more" button is
  1207. absent when not logged in. """
  1208. tests.create_projects(self.session)
  1209. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1210. output = self.app.get('/test')
  1211. self.assertEqual(output.status_code, 200)
  1212. output_text = output.get_data(as_text=True)
  1213. self.assertNotIn(
  1214. '<span class="pull-xs-right"><a data-toggle="collapse" '
  1215. 'href="#moregiturls"', output_text)
  1216. self.assertIn('<p>This repo is brand new!</p>', output_text)
  1217. self.assertIn(
  1218. '<div class="projectinfo m-t-1 m-b-1">\n'
  1219. 'test project #1 </div>', output_text)
  1220. self.assertIn(
  1221. '<span class="hidden-sm-down">Stats&nbsp;</span>',
  1222. output_text)
  1223. self.perfMaxWalks(0, 0)
  1224. self.perfReset()
  1225. def test_view_repo_more_button_present(self):
  1226. """ Test the view_repo endpoint and check if the "more" button is
  1227. present when it should be. """
  1228. tests.create_projects(self.session)
  1229. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1230. user = tests.FakeUser(username='pingou')
  1231. with tests.user_set(self.app.application, user):
  1232. output = self.app.get('/test')
  1233. self.assertEqual(output.status_code, 200)
  1234. output_text = output.get_data(as_text=True)
  1235. self.assertIn(
  1236. '<span class="pull-xs-right"><a data-toggle="collapse" '
  1237. 'href="#moregiturls"', output_text)
  1238. self.assertIn('<p>This repo is brand new!</p>', output_text)
  1239. self.assertIn(
  1240. '<div class="projectinfo m-t-1 m-b-1">\n'
  1241. 'test project #1 </div>', output_text)
  1242. self.assertIn(
  1243. '<span class="hidden-sm-down">Stats&nbsp;</span>',
  1244. output_text)
  1245. self.perfMaxWalks(0, 0)
  1246. self.perfReset()
  1247. def test_view_repo_more_button_absent_no_access(self):
  1248. """ Test the view_repo endpoint and check if the "more" button is
  1249. absent if the user doesn't have access to the project. """
  1250. tests.create_projects(self.session)
  1251. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1252. user = tests.FakeUser(username='foo')
  1253. with tests.user_set(self.app.application, user):
  1254. output = self.app.get('/test')
  1255. self.assertEqual(output.status_code, 200)
  1256. output_text = output.get_data(as_text=True)
  1257. self.assertNotIn(
  1258. '<span class="pull-xs-right"><a data-toggle="collapse" '
  1259. 'href="#moregiturls"', output_text)
  1260. self.assertIn('<p>This repo is brand new!</p>', output_text)
  1261. self.assertIn(
  1262. '<div class="projectinfo m-t-1 m-b-1">\n'
  1263. 'test project #1 </div>', output_text)
  1264. self.assertIn(
  1265. '<span class="hidden-sm-down">Stats&nbsp;</span>',
  1266. output_text)
  1267. self.perfMaxWalks(0, 0)
  1268. self.perfReset()
  1269. def test_view_repo(self):
  1270. """ Test the view_repo endpoint. """
  1271. output = self.app.get('/foo')
  1272. # No project registered in the DB
  1273. self.assertEqual(output.status_code, 404)
  1274. tests.create_projects(self.session)
  1275. output = self.app.get('/test')
  1276. # No git repo associated
  1277. self.assertEqual(output.status_code, 404)
  1278. self.perfMaxWalks(0, 0)
  1279. self.perfReset()
  1280. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1281. output = self.app.get('/test')
  1282. self.assertEqual(output.status_code, 200)
  1283. self.assertTrue('<p>This repo is brand new!</p>' in output.data)
  1284. self.assertIn(
  1285. '<div class="projectinfo m-t-1 m-b-1">\n'
  1286. 'test project #1 </div>', output.data)
  1287. self.assertIn(
  1288. '<span class="hidden-sm-down">Stats&nbsp;</span>',
  1289. output.data)
  1290. self.perfMaxWalks(0, 0)
  1291. self.perfReset()
  1292. output = self.app.get('/test/')
  1293. self.assertEqual(output.status_code, 200)
  1294. self.assertTrue('<p>This repo is brand new!</p>' in output.data)
  1295. self.assertIn(
  1296. '<div class="projectinfo m-t-1 m-b-1">\n'
  1297. 'test project #1 </div>', output.data)
  1298. self.perfMaxWalks(0, 0)
  1299. self.perfReset()
  1300. # Add some content to the git repo
  1301. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  1302. 'test.git'))
  1303. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  1304. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'), 'README.txt')
  1305. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'), 'README.dummy')
  1306. self.perfReset()
  1307. # Authenticated, the Fork button appears
  1308. user = tests.FakeUser(username='pingou')
  1309. with tests.user_set(self.app.application, user):
  1310. output = self.app.get('/test')
  1311. self.assertEqual(output.status_code, 200)
  1312. self.assertIn(
  1313. '<span class="oi" data-glyph="fork"></span>Fork</button>',
  1314. output.data)
  1315. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1316. self.assertFalse('Forked from' in output.data)
  1317. self.assertFalse('README.txt' in output.data)
  1318. self.assertFalse('README.dummy' in output.data)
  1319. self.assertIn(
  1320. '<div class="projectinfo m-t-1 m-b-1">\n'
  1321. 'test project #1 </div>', output.data)
  1322. self.perfMaxWalks(3, 8) # Target: (1, 3)
  1323. self.perfReset()
  1324. # Non-authenticated, the Fork button does not appear
  1325. output = self.app.get('/test')
  1326. self.assertEqual(output.status_code, 200)
  1327. self.assertNotIn(
  1328. '<span class="oi" data-glyph="fork"></span>Fork</button>',
  1329. output.data)
  1330. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1331. self.assertFalse('Forked from' in output.data)
  1332. self.assertFalse('README.txt' in output.data)
  1333. self.assertFalse('README.dummy' in output.data)
  1334. self.assertIn(
  1335. '<div class="projectinfo m-t-1 m-b-1">\n'
  1336. 'test project #1 </div>', output.data)
  1337. self.perfMaxWalks(3, 8) # Target: (1, 3)
  1338. self.perfReset()
  1339. # Turn that repo into a fork
  1340. repo = pagure.lib.get_authorized_project(self.session, 'test')
  1341. repo.parent_id = 2
  1342. repo.is_fork = True
  1343. self.session.add(repo)
  1344. self.session.commit()
  1345. # View the repo in the UI
  1346. output = self.app.get('/test')
  1347. self.assertEqual(output.status_code, 404)
  1348. # Add some content to the git repo
  1349. tests.add_content_git_repo(
  1350. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  1351. tests.add_readme_git_repo(
  1352. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  1353. # Authenticated and already have a fork, the View Fork button appears
  1354. user = tests.FakeUser(username='pingou')
  1355. with tests.user_set(self.app.application, user):
  1356. output = self.app.get('/fork/pingou/test')
  1357. self.assertEqual(output.status_code, 200)
  1358. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1359. self.assertIn(
  1360. '<div class="projectinfo m-t-1 m-b-1">\n'
  1361. 'test project #1 </div>', output.data)
  1362. self.assertTrue('Forked from' in output.data)
  1363. self.assertNotIn(
  1364. '<span class="oi" data-glyph="fork"></span>Fork</button>',
  1365. output.data)
  1366. self.assertIn(
  1367. '<span class="oi" data-glyph="fork"></span> View Fork',
  1368. output.data)
  1369. self.perfMaxWalks(1, 3)
  1370. self.perfReset()
  1371. # Authenticated, the Fork button appears
  1372. user = tests.FakeUser(username='foo')
  1373. with tests.user_set(self.app.application, user):
  1374. output = self.app.get('/fork/pingou/test')
  1375. self.assertEqual(output.status_code, 200)
  1376. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1377. self.assertIn(
  1378. '<div class="projectinfo m-t-1 m-b-1">\n'
  1379. 'test project #1 </div>', output.data)
  1380. self.assertTrue('Forked from' in output.data)
  1381. self.assertNotIn(
  1382. '<span class="oi" data-glyph="fork"></span> View Fork',
  1383. output.data)
  1384. self.assertIn(
  1385. '<span class="oi" data-glyph="fork"></span>Fork</button>',
  1386. output.data)
  1387. self.perfMaxWalks(1, 3)
  1388. self.perfReset()
  1389. # Non-authenticated, the Fork button does not appear
  1390. output = self.app.get('/fork/pingou/test')
  1391. self.assertEqual(output.status_code, 200)
  1392. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1393. self.assertIn(
  1394. '<div class="projectinfo m-t-1 m-b-1">\n'
  1395. 'test project #1 </div>', output.data)
  1396. self.assertTrue('Forked from' in output.data)
  1397. self.assertNotIn(
  1398. '<span class="oi" data-glyph="fork"></span> View Fork',
  1399. output.data)
  1400. self.assertNotIn(
  1401. '<span class="oi" data-glyph="fork"></span>Fork</button>',
  1402. output.data)
  1403. self.perfMaxWalks(1, 3)
  1404. self.perfReset()
  1405. # Add a fork of a fork
  1406. item = pagure.lib.model.Project(
  1407. user_id=1, # pingou
  1408. name='test3',
  1409. description='test project #3',
  1410. is_fork=True,
  1411. parent_id=1,
  1412. hook_token='aaabbbmmm',
  1413. )
  1414. self.session.add(item)
  1415. self.session.commit()
  1416. tests.add_content_git_repo(
  1417. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  1418. tests.add_readme_git_repo(
  1419. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  1420. tests.add_commit_git_repo(
  1421. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'),
  1422. ncommits=10)
  1423. output = self.app.get('/fork/pingou/test3')
  1424. self.assertEqual(output.status_code, 200)
  1425. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1426. self.assertIn(
  1427. '<div class="projectinfo m-t-1 m-b-1">\n'
  1428. 'test project #3 </div>', output.data)
  1429. self.assertTrue('Forked from' in output.data)
  1430. self.perfMaxWalks(3, 18) # Ideal: (1, 3)
  1431. self.perfReset()
  1432. def test_view_repo_empty(self):
  1433. """ Test the view_repo endpoint on a repo w/o master branch. """
  1434. tests.create_projects(self.session)
  1435. # Create a git repo to play with
  1436. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  1437. pygit2.init_repository(gitrepo, bare=True)
  1438. # Create a fork of this repo
  1439. newpath = tempfile.mkdtemp(prefix='pagure-viewrepo-test')
  1440. new_repo = pygit2.clone_repository(gitrepo, newpath)
  1441. # Edit the sources file again
  1442. with open(os.path.join(newpath, 'sources'), 'w') as stream:
  1443. stream.write('foo\n bar\nbaz\n boose')
  1444. new_repo.index.add('sources')
  1445. new_repo.index.write()
  1446. # Commits the files added
  1447. tree = new_repo.index.write_tree()
  1448. author = pygit2.Signature(
  1449. 'Alice Author', 'alice@authors.tld')
  1450. committer = pygit2.Signature(
  1451. 'Cecil Committer', 'cecil@committers.tld')
  1452. new_repo.create_commit(
  1453. 'refs/heads/feature',
  1454. author,
  1455. committer,
  1456. 'A commit on branch feature',
  1457. tree,
  1458. []
  1459. )
  1460. refname = 'refs/heads/feature'
  1461. ori_remote = new_repo.remotes[0]
  1462. PagureRepo.push(ori_remote, refname)
  1463. output = self.app.get('/test')
  1464. self.assertEqual(output.status_code, 200)
  1465. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1466. self.assertFalse('Forked from' in output.data)
  1467. self.assertIn(
  1468. '<div class="projectinfo m-t-1 m-b-1">\n'
  1469. 'test project #1 </div>', output.data)
  1470. self.assertEqual(
  1471. output.data.count('<span class="commitid">'), 0)
  1472. shutil.rmtree(newpath)
  1473. def test_view_repo_branch(self):
  1474. """ Test the view_repo_branch endpoint. """
  1475. output = self.app.get('/foo/branch/master')
  1476. # No project registered in the DB
  1477. self.assertEqual(output.status_code, 404)
  1478. tests.create_projects(self.session)
  1479. output = self.app.get('/test/branch/master')
  1480. # No git repo associated
  1481. self.assertEqual(output.status_code, 404)
  1482. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1483. output = self.app.get('/test/branch/master')
  1484. self.assertEqual(output.status_code, 404)
  1485. # Add some content to the git repo
  1486. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  1487. 'test.git'))
  1488. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  1489. output = self.app.get('/test/branch/master')
  1490. self.assertEqual(output.status_code, 200)
  1491. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1492. self.assertFalse('Forked from' in output.data)
  1493. self.assertIn(
  1494. '<div class="projectinfo m-t-1 m-b-1">\n'
  1495. 'test project #1 </div>', output.data)
  1496. # Turn that repo into a fork
  1497. repo = pagure.lib.get_authorized_project(self.session, 'test')
  1498. repo.parent_id = 2
  1499. repo.is_fork = True
  1500. self.session.add(repo)
  1501. self.session.commit()
  1502. # View the repo in the UI
  1503. output = self.app.get('/test/branch/master')
  1504. self.assertEqual(output.status_code, 404)
  1505. # Add some content to the git repo
  1506. tests.add_content_git_repo(
  1507. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  1508. tests.add_readme_git_repo(
  1509. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  1510. output = self.app.get('/fork/pingou/test/branch/master')
  1511. self.assertEqual(output.status_code, 200)
  1512. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1513. self.assertIn(
  1514. '<div class="projectinfo m-t-1 m-b-1">\n'
  1515. 'test project #1 </div>', output.data)
  1516. self.assertTrue('Forked from' in output.data)
  1517. # Add a fork of a fork
  1518. item = pagure.lib.model.Project(
  1519. user_id=1, # pingou
  1520. name='test3',
  1521. description='test project #3',
  1522. is_fork=True,
  1523. parent_id=1,
  1524. hook_token='aaabbbnnn',
  1525. )
  1526. self.session.add(item)
  1527. self.session.commit()
  1528. tests.add_content_git_repo(
  1529. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  1530. tests.add_readme_git_repo(
  1531. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  1532. tests.add_commit_git_repo(
  1533. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'),
  1534. ncommits=10)
  1535. output = self.app.get('/fork/pingou/test3/branch/master')
  1536. self.assertEqual(output.status_code, 200)
  1537. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  1538. self.assertIn(
  1539. '<div class="projectinfo m-t-1 m-b-1">\n'
  1540. 'test project #3 </div>', output.data)
  1541. self.assertTrue('Forked from' in output.data)
  1542. def test_view_commits(self):
  1543. """ Test the view_commits endpoint. """
  1544. output = self.app.get('/foo/commits')
  1545. # No project registered in the DB
  1546. self.assertEqual(output.status_code, 404)
  1547. tests.create_projects(self.session)
  1548. output = self.app.get('/test/commits')
  1549. # No git repo associated
  1550. self.assertEqual(output.status_code, 404)
  1551. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1552. output = self.app.get('/test/commits')
  1553. self.assertEqual(output.status_code, 200)
  1554. self.assertIn('<p>This repo is brand new!</p>', output.data)
  1555. self.assertIn(
  1556. '<div class="projectinfo m-t-1 m-b-1">\n'
  1557. 'test project #1 </div>', output.data)
  1558. # Add some content to the git repo
  1559. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  1560. 'test.git'))
  1561. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  1562. output = self.app.get('/test/commits')
  1563. self.assertEqual(output.status_code, 200)
  1564. self.assertNotIn('<p>This repo is brand new!</p>', output.data)
  1565. self.assertNotIn('Forked from', output.data)
  1566. self.assertIn(
  1567. '<div class="projectinfo m-t-1 m-b-1">\n'
  1568. 'test project #1 </div>', output.data)
  1569. self.assertIn('<title>Commits - test - Pagure</title>', output.data)
  1570. output = self.app.get('/test/commits/master')
  1571. self.assertEqual(output.status_code, 200)
  1572. self.assertNotIn('<p>This repo is brand new!</p>', output.data)
  1573. self.assertNotIn('Forked from', output.data)
  1574. self.assertIn(
  1575. '<div class="projectinfo m-t-1 m-b-1">\n'
  1576. 'test project #1 </div>', output.data)
  1577. # Turn that repo into a fork
  1578. repo = pagure.lib.get_authorized_project(self.session, 'test')
  1579. repo.parent_id = 2
  1580. repo.is_fork = True
  1581. self.session.add(repo)
  1582. self.session.commit()
  1583. # View the repo in the UI
  1584. output = self.app.get('/test/commits')
  1585. self.assertEqual(output.status_code, 404)
  1586. # Add some content to the git repo
  1587. tests.add_content_git_repo(
  1588. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  1589. tests.add_readme_git_repo(
  1590. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  1591. output = self.app.get('/fork/pingou/test/commits?page=abc')
  1592. self.assertEqual(output.status_code, 200)
  1593. self.assertNotIn('<p>This repo is brand new!</p>', output.data)
  1594. self.assertIn(
  1595. '<div class="projectinfo m-t-1 m-b-1">\n'
  1596. 'test project #1 </div>', output.data)
  1597. self.assertIn('Forked from', output.data)
  1598. # Add a fork of a fork
  1599. item = pagure.lib.model.Project(
  1600. user_id=1, # pingou
  1601. name='test3',
  1602. description='test project #3',
  1603. is_fork=True,
  1604. parent_id=1,
  1605. hook_token='aaabbbooo',
  1606. )
  1607. self.session.add(item)
  1608. self.session.commit()
  1609. tests.add_content_git_repo(
  1610. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  1611. tests.add_readme_git_repo(
  1612. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  1613. tests.add_commit_git_repo(
  1614. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'),
  1615. ncommits=10)
  1616. output = self.app.get('/fork/pingou/test3/commits/fobranch')
  1617. self.assertEqual(output.status_code, 404)
  1618. output = self.app.get('/fork/pingou/test3/commits')
  1619. self.assertEqual(output.status_code, 200)
  1620. self.assertNotIn('<p>This repo is brand new!</p>', output.data)
  1621. self.assertIn(
  1622. '<div class="projectinfo m-t-1 m-b-1">\n'
  1623. 'test project #3 </div>', output.data)
  1624. self.assertIn('Forked from', output.data)
  1625. def test_compare_commits(self):
  1626. """ Test the compare_commits endpoint. """
  1627. # First two commits comparison
  1628. def compare_first_two(c1, c2):
  1629. # View commits comparison
  1630. output = self.app.get('/test/c/%s..%s' % (c2.oid.hex, c1.oid.hex))
  1631. self.assertEqual(output.status_code, 200)
  1632. self.assertIn(
  1633. '<title>Diff from %s to %s - test\n - Pagure</title>'
  1634. % (c2.oid.hex, c1.oid.hex),
  1635. output.data)
  1636. self.assertIn(
  1637. '<h5 class="text-muted">%s .. %s</h5>'
  1638. % (c2.oid.hex, c1.oid.hex),
  1639. output.data)
  1640. self.assertIn(
  1641. '<span>Commits&nbsp;</span>\n ' +
  1642. '<span ' +
  1643. 'class="label label-default label-pill">' +
  1644. '\n 2\n </span>',
  1645. output.data)
  1646. self.assertIn(
  1647. '<span style="color: #a40000; background-color: #ffdddd">- ' +
  1648. 'Row 0', output.data)
  1649. # View inverse commits comparison
  1650. output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c2.oid.hex))
  1651. self.assertEqual(output.status_code, 200)
  1652. self.assertIn(
  1653. '<title>Diff from %s to %s - test\n - Pagure</title>' %
  1654. (c1.oid.hex, c2.oid.hex),
  1655. output.data)
  1656. self.assertIn(
  1657. '<span>Commits&nbsp;</span>\n ' +
  1658. '<span ' +
  1659. 'class="label label-default label-pill">' +
  1660. '\n 2\n </span>',
  1661. output.data)
  1662. self.assertIn(
  1663. '<h5 class="text-muted">%s .. %s</h5>' %
  1664. (c1.oid.hex, c2.oid.hex),
  1665. output.data)
  1666. self.assertIn(
  1667. '<span style="color: #00A000; background-color: #ddffdd">' +
  1668. '+ Row 0', output.data)
  1669. def compare_all(c1, c3):
  1670. # View commits comparison
  1671. output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c3.oid.hex))
  1672. self.assertEqual(output.status_code, 200)
  1673. self.assertIn(
  1674. '<title>Diff from %s to %s - test\n - Pagure</title>' %
  1675. (c1.oid.hex, c3.oid.hex), output.data)
  1676. self.assertIn(
  1677. '<h5 class="text-muted">%s .. %s</h5>' %
  1678. (c1.oid.hex, c3.oid.hex),
  1679. output.data)
  1680. self.assertIn(
  1681. '<span style="color: #00A000; background-color: ' +
  1682. '#ddffdd">+ Row 0</span>', output.data)
  1683. self.assertEqual(
  1684. output.data.count(
  1685. '<span style="color: #00A000; background-color: ' +
  1686. '#ddffdd">+ Row 0'), 2)
  1687. self.assertIn(
  1688. '<span>Commits&nbsp;</span>\n ' +
  1689. '<span ' +
  1690. 'class="label label-default label-pill">' +
  1691. '\n 3\n </span>',
  1692. output.data)
  1693. self.assertIn(
  1694. 'title="View file as of 4829cf">Šource</a>', output.data)
  1695. self.assertIn(
  1696. '<div><small>file added</small></div></h5>', output.data)
  1697. # View inverse commits comparison
  1698. output = self.app.get(
  1699. '/test/c/%s..%s' % (c3.oid.hex, c1.oid.hex))
  1700. self.assertEqual(output.status_code, 200)
  1701. self.assertIn(
  1702. '<title>Diff from %s to %s - test\n - Pagure</title>' %
  1703. (c3.oid.hex, c1.oid.hex), output.data)
  1704. self.assertIn(
  1705. '<h5 class="text-muted">%s .. %s</h5>' %
  1706. (c3.oid.hex, c1.oid.hex),
  1707. output.data)
  1708. self.assertIn(
  1709. '<span style="color: #800080; font-weight: bold">@@ -1,2 +1,1' +
  1710. ' @@', output.data)
  1711. self.assertIn(
  1712. '<span style="color: #a40000; background-color: #ffdddd">- ' +
  1713. 'Row 0</span>', output.data)
  1714. self.assertIn(
  1715. '<span>Commits&nbsp;</span>\n ' +
  1716. '<span ' +
  1717. 'class="label label-default label-pill">' +
  1718. '\n 3\n </span>',
  1719. output.data)
  1720. self.assertIn(
  1721. 'title="View file as of 000000">Šource</a>', output.data)
  1722. self.assertIn(
  1723. '<div><small>file removed</small></div></h5>', output.data)
  1724. output = self.app.get('/foo/bar')
  1725. # No project registered in the DB
  1726. self.assertEqual(output.status_code, 404)
  1727. tests.create_projects(self.session)
  1728. output = self.app.get('/test/bar')
  1729. # No git repo associated
  1730. self.assertEqual(output.status_code, 404)
  1731. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1732. output = self.app.get('/test/bar')
  1733. self.assertEqual(output.status_code, 404)
  1734. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  1735. # Add one commit to git repo
  1736. tests.add_commit_git_repo(
  1737. os.path.join(self.path, 'repos', 'test.git'), ncommits=1)
  1738. c1 = repo.revparse_single('HEAD')
  1739. time.sleep(1)
  1740. # Add another commit to git repo
  1741. tests.add_commit_git_repo(
  1742. os.path.join(self.path, 'repos', 'test.git'), ncommits=1)
  1743. c2 = repo.revparse_single('HEAD')
  1744. time.sleep(1)
  1745. # Add one more commit to git repo
  1746. tests.add_commit_git_repo(
  1747. os.path.join(self.path, 'repos', 'test.git'),
  1748. ncommits=1, filename='Šource')
  1749. c3 = repo.revparse_single('HEAD')
  1750. compare_first_two(c1, c2)
  1751. compare_all(c1, c3)
  1752. user = tests.FakeUser()
  1753. # Set user logged in
  1754. with tests.user_set(self.app.application, user):
  1755. compare_first_two(c1, c2)
  1756. compare_all(c1, c3)
  1757. def test_view_file(self):
  1758. """ Test the view_file endpoint. """
  1759. output = self.app.get('/foo/blob/foo/f/sources')
  1760. # No project registered in the DB
  1761. self.assertEqual(output.status_code, 404)
  1762. tests.create_projects(self.session)
  1763. output = self.app.get('/test/blob/foo/f/sources')
  1764. # No git repo associated
  1765. self.assertEqual(output.status_code, 404)
  1766. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1767. output = self.app.get('/test/blob/foo/f/sources')
  1768. self.assertEqual(output.status_code, 404)
  1769. # Add some content to the git repo
  1770. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  1771. 'test.git'))
  1772. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  1773. tests.add_binary_git_repo(
  1774. os.path.join(self.path, 'repos', 'test.git'), 'test.jpg')
  1775. tests.add_binary_git_repo(
  1776. os.path.join(self.path, 'repos', 'test.git'), 'test_binary')
  1777. output = self.app.get('/test/blob/master/foofile')
  1778. self.assertEqual(output.status_code, 404)
  1779. # View in a branch
  1780. output = self.app.get('/test/blob/master/f/sources')
  1781. self.assertEqual(output.status_code, 200)
  1782. self.assertTrue('<table class="code_table">' in output.data)
  1783. self.assertTrue(
  1784. '<tr><td class="cell1"><a id="_1" href="#_1" data-line-number="1"></a></td>'
  1785. in output.data)
  1786. self.assertTrue(
  1787. '<td class="cell2"><pre> bar</pre></td>' in output.data)
  1788. # Empty files should also be displayed
  1789. tests.add_content_to_git(
  1790. os.path.join(self.path, 'repos', 'test.git'),
  1791. filename="emptyfile.md",
  1792. content="")
  1793. output = self.app.get('/test/blob/master/f/emptyfile.md')
  1794. self.assertEqual(output.status_code, 200)
  1795. self.assertIn(
  1796. '<a class="btn btn-secondary btn-sm" '
  1797. 'href="/test/raw/master/f/emptyfile.md" '
  1798. 'title="View as raw">Raw</a>', output.data)
  1799. self.assertIn(
  1800. '<div class="m-a-2">\n'
  1801. ' \n </div>', output.data)
  1802. # View what's supposed to be an image
  1803. output = self.app.get('/test/blob/master/f/test.jpg')
  1804. self.assertEqual(output.status_code, 200)
  1805. self.assertIn(
  1806. 'Binary files cannot be rendered.<br/>', output.data)
  1807. self.assertIn(
  1808. '<a href="/test/raw/master/f/test.jpg">view the raw version',
  1809. output.data)
  1810. # View by commit id
  1811. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  1812. commit = repo.revparse_single('HEAD')
  1813. output = self.app.get('/test/blob/%s/f/test.jpg' % commit.oid.hex)
  1814. self.assertEqual(output.status_code, 200)
  1815. self.assertIn(
  1816. 'Binary files cannot be rendered.<br/>', output.data)
  1817. self.assertIn('/f/test.jpg">view the raw version', output.data)
  1818. # View by image name -- somehow we support this
  1819. output = self.app.get('/test/blob/sources/f/test.jpg')
  1820. self.assertEqual(output.status_code, 200)
  1821. self.assertIn(
  1822. 'Binary files cannot be rendered.<br/>', output.data)
  1823. self.assertIn('/f/test.jpg">view the raw version', output.data)
  1824. # View binary file
  1825. output = self.app.get('/test/blob/sources/f/test_binary')
  1826. self.assertEqual(output.status_code, 200)
  1827. self.assertIn('/f/test_binary">view the raw version', output.data)
  1828. self.assertTrue(
  1829. 'Binary files cannot be rendered.<br/>'
  1830. in output.data)
  1831. # View folder
  1832. output = self.app.get('/test/blob/master/f/folder1')
  1833. self.assertEqual(output.status_code, 200)
  1834. self.assertIn(
  1835. '<span class="oi text-muted" data-glyph="folder"></span>',
  1836. output.data)
  1837. self.assertIn('<title>Tree - test - Pagure</title>', output.data)
  1838. self.assertIn(
  1839. '<a href="/test/blob/master/f/folder1/folder2">', output.data)
  1840. # Verify the nav links correctly when viewing a nested folder/file.
  1841. output = self.app.get('/test/blob/master/f/folder1/folder2/file')
  1842. self.assertEqual(output.status_code, 200)
  1843. self.assertIn(
  1844. '<li><a href="/test/blob/master/f/folder1/folder2">\n'
  1845. ' <span class="oi" data-glyph="folder">'
  1846. '</span>&nbsp; folder2</a>\n'
  1847. ' </li>', output.data)
  1848. # View by image name -- with a non-existant file
  1849. output = self.app.get('/test/blob/sources/f/testfoo.jpg')
  1850. self.assertEqual(output.status_code, 404)
  1851. output = self.app.get('/test/blob/master/f/folder1/testfoo.jpg')
  1852. self.assertEqual(output.status_code, 404)
  1853. # View file with a non-ascii name
  1854. tests.add_commit_git_repo(
  1855. os.path.join(self.path, 'repos', 'test.git'),
  1856. ncommits=1, filename='Šource')
  1857. output = self.app.get('/test/blob/master/f/Šource')
  1858. self.assertEqual(output.status_code, 200)
  1859. self.assertEqual(output.headers['Content-Type'].lower(),
  1860. 'text/html; charset=utf-8')
  1861. self.assertIn('</span>&nbsp; Šource', output.data)
  1862. self.assertIn('<table class="code_table">', output.data)
  1863. self.assertIn(
  1864. '<tr><td class="cell1"><a id="_1" href="#_1" '
  1865. 'data-line-number="1"></a></td>', output.data)
  1866. self.assertTrue(
  1867. '<td class="cell2"><pre><span></span>Row 0</pre></td>'
  1868. in output.data
  1869. or
  1870. '<td class="cell2"><pre>Row 0</pre></td>' in output.data
  1871. )
  1872. # Add a fork of a fork
  1873. item = pagure.lib.model.Project(
  1874. user_id=1, # pingou
  1875. name='test3',
  1876. description='test project #3',
  1877. is_fork=True,
  1878. parent_id=1,
  1879. hook_token='aaabbbppp',
  1880. )
  1881. self.session.add(item)
  1882. self.session.commit()
  1883. tests.add_content_git_repo(
  1884. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  1885. tests.add_readme_git_repo(
  1886. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  1887. tests.add_commit_git_repo(
  1888. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'),
  1889. ncommits=10)
  1890. # Verify the nav links correctly when viewing a file/folder in a fork.
  1891. output = self.app.get(
  1892. '/fork/pingou/test3/blob/master/f/folder1/folder2/file')
  1893. self.assertEqual(output.status_code, 200)
  1894. self.assertIn(
  1895. '<li><a href="/fork/pingou/test3/blob/master/f/folder1/folder2">\n'
  1896. ' <span class="oi" data-glyph="folder"></span>&nbsp; '
  1897. 'folder2</a>\n </li>', output.data)
  1898. output = self.app.get('/fork/pingou/test3/blob/master/f/sources')
  1899. self.assertEqual(output.status_code, 200)
  1900. self.assertIn('<table class="code_table">', output.data)
  1901. self.assertIn(
  1902. '<tr><td class="cell1"><a id="_1" href="#_1" data-line-number="1"></a></td>',
  1903. output.data)
  1904. self.assertIn(
  1905. '<td class="cell2"><pre> barRow 0</pre></td>', output.data)
  1906. @patch(
  1907. 'pagure.lib.encoding_utils.decode',
  1908. MagicMock(side_effect=pagure.exceptions.PagureException))
  1909. def test_view_file_with_wrong_encoding(self):
  1910. """ Test the view_file endpoint. """
  1911. tests.create_projects(self.session)
  1912. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1913. # Add some content to the git repo
  1914. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  1915. 'test.git'))
  1916. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  1917. tests.add_binary_git_repo(
  1918. os.path.join(self.path, 'repos', 'test.git'), 'test.jpg')
  1919. tests.add_binary_git_repo(
  1920. os.path.join(self.path, 'repos', 'test.git'), 'test_binary')
  1921. # View file
  1922. output = self.app.get('/test/blob/master/f/sources')
  1923. self.assertEqual(output.status_code, 200)
  1924. self.assertIn('Binary files cannot be rendered.<br/>', output.data)
  1925. def test_view_raw_file(self):
  1926. """ Test the view_raw_file endpoint. """
  1927. output = self.app.get('/foo/raw/foo/sources')
  1928. # No project registered in the DB
  1929. self.assertEqual(output.status_code, 404)
  1930. tests.create_projects(self.session)
  1931. output = self.app.get('/test/raw/foo/sources')
  1932. # No git repo associated
  1933. self.assertEqual(output.status_code, 404)
  1934. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  1935. output = self.app.get('/test/raw/foo/sources')
  1936. self.assertEqual(output.status_code, 404)
  1937. # Add some content to the git repo
  1938. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  1939. # View first commit
  1940. output = self.app.get('/test/raw/master')
  1941. self.assertEqual(output.status_code, 200)
  1942. self.assertEqual(output.headers['Content-Type'].lower(),
  1943. 'text/plain; charset=ascii')
  1944. self.assertTrue(':Author: Pierre-Yves Chibon' in output.data)
  1945. # Add some more content to the repo
  1946. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  1947. 'test.git'))
  1948. tests.add_binary_git_repo(
  1949. os.path.join(self.path, 'repos', 'test.git'), 'test.jpg')
  1950. tests.add_binary_git_repo(
  1951. os.path.join(self.path, 'repos', 'test.git'), 'test_binary')
  1952. output = self.app.get('/test/raw/master/f/foofile')
  1953. self.assertEqual(output.status_code, 404)
  1954. # View in a branch
  1955. output = self.app.get('/test/raw/master/f/sources')
  1956. self.assertEqual(output.headers['Content-Type'].lower(),
  1957. 'text/plain; charset=ascii')
  1958. self.assertEqual(output.status_code, 200)
  1959. self.assertTrue('foo\n bar' in output.data)
  1960. # View what's supposed to be an image
  1961. output = self.app.get('/test/raw/master/f/test.jpg')
  1962. self.assertEqual(output.status_code, 200)
  1963. self.assertTrue(output.data.startswith('\x00\x00\x01\x00'))
  1964. # View by commit id
  1965. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  1966. commit = repo.revparse_single('HEAD')
  1967. output = self.app.get('/test/raw/%s/f/test.jpg' % commit.oid.hex)
  1968. self.assertEqual(output.status_code, 200)
  1969. self.assertTrue(output.data.startswith('\x00\x00\x01\x00'))
  1970. # View by image name -- somehow we support this
  1971. output = self.app.get('/test/raw/sources/f/test.jpg')
  1972. self.assertEqual(output.status_code, 200)
  1973. self.assertTrue(output.data.startswith('\x00\x00\x01\x00'))
  1974. # View binary file
  1975. output = self.app.get('/test/raw/sources/f/test_binary')
  1976. self.assertEqual(output.status_code, 200)
  1977. self.assertEqual(output.headers['Content-Type'].lower(),
  1978. 'application/octet-stream')
  1979. self.assertTrue(output.data.startswith('\x00\x00\x01\x00'))
  1980. # View folder
  1981. output = self.app.get('/test/raw/master/f/folder1')
  1982. self.assertEqual(output.status_code, 404)
  1983. # View by image name -- with a non-existant file
  1984. output = self.app.get('/test/raw/sources/f/testfoo.jpg')
  1985. self.assertEqual(output.status_code, 404)
  1986. output = self.app.get('/test/raw/master/f/folder1/testfoo.jpg')
  1987. self.assertEqual(output.status_code, 404)
  1988. output = self.app.get('/test/raw/master/f/')
  1989. self.assertEqual(output.status_code, 404)
  1990. output = self.app.get('/test/raw/master')
  1991. self.assertEqual(output.status_code, 200)
  1992. self.assertEqual(output.headers['Content-Type'].lower(),
  1993. 'text/plain; charset=ascii')
  1994. self.assertTrue(output.data.startswith(
  1995. 'diff --git a/test_binary b/test_binary\n'))
  1996. output = self.app.get('/test/raw/%s' % commit.oid.hex)
  1997. self.assertEqual(output.status_code, 200)
  1998. self.assertTrue(output.data.startswith(
  1999. 'diff --git a/test_binary b/test_binary\n'))
  2000. # Add a fork of a fork
  2001. item = pagure.lib.model.Project(
  2002. user_id=1, # pingou
  2003. name='test3',
  2004. description='test project #3',
  2005. is_fork=True,
  2006. parent_id=1,
  2007. hook_token='aaabbbqqq',
  2008. )
  2009. self.session.add(item)
  2010. self.session.commit()
  2011. tests.add_content_git_repo(
  2012. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  2013. tests.add_readme_git_repo(
  2014. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  2015. tests.add_commit_git_repo(
  2016. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'),
  2017. ncommits=10)
  2018. output = self.app.get('/fork/pingou/test3/raw/master/f/sources')
  2019. self.assertEqual(output.status_code, 200)
  2020. self.assertEqual(output.headers['Content-Type'].lower(),
  2021. 'text/plain; charset=ascii')
  2022. self.assertTrue('foo\n bar' in output.data)
  2023. def test_view_blame_file(self):
  2024. """ Test the view_blame_file endpoint. """
  2025. output = self.app.get('/foo/blame/sources')
  2026. # No project registered in the DB
  2027. self.assertEqual(output.status_code, 404)
  2028. tests.create_projects(self.session)
  2029. output = self.app.get('/test/blame/sources')
  2030. # No git repo associated
  2031. self.assertEqual(output.status_code, 404)
  2032. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  2033. output = self.app.get('/test/blame/sources')
  2034. self.assertEqual(output.status_code, 404)
  2035. # Add some content to the git repo
  2036. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  2037. 'test.git'))
  2038. tests.add_content_git_repo(
  2039. os.path.join(self.path, 'repos', 'test.git'),
  2040. branch='feature')
  2041. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  2042. tests.add_binary_git_repo(
  2043. os.path.join(self.path, 'repos', 'test.git'), 'test.jpg')
  2044. tests.add_binary_git_repo(
  2045. os.path.join(self.path, 'repos', 'test.git'), 'test_binary')
  2046. output = self.app.get('/test/blame/foofile')
  2047. self.assertEqual(output.status_code, 404)
  2048. regex = re.compile('>(\w+)</a></td>\n<td class="cell2">')
  2049. # View in master branch
  2050. output = self.app.get('/test/blame/sources')
  2051. self.assertEqual(output.status_code, 200)
  2052. self.assertIn(b'<table class="code_table">', output.data)
  2053. self.assertIn(
  2054. b'<tr><td class="cell1"><a id="1" href="#1" '
  2055. b'data-line-number="1"></a></td>', output.data)
  2056. self.assertIn(
  2057. b'<td class="cell2"><pre> bar</pre></td>', output.data)
  2058. data = regex.findall(output.data)
  2059. self.assertEqual(len(data), 2)
  2060. # View for a commit
  2061. repo_obj = pygit2.Repository(
  2062. os.path.join(self.path, 'repos', 'test.git'))
  2063. commit = repo_obj[repo_obj.head.target]
  2064. parent = commit.parents[0].oid.hex
  2065. output = self.app.get('/test/blame/sources?identifier=%s' % parent)
  2066. self.assertEqual(output.status_code, 200)
  2067. self.assertIn(b'<table class="code_table">', output.data)
  2068. self.assertIn(
  2069. b'<tr><td class="cell1"><a id="1" href="#1" '
  2070. b'data-line-number="1"></a></td>', output.data)
  2071. self.assertIn(
  2072. b'<td class="cell2"><pre> bar</pre></td>', output.data)
  2073. data = regex.findall(output.data)
  2074. self.assertEqual(len(data), 2)
  2075. # View in feature branch
  2076. output = self.app.get('/test/blame/sources?identifier=feature')
  2077. self.assertEqual(output.status_code, 200)
  2078. self.assertIn(b'<table class="code_table">', output.data)
  2079. self.assertIn(
  2080. b'<tr><td class="cell1"><a id="1" href="#1" '
  2081. b'data-line-number="1"></a></td>', output.data)
  2082. self.assertIn(
  2083. b'<td class="cell2"><pre> bar</pre></td>', output.data)
  2084. data2 = regex.findall(output.data)
  2085. self.assertEqual(len(data2), 2)
  2086. self.assertNotEqual(data, data2)
  2087. # View what's supposed to be an image
  2088. output = self.app.get('/test/blame/test.jpg')
  2089. self.assertEqual(output.status_code, 400)
  2090. self.assertIn(
  2091. b'<title>400 Bad Request</title>', output.data)
  2092. self.assertIn(
  2093. b'<p>Binary files cannot be blamed</p>', output.data)
  2094. # View folder
  2095. output = self.app.get('/test/blame/folder1')
  2096. self.assertEqual(output.status_code, 404)
  2097. self.assertIn("<title>Page not found :'( - Pagure</title>", output.data)
  2098. self.assertIn(
  2099. '<h2>Page not found (404)</h2>', output.data)
  2100. # View by image name -- with a non-existant file
  2101. output = self.app.get('/test/blame/testfoo.jpg')
  2102. self.assertEqual(output.status_code, 404)
  2103. output = self.app.get('/test/blame/folder1/testfoo.jpg')
  2104. self.assertEqual(output.status_code, 404)
  2105. # View file with a non-ascii name
  2106. tests.add_commit_git_repo(
  2107. os.path.join(self.path, 'repos', 'test.git'),
  2108. ncommits=1, filename='Šource')
  2109. output = self.app.get('/test/blame/Šource')
  2110. self.assertEqual(output.status_code, 200)
  2111. self.assertEqual(output.headers['Content-Type'].lower(),
  2112. 'text/html; charset=utf-8')
  2113. self.assertIn('</span>&nbsp; Šource', output.data)
  2114. self.assertIn('<table class="code_table">', output.data)
  2115. self.assertIn(
  2116. '<tr><td class="cell1"><a id="1" href="#1" '
  2117. 'data-line-number="1"></a></td>', output.data)
  2118. self.assertTrue(
  2119. '<td class="cell2"><pre><span></span>Row 0</pre></td>'
  2120. in output.data
  2121. or
  2122. '<td class="cell2"><pre>Row 0</pre></td>' in output.data
  2123. )
  2124. # Add a fork of a fork
  2125. item = pagure.lib.model.Project(
  2126. user_id=1, # pingou
  2127. name='test3',
  2128. description='test project #3',
  2129. is_fork=True,
  2130. parent_id=1,
  2131. hook_token='aaabbbppp',
  2132. )
  2133. self.session.add(item)
  2134. self.session.commit()
  2135. tests.add_content_git_repo(
  2136. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  2137. tests.add_readme_git_repo(
  2138. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  2139. tests.add_commit_git_repo(
  2140. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'),
  2141. ncommits=10)
  2142. tests.add_content_to_git(
  2143. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'),
  2144. content=u'✨☃🍰☃✨'.encode('utf-8'))
  2145. output = self.app.get('/fork/pingou/test3/blame/sources')
  2146. self.assertEqual(output.status_code, 200)
  2147. self.assertIn('<table class="code_table">', output.data)
  2148. self.assertIn(
  2149. '<tr><td class="cell1"><a id="1" href="#1" '
  2150. 'data-line-number="1"></a></td>', output.data)
  2151. self.assertIn(
  2152. '<td class="cell2"><pre> barRow 0</pre></td>', output.data)
  2153. def test_view_blame_file_on_tag(self):
  2154. """ Test the view_blame_file endpoint. """
  2155. regex = re.compile('>(\w+)</a></td>\n<td class="cell2">')
  2156. tests.create_projects(self.session)
  2157. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  2158. # Add some content to the git repo
  2159. tests.add_content_git_repo(
  2160. os.path.join(self.path, 'repos', 'test.git'))
  2161. tests.add_readme_git_repo(
  2162. os.path.join(self.path, 'repos', 'test.git'))
  2163. # add a tag to the git repo
  2164. repo = pygit2.Repository(
  2165. os.path.join(self.path, 'repos', 'test.git'))
  2166. commit = repo[repo.head.target]
  2167. tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0)
  2168. repo.create_tag(
  2169. 'v1.0', commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger,
  2170. "Release v1.0")
  2171. # View for tag v1.0
  2172. output = self.app.get('/test/blame/sources?identifier=v1.0')
  2173. self.assertEqual(output.status_code, 200)
  2174. self.assertIn(b'<table class="code_table">', output.data)
  2175. self.assertIn(
  2176. b'<tr><td class="cell1"><a id="1" href="#1" '
  2177. b'data-line-number="1"></a></td>', output.data)
  2178. self.assertIn(
  2179. b'<td class="cell2"><pre> bar</pre></td>', output.data)
  2180. data = regex.findall(output.data)
  2181. self.assertEqual(len(data), 2)
  2182. def test_view_commit(self):
  2183. """ Test the view_commit endpoint. """
  2184. output = self.app.get('/foo/c/bar')
  2185. # No project registered in the DB
  2186. self.assertEqual(output.status_code, 404)
  2187. tests.create_projects(self.session)
  2188. output = self.app.get('/test/c/bar')
  2189. # No git repo associated
  2190. self.assertEqual(output.status_code, 404)
  2191. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  2192. output = self.app.get('/test/c/bar')
  2193. self.assertEqual(output.status_code, 404)
  2194. # Add a README to the git repo - First commit
  2195. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  2196. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  2197. commit = repo.revparse_single('HEAD')
  2198. # View first commit
  2199. output = self.app.get('/test/c/%s' % commit.oid.hex)
  2200. self.assertEqual(output.status_code, 200)
  2201. self.assertTrue(
  2202. '<div class="list-group" id="diff_list" style="display:none;">'
  2203. in output.data)
  2204. self.assertTrue(' Merged by Alice Author\n' in output.data)
  2205. self.assertTrue(' Committed by Cecil Committer\n' in output.data)
  2206. # View first commit - with the old URL scheme disabled - default
  2207. output = self.app.get(
  2208. '/test/%s' % commit.oid.hex, follow_redirects=True)
  2209. self.assertEqual(output.status_code, 404)
  2210. self.assertIn('<p>Project not found</p>', output.data)
  2211. # Add some content to the git repo
  2212. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  2213. 'test.git'))
  2214. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  2215. commit = repo.revparse_single('HEAD')
  2216. # View another commit
  2217. output = self.app.get('/test/c/%s' % commit.oid.hex)
  2218. self.assertEqual(output.status_code, 200)
  2219. self.assertTrue(
  2220. '<div class="list-group" id="diff_list" style="display:none;">'
  2221. in output.data)
  2222. self.assertTrue(' Authored by Alice Author\n' in output.data)
  2223. self.assertTrue(' Committed by Cecil Committer\n' in output.data)
  2224. self.assertTrue(
  2225. '<span style="background-color: #f0f0f0' in
  2226. output.data)
  2227. #View the commit when branch name is provided
  2228. output = self.app.get('/test/c/%s?branch=master' % commit.oid.hex)
  2229. self.assertEqual(output.status_code, 200)
  2230. self.assertTrue(
  2231. '<a class="active nav-link" href="/test/commits/master">'
  2232. in output.data)
  2233. #View the commit when branch name is wrong, show the commit
  2234. output = self.app.get('/test/c/%s?branch=abcxyz' % commit.oid.hex)
  2235. self.assertEqual(output.status_code, 200)
  2236. self.assertTrue(
  2237. '<a class="active nav-link" href="/test/commits">'
  2238. in output.data)
  2239. # Add a fork of a fork
  2240. item = pagure.lib.model.Project(
  2241. user_id=1, # pingou
  2242. name='test3',
  2243. description='test project #3',
  2244. is_fork=True,
  2245. parent_id=1,
  2246. hook_token='aaabbbkkk',
  2247. )
  2248. self.session.add(item)
  2249. self.session.commit()
  2250. forkedgit = os.path.join(
  2251. self.path, 'repos', 'forks', 'pingou', 'test3.git')
  2252. tests.add_content_git_repo(forkedgit)
  2253. tests.add_readme_git_repo(forkedgit)
  2254. repo = pygit2.Repository(forkedgit)
  2255. commit = repo.revparse_single('HEAD')
  2256. # Commit does not exist in anothe repo :)
  2257. output = self.app.get('/test/c/%s' % commit.oid.hex)
  2258. self.assertEqual(output.status_code, 404)
  2259. # View commit of fork
  2260. output = self.app.get(
  2261. '/fork/pingou/test3/c/%s' % commit.oid.hex)
  2262. self.assertEqual(output.status_code, 200)
  2263. self.assertTrue(
  2264. '<div class="list-group" id="diff_list" style="display:none;">'
  2265. in output.data)
  2266. self.assertTrue(' Authored by Alice Author\n' in output.data)
  2267. self.assertTrue(' Committed by Cecil Committer\n' in output.data)
  2268. self.assertTrue(
  2269. '<span style="background-color: #f0f0f0; padding: 0 5px 0 5px"> 2' +
  2270. ' </span><span style="color: #00A000; background-color: #ddffdd">' +
  2271. '+ Pagure</span>' in output.data)
  2272. self.assertTrue(
  2273. '<span style="background-color: #f0f0f0; padding: 0 5px 0 5px"> 3' +
  2274. ' </span><span style="color: #00A000; background-color: #ddffdd">' +
  2275. '+ ======</span>' in output.data)
  2276. # Try the old URL scheme with a short hash
  2277. output = self.app.get(
  2278. '/fork/pingou/test3/%s' % commit.oid.hex[:10],
  2279. follow_redirects=True)
  2280. self.assertEqual(output.status_code, 404)
  2281. self.assertIn('<p>Project not found</p>', output.data)
  2282. #View the commit of the fork when branch name is provided
  2283. output = self.app.get('/fork/pingou/test3/c/%s?branch=master' % commit.oid.hex)
  2284. self.assertEqual(output.status_code, 200)
  2285. self.assertTrue(
  2286. '<a class="active nav-link" href="/fork/pingou/test3/commits/master">'
  2287. in output.data)
  2288. #View the commit of the fork when branch name is wrong
  2289. output = self.app.get('/fork/pingou/test3/c/%s?branch=abcxyz' % commit.oid.hex)
  2290. self.assertEqual(output.status_code, 200)
  2291. self.assertTrue(
  2292. '<a class="active nav-link" href="/fork/pingou/test3/commits">'
  2293. in output.data)
  2294. def test_view_commit_patch(self):
  2295. """ Test the view_commit_patch endpoint. """
  2296. # No project registered in the DB
  2297. output = self.app.get('/foo/c/bar.patch')
  2298. self.assertEqual(output.status_code, 404)
  2299. tests.create_projects(self.session)
  2300. output = self.app.get('/test/c/bar.patch')
  2301. # No git repo associated
  2302. self.assertEqual(output.status_code, 404)
  2303. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  2304. output = self.app.get('/test/c/bar.patch')
  2305. self.assertEqual(output.status_code, 404)
  2306. # Add a README to the git repo - First commit
  2307. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  2308. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  2309. commit = repo.revparse_single('HEAD')
  2310. # View first commit
  2311. output = self.app.get('/test/c/%s.patch' % commit.oid.hex)
  2312. self.assertEqual(output.status_code, 200)
  2313. self.assertTrue('''diff --git a/README.rst b/README.rst
  2314. new file mode 100644
  2315. index 0000000..fb7093d
  2316. --- /dev/null
  2317. +++ b/README.rst
  2318. @@ -0,0 +1,16 @@
  2319. +Pagure
  2320. +======
  2321. +
  2322. +:Author: Pierre-Yves Chibon <pingou@pingoured.fr>
  2323. +
  2324. +
  2325. +Pagure is a light-weight git-centered forge based on pygit2.
  2326. +
  2327. +Currently, Pagure offers a web-interface for git repositories, a ticket
  2328. +system and possibilities to create new projects, fork existing ones and
  2329. +create/merge pull-requests across or within projects.
  2330. +
  2331. +
  2332. +Homepage: https://github.com/pypingou/pagure
  2333. +
  2334. +Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-))
  2335. ''' in output.data)
  2336. self.assertTrue('Subject: Add a README file' in output.data)
  2337. # Add some content to the git repo
  2338. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  2339. 'test.git'))
  2340. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  2341. commit = repo.revparse_single('HEAD')
  2342. # View another commit
  2343. output = self.app.get('/test/c/%s.patch' % commit.oid.hex)
  2344. self.assertEqual(output.status_code, 200)
  2345. self.assertTrue(
  2346. 'Subject: Add some directory and a file for more testing'
  2347. in output.data)
  2348. self.assertTrue('''diff --git a/folder1/folder2/file b/folder1/folder2/file
  2349. new file mode 100644
  2350. index 0000000..11980b1
  2351. --- /dev/null
  2352. +++ b/folder1/folder2/file
  2353. @@ -0,0 +1,3 @@
  2354. +foo
  2355. + bar
  2356. +baz
  2357. \ No newline at end of file
  2358. ''' in output.data)
  2359. # Add a fork of a fork
  2360. item = pagure.lib.model.Project(
  2361. user_id=1, # pingou
  2362. name='test3',
  2363. description='test project #3',
  2364. is_fork=True,
  2365. parent_id=1,
  2366. hook_token='aaabbblll',
  2367. )
  2368. self.session.add(item)
  2369. self.session.commit()
  2370. forkedgit = os.path.join(self.path, 'repos', 'forks', 'pingou',
  2371. 'test3.git')
  2372. tests.add_content_git_repo(forkedgit)
  2373. tests.add_readme_git_repo(forkedgit)
  2374. repo = pygit2.Repository(forkedgit)
  2375. commit = repo.revparse_single('HEAD')
  2376. # Commit does not exist in anothe repo :)
  2377. output = self.app.get('/test/c/%s.patch' % commit.oid.hex)
  2378. self.assertEqual(output.status_code, 404)
  2379. # View commit of fork
  2380. output = self.app.get(
  2381. '/fork/pingou/test3/c/%s.patch' % commit.oid.hex)
  2382. self.assertEqual(output.status_code, 200)
  2383. self.assertTrue('''diff --git a/README.rst b/README.rst
  2384. new file mode 100644
  2385. index 0000000..fb7093d
  2386. --- /dev/null
  2387. +++ b/README.rst
  2388. @@ -0,0 +1,16 @@
  2389. +Pagure
  2390. +======
  2391. +
  2392. +:Author: Pierre-Yves Chibon <pingou@pingoured.fr>
  2393. +
  2394. +
  2395. +Pagure is a light-weight git-centered forge based on pygit2.
  2396. +
  2397. +Currently, Pagure offers a web-interface for git repositories, a ticket
  2398. +system and possibilities to create new projects, fork existing ones and
  2399. +create/merge pull-requests across or within projects.
  2400. +
  2401. +
  2402. +Homepage: https://github.com/pypingou/pagure
  2403. +
  2404. +Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-))
  2405. ''' in output.data)
  2406. def test_view_commit_diff(self):
  2407. """ Test the view_commit_diff endpoint. """
  2408. # No project registered in the DB
  2409. output = self.app.get('/foo/c/bar.diff')
  2410. self.assertEqual(output.status_code, 404)
  2411. tests.create_projects(self.session)
  2412. output = self.app.get('/test/c/bar.diff')
  2413. # No git repo associated
  2414. self.assertEqual(output.status_code, 404)
  2415. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  2416. output = self.app.get('/test/c/bar.diff')
  2417. self.assertEqual(output.status_code, 404)
  2418. # Add a README to the git repo - First commit
  2419. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  2420. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  2421. commit = repo.revparse_single('HEAD')
  2422. # View first commit
  2423. output = self.app.get('/test/c/%s.diff' % commit.oid.hex)
  2424. self.assertEqual(output.status_code, 200)
  2425. self.assertEqual('''diff --git a/README.rst b/README.rst
  2426. new file mode 100644
  2427. index 0000000..fb7093d
  2428. --- /dev/null
  2429. +++ b/README.rst
  2430. @@ -0,0 +1,16 @@
  2431. +Pagure
  2432. +======
  2433. +
  2434. +:Author: Pierre-Yves Chibon <pingou@pingoured.fr>
  2435. +
  2436. +
  2437. +Pagure is a light-weight git-centered forge based on pygit2.
  2438. +
  2439. +Currently, Pagure offers a web-interface for git repositories, a ticket
  2440. +system and possibilities to create new projects, fork existing ones and
  2441. +create/merge pull-requests across or within projects.
  2442. +
  2443. +
  2444. +Homepage: https://github.com/pypingou/pagure
  2445. +
  2446. +Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-))
  2447. ''', output.data)
  2448. def test_view_tree(self):
  2449. """ Test the view_tree endpoint. """
  2450. output = self.app.get('/foo/tree/')
  2451. # No project registered in the DB
  2452. self.assertEqual(output.status_code, 404)
  2453. tests.create_projects(self.session)
  2454. output = self.app.get('/test/tree/')
  2455. # No git repo associated
  2456. self.assertEqual(output.status_code, 404)
  2457. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  2458. output = self.app.get('/test/tree/')
  2459. self.assertEqual(output.status_code, 200)
  2460. self.assertIn(
  2461. '''
  2462. <ol class="breadcrumb">
  2463. <li>
  2464. <a href="/test/tree">
  2465. <span class="oi" data-glyph="random">
  2466. </span>&nbsp; None
  2467. </a>
  2468. </li>
  2469. </ol>''', output.data)
  2470. self.assertTrue(
  2471. 'No content found in this repository' in output.data)
  2472. # Add a README to the git repo - First commit
  2473. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  2474. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  2475. commit = repo.revparse_single('HEAD')
  2476. # View first commit
  2477. output = self.app.get('/test/tree/%s' % commit.oid.hex)
  2478. self.assertEqual(output.status_code, 200)
  2479. self.assertIn(
  2480. '<div class="projectinfo m-t-1 m-b-1">\n'
  2481. 'test project #1 </div>', output.data)
  2482. self.assertIn('<title>Tree - test - Pagure</title>', output.data)
  2483. self.assertTrue('README.rst' in output.data)
  2484. self.assertFalse(
  2485. 'No content found in this repository' in output.data)
  2486. # View tree by branch
  2487. output = self.app.get('/test/tree/master')
  2488. self.assertEqual(output.status_code, 200)
  2489. self.assertIn(
  2490. '<div class="projectinfo m-t-1 m-b-1">\n'
  2491. 'test project #1 </div>', output.data)
  2492. self.assertIn('<title>Tree - test - Pagure</title>', output.data)
  2493. self.assertTrue('README.rst' in output.data)
  2494. self.assertFalse(
  2495. 'No content found in this repository' in output.data)
  2496. # Add a fork of a fork
  2497. item = pagure.lib.model.Project(
  2498. user_id=1, # pingou
  2499. name='test3',
  2500. description='test project #3',
  2501. is_fork=True,
  2502. parent_id=1,
  2503. hook_token='aaabbbfff',
  2504. )
  2505. self.session.add(item)
  2506. self.session.commit()
  2507. forkedgit = os.path.join(self.path, 'repos', 'forks', 'pingou',
  2508. 'test3.git')
  2509. tests.add_content_git_repo(forkedgit)
  2510. output = self.app.get('/fork/pingou/test3/tree/')
  2511. self.assertEqual(output.status_code, 200)
  2512. self.assertIn(
  2513. '<div class="projectinfo m-t-1 m-b-1">\n'
  2514. 'test project #3 </div>', output.data)
  2515. self.assertIn('<title>Tree - test3 - Pagure</title>', output.data)
  2516. self.assertTrue(
  2517. '<a href="/fork/pingou/test3/blob/master/f/folder1">'
  2518. in output.data)
  2519. self.assertTrue(
  2520. '<a href="/fork/pingou/test3/blob/master/f/sources">'
  2521. in output.data)
  2522. self.assertFalse(
  2523. 'No content found in this repository' in output.data)
  2524. output = self.app.get(
  2525. '/fork/pingou/test3/blob/master/f/folder1/folder2')
  2526. self.assertEqual(output.status_code, 200)
  2527. self.assertTrue(
  2528. '<a href="/fork/pingou/test3/blob/master/'
  2529. 'f/folder1/folder2/file%C5%A0">' in output.data)
  2530. @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
  2531. @patch('pagure.lib.notify.send_email')
  2532. @patch('pagure.decorators.admin_session_timedout')
  2533. def test_delete_repo_when_turned_off(self, ast, send_email):
  2534. """ Test the delete_repo endpoint when deletion of a repo is
  2535. turned off in the pagure instance """
  2536. ast.return_value = False
  2537. send_email.return_value = True
  2538. # No Git repo
  2539. output = self.app.post('/foo/delete')
  2540. self.assertEqual(output.status_code, 404)
  2541. user = tests.FakeUser(username='pingou')
  2542. with tests.user_set(self.app.application, user):
  2543. tests.create_projects(self.session)
  2544. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2545. output = self.app.post('/test/delete', follow_redirects=True)
  2546. self.assertEqual(output.status_code, 404)
  2547. # User not logged in
  2548. output = self.app.post('/test/delete')
  2549. self.assertEqual(output.status_code, 302)
  2550. # Ensure the project isn't read-only
  2551. repo = pagure.lib.get_authorized_project(self.session, 'test')
  2552. repo.read_only = False
  2553. self.session.add(repo)
  2554. self.session.commit()
  2555. with tests.user_set(self.app.application, user):
  2556. # Only git repo
  2557. item = pagure.lib.model.Project(
  2558. user_id=1, # pingou
  2559. name='test',
  2560. description='test project #1',
  2561. hook_token='aaabbbggg',
  2562. )
  2563. self.session.add(item)
  2564. self.session.commit()
  2565. output = self.app.post('/test/delete', follow_redirects=True)
  2566. self.assertEqual(output.status_code, 404)
  2567. # Only git and doc repo
  2568. item = pagure.lib.model.Project(
  2569. user_id=1, # pingou
  2570. name='test',
  2571. description='test project #1',
  2572. hook_token='aaabbbhhh',
  2573. )
  2574. self.session.add(item)
  2575. self.session.commit()
  2576. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2577. tests.create_projects_git(os.path.join(self.path, 'docs'))
  2578. output = self.app.post('/test/delete', follow_redirects=True)
  2579. self.assertEqual(output.status_code, 404)
  2580. # All repo there
  2581. item = pagure.lib.model.Project(
  2582. user_id=1, # pingou
  2583. name='test',
  2584. description='test project #1',
  2585. hook_token='aaabbbiii',
  2586. )
  2587. self.session.add(item)
  2588. self.session.commit()
  2589. # Create all the git repos
  2590. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2591. tests.create_projects_git(os.path.join(self.path, 'docs'))
  2592. tests.create_projects_git(
  2593. os.path.join(self.path, 'tickets'), bare=True)
  2594. tests.create_projects_git(
  2595. os.path.join(self.path, 'requests'), bare=True)
  2596. # Check repo was created
  2597. output = self.app.get('/')
  2598. self.assertEqual(output.status_code, 200)
  2599. self.assertIn(
  2600. '<div class="card-header">\n My Projects <span '
  2601. 'class="label label-default">6</span>', output.data)
  2602. self.assertIn(
  2603. 'Forks <span class="label label-default">0</span>',
  2604. output.data)
  2605. # add issues
  2606. repo = pagure.lib.get_authorized_project(self.session, 'test')
  2607. msg = pagure.lib.new_issue(
  2608. session=self.session,
  2609. repo=repo,
  2610. title='Test issue',
  2611. content='We should work on this',
  2612. user='pingou',
  2613. ticketfolder=os.path.join(self.path, 'tickets')
  2614. )
  2615. self.session.commit()
  2616. self.assertEqual(msg.title, 'Test issue')
  2617. msg = pagure.lib.new_issue(
  2618. session=self.session,
  2619. repo=repo,
  2620. title='Test issue #2',
  2621. content='We should work on this, really',
  2622. user='pingou',
  2623. ticketfolder=os.path.join(self.path, 'tickets')
  2624. )
  2625. self.session.commit()
  2626. self.assertEqual(msg.title, 'Test issue #2')
  2627. # Add a comment to an issue
  2628. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  2629. msg = pagure.lib.add_issue_comment(
  2630. session=self.session,
  2631. issue=issue,
  2632. comment='Hey look a comment!',
  2633. user='foo',
  2634. ticketfolder=None
  2635. )
  2636. self.session.commit()
  2637. self.assertEqual(msg, 'Comment added')
  2638. # add pull-requests
  2639. req = pagure.lib.new_pull_request(
  2640. session=self.session,
  2641. repo_from=repo,
  2642. branch_from='feature',
  2643. repo_to=repo,
  2644. branch_to='master',
  2645. title='test pull-request',
  2646. user='pingou',
  2647. requestfolder=os.path.join(self.path, 'requests'),
  2648. )
  2649. self.session.commit()
  2650. self.assertEqual(req.id, 3)
  2651. self.assertEqual(req.title, 'test pull-request')
  2652. req = pagure.lib.new_pull_request(
  2653. session=self.session,
  2654. repo_from=repo,
  2655. branch_from='feature2',
  2656. repo_to=repo,
  2657. branch_to='master',
  2658. title='test pull-request',
  2659. user='pingou',
  2660. requestfolder=os.path.join(self.path, 'requests'),
  2661. )
  2662. self.session.commit()
  2663. self.assertEqual(req.id, 4)
  2664. self.assertEqual(req.title, 'test pull-request')
  2665. # Add comment on a pull-request
  2666. request = pagure.lib.search_pull_requests(
  2667. self.session, requestid=3)
  2668. msg = pagure.lib.add_pull_request_comment(
  2669. session=self.session,
  2670. request=request,
  2671. commit='commithash',
  2672. tree_id=None,
  2673. filename='file',
  2674. row=None,
  2675. comment='This is awesome, I got to remember it!',
  2676. user='foo',
  2677. requestfolder=None,
  2678. )
  2679. self.assertEqual(msg, 'Comment added')
  2680. # Check before deleting the project
  2681. output = self.app.get('/')
  2682. self.assertEqual(output.status_code, 200)
  2683. self.assertIn(
  2684. '<div class="card-header">\n My Projects <span '
  2685. 'class="label label-default">6</span>', output.data)
  2686. self.assertIn(
  2687. 'Forks <span class="label label-default">0</span>',
  2688. output.data)
  2689. output = self.app.post('/test/delete', follow_redirects=True)
  2690. self.assertEqual(output.status_code, 404)
  2691. repo = pagure.lib.get_authorized_project(self.session, 'test')
  2692. self.assertNotEqual(repo, None)
  2693. repo = pagure.lib.get_authorized_project(self.session, 'test2')
  2694. self.assertNotEqual(repo, None)
  2695. # Add a fork of a fork
  2696. item = pagure.lib.model.Project(
  2697. user_id=1, # pingou
  2698. name='test3',
  2699. description='test project #3',
  2700. is_fork=True,
  2701. parent_id=2,
  2702. hook_token='aaabbbjjj',
  2703. )
  2704. self.session.add(item)
  2705. self.session.commit()
  2706. tests.add_content_git_repo(
  2707. os.path.join(self.path, 'repos', 'forks', 'pingou',
  2708. 'test3.git'))
  2709. tests.add_content_git_repo(
  2710. os.path.join(self.path, 'docs', 'pingou', 'test3.git'))
  2711. tests.add_content_git_repo(
  2712. os.path.join(self.path, 'tickets', 'pingou', 'test3.git'))
  2713. # Check before deleting the fork
  2714. output = self.app.get('/')
  2715. self.assertEqual(output.status_code, 200)
  2716. self.assertIn(
  2717. '<div class="card-header">\n My Projects <span '
  2718. 'class="label label-default">6</span>', output.data)
  2719. self.assertIn(
  2720. 'Forks <span class="label label-default">1</span>',
  2721. output.data)
  2722. output = self.app.post(
  2723. '/fork/pingou/test3/delete', follow_redirects=True)
  2724. self.assertEqual(output.status_code, 404)
  2725. @patch('pagure.lib.notify.send_email')
  2726. @patch('pagure.decorators.admin_session_timedout')
  2727. def test_delete_read_only_repo(self, ast, send_email):
  2728. """ Test the delete_repo endpoint when the repo is read_only """
  2729. ast.return_value = False
  2730. send_email.return_value = True
  2731. tests.create_projects(self.session)
  2732. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2733. # All repo there
  2734. item = pagure.lib.model.Project(
  2735. user_id=1, # pingou
  2736. name='test',
  2737. description='test project #1',
  2738. hook_token='aaabbbiii',
  2739. )
  2740. self.session.add(item)
  2741. self.session.commit()
  2742. # Create all the git repos
  2743. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2744. tests.create_projects_git(os.path.join(self.path, 'docs'))
  2745. tests.create_projects_git(
  2746. os.path.join(self.path, 'tickets'), bare=True)
  2747. tests.create_projects_git(
  2748. os.path.join(self.path, 'requests'), bare=True)
  2749. user = tests.FakeUser(username='pingou')
  2750. with tests.user_set(self.app.application, user):
  2751. repo = pagure.lib.get_authorized_project(self.session, 'test')
  2752. self.assertNotEqual(repo, None)
  2753. repo.read_only = True
  2754. self.session.add(repo)
  2755. self.session.commit()
  2756. output = self.app.post('/test/delete', follow_redirects=True)
  2757. self.assertEqual(output.status_code, 200)
  2758. self.assertIn(
  2759. u'<title>Settings - test - Pagure</title>', output.data)
  2760. self.assertIn(
  2761. u'The ACLs of this project are being refreshed in the '
  2762. u'backend this prevents the project from being deleted. '
  2763. u'Please wait for this task to finish before trying again. '
  2764. u'Thanks!', output.data)
  2765. self.assertIn(
  2766. u'title="Action disabled while project\'s ACLs are being refreshed">',
  2767. output.data)
  2768. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  2769. @patch('pagure.decorators.admin_session_timedout')
  2770. def test_delete_repo(self, ast):
  2771. """ Test the delete_repo endpoint. """
  2772. ast.return_value = False
  2773. # No Git repo
  2774. output = self.app.post('/foo/delete')
  2775. self.assertEqual(output.status_code, 404)
  2776. user = tests.FakeUser()
  2777. with tests.user_set(self.app.application, user):
  2778. tests.create_projects(self.session)
  2779. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2780. # No project registered in the DB (no git repo)
  2781. output = self.app.post('/foo/delete')
  2782. self.assertEqual(output.status_code, 404)
  2783. # User not allowed
  2784. output = self.app.post('/test/delete')
  2785. self.assertEqual(output.status_code, 403)
  2786. # User not logged in
  2787. output = self.app.post('/test/delete')
  2788. self.assertEqual(output.status_code, 302)
  2789. # Ensure the project isn't read-only
  2790. repo = pagure.lib.get_authorized_project(self.session, 'test')
  2791. repo.read_only = False
  2792. self.session.add(repo)
  2793. self.session.commit()
  2794. user = tests.FakeUser(username='pingou')
  2795. with tests.user_set(self.app.application, user):
  2796. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2797. ast.return_value = True
  2798. output = self.app.post('/test/delete')
  2799. self.assertEqual(output.status_code, 302)
  2800. ast.return_value = False
  2801. output = self.app.post('/test/delete', follow_redirects=True)
  2802. self.assertEqual(output.status_code, 200)
  2803. self.assertIn(
  2804. '<div class="card-header">\n Projects <span '
  2805. 'class="label label-default">2</span>', output.data)
  2806. self.assertIn(
  2807. 'Forks <span class="label label-default">0</span>',
  2808. output.data)
  2809. # Only git repo
  2810. item = pagure.lib.model.Project(
  2811. user_id=1, # pingou
  2812. name='test',
  2813. description='test project #1',
  2814. hook_token='aaabbbggg',
  2815. read_only=False,
  2816. )
  2817. self.session.add(item)
  2818. self.session.commit()
  2819. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2820. output = self.app.post('/test/delete', follow_redirects=True)
  2821. self.assertEqual(output.status_code, 200)
  2822. self.assertIn(
  2823. '<div class="card-header">\n Projects <span '
  2824. 'class="label label-default">2</span>', output.data)
  2825. self.assertIn(
  2826. 'Forks <span class="label label-default">0</span>',
  2827. output.data)
  2828. # Only git and doc repo
  2829. item = pagure.lib.model.Project(
  2830. user_id=1, # pingou
  2831. name='test',
  2832. description='test project #1',
  2833. hook_token='aaabbbhhh',
  2834. read_only=False,
  2835. )
  2836. self.session.add(item)
  2837. self.session.commit()
  2838. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2839. tests.create_projects_git(os.path.join(self.path, 'docs'))
  2840. output = self.app.post('/test/delete', follow_redirects=True)
  2841. self.assertEqual(output.status_code, 200)
  2842. self.assertIn(
  2843. '<div class="card-header">\n Projects <span '
  2844. 'class="label label-default">2</span>', output.data)
  2845. self.assertIn(
  2846. 'Forks <span class="label label-default">0</span>',
  2847. output.data)
  2848. # All repo there
  2849. item = pagure.lib.model.Project(
  2850. user_id=1, # pingou
  2851. name='test',
  2852. description='test project #1',
  2853. hook_token='aaabbbiii',
  2854. read_only=False,
  2855. )
  2856. self.session.add(item)
  2857. self.session.commit()
  2858. # Create all the git repos
  2859. tests.create_projects_git(os.path.join(self.path, 'repos'))
  2860. tests.create_projects_git(os.path.join(self.path, 'docs'))
  2861. tests.create_projects_git(
  2862. os.path.join(self.path, 'tickets'), bare=True)
  2863. tests.create_projects_git(
  2864. os.path.join(self.path, 'requests'), bare=True)
  2865. # Check repo was created
  2866. output = self.app.get('/')
  2867. self.assertEqual(output.status_code, 200)
  2868. self.assertIn(
  2869. '<div class="card-header">\n My Projects <span '
  2870. 'class="label label-default">3</span>', output.data)
  2871. self.assertIn(
  2872. 'Forks <span class="label label-default">0</span>',
  2873. output.data)
  2874. # add issues
  2875. repo = pagure.lib.get_authorized_project(self.session, 'test')
  2876. msg = pagure.lib.new_issue(
  2877. session=self.session,
  2878. repo=repo,
  2879. title='Test issue',
  2880. content='We should work on this',
  2881. user='pingou',
  2882. ticketfolder=os.path.join(self.path, 'tickets')
  2883. )
  2884. self.session.commit()
  2885. self.assertEqual(msg.title, 'Test issue')
  2886. msg = pagure.lib.new_issue(
  2887. session=self.session,
  2888. repo=repo,
  2889. title='Test issue #2',
  2890. content='We should work on this, really',
  2891. user='pingou',
  2892. ticketfolder=os.path.join(self.path, 'tickets')
  2893. )
  2894. self.session.commit()
  2895. self.assertEqual(msg.title, 'Test issue #2')
  2896. # Add a comment to an issue
  2897. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  2898. msg = pagure.lib.add_issue_comment(
  2899. session=self.session,
  2900. issue=issue,
  2901. comment='Hey look a comment!',
  2902. user='foo',
  2903. ticketfolder=None
  2904. )
  2905. self.session.commit()
  2906. self.assertEqual(msg, 'Comment added')
  2907. # add pull-requests
  2908. req = pagure.lib.new_pull_request(
  2909. session=self.session,
  2910. repo_from=repo,
  2911. branch_from='feature',
  2912. repo_to=repo,
  2913. branch_to='master',
  2914. title='test pull-request',
  2915. user='pingou',
  2916. requestfolder=os.path.join(self.path, 'requests'),
  2917. )
  2918. self.session.commit()
  2919. self.assertEqual(req.id, 3)
  2920. self.assertEqual(req.title, 'test pull-request')
  2921. req = pagure.lib.new_pull_request(
  2922. session=self.session,
  2923. repo_from=repo,
  2924. branch_from='feature2',
  2925. repo_to=repo,
  2926. branch_to='master',
  2927. title='test pull-request',
  2928. user='pingou',
  2929. requestfolder=os.path.join(self.path, 'requests'),
  2930. )
  2931. self.session.commit()
  2932. self.assertEqual(req.id, 4)
  2933. self.assertEqual(req.title, 'test pull-request')
  2934. # Add comment on a pull-request
  2935. request = pagure.lib.search_pull_requests(
  2936. self.session, requestid=3)
  2937. msg = pagure.lib.add_pull_request_comment(
  2938. session=self.session,
  2939. request=request,
  2940. commit='commithash',
  2941. tree_id=None,
  2942. filename='file',
  2943. row=None,
  2944. comment='This is awesome, I got to remember it!',
  2945. user='foo',
  2946. requestfolder=None,
  2947. )
  2948. self.assertEqual(msg, 'Comment added')
  2949. # Check before deleting the project
  2950. output = self.app.get('/')
  2951. self.assertEqual(output.status_code, 200)
  2952. self.assertIn(
  2953. '<div class="card-header">\n My Projects <span '
  2954. 'class="label label-default">3</span>', output.data)
  2955. self.assertIn(
  2956. 'Forks <span class="label label-default">0</span>',
  2957. output.data)
  2958. output = self.app.post('/test/delete', follow_redirects=True)
  2959. self.assertEqual(output.status_code, 200)
  2960. self.assertIn(
  2961. '<div class="card-header">\n Projects <span '
  2962. 'class="label label-default">2</span>', output.data)
  2963. self.assertIn(
  2964. 'Forks <span class="label label-default">0</span>',
  2965. output.data)
  2966. repo = pagure.lib.get_authorized_project(self.session, 'test')
  2967. self.assertEqual(repo, None)
  2968. repo = pagure.lib.get_authorized_project(self.session, 'test2')
  2969. self.assertNotEqual(repo, None)
  2970. # Add a fork of a fork
  2971. item = pagure.lib.model.Project(
  2972. user_id=1, # pingou
  2973. name='test3',
  2974. description='test project #3',
  2975. is_fork=True,
  2976. parent_id=2,
  2977. hook_token='aaabbbjjj',
  2978. read_only=False,
  2979. )
  2980. self.session.add(item)
  2981. self.session.commit()
  2982. tests.add_content_git_repo(
  2983. os.path.join(self.path, 'repos', 'forks', 'pingou',
  2984. 'test3.git'))
  2985. tests.add_content_git_repo(
  2986. os.path.join(self.path, 'docs', 'pingou', 'test3.git'))
  2987. tests.add_content_git_repo(
  2988. os.path.join(self.path, 'tickets', 'pingou', 'test3.git'))
  2989. # Check before deleting the fork
  2990. output = self.app.get('/')
  2991. self.assertEqual(output.status_code, 200)
  2992. self.assertIn(
  2993. '<div class="card-header">\n My Projects <span '
  2994. 'class="label label-default">2</span>', output.data)
  2995. self.assertIn(
  2996. 'Forks <span class="label label-default">1</span>',
  2997. output.data)
  2998. output = self.app.post(
  2999. '/fork/pingou/test3/delete', follow_redirects=True)
  3000. self.assertEqual(output.status_code, 200)
  3001. self.assertIn(
  3002. '<div class="card-header">\n Projects <span '
  3003. 'class="label label-default">2</span>', output.data)
  3004. self.assertIn(
  3005. 'Forks <span class="label label-default">0</span>',
  3006. output.data)
  3007. @patch.dict('pagure.config.config', {'TICKETS_FOLDER': None})
  3008. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  3009. @patch('pagure.decorators.admin_session_timedout', MagicMock(return_value=False))
  3010. def test_delete_repo_no_ticket(self):
  3011. """ Test the delete_repo endpoint when tickets aren't enabled in
  3012. this pagure instance. """
  3013. tests.create_projects(self.session)
  3014. tests.create_projects_git(os.path.join(self.path, 'repos'))
  3015. # Ensure the project isn't read-only
  3016. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3017. repo.read_only = False
  3018. self.session.add(repo)
  3019. self.session.commit()
  3020. user = tests.FakeUser(username='pingou')
  3021. with tests.user_set(self.app.application, user):
  3022. # Check before deleting the project
  3023. output = self.app.get('/')
  3024. self.assertEqual(output.status_code, 200)
  3025. self.assertIn(
  3026. '<div class="card-header">\n My Projects <span '
  3027. 'class="label label-default">3</span>', output.data)
  3028. self.assertIn(
  3029. 'Forks <span class="label label-default">0</span>',
  3030. output.data)
  3031. # Delete the project
  3032. output = self.app.post('/test/delete', follow_redirects=True)
  3033. self.assertEqual(output.status_code, 200)
  3034. # Check deletion worked
  3035. self.assertIn(
  3036. '<div class="card-header">\n Projects <span '
  3037. 'class="label label-default">2</span>', output.data)
  3038. self.assertIn(
  3039. 'Forks <span class="label label-default">0</span>',
  3040. output.data)
  3041. @patch('pagure.lib.notify.send_email')
  3042. @patch('pagure.decorators.admin_session_timedout')
  3043. def test_delete_repo_with_users(self, ast, send_email):
  3044. """ Test the delete_repo endpoint. """
  3045. ast.return_value = False
  3046. send_email.return_value = True
  3047. user = tests.FakeUser()
  3048. user = tests.FakeUser(username='pingou')
  3049. with tests.user_set(self.app.application, user):
  3050. # Create new project
  3051. item = pagure.lib.model.Project(
  3052. user_id=1, # pingou
  3053. name='test',
  3054. description='test project #1',
  3055. hook_token='aaabbbiii',
  3056. read_only=False,
  3057. )
  3058. self.session.add(item)
  3059. self.session.commit()
  3060. # Create all the git repos
  3061. tests.create_projects_git(os.path.join(self.path, 'repos'))
  3062. tests.create_projects_git(
  3063. os.path.join(self.path, 'docs'), bare=True)
  3064. tests.create_projects_git(
  3065. os.path.join(self.path, 'tickets'), bare=True)
  3066. tests.create_projects_git(
  3067. os.path.join(self.path, 'requests'), bare=True)
  3068. # Check repo was created
  3069. output = self.app.get('/')
  3070. self.assertEqual(output.status_code, 200)
  3071. self.assertIn(
  3072. '<div class="card-header">\n My Projects <span '
  3073. 'class="label label-default">1</span>', output.data)
  3074. self.assertIn(
  3075. 'Forks <span class="label label-default">0</span>',
  3076. output.data)
  3077. # add user
  3078. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3079. msg = pagure.lib.add_user_to_project(
  3080. session=self.session,
  3081. project=repo,
  3082. new_user='foo',
  3083. user='pingou',
  3084. )
  3085. self.session.commit()
  3086. self.assertEqual(msg, 'User added')
  3087. # Ensure the project isn't read-only (because adding an user
  3088. # will trigger an ACL refresh, thus read-only)
  3089. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3090. repo.read_only = False
  3091. self.session.add(repo)
  3092. self.session.commit()
  3093. # Check before deleting the project
  3094. output = self.app.get('/')
  3095. self.assertEqual(output.status_code, 200)
  3096. self.assertIn(
  3097. '<div class="card-header">\n My Projects <span '
  3098. 'class="label label-default">1</span>', output.data)
  3099. self.assertIn(
  3100. 'Forks <span class="label label-default">0</span>',
  3101. output.data)
  3102. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3103. self.assertNotEqual(repo, None)
  3104. repo = pagure.lib.get_authorized_project(self.session, 'test2')
  3105. self.assertEqual(repo, None)
  3106. # Delete the project
  3107. output = self.app.post('/test/delete', follow_redirects=True)
  3108. self.assertEqual(output.status_code, 200)
  3109. self.assertIn(
  3110. '<div class="card-header">\n Projects <span '
  3111. 'class="label label-default">0</span>', output.data)
  3112. self.assertIn(
  3113. 'Forks <span class="label label-default">0</span>',
  3114. output.data)
  3115. # Check after
  3116. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3117. self.assertEqual(repo, None)
  3118. repo = pagure.lib.get_authorized_project(self.session, 'test2')
  3119. self.assertEqual(repo, None)
  3120. @patch('pagure.lib.notify.send_email')
  3121. @patch('pagure.decorators.admin_session_timedout')
  3122. def test_delete_repo_with_group(self, ast, send_email):
  3123. """ Test the delete_repo endpoint. """
  3124. ast.return_value = False
  3125. send_email.return_value = True
  3126. user = tests.FakeUser()
  3127. user = tests.FakeUser(username='pingou')
  3128. with tests.user_set(self.app.application, user):
  3129. # Create new project
  3130. item = pagure.lib.model.Project(
  3131. user_id=1, # pingou
  3132. name='test',
  3133. description='test project #1',
  3134. hook_token='aaabbbiii',
  3135. read_only=False,
  3136. )
  3137. self.session.add(item)
  3138. self.session.commit()
  3139. # Create all the git repos
  3140. tests.create_projects_git(os.path.join(self.path, 'repos'))
  3141. tests.create_projects_git(
  3142. os.path.join(self.path, 'docs'), bare=True)
  3143. tests.create_projects_git(
  3144. os.path.join(self.path, 'tickets'), bare=True)
  3145. tests.create_projects_git(
  3146. os.path.join(self.path, 'requests'), bare=True)
  3147. # Check repo was created
  3148. output = self.app.get('/')
  3149. self.assertEqual(output.status_code, 200)
  3150. self.assertIn(
  3151. '<div class="card-header">\n My Projects <span '
  3152. 'class="label label-default">1</span>', output.data)
  3153. self.assertIn(
  3154. 'Forks <span class="label label-default">0</span>',
  3155. output.data)
  3156. # Create group
  3157. msg = pagure.lib.add_group(
  3158. self.session,
  3159. group_name='foo',
  3160. display_name='foo group',
  3161. description=None,
  3162. group_type='bar',
  3163. user='pingou',
  3164. is_admin=False,
  3165. blacklist=[],
  3166. )
  3167. self.session.commit()
  3168. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  3169. # Add group to the project
  3170. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3171. msg = pagure.lib.add_group_to_project(
  3172. session=self.session,
  3173. project=repo,
  3174. new_group='foo',
  3175. user='pingou',
  3176. )
  3177. self.session.commit()
  3178. self.assertEqual(msg, 'Group added')
  3179. # Ensure the project isn't read-only (because adding a group
  3180. # will trigger an ACL refresh, thus read-only)
  3181. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3182. repo.read_only = False
  3183. self.session.add(repo)
  3184. self.session.commit()
  3185. # check if group where we expect it
  3186. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3187. self.assertEqual(len(repo.projects_groups), 1)
  3188. # Check before deleting the project
  3189. output = self.app.get('/')
  3190. self.assertEqual(output.status_code, 200)
  3191. self.assertIn(
  3192. '<div class="card-header">\n My Projects <span '
  3193. 'class="label label-default">1</span>', output.data)
  3194. self.assertIn(
  3195. 'Forks <span class="label label-default">0</span>',
  3196. output.data)
  3197. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3198. self.assertNotEqual(repo, None)
  3199. # Delete the project
  3200. output = self.app.post('/test/delete', follow_redirects=True)
  3201. self.assertEqual(output.status_code, 200)
  3202. self.assertIn(
  3203. '<div class="card-header">\n Projects <span '
  3204. 'class="label label-default">0</span>', output.data)
  3205. self.assertIn(
  3206. 'Forks <span class="label label-default">0</span>',
  3207. output.data)
  3208. # Check after
  3209. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3210. self.assertEqual(repo, None)
  3211. @patch('pagure.lib.notify.send_email')
  3212. @patch('pagure.decorators.admin_session_timedout')
  3213. def test_delete_repo_with_coloredtag(self, ast, send_email):
  3214. """ Test the delete_repo endpoint. """
  3215. ast.return_value = False
  3216. send_email.return_value = True
  3217. user = tests.FakeUser()
  3218. user = tests.FakeUser(username='pingou')
  3219. with tests.user_set(self.app.application, user):
  3220. # Create new project
  3221. item = pagure.lib.model.Project(
  3222. user_id=1, # pingou
  3223. name='test',
  3224. description='test project #1',
  3225. hook_token='aaabbbiii',
  3226. read_only=False,
  3227. )
  3228. self.session.add(item)
  3229. self.session.commit()
  3230. # Create all the git repos
  3231. tests.create_projects_git(os.path.join(self.path, 'repos'))
  3232. tests.create_projects_git(
  3233. os.path.join(self.path, 'docs'), bare=True)
  3234. tests.create_projects_git(
  3235. os.path.join(self.path, 'tickets'), bare=True)
  3236. tests.create_projects_git(
  3237. os.path.join(self.path, 'requests'), bare=True)
  3238. # Check repo was created
  3239. output = self.app.get('/')
  3240. self.assertEqual(output.status_code, 200)
  3241. self.assertIn(
  3242. '<div class="card-header">\n My Projects <span '
  3243. 'class="label label-default">1</span>', output.data)
  3244. self.assertIn(
  3245. 'Forks <span class="label label-default">0</span>',
  3246. output.data)
  3247. # Create the issue
  3248. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3249. msg = pagure.lib.new_issue(
  3250. session=self.session,
  3251. repo=repo,
  3252. title='Test issue',
  3253. content='We should work on this',
  3254. user='pingou',
  3255. ticketfolder=os.path.join(self.path, 'tickets')
  3256. )
  3257. self.session.commit()
  3258. self.assertEqual(msg.title, 'Test issue')
  3259. # Add a tag to the issue
  3260. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3261. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  3262. msg = pagure.lib.add_tag_obj(
  3263. session=self.session,
  3264. obj=issue,
  3265. tags='tag1',
  3266. user='pingou',
  3267. gitfolder=None)
  3268. self.session.commit()
  3269. self.assertEqual(msg, 'Issue tagged with: tag1')
  3270. # Check before deleting the project
  3271. output = self.app.get('/')
  3272. self.assertEqual(output.status_code, 200)
  3273. self.assertIn(
  3274. '<div class="card-header">\n My Projects <span '
  3275. 'class="label label-default">1</span>', output.data)
  3276. self.assertIn(
  3277. 'Forks <span class="label label-default">0</span>',
  3278. output.data)
  3279. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3280. self.assertNotEqual(repo, None)
  3281. repo = pagure.lib.get_authorized_project(self.session, 'test2')
  3282. self.assertEqual(repo, None)
  3283. # Delete the project
  3284. output = self.app.post('/test/delete', follow_redirects=True)
  3285. self.assertEqual(output.status_code, 200)
  3286. self.assertIn(
  3287. '<div class="card-header">\n Projects <span '
  3288. 'class="label label-default">0</span>', output.data)
  3289. self.assertIn(
  3290. 'Forks <span class="label label-default">0</span>',
  3291. output.data)
  3292. # Check after
  3293. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3294. self.assertEqual(repo, None)
  3295. repo = pagure.lib.get_authorized_project(self.session, 'test2')
  3296. self.assertEqual(repo, None)
  3297. @patch('pagure.decorators.admin_session_timedout')
  3298. def test_new_repo_hook_token(self, ast):
  3299. """ Test the new_repo_hook_token endpoint. """
  3300. ast.return_value = False
  3301. tests.create_projects(self.session)
  3302. tests.create_projects_git(os.path.join(self.path, 'repos'))
  3303. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3304. self.assertEqual(repo.hook_token, 'aaabbbccc')
  3305. user = tests.FakeUser()
  3306. with tests.user_set(self.app.application, user):
  3307. pagure.config.config['WEBHOOK'] = True
  3308. output = self.app.get('/new/')
  3309. self.assertEqual(output.status_code, 200)
  3310. self.assertIn('<strong>Create new Project</strong>', output.data)
  3311. csrf_token = output.data.split(
  3312. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  3313. output = self.app.post('/foo/hook_token')
  3314. self.assertEqual(output.status_code, 404)
  3315. output = self.app.post('/test/hook_token')
  3316. self.assertEqual(output.status_code, 403)
  3317. ast.return_value = True
  3318. output = self.app.post('/test/hook_token')
  3319. self.assertEqual(output.status_code, 302)
  3320. ast.return_value = False
  3321. pagure.config.config['WEBHOOK'] = False
  3322. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3323. self.assertEqual(repo.hook_token, 'aaabbbccc')
  3324. user.username = 'pingou'
  3325. with tests.user_set(self.app.application, user):
  3326. pagure.config.config['WEBHOOK'] = True
  3327. output = self.app.post('/test/hook_token')
  3328. self.assertEqual(output.status_code, 400)
  3329. data = {'csrf_token': csrf_token}
  3330. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3331. self.assertEqual(repo.hook_token, 'aaabbbccc')
  3332. output = self.app.post(
  3333. '/test/hook_token', data=data, follow_redirects=True)
  3334. self.assertEqual(output.status_code, 200)
  3335. self.assertIn(
  3336. '</button>\n New hook token generated',
  3337. output.data)
  3338. pagure.config.config['WEBHOOK'] = False
  3339. self.session.commit()
  3340. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3341. self.assertNotEqual(repo.hook_token, 'aaabbbccc')
  3342. @patch('pagure.lib.notify.send_email')
  3343. @patch('pagure.decorators.admin_session_timedout')
  3344. @patch('pagure.lib.git.update_git')
  3345. def test_regenerate_git(self, upgit, ast, sendmail):
  3346. """ Test the regenerate_git endpoint. """
  3347. ast.return_value = False
  3348. upgit.return_value = True
  3349. sendmail.return_value = True
  3350. tests.create_projects(self.session)
  3351. tests.create_projects_git(os.path.join(self.path, 'repos'))
  3352. user = tests.FakeUser()
  3353. with tests.user_set(self.app.application, user):
  3354. output = self.app.get('/new/')
  3355. self.assertEqual(output.status_code, 200)
  3356. self.assertIn('<strong>Create new Project</strong>', output.data)
  3357. csrf_token = output.data.split(
  3358. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  3359. output = self.app.post('/foo/regenerate')
  3360. self.assertEqual(output.status_code, 404)
  3361. output = self.app.post('/test/regenerate')
  3362. self.assertEqual(output.status_code, 403)
  3363. ast.return_value = True
  3364. output = self.app.post('/test/regenerate')
  3365. self.assertEqual(output.status_code, 302)
  3366. ast.return_value = False
  3367. user.username = 'pingou'
  3368. with tests.user_set(self.app.application, user):
  3369. output = self.app.post('/test/regenerate')
  3370. self.assertEqual(output.status_code, 400)
  3371. data = {'csrf_token': csrf_token}
  3372. output = self.app.post('/test/regenerate', data=data)
  3373. self.assertEqual(output.status_code, 400)
  3374. data['regenerate'] = 'ticket'
  3375. output = self.app.post('/test/regenerate', data=data)
  3376. self.assertEqual(output.status_code, 400)
  3377. # Create an issue to play with
  3378. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3379. msg = pagure.lib.new_issue(
  3380. session=self.session,
  3381. repo=repo,
  3382. title='Test issue',
  3383. content='We should work on this',
  3384. user='pingou',
  3385. ticketfolder=None
  3386. )
  3387. self.session.commit()
  3388. self.assertEqual(msg.title, 'Test issue')
  3389. data['regenerate'] = 'tickets'
  3390. output = self.app.post(
  3391. '/test/regenerate', data=data, follow_redirects=True)
  3392. self.assertEqual(output.status_code, 200)
  3393. self.assertIn(
  3394. '</button>\n Tickets git repo updated',
  3395. output.data)
  3396. # Create a request to play with
  3397. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3398. msg = pagure.lib.new_pull_request(
  3399. session=self.session,
  3400. repo_from=repo,
  3401. branch_from='branch',
  3402. repo_to=repo,
  3403. branch_to='master',
  3404. title='Test pull-request',
  3405. user='pingou',
  3406. requestfolder=None,
  3407. )
  3408. self.session.commit()
  3409. self.assertEqual(msg.title, 'Test pull-request')
  3410. data['regenerate'] = 'requests'
  3411. output = self.app.post(
  3412. '/test/regenerate', data=data, follow_redirects=True)
  3413. self.assertEqual(output.status_code, 200)
  3414. self.assertIn(
  3415. '</button>\n Requests git repo updated',
  3416. output.data)
  3417. def test_view_tags(self):
  3418. """ Test the view_tags endpoint. """
  3419. output = self.app.get('/foo/releases')
  3420. # No project registered in the DB
  3421. self.assertEqual(output.status_code, 404)
  3422. tests.create_projects(self.session)
  3423. output = self.app.get('/test/releases')
  3424. # No git repo associated
  3425. self.assertEqual(output.status_code, 404)
  3426. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  3427. output = self.app.get('/test/releases')
  3428. self.assertEqual(output.status_code, 200)
  3429. self.assertIn('This project has not been tagged.', output.data)
  3430. # Add a README to the git repo - First commit
  3431. tests.add_readme_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  3432. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  3433. first_commit = repo.revparse_single('HEAD')
  3434. tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0)
  3435. repo.create_tag(
  3436. "0.0.1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger,
  3437. "Release 0.0.1")
  3438. output = self.app.get('/test/releases')
  3439. self.assertEqual(output.status_code, 200)
  3440. self.assertIn('0.0.1', output.data)
  3441. self.assertIn('<span id="tagid" class="label label-default">', output.data)
  3442. self.assertTrue(output.data.count('tagid'), 1)
  3443. def test_edit_file_no_signed_off(self):
  3444. """ Test the edit_file endpoint when signed-off isn't enforced. """
  3445. tests.create_projects(self.session)
  3446. tests.create_projects_git(
  3447. os.path.join(self.path, 'repos'), bare=True)
  3448. user = tests.FakeUser()
  3449. user.username = 'pingou'
  3450. with tests.user_set(self.app.application, user):
  3451. # Add some content to the git repo
  3452. tests.add_content_git_repo(
  3453. os.path.join(self.path, 'repos', 'test.git'))
  3454. output = self.app.get('/test/edit/master/f/sources')
  3455. self.assertEqual(output.status_code, 200)
  3456. self.assertIn(
  3457. '<li><a href="/test/tree/master">'
  3458. '<span class="oi" data-glyph="random"></span>&nbsp; master'
  3459. '</a></li><li class="active">'
  3460. '<span class="oi" data-glyph="file"></span>&nbsp; sources</li>',
  3461. output.data)
  3462. self.assertIn(
  3463. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  3464. output.data)
  3465. self.assertIn(
  3466. '<textarea rows="5" class="form-control" type="text" '
  3467. 'id="commit_message"\n name="commit_message" '
  3468. 'placeholder="An optional description of the change">'
  3469. '</textarea>', output.data
  3470. )
  3471. def test_edit_file_signed_off(self):
  3472. """ Test the edit_file endpoint when signed-off is enforced. """
  3473. tests.create_projects(self.session)
  3474. tests.create_projects_git(
  3475. os.path.join(self.path, 'repos'), bare=True)
  3476. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3477. settings = repo.settings
  3478. settings['Enforce_signed-off_commits_in_pull-request'] = True
  3479. repo.settings = settings
  3480. self.session.add(repo)
  3481. self.session.commit()
  3482. user = tests.FakeUser()
  3483. user.username = 'pingou'
  3484. with tests.user_set(self.app.application, user):
  3485. # Add some content to the git repo
  3486. tests.add_content_git_repo(
  3487. os.path.join(self.path, 'repos', 'test.git'))
  3488. output = self.app.get('/test/edit/master/f/sources')
  3489. self.assertEqual(output.status_code, 200)
  3490. self.assertIn(
  3491. '<li><a href="/test/tree/master">'
  3492. '<span class="oi" data-glyph="random"></span>&nbsp; master'
  3493. '</a></li><li class="active">'
  3494. '<span class="oi" data-glyph="file"></span>&nbsp; sources</li>',
  3495. output.data)
  3496. self.assertIn(
  3497. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  3498. output.data)
  3499. self.assertIn(
  3500. '<textarea rows="5" class="form-control" type="text" '
  3501. 'id="commit_message"\n name="commit_message" '
  3502. 'placeholder="An optional description of the change">'
  3503. 'Signed-off-by: pingou <bar@pingou.com></textarea>', output.data
  3504. )
  3505. def test_edit_file(self):
  3506. """ Test the edit_file endpoint. """
  3507. # No Git repo
  3508. output = self.app.get('/foo/edit/foo/f/sources')
  3509. self.assertEqual(output.status_code, 404)
  3510. user = tests.FakeUser()
  3511. with tests.user_set(self.app.application, user):
  3512. # No project registered in the DB
  3513. output = self.app.get('/foo/edit/foo/f/sources')
  3514. self.assertEqual(output.status_code, 404)
  3515. tests.create_projects(self.session)
  3516. tests.create_projects_git(os.path.join(self.path, 'repos'),
  3517. bare=True)
  3518. # No a repo admin
  3519. output = self.app.get('/test/edit/foo/f/sources')
  3520. self.assertEqual(output.status_code, 403)
  3521. # User not logged in
  3522. output = self.app.get('/test/edit/foo/f/sources')
  3523. self.assertEqual(output.status_code, 302)
  3524. user.username = 'pingou'
  3525. with tests.user_set(self.app.application, user):
  3526. # No such file
  3527. output = self.app.get('/test/edit/foo/f/sources')
  3528. self.assertEqual(output.status_code, 404)
  3529. # Add some content to the git repo
  3530. tests.add_content_git_repo(os.path.join(self.path, 'repos',
  3531. 'test.git'))
  3532. tests.add_readme_git_repo(os.path.join(self.path, 'repos',
  3533. 'test.git'))
  3534. tests.add_binary_git_repo(
  3535. os.path.join(self.path, 'repos', 'test.git'), 'test.jpg')
  3536. tests.add_binary_git_repo(
  3537. os.path.join(self.path, 'repos', 'test.git'), 'test_binary')
  3538. output = self.app.get('/test/edit/master/foofile')
  3539. self.assertEqual(output.status_code, 404)
  3540. # Edit page
  3541. output = self.app.get('/test/edit/master/f/sources')
  3542. self.assertEqual(output.status_code, 200)
  3543. self.assertIn(
  3544. '<li><a href="/test/tree/master">'
  3545. '<span class="oi" data-glyph="random"></span>&nbsp; master'
  3546. '</a></li><li class="active">'
  3547. '<span class="oi" data-glyph="file"></span>&nbsp; sources</li>',
  3548. output.data)
  3549. self.assertIn(
  3550. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  3551. output.data)
  3552. # Verify the nav links correctly when editing a file.
  3553. output = self.app.get('/test/blob/master/f/folder1/folder2/file')
  3554. self.assertEqual(output.status_code, 200)
  3555. self.assertIn(
  3556. '<li><a href="/test/blob/master/f/folder1/folder2">\n'
  3557. ' <span class="oi" data-glyph="folder">'
  3558. '</span>&nbsp; folder2</a>\n'
  3559. ' </li>', output.data)
  3560. csrf_token = output.data.split(
  3561. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  3562. # View what's supposed to be an image
  3563. output = self.app.get('/test/edit/master/f/test.jpg')
  3564. self.assertEqual(output.status_code, 400)
  3565. self.assertIn('<p>Cannot edit binary files</p>', output.data)
  3566. # Check file before the commit:
  3567. output = self.app.get('/test/raw/master/f/sources')
  3568. self.assertEqual(output.status_code, 200)
  3569. self.assertEqual(output.data, 'foo\n bar')
  3570. # No CSRF Token
  3571. data = {
  3572. 'content': 'foo\n bar\n baz',
  3573. 'commit_title': 'test commit',
  3574. 'commit_message': 'Online commits from the gure.lib.get',
  3575. }
  3576. output = self.app.post('/test/edit/master/f/sources', data=data)
  3577. self.assertEqual(output.status_code, 200)
  3578. self.assertIn(
  3579. '<title>Edit - test - Pagure</title>', output.data)
  3580. # Check that nothing changed
  3581. output = self.app.get('/test/raw/master/f/sources')
  3582. self.assertEqual(output.status_code, 200)
  3583. self.assertEqual(output.data, 'foo\n bar')
  3584. # Missing email
  3585. data['csrf_token'] = csrf_token
  3586. output = self.app.post('/test/edit/master/f/sources', data=data)
  3587. self.assertEqual(output.status_code, 200)
  3588. self.assertIn(
  3589. '<title>Edit - test - Pagure</title>', output.data)
  3590. # Invalid email
  3591. data['email'] = 'pingou@fp.o'
  3592. output = self.app.post('/test/edit/master/f/sources', data=data)
  3593. self.assertIn(
  3594. '<title>Edit - test - Pagure</title>', output.data)
  3595. # Works
  3596. data['email'] = 'bar@pingou.com'
  3597. data['branch'] = 'master'
  3598. output = self.app.post(
  3599. '/test/edit/master/f/sources', data=data,
  3600. follow_redirects=True)
  3601. self.assertEqual(output.status_code, 200)
  3602. self.assertIn(
  3603. '<title>Commits - test - Pagure</title>', output.data)
  3604. self.assertIn('test commit', output.data)
  3605. # Check file after the commit:
  3606. output = self.app.get('/test/raw/master/f/sources')
  3607. self.assertEqual(output.status_code, 200)
  3608. self.assertEqual(output.data, 'foo\n bar\n baz')
  3609. # Add a fork of a fork
  3610. item = pagure.lib.model.Project(
  3611. user_id=1, # pingou
  3612. name='test3',
  3613. description='test project #3',
  3614. is_fork=True,
  3615. parent_id=1,
  3616. hook_token='aaabbbppp',
  3617. )
  3618. self.session.add(item)
  3619. self.session.commit()
  3620. tests.add_content_git_repo(
  3621. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  3622. tests.add_readme_git_repo(
  3623. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'))
  3624. tests.add_commit_git_repo(
  3625. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test3.git'),
  3626. ncommits=10)
  3627. # Verify the nav links correctly when editing a file in a fork.
  3628. output = self.app.get(
  3629. '/fork/pingou/test3/edit/master/f/folder1/folder2/file')
  3630. self.assertEqual(output.status_code, 200)
  3631. self.assertIn(
  3632. '<li><a\n '
  3633. 'href="/fork/pingou/test3/blob/master/f/folder1/folder2"\n'
  3634. ' ><span class="oi" data-glyph="folder"></span>&nbsp; '
  3635. 'folder2</a>\n </li>', output.data)
  3636. output = self.app.get('/fork/pingou/test3/edit/master/f/sources')
  3637. self.assertEqual(output.status_code, 200)
  3638. self.assertIn(
  3639. '<li><a href="/fork/pingou/test3/tree/master">'
  3640. '<span class="oi" data-glyph="random"></span>&nbsp; master'
  3641. '</a></li><li class="active">'
  3642. '<span class="oi" data-glyph="file"></span>&nbsp; sources'
  3643. '</li>', output.data)
  3644. self.assertIn(
  3645. '<textarea id="textareaCode" name="content">foo\n barRow 0\n',
  3646. output.data)
  3647. # Empty the file - no `content` provided
  3648. data = {
  3649. 'commit_title': 'test commit',
  3650. 'commit_message': 'Online commits from the gure.lib.get',
  3651. 'csrf_token': csrf_token,
  3652. 'email': 'bar@pingou.com',
  3653. 'branch': 'master',
  3654. }
  3655. output = self.app.post(
  3656. '/test/edit/master/f/sources', data=data,
  3657. follow_redirects=True)
  3658. self.assertEqual(output.status_code, 200)
  3659. self.assertIn(
  3660. '<title>Commits - test - Pagure</title>', output.data)
  3661. self.assertIn('test commit', output.data)
  3662. # Check file after the commit:
  3663. output = self.app.get('/test/raw/master/f/sources')
  3664. self.assertEqual(output.status_code, 404)
  3665. self.assertIn(
  3666. '<p>No content found</p>', output.data)
  3667. @patch('pagure.decorators.admin_session_timedout')
  3668. def test_change_ref_head(self,ast):
  3669. """ Test the change_ref_head endpoint. """
  3670. ast.return_value = True
  3671. # No Git repo
  3672. output = self.app.post('/foo/default/branch/')
  3673. self.assertEqual(output.status_code, 404)
  3674. user = tests.FakeUser()
  3675. with tests.user_set(self.app.application, user):
  3676. output = self.app.post('/foo/default/branch/')
  3677. self.assertEqual(output.status_code, 404)
  3678. ast.return_value = False
  3679. output = self.app.post('/foo/default/branch/')
  3680. self.assertEqual(output.status_code, 404)
  3681. tests.create_projects(self.session)
  3682. repos = tests.create_projects_git(os.path.join(self.path, 'repos'))
  3683. output = self.app.post('/test/default/branch/')
  3684. self.assertEqual(output.status_code, 403)
  3685. # User no logged in
  3686. output = self.app.post('/test/default/branch/')
  3687. self.assertEqual(output.status_code, 302)
  3688. user.username = 'pingou'
  3689. with tests.user_set(self.app.application, user):
  3690. output = self.app.post('/test/default/branch/',
  3691. follow_redirects=True) # without git branch
  3692. self.assertEqual(output.status_code, 200)
  3693. self.assertIn(
  3694. '<title>Settings - test - Pagure</title>', output.data)
  3695. self.assertIn('<h3>Settings for test</h3>', output.data)
  3696. self.assertIn(
  3697. '<select class="c-select" id="branches" name="branches">'
  3698. '</select>', output.data)
  3699. csrf_token = output.data.split(
  3700. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  3701. repo_obj = pygit2.Repository(repos[0])
  3702. tree = repo_obj.index.write_tree()
  3703. author = pygit2.Signature(
  3704. 'Alice Author', 'alice@authors.tld')
  3705. committer = pygit2.Signature(
  3706. 'Cecil Committer', 'cecil@committers.tld')
  3707. repo_obj.create_commit(
  3708. 'refs/heads/master', # the name of the reference to update
  3709. author,
  3710. committer,
  3711. 'Add sources file for testing',
  3712. # binary string representing the tree object ID
  3713. tree,
  3714. # list of binary strings representing parents of the new commit
  3715. []
  3716. )
  3717. repo_obj.create_branch("feature",repo_obj.head.get_object())
  3718. data = {
  3719. 'branches': 'feature',
  3720. 'csrf_token': csrf_token,
  3721. }
  3722. output = self.app.post('/test/default/branch/', # changing head to feature branch
  3723. data=data,
  3724. follow_redirects=True)
  3725. self.assertEqual(output.status_code, 200)
  3726. self.assertIn(
  3727. '<title>Settings - test - Pagure</title>', output.data)
  3728. self.assertIn('<h3>Settings for test</h3>', output.data)
  3729. self.assertIn(
  3730. '<select class="c-select" id="branches" name="branches">'
  3731. '<option selected value="feature">feature</option>'
  3732. '<option value="master">master</option>'
  3733. '</select>', output.data)
  3734. self.assertIn(
  3735. '</button>\n Default branch updated '
  3736. 'to feature', output.data)
  3737. data = {
  3738. 'branches': 'master',
  3739. 'csrf_token': csrf_token,
  3740. }
  3741. output = self.app.post('/test/default/branch/', # changing head to master branch
  3742. data=data,
  3743. follow_redirects=True)
  3744. self.assertEqual(output.status_code, 200)
  3745. self.assertIn(
  3746. '<title>Settings - test - Pagure</title>', output.data)
  3747. self.assertIn('<h3>Settings for test</h3>', output.data)
  3748. self.assertIn(
  3749. '<select class="c-select" id="branches" name="branches">'
  3750. '<option value="feature">feature</option>'
  3751. '<option selected value="master">master</option>'
  3752. '</select>', output.data)
  3753. self.assertIn(
  3754. '</button>\n Default branch updated '
  3755. 'to master', output.data)
  3756. def test_new_release(self):
  3757. """ Test the new_release endpoint. """
  3758. # No Git repo
  3759. output = self.app.post('/foo/upload/')
  3760. self.assertEqual(output.status_code, 404)
  3761. user = tests.FakeUser()
  3762. with tests.user_set(self.app.application, user):
  3763. output = self.app.post('/foo/upload/')
  3764. self.assertEqual(output.status_code, 404)
  3765. tests.create_projects(self.session)
  3766. repo = tests.create_projects_git(os.path.join(self.path, 'repos'))
  3767. output = self.app.post('/test/upload/')
  3768. self.assertEqual(output.status_code, 403)
  3769. # User not logged in
  3770. output = self.app.post('/test/upload/')
  3771. self.assertEqual(output.status_code, 302)
  3772. user.username = 'pingou'
  3773. with tests.user_set(self.app.application, user):
  3774. img = os.path.join(os.path.abspath(os.path.dirname(__file__)),
  3775. 'placebo.png')
  3776. # Missing CSRF Token
  3777. with open(img, mode='rb') as stream:
  3778. data = {'filestream': stream}
  3779. output = self.app.post('/test/upload/', data=data)
  3780. self.assertEqual(output.status_code, 200)
  3781. self.assertIn('<h2>Upload a new release</h2>', output.data)
  3782. csrf_token = output.data.split(
  3783. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  3784. upload_dir = os.path.join(self.path, 'releases')
  3785. self.assertEqual(os.listdir(upload_dir), [])
  3786. # Upload successful
  3787. with open(img, mode='rb') as stream:
  3788. data = {'filestream': stream, 'csrf_token': csrf_token}
  3789. output = self.app.post(
  3790. '/test/upload/', data=data, follow_redirects=True)
  3791. self.assertEqual(output.status_code, 200)
  3792. self.assertIn(
  3793. '</button>\n File', output.data)
  3794. self.assertIn(
  3795. 'uploaded\n </div>', output.data)
  3796. self.assertIn('This project has not been tagged.', output.data)
  3797. self.assertEqual(os.listdir(upload_dir), ['test'])
  3798. folder = os.path.join(upload_dir, 'test')
  3799. checksum_file = os.path.join(folder, 'CHECKSUMS')
  3800. # Wait for the worker to create the checksums file
  3801. cnt = 0
  3802. while not os.path.exists(checksum_file):
  3803. print os.listdir(os.path.join(upload_dir, 'test'))
  3804. cnt += 1
  3805. if cnt == 40:
  3806. raise ValueError(
  3807. 'The worker did not create the checksums file '
  3808. 'in a timely manner')
  3809. time.sleep(0.5)
  3810. self.assertEqual(len(os.listdir(folder)), 2)
  3811. self.assertTrue(os.path.exists(checksum_file))
  3812. # Check the content of the checksums file
  3813. with open(checksum_file) as stream:
  3814. data = stream.readlines()
  3815. self.assertEqual(len(data), 3)
  3816. self.assertEqual(data[0], '# Generated and updated by pagure\n')
  3817. self.assertTrue(data[1].startswith('SHA256 ('))
  3818. self.assertTrue(data[1].endswith(
  3819. 'tests_placebo.png) = 8a06845923010b27bfd8e7e75acff'
  3820. '7badc40d1021b4994e01f5e11ca40bc3abe\n'))
  3821. self.assertTrue(data[2].startswith('SHA512 ('))
  3822. self.assertTrue(data[2].endswith(
  3823. 'tests_placebo.png) = 65a4458df0acb29dc3c5ad4a3620e'
  3824. '98841d1fcf3f8df358f5348fdeddd1a86706491ac6e416768e'
  3825. '9f218aae8147d6ac524a59d3eb91fb925fdcb5c489e55ccbb\n'))
  3826. # Try uploading the same file -- fails
  3827. with open(img, mode='rb') as stream:
  3828. data = {'filestream': stream, 'csrf_token': csrf_token}
  3829. output = self.app.post(
  3830. '/test/upload/', data=data, follow_redirects=True)
  3831. self.assertEqual(output.status_code, 200)
  3832. self.assertIn(
  3833. '</button>\n This tarball has already '
  3834. 'been uploaded', output.data)
  3835. self.assertIn('This project has not been tagged.', output.data)
  3836. @patch('pagure.decorators.admin_session_timedout')
  3837. def test_add_token_all_tokens(self, ast):
  3838. """ Test the add_token endpoint. """
  3839. ast.return_value = False
  3840. tests.create_projects(self.session)
  3841. tests.create_projects_git(
  3842. os.path.join(self.path, 'repos'), bare=True)
  3843. user = tests.FakeUser(username='pingou')
  3844. with tests.user_set(self.app.application, user):
  3845. output = self.app.get('/test/token/new/')
  3846. self.assertEqual(output.status_code, 200)
  3847. self.assertIn('<strong>Create a new token</strong>', output.data)
  3848. self.assertEqual(
  3849. output.data.count('<label class="c-input c-checkbox">'),
  3850. len(pagure.config.config['ACLS'].keys()) - 1
  3851. )
  3852. @patch.dict('pagure.config.config', {'USER_ACLS': ['create_project']})
  3853. @patch('pagure.decorators.admin_session_timedout')
  3854. def test_add_token_one_token(self, ast):
  3855. """ Test the add_token endpoint. """
  3856. ast.return_value = False
  3857. tests.create_projects(self.session)
  3858. tests.create_projects_git(
  3859. os.path.join(self.path, 'repos'), bare=True)
  3860. user = tests.FakeUser(username='pingou')
  3861. with tests.user_set(self.app.application, user):
  3862. output = self.app.get('/test/token/new/')
  3863. self.assertEqual(output.status_code, 200)
  3864. self.assertIn('<strong>Create a new token</strong>', output.data)
  3865. self.assertEqual(
  3866. output.data.count('<label class="c-input c-checkbox">'),
  3867. 1
  3868. )
  3869. @patch('pagure.decorators.admin_session_timedout')
  3870. def test_add_token(self, ast):
  3871. """ Test the add_token endpoint. """
  3872. ast.return_value = False
  3873. # No Git repo
  3874. output = self.app.get('/foo/token/new/')
  3875. self.assertEqual(output.status_code, 404)
  3876. user = tests.FakeUser()
  3877. with tests.user_set(self.app.application, user):
  3878. output = self.app.get('/foo/token/new/')
  3879. self.assertEqual(output.status_code, 404)
  3880. tests.create_projects(self.session)
  3881. tests.create_projects_git(os.path.join(self.path, 'repos'),
  3882. bare=True)
  3883. output = self.app.get('/test/token/new/')
  3884. self.assertEqual(output.status_code, 403)
  3885. # User not logged in
  3886. output = self.app.get('/test/token/new/')
  3887. self.assertEqual(output.status_code, 302)
  3888. user.username = 'pingou'
  3889. with tests.user_set(self.app.application, user):
  3890. output = self.app.get('/test/token/new/')
  3891. self.assertEqual(output.status_code, 200)
  3892. self.assertIn('<strong>Create a new token</strong>', output.data)
  3893. csrf_token = output.data.split(
  3894. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  3895. data = {'csrf_token': csrf_token}
  3896. ast.return_value = True
  3897. # Test when the session timed-out
  3898. output = self.app.post('/test/token/new/', data=data)
  3899. self.assertEqual(output.status_code, 302)
  3900. output = self.app.get('/')
  3901. self.assertEqual(output.status_code, 200)
  3902. self.assertIn(
  3903. '</button>\n Action canceled, try it '
  3904. 'again', output.data)
  3905. ast.return_value = False
  3906. # Missing acls
  3907. output = self.app.post('/test/token/new/', data=data)
  3908. self.assertEqual(output.status_code, 200)
  3909. self.assertIn('<strong>Create a new token</strong>', output.data)
  3910. self.assertIn(
  3911. '</button>\n You must select at least '
  3912. 'one permission.', output.data)
  3913. data = {
  3914. 'csrf_token': csrf_token,
  3915. 'acls': ['issue_create'],
  3916. 'description': 'Test token',
  3917. }
  3918. # New token created
  3919. output = self.app.post(
  3920. '/test/token/new/', data=data, follow_redirects=True)
  3921. self.assertEqual(output.status_code, 200)
  3922. self.assertIn(
  3923. '</button>\n Token created', output.data)
  3924. self.assertIn(
  3925. '<title>Settings - test - Pagure</title>', output.data)
  3926. self.assertIn('<h3>Settings for test</h3>', output.data)
  3927. self.assertIn('<strong>Test token</strong>', output.data)
  3928. self.assertIn(
  3929. '<span class="text-success btn-align"><strong>Valid</strong> until: ',
  3930. output.data)
  3931. @patch('pagure.decorators.admin_session_timedout')
  3932. def test_revoke_api_token(self, ast):
  3933. """ Test the revoke_api_token endpoint. """
  3934. ast.return_value = False
  3935. # No Git repo
  3936. output = self.app.post('/foo/token/revoke/123')
  3937. self.assertEqual(output.status_code, 404)
  3938. user = tests.FakeUser()
  3939. with tests.user_set(self.app.application, user):
  3940. output = self.app.post('/foo/token/revoke/123')
  3941. self.assertEqual(output.status_code, 404)
  3942. tests.create_projects(self.session)
  3943. tests.create_projects_git(os.path.join(self.path, 'repos'),
  3944. bare=True)
  3945. output = self.app.post('/test/token/revoke/123')
  3946. self.assertEqual(output.status_code, 403)
  3947. # User not logged in
  3948. output = self.app.post('/test/token/revoke/123')
  3949. self.assertEqual(output.status_code, 302)
  3950. user.username = 'pingou'
  3951. with tests.user_set(self.app.application, user):
  3952. output = self.app.get('/test/token/new')
  3953. self.assertEqual(output.status_code, 200)
  3954. self.assertIn('<strong>Create a new token</strong>', output.data)
  3955. csrf_token = output.data.split(
  3956. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  3957. data = {'csrf_token': csrf_token}
  3958. ast.return_value = True
  3959. # Test when the session timed-out
  3960. output = self.app.post('/test/token/revoke/123', data=data)
  3961. self.assertEqual(output.status_code, 302)
  3962. output = self.app.get('/')
  3963. self.assertEqual(output.status_code, 200)
  3964. self.assertIn(
  3965. '</button>\n Action canceled, try it again',
  3966. output.data)
  3967. ast.return_value = False
  3968. output = self.app.post('/test/token/revoke/123', data=data)
  3969. self.assertEqual(output.status_code, 404)
  3970. self.assertIn('<p>Token not found</p>', output.data)
  3971. # Create a token to revoke
  3972. data = {'csrf_token': csrf_token, 'acls': ['issue_create']}
  3973. output = self.app.post(
  3974. '/test/token/new/', data=data, follow_redirects=True)
  3975. self.assertEqual(output.status_code, 200)
  3976. self.assertIn(
  3977. '</button>\n Token created',
  3978. output.data)
  3979. # Existing token will expire in 60 days
  3980. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3981. self.assertEqual(
  3982. repo.tokens[0].expiration.date(),
  3983. datetime.datetime.utcnow().date() + datetime.timedelta(days=60))
  3984. token = repo.tokens[0].id
  3985. output = self.app.post(
  3986. '/test/token/revoke/%s' % token,
  3987. data=data,
  3988. follow_redirects=True)
  3989. self.assertIn(
  3990. '<title>Settings - test - Pagure</title>', output.data)
  3991. self.assertIn(
  3992. '</button>\n Token revoked',
  3993. output.data)
  3994. # Existing token has been expired
  3995. self.session.commit()
  3996. repo = pagure.lib.get_authorized_project(self.session, 'test')
  3997. self.assertEqual(
  3998. repo.tokens[0].expiration.date(),
  3999. repo.tokens[0].created.date())
  4000. self.assertEqual(
  4001. repo.tokens[0].expiration.date(),
  4002. datetime.datetime.utcnow().date())
  4003. def test_delete_branch(self):
  4004. """ Test the delete_branch endpoint. """
  4005. # No Git repo
  4006. output = self.app.post('/foo/b/master/delete')
  4007. self.assertEqual(output.status_code, 404)
  4008. tests.create_projects(self.session)
  4009. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4010. # User not logged in
  4011. output = self.app.post('/test/b/master/delete')
  4012. self.assertEqual(output.status_code, 302)
  4013. user = tests.FakeUser()
  4014. with tests.user_set(self.app.application, user):
  4015. # Unknown repo
  4016. output = self.app.post('/foo/b/master/delete')
  4017. self.assertEqual(output.status_code, 404)
  4018. output = self.app.post('/test/b/master/delete')
  4019. self.assertEqual(output.status_code, 403)
  4020. user.username = 'pingou'
  4021. with tests.user_set(self.app.application, user):
  4022. output = self.app.post('/test/b/master/delete')
  4023. self.assertEqual(output.status_code, 403)
  4024. self.assertIn(
  4025. '<p>You are not allowed to delete the master branch</p>',
  4026. output.data)
  4027. output = self.app.post('/test/b/bar/delete')
  4028. self.assertEqual(output.status_code, 404)
  4029. self.assertIn('<p>Branch not found</p>', output.data)
  4030. # Add a branch that we can delete
  4031. path = os.path.join(self.path, 'repos', 'test.git')
  4032. tests.add_content_git_repo(path)
  4033. repo = pygit2.Repository(path)
  4034. repo.create_branch('foo', repo.head.get_object())
  4035. # Check before deletion
  4036. output = self.app.get('/test')
  4037. self.assertEqual(output.status_code, 200)
  4038. self.assertIn(
  4039. 'data-toggle="tooltip">foo',
  4040. output.data)
  4041. self.assertIn('<form id="delete_branch_form-foo"', output.data)
  4042. self.assertIn(
  4043. '<strong title="Currently viewing branch master"',
  4044. output.data)
  4045. # Delete the branch
  4046. output = self.app.post('/test/b/foo/delete', follow_redirects=True)
  4047. self.assertEqual(output.status_code, 200)
  4048. self.assertNotIn(
  4049. 'data-toggle="tooltip">foo',
  4050. output.data)
  4051. self.assertNotIn(
  4052. '<form id="delete_branch_form-foo"', output.data)
  4053. self.assertIn(
  4054. '<strong title="Currently viewing branch master"',
  4055. output.data)
  4056. # Add a branch with a '/' in its name that we can delete
  4057. path = os.path.join(self.path, 'repos', 'test.git')
  4058. tests.add_content_git_repo(path)
  4059. repo = pygit2.Repository(path)
  4060. repo.create_branch('feature/foo', repo.head.get_object())
  4061. # Check before deletion
  4062. output = self.app.get('/test')
  4063. self.assertEqual(output.status_code, 200)
  4064. self.assertIn(
  4065. 'data-toggle="tooltip">feature/foo',
  4066. output.data)
  4067. self.assertIn(
  4068. '<form id="delete_branch_form-feature__foo"', output.data)
  4069. self.assertIn(
  4070. '<strong title="Currently viewing branch master"',
  4071. output.data)
  4072. # Delete the branch
  4073. output = self.app.post('/test/b/feature/foo/delete', follow_redirects=True)
  4074. self.assertEqual(output.status_code, 200)
  4075. self.assertNotIn(
  4076. 'data-toggle="tooltip">feature/foo',
  4077. output.data)
  4078. self.assertNotIn(
  4079. '<form id="delete_branch_form-feature__foo"', output.data)
  4080. self.assertIn(
  4081. '<strong title="Currently viewing branch master"',
  4082. output.data)
  4083. @patch.dict('pagure.config.config', {'ALLOW_DELETE_BRANCH': False})
  4084. def test_delete_branch_disabled_in_ui(self):
  4085. """ Test that the delete branch button doesn't show when the feature
  4086. is turned off. """
  4087. tests.create_projects(self.session)
  4088. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4089. # Add a branch that we can delete
  4090. path = os.path.join(self.path, 'repos', 'test.git')
  4091. tests.add_content_git_repo(path)
  4092. repo = pygit2.Repository(path)
  4093. repo.create_branch('foo', repo.head.get_object())
  4094. user = tests.FakeUser(username = 'pingou')
  4095. with tests.user_set(self.app.application, user):
  4096. # Check that the UI doesn't offer the button
  4097. output = self.app.get('/test')
  4098. self.assertEqual(output.status_code, 200)
  4099. self.assertIn(
  4100. 'data-toggle="tooltip">foo',
  4101. output.data)
  4102. self.assertNotIn('<form id="delete_branch_form-foo"', output.data)
  4103. self.assertNotIn(
  4104. 'Are you sure you want to remove the branch',
  4105. output.data)
  4106. @patch.dict('pagure.config.config', {'ALLOW_DELETE_BRANCH': False})
  4107. def test_delete_branch_disabled(self):
  4108. """ Test the delete_branch endpoint when it's disabled in the entire
  4109. instance. """
  4110. tests.create_projects(self.session)
  4111. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4112. # Add a branch that we can delete
  4113. path = os.path.join(self.path, 'repos', 'test.git')
  4114. tests.add_content_git_repo(path)
  4115. repo = pygit2.Repository(path)
  4116. repo.create_branch('foo', repo.head.get_object())
  4117. user = tests.FakeUser(username = 'pingou')
  4118. with tests.user_set(self.app.application, user):
  4119. # Delete the branch
  4120. output = self.app.post('/test/b/foo/delete', follow_redirects=True)
  4121. self.assertEqual(output.status_code, 404)
  4122. def test_view_docs(self):
  4123. """ Test the view_docs endpoint. """
  4124. output = self.app.get('/docs/foo/')
  4125. # No project registered in the DB
  4126. self.assertEqual(output.status_code, 404)
  4127. tests.create_projects(self.session)
  4128. output = self.app.get('/docs/test/')
  4129. # No git repo associated
  4130. self.assertEqual(output.status_code, 404)
  4131. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4132. output = self.app.get('/docs/test/')
  4133. self.assertEqual(output.status_code, 404)
  4134. def test_view_project_activity(self):
  4135. """ Test the view_project_activity endpoint. """
  4136. tests.create_projects(self.session)
  4137. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4138. # Project Exists, but No DATAGREPPER_URL set
  4139. output = self.app.get('/test/activity/')
  4140. self.assertEqual(output.status_code, 404)
  4141. # Project Exists, and DATAGREPPER_URL set
  4142. pagure.config.config['DATAGREPPER_URL'] = 'foo'
  4143. output = self.app.get('/test/activity/')
  4144. self.assertEqual(output.status_code, 200)
  4145. self.assertIn(
  4146. '<title>Activity - test - Pagure</title>', output.data)
  4147. self.assertIn(
  4148. 'No activity reported on the test project', output.data)
  4149. # project doesnt exist
  4150. output = self.app.get('/foo/activity/')
  4151. self.assertEqual(output.status_code, 404)
  4152. def test_goimport(self):
  4153. """ Test the go-import tag. """
  4154. tests.create_projects(self.session)
  4155. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4156. output = self.app.get('/test/')
  4157. self.assertEqual(output.status_code, 200)
  4158. self.assertIn('<meta name="go-import" '
  4159. 'content="pagure.org/test git git://pagure.org/test.git"'
  4160. '>',
  4161. output.data)
  4162. def test_watch_repo(self):
  4163. """ Test the watch_repo endpoint. """
  4164. output = self.app.post('/watch/')
  4165. self.assertEqual(output.status_code, 405)
  4166. tests.create_projects(self.session)
  4167. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4168. user = tests.FakeUser()
  4169. user.username = 'pingou'
  4170. with tests.user_set(self.app.application, user):
  4171. output = self.app.get('/new/')
  4172. self.assertEqual(output.status_code, 200)
  4173. self.assertIn('<strong>Create new Project</strong>', output.data)
  4174. csrf_token = output.data.split(
  4175. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  4176. data = {
  4177. 'csrf_token':csrf_token
  4178. }
  4179. output = self.app.post(
  4180. '/foo/watch/settings/1', data=data, follow_redirects=True)
  4181. self.assertEqual(output.status_code, 404)
  4182. output = self.app.post(
  4183. '/test/watch/settings/8', data=data, follow_redirects=True)
  4184. self.assertEqual(output.status_code, 400)
  4185. output = self.app.post(
  4186. '/test/watch/settings/0', data=data, follow_redirects=True)
  4187. self.assertIn(
  4188. '</button>\n You are no longer'
  4189. ' watching this project', output.data)
  4190. output = self.app.post(
  4191. '/test/watch/settings/1', data=data, follow_redirects=True)
  4192. self.assertIn(
  4193. '</button>\n You are now'
  4194. ' watching issues and PRs on this project', output.data)
  4195. output = self.app.post(
  4196. '/test/watch/settings/2', data=data, follow_redirects=True)
  4197. self.assertIn(
  4198. '</button>\n You are now'
  4199. ' watching commits on this project', output.data)
  4200. output = self.app.post(
  4201. '/test/watch/settings/3', data=data, follow_redirects=True)
  4202. self.assertIn(
  4203. ('</button>\n You are now'
  4204. ' watching issues, PRs, and commits on this project'),
  4205. output.data)
  4206. item = pagure.lib.model.Project(
  4207. user_id=2, # foo
  4208. name='test',
  4209. description='foo project #1',
  4210. hook_token='aaabbb',
  4211. is_fork=True,
  4212. parent_id=1,
  4213. )
  4214. self.session.add(item)
  4215. self.session.commit()
  4216. gitrepo = os.path.join(self.path, 'repos', 'forks', 'foo',
  4217. 'test.git')
  4218. pygit2.init_repository(gitrepo, bare=True)
  4219. output = self.app.post(
  4220. '/fork/foo/test/watch/settings/-1', data=data,
  4221. follow_redirects=True)
  4222. self.assertIn(
  4223. '</button>\n Watch status is already reset',
  4224. output.data)
  4225. output = self.app.post(
  4226. '/fork/foo/test/watch/settings/0', data=data,
  4227. follow_redirects=True)
  4228. self.assertIn(
  4229. '</button>\n You are no longer'
  4230. ' watching this project', output.data)
  4231. output = self.app.post(
  4232. '/fork/foo/test/watch/settings/1', data=data,
  4233. follow_redirects=True)
  4234. self.assertIn(
  4235. '</button>\n You are now'
  4236. ' watching issues and PRs on this project', output.data)
  4237. output = self.app.post(
  4238. '/fork/foo/test/watch/settings/2', data=data,
  4239. follow_redirects=True)
  4240. self.assertIn(
  4241. '</button>\n You are now'
  4242. ' watching commits on this project', output.data)
  4243. output = self.app.post(
  4244. '/fork/foo/test/watch/settings/3', data=data,
  4245. follow_redirects=True)
  4246. self.assertIn(
  4247. ('</button>\n You are now'
  4248. ' watching issues, PRs, and commits on this project'),
  4249. output.data)
  4250. output = self.app.post(
  4251. '/fork/foo/test/watch/settings/-1', data=data,
  4252. follow_redirects=True)
  4253. self.assertIn(
  4254. '</button>\n Watch status reset',
  4255. output.data)
  4256. def test_delete_report(self):
  4257. """ Test the delete_report endpoint. """
  4258. output = self.app.post('/test/delete/report')
  4259. self.assertEqual(output.status_code, 404)
  4260. tests.create_projects(self.session)
  4261. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4262. user = tests.FakeUser()
  4263. user.username = 'pingou'
  4264. with tests.user_set(self.app.application, user):
  4265. output = self.app.get('/new/')
  4266. self.assertEqual(output.status_code, 200)
  4267. self.assertIn('<strong>Create new Project</strong>', output.data)
  4268. csrf_token = output.data.split(
  4269. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  4270. # No report specified
  4271. data = {
  4272. 'csrf_token':csrf_token
  4273. }
  4274. output = self.app.post(
  4275. '/test/delete/report', data=data, follow_redirects=True)
  4276. self.assertEqual(output.status_code, 200)
  4277. self.assertIn(
  4278. '</button>\n Unknown report: None',
  4279. output.data)
  4280. # Report specified not in the project's reports
  4281. data = {
  4282. 'csrf_token':csrf_token,
  4283. 'report': 'foo'
  4284. }
  4285. output = self.app.post(
  4286. '/test/delete/report', data=data, follow_redirects=True)
  4287. self.assertEqual(output.status_code, 200)
  4288. self.assertIn(
  4289. '</button>\n Unknown report: foo',
  4290. output.data)
  4291. # Create a report
  4292. project = pagure.lib.get_authorized_project(self.session, project_name='test')
  4293. self.assertEqual(project.reports, {})
  4294. name = 'test report'
  4295. url = '?foo=bar&baz=biz'
  4296. pagure.lib.save_report(
  4297. self.session,
  4298. repo=project,
  4299. name=name,
  4300. url=url,
  4301. username=None
  4302. )
  4303. self.session.commit()
  4304. project = pagure.lib.get_authorized_project(self.session, project_name='test')
  4305. self.assertEqual(
  4306. project.reports,
  4307. {'test report': {'baz': 'biz', 'foo': 'bar'}}
  4308. )
  4309. # Missing CSRF
  4310. data = {
  4311. 'report': 'test report'
  4312. }
  4313. output = self.app.post(
  4314. '/test/delete/report', data=data, follow_redirects=True)
  4315. self.assertEqual(output.status_code, 200)
  4316. self.assertIn(
  4317. '<title>Settings - test - Pagure</title>',
  4318. output.data)
  4319. project = pagure.lib.get_authorized_project(self.session, project_name='test')
  4320. self.assertEqual(
  4321. project.reports,
  4322. {'test report': {'baz': 'biz', 'foo': 'bar'}}
  4323. )
  4324. # Delete the report
  4325. data = {
  4326. 'csrf_token':csrf_token,
  4327. 'report': 'test report'
  4328. }
  4329. output = self.app.post(
  4330. '/test/delete/report', data=data, follow_redirects=True)
  4331. self.assertEqual(output.status_code, 200)
  4332. self.assertIn(
  4333. '</button>\n List of reports updated',
  4334. output.data)
  4335. self.session.commit()
  4336. project = pagure.lib.get_authorized_project(self.session, project_name='test')
  4337. self.assertEqual(project.reports, {})
  4338. def test_delete_report_ns_project(self):
  4339. """ Test the delete_report endpoint on a namespaced project. """
  4340. output = self.app.post('/foo/test/delete/report')
  4341. self.assertEqual(output.status_code, 404)
  4342. tests.create_projects(self.session)
  4343. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4344. user = tests.FakeUser()
  4345. user.username = 'pingou'
  4346. with tests.user_set(self.app.application, user):
  4347. output = self.app.get('/new/')
  4348. self.assertEqual(output.status_code, 200)
  4349. self.assertIn('<strong>Create new Project</strong>', output.data)
  4350. csrf_token = output.data.split(
  4351. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  4352. item = pagure.lib.model.Project(
  4353. user_id=1, # pingou
  4354. namespace='foo',
  4355. name='test',
  4356. description='foo project #2',
  4357. hook_token='aaabbb',
  4358. )
  4359. self.session.add(item)
  4360. self.session.commit()
  4361. gitrepo = os.path.join(self.path, 'repos', 'foo', 'test.git')
  4362. pygit2.init_repository(gitrepo, bare=True)
  4363. # No report specified
  4364. data = {
  4365. 'csrf_token':csrf_token
  4366. }
  4367. output = self.app.post(
  4368. '/foo/test/delete/report', data=data, follow_redirects=True)
  4369. self.assertEqual(output.status_code, 200)
  4370. self.assertIn(
  4371. '</button>\n Unknown report: None',
  4372. output.data)
  4373. # Report specified not in the project's reports
  4374. data = {
  4375. 'csrf_token':csrf_token,
  4376. 'report': 'foo'
  4377. }
  4378. output = self.app.post(
  4379. '/foo/test/delete/report', data=data, follow_redirects=True)
  4380. self.assertEqual(output.status_code, 200)
  4381. self.assertIn(
  4382. '</button>\n Unknown report: foo',
  4383. output.data)
  4384. # Create a report
  4385. self.session.commit()
  4386. project = pagure.lib.get_authorized_project(
  4387. self.session, project_name='test', namespace='foo')
  4388. self.assertEqual(project.reports, {})
  4389. name = 'test report'
  4390. url = '?foo=bar&baz=biz'
  4391. pagure.lib.save_report(
  4392. self.session,
  4393. repo=project,
  4394. name=name,
  4395. url=url,
  4396. username=None
  4397. )
  4398. self.session.commit()
  4399. project = pagure.lib.get_authorized_project(
  4400. self.session, project_name='test', namespace='foo')
  4401. self.assertEqual(
  4402. project.reports,
  4403. {'test report': {'baz': 'biz', 'foo': 'bar'}}
  4404. )
  4405. # Missing CSRF
  4406. data = {
  4407. 'report': 'test report'
  4408. }
  4409. output = self.app.post(
  4410. '/foo/test/delete/report', data=data, follow_redirects=True)
  4411. self.assertEqual(output.status_code, 200)
  4412. self.assertIn(
  4413. '<title>Settings - foo/test - Pagure</title>',
  4414. output.data)
  4415. project = pagure.lib.get_authorized_project(
  4416. self.session, project_name='test', namespace='foo')
  4417. self.assertEqual(
  4418. project.reports,
  4419. {'test report': {'baz': 'biz', 'foo': 'bar'}}
  4420. )
  4421. # Delete the report
  4422. data = {
  4423. 'csrf_token':csrf_token,
  4424. 'report': 'test report'
  4425. }
  4426. output = self.app.post(
  4427. '/foo/test/delete/report', data=data, follow_redirects=True)
  4428. self.assertEqual(output.status_code, 200)
  4429. self.assertIn(
  4430. '</button>\n List of reports updated',
  4431. output.data)
  4432. self.session.commit()
  4433. project = pagure.lib.get_authorized_project(
  4434. self.session, project_name='test', namespace='foo')
  4435. self.assertEqual(project.reports, {})
  4436. def test_open_pr_button_empty_repo(self):
  4437. """ Test "Open Pull-Request" button on empty project. """
  4438. tests.create_projects(self.session)
  4439. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4440. output = self.app.get('/test')
  4441. self.assertEqual(output.status_code, 200)
  4442. self.assertIn(u'<p>This repo is brand new!</p>', output.data)
  4443. self.assertNotIn(
  4444. u'href="/test/diff/master..master">Open Pull-Request',
  4445. output.data)
  4446. def test_open_pr_button(self):
  4447. """ Test "Open Pull-Request" button on non-empty project. """
  4448. tests.create_projects(self.session)
  4449. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  4450. path = os.path.join(self.path, 'repos', 'test.git')
  4451. tests.add_content_git_repo(path)
  4452. output = self.app.get('/test')
  4453. self.assertEqual(output.status_code, 200)
  4454. self.assertNotIn(u'<p>This repo is brand new!</p>', output.data)
  4455. self.assertIn(
  4456. u'href="/test/diff/master..master">Open Pull-Request',
  4457. output.data)
  4458. if __name__ == '__main__':
  4459. unittest.main(verbosity=2)