test_pagure_flask_ui_repo.py 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - 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 shutil
  13. import sys
  14. import tempfile
  15. import os
  16. import pygit2
  17. from mock import patch
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib
  21. import tests
  22. from pagure.lib.repo import PagureRepo
  23. class PagureFlaskRepotests(tests.Modeltests):
  24. """ Tests for flask app controller of pagure """
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskRepotests, self).setUp()
  28. pagure.APP.config['TESTING'] = True
  29. pagure.SESSION = self.session
  30. pagure.ui.SESSION = self.session
  31. pagure.ui.app.SESSION = self.session
  32. pagure.ui.filters.SESSION = self.session
  33. pagure.ui.repo.SESSION = self.session
  34. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  35. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  36. tests.HERE, 'forks')
  37. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  38. tests.HERE, 'requests')
  39. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  40. tests.HERE, 'tickets')
  41. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  42. tests.HERE, 'docs')
  43. pagure.APP.config['UPLOAD_FOLDER_PATH'] = os.path.join(
  44. tests.HERE, 'releases')
  45. self.app = pagure.APP.test_client()
  46. @patch('pagure.ui.repo.admin_session_timedout')
  47. def test_add_user_when_user_mngt_off(self, ast):
  48. """ Test the add_user endpoint when user management is turned off
  49. in the pagure instance """
  50. pagure.APP.config['ENABLE_USER_MNGT'] = False
  51. ast.return_value = False
  52. output = self.app.get('/foo/adduser')
  53. self.assertEqual(output.status_code, 302)
  54. user = tests.FakeUser(username='pingou')
  55. with tests.user_set(pagure.APP, user):
  56. tests.create_projects(self.session)
  57. output = self.app.get('/test/adduser')
  58. self.assertEqual(output.status_code, 404)
  59. #just get the csrf token
  60. pagure.APP.config['ENABLE_USER_MNGT'] = True
  61. output = self.app.get('/test/adduser')
  62. csrf_token = output.data.split(
  63. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  64. pagure.APP.config['ENABLE_USER_MNGT'] = False
  65. data = {
  66. 'user': 'ralph',
  67. }
  68. output = self.app.post('/test/adduser', data=data)
  69. self.assertEqual(output.status_code, 404)
  70. data['csrf_token'] = csrf_token
  71. output = self.app.post('/test/adduser', data=data)
  72. self.assertEqual(output.status_code, 404)
  73. data['user'] = 'foo'
  74. tests.create_projects_git(tests.HERE)
  75. output = self.app.post(
  76. '/test/adduser', data=data, follow_redirects=True)
  77. self.assertEqual(output.status_code, 404)
  78. pagure.APP.config['ENABLE_USER_MNGT'] = True
  79. @patch('pagure.ui.repo.admin_session_timedout')
  80. def test_add_user(self, ast):
  81. """ Test the add_user endpoint. """
  82. ast.return_value = False
  83. output = self.app.get('/foo/adduser')
  84. self.assertEqual(output.status_code, 302)
  85. user = tests.FakeUser()
  86. with tests.user_set(pagure.APP, user):
  87. output = self.app.get('/foo/adduser')
  88. self.assertEqual(output.status_code, 404)
  89. tests.create_projects(self.session)
  90. output = self.app.get('/test/adduser')
  91. self.assertEqual(output.status_code, 403)
  92. ast.return_value = True
  93. output = self.app.get('/test/adduser')
  94. self.assertEqual(output.status_code, 302)
  95. # Redirect also happens for POST request
  96. output = self.app.post('/test/adduser')
  97. self.assertEqual(output.status_code, 302)
  98. # Need to do this un-authentified since our fake user isn't in the DB
  99. # Check the message flashed during the redirect
  100. output = self.app.get('/')
  101. self.assertEqual(output.status_code, 200)
  102. self.assertIn(
  103. '</button>\n Action canceled, try it '
  104. 'again',output.data)
  105. ast.return_value = False
  106. user.username = 'pingou'
  107. with tests.user_set(pagure.APP, user):
  108. output = self.app.get('/test/adduser')
  109. self.assertEqual(output.status_code, 200)
  110. self.assertIn('<strong>Add user to the', output.data)
  111. csrf_token = output.data.split(
  112. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  113. data = {
  114. 'user': 'ralph',
  115. }
  116. output = self.app.post('/test/adduser', data=data)
  117. self.assertEqual(output.status_code, 200)
  118. self.assertTrue('<strong>Add user to the' in output.data)
  119. data['csrf_token'] = csrf_token
  120. output = self.app.post('/test/adduser', data=data)
  121. self.assertEqual(output.status_code, 200)
  122. self.assertIn('<strong>Add user to the', output.data)
  123. self.assertIn(
  124. '</button>\n No user &#34;ralph&#34; '
  125. 'found', output.data)
  126. data['user'] = 'foo'
  127. tests.create_projects_git(tests.HERE)
  128. output = self.app.post(
  129. '/test/adduser', data=data, follow_redirects=True)
  130. self.assertEqual(output.status_code, 200)
  131. self.assertIn(
  132. '<title>Settings - test - Pagure</title>', output.data)
  133. self.assertIn('<h3>Settings for test</h3>', output.data)
  134. self.assertIn(
  135. '</button>\n User added', output.data)
  136. @patch('pagure.ui.repo.admin_session_timedout')
  137. def test_add_group_project_when_user_mngt_off(self, ast):
  138. """ Test the add_group_project endpoint when user management is
  139. turned off in the pagure instance"""
  140. pagure.APP.config['ENABLE_USER_MNGT'] = False
  141. ast.return_value = False
  142. output = self.app.get('/foo/addgroup')
  143. self.assertEqual(output.status_code, 302)
  144. msg = pagure.lib.add_group(
  145. self.session,
  146. group_name='foo',
  147. group_type='bar',
  148. user='pingou',
  149. is_admin=False,
  150. blacklist=pagure.APP.config['BLACKLISTED_GROUPS'],
  151. )
  152. self.session.commit()
  153. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  154. user = tests.FakeUser(username='pingou')
  155. with tests.user_set(pagure.APP, user):
  156. output = self.app.get('/test/addgroup')
  157. self.assertEqual(output.status_code, 404)
  158. #just get the csrf token
  159. pagure.APP.config['ENABLE_USER_MNGT'] = True
  160. tests.create_projects(self.session)
  161. output = self.app.get('/test/addgroup')
  162. csrf_token = output.data.split(
  163. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  164. pagure.APP.config['ENABLE_USER_MNGT'] = False
  165. data = {
  166. 'group': 'ralph',
  167. }
  168. output = self.app.post('/test/addgroup', data=data)
  169. self.assertEqual(output.status_code, 404)
  170. data['csrf_token'] = csrf_token
  171. output = self.app.post('/test/addgroup', data=data)
  172. self.assertEqual(output.status_code, 404)
  173. data['group'] = 'foo'
  174. tests.create_projects_git(tests.HERE)
  175. output = self.app.post(
  176. '/test/addgroup', data=data, follow_redirects=True)
  177. self.assertEqual(output.status_code, 404)
  178. pagure.APP.config['ENABLE_USER_MNGT'] = True
  179. @patch('pagure.ui.repo.admin_session_timedout')
  180. def test_add_group_project(self, ast):
  181. """ Test the add_group_project endpoint. """
  182. ast.return_value = False
  183. output = self.app.get('/foo/addgroup')
  184. self.assertEqual(output.status_code, 302)
  185. user = tests.FakeUser()
  186. with tests.user_set(pagure.APP, user):
  187. output = self.app.get('/foo/addgroup')
  188. self.assertEqual(output.status_code, 404)
  189. tests.create_projects(self.session)
  190. output = self.app.get('/test/addgroup')
  191. self.assertEqual(output.status_code, 403)
  192. ast.return_value = True
  193. output = self.app.get('/test/addgroup')
  194. self.assertEqual(output.status_code, 302)
  195. # Redirect also happens for POST request
  196. output = self.app.post('/test/addgroup')
  197. self.assertEqual(output.status_code, 302)
  198. # Need to do this un-authentified since our fake user isn't in the DB
  199. # Check the message flashed during the redirect
  200. output = self.app.get('/')
  201. self.assertEqual(output.status_code, 200)
  202. self.assertIn(
  203. '</button>\n Action canceled, try it '
  204. 'again', output.data)
  205. ast.return_value = False
  206. msg = pagure.lib.add_group(
  207. self.session,
  208. group_name='foo',
  209. group_type='bar',
  210. user='pingou',
  211. is_admin=False,
  212. blacklist=pagure.APP.config['BLACKLISTED_GROUPS'],
  213. )
  214. self.session.commit()
  215. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  216. user.username = 'pingou'
  217. with tests.user_set(pagure.APP, user):
  218. output = self.app.get('/test/addgroup')
  219. self.assertEqual(output.status_code, 200)
  220. self.assertTrue('<strong>Add group to the' in output.data)
  221. csrf_token = output.data.split(
  222. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  223. data = {
  224. 'group': 'ralph',
  225. }
  226. output = self.app.post('/test/addgroup', data=data)
  227. self.assertEqual(output.status_code, 200)
  228. self.assertTrue('<strong>Add group to the' in output.data)
  229. data['csrf_token'] = csrf_token
  230. output = self.app.post('/test/addgroup', data=data)
  231. self.assertEqual(output.status_code, 200)
  232. self.assertTrue('<strong>Add group to the' in output.data)
  233. self.assertIn(
  234. '</button>\n No group ralph found.',
  235. output.data)
  236. data['group'] = 'foo'
  237. tests.create_projects_git(tests.HERE)
  238. output = self.app.post(
  239. '/test/addgroup', data=data, follow_redirects=True)
  240. self.assertEqual(output.status_code, 200)
  241. self.assertIn(
  242. '<title>Settings - test - Pagure</title>', output.data)
  243. self.assertIn('<h3>Settings for test</h3>', output.data)
  244. self.assertIn(
  245. '</button>\n Group added', output.data)
  246. @patch('pagure.ui.repo.admin_session_timedout')
  247. def test_remove_user_when_user_mngt_off(self, ast):
  248. """ Test the remove_user endpoint when user management is turned
  249. off in the pagure instance"""
  250. pagure.APP.config['ENABLE_USER_MNGT'] = False
  251. ast.return_value = False
  252. output = self.app.post('/foo/dropuser/1')
  253. self.assertEqual(output.status_code, 302)
  254. user = tests.FakeUser(username='pingou')
  255. with tests.user_set(pagure.APP, user):
  256. tests.create_projects(self.session)
  257. tests.create_projects_git(tests.HERE)
  258. output = self.app.post('/test/settings')
  259. csrf_token = output.data.split(
  260. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  261. data = {'csrf_token': csrf_token}
  262. output = self.app.post(
  263. '/test/dropuser/2', data=data, follow_redirects=True)
  264. self.assertEqual(output.status_code, 404)
  265. # Add an user to a project
  266. repo = pagure.lib.get_project(self.session, 'test')
  267. msg = pagure.lib.add_user_to_project(
  268. session=self.session,
  269. project=repo,
  270. new_user='foo',
  271. user='pingou',
  272. )
  273. self.session.commit()
  274. self.assertEqual(msg, 'User added')
  275. with tests.user_set(pagure.APP, user):
  276. output = self.app.post('/test/dropuser/2', follow_redirects=True)
  277. self.assertEqual(output.status_code, 404)
  278. data = {'csrf_token': csrf_token}
  279. output = self.app.post(
  280. '/test/dropuser/2', data=data, follow_redirects=True)
  281. self.assertEqual(output.status_code, 404)
  282. pagure.APP.config['ENABLE_USER_MNGT'] = True
  283. @patch('pagure.ui.repo.admin_session_timedout')
  284. def test_remove_user(self, ast):
  285. """ Test the remove_user endpoint. """
  286. ast.return_value = False
  287. output = self.app.post('/foo/dropuser/1')
  288. self.assertEqual(output.status_code, 302)
  289. user = tests.FakeUser()
  290. with tests.user_set(pagure.APP, user):
  291. output = self.app.post('/foo/dropuser/1')
  292. self.assertEqual(output.status_code, 404)
  293. tests.create_projects(self.session)
  294. output = self.app.post('/test/dropuser/1')
  295. self.assertEqual(output.status_code, 403)
  296. ast.return_value = True
  297. output = self.app.post('/test/dropuser/1')
  298. self.assertEqual(output.status_code, 302)
  299. ast.return_value = False
  300. user.username = 'pingou'
  301. with tests.user_set(pagure.APP, user):
  302. tests.create_projects_git(tests.HERE)
  303. output = self.app.post('/test/settings')
  304. csrf_token = output.data.split(
  305. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  306. data = {'csrf_token': csrf_token}
  307. output = self.app.post(
  308. '/test/dropuser/2', data=data, follow_redirects=True)
  309. self.assertEqual(output.status_code, 200)
  310. self.assertIn(
  311. '<title>Settings - test - Pagure</title>', output.data)
  312. self.assertIn('<h3>Settings for test</h3>', output.data)
  313. self.assertIn(
  314. '</button>\n User does not have commit or cannot '
  315. 'loose it right', output.data)
  316. # Add an user to a project
  317. repo = pagure.lib.get_project(self.session, 'test')
  318. msg = pagure.lib.add_user_to_project(
  319. session=self.session,
  320. project=repo,
  321. new_user='foo',
  322. user='pingou',
  323. )
  324. self.session.commit()
  325. self.assertEqual(msg, 'User added')
  326. with tests.user_set(pagure.APP, user):
  327. output = self.app.post('/test/dropuser/2', follow_redirects=True)
  328. self.assertEqual(output.status_code, 200)
  329. self.assertIn(
  330. '<title>Settings - test - Pagure</title>', output.data)
  331. self.assertIn('<h3>Settings for test</h3>', output.data)
  332. self.assertNotIn(
  333. '</button>\n User removed', output.data)
  334. data = {'csrf_token': csrf_token}
  335. output = self.app.post(
  336. '/test/dropuser/2', data=data, follow_redirects=True)
  337. self.assertEqual(output.status_code, 200)
  338. self.assertIn(
  339. '<title>Settings - test - Pagure</title>', output.data)
  340. self.assertIn('<h3>Settings for test</h3>', output.data)
  341. self.assertIn(
  342. '</button>\n User removed', output.data)
  343. @patch('pagure.ui.repo.admin_session_timedout')
  344. def test_remove_group_project_when_user_mngt_off(self, ast):
  345. """ Test the remove_group_project endpoint when user management is
  346. turned off in the pagure instance"""
  347. pagure.APP.config['ENABLE_USER_MNGT'] = False
  348. ast.return_value = False
  349. output = self.app.post('/foo/dropgroup/1')
  350. self.assertEqual(output.status_code, 302)
  351. user = tests.FakeUser()
  352. with tests.user_set(pagure.APP, user):
  353. tests.create_projects(self.session)
  354. user.username = 'pingou'
  355. with tests.user_set(pagure.APP, user):
  356. tests.create_projects_git(tests.HERE)
  357. output = self.app.post('/test/settings')
  358. csrf_token = output.data.split(
  359. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  360. data = {'csrf_token': csrf_token}
  361. output = self.app.post(
  362. '/test/dropgroup/2', data=data, follow_redirects=True)
  363. self.assertEqual(output.status_code, 404)
  364. # Create the new group
  365. msg = pagure.lib.add_group(
  366. session=self.session,
  367. group_name='testgrp',
  368. group_type='user',
  369. user='pingou',
  370. is_admin=False,
  371. blacklist=[],
  372. )
  373. self.assertEqual(msg, 'User `pingou` added to the group `testgrp`.')
  374. self.session.commit()
  375. repo = pagure.lib.get_project(self.session, 'test')
  376. # Add the group to a project
  377. msg = pagure.lib.add_group_to_project(
  378. session=self.session,
  379. project=repo,
  380. new_group='testgrp',
  381. user='pingou',
  382. )
  383. self.session.commit()
  384. self.assertEqual(msg, 'Group added')
  385. with tests.user_set(pagure.APP, user):
  386. output = self.app.post('/test/dropgroup/1', follow_redirects=True)
  387. self.assertEqual(output.status_code, 404)
  388. data = {'csrf_token': csrf_token}
  389. output = self.app.post(
  390. '/test/dropgroup/1', data=data, follow_redirects=True)
  391. self.assertEqual(output.status_code, 404)
  392. pagure.APP.config['ENABLE_USER_MNGT'] = True
  393. @patch('pagure.ui.repo.admin_session_timedout')
  394. def test_remove_group_project(self, ast):
  395. """ Test the remove_group_project endpoint. """
  396. ast.return_value = False
  397. output = self.app.post('/foo/dropgroup/1')
  398. self.assertEqual(output.status_code, 302)
  399. user = tests.FakeUser()
  400. with tests.user_set(pagure.APP, user):
  401. output = self.app.post('/foo/dropgroup/1')
  402. self.assertEqual(output.status_code, 404)
  403. tests.create_projects(self.session)
  404. output = self.app.post('/test/dropgroup/1')
  405. self.assertEqual(output.status_code, 403)
  406. ast.return_value = True
  407. output = self.app.post('/test/dropgroup/1')
  408. self.assertEqual(output.status_code, 302)
  409. ast.return_value = False
  410. user.username = 'pingou'
  411. with tests.user_set(pagure.APP, user):
  412. tests.create_projects_git(tests.HERE)
  413. output = self.app.post('/test/settings')
  414. csrf_token = output.data.split(
  415. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  416. data = {'csrf_token': csrf_token}
  417. output = self.app.post(
  418. '/test/dropgroup/2', data=data, follow_redirects=True)
  419. self.assertEqual(output.status_code, 200)
  420. self.assertIn(
  421. '<title>Settings - test - Pagure</title>', output.data)
  422. self.assertIn('<h3>Settings for test</h3>', output.data)
  423. self.assertIn(
  424. '</button>\n '
  425. 'Group does not seem to be part of this project',
  426. output.data)
  427. # Create the new group
  428. msg = pagure.lib.add_group(
  429. session=self.session,
  430. group_name='testgrp',
  431. group_type='user',
  432. user='pingou',
  433. is_admin=False,
  434. blacklist=[],
  435. )
  436. self.assertEqual(msg, 'User `pingou` added to the group `testgrp`.')
  437. self.session.commit()
  438. repo = pagure.lib.get_project(self.session, 'test')
  439. # Add the group to a project
  440. msg = pagure.lib.add_group_to_project(
  441. session=self.session,
  442. project=repo,
  443. new_group='testgrp',
  444. user='pingou',
  445. )
  446. self.session.commit()
  447. self.assertEqual(msg, 'Group added')
  448. with tests.user_set(pagure.APP, user):
  449. output = self.app.post('/test/dropgroup/1', follow_redirects=True)
  450. self.assertEqual(output.status_code, 200)
  451. self.assertIn(
  452. '<title>Settings - test - Pagure</title>', output.data)
  453. self.assertIn('<h3>Settings for test</h3>', output.data)
  454. self.assertNotIn(
  455. '</button>\n Group removed',
  456. output.data)
  457. data = {'csrf_token': csrf_token}
  458. output = self.app.post(
  459. '/test/dropgroup/1', data=data, follow_redirects=True)
  460. self.assertEqual(output.status_code, 200)
  461. self.assertIn(
  462. '<title>Settings - test - Pagure</title>', output.data)
  463. self.assertIn('<h3>Settings for test</h3>', output.data)
  464. self.assertIn(
  465. '</button>\n Group removed',
  466. output.data)
  467. @patch('pagure.ui.repo.admin_session_timedout')
  468. def test_update_project(self, ast):
  469. """ Test the update_project endpoint. """
  470. ast.return_value = True
  471. output = self.app.post('/foo/update')
  472. self.assertEqual(output.status_code, 302)
  473. user = tests.FakeUser()
  474. with tests.user_set(pagure.APP, user):
  475. output = self.app.post('/foo/update')
  476. self.assertEqual(output.status_code, 302)
  477. ast.return_value = False
  478. output = self.app.post('/foo/update')
  479. self.assertEqual(output.status_code, 404)
  480. tests.create_projects(self.session)
  481. output = self.app.post('/test/update')
  482. self.assertEqual(output.status_code, 403)
  483. user.username = 'pingou'
  484. with tests.user_set(pagure.APP, user):
  485. tests.create_projects_git(tests.HERE)
  486. output = self.app.post('/test/update', follow_redirects=True)
  487. self.assertEqual(output.status_code, 200)
  488. self.assertIn(
  489. '<title>Settings - test - Pagure</title>', output.data)
  490. self.assertIn('<h3>Settings for test</h3>', output.data)
  491. csrf_token = output.data.split(
  492. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  493. data = {
  494. 'description': 'new description for test project #1',
  495. 'csrf_token': csrf_token,
  496. }
  497. output = self.app.post(
  498. '/test/update', data=data, follow_redirects=True)
  499. self.assertEqual(output.status_code, 200)
  500. self.assertIn(
  501. '<title>Settings - test - Pagure</title>', output.data)
  502. self.assertIn('<h3>Settings for test</h3>', output.data)
  503. self.assertIn(
  504. '<input class="form-control" name="avatar_email" value="" />', output.data)
  505. self.assertIn(
  506. '</button>\n Project updated',
  507. output.data)
  508. # Edit the avatar_email
  509. data = {
  510. 'description': 'new description for test project #1',
  511. 'avatar_email': 'pingou@fp.o',
  512. 'csrf_token': csrf_token,
  513. }
  514. output = self.app.post(
  515. '/test/update', data=data, follow_redirects=True)
  516. self.assertEqual(output.status_code, 200)
  517. self.assertIn(
  518. '<title>Settings - test - Pagure</title>', output.data)
  519. self.assertIn('<h3>Settings for test</h3>', output.data)
  520. self.assertIn(
  521. '<input class="form-control" name="avatar_email" value="pingou@fp.o" />',
  522. output.data)
  523. self.assertIn(
  524. '</button>\n Project updated',
  525. output.data)
  526. # Reset the avatar_email
  527. data = {
  528. 'description': 'new description for test project #1',
  529. 'avatar_email': '',
  530. 'csrf_token': csrf_token,
  531. }
  532. output = self.app.post(
  533. '/test/update', data=data, follow_redirects=True)
  534. self.assertEqual(output.status_code, 200)
  535. self.assertIn(
  536. '<title>Settings - test - Pagure</title>', output.data)
  537. self.assertIn('<h3>Settings for test</h3>', output.data)
  538. self.assertIn(
  539. '<input class="form-control" name="avatar_email" value="" />', output.data)
  540. self.assertIn(
  541. '</button>\n Project updated',
  542. output.data)
  543. @patch('pagure.ui.repo.admin_session_timedout')
  544. def test_view_settings(self, ast):
  545. """ Test the view_settings endpoint. """
  546. ast.return_value = False
  547. output = self.app.get('/foo/settings')
  548. self.assertEqual(output.status_code, 302)
  549. user = tests.FakeUser()
  550. with tests.user_set(pagure.APP, user):
  551. output = self.app.get('/foo/settings')
  552. self.assertEqual(output.status_code, 404)
  553. tests.create_projects(self.session)
  554. tests.create_projects_git(tests.HERE)
  555. output = self.app.get('/test/settings')
  556. self.assertEqual(output.status_code, 403)
  557. user.username = 'pingou'
  558. with tests.user_set(pagure.APP, user):
  559. ast.return_value = True
  560. output = self.app.get('/test/settings')
  561. self.assertEqual(output.status_code, 302)
  562. ast.return_value = False
  563. output = self.app.get('/test/settings')
  564. self.assertEqual(output.status_code, 200)
  565. self.assertIn(
  566. '<title>Settings - test - Pagure</title>', output.data)
  567. self.assertIn('<h3>Settings for test</h3>', output.data)
  568. # Both checkbox checked before
  569. self.assertIn(
  570. '<input id="pull_requests" type="checkbox" value="y" '
  571. 'name="pull_requests" checked=""/>', output.data)
  572. self.assertIn(
  573. '<input id="issue_tracker" type="checkbox" value="y" '
  574. 'name="issue_tracker" checked=""/>', output.data)
  575. csrf_token = output.data.split(
  576. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  577. data = {}
  578. output = self.app.post(
  579. '/test/settings', data=data, follow_redirects=True)
  580. self.assertEqual(output.status_code, 200)
  581. self.assertIn(
  582. '<title>Settings - test - Pagure</title>', output.data)
  583. self.assertIn('<h3>Settings for test</h3>', output.data)
  584. # Both checkbox are still checked
  585. output = self.app.get('/test/settings', follow_redirects=True)
  586. self.assertEqual(output.status_code, 200)
  587. self.assertIn(
  588. '<title>Settings - test - Pagure</title>', output.data)
  589. self.assertIn('<h3>Settings for test</h3>', output.data)
  590. self.assertIn(
  591. '<input id="pull_requests" type="checkbox" value="y" '
  592. 'name="pull_requests" checked=""/>', output.data)
  593. self.assertIn(
  594. '<input id="issue_tracker" type="checkbox" value="y" '
  595. 'name="issue_tracker" checked=""/>', output.data)
  596. data = {'csrf_token': csrf_token}
  597. output = self.app.post(
  598. '/test/settings', data=data, follow_redirects=True)
  599. self.assertEqual(output.status_code, 200)
  600. self.assertIn(
  601. '<title>Overview - test - Pagure</title>', output.data)
  602. self.assertIn(
  603. '<div class="projectinfo m-t-1 m-b-1">\n'
  604. 'test project #1 </div>', output.data)
  605. self.assertIn(
  606. '</button>\n Edited successfully '
  607. 'settings of repo: test', output.data)
  608. # Both checkbox are now un-checked
  609. output = self.app.get('/test/settings', follow_redirects=True)
  610. self.assertEqual(output.status_code, 200)
  611. self.assertIn(
  612. '<title>Settings - test - Pagure</title>', output.data)
  613. self.assertIn('<h3>Settings for test</h3>', output.data)
  614. self.assertIn(
  615. '<input id="pull_requests" type="checkbox" value="y" '
  616. 'name="pull_requests" />', output.data)
  617. self.assertIn(
  618. '<input id="issue_tracker" type="checkbox" value="y" '
  619. 'name="issue_tracker" />', output.data)
  620. data = {
  621. 'csrf_token': csrf_token,
  622. 'pull_requests': 'y',
  623. 'issue_tracker': 'y',
  624. }
  625. output = self.app.post(
  626. '/test/settings', data=data, follow_redirects=True)
  627. self.assertEqual(output.status_code, 200)
  628. self.assertIn(
  629. '<title>Overview - test - Pagure</title>', output.data)
  630. self.assertIn(
  631. '<div class="projectinfo m-t-1 m-b-1">\n'
  632. 'test project #1 </div>', output.data)
  633. self.assertIn(
  634. '</button>\n Edited successfully '
  635. 'settings of repo: test', output.data)
  636. # Both checkbox are again checked
  637. output = self.app.get('/test/settings', follow_redirects=True)
  638. self.assertEqual(output.status_code, 200)
  639. self.assertIn(
  640. '<title>Settings - test - Pagure</title>', output.data)
  641. self.assertIn('<h3>Settings for test</h3>', output.data)
  642. self.assertIn(
  643. '<input id="pull_requests" type="checkbox" value="y" '
  644. 'name="pull_requests" checked=""/>', output.data)
  645. self.assertIn(
  646. '<input id="issue_tracker" type="checkbox" value="y" '
  647. 'name="issue_tracker" checked=""/>', output.data)
  648. def test_view_forks(self):
  649. """ Test the view_forks endpoint. """
  650. output = self.app.get('/foo/forks', follow_redirects=True)
  651. self.assertEqual(output.status_code, 404)
  652. tests.create_projects(self.session)
  653. tests.create_projects_git(tests.HERE, bare=True)
  654. output = self.app.get('/test/forks', follow_redirects=True)
  655. self.assertEqual(output.status_code, 200)
  656. self.assertTrue('This project has not been forked.' in output.data)
  657. def test_view_repo(self):
  658. """ Test the view_repo endpoint. """
  659. output = self.app.get('/foo')
  660. # No project registered in the DB
  661. self.assertEqual(output.status_code, 404)
  662. tests.create_projects(self.session)
  663. output = self.app.get('/test')
  664. # No git repo associated
  665. self.assertEqual(output.status_code, 404)
  666. tests.create_projects_git(tests.HERE, bare=True)
  667. output = self.app.get('/test')
  668. self.assertEqual(output.status_code, 200)
  669. self.assertTrue('<p>This repo is brand new!</p>' in output.data)
  670. self.assertIn(
  671. '<div class="projectinfo m-t-1 m-b-1">\n'
  672. 'test project #1 </div>', output.data)
  673. output = self.app.get('/test/')
  674. self.assertEqual(output.status_code, 200)
  675. self.assertTrue('<p>This repo is brand new!</p>' in output.data)
  676. self.assertIn(
  677. '<div class="projectinfo m-t-1 m-b-1">\n'
  678. 'test project #1 </div>', output.data)
  679. # Add some content to the git repo
  680. tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
  681. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  682. output = self.app.get('/test')
  683. self.assertEqual(output.status_code, 200)
  684. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  685. self.assertFalse('Forked from' in output.data)
  686. self.assertIn(
  687. '<div class="projectinfo m-t-1 m-b-1">\n'
  688. 'test project #1 </div>', output.data)
  689. # Turn that repo into a fork
  690. repo = pagure.lib.get_project(self.session, 'test')
  691. repo.parent_id = 2
  692. self.session.add(repo)
  693. self.session.commit()
  694. # View the repo in the UI
  695. output = self.app.get('/test')
  696. self.assertEqual(output.status_code, 404)
  697. # Add some content to the git repo
  698. tests.add_content_git_repo(
  699. os.path.join(tests.HERE, 'forks', 'pingou', 'test.git'))
  700. tests.add_readme_git_repo(
  701. os.path.join(tests.HERE, 'forks', 'pingou', 'test.git'))
  702. output = self.app.get('/fork/pingou/test')
  703. self.assertEqual(output.status_code, 200)
  704. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  705. self.assertIn(
  706. '<div class="projectinfo m-t-1 m-b-1">\n'
  707. 'test project #1 </div>', output.data)
  708. self.assertTrue('Forked from' in output.data)
  709. # Add a fork of a fork
  710. item = pagure.lib.model.Project(
  711. user_id=1, # pingou
  712. name='test3',
  713. description='test project #3',
  714. parent_id=1,
  715. hook_token='aaabbbmmm',
  716. )
  717. self.session.add(item)
  718. self.session.commit()
  719. tests.add_content_git_repo(
  720. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  721. tests.add_readme_git_repo(
  722. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  723. tests.add_commit_git_repo(
  724. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'),
  725. ncommits=10)
  726. output = self.app.get('/fork/pingou/test3')
  727. self.assertEqual(output.status_code, 200)
  728. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  729. self.assertIn(
  730. '<div class="projectinfo m-t-1 m-b-1">\n'
  731. 'test project #3 </div>', output.data)
  732. self.assertTrue('Forked from' in output.data)
  733. def test_view_repo_empty(self):
  734. """ Test the view_repo endpoint on a repo w/o master branch. """
  735. tests.create_projects(self.session)
  736. # Create a git repo to play with
  737. gitrepo = os.path.join(tests.HERE, 'test.git')
  738. pygit2.init_repository(gitrepo, bare=True)
  739. # Create a fork of this repo
  740. newpath = tempfile.mkdtemp(prefix='pagure-viewrepo-test')
  741. new_repo = pygit2.clone_repository(gitrepo, newpath)
  742. # Edit the sources file again
  743. with open(os.path.join(newpath, 'sources'), 'w') as stream:
  744. stream.write('foo\n bar\nbaz\n boose')
  745. new_repo.index.add('sources')
  746. new_repo.index.write()
  747. # Commits the files added
  748. tree = new_repo.index.write_tree()
  749. author = pygit2.Signature(
  750. 'Alice Author', 'alice@authors.tld')
  751. committer = pygit2.Signature(
  752. 'Cecil Committer', 'cecil@committers.tld')
  753. new_repo.create_commit(
  754. 'refs/heads/feature',
  755. author,
  756. committer,
  757. 'A commit on branch feature',
  758. tree,
  759. []
  760. )
  761. refname = 'refs/heads/feature'
  762. ori_remote = new_repo.remotes[0]
  763. PagureRepo.push(ori_remote, refname)
  764. output = self.app.get('/test')
  765. self.assertEqual(output.status_code, 200)
  766. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  767. self.assertFalse('Forked from' in output.data)
  768. self.assertIn(
  769. '<div class="projectinfo m-t-1 m-b-1">\n'
  770. 'test project #1 </div>', output.data)
  771. self.assertEqual(
  772. output.data.count('<span class="commitid">'), 0)
  773. shutil.rmtree(newpath)
  774. def test_view_repo_branch(self):
  775. """ Test the view_repo_branch endpoint. """
  776. output = self.app.get('/foo/branch/master')
  777. # No project registered in the DB
  778. self.assertEqual(output.status_code, 404)
  779. tests.create_projects(self.session)
  780. output = self.app.get('/test/branch/master')
  781. # No git repo associated
  782. self.assertEqual(output.status_code, 404)
  783. tests.create_projects_git(tests.HERE, bare=True)
  784. output = self.app.get('/test/branch/master')
  785. self.assertEqual(output.status_code, 404)
  786. # Add some content to the git repo
  787. tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
  788. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  789. output = self.app.get('/test/branch/master')
  790. self.assertEqual(output.status_code, 200)
  791. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  792. self.assertFalse('Forked from' in output.data)
  793. self.assertIn(
  794. '<div class="projectinfo m-t-1 m-b-1">\n'
  795. 'test project #1 </div>', output.data)
  796. # Turn that repo into a fork
  797. repo = pagure.lib.get_project(self.session, 'test')
  798. repo.parent_id = 2
  799. self.session.add(repo)
  800. self.session.commit()
  801. # View the repo in the UI
  802. output = self.app.get('/test/branch/master')
  803. self.assertEqual(output.status_code, 404)
  804. # Add some content to the git repo
  805. tests.add_content_git_repo(
  806. os.path.join(tests.HERE, 'forks', 'pingou', 'test.git'))
  807. tests.add_readme_git_repo(
  808. os.path.join(tests.HERE, 'forks', 'pingou', 'test.git'))
  809. output = self.app.get('/fork/pingou/test/branch/master')
  810. self.assertEqual(output.status_code, 200)
  811. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  812. self.assertIn(
  813. '<div class="projectinfo m-t-1 m-b-1">\n'
  814. 'test project #1 </div>', output.data)
  815. self.assertTrue('Forked from' in output.data)
  816. # Add a fork of a fork
  817. item = pagure.lib.model.Project(
  818. user_id=1, # pingou
  819. name='test3',
  820. description='test project #3',
  821. parent_id=1,
  822. hook_token='aaabbbnnn',
  823. )
  824. self.session.add(item)
  825. self.session.commit()
  826. tests.add_content_git_repo(
  827. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  828. tests.add_readme_git_repo(
  829. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  830. tests.add_commit_git_repo(
  831. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'),
  832. ncommits=10)
  833. output = self.app.get('/fork/pingou/test3/branch/master')
  834. self.assertEqual(output.status_code, 200)
  835. self.assertFalse('<p>This repo is brand new!</p>' in output.data)
  836. self.assertIn(
  837. '<div class="projectinfo m-t-1 m-b-1">\n'
  838. 'test project #3 </div>', output.data)
  839. self.assertTrue('Forked from' in output.data)
  840. def test_view_commits(self):
  841. """ Test the view_commits endpoint. """
  842. output = self.app.get('/foo/commits')
  843. # No project registered in the DB
  844. self.assertEqual(output.status_code, 404)
  845. tests.create_projects(self.session)
  846. output = self.app.get('/test/commits')
  847. # No git repo associated
  848. self.assertEqual(output.status_code, 404)
  849. tests.create_projects_git(tests.HERE, bare=True)
  850. output = self.app.get('/test/commits')
  851. self.assertEqual(output.status_code, 200)
  852. self.assertIn('<p>This repo is brand new!</p>', output.data)
  853. self.assertIn(
  854. '<div class="projectinfo m-t-1 m-b-1">\n'
  855. 'test project #1 </div>', output.data)
  856. # Add some content to the git repo
  857. tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
  858. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  859. output = self.app.get('/test/commits')
  860. self.assertEqual(output.status_code, 200)
  861. self.assertNotIn('<p>This repo is brand new!</p>', output.data)
  862. self.assertNotIn('Forked from', output.data)
  863. self.assertIn(
  864. '<div class="projectinfo m-t-1 m-b-1">\n'
  865. 'test project #1 </div>', output.data)
  866. self.assertIn('<title>Commits - test - Pagure</title>', output.data)
  867. output = self.app.get('/test/commits/master')
  868. self.assertEqual(output.status_code, 200)
  869. self.assertNotIn('<p>This repo is brand new!</p>', output.data)
  870. self.assertNotIn('Forked from', output.data)
  871. self.assertIn(
  872. '<div class="projectinfo m-t-1 m-b-1">\n'
  873. 'test project #1 </div>', output.data)
  874. # Turn that repo into a fork
  875. repo = pagure.lib.get_project(self.session, 'test')
  876. repo.parent_id = 2
  877. self.session.add(repo)
  878. self.session.commit()
  879. # View the repo in the UI
  880. output = self.app.get('/test/commits')
  881. self.assertEqual(output.status_code, 404)
  882. # Add some content to the git repo
  883. tests.add_content_git_repo(
  884. os.path.join(tests.HERE, 'forks', 'pingou', 'test.git'))
  885. tests.add_readme_git_repo(
  886. os.path.join(tests.HERE, 'forks', 'pingou', 'test.git'))
  887. output = self.app.get('/fork/pingou/test/commits?page=abc')
  888. self.assertEqual(output.status_code, 200)
  889. self.assertNotIn('<p>This repo is brand new!</p>', output.data)
  890. self.assertIn(
  891. '<div class="projectinfo m-t-1 m-b-1">\n'
  892. 'test project #1 </div>', output.data)
  893. self.assertIn('Forked from', output.data)
  894. # Add a fork of a fork
  895. item = pagure.lib.model.Project(
  896. user_id=1, # pingou
  897. name='test3',
  898. description='test project #3',
  899. parent_id=1,
  900. hook_token='aaabbbooo',
  901. )
  902. self.session.add(item)
  903. self.session.commit()
  904. tests.add_content_git_repo(
  905. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  906. tests.add_readme_git_repo(
  907. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  908. tests.add_commit_git_repo(
  909. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'),
  910. ncommits=10)
  911. output = self.app.get('/fork/pingou/test3/commits/fobranch')
  912. self.assertEqual(output.status_code, 404)
  913. output = self.app.get('/fork/pingou/test3/commits')
  914. self.assertEqual(output.status_code, 200)
  915. self.assertNotIn('<p>This repo is brand new!</p>', output.data)
  916. self.assertIn(
  917. '<div class="projectinfo m-t-1 m-b-1">\n'
  918. 'test project #3 </div>', output.data)
  919. self.assertIn('Forked from', output.data)
  920. def test_compare_commits(self):
  921. """ Test the compare_commits endpoint. """
  922. # First two commits comparison
  923. def compare_first_two(c1, c2):
  924. # View commits comparison
  925. output = self.app.get('/test/c/%s..%s' % (c2.oid.hex, c1.oid.hex))
  926. self.assertEqual(output.status_code, 200)
  927. self.assertIn(
  928. '<title>Diff from %s to %s - test - Pagure</title>'
  929. % (c2.oid.hex, c1.oid.hex),
  930. output.data)
  931. self.assertIn(
  932. '<h5 class="text-muted">%s .. %s</h5>'
  933. % (c2.oid.hex, c1.oid.hex),
  934. output.data)
  935. self.assertIn(
  936. '<span class="hidden-sm-down">Commits&nbsp;</span>\n ' +
  937. '<span ' +
  938. 'class="label label-default label-pill hidden-sm-down">' +
  939. '\n 2\n </span>',
  940. output.data)
  941. self.assertIn(
  942. '<span style="color: #800080; font-weight: bold">' +
  943. '@@ -1,2 +1,1 @@</span>',
  944. output.data)
  945. self.assertIn(
  946. '<span style="color: #a40000">- Row 0</span>', output.data)
  947. # View inverse commits comparison
  948. output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c2.oid.hex))
  949. self.assertEqual(output.status_code, 200)
  950. self.assertIn(
  951. '<title>Diff from %s to %s - test - Pagure</title>' %
  952. (c1.oid.hex, c2.oid.hex),
  953. output.data)
  954. self.assertIn(
  955. '<span class="hidden-sm-down">Commits&nbsp;</span>\n ' +
  956. '<span ' +
  957. 'class="label label-default label-pill hidden-sm-down">' +
  958. '\n 2\n </span>',
  959. output.data)
  960. self.assertIn(
  961. '<h5 class="text-muted">%s .. %s</h5>' %
  962. (c1.oid.hex, c2.oid.hex),
  963. output.data)
  964. self.assertIn(
  965. '<span style="color: #800080; font-weight: bold">' +
  966. '@@ -1,1 +1,2 @@</span>',
  967. output.data)
  968. self.assertIn(
  969. '<span style="color: #00A000">+ Row 0</span>',
  970. output.data)
  971. def compare_all(c1, c3):
  972. # View commits comparison
  973. output = self.app.get('/test/c/%s..%s' % (c1.oid.hex, c3.oid.hex))
  974. self.assertEqual(output.status_code, 200)
  975. self.assertIn(
  976. '<title>Diff from %s to %s - test - Pagure</title>' %
  977. (c1.oid.hex, c3.oid.hex), output.data)
  978. self.assertIn(
  979. '<h5 class="text-muted">%s .. %s</h5>' %
  980. (c1.oid.hex, c3.oid.hex),
  981. output.data)
  982. self.assertIn(
  983. '<span style="color: #800080; font-weight: bold">' +
  984. '@@ -1,1 +1,2 @@</span>',
  985. output.data)
  986. self.assertIn(
  987. '<span style="color: #00A000">+ Row 0</span>', output.data)
  988. self.assertEqual(
  989. output.data.count(
  990. '<span style="color: #00A000">+ Row 0</span>'), 2)
  991. self.assertIn(
  992. '<span class="hidden-sm-down">Commits&nbsp;</span>\n ' +
  993. '<span ' +
  994. 'class="label label-default label-pill hidden-sm-down">' +
  995. '\n 3\n </span>',
  996. output.data)
  997. self.assertIn(
  998. 'title="View file as of 4829cf">Šource</a>', output.data)
  999. self.assertIn(
  1000. '<div><small>file added</small></div></h5>', output.data)
  1001. # View inverse commits comparison
  1002. output = self.app.get(
  1003. '/test/c/%s..%s' % (c3.oid.hex, c1.oid.hex))
  1004. self.assertEqual(output.status_code, 200)
  1005. self.assertIn(
  1006. '<title>Diff from %s to %s - test - Pagure</title>' %
  1007. (c3.oid.hex, c1.oid.hex), output.data)
  1008. self.assertIn(
  1009. '<h5 class="text-muted">%s .. %s</h5>' %
  1010. (c3.oid.hex, c1.oid.hex),
  1011. output.data)
  1012. self.assertIn(
  1013. '<span style="color: #800080; font-weight: bold">' +
  1014. '@@ -1,2 +1,1 @@</span>',
  1015. output.data)
  1016. self.assertIn(
  1017. '<span style="color: #a40000">- Row 0</span>',
  1018. output.data)
  1019. self.assertEqual(
  1020. output.data.count(
  1021. '<span style="color: #a40000">- Row 0</span>'), 1)
  1022. self.assertIn(
  1023. '<span class="hidden-sm-down">Commits&nbsp;</span>\n ' +
  1024. '<span ' +
  1025. 'class="label label-default label-pill hidden-sm-down">' +
  1026. '\n 3\n </span>',
  1027. output.data)
  1028. self.assertIn(
  1029. 'title="View file as of 000000">Šource</a>', output.data)
  1030. self.assertIn(
  1031. '<div><small>file removed</small></div></h5>', output.data)
  1032. output = self.app.get('/foo/bar')
  1033. # No project registered in the DB
  1034. self.assertEqual(output.status_code, 404)
  1035. tests.create_projects(self.session)
  1036. output = self.app.get('/test/bar')
  1037. # No git repo associated
  1038. self.assertEqual(output.status_code, 404)
  1039. tests.create_projects_git(tests.HERE, bare=True)
  1040. output = self.app.get('/test/bar')
  1041. self.assertEqual(output.status_code, 404)
  1042. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  1043. # Add one commit to git repo
  1044. tests.add_commit_git_repo(
  1045. os.path.join(tests.HERE, 'test.git'), ncommits=1)
  1046. c1 = repo.revparse_single('HEAD')
  1047. # Add another commit to git repo
  1048. tests.add_commit_git_repo(
  1049. os.path.join(tests.HERE, 'test.git'), ncommits=1)
  1050. c2 = repo.revparse_single('HEAD')
  1051. # Add one more commit to git repo
  1052. tests.add_commit_git_repo(
  1053. os.path.join(tests.HERE, 'test.git'),
  1054. ncommits=1, filename='Šource')
  1055. c3 = repo.revparse_single('HEAD')
  1056. compare_first_two(c1, c2)
  1057. compare_all(c1, c3)
  1058. user = tests.FakeUser()
  1059. # Set user logged in
  1060. with tests.user_set(pagure.APP, user):
  1061. compare_first_two(c1, c2)
  1062. compare_all(c1, c3)
  1063. def test_view_file(self):
  1064. """ Test the view_file endpoint. """
  1065. output = self.app.get('/foo/blob/foo/f/sources')
  1066. # No project registered in the DB
  1067. self.assertEqual(output.status_code, 404)
  1068. tests.create_projects(self.session)
  1069. output = self.app.get('/test/blob/foo/f/sources')
  1070. # No git repo associated
  1071. self.assertEqual(output.status_code, 404)
  1072. tests.create_projects_git(tests.HERE, bare=True)
  1073. output = self.app.get('/test/blob/foo/f/sources')
  1074. self.assertEqual(output.status_code, 404)
  1075. # Add some content to the git repo
  1076. tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
  1077. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  1078. tests.add_binary_git_repo(
  1079. os.path.join(tests.HERE, 'test.git'), 'test.jpg')
  1080. tests.add_binary_git_repo(
  1081. os.path.join(tests.HERE, 'test.git'), 'test_binary')
  1082. output = self.app.get('/test/blob/master/foofile')
  1083. self.assertEqual(output.status_code, 404)
  1084. # View in a branch
  1085. output = self.app.get('/test/blob/master/f/sources')
  1086. self.assertEqual(output.status_code, 200)
  1087. self.assertTrue('<table class="code_table">' in output.data)
  1088. self.assertTrue(
  1089. '<tr><td class="cell1"><a id="_1" href="#_1" data-line-number="1"></a></td>'
  1090. in output.data)
  1091. self.assertTrue(
  1092. '<td class="cell2"><pre> bar</pre></td>' in output.data)
  1093. # View what's supposed to be an image
  1094. output = self.app.get('/test/blob/master/f/test.jpg')
  1095. self.assertEqual(output.status_code, 200)
  1096. self.assertIn(
  1097. 'Binary files cannot be rendered.<br/>', output.data)
  1098. self.assertIn(
  1099. '<a href="/test/raw/master/f/test.jpg">view the raw version',
  1100. output.data)
  1101. # View by commit id
  1102. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  1103. commit = repo.revparse_single('HEAD')
  1104. output = self.app.get('/test/blob/%s/f/test.jpg' % commit.oid.hex)
  1105. self.assertEqual(output.status_code, 200)
  1106. self.assertIn(
  1107. 'Binary files cannot be rendered.<br/>', output.data)
  1108. self.assertIn('/f/test.jpg">view the raw version', output.data)
  1109. # View by image name -- somehow we support this
  1110. output = self.app.get('/test/blob/sources/f/test.jpg')
  1111. self.assertEqual(output.status_code, 200)
  1112. self.assertIn(
  1113. 'Binary files cannot be rendered.<br/>', output.data)
  1114. self.assertIn('/f/test.jpg">view the raw version', output.data)
  1115. # View binary file
  1116. output = self.app.get('/test/blob/sources/f/test_binary')
  1117. self.assertEqual(output.status_code, 200)
  1118. self.assertIn('/f/test_binary">view the raw version', output.data)
  1119. self.assertTrue(
  1120. 'Binary files cannot be rendered.<br/>'
  1121. in output.data)
  1122. # View folder
  1123. output = self.app.get('/test/blob/master/f/folder1')
  1124. self.assertEqual(output.status_code, 200)
  1125. self.assertIn(
  1126. '<span class="oi text-muted" data-glyph="folder"></span>',
  1127. output.data)
  1128. self.assertIn('<title>Tree - test - Pagure</title>', output.data)
  1129. self.assertIn(
  1130. '<a href="/test/blob/master/f/folder1/folder2">', output.data)
  1131. # View by image name -- with a non-existant file
  1132. output = self.app.get('/test/blob/sources/f/testfoo.jpg')
  1133. self.assertEqual(output.status_code, 404)
  1134. output = self.app.get('/test/blob/master/f/folder1/testfoo.jpg')
  1135. self.assertEqual(output.status_code, 404)
  1136. # View file with a non-ascii name
  1137. tests.add_commit_git_repo(
  1138. os.path.join(tests.HERE, 'test.git'),
  1139. ncommits=1, filename='Šource')
  1140. output = self.app.get('/test/blob/master/f/Šource')
  1141. self.assertEqual(output.status_code, 200)
  1142. self.assertIn('</span>&nbsp; Šource', output.data)
  1143. self.assertIn('<table class="code_table">', output.data)
  1144. self.assertIn(
  1145. '<tr><td class="cell1"><a id="_1" href="#_1" '
  1146. 'data-line-number="1"></a></td>', output.data)
  1147. self.assertTrue(
  1148. '<td class="cell2"><pre><span></span>Row 0</pre></td>'
  1149. in output.data
  1150. or
  1151. '<td class="cell2"><pre>Row 0</pre></td>' in output.data
  1152. )
  1153. # Add a fork of a fork
  1154. item = pagure.lib.model.Project(
  1155. user_id=1, # pingou
  1156. name='test3',
  1157. description='test project #3',
  1158. parent_id=1,
  1159. hook_token='aaabbbppp',
  1160. )
  1161. self.session.add(item)
  1162. self.session.commit()
  1163. tests.add_content_git_repo(
  1164. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  1165. tests.add_readme_git_repo(
  1166. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  1167. tests.add_commit_git_repo(
  1168. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'),
  1169. ncommits=10)
  1170. output = self.app.get('/fork/pingou/test3/blob/master/f/sources')
  1171. self.assertEqual(output.status_code, 200)
  1172. self.assertIn('<table class="code_table">', output.data)
  1173. self.assertIn(
  1174. '<tr><td class="cell1"><a id="_1" href="#_1" data-line-number="1"></a></td>',
  1175. output.data)
  1176. self.assertIn(
  1177. '<td class="cell2"><pre> barRow 0</pre></td>', output.data)
  1178. def test_view_raw_file(self):
  1179. """ Test the view_raw_file endpoint. """
  1180. output = self.app.get('/foo/raw/foo/sources')
  1181. # No project registered in the DB
  1182. self.assertEqual(output.status_code, 404)
  1183. tests.create_projects(self.session)
  1184. output = self.app.get('/test/raw/foo/sources')
  1185. # No git repo associated
  1186. self.assertEqual(output.status_code, 404)
  1187. tests.create_projects_git(tests.HERE, bare=True)
  1188. output = self.app.get('/test/raw/foo/sources')
  1189. self.assertEqual(output.status_code, 404)
  1190. # Add some content to the git repo
  1191. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  1192. # View first commit
  1193. output = self.app.get('/test/raw/master')
  1194. self.assertEqual(output.status_code, 200)
  1195. self.assertTrue(':Author: Pierre-Yves Chibon' in output.data)
  1196. # Add some more content to the repo
  1197. tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
  1198. tests.add_binary_git_repo(
  1199. os.path.join(tests.HERE, 'test.git'), 'test.jpg')
  1200. tests.add_binary_git_repo(
  1201. os.path.join(tests.HERE, 'test.git'), 'test_binary')
  1202. output = self.app.get('/test/raw/master/f/foofile')
  1203. self.assertEqual(output.status_code, 404)
  1204. # View in a branch
  1205. output = self.app.get('/test/raw/master/f/sources')
  1206. self.assertEqual(output.status_code, 200)
  1207. self.assertTrue('foo\n bar' in output.data)
  1208. # View what's supposed to be an image
  1209. output = self.app.get('/test/raw/master/f/test.jpg')
  1210. self.assertEqual(output.status_code, 200)
  1211. self.assertTrue(output.data.startswith('\x00\x00\x01\x00'))
  1212. # View by commit id
  1213. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  1214. commit = repo.revparse_single('HEAD')
  1215. output = self.app.get('/test/raw/%s/f/test.jpg' % commit.oid.hex)
  1216. self.assertEqual(output.status_code, 200)
  1217. self.assertTrue(output.data.startswith('\x00\x00\x01\x00'))
  1218. # View by image name -- somehow we support this
  1219. output = self.app.get('/test/raw/sources/f/test.jpg')
  1220. self.assertEqual(output.status_code, 200)
  1221. self.assertTrue(output.data.startswith('\x00\x00\x01\x00'))
  1222. # View binary file
  1223. output = self.app.get('/test/raw/sources/f/test_binary')
  1224. self.assertEqual(output.status_code, 200)
  1225. self.assertTrue(output.data.startswith('\x00\x00\x01\x00'))
  1226. # View folder
  1227. output = self.app.get('/test/raw/master/f/folder1')
  1228. self.assertEqual(output.status_code, 404)
  1229. # View by image name -- with a non-existant file
  1230. output = self.app.get('/test/raw/sources/f/testfoo.jpg')
  1231. self.assertEqual(output.status_code, 404)
  1232. output = self.app.get('/test/raw/master/f/folder1/testfoo.jpg')
  1233. self.assertEqual(output.status_code, 404)
  1234. output = self.app.get('/test/raw/master/f/')
  1235. self.assertEqual(output.status_code, 404)
  1236. output = self.app.get('/test/raw/master')
  1237. self.assertEqual(output.status_code, 200)
  1238. self.assertTrue(output.data.startswith(
  1239. 'diff --git a/test_binary b/test_binary\n'))
  1240. output = self.app.get('/test/raw/%s' % commit.oid.hex)
  1241. self.assertEqual(output.status_code, 200)
  1242. self.assertTrue(output.data.startswith(
  1243. 'diff --git a/test_binary b/test_binary\n'))
  1244. # Add a fork of a fork
  1245. item = pagure.lib.model.Project(
  1246. user_id=1, # pingou
  1247. name='test3',
  1248. description='test project #3',
  1249. parent_id=1,
  1250. hook_token='aaabbbqqq',
  1251. )
  1252. self.session.add(item)
  1253. self.session.commit()
  1254. tests.add_content_git_repo(
  1255. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  1256. tests.add_readme_git_repo(
  1257. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  1258. tests.add_commit_git_repo(
  1259. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'),
  1260. ncommits=10)
  1261. output = self.app.get('/fork/pingou/test3/raw/master/f/sources')
  1262. self.assertEqual(output.status_code, 200)
  1263. self.assertTrue('foo\n bar' in output.data)
  1264. def test_view_commit(self):
  1265. """ Test the view_commit endpoint. """
  1266. output = self.app.get('/foo/c/bar')
  1267. # No project registered in the DB
  1268. self.assertEqual(output.status_code, 404)
  1269. tests.create_projects(self.session)
  1270. output = self.app.get('/test/c/bar')
  1271. # No git repo associated
  1272. self.assertEqual(output.status_code, 404)
  1273. tests.create_projects_git(tests.HERE, bare=True)
  1274. output = self.app.get('/test/c/bar')
  1275. self.assertEqual(output.status_code, 404)
  1276. # Add a README to the git repo - First commit
  1277. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  1278. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  1279. commit = repo.revparse_single('HEAD')
  1280. # View first commit
  1281. output = self.app.get('/test/c/%s' % commit.oid.hex)
  1282. self.assertEqual(output.status_code, 200)
  1283. self.assertTrue(
  1284. '<div class="list-group" id="diff_list" style="display:none;">'
  1285. in output.data)
  1286. self.assertTrue('</a> Authored by Alice Author' in output.data)
  1287. self.assertTrue('Committed by Cecil Committer' in output.data)
  1288. self.assertTrue(
  1289. '<span style="color: #00A000">+ Pagure</span>' in output.data)
  1290. self.assertTrue(
  1291. '<span style="color: #00A000">+ ======</span>' in output.data)
  1292. # View first commit - with the old URL scheme disabled - default
  1293. output = self.app.get(
  1294. '/test/%s' % commit.oid.hex, follow_redirects=True)
  1295. self.assertEqual(output.status_code, 404)
  1296. self.assertIn('<p>Project not found</p>', output.data)
  1297. # Add some content to the git repo
  1298. tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
  1299. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  1300. commit = repo.revparse_single('HEAD')
  1301. # View another commit
  1302. output = self.app.get('/test/c/%s' % commit.oid.hex)
  1303. self.assertEqual(output.status_code, 200)
  1304. self.assertTrue(
  1305. '<div class="list-group" id="diff_list" style="display:none;">'
  1306. in output.data)
  1307. self.assertTrue('</a> Authored by Alice Author' in output.data)
  1308. self.assertTrue('Committed by Cecil Committer' in output.data)
  1309. self.assertTrue(
  1310. # new version of pygments
  1311. '<div class="highlight" style="background: #f8f8f8">'
  1312. '<pre style="line-height: 125%">'
  1313. '<span></span>'
  1314. '<span style="color: #800080; font-weight: bold">'
  1315. '@@ -0,0 +1,3 @@</span>' in output.data
  1316. or
  1317. # old version of pygments
  1318. '<div class="highlight" style="background: #f8f8f8">'
  1319. '<pre style="line-height: 125%">'
  1320. '<span style="color: #800080; font-weight: bold">'
  1321. '@@ -0,0 +1,3 @@</span>' in output.data)
  1322. #View the commit when branch name is provided
  1323. output = self.app.get('/test/c/%s?branch=master' % commit.oid.hex)
  1324. self.assertEqual(output.status_code, 200)
  1325. self.assertTrue(
  1326. '<a class="active nav-link" href="/test/commits/master">'
  1327. in output.data)
  1328. #View the commit when branch name is wrong, show the commit
  1329. output = self.app.get('/test/c/%s?branch=abcxyz' % commit.oid.hex)
  1330. self.assertEqual(output.status_code, 200)
  1331. self.assertTrue(
  1332. '<a class="active nav-link" href="/test/commits">'
  1333. in output.data)
  1334. # Add a fork of a fork
  1335. item = pagure.lib.model.Project(
  1336. user_id=1, # pingou
  1337. name='test3',
  1338. description='test project #3',
  1339. parent_id=1,
  1340. hook_token='aaabbbkkk',
  1341. )
  1342. self.session.add(item)
  1343. self.session.commit()
  1344. forkedgit = os.path.join(
  1345. tests.HERE, 'forks', 'pingou', 'test3.git')
  1346. tests.add_content_git_repo(forkedgit)
  1347. tests.add_readme_git_repo(forkedgit)
  1348. repo = pygit2.Repository(forkedgit)
  1349. commit = repo.revparse_single('HEAD')
  1350. # Commit does not exist in anothe repo :)
  1351. output = self.app.get('/test/c/%s' % commit.oid.hex)
  1352. self.assertEqual(output.status_code, 404)
  1353. # View commit of fork
  1354. output = self.app.get(
  1355. '/fork/pingou/test3/c/%s' % commit.oid.hex)
  1356. self.assertEqual(output.status_code, 200)
  1357. self.assertTrue(
  1358. '<div class="list-group" id="diff_list" style="display:none;">'
  1359. in output.data)
  1360. self.assertTrue('</a> Authored by Alice Author' in output.data)
  1361. self.assertTrue('Committed by Cecil Committer' in output.data)
  1362. self.assertTrue(
  1363. '<span style="color: #00A000">+ Pagure</span>' in output.data)
  1364. self.assertTrue(
  1365. '<span style="color: #00A000">+ ======</span>' in output.data)
  1366. # Try the old URL scheme with a short hash
  1367. output = self.app.get(
  1368. '/fork/pingou/test3/%s' % commit.oid.hex[:10],
  1369. follow_redirects=True)
  1370. self.assertEqual(output.status_code, 404)
  1371. self.assertIn('<p>Project not found</p>', output.data)
  1372. #View the commit of the fork when branch name is provided
  1373. output = self.app.get('/fork/pingou/test3/c/%s?branch=master' % commit.oid.hex)
  1374. self.assertEqual(output.status_code, 200)
  1375. self.assertTrue(
  1376. '<a class="active nav-link" href="/fork/pingou/test3/commits/master">'
  1377. in output.data)
  1378. #View the commit of the fork when branch name is wrong
  1379. output = self.app.get('/fork/pingou/test3/c/%s?branch=abcxyz' % commit.oid.hex)
  1380. self.assertEqual(output.status_code, 200)
  1381. self.assertTrue(
  1382. '<a class="active nav-link" href="/fork/pingou/test3/commits">'
  1383. in output.data)
  1384. def test_view_commit_patch(self):
  1385. """ Test the view_commit_patch endpoint. """
  1386. # No project registered in the DB
  1387. output = self.app.get('/foo/c/bar.patch')
  1388. self.assertEqual(output.status_code, 404)
  1389. tests.create_projects(self.session)
  1390. output = self.app.get('/test/c/bar.patch')
  1391. # No git repo associated
  1392. self.assertEqual(output.status_code, 404)
  1393. tests.create_projects_git(tests.HERE, bare=True)
  1394. output = self.app.get('/test/c/bar.patch')
  1395. self.assertEqual(output.status_code, 404)
  1396. # Add a README to the git repo - First commit
  1397. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  1398. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  1399. commit = repo.revparse_single('HEAD')
  1400. # View first commit
  1401. output = self.app.get('/test/c/%s.patch' % commit.oid.hex)
  1402. self.assertEqual(output.status_code, 200)
  1403. self.assertTrue('''diff --git a/README.rst b/README.rst
  1404. new file mode 100644
  1405. index 0000000..fb7093d
  1406. --- /dev/null
  1407. +++ b/README.rst
  1408. @@ -0,0 +1,16 @@
  1409. +Pagure
  1410. +======
  1411. +
  1412. +:Author: Pierre-Yves Chibon <pingou@pingoured.fr>
  1413. +
  1414. +
  1415. +Pagure is a light-weight git-centered forge based on pygit2.
  1416. +
  1417. +Currently, Pagure offers a web-interface for git repositories, a ticket
  1418. +system and possibilities to create new projects, fork existing ones and
  1419. +create/merge pull-requests across or within projects.
  1420. +
  1421. +
  1422. +Homepage: https://github.com/pypingou/pagure
  1423. +
  1424. +Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-))
  1425. ''' in output.data)
  1426. self.assertTrue('Subject: Add a README file' in output.data)
  1427. # Add some content to the git repo
  1428. tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
  1429. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  1430. commit = repo.revparse_single('HEAD')
  1431. # View another commit
  1432. output = self.app.get('/test/c/%s.patch' % commit.oid.hex)
  1433. self.assertEqual(output.status_code, 200)
  1434. self.assertTrue(
  1435. 'Subject: Add some directory and a file for more testing'
  1436. in output.data)
  1437. self.assertTrue('''diff --git a/folder1/folder2/file b/folder1/folder2/file
  1438. new file mode 100644
  1439. index 0000000..11980b1
  1440. --- /dev/null
  1441. +++ b/folder1/folder2/file
  1442. @@ -0,0 +1,3 @@
  1443. +foo
  1444. + bar
  1445. +baz
  1446. \ No newline at end of file
  1447. ''' in output.data)
  1448. # Add a fork of a fork
  1449. item = pagure.lib.model.Project(
  1450. user_id=1, # pingou
  1451. name='test3',
  1452. description='test project #3',
  1453. parent_id=1,
  1454. hook_token='aaabbblll',
  1455. )
  1456. self.session.add(item)
  1457. self.session.commit()
  1458. forkedgit = os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')
  1459. tests.add_content_git_repo(forkedgit)
  1460. tests.add_readme_git_repo(forkedgit)
  1461. repo = pygit2.Repository(forkedgit)
  1462. commit = repo.revparse_single('HEAD')
  1463. # Commit does not exist in anothe repo :)
  1464. output = self.app.get('/test/c/%s.patch' % commit.oid.hex)
  1465. self.assertEqual(output.status_code, 404)
  1466. # View commit of fork
  1467. output = self.app.get(
  1468. '/fork/pingou/test3/c/%s.patch' % commit.oid.hex)
  1469. self.assertEqual(output.status_code, 200)
  1470. self.assertTrue('''diff --git a/README.rst b/README.rst
  1471. new file mode 100644
  1472. index 0000000..fb7093d
  1473. --- /dev/null
  1474. +++ b/README.rst
  1475. @@ -0,0 +1,16 @@
  1476. +Pagure
  1477. +======
  1478. +
  1479. +:Author: Pierre-Yves Chibon <pingou@pingoured.fr>
  1480. +
  1481. +
  1482. +Pagure is a light-weight git-centered forge based on pygit2.
  1483. +
  1484. +Currently, Pagure offers a web-interface for git repositories, a ticket
  1485. +system and possibilities to create new projects, fork existing ones and
  1486. +create/merge pull-requests across or within projects.
  1487. +
  1488. +
  1489. +Homepage: https://github.com/pypingou/pagure
  1490. +
  1491. +Dev instance: http://209.132.184.222/ (/!\ May change unexpectedly, it's a dev instance ;-))
  1492. ''' in output.data)
  1493. def test_view_tree(self):
  1494. """ Test the view_tree endpoint. """
  1495. output = self.app.get('/foo/tree/')
  1496. # No project registered in the DB
  1497. self.assertEqual(output.status_code, 404)
  1498. tests.create_projects(self.session)
  1499. output = self.app.get('/test/tree/')
  1500. # No git repo associated
  1501. self.assertEqual(output.status_code, 404)
  1502. tests.create_projects_git(tests.HERE, bare=True)
  1503. output = self.app.get('/test/tree/')
  1504. self.assertEqual(output.status_code, 200)
  1505. self.assertIn(
  1506. '''
  1507. <ol class="breadcrumb">
  1508. <li>
  1509. <a href="/test/tree">
  1510. <span class="oi" data-glyph="random">
  1511. </span>&nbsp; None
  1512. </a>
  1513. </li>
  1514. </ol>''', output.data)
  1515. self.assertTrue(
  1516. 'No content found in this repository' in output.data)
  1517. # Add a README to the git repo - First commit
  1518. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  1519. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  1520. commit = repo.revparse_single('HEAD')
  1521. # View first commit
  1522. output = self.app.get('/test/tree/%s' % commit.oid.hex)
  1523. self.assertEqual(output.status_code, 200)
  1524. self.assertIn(
  1525. '<div class="projectinfo m-t-1 m-b-1">\n'
  1526. 'test project #1 </div>', output.data)
  1527. self.assertIn('<title>Tree - test - Pagure</title>', output.data)
  1528. self.assertTrue('README.rst' in output.data)
  1529. self.assertFalse(
  1530. 'No content found in this repository' in output.data)
  1531. # View tree by branch
  1532. output = self.app.get('/test/tree/master')
  1533. self.assertEqual(output.status_code, 200)
  1534. self.assertIn(
  1535. '<div class="projectinfo m-t-1 m-b-1">\n'
  1536. 'test project #1 </div>', output.data)
  1537. self.assertIn('<title>Tree - test - Pagure</title>', output.data)
  1538. self.assertTrue('README.rst' in output.data)
  1539. self.assertFalse(
  1540. 'No content found in this repository' in output.data)
  1541. # Add a fork of a fork
  1542. item = pagure.lib.model.Project(
  1543. user_id=1, # pingou
  1544. name='test3',
  1545. description='test project #3',
  1546. parent_id=1,
  1547. hook_token='aaabbbfff',
  1548. )
  1549. self.session.add(item)
  1550. self.session.commit()
  1551. forkedgit = os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git')
  1552. tests.add_content_git_repo(forkedgit)
  1553. output = self.app.get('/fork/pingou/test3/tree/')
  1554. self.assertEqual(output.status_code, 200)
  1555. self.assertIn(
  1556. '<div class="projectinfo m-t-1 m-b-1">\n'
  1557. 'test project #3 </div>', output.data)
  1558. self.assertIn('<title>Tree - test3 - Pagure</title>', output.data)
  1559. self.assertTrue(
  1560. '<a href="/fork/pingou/test3/blob/master/f/folder1">'
  1561. in output.data)
  1562. self.assertTrue(
  1563. '<a href="/fork/pingou/test3/blob/master/f/sources">'
  1564. in output.data)
  1565. self.assertFalse(
  1566. 'No content found in this repository' in output.data)
  1567. output = self.app.get(
  1568. '/fork/pingou/test3/blob/master/f/folder1/folder2')
  1569. self.assertEqual(output.status_code, 200)
  1570. self.assertTrue(
  1571. '<a href="/fork/pingou/test3/blob/master/'
  1572. 'f/folder1/folder2/file%C5%A0">' in output.data)
  1573. @patch('pagure.lib.notify.send_email')
  1574. @patch('pagure.ui.repo.admin_session_timedout')
  1575. def test_delete_repo_when_turned_off(self, ast, send_email):
  1576. """ Test the delete_repo endpoint when deletion of a repo is
  1577. turned off in the pagure instance """
  1578. ast.return_value = False
  1579. send_email.return_value = True
  1580. pagure.APP.config['ENABLE_DEL_PROJECTS'] = False
  1581. output = self.app.post('/foo/delete')
  1582. # User not logged in
  1583. self.assertEqual(output.status_code, 302)
  1584. user = tests.FakeUser(username='pingou')
  1585. with tests.user_set(pagure.APP, user):
  1586. tests.create_projects(self.session)
  1587. output = self.app.post('/test/delete', follow_redirects=True)
  1588. self.assertEqual(output.status_code, 404)
  1589. # Only git repo
  1590. item = pagure.lib.model.Project(
  1591. user_id=1, # pingou
  1592. name='test',
  1593. description='test project #1',
  1594. hook_token='aaabbbggg',
  1595. )
  1596. self.session.add(item)
  1597. self.session.commit()
  1598. tests.create_projects_git(tests.HERE)
  1599. output = self.app.post('/test/delete', follow_redirects=True)
  1600. self.assertEqual(output.status_code, 404)
  1601. # Only git and doc repo
  1602. item = pagure.lib.model.Project(
  1603. user_id=1, # pingou
  1604. name='test',
  1605. description='test project #1',
  1606. hook_token='aaabbbhhh',
  1607. )
  1608. self.session.add(item)
  1609. self.session.commit()
  1610. tests.create_projects_git(tests.HERE)
  1611. tests.create_projects_git(os.path.join(tests.HERE, 'docs'))
  1612. output = self.app.post('/test/delete', follow_redirects=True)
  1613. self.assertEqual(output.status_code, 404)
  1614. # All repo there
  1615. item = pagure.lib.model.Project(
  1616. user_id=1, # pingou
  1617. name='test',
  1618. description='test project #1',
  1619. hook_token='aaabbbiii',
  1620. )
  1621. self.session.add(item)
  1622. self.session.commit()
  1623. # Create all the git repos
  1624. tests.create_projects_git(tests.HERE)
  1625. tests.create_projects_git(os.path.join(tests.HERE, 'docs'))
  1626. tests.create_projects_git(
  1627. os.path.join(tests.HERE, 'tickets'), bare=True)
  1628. tests.create_projects_git(
  1629. os.path.join(tests.HERE, 'requests'), bare=True)
  1630. # Check repo was created
  1631. output = self.app.get('/')
  1632. self.assertEqual(output.status_code, 200)
  1633. self.assertIn(
  1634. '<div class="card-header">\n My Projects <span '
  1635. 'class="label label-default">5</span>', output.data)
  1636. self.assertIn(
  1637. 'Forks <span class="label label-default">0</span>',
  1638. output.data)
  1639. # add issues
  1640. repo = pagure.lib.get_project(self.session, 'test')
  1641. msg = pagure.lib.new_issue(
  1642. session=self.session,
  1643. repo=repo,
  1644. title='Test issue',
  1645. content='We should work on this',
  1646. user='pingou',
  1647. ticketfolder=os.path.join(tests.HERE, 'tickets')
  1648. )
  1649. self.session.commit()
  1650. self.assertEqual(msg.title, 'Test issue')
  1651. msg = pagure.lib.new_issue(
  1652. session=self.session,
  1653. repo=repo,
  1654. title='Test issue #2',
  1655. content='We should work on this, really',
  1656. user='pingou',
  1657. ticketfolder=os.path.join(tests.HERE, 'tickets')
  1658. )
  1659. self.session.commit()
  1660. self.assertEqual(msg.title, 'Test issue #2')
  1661. # Add a comment to an issue
  1662. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1663. msg = pagure.lib.add_issue_comment(
  1664. session=self.session,
  1665. issue=issue,
  1666. comment='Hey look a comment!',
  1667. user='foo',
  1668. ticketfolder=None
  1669. )
  1670. self.session.commit()
  1671. self.assertEqual(msg, 'Comment added')
  1672. # add pull-requests
  1673. req = pagure.lib.new_pull_request(
  1674. session=self.session,
  1675. repo_from=repo,
  1676. branch_from='feature',
  1677. repo_to=repo,
  1678. branch_to='master',
  1679. title='test pull-request',
  1680. user='pingou',
  1681. requestfolder=os.path.join(tests.HERE, 'requests'),
  1682. )
  1683. self.session.commit()
  1684. self.assertEqual(req.id, 3)
  1685. self.assertEqual(req.title, 'test pull-request')
  1686. req = pagure.lib.new_pull_request(
  1687. session=self.session,
  1688. repo_from=repo,
  1689. branch_from='feature2',
  1690. repo_to=repo,
  1691. branch_to='master',
  1692. title='test pull-request',
  1693. user='pingou',
  1694. requestfolder=os.path.join(tests.HERE, 'requests'),
  1695. )
  1696. self.session.commit()
  1697. self.assertEqual(req.id, 4)
  1698. self.assertEqual(req.title, 'test pull-request')
  1699. # Add comment on a pull-request
  1700. request = pagure.lib.search_pull_requests(
  1701. self.session, requestid=3)
  1702. msg = pagure.lib.add_pull_request_comment(
  1703. session=self.session,
  1704. request=request,
  1705. commit='commithash',
  1706. tree_id=None,
  1707. filename='file',
  1708. row=None,
  1709. comment='This is awesome, I got to remember it!',
  1710. user='foo',
  1711. requestfolder=None,
  1712. )
  1713. self.assertEqual(msg, 'Comment added')
  1714. # Check before deleting the project
  1715. output = self.app.get('/')
  1716. self.assertEqual(output.status_code, 200)
  1717. self.assertIn(
  1718. '<div class="card-header">\n My Projects <span '
  1719. 'class="label label-default">5</span>', output.data)
  1720. self.assertIn(
  1721. 'Forks <span class="label label-default">0</span>',
  1722. output.data)
  1723. output = self.app.post('/test/delete', follow_redirects=True)
  1724. self.assertEqual(output.status_code, 404)
  1725. repo = pagure.lib.get_project(self.session, 'test')
  1726. self.assertNotEqual(repo, None)
  1727. repo = pagure.lib.get_project(self.session, 'test2')
  1728. self.assertNotEqual(repo, None)
  1729. # Add a fork of a fork
  1730. item = pagure.lib.model.Project(
  1731. user_id=1, # pingou
  1732. name='test3',
  1733. description='test project #3',
  1734. parent_id=2,
  1735. hook_token='aaabbbjjj',
  1736. )
  1737. self.session.add(item)
  1738. self.session.commit()
  1739. tests.add_content_git_repo(
  1740. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  1741. tests.add_content_git_repo(
  1742. os.path.join(tests.HERE, 'docs', 'pingou', 'test3.git'))
  1743. tests.add_content_git_repo(
  1744. os.path.join(tests.HERE, 'tickets', 'pingou', 'test3.git'))
  1745. # Check before deleting the fork
  1746. output = self.app.get('/')
  1747. self.assertEqual(output.status_code, 200)
  1748. self.assertIn(
  1749. '<div class="card-header">\n My Projects <span '
  1750. 'class="label label-default">5</span>', output.data)
  1751. self.assertIn(
  1752. 'Forks <span class="label label-default">1</span>',
  1753. output.data)
  1754. output = self.app.post(
  1755. '/fork/pingou/test3/delete', follow_redirects=True)
  1756. self.assertEqual(output.status_code, 404)
  1757. pagure.APP.config['ENABLE_DEL_PROJECTS'] = True
  1758. @patch('pagure.lib.notify.send_email')
  1759. @patch('pagure.ui.repo.admin_session_timedout')
  1760. def test_delete_repo(self, ast, send_email):
  1761. """ Test the delete_repo endpoint. """
  1762. ast.return_value = False
  1763. send_email.return_value = True
  1764. output = self.app.post('/foo/delete')
  1765. # User not logged in
  1766. self.assertEqual(output.status_code, 302)
  1767. user = tests.FakeUser()
  1768. with tests.user_set(pagure.APP, user):
  1769. output = self.app.post('/foo/delete')
  1770. # No project registered in the DB
  1771. self.assertEqual(output.status_code, 404)
  1772. tests.create_projects(self.session)
  1773. output = self.app.post('/test/delete')
  1774. # No git repo associated
  1775. self.assertEqual(output.status_code, 403)
  1776. user = tests.FakeUser(username='pingou')
  1777. with tests.user_set(pagure.APP, user):
  1778. ast.return_value = True
  1779. output = self.app.post('/test/delete')
  1780. self.assertEqual(output.status_code, 302)
  1781. ast.return_value = False
  1782. output = self.app.post('/test/delete', follow_redirects=True)
  1783. self.assertEqual(output.status_code, 200)
  1784. self.assertIn(
  1785. '</button>\n Could not delete all the '
  1786. 'repos from the system', output.data)
  1787. self.assertIn(
  1788. '<div class="card-header">\n Projects <span '
  1789. 'class="label label-default">1</span>', output.data)
  1790. self.assertIn(
  1791. 'Forks <span class="label label-default">0</span>',
  1792. output.data)
  1793. # Only git repo
  1794. item = pagure.lib.model.Project(
  1795. user_id=1, # pingou
  1796. name='test',
  1797. description='test project #1',
  1798. hook_token='aaabbbggg',
  1799. )
  1800. self.session.add(item)
  1801. self.session.commit()
  1802. tests.create_projects_git(tests.HERE)
  1803. output = self.app.post('/test/delete', follow_redirects=True)
  1804. self.assertEqual(output.status_code, 200)
  1805. self.assertTrue(
  1806. '</button>\n Could not delete all the '
  1807. 'repos from the system' in output.data)
  1808. self.assertIn(
  1809. '<div class="card-header">\n Projects <span '
  1810. 'class="label label-default">1</span>', output.data)
  1811. self.assertIn(
  1812. 'Forks <span class="label label-default">0</span>',
  1813. output.data)
  1814. # Only git and doc repo
  1815. item = pagure.lib.model.Project(
  1816. user_id=1, # pingou
  1817. name='test',
  1818. description='test project #1',
  1819. hook_token='aaabbbhhh',
  1820. )
  1821. self.session.add(item)
  1822. self.session.commit()
  1823. tests.create_projects_git(tests.HERE)
  1824. tests.create_projects_git(os.path.join(tests.HERE, 'docs'))
  1825. output = self.app.post('/test/delete', follow_redirects=True)
  1826. self.assertEqual(output.status_code, 200)
  1827. self.assertTrue(
  1828. '</button>\n Could not delete all the '
  1829. 'repos from the system' in output.data)
  1830. # All repo there
  1831. item = pagure.lib.model.Project(
  1832. user_id=1, # pingou
  1833. name='test',
  1834. description='test project #1',
  1835. hook_token='aaabbbiii',
  1836. )
  1837. self.session.add(item)
  1838. self.session.commit()
  1839. # Create all the git repos
  1840. tests.create_projects_git(tests.HERE)
  1841. tests.create_projects_git(os.path.join(tests.HERE, 'docs'))
  1842. tests.create_projects_git(
  1843. os.path.join(tests.HERE, 'tickets'), bare=True)
  1844. tests.create_projects_git(
  1845. os.path.join(tests.HERE, 'requests'), bare=True)
  1846. # Check repo was created
  1847. output = self.app.get('/')
  1848. self.assertEqual(output.status_code, 200)
  1849. self.assertIn(
  1850. '<div class="card-header">\n My Projects <span '
  1851. 'class="label label-default">2</span>', output.data)
  1852. self.assertIn(
  1853. 'Forks <span class="label label-default">0</span>',
  1854. output.data)
  1855. # add issues
  1856. repo = pagure.lib.get_project(self.session, 'test')
  1857. msg = pagure.lib.new_issue(
  1858. session=self.session,
  1859. repo=repo,
  1860. title='Test issue',
  1861. content='We should work on this',
  1862. user='pingou',
  1863. ticketfolder=os.path.join(tests.HERE, 'tickets')
  1864. )
  1865. self.session.commit()
  1866. self.assertEqual(msg.title, 'Test issue')
  1867. msg = pagure.lib.new_issue(
  1868. session=self.session,
  1869. repo=repo,
  1870. title='Test issue #2',
  1871. content='We should work on this, really',
  1872. user='pingou',
  1873. ticketfolder=os.path.join(tests.HERE, 'tickets')
  1874. )
  1875. self.session.commit()
  1876. self.assertEqual(msg.title, 'Test issue #2')
  1877. # Add a comment to an issue
  1878. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  1879. msg = pagure.lib.add_issue_comment(
  1880. session=self.session,
  1881. issue=issue,
  1882. comment='Hey look a comment!',
  1883. user='foo',
  1884. ticketfolder=None
  1885. )
  1886. self.session.commit()
  1887. self.assertEqual(msg, 'Comment added')
  1888. # add pull-requests
  1889. req = pagure.lib.new_pull_request(
  1890. session=self.session,
  1891. repo_from=repo,
  1892. branch_from='feature',
  1893. repo_to=repo,
  1894. branch_to='master',
  1895. title='test pull-request',
  1896. user='pingou',
  1897. requestfolder=os.path.join(tests.HERE, 'requests'),
  1898. )
  1899. self.session.commit()
  1900. self.assertEqual(req.id, 3)
  1901. self.assertEqual(req.title, 'test pull-request')
  1902. req = pagure.lib.new_pull_request(
  1903. session=self.session,
  1904. repo_from=repo,
  1905. branch_from='feature2',
  1906. repo_to=repo,
  1907. branch_to='master',
  1908. title='test pull-request',
  1909. user='pingou',
  1910. requestfolder=os.path.join(tests.HERE, 'requests'),
  1911. )
  1912. self.session.commit()
  1913. self.assertEqual(req.id, 4)
  1914. self.assertEqual(req.title, 'test pull-request')
  1915. # Add comment on a pull-request
  1916. request = pagure.lib.search_pull_requests(
  1917. self.session, requestid=3)
  1918. msg = pagure.lib.add_pull_request_comment(
  1919. session=self.session,
  1920. request=request,
  1921. commit='commithash',
  1922. tree_id=None,
  1923. filename='file',
  1924. row=None,
  1925. comment='This is awesome, I got to remember it!',
  1926. user='foo',
  1927. requestfolder=None,
  1928. )
  1929. self.assertEqual(msg, 'Comment added')
  1930. # Check before deleting the project
  1931. output = self.app.get('/')
  1932. self.assertEqual(output.status_code, 200)
  1933. self.assertIn(
  1934. '<div class="card-header">\n My Projects <span '
  1935. 'class="label label-default">2</span>', output.data)
  1936. self.assertIn(
  1937. 'Forks <span class="label label-default">0</span>',
  1938. output.data)
  1939. output = self.app.post('/test/delete', follow_redirects=True)
  1940. self.assertEqual(output.status_code, 200)
  1941. self.assertIn(
  1942. '<div class="card-header">\n Projects <span '
  1943. 'class="label label-default">1</span>', output.data)
  1944. self.assertIn(
  1945. 'Forks <span class="label label-default">0</span>',
  1946. output.data)
  1947. repo = pagure.lib.get_project(self.session, 'test')
  1948. self.assertEqual(repo, None)
  1949. repo = pagure.lib.get_project(self.session, 'test2')
  1950. self.assertNotEqual(repo, None)
  1951. # Add a fork of a fork
  1952. item = pagure.lib.model.Project(
  1953. user_id=1, # pingou
  1954. name='test3',
  1955. description='test project #3',
  1956. parent_id=2,
  1957. hook_token='aaabbbjjj',
  1958. )
  1959. self.session.add(item)
  1960. self.session.commit()
  1961. tests.add_content_git_repo(
  1962. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  1963. tests.add_content_git_repo(
  1964. os.path.join(tests.HERE, 'docs', 'pingou', 'test3.git'))
  1965. tests.add_content_git_repo(
  1966. os.path.join(tests.HERE, 'tickets', 'pingou', 'test3.git'))
  1967. # Check before deleting the fork
  1968. output = self.app.get('/')
  1969. self.assertEqual(output.status_code, 200)
  1970. self.assertIn(
  1971. '<div class="card-header">\n My Projects <span '
  1972. 'class="label label-default">1</span>', output.data)
  1973. self.assertIn(
  1974. 'Forks <span class="label label-default">1</span>',
  1975. output.data)
  1976. output = self.app.post(
  1977. '/fork/pingou/test3/delete', follow_redirects=True)
  1978. self.assertEqual(output.status_code, 200)
  1979. self.assertIn(
  1980. '<div class="card-header">\n Projects <span '
  1981. 'class="label label-default">1</span>', output.data)
  1982. self.assertIn(
  1983. 'Forks <span class="label label-default">0</span>',
  1984. output.data)
  1985. @patch('pagure.ui.repo.admin_session_timedout')
  1986. def test_new_repo_hook_token(self, ast):
  1987. """ Test the new_repo_hook_token endpoint. """
  1988. ast.return_value = False
  1989. tests.create_projects(self.session)
  1990. repo = pagure.lib.get_project(self.session, 'test')
  1991. self.assertEqual(repo.hook_token, 'aaabbbccc')
  1992. user = tests.FakeUser()
  1993. with tests.user_set(pagure.APP, user):
  1994. pagure.APP.config['WEBHOOK'] = True
  1995. output = self.app.get('/new/')
  1996. self.assertEqual(output.status_code, 200)
  1997. self.assertIn('<strong>Create new Project</strong>', output.data)
  1998. csrf_token = output.data.split(
  1999. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  2000. output = self.app.post('/foo/hook_token')
  2001. self.assertEqual(output.status_code, 404)
  2002. output = self.app.post('/test/hook_token')
  2003. self.assertEqual(output.status_code, 403)
  2004. ast.return_value = True
  2005. output = self.app.post('/test/hook_token')
  2006. self.assertEqual(output.status_code, 302)
  2007. ast.return_value = False
  2008. pagure.APP.config['WEBHOOK'] = False
  2009. repo = pagure.lib.get_project(self.session, 'test')
  2010. self.assertEqual(repo.hook_token, 'aaabbbccc')
  2011. user.username = 'pingou'
  2012. with tests.user_set(pagure.APP, user):
  2013. pagure.APP.config['WEBHOOK'] = True
  2014. output = self.app.post('/test/hook_token')
  2015. self.assertEqual(output.status_code, 400)
  2016. data = {'csrf_token': csrf_token}
  2017. repo = pagure.lib.get_project(self.session, 'test')
  2018. self.assertEqual(repo.hook_token, 'aaabbbccc')
  2019. tests.create_projects_git(tests.HERE)
  2020. output = self.app.post(
  2021. '/test/hook_token', data=data, follow_redirects=True)
  2022. self.assertEqual(output.status_code, 200)
  2023. self.assertIn(
  2024. '</button>\n New hook token generated',
  2025. output.data)
  2026. pagure.APP.config['WEBHOOK'] = False
  2027. repo = pagure.lib.get_project(self.session, 'test')
  2028. self.assertNotEqual(repo.hook_token, 'aaabbbccc')
  2029. @patch('pagure.lib.notify.send_email')
  2030. @patch('pagure.ui.repo.admin_session_timedout')
  2031. @patch('pagure.lib.git.update_git')
  2032. def test_regenerate_git(self, upgit, ast, sendmail):
  2033. """ Test the regenerate_git endpoint. """
  2034. ast.return_value = False
  2035. upgit.return_value = True
  2036. sendmail.return_value = True
  2037. tests.create_projects(self.session)
  2038. user = tests.FakeUser()
  2039. with tests.user_set(pagure.APP, user):
  2040. output = self.app.get('/new/')
  2041. self.assertEqual(output.status_code, 200)
  2042. self.assertIn('<strong>Create new Project</strong>', output.data)
  2043. csrf_token = output.data.split(
  2044. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  2045. output = self.app.post('/foo/regenerate')
  2046. self.assertEqual(output.status_code, 404)
  2047. output = self.app.post('/test/regenerate')
  2048. self.assertEqual(output.status_code, 403)
  2049. ast.return_value = True
  2050. output = self.app.post('/test/regenerate')
  2051. self.assertEqual(output.status_code, 302)
  2052. ast.return_value = False
  2053. user.username = 'pingou'
  2054. with tests.user_set(pagure.APP, user):
  2055. output = self.app.post('/test/regenerate')
  2056. self.assertEqual(output.status_code, 400)
  2057. data = {'csrf_token': csrf_token}
  2058. output = self.app.post('/test/regenerate', data=data)
  2059. self.assertEqual(output.status_code, 400)
  2060. data['regenerate'] = 'ticket'
  2061. output = self.app.post('/test/regenerate', data=data)
  2062. self.assertEqual(output.status_code, 400)
  2063. # Create an issue to play with
  2064. repo = pagure.lib.get_project(self.session, 'test')
  2065. msg = pagure.lib.new_issue(
  2066. session=self.session,
  2067. repo=repo,
  2068. title='Test issue',
  2069. content='We should work on this',
  2070. user='pingou',
  2071. ticketfolder=None
  2072. )
  2073. self.session.commit()
  2074. self.assertEqual(msg.title, 'Test issue')
  2075. data['regenerate'] = 'tickets'
  2076. tests.create_projects_git(tests.HERE)
  2077. output = self.app.post(
  2078. '/test/regenerate', data=data, follow_redirects=True)
  2079. self.assertEqual(output.status_code, 200)
  2080. self.assertIn(
  2081. '</button>\n Tickets git repo updated',
  2082. output.data)
  2083. # Create a request to play with
  2084. repo = pagure.lib.get_project(self.session, 'test')
  2085. msg = pagure.lib.new_pull_request(
  2086. session=self.session,
  2087. repo_from=repo,
  2088. branch_from='branch',
  2089. repo_to=repo,
  2090. branch_to='master',
  2091. title='Test pull-request',
  2092. user='pingou',
  2093. requestfolder=None,
  2094. )
  2095. self.session.commit()
  2096. self.assertEqual(msg.title, 'Test pull-request')
  2097. data['regenerate'] = 'requests'
  2098. output = self.app.post(
  2099. '/test/regenerate', data=data, follow_redirects=True)
  2100. self.assertEqual(output.status_code, 200)
  2101. self.assertIn(
  2102. '</button>\n Requests git repo updated',
  2103. output.data)
  2104. def test_view_tags(self):
  2105. """ Test the view_tags endpoint. """
  2106. output = self.app.get('/foo/releases')
  2107. # No project registered in the DB
  2108. self.assertEqual(output.status_code, 404)
  2109. tests.create_projects(self.session)
  2110. output = self.app.get('/test/releases')
  2111. # No git repo associated
  2112. self.assertEqual(output.status_code, 404)
  2113. tests.create_projects_git(tests.HERE, bare=True)
  2114. output = self.app.get('/test/releases')
  2115. self.assertEqual(output.status_code, 200)
  2116. self.assertIn('This project has not been tagged.', output.data)
  2117. # Add a README to the git repo - First commit
  2118. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  2119. repo = pygit2.Repository(os.path.join(tests.HERE, 'test.git'))
  2120. first_commit = repo.revparse_single('HEAD')
  2121. tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0)
  2122. repo.create_tag(
  2123. "0.0.1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger,
  2124. "Release 0.0.1")
  2125. output = self.app.get('/test/releases')
  2126. self.assertEqual(output.status_code, 200)
  2127. self.assertIn('0.0.1', output.data)
  2128. self.assertIn('<span id="tagid" class="label label-default">', output.data)
  2129. self.assertTrue(output.data.count('tagid'), 1)
  2130. def test_edit_file(self):
  2131. """ Test the edit_file endpoint. """
  2132. output = self.app.get('/foo/edit/foo/f/sources')
  2133. self.assertEqual(output.status_code, 302)
  2134. user = tests.FakeUser()
  2135. with tests.user_set(pagure.APP, user):
  2136. # No project registered in the DB
  2137. output = self.app.get('/foo/edit/foo/f/sources')
  2138. self.assertEqual(output.status_code, 404)
  2139. tests.create_projects(self.session)
  2140. # No a repo admin
  2141. output = self.app.get('/test/edit/foo/f/sources')
  2142. self.assertEqual(output.status_code, 403)
  2143. user.username = 'pingou'
  2144. with tests.user_set(pagure.APP, user):
  2145. # No associated git repo
  2146. output = self.app.get('/test/edit/foo/f/sources')
  2147. self.assertEqual(output.status_code, 404)
  2148. tests.create_projects_git(tests.HERE, bare=True)
  2149. output = self.app.get('/test/edit/foo/f/sources')
  2150. self.assertEqual(output.status_code, 404)
  2151. # Add some content to the git repo
  2152. tests.add_content_git_repo(os.path.join(tests.HERE, 'test.git'))
  2153. tests.add_readme_git_repo(os.path.join(tests.HERE, 'test.git'))
  2154. tests.add_binary_git_repo(
  2155. os.path.join(tests.HERE, 'test.git'), 'test.jpg')
  2156. tests.add_binary_git_repo(
  2157. os.path.join(tests.HERE, 'test.git'), 'test_binary')
  2158. output = self.app.get('/test/edit/master/foofile')
  2159. self.assertEqual(output.status_code, 404)
  2160. # Edit page
  2161. output = self.app.get('/test/edit/master/f/sources')
  2162. self.assertEqual(output.status_code, 200)
  2163. self.assertIn(
  2164. '<li><a href="/test/tree/master">'
  2165. '<span class="oi" data-glyph="random"></span>&nbsp; master'
  2166. '</a></li><li class="active">'
  2167. '<span class="oi" data-glyph="file"></span>&nbsp; sources</li>',
  2168. output.data)
  2169. self.assertIn(
  2170. '<textarea id="textareaCode" name="content">foo\n bar</textarea>',
  2171. output.data)
  2172. csrf_token = output.data.split(
  2173. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  2174. # View what's supposed to be an image
  2175. output = self.app.get('/test/edit/master/f/test.jpg')
  2176. self.assertEqual(output.status_code, 400)
  2177. self.assertIn('<p>Cannot edit binary files</p>', output.data)
  2178. # Check file before the commit:
  2179. output = self.app.get('/test/raw/master/f/sources')
  2180. self.assertEqual(output.status_code, 200)
  2181. self.assertEqual(output.data, 'foo\n bar')
  2182. # No CSRF Token
  2183. data = {
  2184. 'content': 'foo\n bar\n baz',
  2185. 'commit_title': 'test commit',
  2186. 'commit_message': 'Online commits from the tests',
  2187. }
  2188. output = self.app.post('/test/edit/master/f/sources', data=data)
  2189. self.assertEqual(output.status_code, 200)
  2190. self.assertIn(
  2191. '<title>Edit - test - Pagure</title>', output.data)
  2192. # Check that nothing changed
  2193. output = self.app.get('/test/raw/master/f/sources')
  2194. self.assertEqual(output.status_code, 200)
  2195. self.assertEqual(output.data, 'foo\n bar')
  2196. # Missing email
  2197. data['csrf_token'] = csrf_token
  2198. output = self.app.post('/test/edit/master/f/sources', data=data)
  2199. self.assertEqual(output.status_code, 200)
  2200. self.assertIn(
  2201. '<title>Edit - test - Pagure</title>', output.data)
  2202. # Invalid email
  2203. data['email'] = 'pingou@fp.o'
  2204. output = self.app.post('/test/edit/master/f/sources', data=data)
  2205. self.assertIn(
  2206. '<title>Edit - test - Pagure</title>', output.data)
  2207. # Works
  2208. data['email'] = 'bar@pingou.com'
  2209. data['branch'] = 'master'
  2210. output = self.app.post(
  2211. '/test/edit/master/f/sources', data=data,
  2212. follow_redirects=True)
  2213. self.assertEqual(output.status_code, 200)
  2214. self.assertIn(
  2215. '<title>Commits - test - Pagure</title>', output.data)
  2216. self.assertIn(
  2217. '</button>\n Changes committed',
  2218. output.data)
  2219. # Check file after the commit:
  2220. output = self.app.get('/test/raw/master/f/sources')
  2221. self.assertEqual(output.status_code, 200)
  2222. self.assertEqual(output.data, 'foo\n bar\n baz')
  2223. # Add a fork of a fork
  2224. item = pagure.lib.model.Project(
  2225. user_id=1, # pingou
  2226. name='test3',
  2227. description='test project #3',
  2228. parent_id=1,
  2229. hook_token='aaabbbppp',
  2230. )
  2231. self.session.add(item)
  2232. self.session.commit()
  2233. tests.add_content_git_repo(
  2234. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  2235. tests.add_readme_git_repo(
  2236. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'))
  2237. tests.add_commit_git_repo(
  2238. os.path.join(tests.HERE, 'forks', 'pingou', 'test3.git'),
  2239. ncommits=10)
  2240. output = self.app.get('/fork/pingou/test3/edit/master/f/sources')
  2241. self.assertEqual(output.status_code, 200)
  2242. self.assertIn(
  2243. '<li><a href="/fork/pingou/test3/tree/master">'
  2244. '<span class="oi" data-glyph="random"></span>&nbsp; master'
  2245. '</a></li><li class="active">'
  2246. '<span class="oi" data-glyph="file"></span>&nbsp; sources'
  2247. '</li>', output.data)
  2248. self.assertIn(
  2249. '<textarea id="textareaCode" name="content">foo\n barRow 0\n',
  2250. output.data)
  2251. @patch('pagure.ui.repo.admin_session_timedout')
  2252. def test_change_ref_head(self,ast):
  2253. """ Test the change_ref_head endpoint. """
  2254. ast.return_value = True
  2255. output = self.app.post('/foo/default/branch/')
  2256. self.assertEqual(output.status_code, 302)
  2257. user = tests.FakeUser()
  2258. with tests.user_set(pagure.APP, user):
  2259. output = self.app.post('/foo/default/branch/')
  2260. self.assertEqual(output.status_code, 302)
  2261. ast.return_value = False
  2262. output = self.app.post('/foo/default/branch/')
  2263. self.assertEqual(output.status_code, 404)
  2264. tests.create_projects(self.session)
  2265. output = self.app.post('/test/default/branch/')
  2266. self.assertEqual(output.status_code, 403)
  2267. user.username = 'pingou'
  2268. with tests.user_set(pagure.APP, user):
  2269. repo = tests.create_projects_git(tests.HERE)
  2270. output = self.app.post('/test/default/branch/',
  2271. follow_redirects=True) # without git branch
  2272. self.assertEqual(output.status_code, 200)
  2273. self.assertIn(
  2274. '<title>Settings - test - Pagure</title>', output.data)
  2275. self.assertIn('<h3>Settings for test</h3>', output.data)
  2276. self.assertIn(
  2277. '<select class="c-select" id="branches" name="branches">'
  2278. '</select>', output.data)
  2279. csrf_token = output.data.split(
  2280. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  2281. repo_obj = pygit2.Repository(repo[0])
  2282. tree = repo_obj.index.write_tree()
  2283. author = pygit2.Signature(
  2284. 'Alice Author', 'alice@authors.tld')
  2285. committer = pygit2.Signature(
  2286. 'Cecil Committer', 'cecil@committers.tld')
  2287. repo_obj.create_commit(
  2288. 'refs/heads/master', # the name of the reference to update
  2289. author,
  2290. committer,
  2291. 'Add sources file for testing',
  2292. # binary string representing the tree object ID
  2293. tree,
  2294. # list of binary strings representing parents of the new commit
  2295. []
  2296. )
  2297. repo_obj.create_branch("feature",repo_obj.head.get_object())
  2298. data = {
  2299. 'branches': 'feature',
  2300. 'csrf_token': csrf_token,
  2301. }
  2302. output = self.app.post('/test/default/branch/', # changing head to feature branch
  2303. data=data,
  2304. follow_redirects=True)
  2305. self.assertEqual(output.status_code, 200)
  2306. self.assertIn(
  2307. '<title>Settings - test - Pagure</title>', output.data)
  2308. self.assertIn('<h3>Settings for test</h3>', output.data)
  2309. self.assertIn(
  2310. '<select class="c-select" id="branches" name="branches">'
  2311. '<option selected value="feature">feature</option>'
  2312. '<option value="master">master</option>'
  2313. '</select>', output.data)
  2314. self.assertIn(
  2315. '</button>\n Default branch updated '
  2316. 'to feature', output.data)
  2317. data = {
  2318. 'branches': 'master',
  2319. 'csrf_token': csrf_token,
  2320. }
  2321. output = self.app.post('/test/default/branch/', # changing head to master branch
  2322. data=data,
  2323. follow_redirects=True)
  2324. self.assertEqual(output.status_code, 200)
  2325. self.assertIn(
  2326. '<title>Settings - test - Pagure</title>', output.data)
  2327. self.assertIn('<h3>Settings for test</h3>', output.data)
  2328. self.assertIn(
  2329. '<select class="c-select" id="branches" name="branches">'
  2330. '<option value="feature">feature</option>'
  2331. '<option selected value="master">master</option>'
  2332. '</select>', output.data)
  2333. self.assertIn(
  2334. '</button>\n Default branch updated '
  2335. 'to master', output.data)
  2336. def test_new_release(self):
  2337. """ Test the new_release endpoint. """
  2338. output = self.app.post('/foo/upload/')
  2339. self.assertEqual(output.status_code, 302)
  2340. user = tests.FakeUser()
  2341. with tests.user_set(pagure.APP, user):
  2342. output = self.app.post('/foo/upload/')
  2343. self.assertEqual(output.status_code, 404)
  2344. tests.create_projects(self.session)
  2345. output = self.app.post('/test/upload/')
  2346. self.assertEqual(output.status_code, 403)
  2347. user.username = 'pingou'
  2348. with tests.user_set(pagure.APP, user):
  2349. img = os.path.join(tests.HERE, 'placebo.png')
  2350. # Missing CSRF Token
  2351. data = {'filestream': open(img)}
  2352. output = self.app.post('/test/upload/', data=data)
  2353. self.assertEqual(output.status_code, 200)
  2354. self.assertIn('<h2>Upload a new release</h2>', output.data)
  2355. csrf_token = output.data.split(
  2356. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  2357. # Upload Ok but No git repo
  2358. data = {'filestream': open(img), 'csrf_token': csrf_token}
  2359. output = self.app.post(
  2360. '/test/upload/', data=data, follow_redirects=True)
  2361. self.assertEqual(output.status_code, 404)
  2362. self.assertIn(
  2363. '</button>\n File', output.data)
  2364. self.assertIn(
  2365. 'uploaded\n </div>', output.data)
  2366. self.assertIn('<p>No git repo found</p>', output.data)
  2367. repo = tests.create_projects_git(tests.HERE)
  2368. # Upload successful
  2369. data = {'filestream': open(img), 'csrf_token': csrf_token}
  2370. output = self.app.post(
  2371. '/test/upload/', data=data, follow_redirects=True)
  2372. self.assertEqual(output.status_code, 200)
  2373. self.assertIn(
  2374. '</button>\n File', output.data)
  2375. self.assertIn(
  2376. 'uploaded\n </div>', output.data)
  2377. self.assertIn('This project has not been tagged.', output.data)
  2378. @patch('pagure.ui.repo.admin_session_timedout')
  2379. def test_add_token(self, ast):
  2380. """ Test the add_token endpoint. """
  2381. ast.return_value = False
  2382. output = self.app.get('/foo/token/new/')
  2383. self.assertEqual(output.status_code, 302)
  2384. user = tests.FakeUser()
  2385. with tests.user_set(pagure.APP, user):
  2386. output = self.app.get('/foo/token/new/')
  2387. self.assertEqual(output.status_code, 404)
  2388. tests.create_projects(self.session)
  2389. output = self.app.get('/test/token/new/')
  2390. self.assertEqual(output.status_code, 403)
  2391. user.username = 'pingou'
  2392. with tests.user_set(pagure.APP, user):
  2393. output = self.app.get('/test/token/new/')
  2394. self.assertEqual(output.status_code, 200)
  2395. self.assertIn('<strong>Create a new token</strong>', output.data)
  2396. csrf_token = output.data.split(
  2397. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  2398. data = {'csrf_token': csrf_token}
  2399. ast.return_value = True
  2400. # Test when the session timed-out
  2401. output = self.app.post('/test/token/new/', data=data)
  2402. self.assertEqual(output.status_code, 302)
  2403. output = self.app.get('/')
  2404. self.assertEqual(output.status_code, 200)
  2405. self.assertIn(
  2406. '</button>\n Action canceled, try it '
  2407. 'again', output.data)
  2408. ast.return_value = False
  2409. # Missing acls
  2410. output = self.app.post('/test/token/new/', data=data)
  2411. self.assertEqual(output.status_code, 200)
  2412. self.assertIn('<strong>Create a new token</strong>', output.data)
  2413. data = {'csrf_token': csrf_token, 'acls': ['issue_create']}
  2414. output = self.app.post(
  2415. '/test/token/new/', data=data, follow_redirects=True)
  2416. self.assertEqual(output.status_code, 404)
  2417. self.assertIn(
  2418. '</button>\n Token created',
  2419. output.data)
  2420. self.assertIn('<p>No git repo found</p>', output.data)
  2421. repo = tests.create_projects_git(tests.HERE)
  2422. # Upload successful
  2423. data = {'csrf_token': csrf_token, 'acls': ['issue_create']}
  2424. output = self.app.post(
  2425. '/test/token/new/', data=data, follow_redirects=True)
  2426. self.assertEqual(output.status_code, 200)
  2427. self.assertIn(
  2428. '</button>\n Token created', output.data)
  2429. self.assertIn(
  2430. '<title>Settings - test - Pagure</title>', output.data)
  2431. self.assertIn('<h3>Settings for test</h3>', output.data)
  2432. self.assertIn(
  2433. '<span class="text-success"><strong>Valid</strong> until: ',
  2434. output.data)
  2435. @patch('pagure.ui.repo.admin_session_timedout')
  2436. def test_revoke_api_token(self, ast):
  2437. """ Test the revoke_api_token endpoint. """
  2438. ast.return_value = False
  2439. output = self.app.post('/foo/token/revoke/123')
  2440. self.assertEqual(output.status_code, 302)
  2441. user = tests.FakeUser()
  2442. with tests.user_set(pagure.APP, user):
  2443. output = self.app.post('/foo/token/revoke/123')
  2444. self.assertEqual(output.status_code, 404)
  2445. tests.create_projects(self.session)
  2446. output = self.app.post('/test/token/revoke/123')
  2447. self.assertEqual(output.status_code, 403)
  2448. user.username = 'pingou'
  2449. with tests.user_set(pagure.APP, user):
  2450. output = self.app.get('/test/token/new')
  2451. self.assertEqual(output.status_code, 200)
  2452. self.assertIn('<strong>Create a new token</strong>', output.data)
  2453. csrf_token = output.data.split(
  2454. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  2455. data = {'csrf_token': csrf_token}
  2456. ast.return_value = True
  2457. # Test when the session timed-out
  2458. output = self.app.post('/test/token/revoke/123', data=data)
  2459. self.assertEqual(output.status_code, 302)
  2460. output = self.app.get('/')
  2461. self.assertEqual(output.status_code, 200)
  2462. self.assertIn(
  2463. '</button>\n Action canceled, try it again',
  2464. output.data)
  2465. ast.return_value = False
  2466. output = self.app.post('/test/token/revoke/123', data=data)
  2467. self.assertEqual(output.status_code, 404)
  2468. self.assertIn('<p>Token not found</p>', output.data)
  2469. # Create a token to revoke
  2470. repo = tests.create_projects_git(tests.HERE)
  2471. data = {'csrf_token': csrf_token, 'acls': ['issue_create']}
  2472. output = self.app.post(
  2473. '/test/token/new/', data=data, follow_redirects=True)
  2474. self.assertEqual(output.status_code, 200)
  2475. self.assertIn(
  2476. '</button>\n Token created',
  2477. output.data)
  2478. # Existing token will expire in 60 days
  2479. repo = pagure.lib.get_project(self.session, 'test')
  2480. self.assertEqual(
  2481. repo.tokens[0].expiration.date(),
  2482. datetime.datetime.utcnow().date() + datetime.timedelta(days=60))
  2483. token = repo.tokens[0].id
  2484. output = self.app.post(
  2485. '/test/token/revoke/%s' % token,
  2486. data=data,
  2487. follow_redirects=True)
  2488. self.assertIn(
  2489. '<title>Settings - test - Pagure</title>', output.data)
  2490. self.assertIn(
  2491. '</button>\n Token revoked',
  2492. output.data)
  2493. # Existing token has been expired
  2494. repo = pagure.lib.get_project(self.session, 'test')
  2495. self.assertEqual(
  2496. repo.tokens[0].expiration.date(),
  2497. repo.tokens[0].created.date())
  2498. self.assertEqual(
  2499. repo.tokens[0].expiration.date(),
  2500. datetime.datetime.utcnow().date())
  2501. def test_delete_branch(self):
  2502. """ Test the delete_branch endpoint. """
  2503. output = self.app.post('/foo/b/master/delete')
  2504. # No project registered in the DB
  2505. self.assertEqual(output.status_code, 302)
  2506. tests.create_projects(self.session)
  2507. user = tests.FakeUser()
  2508. with tests.user_set(pagure.APP, user):
  2509. output = self.app.post('/foo/b/master/delete')
  2510. # Unknown repo
  2511. self.assertEqual(output.status_code, 404)
  2512. output = self.app.post('/test/b/master/delete')
  2513. self.assertEqual(output.status_code, 403)
  2514. user.username = 'pingou'
  2515. with tests.user_set(pagure.APP, user):
  2516. output = self.app.post('/test/b/master/delete')
  2517. self.assertEqual(output.status_code, 403)
  2518. self.assertIn(
  2519. '<p>You are not allowed to delete the master branch</p>',
  2520. output.data)
  2521. tests.create_projects_git(tests.HERE, bare=True)
  2522. output = self.app.post('/test/b/bar/delete')
  2523. self.assertEqual(output.status_code, 404)
  2524. self.assertIn('<p>Branch no found</p>', output.data)
  2525. # Add a branch that we can delete
  2526. path = os.path.join(tests.HERE, 'test.git')
  2527. tests.add_content_git_repo(path)
  2528. repo = pygit2.Repository(path)
  2529. repo.create_branch('foo', repo.head.get_object())
  2530. # Check before deletion
  2531. output = self.app.get('/test')
  2532. self.assertEqual(output.status_code, 200)
  2533. self.assertIn(
  2534. 'data-toggle="tooltip">foo',
  2535. output.data)
  2536. self.assertIn('<form id="delete_branch_form-foo"', output.data)
  2537. self.assertIn(
  2538. '<strong title="Currently viewing branch master"',
  2539. output.data)
  2540. # Delete the branch
  2541. output = self.app.post('/test/b/foo/delete', follow_redirects=True)
  2542. self.assertEqual(output.status_code, 200)
  2543. self.assertNotIn(
  2544. 'data-toggle="tooltip">foo',
  2545. output.data)
  2546. self.assertNotIn(
  2547. '<form id="delete_branch_form-foo"', output.data)
  2548. self.assertIn(
  2549. '<strong title="Currently viewing branch master"',
  2550. output.data)
  2551. # Add a branch with a '/' in its name that we can delete
  2552. path = os.path.join(tests.HERE, 'test.git')
  2553. tests.add_content_git_repo(path)
  2554. repo = pygit2.Repository(path)
  2555. repo.create_branch('feature/foo', repo.head.get_object())
  2556. # Check before deletion
  2557. output = self.app.get('/test')
  2558. self.assertEqual(output.status_code, 200)
  2559. self.assertIn(
  2560. 'data-toggle="tooltip">feature/foo',
  2561. output.data)
  2562. self.assertIn(
  2563. '<form id="delete_branch_form-feature_foo"', output.data)
  2564. self.assertIn(
  2565. '<strong title="Currently viewing branch master"',
  2566. output.data)
  2567. # Delete the branch
  2568. output = self.app.post('/test/b/feature/foo/delete', follow_redirects=True)
  2569. self.assertEqual(output.status_code, 200)
  2570. self.assertNotIn(
  2571. 'data-toggle="tooltip">feature/foo',
  2572. output.data)
  2573. self.assertNotIn(
  2574. '<form id="delete_branch_form-feature_foo"', output.data)
  2575. self.assertIn(
  2576. '<strong title="Currently viewing branch master"',
  2577. output.data)
  2578. def test_view_docs(self):
  2579. """ Test the view_docs endpoint. """
  2580. output = self.app.get('/docs/foo/')
  2581. # No project registered in the DB
  2582. self.assertEqual(output.status_code, 404)
  2583. tests.create_projects(self.session)
  2584. output = self.app.get('/docs/test/')
  2585. # No git repo associated
  2586. self.assertEqual(output.status_code, 404)
  2587. tests.create_projects_git(tests.HERE, bare=True)
  2588. output = self.app.get('/docs/test/')
  2589. self.assertEqual(output.status_code, 404)
  2590. def test_view_project_activity(self):
  2591. """ Test the view_project_activity endpoint. """
  2592. tests.create_projects(self.session)
  2593. # Project Exists, but No DATAGREPPER_URL set
  2594. output = self.app.get('/test/activity/')
  2595. self.assertEqual(output.status_code, 404)
  2596. # Project Exists, and DATAGREPPER_URL set
  2597. pagure.APP.config['DATAGREPPER_URL'] = 'foo'
  2598. output = self.app.get('/test/activity/')
  2599. self.assertEqual(output.status_code, 200)
  2600. self.assertIn(
  2601. '<title>Activity - test - Pagure</title>', output.data)
  2602. self.assertIn(
  2603. 'No activity reported on the test project', output.data)
  2604. # project doesnt exist
  2605. output = self.app.get('/foo/activity/')
  2606. self.assertEqual(output.status_code, 404)
  2607. def test_watch_repo(self):
  2608. """ Test the watch_repo endpoint. """
  2609. output = self.app.post('/watch/')
  2610. self.assertEqual(output.status_code, 405)
  2611. tests.create_projects(self.session)
  2612. user = tests.FakeUser()
  2613. user.username = 'pingou'
  2614. with tests.user_set(pagure.APP, user):
  2615. output = self.app.get('/new/')
  2616. self.assertEqual(output.status_code, 200)
  2617. self.assertIn('<strong>Create new Project</strong>', output.data)
  2618. csrf_token = output.data.split(
  2619. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  2620. data = {
  2621. 'csrf_token':csrf_token
  2622. }
  2623. output = self.app.post(
  2624. '/watch', data=data, follow_redirects=True)
  2625. self.assertEqual(output.status_code, 404)
  2626. output = self.app.post(
  2627. '/watch/foo', data=data, follow_redirects=True)
  2628. self.assertEqual(output.status_code, 404)
  2629. output = self.app.post(
  2630. '/watch/test/settings/2', data=data, follow_redirects=True)
  2631. self.assertEqual(output.status_code, 400)
  2632. output = self.app.post(
  2633. '/watch/test/settings/0', data=data, follow_redirects=True)
  2634. self.assertIn(
  2635. '</button>\n You are no longer'
  2636. ' watching this repo.', output.data)
  2637. output = self.app.post(
  2638. '/watch/test/settings/1', data=data, follow_redirects=True)
  2639. self.assertIn(
  2640. '</button>\n You are now'
  2641. ' watching this repo.', output.data)
  2642. item = pagure.lib.model.Project(
  2643. user_id=2, # foo
  2644. name='test',
  2645. description='test project #1',
  2646. hook_token='aaabbb',
  2647. parent_id=1,
  2648. )
  2649. self.session.add(item)
  2650. self.session.commit()
  2651. output = self.app.post(
  2652. '/watch/fork/foo/test/settings/0', data=data, follow_redirects=True)
  2653. self.assertIn(
  2654. '</button>\n You are no longer'
  2655. ' watching this repo.', output.data)
  2656. output = self.app.post(
  2657. '/watch/fork/foo/test/settings/1', data=data, follow_redirects=True)
  2658. self.assertIn(
  2659. '</button>\n You are now'
  2660. ' watching this repo.', output.data)
  2661. if __name__ == '__main__':
  2662. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskRepotests)
  2663. unittest.TextTestRunner(verbosity=2).run(SUITE)