test_pagure_flask_ui_repo.py 128 KB

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