test_pagure_flask_ui_app.py 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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 os
  14. import six
  15. import json
  16. import pygit2
  17. from mock import patch, MagicMock
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib
  21. import tests
  22. class PagureFlaskApptests(tests.Modeltests):
  23. """ Tests for flask app controller of pagure """
  24. def setUp(self):
  25. """ Set up the environnment, ran before every tests. """
  26. super(PagureFlaskApptests, self).setUp()
  27. pagure.APP.config['TESTING'] = True
  28. pagure.SESSION = self.session
  29. pagure.ui.SESSION = self.session
  30. pagure.ui.app.SESSION = self.session
  31. pagure.ui.filters.SESSION = self.session
  32. pagure.ui.repo.SESSION = self.session
  33. self.app = pagure.APP.test_client()
  34. def test_index(self):
  35. """ Test the index endpoint. """
  36. output = self.app.get('/')
  37. self.assertEqual(output.status_code, 200)
  38. self.assertIn(
  39. '<h2 class="m-b-1">All Projects '
  40. '<span class="label label-default">0</span></h2>', output.data)
  41. tests.create_projects(self.session)
  42. output = self.app.get('/?page=abc')
  43. self.assertEqual(output.status_code, 200)
  44. self.assertIn(
  45. '<h2 class="m-b-1">All Projects '
  46. '<span class="label label-default">3</span></h2>', output.data)
  47. # Add a 3rd project with a long description
  48. item = pagure.lib.model.Project(
  49. user_id=2, # foo
  50. name='test3',
  51. description='test project #3 with a very long description',
  52. hook_token='aaabbbeeefff',
  53. )
  54. self.session.add(item)
  55. self.session.commit()
  56. user = tests.FakeUser(username='foo')
  57. with tests.user_set(pagure.APP, user):
  58. output = self.app.get('/?repopage=abc&forkpage=def')
  59. self.assertIn(
  60. 'Projects <span class="label label-default">1</span>',
  61. output.data)
  62. self.assertIn(
  63. 'Forks <span class="label label-default">0</span>',
  64. output.data)
  65. self.assertEqual(
  66. output.data.count('<p>No group found</p>'), 1)
  67. self.assertEqual(
  68. output.data.count('<div class="card-header">'), 6)
  69. def test_watch_list(self):
  70. ''' Test for watch list of a user '''
  71. user = tests.FakeUser(username='pingou')
  72. with tests.user_set(pagure.APP, user):
  73. output = self.app.get('/')
  74. self.assertIn(
  75. '<div class="text-xs-center">You have no projects</div>',
  76. output.data)
  77. self.assertIn(
  78. '<p>You have no forks</p>',
  79. output.data)
  80. self.assertIn(
  81. '<p>No project in watch list</p>',
  82. output.data)
  83. tests.create_projects(self.session)
  84. output = self.app.get('/')
  85. self.assertIn(
  86. 'My Projects <span class="label label-default">3</span>',
  87. output.data)
  88. self.assertIn(
  89. 'My Forks <span class="label label-default">0</span>',
  90. output.data)
  91. self.assertIn(
  92. 'My Watch List <span class="label label-default">3</span>',
  93. output.data)
  94. def test_view_users(self):
  95. """ Test the view_users endpoint. """
  96. output = self.app.get('/users/?page=abc')
  97. self.assertEqual(output.status_code, 200)
  98. self.assertIn(
  99. '<h2 class="m-b-1">\n Users '
  100. '<span class="label label-default">2</span></h2>', output.data)
  101. self.assertIn(
  102. '<a class="project_link logo_link" href="/user/pingou">',
  103. output.data)
  104. self.assertIn(
  105. '<a class="project_link logo_link" href="/user/foo">',
  106. output.data)
  107. def test_view_user(self):
  108. """ Test the view_user endpoint. """
  109. output = self.app.get('/user/pingou?repopage=abc&forkpage=def')
  110. self.assertEqual(output.status_code, 200)
  111. self.assertIn(
  112. 'Projects <span class="label label-default">0</span>',
  113. output.data)
  114. self.assertIn(
  115. 'Forks <span class="label label-default">0</span>',
  116. output.data)
  117. tests.create_projects(self.session)
  118. self.gitrepos = tests.create_projects_git(
  119. pagure.APP.config['GIT_FOLDER'])
  120. output = self.app.get('/user/pingou?repopage=abc&forkpage=def')
  121. self.assertEqual(output.status_code, 200)
  122. self.assertIn(
  123. 'Projects <span class="label label-default">3</span>',
  124. output.data)
  125. self.assertIn(
  126. 'Forks <span class="label label-default">0</span>', output.data)
  127. def test_new_project_when_turned_off(self):
  128. """ Test the new_project endpoint when new project creation is
  129. not allowed in the pagure instance. """
  130. #turn the project creation off
  131. pagure.APP.config['ENABLE_NEW_PROJECTS'] = False
  132. # Before
  133. projects = pagure.lib.search_projects(self.session)
  134. self.assertEqual(len(projects), 0)
  135. self.assertFalse(os.path.exists(
  136. os.path.join(self.path, 'repos', 'project-1.git')))
  137. self.assertFalse(os.path.exists(
  138. os.path.join(self.path, 'tickets', 'project-1.git')))
  139. self.assertFalse(os.path.exists(
  140. os.path.join(self.path, 'docs', 'project-1.git')))
  141. self.assertFalse(os.path.exists(
  142. os.path.join(self.path, 'requests', 'project-1.git')))
  143. user = tests.FakeUser()
  144. with tests.user_set(pagure.APP, user):
  145. output = self.app.get('/new/')
  146. self.assertEqual(output.status_code, 404)
  147. #just get the csrf token
  148. pagure.APP.config['ENABLE_NEW_PROJECTS'] = True
  149. output = self.app.get('/new/')
  150. pagure.APP.config['ENABLE_NEW_PROJECTS'] = False
  151. csrf_token = output.data.split(
  152. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  153. data = {
  154. 'description': 'Project #1',
  155. 'name': 'project-1',
  156. }
  157. user.username = 'foo'
  158. with tests.user_set(pagure.APP, user):
  159. data['csrf_token'] = csrf_token
  160. output = self.app.post('/new/', data=data, follow_redirects=True)
  161. self.assertEqual(output.status_code, 404)
  162. #After
  163. projects = pagure.lib.search_projects(self.session)
  164. self.assertEqual(len(projects), 0)
  165. self.assertFalse(os.path.exists(
  166. os.path.join(self.path, 'repos', 'project-1.git')))
  167. self.assertFalse(os.path.exists(
  168. os.path.join(self.path, 'tickets', 'project-1.git')))
  169. self.assertFalse(os.path.exists(
  170. os.path.join(self.path, 'docs', 'project-1.git')))
  171. self.assertFalse(os.path.exists(
  172. os.path.join(self.path, 'requests', 'project-1.git')))
  173. pagure.APP.config['ENABLE_NEW_PROJECTS'] = True
  174. def test_new_project(self):
  175. """ Test the new_project endpoint. """
  176. # Before
  177. projects = pagure.lib.search_projects(self.session)
  178. self.assertEqual(len(projects), 0)
  179. self.assertFalse(os.path.exists(
  180. os.path.join(self.path, 'repos', 'project#1.git')))
  181. self.assertFalse(os.path.exists(
  182. os.path.join(self.path, 'tickets', 'project#1.git')))
  183. self.assertFalse(os.path.exists(
  184. os.path.join(self.path, 'docs', 'project#1.git')))
  185. self.assertFalse(os.path.exists(
  186. os.path.join(self.path, 'requests', 'project#1.git')))
  187. user = tests.FakeUser()
  188. with tests.user_set(pagure.APP, user):
  189. output = self.app.get('/new/')
  190. self.assertEqual(output.status_code, 200)
  191. self.assertIn(
  192. u'<strong>Create new Project</strong>', output.data)
  193. csrf_token = output.data.split(
  194. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  195. data = {
  196. 'description': 'Project #1',
  197. }
  198. output = self.app.post('/new/', data=data)
  199. self.assertEqual(output.status_code, 200)
  200. self.assertIn(
  201. u'<strong>Create new Project</strong>', output.data)
  202. self.assertIn(
  203. u'<small>\n This field is required.&nbsp;\n'
  204. ' </small>', output.data)
  205. data['name'] = 'project-1'
  206. output = self.app.post('/new/', data=data)
  207. self.assertEqual(output.status_code, 200)
  208. self.assertIn('<strong>Create new Project</strong>', output.data)
  209. self.assertNotIn(
  210. u'<small>\n This field is required.&nbsp;\n'
  211. ' </small>', output.data)
  212. data['csrf_token'] = csrf_token
  213. output = self.app.post('/new/', data=data)
  214. self.assertEqual(output.status_code, 200)
  215. self.assertIn('<strong>Create new Project</strong>', output.data)
  216. self.assertIn(
  217. u'</button>\n No user '
  218. '&#34;username&#34; found\n </div>',
  219. output.data)
  220. user.username = 'foo'
  221. with tests.user_set(pagure.APP, user):
  222. data['csrf_token'] = csrf_token
  223. output = self.app.post('/new/', data=data, follow_redirects=True)
  224. self.assertEqual(output.status_code, 200)
  225. self.assertIn(
  226. u'<div class="projectinfo m-t-1 m-b-1">\nProject #1 </div>',
  227. output.data)
  228. self.assertIn(u'<p>This repo is brand new!</p>', output.data)
  229. self.assertIn(
  230. u'<title>Overview - project-1 - Pagure</title>', output.data)
  231. # After
  232. projects = pagure.lib.search_projects(self.session)
  233. self.assertEqual(len(projects), 1)
  234. self.assertTrue(os.path.exists(
  235. os.path.join(self.path, 'repos', 'project-1.git')))
  236. self.assertTrue(os.path.exists(
  237. os.path.join(self.path, 'tickets', 'project-1.git')))
  238. self.assertTrue(os.path.exists(
  239. os.path.join(self.path, 'docs', 'project-1.git')))
  240. self.assertTrue(os.path.exists(
  241. os.path.join(self.path, 'requests', 'project-1.git')))
  242. @patch.dict('pagure.APP.config', {'PRIVATE_PROJECTS': True})
  243. def test_new_project_private(self):
  244. """ Test the new_project endpoint for a private project. """
  245. # Before
  246. projects = pagure.lib.search_projects(self.session)
  247. self.assertEqual(len(projects), 0)
  248. self.assertFalse(os.path.exists(
  249. os.path.join(self.path, 'repos', 'foo', 'project#1.git')))
  250. self.assertFalse(os.path.exists(
  251. os.path.join(self.path, 'tickets', 'foo', 'project#1.git')))
  252. self.assertFalse(os.path.exists(
  253. os.path.join(self.path, 'docs', 'foo', 'project#1.git')))
  254. self.assertFalse(os.path.exists(
  255. os.path.join(self.path, 'requests', 'foo', 'project#1.git')))
  256. user = tests.FakeUser()
  257. with tests.user_set(pagure.APP, user):
  258. output = self.app.get('/new/')
  259. self.assertEqual(output.status_code, 200)
  260. self.assertIn(
  261. u'<strong>Create new Project</strong>', output.data)
  262. csrf_token = output.data.split(
  263. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  264. data = {
  265. 'description': 'Project #1',
  266. 'private': True,
  267. }
  268. output = self.app.post('/new/', data=data)
  269. self.assertEqual(output.status_code, 200)
  270. self.assertIn(
  271. u'<strong>Create new Project</strong>', output.data)
  272. self.assertIn(
  273. u'<small>\n This field is required.&nbsp;\n'
  274. ' </small>', output.data)
  275. data['name'] = 'project-1'
  276. output = self.app.post('/new/', data=data)
  277. self.assertEqual(output.status_code, 200)
  278. self.assertIn('<strong>Create new Project</strong>', output.data)
  279. self.assertNotIn(
  280. u'<small>\n This field is required.&nbsp;\n'
  281. ' </small>', output.data)
  282. data['csrf_token'] = csrf_token
  283. output = self.app.post('/new/', data=data)
  284. self.assertEqual(output.status_code, 200)
  285. self.assertIn('<strong>Create new Project</strong>', output.data)
  286. self.assertIn(
  287. u'</button>\n No user '
  288. '&#34;username&#34; found\n </div>',
  289. output.data)
  290. user.username = 'foo'
  291. with tests.user_set(pagure.APP, user):
  292. data['csrf_token'] = csrf_token
  293. output = self.app.post('/new/', data=data, follow_redirects=True)
  294. self.assertEqual(output.status_code, 200)
  295. self.assertIn(
  296. u'<div class="projectinfo m-t-1 m-b-1">\nProject #1 </div>',
  297. output.data)
  298. self.assertIn(u'<p>This repo is brand new!</p>', output.data)
  299. self.assertIn(
  300. u'<title>Overview - foo/project-1 - Pagure</title>', output.data)
  301. # After
  302. projects = pagure.lib.search_projects(self.session)
  303. self.assertEqual(len(projects), 0)
  304. projects = pagure.lib.search_projects(self.session, private=True)
  305. self.assertEqual(len(projects), 1)
  306. self.assertTrue(os.path.exists(
  307. os.path.join(self.path, 'repos', 'foo', 'project-1.git')))
  308. self.assertTrue(os.path.exists(
  309. os.path.join(self.path, 'tickets', 'foo', 'project-1.git')))
  310. self.assertTrue(os.path.exists(
  311. os.path.join(self.path, 'docs', 'foo', 'project-1.git')))
  312. self.assertTrue(os.path.exists(
  313. os.path.join(self.path, 'requests', 'foo', 'project-1.git')))
  314. def test_non_ascii_new_project(self):
  315. """ Test the new_project endpoint with a non-ascii project. """
  316. # Before
  317. projects = pagure.lib.search_projects(self.session)
  318. self.assertEqual(len(projects), 0)
  319. self.assertFalse(os.path.exists(
  320. os.path.join(self.path, 'repos', 'project-1.git')))
  321. self.assertFalse(os.path.exists(
  322. os.path.join(self.path, 'tickets', 'project-1.git')))
  323. self.assertFalse(os.path.exists(
  324. os.path.join(self.path, 'docs', 'project-1.git')))
  325. self.assertFalse(os.path.exists(
  326. os.path.join(self.path, 'requests', 'project-1.git')))
  327. user = tests.FakeUser()
  328. user.username = 'foo'
  329. with tests.user_set(pagure.APP, user):
  330. output = self.app.get('/new/')
  331. self.assertEqual(output.status_code, 200)
  332. self.assertIn(
  333. b'<strong>Create new Project</strong>', output.data)
  334. csrf_token = output.data.decode('utf-8').split(
  335. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  336. data = {
  337. 'description': 'Prõjéctö #1',
  338. 'name': 'project-1',
  339. 'csrf_token': csrf_token,
  340. 'create_readme': True,
  341. }
  342. output = self.app.post('/new/', data=data, follow_redirects=True)
  343. self.assertEqual(output.status_code, 200)
  344. self.assertIn(
  345. '<div class="projectinfo m-t-1 m-b-1">\nPrõjéctö #1 </div>',
  346. output.data if six.PY2 else output.data.decode('utf-8'))
  347. self.assertIn(
  348. '''<section class="readme">
  349. <h1>project-1</h1>
  350. <p>Prõjéctö #1</p>
  351. </section>''', output.data if six.PY2 else output.data.decode('utf-8'))
  352. data = {
  353. 'description': 'Мой первый суперский репозиторий',
  354. 'name': 'project-2',
  355. 'csrf_token': csrf_token,
  356. 'create_readme': True,
  357. }
  358. output = self.app.post('/new/', data=data, follow_redirects=True)
  359. self.assertEqual(output.status_code, 200)
  360. self.assertIn(
  361. '<div class="projectinfo m-t-1 m-b-1">\nМой первый суперский репозиторий </div>',
  362. output.data if six.PY2 else output.data.decode('utf-8'))
  363. self.assertIn(
  364. '''<section class="readme">
  365. <h1>project-2</h1>
  366. <p>Мой первый суперский репозиторий</p>
  367. </section>''', output.data if six.PY2 else output.data.decode('utf-8'))
  368. # After
  369. projects = pagure.lib.search_projects(self.session)
  370. self.assertEqual(len(projects), 2)
  371. for project in ['project-1', 'project-2']:
  372. self.assertTrue(os.path.exists(
  373. os.path.join(self.path, 'repos', '%s.git' % project)))
  374. self.assertTrue(os.path.exists(
  375. os.path.join(self.path, 'tickets', '%s.git' % project)))
  376. self.assertTrue(os.path.exists(
  377. os.path.join(self.path, 'docs', '%s.git' % project)))
  378. self.assertTrue(os.path.exists(
  379. os.path.join(self.path, 'requests', '%s.git' % project)))
  380. @patch('pagure.ui.app.admin_session_timedout')
  381. def test_user_settings(self, ast):
  382. """ Test the user_settings endpoint. """
  383. ast.return_value = False
  384. self.test_new_project()
  385. user = tests.FakeUser()
  386. with tests.user_set(pagure.APP, user):
  387. output = self.app.get('/settings/')
  388. self.assertEqual(output.status_code, 404)
  389. self.assertTrue('<h2>Page not found (404)</h2>' in output.data)
  390. user.username = 'foo'
  391. with tests.user_set(pagure.APP, user):
  392. output = self.app.get('/settings/')
  393. self.assertEqual(output.status_code, 200)
  394. self.assertIn(
  395. '<div class="card-header">\n Basic Information\n'
  396. ' </div>', output.data)
  397. self.assertIn(
  398. '<textarea class="form-control" id="ssh_key" name="ssh_key">'
  399. '</textarea>', output.data)
  400. csrf_token = output.data.split(
  401. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  402. data = {
  403. 'ssh_key': 'blah'
  404. }
  405. output = self.app.post('/settings/', data=data)
  406. self.assertEqual(output.status_code, 200)
  407. self.assertIn(
  408. '<div class="card-header">\n Basic Information\n'
  409. ' </div>', output.data)
  410. data['csrf_token'] = csrf_token
  411. output = self.app.post(
  412. '/settings/', data=data, follow_redirects=True)
  413. self.assertEqual(output.status_code, 200)
  414. self.assertIn('Invalid SSH keys', output.data)
  415. self.assertIn(
  416. '<div class="card-header">\n Basic Information\n'
  417. ' </div>', output.data)
  418. self.assertIn('>blah</textarea>', output.data)
  419. csrf_token = output.data.split(
  420. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  421. data = {
  422. 'ssh_key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDUkub32fZnNI'
  423. '1zJYs43vhhx3c6IcYo4yzhw1gQ37BLhrrNeS6x8l5PKX4J8ZP5'
  424. '1XhViPaLbeOpl94Vm5VSCbLy0xtY9KwLhMkbKj7g6vvfxLm2sT'
  425. 'Osb15j4jzIkUYYgIE7cHhZMCLWR6UA1c1HEzo6mewMDsvpQ9wk'
  426. 'cDnAuXjK3Q==',
  427. 'csrf_token': csrf_token
  428. }
  429. output = self.app.post(
  430. '/settings/', data=data, follow_redirects=True)
  431. self.assertEqual(output.status_code, 200)
  432. self.assertIn('Public ssh key updated', output.data)
  433. self.assertIn(
  434. '<div class="card-header">\n Basic Information\n'
  435. ' </div>', output.data)
  436. self.assertIn(
  437. '<textarea class="form-control" id="ssh_key" name="ssh_key">'
  438. 'ssh-rsa AAAA', output.data)
  439. ast.return_value = True
  440. output = self.app.get('/settings/')
  441. self.assertEqual(output.status_code, 302)
  442. def patched_commit_exists(user, namespace, repo, githash):
  443. ''' Patched version of pagure.pfmarkdown._commit_exists to enforce
  444. returning true on some given hash without having us actually check
  445. the git repos.
  446. '''
  447. if githash in ['9364354', '9364354a', '9364354a4555ba17aa60f0dc844d70b74eb1aecd']:
  448. return True
  449. else:
  450. return False
  451. @patch(
  452. 'pagure.pfmarkdown._commit_exists',
  453. MagicMock(side_effect=patched_commit_exists))
  454. def test_patched_markdown_preview(self):
  455. """ Test the markdown_preview endpoint. """
  456. data = {
  457. 'content': 'test\n----\n\n * 1\n * item 2'
  458. }
  459. # CSRF missing
  460. output = self.app.post('/markdown/', data=data)
  461. self.assertEqual(output.status_code, 400)
  462. user = tests.FakeUser()
  463. user.username = 'foo'
  464. with tests.user_set(pagure.APP, user):
  465. output = self.app.get('/settings/')
  466. self.assertEqual(output.status_code, 200)
  467. self.assertIn(
  468. '<div class="card-header">\n Basic Information\n'
  469. ' </div>', output.data)
  470. self.assertIn(
  471. '<textarea class="form-control" id="ssh_key" name="ssh_key">'
  472. '</textarea>', output.data)
  473. csrf_token = output.data.split(
  474. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  475. # With CSRF
  476. data['csrf_token'] = csrf_token
  477. output = self.app.post('/markdown/', data=data)
  478. self.assertEqual(output.status_code, 200)
  479. exp = """<h2>test</h2>
  480. <ul>
  481. <li>1</li>
  482. <li>item 2</li>
  483. </ul>"""
  484. self.assertEqual(output.data, exp)
  485. tests.create_projects(self.session)
  486. texts = [
  487. 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  488. 'Cf commit 936435', # 6 chars - not long enough
  489. 'Cf commit 9364354', # 7 chars - long enough
  490. 'Cf commit 9364354a', # 8 chars - still long enough
  491. 'Cf commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd', # 40 chars
  492. ]
  493. expected = [
  494. # 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  495. '<p>pingou committed on <a href="/test/c/9364354a4555ba17aa60f0dc844d70b74eb1aecd" '
  496. 'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
  497. '>test#9364354a4555ba17aa60f0dc844d70b74eb1aecd</a></p>',
  498. # 'Cf commit 936435',
  499. '<p>Cf commit 936435</p>',
  500. # 'Cf commit 9364354',
  501. #'<p>Cf commit 9364354</p>',
  502. '<p>Cf commit<a href="/test/c/9364354" '
  503. 'title="Commit 9364354"> 9364354</a></p>',
  504. # 'Cf commit 9364354a',
  505. '<p>Cf commit<a href="/test/c/9364354a" '
  506. 'title="Commit 9364354a"> 9364354</a></p>',
  507. # 'Cf commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd',
  508. '<p>Cf commit<a href="/test/c/9364354a4555ba17aa60f0dc844d70b74eb1aecd" '
  509. 'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
  510. '> 9364354</a></p>',
  511. ]
  512. with pagure.APP.app_context():
  513. for idx, text in enumerate(texts):
  514. data = {
  515. 'content': text,
  516. 'csrf_token': csrf_token,
  517. }
  518. output = self.app.post('/markdown/?repo=test', data=data)
  519. self.assertEqual(output.status_code, 200)
  520. self.assertEqual(expected[idx], output.data)
  521. def test_markdown_preview(self):
  522. """ Test the markdown_preview endpoint with a non-existing commit.
  523. """
  524. user = tests.FakeUser()
  525. user.username = 'foo'
  526. with tests.user_set(pagure.APP, user):
  527. output = self.app.get('/settings/')
  528. self.assertEqual(output.status_code, 200)
  529. self.assertIn(
  530. '<div class="card-header">\n Basic Information\n'
  531. ' </div>', output.data)
  532. csrf_token = output.data.split(
  533. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  534. tests.create_projects(self.session)
  535. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  536. text = 'Cf commit 9364354a4555ba17aa60f0d'
  537. exp = '<p>Cf commit 9364354a4555ba17aa60f0d</p>'
  538. with pagure.APP.app_context():
  539. data = {
  540. 'content': text,
  541. 'csrf_token': csrf_token,
  542. }
  543. output = self.app.post('/markdown/?repo=test', data=data)
  544. self.assertEqual(output.status_code, 200)
  545. self.assertEqual(exp, output.data)
  546. def test_markdown_preview_valid_commit(self):
  547. """ Test the markdown_preview endpoint with an existing commit. """
  548. user = tests.FakeUser()
  549. user.username = 'foo'
  550. with tests.user_set(pagure.APP, user):
  551. output = self.app.get('/settings/')
  552. self.assertEqual(output.status_code, 200)
  553. self.assertIn(
  554. '<div class="card-header">\n Basic Information\n'
  555. ' </div>', output.data)
  556. csrf_token = output.data.split(
  557. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  558. tests.create_projects(self.session)
  559. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  560. repopath = os.path.join(self.path, 'repos', 'test.git')
  561. tests.add_content_git_repo(repopath)
  562. repo = pygit2.Repository(repopath)
  563. first_commit = repo.revparse_single('HEAD')
  564. text = 'Cf commit %s' % first_commit.oid.hex
  565. exp = '<p>Cf commit<a href="/test/c/{0}" title="Commit {0}"> {1}'\
  566. '</a></p>'.format(first_commit.oid.hex, first_commit.oid.hex[:7])
  567. with pagure.APP.app_context():
  568. data = {
  569. 'content': text,
  570. 'csrf_token': csrf_token,
  571. }
  572. output = self.app.post('/markdown/?repo=test', data=data)
  573. self.assertEqual(output.status_code, 200)
  574. self.assertEqual(exp, output.data)
  575. @patch('pagure.ui.app.admin_session_timedout')
  576. def test_remove_user_email(self, ast):
  577. """ Test the remove_user_email endpoint. """
  578. ast.return_value = False
  579. self.test_new_project()
  580. user = tests.FakeUser()
  581. with tests.user_set(pagure.APP, user):
  582. output = self.app.post('/settings/email/drop')
  583. self.assertEqual(output.status_code, 404)
  584. self.assertTrue('<h2>Page not found (404)</h2>' in output.data)
  585. user.username = 'foo'
  586. with tests.user_set(pagure.APP, user):
  587. output = self.app.post('/settings/')
  588. self.assertEqual(output.status_code, 200)
  589. self.assertIn(
  590. '<div class="card-header">\n Basic Information\n'
  591. ' </div>', output.data)
  592. self.assertIn(
  593. '<textarea class="form-control form-control-error" id="ssh_key" name="ssh_key">'
  594. '</textarea>', output.data)
  595. csrf_token = output.data.split(
  596. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  597. data = {
  598. 'email': 'foo@pingou.com',
  599. }
  600. output = self.app.post(
  601. '/settings/email/drop', data=data, follow_redirects=True)
  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.assertIn(
  607. '<textarea class="form-control" id="ssh_key" name="ssh_key">'
  608. '</textarea>', output.data)
  609. self.assertIn(
  610. '</button>\n You must always have at least one email',
  611. output.data)
  612. user.username = 'pingou'
  613. with tests.user_set(pagure.APP, user):
  614. output = self.app.post('/settings/')
  615. self.assertEqual(output.status_code, 200)
  616. self.assertIn(
  617. '<div class="card-header">\n Basic Information\n'
  618. ' </div>', output.data)
  619. self.assertIn(
  620. '<textarea class="form-control form-control-error" id="ssh_key" name="ssh_key">'
  621. '</textarea>', output.data)
  622. csrf_token = output.data.split(
  623. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  624. data = {
  625. 'email': 'foo@pingou.com',
  626. }
  627. output = self.app.post(
  628. '/settings/email/drop', data=data, follow_redirects=True)
  629. self.assertEqual(output.status_code, 200)
  630. self.assertIn(
  631. '<div class="card-header">\n Basic Information\n'
  632. ' </div>', output.data)
  633. self.assertEqual(output.data.count('foo@pingou.com'), 4)
  634. data = {
  635. 'csrf_token': csrf_token,
  636. 'email': 'foobar@pingou.com',
  637. }
  638. output = self.app.post(
  639. '/settings/email/drop', data=data, follow_redirects=True)
  640. self.assertEqual(output.status_code, 200)
  641. self.assertIn(
  642. '<div class="card-header">\n Basic Information\n'
  643. ' </div>', output.data)
  644. self.assertIn(
  645. '</button>\n You do not have the '
  646. 'email: foobar@pingou.com, nothing to remove', output.data)
  647. data = {
  648. 'csrf_token': csrf_token,
  649. 'email': 'foo@pingou.com',
  650. }
  651. output = self.app.post(
  652. '/settings/email/drop', data=data, follow_redirects=True)
  653. self.assertEqual(output.status_code, 200)
  654. self.assertEqual(output.data.count('foo@pingou.com'), 0)
  655. self.assertEqual(output.data.count('bar@pingou.com'), 3)
  656. output = self.app.post(
  657. '/settings/email/drop', data=data, follow_redirects=True)
  658. self.assertEqual(output.status_code, 200)
  659. self.assertEqual(output.data.count('foo@pingou.com'), 0)
  660. self.assertEqual(output.data.count('bar@pingou.com'), 3)
  661. ast.return_value = True
  662. output = self.app.post('/settings/email/drop', data=data)
  663. self.assertEqual(output.status_code, 302)
  664. @patch('pagure.lib.notify.send_email')
  665. @patch('pagure.ui.app.admin_session_timedout')
  666. def test_add_api_user_email(self, ast, send_email):
  667. """ Test the add_api_user_email endpoint. """
  668. send_email.return_value = True
  669. ast.return_value = False
  670. self.test_new_project()
  671. user = tests.FakeUser()
  672. with tests.user_set(pagure.APP, user):
  673. output = self.app.post('/settings/email/add')
  674. self.assertEqual(output.status_code, 404)
  675. self.assertTrue('<h2>Page not found (404)</h2>' in output.data)
  676. user.username = 'foo'
  677. with tests.user_set(pagure.APP, user):
  678. output = self.app.post('/settings/email/add')
  679. self.assertEqual(output.status_code, 200)
  680. self.assertTrue("<strong>Add new email</strong>" in output.data)
  681. self.assertIn(
  682. '<input class="form-control form-control-error" id="email" '
  683. 'name="email" type="text" value="">', output.data)
  684. user.username = 'pingou'
  685. with tests.user_set(pagure.APP, user):
  686. output = self.app.post('/settings/email/add')
  687. self.assertEqual(output.status_code, 200)
  688. self.assertTrue("<strong>Add new email</strong>" in output.data)
  689. self.assertIn(
  690. '<input class="form-control form-control-error" id="email" '
  691. 'name="email" type="text" value="">', output.data)
  692. csrf_token = output.data.split(
  693. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  694. data = {
  695. 'email': 'foo2@pingou.com',
  696. }
  697. output = self.app.post(
  698. '/settings/email/add', data=data, follow_redirects=True)
  699. self.assertEqual(output.status_code, 200)
  700. self.assertTrue("<strong>Add new email</strong>" in output.data)
  701. self.assertEqual(output.data.count('foo2@pingou.com'), 1)
  702. # New email
  703. data = {
  704. 'csrf_token': csrf_token,
  705. 'email': 'foobar@pingou.com',
  706. }
  707. output = self.app.post(
  708. '/settings/email/add', data=data, follow_redirects=True)
  709. self.assertEqual(output.status_code, 200)
  710. self.assertIn(
  711. '<div class="card-header">\n Basic Information\n'
  712. ' </div>', output.data)
  713. self.assertIn(
  714. '</button>\n Email pending validation',
  715. output.data)
  716. self.assertEqual(output.data.count('foo@pingou.com'), 4)
  717. self.assertEqual(output.data.count('bar@pingou.com'), 5)
  718. self.assertEqual(output.data.count('foobar@pingou.com'), 2)
  719. # Email already pending
  720. output = self.app.post(
  721. '/settings/email/add', data=data, follow_redirects=True)
  722. self.assertEqual(output.status_code, 200)
  723. self.assertIn(
  724. '<div class="card-header">\n '
  725. '<strong>Add new email</strong>', output.data)
  726. self.assertIn(
  727. '</button>\n This email is already '
  728. 'pending confirmation', output.data)
  729. # User already has this email
  730. data = {
  731. 'csrf_token': csrf_token,
  732. 'email': 'foo@pingou.com',
  733. }
  734. output = self.app.post(
  735. '/settings/email/add', data=data, follow_redirects=True)
  736. self.assertEqual(output.status_code, 200)
  737. self.assertTrue("<strong>Add new email</strong>" in output.data)
  738. self.assertTrue(
  739. 'Invalid value, can&#39;t be any of: bar@pingou.com, '
  740. 'foo@pingou.com.&nbsp;' in output.data
  741. or
  742. 'Invalid value, can&#39;t be any of: foo@pingou.com, '
  743. 'bar@pingou.com.&nbsp;' in output.data
  744. )
  745. self.assertEqual(output.data.count('foo@pingou.com'), 6)
  746. self.assertEqual(output.data.count('bar@pingou.com'), 5)
  747. self.assertEqual(output.data.count('foobar@pingou.com'), 0)
  748. # Email registered by someone else
  749. data = {
  750. 'csrf_token': csrf_token,
  751. 'email': 'foo@bar.com',
  752. }
  753. output = self.app.post(
  754. '/settings/email/add', data=data, follow_redirects=True)
  755. self.assertEqual(output.status_code, 200)
  756. self.assertTrue("<strong>Add new email</strong>" in output.data)
  757. self.assertIn(
  758. 'Invalid value, can&#39;t be any of: foo@bar.com.&nbsp;',
  759. output.data)
  760. ast.return_value = True
  761. output = self.app.post('/settings/email/add', data=data)
  762. self.assertEqual(output.status_code, 302)
  763. @patch('pagure.lib.notify.send_email')
  764. @patch('pagure.ui.app.admin_session_timedout')
  765. def test_set_default_email(self, ast, send_email):
  766. """ Test the set_default_email endpoint. """
  767. send_email.return_value = True
  768. ast.return_value = False
  769. self.test_new_project()
  770. user = tests.FakeUser()
  771. with tests.user_set(pagure.APP, user):
  772. output = self.app.post('/settings/email/default')
  773. self.assertEqual(output.status_code, 404)
  774. self.assertTrue('<h2>Page not found (404)</h2>' in output.data)
  775. user.username = 'pingou'
  776. with tests.user_set(pagure.APP, user):
  777. output = self.app.get('/settings/')
  778. self.assertEqual(output.status_code, 200)
  779. self.assertIn(
  780. '<div class="card-header">\n Basic Information\n'
  781. ' </div>', output.data)
  782. self.assertIn(
  783. '<textarea class="form-control" id="ssh_key" name="ssh_key">'
  784. '</textarea>', output.data)
  785. csrf_token = output.data.split(
  786. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  787. data = {
  788. 'email': 'foo@pingou.com',
  789. }
  790. output = self.app.post(
  791. '/settings/email/default', data=data, follow_redirects=True)
  792. self.assertEqual(output.status_code, 200)
  793. self.assertIn(
  794. '<div class="card-header">\n Basic Information\n'
  795. ' </div>', output.data)
  796. self.assertEqual(output.data.count('foo@pingou.com'), 4)
  797. # Set invalid default email
  798. data = {
  799. 'csrf_token': csrf_token,
  800. 'email': 'foobar@pingou.com',
  801. }
  802. output = self.app.post(
  803. '/settings/email/default', data=data, follow_redirects=True)
  804. self.assertEqual(output.status_code, 200)
  805. self.assertIn(
  806. '<div class="card-header">\n Basic Information\n'
  807. ' </div>', output.data)
  808. self.assertEqual(output.data.count('foo@pingou.com'), 4)
  809. self.assertIn(
  810. '</button>\n You do not have the '
  811. 'email: foobar@pingou.com, nothing to set',
  812. output.data)
  813. # Set default email
  814. data = {
  815. 'csrf_token': csrf_token,
  816. 'email': 'foo@pingou.com',
  817. }
  818. output = self.app.post(
  819. '/settings/email/default', data=data, follow_redirects=True)
  820. self.assertEqual(output.status_code, 200)
  821. self.assertIn(
  822. '<div class="card-header">\n Basic Information\n'
  823. ' </div>', output.data)
  824. self.assertEqual(output.data.count('foo@pingou.com'), 4)
  825. self.assertIn(
  826. '</button>\n Default email set to: '
  827. 'foo@pingou.com', output.data)
  828. ast.return_value = True
  829. output = self.app.post('/settings/email/default', data=data)
  830. self.assertEqual(output.status_code, 302)
  831. @patch('pagure.lib.notify.send_email')
  832. @patch('pagure.ui.app.admin_session_timedout')
  833. def test_reconfirm_email(self, ast, send_email):
  834. """ Test the reconfirm_email endpoint. """
  835. send_email.return_value = True
  836. ast.return_value = False
  837. self.test_new_project()
  838. # Add a pending email to pingou
  839. userobj = pagure.lib.search_user(self.session, username='pingou')
  840. self.assertEqual(len(userobj.emails), 2)
  841. email_pend = pagure.lib.model.UserEmailPending(
  842. user_id=userobj.id,
  843. email='foo@fp.o',
  844. token='abcdef',
  845. )
  846. self.session.add(email_pend)
  847. self.session.commit()
  848. user = tests.FakeUser()
  849. with tests.user_set(pagure.APP, user):
  850. output = self.app.post('/settings/email/resend')
  851. self.assertEqual(output.status_code, 404)
  852. self.assertTrue('<h2>Page not found (404)</h2>' in output.data)
  853. user.username = 'pingou'
  854. with tests.user_set(pagure.APP, user):
  855. output = self.app.get('/settings/')
  856. self.assertEqual(output.status_code, 200)
  857. self.assertIn(
  858. '<div class="card-header">\n Basic Information\n'
  859. ' </div>', output.data)
  860. self.assertIn(
  861. '<textarea class="form-control" id="ssh_key" name="ssh_key">'
  862. '</textarea>', output.data)
  863. csrf_token = output.data.split(
  864. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  865. data = {
  866. 'email': 'foo@pingou.com',
  867. }
  868. output = self.app.post(
  869. '/settings/email/resend', data=data, follow_redirects=True)
  870. self.assertEqual(output.status_code, 200)
  871. self.assertIn(
  872. '<div class="card-header">\n Basic Information\n'
  873. ' </div>', output.data)
  874. self.assertEqual(output.data.count('foo@pingou.com'), 4)
  875. # Set invalid default email
  876. data = {
  877. 'csrf_token': csrf_token,
  878. 'email': 'foobar@pingou.com',
  879. }
  880. output = self.app.post(
  881. '/settings/email/resend', data=data, follow_redirects=True)
  882. self.assertEqual(output.status_code, 200)
  883. self.assertIn(
  884. '<div class="card-header">\n Basic Information\n'
  885. ' </div>', output.data)
  886. self.assertEqual(output.data.count('foo@pingou.com'), 4)
  887. self.assertIn(
  888. '</button>\n This email address has '
  889. 'already been confirmed', output.data)
  890. # Validate a non-validated email
  891. data = {
  892. 'csrf_token': csrf_token,
  893. 'email': 'foo@fp.o',
  894. }
  895. output = self.app.post(
  896. '/settings/email/resend', data=data, follow_redirects=True)
  897. self.assertEqual(output.status_code, 200)
  898. self.assertIn(
  899. '<div class="card-header">\n Basic Information\n'
  900. ' </div>', output.data)
  901. self.assertEqual(output.data.count('foo@pingou.com'), 4)
  902. self.assertIn(
  903. '</button>\n Confirmation email re-sent',
  904. output.data)
  905. ast.return_value = True
  906. output = self.app.post('/settings/email/resend', data=data)
  907. self.assertEqual(output.status_code, 302)
  908. @patch('pagure.ui.app.admin_session_timedout')
  909. def test_confirm_email(self, ast):
  910. """ Test the confirm_email endpoint. """
  911. output = self.app.get('/settings/email/confirm/foobar')
  912. self.assertEqual(output.status_code, 302)
  913. ast.return_value = False
  914. # Add a pending email to pingou
  915. userobj = pagure.lib.search_user(self.session, username='pingou')
  916. self.assertEqual(len(userobj.emails), 2)
  917. email_pend = pagure.lib.model.UserEmailPending(
  918. user_id=userobj.id,
  919. email='foo@fp.o',
  920. token='abcdef',
  921. )
  922. self.session.add(email_pend)
  923. self.session.commit()
  924. user = tests.FakeUser()
  925. user.username = 'pingou'
  926. with tests.user_set(pagure.APP, user):
  927. # Wrong token
  928. output = self.app.get(
  929. '/settings/email/confirm/foobar', follow_redirects=True)
  930. self.assertEqual(output.status_code, 200)
  931. self.assertIn(
  932. '<div class="card-header">\n Basic Information\n'
  933. ' </div>', output.data)
  934. self.assertIn(
  935. '</button>\n No email associated with this token.',
  936. output.data)
  937. # Confirm email
  938. output = self.app.get(
  939. '/settings/email/confirm/abcdef', follow_redirects=True)
  940. self.assertEqual(output.status_code, 200)
  941. self.assertIn(
  942. '<div class="card-header">\n Basic Information\n'
  943. ' </div>', output.data)
  944. self.assertIn(
  945. '</button>\n Email validated',
  946. output.data)
  947. userobj = pagure.lib.search_user(self.session, username='pingou')
  948. self.assertEqual(len(userobj.emails), 3)
  949. ast.return_value = True
  950. output = self.app.get('/settings/email/confirm/foobar')
  951. self.assertEqual(output.status_code, 302)
  952. def test_view_my_requests_no_user(self):
  953. """Test the view_user_requests endpoint."""
  954. output = self.app.get('/user/somenonexistentuser/requests')
  955. self.assertEqual(output.status_code, 404)
  956. @patch(
  957. 'pagure.lib.git.update_git', MagicMock(return_value=True))
  958. @patch(
  959. 'pagure.lib.notify.send_email', MagicMock(return_value=True))
  960. def test_view_my_requests(self):
  961. """Test the view_user_requests endpoint. """
  962. # Create the PR
  963. tests.create_projects(self.session)
  964. repo = pagure.lib._get_project(self.session, 'test')
  965. req = pagure.lib.new_pull_request(
  966. session=self.session,
  967. repo_from=repo,
  968. branch_from='dev',
  969. repo_to=repo,
  970. branch_to='master',
  971. title='test pull-request #1',
  972. user='pingou',
  973. requestfolder=None,
  974. )
  975. self.session.commit()
  976. self.assertEqual(req.id, 1)
  977. self.assertEqual(req.title, 'test pull-request #1')
  978. output = self.app.get('/user/pingou/requests')
  979. self.assertEqual(output.status_code, 200)
  980. self.assertIn('test pull-request #1', output.data)
  981. self.assertEqual(
  982. output.data.count('<tr class="pr-status pr-status-open"'),
  983. 1)
  984. # Add a PR in a fork
  985. item = pagure.lib.model.Project(
  986. user_id=1, # pingou
  987. name='test_fork',
  988. description='test project #1',
  989. is_fork=True,
  990. parent_id=1,
  991. hook_token='aaabbbttt',
  992. )
  993. self.session.add(item)
  994. repo = pagure.lib._get_project(
  995. self.session, 'test_fork', user='pingou')
  996. req = pagure.lib.new_pull_request(
  997. session=self.session,
  998. repo_from=repo,
  999. branch_from='dev',
  1000. repo_to=repo,
  1001. branch_to='master',
  1002. title='test pull-request #2',
  1003. user='pingou',
  1004. requestfolder=None,
  1005. )
  1006. self.session.commit()
  1007. self.assertEqual(req.id, 1)
  1008. self.assertEqual(req.title, 'test pull-request #2')
  1009. output = self.app.get('/user/pingou/requests')
  1010. self.assertEqual(output.status_code, 200)
  1011. self.assertIn('test pull-request #1', output.data)
  1012. self.assertIn('test pull-request #2', output.data)
  1013. self.assertEqual(
  1014. output.data.count('<tr class="pr-status pr-status-open"'),
  1015. 2)
  1016. def test_view_my_issues_no_user(self):
  1017. """Test the view_user_issues endpoint with a missing user."""
  1018. output = self.app.get('/user/somenonexistentuser/issues')
  1019. self.assertEqual(output.status_code, 404)
  1020. @patch(
  1021. 'pagure.lib.git.update_git', MagicMock(return_value=True))
  1022. @patch(
  1023. 'pagure.lib.notify.send_email', MagicMock(return_value=True))
  1024. def test_view_my_issues(self):
  1025. """Test the view_user_issues endpoint when the user exists."""
  1026. # Create the issue
  1027. tests.create_projects(self.session)
  1028. repo = pagure.lib._get_project(self.session, 'test')
  1029. msg = pagure.lib.new_issue(
  1030. session=self.session,
  1031. repo=repo,
  1032. title='Test issue #1',
  1033. content='We should work on this for the second time',
  1034. user='pingou',
  1035. status='Open',
  1036. ticketfolder=None
  1037. )
  1038. self.session.commit()
  1039. self.assertEqual(msg.title, 'Test issue #1')
  1040. output = self.app.get('/user/pingou/issues')
  1041. self.assertEqual(output.status_code, 200)
  1042. self.assertIn('Test issue #1', output.data)
  1043. self.assertEqual(
  1044. output.data.count('<tr class="issue-status issue-status-open"'),
  1045. 1)
  1046. # Add an issue in a fork
  1047. item = pagure.lib.model.Project(
  1048. user_id=2, # foo
  1049. name='test_fork',
  1050. description='test project #1',
  1051. is_fork=True,
  1052. parent_id=1,
  1053. hook_token='aaabbbttt',
  1054. )
  1055. self.session.add(item)
  1056. repo = pagure.lib._get_project(self.session, 'test_fork', user='foo')
  1057. msg = pagure.lib.new_issue(
  1058. session=self.session,
  1059. repo=repo,
  1060. title='Test issue #2',
  1061. content='We should work on this for the second time',
  1062. user='pingou',
  1063. status='Open',
  1064. ticketfolder=None
  1065. )
  1066. self.session.commit()
  1067. self.assertEqual(msg.title, 'Test issue #2')
  1068. # Test the assigned issue table. Create issue then set the assignee
  1069. msg = pagure.lib.new_issue(
  1070. session=self.session,
  1071. repo=repo,
  1072. title='Test issue #3',
  1073. content='This issue created by foo, but assigned to pingou',
  1074. user='foo',
  1075. status='Open',
  1076. ticketfolder=None
  1077. )
  1078. self.session.commit()
  1079. self.assertEqual(msg.title, 'Test issue #3')
  1080. msg = pagure.lib.add_issue_assignee(
  1081. session=self.session,
  1082. issue=msg,
  1083. assignee='pingou',
  1084. user='foo',
  1085. ticketfolder=None)
  1086. self.session.commit()
  1087. self.assertEqual(msg, 'Issue assigned to pingou')
  1088. output = self.app.get('/user/pingou/issues')
  1089. self.assertEqual(output.status_code, 200)
  1090. self.assertIn('Test issue #1', output.data)
  1091. self.assertIn('Test issue #2', output.data)
  1092. self.assertIn('Test issue #3', output.data)
  1093. self.assertEqual(
  1094. output.data.count('<tr class="issue-status issue-status-open"'), 3)
  1095. def test_view_my_issues_tickets_turned_off(self):
  1096. """Test the view_user_issues endpoint when the user exists and
  1097. and ENABLE_TICKETS is False """
  1098. # Turn off the tickets instance wide
  1099. pagure.APP.config['ENABLE_TICKETS'] = False
  1100. output = self.app.get('/user/pingou/issues')
  1101. self.assertEqual(output.status_code, 404)
  1102. pagure.APP.config['ENABLE_TICKETS'] = True
  1103. @patch('pagure.ui.app.admin_session_timedout')
  1104. def test_add_user_token(self, ast):
  1105. """ Test the add_user_token endpoint. """
  1106. ast.return_value = False
  1107. self.test_new_project()
  1108. user = tests.FakeUser()
  1109. with tests.user_set(pagure.APP, user):
  1110. output = self.app.get('/settings/token/new/')
  1111. self.assertEqual(output.status_code, 404)
  1112. self.assertTrue('<h2>Page not found (404)</h2>' in output.data)
  1113. user.username = 'foo'
  1114. with tests.user_set(pagure.APP, user):
  1115. output = self.app.get('/settings/token/new')
  1116. self.assertEqual(output.status_code, 200)
  1117. self.assertIn(
  1118. '<div class="card-header">\n <strong>'
  1119. 'Create a new token</strong>\n', output.data)
  1120. self.assertIn(
  1121. '<input type="checkbox" name="acls" value="create_project">',
  1122. output.data)
  1123. csrf_token = output.data.split(
  1124. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1125. data = {
  1126. 'acls': ['create_project', 'fork_project']
  1127. }
  1128. # missing CSRF
  1129. output = self.app.post('/settings/token/new', data=data)
  1130. self.assertEqual(output.status_code, 200)
  1131. self.assertIn(
  1132. '<title>Create token - Pagure</title>', output.data)
  1133. self.assertIn(
  1134. '<div class="card-header">\n <strong>'
  1135. 'Create a new token</strong>\n', output.data)
  1136. self.assertIn(
  1137. '<input type="checkbox" name="acls" value="create_project">',
  1138. output.data)
  1139. data = {
  1140. 'acls': ['new_project'],
  1141. 'csrf_token': csrf_token
  1142. }
  1143. # Invalid ACLs
  1144. output = self.app.post('/settings/token/new', data=data)
  1145. self.assertEqual(output.status_code, 200)
  1146. self.assertIn(
  1147. '<title>Create token - Pagure</title>', output.data)
  1148. self.assertIn(
  1149. '<div class="card-header">\n <strong>'
  1150. 'Create a new token</strong>\n', output.data)
  1151. self.assertIn(
  1152. '<input type="checkbox" name="acls" value="create_project">',
  1153. output.data)
  1154. data = {
  1155. 'acls': ['create_project', 'fork_project'],
  1156. 'csrf_token': csrf_token
  1157. }
  1158. # All good
  1159. output = self.app.post(
  1160. '/settings/token/new', data=data, follow_redirects=True)
  1161. self.assertEqual(output.status_code, 200)
  1162. self.assertIn(
  1163. '<title>foo\'s settings - Pagure</title>', output.data)
  1164. self.assertIn(
  1165. '</button>\n Token created\n',
  1166. output.data)
  1167. self.assertEqual(
  1168. output.data.count(
  1169. '<span class="text-success btn-align"><strong>Valid'
  1170. '</strong> until: '), 1)
  1171. ast.return_value = True
  1172. output = self.app.get('/settings/token/new')
  1173. self.assertEqual(output.status_code, 302)
  1174. @patch('pagure.ui.app.admin_session_timedout')
  1175. def test_revoke_api_user_token(self, ast):
  1176. """ Test the revoke_api_user_token endpoint. """
  1177. ast.return_value = False
  1178. self.test_new_project()
  1179. user = tests.FakeUser()
  1180. with tests.user_set(pagure.APP, user):
  1181. # Token doesn't exist
  1182. output = self.app.post('/settings/token/revoke/foobar')
  1183. self.assertEqual(output.status_code, 404)
  1184. self.assertTrue('<h2>Page not found (404)</h2>' in output.data)
  1185. # Create the foobar API token but associated w/ the user 'foo'
  1186. item = pagure.lib.model.Token(
  1187. id='foobar',
  1188. user_id=2, # foo
  1189. expiration=datetime.datetime.utcnow() \
  1190. + datetime.timedelta(days=30)
  1191. )
  1192. self.session.add(item)
  1193. self.session.commit()
  1194. # Token not associated w/ this user
  1195. output = self.app.post('/settings/token/revoke/foobar')
  1196. self.assertEqual(output.status_code, 404)
  1197. self.assertTrue('<h2>Page not found (404)</h2>' in output.data)
  1198. user.username = 'foo'
  1199. with tests.user_set(pagure.APP, user):
  1200. # Missing CSRF token
  1201. output = self.app.post(
  1202. '/settings/token/revoke/foobar', follow_redirects=True)
  1203. self.assertEqual(output.status_code, 200)
  1204. self.assertIn(
  1205. "<title>foo's settings - Pagure</title>", output.data)
  1206. self.assertEqual(
  1207. output.data.count(
  1208. '<span class="text-success btn-align"><strong>Valid'
  1209. '</strong> until: '), 1)
  1210. csrf_token = output.data.split(
  1211. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  1212. data = {
  1213. 'csrf_token': csrf_token
  1214. }
  1215. # All good - token is deleted
  1216. output = self.app.post(
  1217. '/settings/token/revoke/foobar', data=data,
  1218. follow_redirects=True)
  1219. self.assertEqual(output.status_code, 200)
  1220. self.assertIn(
  1221. "<title>foo's settings - Pagure</title>", output.data)
  1222. self.assertEqual(
  1223. output.data.count(
  1224. '<span class="text-success btn-align"><strong>Valid'
  1225. '</strong> until: '), 0)
  1226. user = pagure.lib.get_user(self.session, key='foo')
  1227. self.assertEqual(len(user.tokens), 1)
  1228. expiration_dt = user.tokens[0].expiration
  1229. # Token was already deleted - no changes
  1230. output = self.app.post(
  1231. '/settings/token/revoke/foobar', data=data,
  1232. follow_redirects=True)
  1233. self.assertEqual(output.status_code, 200)
  1234. self.assertIn(
  1235. "<title>foo's settings - Pagure</title>", output.data)
  1236. self.assertEqual(
  1237. output.data.count(
  1238. '<span class="text-success btn-align"><strong>Valid'
  1239. '</strong> until: '), 0)
  1240. # Ensure the expiration date did not change
  1241. user = pagure.lib.get_user(self.session, key='foo')
  1242. self.assertEqual(len(user.tokens), 1)
  1243. self.assertEqual(
  1244. expiration_dt, user.tokens[0].expiration
  1245. )
  1246. ast.return_value = True
  1247. output = self.app.get('/settings/token/new')
  1248. self.assertEqual(output.status_code, 302)
  1249. if __name__ == '__main__':
  1250. unittest.main(verbosity=2)