test_pagure_flask_ui_repo.py 125 KB

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