test_pagure_flask_ui_repo.py 121 KB

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