1
0

test_pagure_flask_ui_app.py 76 KB

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