test_pagure_flask_ui_app.py 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. import datetime
  11. import unittest
  12. import shutil
  13. import sys
  14. import tempfile
  15. import os
  16. import six
  17. import json
  18. import pygit2
  19. from mock import patch, MagicMock
  20. sys.path.insert(0, os.path.join(os.path.dirname(
  21. os.path.abspath(__file__)), '..'))
  22. import pagure.lib
  23. import tests
  24. class PagureFlaskApptests(tests.Modeltests):
  25. """ Tests for flask app controller of pagure """
  26. def test_watch_list(self):
  27. ''' Test for watch list of a user '''
  28. user = tests.FakeUser(username='pingou')
  29. with tests.user_set(self.app.application, user):
  30. output = self.app.get('/', follow_redirects=True)
  31. output_text = output.get_data(as_text=True)
  32. self.assertIn(
  33. '<div class="text-center">You have no Projects</div>',
  34. output_text)
  35. tests.create_projects(self.session)
  36. output = self.app.get('/', follow_redirects=True)
  37. output_text = output.get_data(as_text=True)
  38. self.assertIn(
  39. '<h4 class="font-weight-bold mb-0">My Projects</h4>',
  40. output_text)
  41. def test_view_users(self):
  42. """ Test the view_users endpoint. """
  43. output = self.app.get('/users/?page=abc')
  44. self.assertEqual(output.status_code, 200)
  45. output_text = output.get_data(as_text=True)
  46. self.assertIn(
  47. '<h3 class="mb-3 font-weight-bold">\n Users '
  48. '<span class="badge badge-secondary">2</span>', output_text)
  49. self.assertIn(
  50. '<a href="/user/pingou">\n '
  51. '<div class="nowrap"><strong>pingou</strong>',
  52. output_text)
  53. self.assertIn(
  54. '<a href="/user/foo">\n '
  55. '<div class="nowrap"><strong>foo</strong>',
  56. output_text)
  57. @patch.dict('pagure.config.config', {'ITEM_PER_PAGE': 2})
  58. def test_view_user_repo_cnt(self):
  59. """ Test the repo counts on the view_user endpoint. """
  60. tests.create_projects(self.session)
  61. self.gitrepos = tests.create_projects_git(
  62. pagure.config.config['GIT_FOLDER'])
  63. output = self.app.get('/user/pingou')
  64. self.assertEqual(output.status_code, 200)
  65. output_text = output.get_data(as_text=True)
  66. self.assertIn(
  67. """<span>
  68. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  69. <span class="d-none d-md-inline">Projects&nbsp;</span>
  70. </span>
  71. <div class="ml-auto">
  72. <span class="badge badge-secondary">
  73. 3
  74. </span>
  75. </div>""", output_text)
  76. self.assertIn(
  77. """<span>
  78. <i class="fa fa-fw text-muted fa-code-fork"></i>
  79. <span class="d-none d-md-inline">Forks&nbsp;</span>
  80. </span>
  81. <div class="ml-auto">
  82. <span class="badge badge-secondary">
  83. 0
  84. </span>
  85. </div>""", output_text)
  86. def test_view_user(self):
  87. """ Test the view_user endpoint. """
  88. output = self.app.get('/user/pingou?repopage=abc&forkpage=def')
  89. self.assertEqual(output.status_code, 200)
  90. output_text = output.get_data(as_text=True)
  91. self.assertIn(
  92. """<span>
  93. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  94. <span class="d-none d-md-inline">Projects&nbsp;</span>
  95. </span>
  96. <div class="ml-auto">
  97. <span class="badge badge-secondary">
  98. 0
  99. </span>
  100. </div>""", output_text)
  101. self.assertIn(
  102. """<span>
  103. <i class="fa fa-fw text-muted fa-code-fork"></i>
  104. <span class="d-none d-md-inline">Forks&nbsp;</span>
  105. </span>
  106. <div class="ml-auto">
  107. <span class="badge badge-secondary">
  108. 0
  109. </span>
  110. </div>""", output_text)
  111. tests.create_projects(self.session)
  112. self.gitrepos = tests.create_projects_git(
  113. pagure.config.config['GIT_FOLDER'])
  114. output = self.app.get('/user/pingou?repopage=abc&forkpage=def')
  115. self.assertEqual(output.status_code, 200)
  116. output_text = output.get_data(as_text=True)
  117. self.assertIn(
  118. """<span>
  119. <i class="fa fa-fw text-muted fa-calendar-o fa-rotate-270"></i>
  120. <span class="d-none d-md-inline">Projects&nbsp;</span>
  121. </span>
  122. <div class="ml-auto">
  123. <span class="badge badge-secondary">
  124. 3
  125. </span>
  126. </div>""", output_text)
  127. self.assertIn(
  128. """<span>
  129. <i class="fa fa-fw text-muted fa-code-fork"></i>
  130. <span class="d-none d-md-inline">Forks&nbsp;</span>
  131. </span>
  132. <div class="ml-auto">
  133. <span class="badge badge-secondary">
  134. 0
  135. </span>
  136. </div>""", output_text)
  137. self.assertNotIn(
  138. '<a class="page-link" href="#" tabindex="-1">page 1 of 2</a>',
  139. output_text)
  140. @patch.dict('pagure.config.config', {'ENABLE_UI_NEW_PROJECTS': False})
  141. def test_new_project_when_turned_off_in_the_ui(self):
  142. """ Test the new_project endpoint when new project creation is
  143. not allowed in the UI of this pagure instance. """
  144. user = tests.FakeUser(username='foo')
  145. with tests.user_set(self.app.application, user):
  146. output = self.app.get('/new/')
  147. self.assertEqual(output.status_code, 404)
  148. data = {
  149. 'description': 'Project #1',
  150. 'name': 'project-1',
  151. }
  152. output = self.app.post('/new/', data=data, follow_redirects=True)
  153. self.assertEqual(output.status_code, 404)
  154. @patch.dict('pagure.config.config', {'ENABLE_UI_NEW_PROJECTS': False})
  155. def test_new_project_button_when_turned_off_in_the_ui_no_project(self):
  156. """ Test the index endpoint when new project creation is
  157. not allowed in the UI of this pagure instance. """
  158. user = tests.FakeUser(username='foo')
  159. with tests.user_set(self.app.application, user):
  160. output = self.app.get('/', follow_redirects=True)
  161. self.assertEqual(output.status_code, 200)
  162. output_text = output.get_data(as_text=True)
  163. self.assertIn(
  164. '<h4 class="font-weight-bold mb-0">My Projects</h4>',
  165. output_text)
  166. # master template
  167. self.assertNotIn(
  168. '<span class="oi" data-glyph="plus" title="Create New"',
  169. output_text)
  170. # index_auth template
  171. self.assertNotIn(
  172. 'title="Create New Project" aria-hidden="true">',
  173. output_text)
  174. @patch.dict('pagure.config.config', {'ENABLE_UI_NEW_PROJECTS': False})
  175. def test_new_project_button_when_turned_off_in_the_ui_w_project(self):
  176. """ Test the index endpoint when new project creation is
  177. not allowed in the UI of this pagure instance. """
  178. tests.create_projects(self.session)
  179. user = tests.FakeUser(username='pingou')
  180. with tests.user_set(self.app.application, user):
  181. output = self.app.get('/', follow_redirects=True)
  182. self.assertEqual(output.status_code, 200)
  183. output_text = output.get_data(as_text=True)
  184. self.assertIn(
  185. '<h4 class="font-weight-bold mb-0">My Projects</h4>',
  186. output_text)
  187. # master template
  188. self.assertNotIn(
  189. '<span class="oi" data-glyph="plus" title="Create New"',
  190. output_text)
  191. # index_auth template
  192. self.assertNotIn(
  193. 'title="Create New Project" aria-hidden="true">',
  194. output_text)
  195. def test_new_project_when_turned_off(self):
  196. """ Test the new_project endpoint when new project creation is
  197. not allowed in the pagure instance. """
  198. #turn the project creation off
  199. pagure.config.config['ENABLE_NEW_PROJECTS'] = False
  200. # Before
  201. projects = pagure.lib.search_projects(self.session)
  202. self.assertEqual(len(projects), 0)
  203. self.assertFalse(os.path.exists(
  204. os.path.join(self.path, 'repos', 'project-1.git')))
  205. self.assertFalse(os.path.exists(
  206. os.path.join(self.path, 'repos', 'tickets', 'project-1.git')))
  207. self.assertFalse(os.path.exists(
  208. os.path.join(self.path, 'repos', 'docs', 'project-1.git')))
  209. self.assertFalse(os.path.exists(
  210. os.path.join(self.path, 'repos', 'requests', 'project-1.git')))
  211. user = tests.FakeUser()
  212. with tests.user_set(self.app.application, user):
  213. output = self.app.get('/new/')
  214. self.assertEqual(output.status_code, 404)
  215. #just get the csrf token
  216. pagure.config.config['ENABLE_NEW_PROJECTS'] = True
  217. output = self.app.get('/new/')
  218. pagure.config.config['ENABLE_NEW_PROJECTS'] = False
  219. csrf_token = output.get_data(as_text=True).split(
  220. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  221. data = {
  222. 'description': 'Project #1',
  223. 'name': 'project-1',
  224. }
  225. user.username = 'foo'
  226. with tests.user_set(self.app.application, user):
  227. data['csrf_token'] = csrf_token
  228. output = self.app.post('/new/', data=data, follow_redirects=True)
  229. self.assertEqual(output.status_code, 404)
  230. #After
  231. projects = pagure.lib.search_projects(self.session)
  232. self.assertEqual(len(projects), 0)
  233. self.assertFalse(os.path.exists(
  234. os.path.join(self.path, 'repos', 'project-1.git')))
  235. self.assertFalse(os.path.exists(
  236. os.path.join(self.path, 'repos', 'tickets', 'project-1.git')))
  237. self.assertFalse(os.path.exists(
  238. os.path.join(self.path, 'repos', 'docs', 'project-1.git')))
  239. self.assertFalse(os.path.exists(
  240. os.path.join(self.path, 'repos', 'requests', 'project-1.git')))
  241. pagure.config.config['ENABLE_NEW_PROJECTS'] = True
  242. def test_new_project(self):
  243. """ Test the new_project endpoint. """
  244. # Before
  245. projects = pagure.lib.search_projects(self.session)
  246. self.assertEqual(len(projects), 0)
  247. self.assertFalse(os.path.exists(
  248. os.path.join(self.path, 'repos', 'project#1.git')))
  249. self.assertFalse(os.path.exists(
  250. os.path.join(self.path, 'repos', 'tickets', 'project#1.git')))
  251. self.assertFalse(os.path.exists(
  252. os.path.join(self.path, 'repos', 'docs', 'project#1.git')))
  253. self.assertFalse(os.path.exists(
  254. os.path.join(self.path, 'repos', 'requests', 'project#1.git')))
  255. user = tests.FakeUser()
  256. with tests.user_set(self.app.application, user):
  257. output = self.app.get('/new/')
  258. self.assertEqual(output.status_code, 200)
  259. output_text = output.get_data(as_text=True)
  260. self.assertIn(
  261. '<strong>Create new Project</strong>', output_text)
  262. csrf_token = output_text.split(
  263. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  264. data = {
  265. 'description': 'Project #1',
  266. }
  267. output = self.app.post('/new/', data=data)
  268. self.assertEqual(output.status_code, 200)
  269. output_text = output.get_data(as_text=True)
  270. self.assertIn(
  271. '<strong>Create new Project</strong>', output_text)
  272. self.assertIn(
  273. '<small>\n This field is required.&nbsp;\n'
  274. ' </small>', output_text)
  275. data['name'] = 'project-1'
  276. output = self.app.post('/new/', data=data)
  277. self.assertEqual(output.status_code, 200)
  278. output_text = output.get_data(as_text=True)
  279. self.assertIn('<strong>Create new Project</strong>', output_text)
  280. self.assertNotIn(
  281. '<small>\n This field is required.&nbsp;\n'
  282. ' </small>', output_text)
  283. data['csrf_token'] = csrf_token
  284. output = self.app.post('/new/', data=data)
  285. self.assertEqual(output.status_code, 200)
  286. output_text = output.get_data(as_text=True)
  287. self.assertIn('<strong>Create new Project</strong>', output_text)
  288. self.assertIn(
  289. 'No user '
  290. '&#34;username&#34; found',
  291. output_text)
  292. user.username = 'foo'
  293. with tests.user_set(self.app.application, user):
  294. data['csrf_token'] = csrf_token
  295. output = self.app.post('/new/', data=data, follow_redirects=True)
  296. self.assertEqual(output.status_code, 200)
  297. output_text = output.get_data(as_text=True)
  298. self.assertIn(
  299. '<div class="projectinfo my-3">\nProject #1',
  300. output_text)
  301. self.assertIn('<p>This repo is brand new!</p>',
  302. output_text)
  303. self.assertIn(
  304. '<title>Overview - project-1 - Pagure</title>', output_text)
  305. # After
  306. projects = pagure.lib.search_projects(self.session)
  307. self.assertEqual(len(projects), 1)
  308. self.assertTrue(os.path.exists(
  309. os.path.join(self.path, 'repos', 'project-1.git')))
  310. self.assertTrue(os.path.exists(
  311. os.path.join(self.path, 'repos', 'tickets', 'project-1.git')))
  312. self.assertTrue(os.path.exists(
  313. os.path.join(self.path, 'repos', 'docs', 'project-1.git')))
  314. self.assertTrue(os.path.exists(
  315. os.path.join(self.path, 'repos', 'requests', 'project-1.git')))
  316. @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': ['pingou'],
  317. 'ALLOW_ADMIN_IGNORE_EXISTING_REPOS': True})
  318. def test_adopt_repos(self):
  319. """ Test the new_project endpoint with existing git repo. """
  320. # Before
  321. projects = pagure.lib.search_projects(self.session)
  322. self.assertEqual(len(projects), 0)
  323. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  324. tests.add_content_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  325. user = tests.FakeUser(username='pingou')
  326. with tests.user_set(self.app.application, user):
  327. data = {
  328. 'csrf_token': self.get_csrf(),
  329. 'name': 'test',
  330. 'description': 'Project #1',
  331. }
  332. output = self.app.post('/new/', data=data, follow_redirects=True)
  333. self.assertEqual(output.status_code, 200)
  334. output_text = output.get_data(as_text=True)
  335. self.assertIn('The main repo test.git already exists', output_text)
  336. data['ignore_existing_repos'] = 'y'
  337. output = self.app.post('/new/', data=data, follow_redirects=True)
  338. self.assertEqual(output.status_code, 200)
  339. output_text = output.get_data(as_text=True)
  340. self.assertIn("Alice Author", output_text)
  341. @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': [],
  342. 'USERS_IGNORE_EXISTING_REPOS': ['pingou']})
  343. def test_adopt_repos_non_admin(self):
  344. """ Test the new_project endpoint with existing git repo for non-admins. """
  345. # Before
  346. projects = pagure.lib.search_projects(self.session)
  347. self.assertEqual(len(projects), 0)
  348. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  349. tests.add_content_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  350. user = tests.FakeUser(username='pingou')
  351. with tests.user_set(self.app.application, user):
  352. data = {
  353. 'csrf_token': self.get_csrf(),
  354. 'name': 'test',
  355. 'description': 'Project #1',
  356. }
  357. output = self.app.post('/new/', data=data, follow_redirects=True)
  358. self.assertEqual(output.status_code, 200)
  359. output_text = output.get_data(as_text=True)
  360. self.assertIn('The main repo test.git already exists', output_text)
  361. data['ignore_existing_repos'] = 'y'
  362. output = self.app.post('/new/', data=data, follow_redirects=True)
  363. self.assertEqual(output.status_code, 200)
  364. output_text = output.get_data(as_text=True)
  365. self.assertIn("Alice Author", output_text)
  366. @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': [],
  367. 'USERS_IGNORE_EXISTING_REPOS': []})
  368. def test_adopt_repos_not_allowed(self):
  369. """ Test the new_project endpoint with existing git repo for no access. """
  370. # Before
  371. projects = pagure.lib.search_projects(self.session)
  372. self.assertEqual(len(projects), 0)
  373. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  374. tests.add_content_git_repo(os.path.join(self.path, 'repos', 'test.git'))
  375. user = tests.FakeUser(username='pingou')
  376. with tests.user_set(self.app.application, user):
  377. data = {
  378. 'csrf_token': self.get_csrf(),
  379. 'name': 'test',
  380. 'description': 'Project #1',
  381. }
  382. output = self.app.post('/new/', data=data, follow_redirects=True)
  383. self.assertEqual(output.status_code, 200)
  384. output_text = output.get_data(as_text=True)
  385. self.assertIn('The main repo test.git already exists', output_text)
  386. data['ignore_existing_repos'] = 'y'
  387. output = self.app.post('/new/', data=data, follow_redirects=True)
  388. self.assertEqual(output.status_code, 200)
  389. output_text = output.get_data(as_text=True)
  390. self.assertIn('The main repo test.git already exists', output_text)
  391. @patch.dict('pagure.config.config', {'PROJECT_NAME_REGEX': '^1[a-z]*$'})
  392. def test_new_project_diff_regex(self):
  393. """ Test the new_project endpoint with a different regex. """
  394. # Before
  395. projects = pagure.lib.search_projects(self.session)
  396. self.assertEqual(len(projects), 0)
  397. user = tests.FakeUser(username='foo')
  398. with tests.user_set(self.app.application, user):
  399. output = self.app.get('/new/')
  400. self.assertEqual(output.status_code, 200)
  401. output_text = output.get_data(as_text=True)
  402. self.assertIn(
  403. '<strong>Create new Project</strong>', output_text)
  404. csrf_token = self.get_csrf(output=output)
  405. data = {
  406. 'description': 'Project #1',
  407. 'name': 'project-1',
  408. 'csrf_token': csrf_token,
  409. }
  410. output = self.app.post('/new/', data=data, follow_redirects=True)
  411. self.assertEqual(output.status_code, 200)
  412. output_text = output.get_data(as_text=True)
  413. self.assertIn(
  414. '<title>New project - Pagure</title>', output_text)
  415. self.assertIn(
  416. '<strong>Create new Project</strong>', output_text)
  417. self.assertIn(
  418. '<small>\n Invalid input.&nbsp;\n'
  419. ' </small>', output_text)
  420. @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
  421. def test_new_project_private(self):
  422. """ Test the new_project endpoint for a private project. """
  423. # Before
  424. projects = pagure.lib.search_projects(self.session)
  425. self.assertEqual(len(projects), 0)
  426. self.assertFalse(os.path.exists(
  427. os.path.join(self.path, 'repos', 'foo', 'project#1.git')))
  428. self.assertFalse(os.path.exists(
  429. os.path.join(self.path, 'repos', 'tickets', 'foo', 'project#1.git')))
  430. self.assertFalse(os.path.exists(
  431. os.path.join(self.path, 'repos', 'docs', 'foo', 'project#1.git')))
  432. self.assertFalse(os.path.exists(
  433. os.path.join(self.path, 'repos', 'requests', 'foo', 'project#1.git')))
  434. user = tests.FakeUser()
  435. with tests.user_set(self.app.application, user):
  436. output = self.app.get('/new/')
  437. self.assertEqual(output.status_code, 200)
  438. self.assertIn(
  439. '<strong>Create new Project</strong>', output.get_data(as_text=True))
  440. csrf_token = self.get_csrf(output=output)
  441. data = {
  442. 'description': 'Project #1',
  443. 'private': True,
  444. }
  445. output = self.app.post('/new/', data=data)
  446. self.assertEqual(output.status_code, 200)
  447. output_text = output.get_data(as_text=True)
  448. self.assertIn(
  449. '<strong>Create new Project</strong>', output_text)
  450. self.assertIn(
  451. '<small>\n This field is required.&nbsp;\n'
  452. ' </small>', output_text)
  453. data['name'] = 'project-1'
  454. output = self.app.post('/new/', data=data)
  455. self.assertEqual(output.status_code, 200)
  456. output_text = output.get_data(as_text=True)
  457. self.assertIn('<strong>Create new Project</strong>', output_text)
  458. self.assertNotIn(
  459. '<small>\n This field is required.&nbsp;\n'
  460. ' </small>', output_text)
  461. data['csrf_token'] = csrf_token
  462. output = self.app.post('/new/', data=data)
  463. self.assertEqual(output.status_code, 200)
  464. output_text = output.get_data(as_text=True)
  465. self.assertIn('<strong>Create new Project</strong>', output_text)
  466. self.assertIn(
  467. 'No user '
  468. '&#34;username&#34; found',
  469. output_text)
  470. user.username = 'foo'
  471. with tests.user_set(self.app.application, user):
  472. data['csrf_token'] = csrf_token
  473. output = self.app.post('/new/', data=data, follow_redirects=True)
  474. self.assertEqual(output.status_code, 200)
  475. output_text = output.get_data(as_text=True)
  476. self.assertIn(
  477. '<div class="projectinfo my-3">\nProject #1',
  478. output_text)
  479. self.assertIn('<p>This repo is brand new!</p>',
  480. output_text)
  481. self.assertIn(
  482. '<title>Overview - foo/project-1 - Pagure</title>', output_text)
  483. # After
  484. projects = pagure.lib.search_projects(self.session)
  485. self.assertEqual(len(projects), 0)
  486. projects = pagure.lib.search_projects(self.session, private=True)
  487. self.assertEqual(len(projects), 1)
  488. self.assertTrue(os.path.exists(
  489. os.path.join(self.path, 'repos', 'foo', 'project-1.git')))
  490. self.assertTrue(os.path.exists(
  491. os.path.join(self.path, 'repos', 'tickets', 'foo', 'project-1.git')))
  492. self.assertTrue(os.path.exists(
  493. os.path.join(self.path, 'repos', 'docs', 'foo', 'project-1.git')))
  494. self.assertTrue(os.path.exists(
  495. os.path.join(self.path, 'repos', 'requests', 'foo', 'project-1.git')))
  496. def test_non_ascii_new_project(self):
  497. """ Test the new_project endpoint with a non-ascii project. """
  498. # Before
  499. projects = pagure.lib.search_projects(self.session)
  500. self.assertEqual(len(projects), 0)
  501. self.assertFalse(os.path.exists(
  502. os.path.join(self.path, 'repos', 'project-1.git')))
  503. self.assertFalse(os.path.exists(
  504. os.path.join(self.path, 'repos', 'tickets', 'project-1.git')))
  505. self.assertFalse(os.path.exists(
  506. os.path.join(self.path, 'repos', 'docs', 'project-1.git')))
  507. self.assertFalse(os.path.exists(
  508. os.path.join(self.path, 'repos', 'requests', 'project-1.git')))
  509. user = tests.FakeUser()
  510. user.username = 'foo'
  511. with tests.user_set(self.app.application, user):
  512. output = self.app.get('/new/')
  513. self.assertEqual(output.status_code, 200)
  514. output_text = output.get_data(as_text=True)
  515. self.assertIn(
  516. '<strong>Create new Project</strong>', output_text)
  517. csrf_token = output_text.split(
  518. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  519. data = {
  520. 'description': 'Prõjéctö #1',
  521. 'name': 'project-1',
  522. 'csrf_token': csrf_token,
  523. 'create_readme': True,
  524. }
  525. output = self.app.post('/new/', data=data, follow_redirects=True)
  526. self.assertEqual(output.status_code, 200)
  527. output_text = output.get_data(as_text=True)
  528. self.assertIn(
  529. '<div class="projectinfo my-3">\nPrõjéctö #1',
  530. output_text)
  531. self.assertIn(
  532. '''<section class="readme">
  533. <h1>project-1</h1>
  534. <p>Prõjéctö #1</p>
  535. </section>''',
  536. output_text)
  537. data = {
  538. 'description': 'Мой первый суперский репозиторий',
  539. 'name': 'project-2',
  540. 'csrf_token': csrf_token,
  541. 'create_readme': True,
  542. }
  543. output = self.app.post('/new/', data=data, follow_redirects=True)
  544. self.assertEqual(output.status_code, 200)
  545. output_text = output.get_data(as_text=True)
  546. self.assertIn(
  547. '<div class="projectinfo my-3">\nМой первый суперский репозиторий',
  548. output_text)
  549. self.assertIn(
  550. '''<section class="readme">
  551. <h1>project-2</h1>
  552. <p>Мой первый суперский репозиторий</p>
  553. </section>''',
  554. output_text)
  555. # After
  556. projects = pagure.lib.search_projects(self.session)
  557. self.assertEqual(len(projects), 2)
  558. for project in ['project-1', 'project-2']:
  559. self.assertTrue(os.path.exists(
  560. os.path.join(self.path, 'repos', '%s.git' % project)))
  561. self.assertTrue(os.path.exists(
  562. os.path.join(self.path, 'repos', 'tickets', '%s.git' % project)))
  563. self.assertTrue(os.path.exists(
  564. os.path.join(self.path, 'repos', 'docs', '%s.git' % project)))
  565. self.assertTrue(os.path.exists(
  566. os.path.join(self.path, 'repos', 'requests', '%s.git' % project)))
  567. @patch('pygit2.init_repository', wraps=pygit2.init_repository)
  568. def test_new_project_with_template(self, pygit2init):
  569. """ Test the new_project endpoint for a new project with a template set.
  570. """
  571. # Before
  572. projects = pagure.lib.search_projects(self.session)
  573. self.assertEqual(len(projects), 0)
  574. self.assertFalse(os.path.exists(
  575. os.path.join(self.path, 'repos', 'project-1.git')))
  576. self.assertFalse(os.path.exists(
  577. os.path.join(self.path, 'repos', 'tickets', 'project-1.git')))
  578. self.assertFalse(os.path.exists(
  579. os.path.join(self.path, 'repos', 'docs', 'project-1.git')))
  580. self.assertFalse(os.path.exists(
  581. os.path.join(self.path, 'repos', 'requests', 'project-1.git')))
  582. user = tests.FakeUser()
  583. user.username = 'foo'
  584. with tests.user_set(self.app.application, user):
  585. output = self.app.get('/new/')
  586. self.assertEqual(output.status_code, 200)
  587. self.assertIn(
  588. '<strong>Create new Project</strong>', output.get_data(as_text=True))
  589. csrf_token = self.get_csrf(output=output)
  590. data = {
  591. 'description': 'test',
  592. 'name': 'project-1',
  593. 'csrf_token': csrf_token,
  594. 'create_readme': True,
  595. }
  596. output = self.app.post('/new/', data=data, follow_redirects=True)
  597. self.assertEqual(output.status_code, 200)
  598. self.assertIn(
  599. '<div class="projectinfo my-3">\ntest',
  600. output.get_data(as_text=True))
  601. self.assertEqual(pygit2init.call_count, 4)
  602. pygit2init.assert_any_call(
  603. '%s/repos/project-1.git' % self.path,
  604. bare=True, template_path=None)
  605. path = os.path.join(self.path, 'repos', 'project-1.git')
  606. with patch.dict(
  607. 'pagure.config.config',
  608. {'PROJECT_TEMPLATE_PATH': path}):
  609. data = {
  610. 'description': 'test2',
  611. 'name': 'project-2',
  612. 'csrf_token': csrf_token,
  613. 'create_readme': True,
  614. }
  615. output = self.app.post('/new/', data=data, follow_redirects=True)
  616. self.assertEqual(output.status_code, 200)
  617. self.assertIn(
  618. '<div class="projectinfo my-3">\ntest2',
  619. output.get_data(as_text=True))
  620. self.assertEqual(pygit2init.call_count, 8)
  621. pygit2init.assert_any_call(
  622. '%s/repos/project-2.git' % self.path,
  623. bare=True,
  624. template_path='%s/repos/project-1.git' % self.path)
  625. # After
  626. projects = pagure.lib.search_projects(self.session)
  627. self.assertEqual(len(projects), 2)
  628. for project in ['project-1', 'project-2']:
  629. self.assertTrue(os.path.exists(
  630. os.path.join(self.path, 'repos', '%s.git' % project)))
  631. self.assertTrue(os.path.exists(
  632. os.path.join(self.path, 'repos', 'tickets', '%s.git' % project)))
  633. self.assertTrue(os.path.exists(
  634. os.path.join(self.path, 'repos', 'docs', '%s.git' % project)))
  635. self.assertTrue(os.path.exists(
  636. os.path.join(self.path, 'repos', 'requests', '%s.git' % project)))
  637. @patch.dict('pagure.config.config', {'CASE_SENSITIVE': True})
  638. def test_new_project_case_sensitive(self):
  639. tests.create_projects(self.session)
  640. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  641. output = self.app.get('/test')
  642. self.assertEqual(output.status_code, 200)
  643. output = self.app.get('/TEST')
  644. self.assertEqual(output.status_code, 404)
  645. user = tests.FakeUser()
  646. user.username = 'foo'
  647. with tests.user_set(self.app.application, user):
  648. output = self.app.get('/new/')
  649. self.assertEqual(output.status_code, 200)
  650. csrf_token = self.get_csrf(output=output)
  651. data = {
  652. 'description': 'TEST',
  653. 'name': 'TEST',
  654. 'csrf_token': csrf_token,
  655. 'create_readme': True,
  656. }
  657. self.app.post('/new/', data=data, follow_redirects=True)
  658. output = self.app.get('/TEST')
  659. self.assertEqual(output.status_code, 200)
  660. @patch('pagure.ui.app.admin_session_timedout')
  661. def test_user_settings(self, ast):
  662. """ Test the user_settings endpoint. """
  663. ast.return_value = False
  664. self.test_new_project()
  665. user = tests.FakeUser()
  666. with tests.user_set(self.app.application, user):
  667. output = self.app.get('/settings/')
  668. self.assertEqual(output.status_code, 404)
  669. self.assertIn('<h2>Page not found (404)</h2>', output.get_data(as_text=True))
  670. user.username = 'foo'
  671. with tests.user_set(self.app.application, user):
  672. output = self.app.get('/settings/')
  673. self.assertEqual(output.status_code, 200)
  674. output_text = output.get_data(as_text=True)
  675. self.assertIn(
  676. '<title>foo\'s settings - Pagure</title>', output_text)
  677. ast.return_value = True
  678. output = self.app.get('/settings/')
  679. self.assertEqual(output.status_code, 302)
  680. @patch('pagure.decorators.admin_session_timedout')
  681. def test_add_user_sshkey(self, ast):
  682. """ Test the add_user_sshkey endpoint. """
  683. ast.return_value = False
  684. # User not logged in
  685. output = self.app.get('/settings/')
  686. self.assertEqual(output.status_code, 302)
  687. ast.return_value = False
  688. user = tests.FakeUser(username='pingou')
  689. with tests.user_set(self.app.application, user):
  690. output = self.app.get('/settings', follow_redirects=True)
  691. self.assertEqual(output.status_code, 200)
  692. output_text = output.get_data(as_text=True)
  693. self.assertIn('<strong>Add SSH key', output_text)
  694. csrf_token = self.get_csrf(output=output)
  695. data = {
  696. 'ssh_key': 'asdf',
  697. }
  698. # No CSRF token
  699. output = self.app.post(
  700. '/settings/usersettings/addkey', data=data , follow_redirects=True)
  701. self.assertEqual(output.status_code, 200)
  702. output_text = output.get_data(as_text=True)
  703. self.assertIn('<strong>Add SSH key', output_text)
  704. data['csrf_token'] = csrf_token
  705. # First, invalid SSH key
  706. output = self.app.post(
  707. '/settings/usersettings/addkey', data=data, follow_redirects=True)
  708. self.assertEqual(output.status_code, 200)
  709. output_text = output.get_data(as_text=True)
  710. self.assertIn('<strong>Add SSH key', output_text)
  711. self.assertIn('SSH key invalid', output_text)
  712. # Next up, multiple SSH keys
  713. data['ssh_key'] = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==\nssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q=='
  714. output = self.app.post(
  715. '/settings/usersettings/addkey', data=data, follow_redirects=True)
  716. self.assertEqual(output.status_code, 200)
  717. output_text = output.get_data(as_text=True)
  718. self.assertIn('Please add single SSH keys.', output_text)
  719. # Now, a valid SSH key
  720. data['ssh_key'] = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q=='
  721. output = self.app.post(
  722. '/settings/usersettings/addkey', data=data, follow_redirects=True)
  723. self.assertEqual(output.status_code, 200)
  724. output_text = output.get_data(as_text=True)
  725. self.assertIn(
  726. "<title>pingou's settings - Pagure</title>", output_text)
  727. self.assertIn('SSH key added', output_text)
  728. self.assertNotIn('Push Access', output_text)
  729. # And now, adding the same key
  730. output = self.app.post(
  731. '/settings/usersettings/addkey', data=data, follow_redirects=True)
  732. self.assertEqual(output.status_code, 200)
  733. output_text = output.get_data(as_text=True)
  734. self.assertIn('SSH key already exists', output_text)
  735. # And next, a key with push access
  736. data['ssh_key'] = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC9Xwc2RDzPBhlEDARfHldGjudIVoa04tqT1JVKGQmyllTFz7Rb8CngQL3e7zyNzotnhwYKHdoiLlPkVEiDee4dWMUe48ilqId+FJZQGhyv8fu4BoFdE1AJUVylzmltbLg14VqG5gjTpXgtlrEva9arKwBMHJjRYc8ScaSn3OgyQw=='
  737. output = self.app.post(
  738. '/settings/usersettings/addkey', data=data, follow_redirects=True)
  739. self.assertEqual(output.status_code, 200)
  740. output_text = output.get_data(as_text=True)
  741. self.assertIn(
  742. "<title>pingou's settings - Pagure</title>", output_text)
  743. self.assertIn('SSH key added', output_text)
  744. @patch('pagure.decorators.admin_session_timedout')
  745. def test_remove_user_sshkey(self, ast):
  746. """ Test the remove_sshkey endpoint. """
  747. ast.return_value = False
  748. user = tests.FakeUser()
  749. # User not logged in
  750. output = self.app.post('/settings/usersettings/removekey/1')
  751. self.assertEqual(output.status_code, 302)
  752. user.username = 'pingou'
  753. with tests.user_set(self.app.application, user):
  754. data = {'csrf_token': self.get_csrf()}
  755. output = self.app.post(
  756. '/settings/usersettings/removekey/1', data=data, follow_redirects=True)
  757. self.assertEqual(output.status_code, 200)
  758. output_text = output.get_data(as_text=True)
  759. self.assertIn(
  760. "<title>pingou's settings - Pagure</title>", output_text)
  761. self.assertIn('SSH key does not exist', output_text)
  762. # Add a deploy key to a project
  763. pingou = pagure.lib.get_user(self.session, 'pingou')
  764. msg = pagure.lib.add_sshkey_to_project_or_user(
  765. session=self.session,
  766. ssh_key='ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAzBMSIlvPRaEiLOTVInErkRIw9CzQQcnslDekAn1jFnGf+SNa1acvbTiATbCX71AA03giKrPxPH79dxcC7aDXerc6zRcKjJs6MAL9PrCjnbyxCKXRNNZU5U9X/DLaaL1b3caB+WD6OoorhS3LTEtKPX8xyjOzhf3OQSzNjhJp5Q==',
  767. user=pingou,
  768. pushaccess=True,
  769. creator=pingou,
  770. )
  771. self.session.commit()
  772. self.assertEqual(msg, 'SSH key added')
  773. with tests.user_set(self.app.application, user):
  774. output = self.app.post('/settings/usersettings/removekey/1', follow_redirects=True)
  775. self.assertEqual(output.status_code, 200)
  776. output_text = output.get_data(as_text=True)
  777. self.assertIn(
  778. "<title>pingou's settings - Pagure</title>", output_text)
  779. self.assertNotIn('SSH key removed', output_text)
  780. data = {'csrf_token': self.get_csrf()}
  781. output = self.app.post(
  782. '/settings/usersettings/removekey/1', data=data, follow_redirects=True)
  783. self.assertEqual(output.status_code, 200)
  784. output_text = output.get_data(as_text=True)
  785. self.assertIn(
  786. "<title>pingou's settings - Pagure</title>", output_text)
  787. self.assertIn('SSH key removed', output_text)
  788. def patched_commit_exists(user, namespace, repo, githash):
  789. ''' Patched version of pagure.pfmarkdown._commit_exists to enforce
  790. returning true on some given hash without having us actually check
  791. the git repos.
  792. '''
  793. if githash in ['9364354', '9364354a', '9364354a4555ba17aa60f0dc844d70b74eb1aecd']:
  794. return True
  795. else:
  796. return False
  797. @patch(
  798. 'pagure.pfmarkdown._commit_exists',
  799. MagicMock(side_effect=patched_commit_exists))
  800. def test_patched_markdown_preview(self):
  801. """ Test the markdown_preview endpoint. """
  802. data = {
  803. 'content': 'test\n----\n\n * 1\n * item 2'
  804. }
  805. # CSRF missing
  806. output = self.app.post('/markdown/', data=data)
  807. self.assertEqual(output.status_code, 400)
  808. user = tests.FakeUser()
  809. user.username = 'foo'
  810. with tests.user_set(self.app.application, user):
  811. output = self.app.get('/settings/')
  812. self.assertEqual(output.status_code, 200)
  813. output_text = output.get_data(as_text=True)
  814. self.assertIn(
  815. '<title>foo\'s settings - Pagure</title>', output_text)
  816. csrf_token = self.get_csrf(output=output)
  817. # With CSRF
  818. data['csrf_token'] = csrf_token
  819. output = self.app.post('/markdown/', data=data)
  820. self.assertEqual(output.status_code, 200)
  821. exp = """<h2>test</h2>
  822. <ul>
  823. <li>1</li>
  824. <li>item 2</li>
  825. </ul>"""
  826. self.assertEqual(output.get_data(as_text=True), exp)
  827. tests.create_projects(self.session)
  828. texts = [
  829. 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  830. 'Cf commit 936435', # 6 chars - not long enough
  831. 'Cf commit 9364354', # 7 chars - long enough
  832. 'Cf commit 9364354a', # 8 chars - still long enough
  833. 'Cf commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd', # 40 chars
  834. ]
  835. expected = [
  836. # 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  837. '<p>pingou committed on <a href="/test/c/9364354a4555ba17aa60f0dc844d70b74eb1aecd" '
  838. 'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
  839. '>test#9364354a4555ba17aa60f0dc844d70b74eb1aecd</a></p>',
  840. # 'Cf commit 936435',
  841. '<p>Cf commit 936435</p>',
  842. # 'Cf commit 9364354',
  843. #'<p>Cf commit 9364354</p>',
  844. '<p>Cf commit <a href="/test/c/9364354" '
  845. 'title="Commit 9364354">9364354</a></p>',
  846. # 'Cf commit 9364354a',
  847. '<p>Cf commit <a href="/test/c/9364354a" '
  848. 'title="Commit 9364354a">9364354</a></p>',
  849. # 'Cf commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  850. '<p>Cf commit <a href="/test/c/9364354a4555ba17aa60f0dc844d70b74eb1aecd" '
  851. 'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
  852. '>9364354</a></p>',
  853. ]
  854. with self.app.application.app_context():
  855. for idx, text in enumerate(texts):
  856. data = {
  857. 'content': text,
  858. 'csrf_token': csrf_token,
  859. }
  860. output = self.app.post('/markdown/?repo=test', data=data)
  861. self.assertEqual(output.status_code, 200)
  862. self.assertEqual(expected[idx], output.get_data(as_text=True))
  863. def test_markdown_preview(self):
  864. """ Test the markdown_preview endpoint with a non-existing commit.
  865. """
  866. user = tests.FakeUser()
  867. user.username = 'foo'
  868. with tests.user_set(self.app.application, user):
  869. output = self.app.get('/settings/')
  870. self.assertEqual(output.status_code, 200)
  871. output_text = output.get_data(as_text=True)
  872. self.assertIn(
  873. '<title>foo\'s settings - Pagure</title>', output_text)
  874. csrf_token = self.get_csrf(output=output)
  875. tests.create_projects(self.session)
  876. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  877. text = 'Cf commit 9364354a4555ba17aa60f0d'
  878. exp = '<p>Cf commit 9364354a4555ba17aa60f0d</p>'
  879. with self.app.application.app_context():
  880. data = {
  881. 'content': text,
  882. 'csrf_token': csrf_token,
  883. }
  884. output = self.app.post('/markdown/?repo=test', data=data)
  885. self.assertEqual(output.status_code, 200)
  886. self.assertEqual(exp, output.get_data(as_text=True))
  887. def test_markdown_preview_valid_commit(self):
  888. """ Test the markdown_preview endpoint with an existing commit. """
  889. user = tests.FakeUser()
  890. user.username = 'foo'
  891. with tests.user_set(self.app.application, user):
  892. output = self.app.get('/settings/')
  893. self.assertEqual(output.status_code, 200)
  894. output_text = output.get_data(as_text=True)
  895. self.assertIn(
  896. '<title>foo\'s settings - Pagure</title>', output_text)
  897. csrf_token = self.get_csrf(output=output)
  898. tests.create_projects(self.session)
  899. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  900. repopath = os.path.join(self.path, 'repos', 'test.git')
  901. tests.add_content_git_repo(repopath)
  902. repo = pygit2.Repository(repopath)
  903. first_commit = repo.revparse_single('HEAD')
  904. text = 'Cf commit %s' % first_commit.oid.hex
  905. exp = '<p>Cf commit <a href="/test/c/{0}" title="Commit {0}">{1}'\
  906. '</a></p>'.format(first_commit.oid.hex, first_commit.oid.hex[:7])
  907. with self.app.application.app_context():
  908. data = {
  909. 'content': text,
  910. 'csrf_token': csrf_token,
  911. }
  912. output = self.app.post('/markdown/?repo=test', data=data)
  913. self.assertEqual(output.status_code, 200)
  914. self.assertEqual(exp, output.get_data(as_text=True))
  915. @patch('pagure.ui.app.admin_session_timedout')
  916. def test_remove_user_email(self, ast):
  917. """ Test the remove_user_email endpoint. """
  918. ast.return_value = False
  919. self.test_new_project()
  920. user = tests.FakeUser()
  921. with tests.user_set(self.app.application, user):
  922. output = self.app.post('/settings/email/drop')
  923. self.assertEqual(output.status_code, 404)
  924. self.assertIn('<h2>Page not found (404)</h2>', output.get_data(as_text=True))
  925. user.username = 'foo'
  926. with tests.user_set(self.app.application, user):
  927. output = self.app.get('/settings/')
  928. self.assertEqual(output.status_code, 200)
  929. output_text = output.get_data(as_text=True)
  930. self.assertIn(
  931. '<title>foo\'s settings - Pagure</title>', output_text)
  932. csrf_token = self.get_csrf(output=output)
  933. data = {
  934. 'email': 'foo@pingou.com',
  935. }
  936. output = self.app.post(
  937. '/settings/email/drop', data=data, follow_redirects=True)
  938. self.assertEqual(output.status_code, 200)
  939. output_text = output.get_data(as_text=True)
  940. self.assertIn(
  941. '<title>foo\'s settings - Pagure</title>', output_text)
  942. self.assertIn(
  943. 'You must always have at '
  944. 'least one email', output_text)
  945. user.username = 'pingou'
  946. with tests.user_set(self.app.application, user):
  947. output = self.app.get('/settings/')
  948. self.assertEqual(output.status_code, 200)
  949. output_text = output.get_data(as_text=True)
  950. self.assertIn(
  951. '<title>pingou\'s settings - Pagure</title>', output_text)
  952. csrf_token = self.get_csrf(output=output)
  953. data = {
  954. 'email': 'foo@pingou.com',
  955. }
  956. output = self.app.post(
  957. '/settings/email/drop', data=data, follow_redirects=True)
  958. self.assertEqual(output.status_code, 200)
  959. output_text = output.get_data(as_text=True)
  960. self.assertIn(
  961. '<title>pingou\'s settings - Pagure</title>', output_text)
  962. self.assertEqual(output_text.count('foo@pingou.com'), 4)
  963. data = {
  964. 'csrf_token': csrf_token,
  965. 'email': 'foobar@pingou.com',
  966. }
  967. output = self.app.post(
  968. '/settings/email/drop', data=data, follow_redirects=True)
  969. self.assertEqual(output.status_code, 200)
  970. output_text = output.get_data(as_text=True)
  971. self.assertIn(
  972. '<title>pingou\'s settings - Pagure</title>', output_text)
  973. self.assertIn(
  974. 'You do not have the '
  975. 'email: foobar@pingou.com, nothing to remove', output_text)
  976. data = {
  977. 'csrf_token': csrf_token,
  978. 'email': 'foo@pingou.com',
  979. }
  980. output = self.app.post(
  981. '/settings/email/drop', data=data, follow_redirects=True)
  982. self.assertEqual(output.status_code, 200)
  983. output_text = output.get_data(as_text=True)
  984. self.assertEqual(output_text.count('foo@pingou.com'), 0)
  985. self.assertEqual(output_text.count('bar@pingou.com'), 3)
  986. output = self.app.post(
  987. '/settings/email/drop', data=data, follow_redirects=True)
  988. self.assertEqual(output.status_code, 200)
  989. output_text = output.get_data(as_text=True)
  990. self.assertEqual(output_text.count('foo@pingou.com'), 0)
  991. self.assertEqual(output_text.count('bar@pingou.com'), 3)
  992. ast.return_value = True
  993. output = self.app.post('/settings/email/drop', data=data)
  994. self.assertEqual(output.status_code, 302)
  995. @patch('pagure.lib.notify.send_email')
  996. @patch('pagure.ui.app.admin_session_timedout')
  997. def test_add_api_user_email(self, ast, send_email):
  998. """ Test the add_api_user_email endpoint. """
  999. send_email.return_value = True
  1000. ast.return_value = False
  1001. self.test_new_project()
  1002. user = tests.FakeUser()
  1003. with tests.user_set(self.app.application, user):
  1004. output = self.app.post('/settings/email/add')
  1005. self.assertEqual(output.status_code, 404)
  1006. self.assertIn('<h2>Page not found (404)</h2>', output.get_data(as_text=True))
  1007. user.username = 'foo'
  1008. with tests.user_set(self.app.application, user):
  1009. output = self.app.post('/settings/email/add')
  1010. self.assertEqual(output.status_code, 200)
  1011. output_text = output.get_data(as_text=True)
  1012. self.assertIn("<strong>Add new email</strong>", output_text)
  1013. if self.get_wtforms_version() >= (2, 2):
  1014. self.assertIn(
  1015. '<input class="form-control form-control-error" id="email" '
  1016. 'name="email" required type="text" value="">', output_text)
  1017. else:
  1018. self.assertIn(
  1019. '<input class="form-control form-control-error" id="email" '
  1020. 'name="email" type="text" value="">', output_text)
  1021. user.username = 'pingou'
  1022. with tests.user_set(self.app.application, user):
  1023. output = self.app.post('/settings/email/add')
  1024. self.assertEqual(output.status_code, 200)
  1025. output_text = output.get_data(as_text=True)
  1026. self.assertIn("<strong>Add new email</strong>", output_text)
  1027. if self.get_wtforms_version() >= (2, 2):
  1028. self.assertIn(
  1029. '<input class="form-control form-control-error" id="email" '
  1030. 'name="email" required type="text" value="">', output_text)
  1031. else:
  1032. self.assertIn(
  1033. '<input class="form-control form-control-error" id="email" '
  1034. 'name="email" type="text" value="">', output_text)
  1035. csrf_token = output_text.split(
  1036. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1037. data = {
  1038. 'email': 'foo2@pingou.com',
  1039. }
  1040. output = self.app.post(
  1041. '/settings/email/add', data=data, follow_redirects=True)
  1042. self.assertEqual(output.status_code, 200)
  1043. output_text = output.get_data(as_text=True)
  1044. self.assertIn("<strong>Add new email</strong>", output_text)
  1045. self.assertEqual(output_text.count('foo2@pingou.com'), 1)
  1046. # New email
  1047. data = {
  1048. 'csrf_token': csrf_token,
  1049. 'email': 'foðbar@pingou.com',
  1050. }
  1051. output = self.app.post(
  1052. '/settings/email/add', data=data, follow_redirects=True)
  1053. self.assertEqual(output.status_code, 200)
  1054. output_text = output.get_data(as_text=True)
  1055. self.assertIn(
  1056. '<title>pingou\'s settings - Pagure</title>', output_text)
  1057. self.assertIn(
  1058. 'Email pending validation',
  1059. output_text)
  1060. self.assertEqual(output_text.count('foo@pingou.com'), 4)
  1061. self.assertEqual(output_text.count('bar@pingou.com'), 5)
  1062. self.assertEqual(output_text.count('foðbar@pingou.com'), 2)
  1063. # Email already pending
  1064. output = self.app.post(
  1065. '/settings/email/add', data=data, follow_redirects=True)
  1066. self.assertEqual(output.status_code, 200)
  1067. output_text = output.get_data(as_text=True)
  1068. self.assertIn(
  1069. '<div class="card-header">\n '
  1070. '<strong>Add new email</strong>', output_text)
  1071. self.assertIn(
  1072. 'This email is already '
  1073. 'pending confirmation', output_text)
  1074. # User already has this email
  1075. data = {
  1076. 'csrf_token': csrf_token,
  1077. 'email': 'foo@pingou.com',
  1078. }
  1079. output = self.app.post(
  1080. '/settings/email/add', data=data, follow_redirects=True)
  1081. self.assertEqual(output.status_code, 200)
  1082. output_text = output.get_data(as_text=True)
  1083. self.assertTrue("<strong>Add new email</strong>" in output_text)
  1084. self.assertTrue(
  1085. 'Invalid value, can&#39;t be any of: bar@pingou.com, '
  1086. 'foo@pingou.com.&nbsp;' in output_text
  1087. or
  1088. 'Invalid value, can&#39;t be any of: foo@pingou.com, '
  1089. 'bar@pingou.com.&nbsp;' in output_text
  1090. )
  1091. self.assertEqual(
  1092. output_text.count('foo@pingou.com'), 6)
  1093. self.assertEqual(
  1094. output_text.count('bar@pingou.com'), 5)
  1095. self.assertEqual(
  1096. output_text.count('foðbar@pingou.com'), 0)
  1097. # Email registered by someone else
  1098. data = {
  1099. 'csrf_token': csrf_token,
  1100. 'email': 'foo@bar.com',
  1101. }
  1102. output = self.app.post(
  1103. '/settings/email/add', data=data, follow_redirects=True)
  1104. self.assertEqual(output.status_code, 200)
  1105. output_text = output.get_data(as_text=True)
  1106. self.assertTrue("<strong>Add new email</strong>" in output_text)
  1107. self.assertIn(
  1108. 'Invalid value, can&#39;t be any of: foo@bar.com.&nbsp;',
  1109. output_text)
  1110. ast.return_value = True
  1111. output = self.app.post('/settings/email/add', data=data)
  1112. self.assertEqual(output.status_code, 302)
  1113. @patch('pagure.lib.notify.send_email')
  1114. @patch('pagure.ui.app.admin_session_timedout')
  1115. def test_set_default_email(self, ast, send_email):
  1116. """ Test the set_default_email endpoint. """
  1117. send_email.return_value = True
  1118. ast.return_value = False
  1119. self.test_new_project()
  1120. user = tests.FakeUser()
  1121. with tests.user_set(self.app.application, user):
  1122. output = self.app.post('/settings/email/default')
  1123. self.assertEqual(output.status_code, 404)
  1124. self.assertTrue('<h2>Page not found (404)</h2>' in output.get_data(as_text=True))
  1125. user.username = 'pingou'
  1126. with tests.user_set(self.app.application, user):
  1127. output = self.app.get('/settings/')
  1128. self.assertEqual(output.status_code, 200)
  1129. output_text = output.get_data(as_text=True)
  1130. self.assertIn(
  1131. '<title>pingou\'s settings - Pagure</title>', output_text)
  1132. csrf_token = self.get_csrf(output=output)
  1133. data = {
  1134. 'email': 'foo@pingou.com',
  1135. }
  1136. output = self.app.post(
  1137. '/settings/email/default', data=data, follow_redirects=True)
  1138. self.assertEqual(output.status_code, 200)
  1139. output_text = output.get_data(as_text=True)
  1140. self.assertIn(
  1141. '<title>pingou\'s settings - Pagure</title>', output_text)
  1142. self.assertEqual(output_text.count('foo@pingou.com'), 4)
  1143. # Set invalid default email
  1144. data = {
  1145. 'csrf_token': csrf_token,
  1146. 'email': 'foobar@pingou.com',
  1147. }
  1148. output = self.app.post(
  1149. '/settings/email/default', data=data, follow_redirects=True)
  1150. self.assertEqual(output.status_code, 200)
  1151. output_text = output.get_data(as_text=True)
  1152. self.assertIn(
  1153. '<title>pingou\'s settings - Pagure</title>', output_text)
  1154. self.assertEqual(output_text.count('foo@pingou.com'), 4)
  1155. self.assertIn(
  1156. 'You do not have the '
  1157. 'email: foobar@pingou.com, nothing to set',
  1158. output_text)
  1159. # Set default email
  1160. data = {
  1161. 'csrf_token': csrf_token,
  1162. 'email': 'foo@pingou.com',
  1163. }
  1164. output = self.app.post(
  1165. '/settings/email/default', data=data, follow_redirects=True)
  1166. self.assertEqual(output.status_code, 200)
  1167. output_text = output.get_data(as_text=True)
  1168. self.assertIn(
  1169. '<title>pingou\'s settings - Pagure</title>', output_text)
  1170. self.assertEqual(output_text.count('foo@pingou.com'), 4)
  1171. self.assertIn(
  1172. 'Default email set to: '
  1173. 'foo@pingou.com', output_text)
  1174. ast.return_value = True
  1175. output = self.app.post('/settings/email/default', data=data)
  1176. self.assertEqual(output.status_code, 302)
  1177. @patch('pagure.lib.notify.send_email')
  1178. @patch('pagure.ui.app.admin_session_timedout')
  1179. def test_reconfirm_email(self, ast, send_email):
  1180. """ Test the reconfirm_email endpoint. """
  1181. send_email.return_value = True
  1182. ast.return_value = False
  1183. self.test_new_project()
  1184. # Add a pending email to pingou
  1185. userobj = pagure.lib.search_user(self.session, username='pingou')
  1186. self.assertEqual(len(userobj.emails), 2)
  1187. email_pend = pagure.lib.model.UserEmailPending(
  1188. user_id=userobj.id,
  1189. email='foo@fp.o',
  1190. token='abcdef',
  1191. )
  1192. self.session.add(email_pend)
  1193. self.session.commit()
  1194. user = tests.FakeUser()
  1195. with tests.user_set(self.app.application, user):
  1196. output = self.app.post('/settings/email/resend')
  1197. self.assertEqual(output.status_code, 404)
  1198. self.assertTrue('<h2>Page not found (404)</h2>' in output.get_data(as_text=True))
  1199. user.username = 'pingou'
  1200. with tests.user_set(self.app.application, user):
  1201. output = self.app.get('/settings/')
  1202. self.assertEqual(output.status_code, 200)
  1203. output_text = output.get_data(as_text=True)
  1204. self.assertIn(
  1205. '<title>pingou\'s settings - Pagure</title>', output_text)
  1206. csrf_token = self.get_csrf(output=output)
  1207. data = {
  1208. 'email': 'foo@pingou.com',
  1209. }
  1210. output = self.app.post(
  1211. '/settings/email/resend', data=data, follow_redirects=True)
  1212. self.assertEqual(output.status_code, 200)
  1213. output_text = output.get_data(as_text=True)
  1214. self.assertIn(
  1215. '<title>pingou\'s settings - Pagure</title>', output_text)
  1216. self.assertEqual(output_text.count('foo@pingou.com'), 4)
  1217. # Set invalid default email
  1218. data = {
  1219. 'csrf_token': csrf_token,
  1220. 'email': 'foobar@pingou.com',
  1221. }
  1222. output = self.app.post(
  1223. '/settings/email/resend', data=data, follow_redirects=True)
  1224. self.assertEqual(output.status_code, 200)
  1225. output_text = output.get_data(as_text=True)
  1226. self.assertIn(
  1227. '<title>pingou\'s settings - Pagure</title>', output_text)
  1228. self.assertEqual(output_text.count('foo@pingou.com'), 4)
  1229. self.assertIn(
  1230. 'This email address has '
  1231. 'already been confirmed', output_text)
  1232. # Validate a non-validated email
  1233. data = {
  1234. 'csrf_token': csrf_token,
  1235. 'email': 'foo@fp.o',
  1236. }
  1237. output = self.app.post(
  1238. '/settings/email/resend', data=data, follow_redirects=True)
  1239. self.assertEqual(output.status_code, 200)
  1240. output_text = output.get_data(as_text=True)
  1241. self.assertIn(
  1242. '<title>pingou\'s settings - Pagure</title>', output_text)
  1243. self.assertEqual(output_text.count('foo@pingou.com'), 4)
  1244. self.assertIn(
  1245. 'Confirmation email re-sent',
  1246. output_text)
  1247. ast.return_value = True
  1248. output = self.app.post('/settings/email/resend', data=data)
  1249. self.assertEqual(output.status_code, 302)
  1250. @patch('pagure.ui.app.admin_session_timedout')
  1251. def test_confirm_email(self, ast):
  1252. """ Test the confirm_email endpoint. """
  1253. output = self.app.get('/settings/email/confirm/foobar')
  1254. self.assertEqual(output.status_code, 302)
  1255. ast.return_value = False
  1256. # Add a pending email to pingou
  1257. userobj = pagure.lib.search_user(self.session, username='pingou')
  1258. self.assertEqual(len(userobj.emails), 2)
  1259. email_pend = pagure.lib.model.UserEmailPending(
  1260. user_id=userobj.id,
  1261. email='foo@fp.o',
  1262. token='abcdef',
  1263. )
  1264. self.session.add(email_pend)
  1265. self.session.commit()
  1266. user = tests.FakeUser()
  1267. user.username = 'pingou'
  1268. with tests.user_set(self.app.application, user):
  1269. # Wrong token
  1270. output = self.app.get(
  1271. '/settings/email/confirm/foobar', follow_redirects=True)
  1272. self.assertEqual(output.status_code, 200)
  1273. output_text = output.get_data(as_text=True)
  1274. self.assertIn(
  1275. '<title>pingou\'s settings - Pagure</title>', output_text)
  1276. self.assertIn(
  1277. 'No email associated with this token.',
  1278. output_text)
  1279. # Confirm email
  1280. output = self.app.get(
  1281. '/settings/email/confirm/abcdef', follow_redirects=True)
  1282. self.assertEqual(output.status_code, 200)
  1283. output_text = output.get_data(as_text=True)
  1284. self.assertIn(
  1285. '<title>pingou\'s settings - Pagure</title>', output_text)
  1286. self.assertIn(
  1287. 'Email validated',
  1288. output_text)
  1289. userobj = pagure.lib.search_user(self.session, username='pingou')
  1290. self.assertEqual(len(userobj.emails), 3)
  1291. ast.return_value = True
  1292. output = self.app.get('/settings/email/confirm/foobar')
  1293. self.assertEqual(output.status_code, 302)
  1294. def test_view_my_requests_no_user(self):
  1295. """Test the view_user_requests endpoint."""
  1296. output = self.app.get('/user/somenonexistentuser/requests')
  1297. self.assertEqual(output.status_code, 404)
  1298. @patch(
  1299. 'pagure.lib.git.update_git', MagicMock(return_value=True))
  1300. @patch(
  1301. 'pagure.lib.notify.send_email', MagicMock(return_value=True))
  1302. def test_view_my_requests(self):
  1303. """Test the view_user_requests endpoint. """
  1304. # Create the PR
  1305. tests.create_projects(self.session)
  1306. repo = pagure.lib._get_project(self.session, 'test')
  1307. req = pagure.lib.new_pull_request(
  1308. session=self.session,
  1309. repo_from=repo,
  1310. branch_from='dev',
  1311. repo_to=repo,
  1312. branch_to='master',
  1313. title='test pull-request #1',
  1314. user='pingou',
  1315. )
  1316. self.session.commit()
  1317. self.assertEqual(req.id, 1)
  1318. self.assertEqual(req.title, 'test pull-request #1')
  1319. output = self.app.get('/user/pingou/requests')
  1320. self.assertEqual(output.status_code, 200)
  1321. output_text = output.get_data(as_text=True)
  1322. self.assertIn('test pull-request #1', output_text)
  1323. self.assertEqual(
  1324. output_text.count('pr-status pr-status-open"'),
  1325. 1)
  1326. # Add a PR in a fork
  1327. item = pagure.lib.model.Project(
  1328. user_id=1, # pingou
  1329. name='test_fork',
  1330. description='test project #1',
  1331. is_fork=True,
  1332. parent_id=1,
  1333. hook_token='aaabbbttt',
  1334. )
  1335. self.session.add(item)
  1336. repo = pagure.lib._get_project(
  1337. self.session, 'test_fork', user='pingou')
  1338. req = pagure.lib.new_pull_request(
  1339. session=self.session,
  1340. repo_from=repo,
  1341. branch_from='dev',
  1342. repo_to=repo,
  1343. branch_to='master',
  1344. title='tést pull-request #2',
  1345. user='pingou',
  1346. )
  1347. self.session.commit()
  1348. self.assertEqual(req.id, 1)
  1349. self.assertEqual(req.title, 'tést pull-request #2')
  1350. output = self.app.get('/user/pingou/requests')
  1351. self.assertEqual(output.status_code, 200)
  1352. output_text = output.get_data(as_text=True)
  1353. self.assertIn('test pull-request #1', output_text)
  1354. self.assertIn('tést pull-request #2', output_text)
  1355. self.assertEqual(
  1356. output_text.count('pr-status pr-status-open"'),
  1357. 2)
  1358. @patch(
  1359. 'pagure.lib.git.update_git', MagicMock(return_value=True))
  1360. @patch(
  1361. 'pagure.lib.notify.send_email', MagicMock(return_value=True))
  1362. def test_view_my_requests_pr_in_another_project(self):
  1363. """Test the view_user_requests endpoint when the user opened a PR
  1364. in another project. """
  1365. # Pingou creates the PR on test
  1366. tests.create_projects(self.session)
  1367. repo = pagure.lib._get_project(self.session, 'test')
  1368. req = pagure.lib.new_pull_request(
  1369. session=self.session,
  1370. repo_from=repo,
  1371. branch_from='dev',
  1372. repo_to=repo,
  1373. branch_to='master',
  1374. title='test pull-request #1',
  1375. user='pingou',
  1376. )
  1377. self.session.commit()
  1378. self.assertEqual(req.id, 1)
  1379. self.assertEqual(req.title, 'test pull-request #1')
  1380. # foo creates the PR on test
  1381. repo = pagure.lib._get_project(self.session, 'test')
  1382. req = pagure.lib.new_pull_request(
  1383. session=self.session,
  1384. repo_from=repo,
  1385. branch_from='dev',
  1386. repo_to=repo,
  1387. branch_to='master',
  1388. title='test pull-request #2',
  1389. user='foo',
  1390. )
  1391. self.session.commit()
  1392. self.assertEqual(req.id, 2)
  1393. self.assertEqual(req.title, 'test pull-request #2')
  1394. # Check pingou's PR list
  1395. output = self.app.get('/user/pingou/requests')
  1396. self.assertEqual(output.status_code, 200)
  1397. output_text = output.get_data(as_text=True)
  1398. self.assertIn('test pull-request #1', output_text)
  1399. self.assertIn('test pull-request #2', output_text)
  1400. self.assertEqual(
  1401. output_text.count('pr-status pr-status-open"'),
  1402. 2)
  1403. # Check foo's PR list
  1404. output = self.app.get('/user/foo/requests')
  1405. self.assertEqual(output.status_code, 200)
  1406. output_text = output.get_data(as_text=True)
  1407. self.assertNotIn('test pull-request #1', output_text)
  1408. self.assertIn('test pull-request #2', output_text)
  1409. self.assertEqual(
  1410. output_text.count('pr-status pr-status-open"'),
  1411. 1)
  1412. @patch(
  1413. 'pagure.lib.git.update_git', MagicMock(return_value=True))
  1414. @patch(
  1415. 'pagure.lib.notify.send_email', MagicMock(return_value=True))
  1416. def test_view_my_requests_against_another_project(self):
  1417. """Test the view_user_requests endpoint when there is a PR opened
  1418. by me against a project I do not have rights on. """
  1419. # Create the PR
  1420. tests.create_projects(self.session)
  1421. repo = pagure.lib._get_project(self.session, 'test')
  1422. req = pagure.lib.new_pull_request(
  1423. session=self.session,
  1424. repo_from=repo,
  1425. branch_from='dev',
  1426. repo_to=repo,
  1427. branch_to='master',
  1428. title='test pull-request #1',
  1429. user='foo',
  1430. )
  1431. self.session.commit()
  1432. self.assertEqual(req.id, 1)
  1433. self.assertEqual(req.title, 'test pull-request #1')
  1434. output = self.app.get('/user/foo/requests')
  1435. self.assertEqual(output.status_code, 200)
  1436. output_text = output.get_data(as_text=True)
  1437. self.assertIn('test pull-request #1', output_text)
  1438. self.assertEqual(
  1439. output_text.count('pr-status pr-status-open"'),
  1440. 1)
  1441. def test_view_my_issues_no_user(self):
  1442. """Test the view_user_issues endpoint with a missing user."""
  1443. output = self.app.get('/user/somenonexistentuser/issues')
  1444. self.assertEqual(output.status_code, 404)
  1445. @patch(
  1446. 'pagure.lib.git.update_git', MagicMock(return_value=True))
  1447. @patch(
  1448. 'pagure.lib.notify.send_email', MagicMock(return_value=True))
  1449. def test_view_my_issues(self):
  1450. """Test the view_user_issues endpoint when the user exists."""
  1451. # Create the issue
  1452. tests.create_projects(self.session)
  1453. repo = pagure.lib._get_project(self.session, 'test')
  1454. msg = pagure.lib.new_issue(
  1455. session=self.session,
  1456. repo=repo,
  1457. title='Test issue #1',
  1458. content='We should work on this for the second time',
  1459. user='pingou',
  1460. status='Open',
  1461. )
  1462. self.session.commit()
  1463. self.assertEqual(msg.title, 'Test issue #1')
  1464. output = self.app.get('/user/pingou/issues')
  1465. self.assertEqual(output.status_code, 200)
  1466. output_text = output.get_data(as_text=True)
  1467. self.assertIn('Test issue #1', output_text)
  1468. self.assertEqual(
  1469. output_text.count(
  1470. 'issue-status issue-status-open'),
  1471. 1)
  1472. # Add an issue in a fork
  1473. item = pagure.lib.model.Project(
  1474. user_id=2, # foo
  1475. name='test_fork',
  1476. description='test project #1',
  1477. is_fork=True,
  1478. parent_id=1,
  1479. hook_token='aaabbbttt',
  1480. )
  1481. self.session.add(item)
  1482. repo = pagure.lib._get_project(self.session, 'test_fork', user='foo')
  1483. msg = pagure.lib.new_issue(
  1484. session=self.session,
  1485. repo=repo,
  1486. title='Test issue #2',
  1487. content='We should work on this for the second time',
  1488. user='pingou',
  1489. status='Open',
  1490. )
  1491. self.session.commit()
  1492. self.assertEqual(msg.title, 'Test issue #2')
  1493. # Test the assigned issue table. Create issue then set the assignee
  1494. msg = pagure.lib.new_issue(
  1495. session=self.session,
  1496. repo=repo,
  1497. title='Test issue #3',
  1498. content='This issue created by foo, but assigned to pingou',
  1499. user='foo',
  1500. status='Open',
  1501. )
  1502. self.session.commit()
  1503. self.assertEqual(msg.title, 'Test issue #3')
  1504. msg = pagure.lib.add_issue_assignee(
  1505. session=self.session,
  1506. issue=msg,
  1507. assignee='pingou',
  1508. user='foo',
  1509. )
  1510. self.session.commit()
  1511. self.assertEqual(msg, 'Issue assigned to pingou')
  1512. output = self.app.get('/user/pingou/issues')
  1513. self.assertEqual(output.status_code, 200)
  1514. output_text = output.get_data(as_text=True)
  1515. self.assertIn('Test issue #1', output_text)
  1516. self.assertIn('Test issue #2', output_text)
  1517. self.assertIn('Test issue #3', output_text)
  1518. self.assertEqual(
  1519. output_text.count(
  1520. 'issue-status issue-status-open'),
  1521. 3)
  1522. @patch(
  1523. 'pagure.lib.git.update_git', MagicMock(return_value=True))
  1524. @patch(
  1525. 'pagure.lib.notify.send_email', MagicMock(return_value=True))
  1526. def test_view_my_issues_disabled(self):
  1527. """Test the view_user_issues endpoint when the project disabled issue
  1528. tracking."""
  1529. # Create the issue
  1530. tests.create_projects(self.session)
  1531. repo = pagure.lib._get_project(self.session, 'test')
  1532. msg = pagure.lib.new_issue(
  1533. session=self.session,
  1534. repo=repo,
  1535. title='Test issue #1',
  1536. content='We should work on this for the second time',
  1537. user='pingou',
  1538. status='Open',
  1539. )
  1540. self.session.commit()
  1541. self.assertEqual(msg.title, 'Test issue #1')
  1542. # Before
  1543. output = self.app.get('/user/pingou/issues')
  1544. self.assertEqual(output.status_code, 200)
  1545. output_text = output.get_data(as_text=True)
  1546. self.assertIn('Test issue #1', output_text)
  1547. self.assertEqual(
  1548. output_text.count('issue-status issue-status-open'),
  1549. 1)
  1550. # Disable issue tracking
  1551. repo = pagure.lib._get_project(self.session, 'test')
  1552. settings = repo.settings
  1553. settings['issue_tracker'] = False
  1554. repo.settings = settings
  1555. self.session.add(repo)
  1556. self.session.commit()
  1557. # After
  1558. output = self.app.get('/user/pingou/issues')
  1559. self.assertEqual(output.status_code, 200)
  1560. output_text = output.get_data(as_text=True)
  1561. self.assertNotIn('Test issue #1', output_text)
  1562. self.assertEqual(
  1563. output_text.count('issue-status issue-status-open'),
  1564. 0)
  1565. def test_view_my_issues_tickets_turned_off(self):
  1566. """Test the view_user_issues endpoint when the user exists and
  1567. and ENABLE_TICKETS is False """
  1568. # Turn off the tickets instance wide
  1569. pagure.config.config['ENABLE_TICKETS'] = False
  1570. output = self.app.get('/user/pingou/issues')
  1571. self.assertEqual(output.status_code, 404)
  1572. pagure.config.config['ENABLE_TICKETS'] = True
  1573. @patch('pagure.ui.app.admin_session_timedout')
  1574. def test_add_user_token(self, ast):
  1575. """ Test the add_user_token endpoint. """
  1576. ast.return_value = False
  1577. self.test_new_project()
  1578. user = tests.FakeUser()
  1579. with tests.user_set(self.app.application, user):
  1580. output = self.app.get('/settings/token/new/')
  1581. self.assertEqual(output.status_code, 404)
  1582. self.assertIn('<h2>Page not found (404)</h2>',
  1583. output.get_data(as_text=True))
  1584. user.username = 'foo'
  1585. with tests.user_set(self.app.application, user):
  1586. output = self.app.get('/settings/token/new')
  1587. self.assertEqual(output.status_code, 200)
  1588. output_text = output.get_data(as_text=True)
  1589. self.assertIn(
  1590. '<div class="card-header">\n <strong>'
  1591. 'Create a new token</strong>\n', output_text)
  1592. self.assertIn(
  1593. '<input type="checkbox" name="acls" value="create_project">',
  1594. output_text)
  1595. csrf_token = output_text.split(
  1596. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1597. data = {
  1598. 'acls': ['create_project', 'fork_project']
  1599. }
  1600. # missing CSRF
  1601. output = self.app.post('/settings/token/new', data=data)
  1602. self.assertEqual(output.status_code, 200)
  1603. output_text = output.get_data(as_text=True)
  1604. self.assertIn(
  1605. '<title>Create token - Pagure</title>', output_text)
  1606. self.assertIn(
  1607. '<div class="card-header">\n <strong>'
  1608. 'Create a new token</strong>\n', output_text)
  1609. self.assertIn(
  1610. '<input type="checkbox" name="acls" value="create_project">',
  1611. output_text)
  1612. data = {
  1613. 'acls': ['new_project'],
  1614. 'csrf_token': csrf_token
  1615. }
  1616. # Invalid ACLs
  1617. output = self.app.post('/settings/token/new', data=data)
  1618. self.assertEqual(output.status_code, 200)
  1619. output_text = output.get_data(as_text=True)
  1620. self.assertIn(
  1621. '<title>Create token - Pagure</title>', output_text)
  1622. self.assertIn(
  1623. '<div class="card-header">\n <strong>'
  1624. 'Create a new token</strong>\n', output_text)
  1625. self.assertIn(
  1626. '<input type="checkbox" name="acls" value="create_project">',
  1627. output_text)
  1628. data = {
  1629. 'acls': ['create_project', 'fork_project'],
  1630. 'csrf_token': csrf_token
  1631. }
  1632. # All good
  1633. output = self.app.post(
  1634. '/settings/token/new', data=data, follow_redirects=True)
  1635. self.assertEqual(output.status_code, 200)
  1636. output_text = output.get_data(as_text=True)
  1637. self.assertIn(
  1638. '<title>foo\'s settings - Pagure</title>', output_text)
  1639. self.assertIn(
  1640. 'Token created',
  1641. output_text)
  1642. self.assertEqual(
  1643. output_text.count(
  1644. '<small class="font-weight-bold text-success input-group-text">Active until'), 1)
  1645. ast.return_value = True
  1646. output = self.app.get('/settings/token/new')
  1647. self.assertEqual(output.status_code, 302)
  1648. @patch('pagure.ui.app.admin_session_timedout')
  1649. def test_revoke_api_user_token(self, ast):
  1650. """ Test the revoke_api_user_token endpoint. """
  1651. ast.return_value = False
  1652. self.test_new_project()
  1653. user = tests.FakeUser()
  1654. with tests.user_set(self.app.application, user):
  1655. # Token doesn't exist
  1656. output = self.app.post('/settings/token/revoke/foobar')
  1657. self.assertEqual(output.status_code, 404)
  1658. self.assertTrue('<h2>Page not found (404)</h2>' in output.get_data(as_text=True))
  1659. # Create the foobar API token but associated w/ the user 'foo'
  1660. item = pagure.lib.model.Token(
  1661. id='foobar',
  1662. user_id=2, # foo
  1663. expiration=datetime.datetime.utcnow() \
  1664. + datetime.timedelta(days=30)
  1665. )
  1666. self.session.add(item)
  1667. self.session.commit()
  1668. # Token not associated w/ this user
  1669. output = self.app.post('/settings/token/revoke/foobar')
  1670. self.assertEqual(output.status_code, 404)
  1671. self.assertTrue('<h2>Page not found (404)</h2>' in output.get_data(as_text=True))
  1672. user.username = 'foo'
  1673. with tests.user_set(self.app.application, user):
  1674. # Missing CSRF token
  1675. output = self.app.post(
  1676. '/settings/token/revoke/foobar', follow_redirects=True)
  1677. self.assertEqual(output.status_code, 200)
  1678. output_text = output.get_data(as_text=True)
  1679. self.assertIn(
  1680. "<title>foo's settings - Pagure</title>", output_text)
  1681. self.assertEqual(
  1682. output_text.count(
  1683. '<small class="font-weight-bold text-success input-group-text">Active until'), 1)
  1684. csrf_token = output_text.split(
  1685. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1686. data = {
  1687. 'csrf_token': csrf_token
  1688. }
  1689. # All good - token is deleted
  1690. output = self.app.post(
  1691. '/settings/token/revoke/foobar', data=data,
  1692. follow_redirects=True)
  1693. self.assertEqual(output.status_code, 200)
  1694. output_text = output.get_data(as_text=True)
  1695. self.assertIn(
  1696. "<title>foo's settings - Pagure</title>", output_text)
  1697. self.assertEqual(
  1698. output_text.count(
  1699. '<small class="font-weight-bold text-success input-group-text">Active until'), 0)
  1700. user = pagure.lib.get_user(self.session, key='foo')
  1701. self.assertEqual(len(user.tokens), 1)
  1702. expiration_dt = user.tokens[0].expiration
  1703. # Token was already deleted - no changes
  1704. output = self.app.post(
  1705. '/settings/token/revoke/foobar', data=data,
  1706. follow_redirects=True)
  1707. self.assertEqual(output.status_code, 200)
  1708. output_text = output.get_data(as_text=True)
  1709. self.assertIn(
  1710. "<title>foo's settings - Pagure</title>", output_text)
  1711. self.assertEqual(
  1712. output_text.count(
  1713. '<small class="font-weight-bold text-success input-group-text">Active until'), 0)
  1714. # Ensure the expiration date did not change
  1715. user = pagure.lib.get_user(self.session, key='foo')
  1716. self.assertEqual(len(user.tokens), 1)
  1717. self.assertEqual(
  1718. expiration_dt, user.tokens[0].expiration
  1719. )
  1720. ast.return_value = True
  1721. output = self.app.get('/settings/token/new')
  1722. self.assertEqual(output.status_code, 302)
  1723. @patch.dict('pagure.config.config', {'PAGURE_AUTH': 'fas'})
  1724. @patch.dict('pagure.utils.pagure_config', {'PAGURE_AUTH': 'fas'})
  1725. def test_create_project_auth_FAS_no_FPCA(self):
  1726. """ Test creating a project when auth is FAS and the user did not
  1727. sign the FPCA. """
  1728. user = tests.FakeUser(username='foo', cla_done=False)
  1729. with tests.user_set(self.app.application, user):
  1730. output = self.app.get('/new/', follow_redirects=True)
  1731. self.assertEqual(output.status_code, 200)
  1732. output_text = output.get_data(as_text=True)
  1733. self.assertIn('<title>Home - Pagure</title>', output_text)
  1734. self.assertIn(
  1735. '</i> You must <a href="https://admin.fedoraproject.org/accounts/'
  1736. '">sign the FPCA</a> (Fedora Project Contributor Agreement) '
  1737. 'to use pagure</div>', output_text)
  1738. class PagureFlaskAppNoDocstests(tests.Modeltests):
  1739. """ Tests for flask app controller of pagure """
  1740. config_values = {
  1741. "enable_docs": False,
  1742. "docs_folder": None,
  1743. }
  1744. def test_new_project_no_docs_folder(self):
  1745. """ Test the new_project endpoint with DOCS_FOLDER is None. """
  1746. # Before
  1747. projects = pagure.lib.search_projects(self.session)
  1748. self.assertEqual(len(projects), 0)
  1749. self.assertFalse(os.path.exists(
  1750. os.path.join(self.path, 'repos', 'project#1.git')))
  1751. self.assertFalse(os.path.exists(
  1752. os.path.join(self.path, 'repos', 'tickets', 'project#1.git')))
  1753. self.assertFalse(os.path.exists(
  1754. os.path.join(self.path, 'repos', 'docs', 'project#1.git')))
  1755. self.assertFalse(os.path.exists(
  1756. os.path.join(self.path, 'repos', 'requests', 'project#1.git')))
  1757. user = tests.FakeUser(username='foo')
  1758. with tests.user_set(self.app.application, user):
  1759. csrf_token = self.get_csrf()
  1760. data = {
  1761. 'description': 'Project #1',
  1762. 'name': 'project-1',
  1763. 'csrf_token': csrf_token,
  1764. }
  1765. output = self.app.post('/new/', data=data, follow_redirects=True)
  1766. self.assertEqual(output.status_code, 200)
  1767. output_text = output.get_data(as_text=True)
  1768. self.assertIn(
  1769. '<div class="projectinfo my-3">\nProject #1',
  1770. output_text)
  1771. self.assertIn('<p>This repo is brand new!</p>',
  1772. output_text)
  1773. self.assertIn(
  1774. '<title>Overview - project-1 - Pagure</title>', output_text)
  1775. # After
  1776. projects = pagure.lib.search_projects(self.session)
  1777. self.assertEqual(len(projects), 1)
  1778. self.assertTrue(os.path.exists(
  1779. os.path.join(self.path, 'repos', 'project-1.git')))
  1780. self.assertTrue(os.path.exists(
  1781. os.path.join(self.path, 'repos', 'tickets', 'project-1.git')))
  1782. self.assertFalse(os.path.exists(
  1783. os.path.join(self.path, 'repos', 'docs', 'project-1.git')))
  1784. self.assertTrue(os.path.exists(
  1785. os.path.join(self.path, 'repos', 'requests', 'project-1.git')))
  1786. class PagureFlaskAppNoTicketstests(tests.Modeltests):
  1787. """ Tests for flask app controller of pagure """
  1788. config_values = {
  1789. "enable_tickets": False,
  1790. "tickets_folder": None,
  1791. }
  1792. def test_new_project_no_tickets_folder(self):
  1793. """ Test the new_project endpoint with TICKETS_FOLDER is None. """
  1794. # Before
  1795. projects = pagure.lib.search_projects(self.session)
  1796. self.assertEqual(len(projects), 0)
  1797. self.assertFalse(os.path.exists(
  1798. os.path.join(self.path, 'repos', 'project#1.git')))
  1799. self.assertFalse(os.path.exists(
  1800. os.path.join(self.path, 'repos', 'tickets', 'project#1.git')))
  1801. self.assertFalse(os.path.exists(
  1802. os.path.join(self.path, 'repos', 'docs', 'project#1.git')))
  1803. self.assertFalse(os.path.exists(
  1804. os.path.join(self.path, 'repos', 'requests', 'project#1.git')))
  1805. user = tests.FakeUser(username='foo')
  1806. with tests.user_set(self.app.application, user):
  1807. csrf_token = self.get_csrf()
  1808. data = {
  1809. 'description': 'Project #1',
  1810. 'name': 'project-1',
  1811. 'csrf_token': csrf_token,
  1812. }
  1813. output = self.app.post('/new/', data=data, follow_redirects=True)
  1814. self.assertEqual(output.status_code, 200)
  1815. output_text = output.get_data(as_text=True)
  1816. self.assertIn(
  1817. '<div class="projectinfo my-3">\nProject #1',
  1818. output_text)
  1819. self.assertIn('<p>This repo is brand new!</p>',
  1820. output_text)
  1821. self.assertIn(
  1822. '<title>Overview - project-1 - Pagure</title>', output_text)
  1823. # After
  1824. projects = pagure.lib.search_projects(self.session)
  1825. self.assertEqual(len(projects), 1)
  1826. self.assertTrue(os.path.exists(
  1827. os.path.join(self.path, 'repos', 'project-1.git')))
  1828. self.assertFalse(os.path.exists(
  1829. os.path.join(self.path, 'repos', 'tickets', 'project-1.git')))
  1830. self.assertTrue(os.path.exists(
  1831. os.path.join(self.path, 'repos', 'docs', 'project-1.git')))
  1832. self.assertTrue(os.path.exists(
  1833. os.path.join(self.path, 'repos', 'requests', 'project-1.git')))
  1834. if __name__ == '__main__':
  1835. unittest.main(verbosity=2)