1
0

test_pagure_flask_ui_app.py 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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.query
  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.query.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.query.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.query.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.query.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.query.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.query.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.query.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.query.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.query.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.query.search_projects(self.session)
  485. self.assertEqual(len(projects), 0)
  486. projects = pagure.lib.query.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.query.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.query.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.query.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.query.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.query.get_user(self.session, 'pingou')
  764. msg = pagure.lib.query.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.query.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.query.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.query.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.query._get_project(self.session, 'test')
  1307. req = pagure.lib.query.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.query._get_project(
  1337. self.session, 'test_fork', user='pingou')
  1338. req = pagure.lib.query.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.query._get_project(self.session, 'test')
  1368. req = pagure.lib.query.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.query._get_project(self.session, 'test')
  1382. req = pagure.lib.query.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.query._get_project(self.session, 'test')
  1422. req = pagure.lib.query.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.query._get_project(self.session, 'test')
  1454. msg = pagure.lib.query.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.query._get_project(self.session, 'test_fork', user='foo')
  1483. msg = pagure.lib.query.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.query.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.query.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.query._get_project(self.session, 'test')
  1532. msg = pagure.lib.query.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.query._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.query.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.query.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 PagureFlaskAppAboutPagetests(tests.Modeltests):
  1739. """ Unit-tests for the about page. """
  1740. def test_about_page(self):
  1741. """ Test the about page when an admin_email is set. """
  1742. output = self.app.get('/about/')
  1743. self.assertEqual(output.status_code, 200)
  1744. output_text = output.get_data(as_text=True)
  1745. self.assertIn('<title>About - Pagure</title>', output_text)
  1746. self.assertIn(
  1747. 'by emailing:\n '
  1748. '<a href="mailto:root@localhost.localdomain">', output_text)
  1749. self.assertIn(
  1750. 'href="https://pagure.io/pagure/issues">open a ticket</a>',
  1751. output_text)
  1752. @patch.dict('pagure.config.config', {'ADMIN_EMAIL': 'admin@fp.o'})
  1753. def test_about_page_admin_email(self):
  1754. """ Test the about page when an admin_email is set. """
  1755. output = self.app.get('/about/')
  1756. self.assertEqual(output.status_code, 200)
  1757. output_text = output.get_data(as_text=True)
  1758. self.assertIn('<title>About - Pagure</title>', output_text)
  1759. self.assertIn(
  1760. 'by emailing:\n <a href="mailto:admin@fp.o">',
  1761. output_text)
  1762. self.assertIn(
  1763. 'href="https://pagure.io/pagure/issues">open a ticket</a>',
  1764. output_text)
  1765. class PagureFlaskAppNoDocstests(tests.Modeltests):
  1766. """ Tests for flask app controller of pagure """
  1767. config_values = {
  1768. "enable_docs": False,
  1769. "docs_folder": None,
  1770. }
  1771. def test_new_project_no_docs_folder(self):
  1772. """ Test the new_project endpoint with DOCS_FOLDER is None. """
  1773. # Before
  1774. projects = pagure.lib.query.search_projects(self.session)
  1775. self.assertEqual(len(projects), 0)
  1776. self.assertFalse(os.path.exists(
  1777. os.path.join(self.path, 'repos', 'project#1.git')))
  1778. self.assertFalse(os.path.exists(
  1779. os.path.join(self.path, 'repos', 'tickets', 'project#1.git')))
  1780. self.assertFalse(os.path.exists(
  1781. os.path.join(self.path, 'repos', 'docs', 'project#1.git')))
  1782. self.assertFalse(os.path.exists(
  1783. os.path.join(self.path, 'repos', 'requests', 'project#1.git')))
  1784. user = tests.FakeUser(username='foo')
  1785. with tests.user_set(self.app.application, user):
  1786. csrf_token = self.get_csrf()
  1787. data = {
  1788. 'description': 'Project #1',
  1789. 'name': 'project-1',
  1790. 'csrf_token': csrf_token,
  1791. }
  1792. output = self.app.post('/new/', data=data, follow_redirects=True)
  1793. self.assertEqual(output.status_code, 200)
  1794. output_text = output.get_data(as_text=True)
  1795. self.assertIn(
  1796. '<div class="projectinfo my-3">\nProject #1',
  1797. output_text)
  1798. self.assertIn('<p>This repo is brand new!</p>',
  1799. output_text)
  1800. self.assertIn(
  1801. '<title>Overview - project-1 - Pagure</title>', output_text)
  1802. # After
  1803. projects = pagure.lib.query.search_projects(self.session)
  1804. self.assertEqual(len(projects), 1)
  1805. self.assertTrue(os.path.exists(
  1806. os.path.join(self.path, 'repos', 'project-1.git')))
  1807. self.assertTrue(os.path.exists(
  1808. os.path.join(self.path, 'repos', 'tickets', 'project-1.git')))
  1809. self.assertFalse(os.path.exists(
  1810. os.path.join(self.path, 'repos', 'docs', 'project-1.git')))
  1811. self.assertTrue(os.path.exists(
  1812. os.path.join(self.path, 'repos', 'requests', 'project-1.git')))
  1813. class PagureFlaskAppNoTicketstests(tests.Modeltests):
  1814. """ Tests for flask app controller of pagure """
  1815. config_values = {
  1816. "enable_tickets": False,
  1817. "tickets_folder": None,
  1818. }
  1819. def test_new_project_no_tickets_folder(self):
  1820. """ Test the new_project endpoint with TICKETS_FOLDER is None. """
  1821. # Before
  1822. projects = pagure.lib.query.search_projects(self.session)
  1823. self.assertEqual(len(projects), 0)
  1824. self.assertFalse(os.path.exists(
  1825. os.path.join(self.path, 'repos', 'project#1.git')))
  1826. self.assertFalse(os.path.exists(
  1827. os.path.join(self.path, 'repos', 'tickets', 'project#1.git')))
  1828. self.assertFalse(os.path.exists(
  1829. os.path.join(self.path, 'repos', 'docs', 'project#1.git')))
  1830. self.assertFalse(os.path.exists(
  1831. os.path.join(self.path, 'repos', 'requests', 'project#1.git')))
  1832. user = tests.FakeUser(username='foo')
  1833. with tests.user_set(self.app.application, user):
  1834. csrf_token = self.get_csrf()
  1835. data = {
  1836. 'description': 'Project #1',
  1837. 'name': 'project-1',
  1838. 'csrf_token': csrf_token,
  1839. }
  1840. output = self.app.post('/new/', data=data, follow_redirects=True)
  1841. self.assertEqual(output.status_code, 200)
  1842. output_text = output.get_data(as_text=True)
  1843. self.assertIn(
  1844. '<div class="projectinfo my-3">\nProject #1',
  1845. output_text)
  1846. self.assertIn('<p>This repo is brand new!</p>',
  1847. output_text)
  1848. self.assertIn(
  1849. '<title>Overview - project-1 - Pagure</title>', output_text)
  1850. # After
  1851. projects = pagure.lib.query.search_projects(self.session)
  1852. self.assertEqual(len(projects), 1)
  1853. self.assertTrue(os.path.exists(
  1854. os.path.join(self.path, 'repos', 'project-1.git')))
  1855. self.assertFalse(os.path.exists(
  1856. os.path.join(self.path, 'repos', 'tickets', 'project-1.git')))
  1857. self.assertTrue(os.path.exists(
  1858. os.path.join(self.path, 'repos', 'docs', 'project-1.git')))
  1859. self.assertTrue(os.path.exists(
  1860. os.path.join(self.path, 'repos', 'requests', 'project-1.git')))
  1861. if __name__ == '__main__':
  1862. unittest.main(verbosity=2)