test_pagure_flask_ui_app_index.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 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 PagureFlaskAppIndextests(tests.Modeltests):
  23. """ Tests for the index page of flask app controller of pagure """
  24. @patch.dict('pagure.config.config', {'HTML_TITLE': 'Pagure HTML title set'})
  25. def test_index_html_title(self):
  26. """ Test the index endpoint with a set html title. """
  27. output = self.app.get('/')
  28. self.assertEqual(output.status_code, 200)
  29. self.assertIn(
  30. '<title>Home - Pagure HTML title set</title>',
  31. output.data)
  32. def test_index_logged_out(self):
  33. """ Test the index endpoint when logged out. """
  34. output = self.app.get('/')
  35. self.assertEqual(output.status_code, 200)
  36. self.assertIn('<title>Home - Pagure</title>', output.data)
  37. self.assertIn(
  38. '<h2 class="m-b-1">All Projects '
  39. '<span class="label label-default">0</span></h2>', output.data)
  40. tests.create_projects(self.session)
  41. output = self.app.get('/?page=abc')
  42. self.assertEqual(output.status_code, 200)
  43. self.assertIn(
  44. '<h2 class="m-b-1">All Projects '
  45. '<span class="label label-default">3</span></h2>', output.data)
  46. def test_index_logged_in(self):
  47. """ Test the index endpoint when logged in. """
  48. tests.create_projects(self.session)
  49. # Add a 3rd project with a long description
  50. item = pagure.lib.model.Project(
  51. user_id=2, # foo
  52. name='test3',
  53. description='test project #3 with a very long description',
  54. hook_token='aaabbbeeefff',
  55. )
  56. self.session.add(item)
  57. self.session.commit()
  58. user = tests.FakeUser(username='foo')
  59. with tests.user_set(self.app.application, user):
  60. output = self.app.get('/?repopage=abc&forkpage=def')
  61. self.assertEqual(output.status_code, 200)
  62. self.assertIn(
  63. 'Projects <span class="label label-default">1</span>',
  64. output.data)
  65. self.assertIn(
  66. 'Forks <span class="label label-default">0</span>',
  67. output.data)
  68. self.assertEqual(
  69. output.data.count('<p>No group found</p>'), 1)
  70. self.assertEqual(
  71. output.data.count('<div class="card-header">'), 6)
  72. def test_index_commit_access_while_admin(self):
  73. """ Test the index endpoint filter for commit access only when user
  74. is an admin. """
  75. tests.create_projects(self.session)
  76. # Add a 3rd project just for foo
  77. item = pagure.lib.model.Project(
  78. user_id=2, # foo
  79. name='test3',
  80. description='test project #3 with a very long description',
  81. hook_token='aaabbbeeefff',
  82. )
  83. self.session.add(item)
  84. self.session.commit()
  85. user = tests.FakeUser(username='foo')
  86. with tests.user_set(self.app.application, user):
  87. # Before
  88. output = self.app.get('/?acl=commit')
  89. self.assertEqual(output.status_code, 200)
  90. self.assertIn(
  91. 'Projects <span class="label label-default">1</span>',
  92. output.data)
  93. self.assertIn(
  94. 'Forks <span class="label label-default">0</span>',
  95. output.data)
  96. self.assertEqual(
  97. output.data.count('<p>No group found</p>'), 1)
  98. self.assertEqual(
  99. output.data.count('<div class="card-header">'), 6)
  100. # Add foo to test with admin level
  101. project = pagure.lib._get_project(self.session, 'test')
  102. msg = pagure.lib.add_user_to_project(
  103. self.session,
  104. project=project,
  105. new_user='foo',
  106. user='pingou',
  107. access='admin')
  108. self.session.commit()
  109. self.assertEqual(msg, 'User added')
  110. # After
  111. output = self.app.get('/?acl=commit')
  112. self.assertEqual(output.status_code, 200)
  113. self.assertIn(
  114. 'Projects <span class="label label-default">2</span>',
  115. output.data)
  116. self.assertIn(
  117. 'Forks <span class="label label-default">0</span>',
  118. output.data)
  119. self.assertEqual(
  120. output.data.count('<p>No group found</p>'), 1)
  121. self.assertEqual(
  122. output.data.count('<div class="card-header">'), 6)
  123. def test_index_commit_access_while_commit(self):
  124. """ Test the index endpoint filter for commit access only when user
  125. is an committer. """
  126. tests.create_projects(self.session)
  127. # Add a 3rd project just for foo
  128. item = pagure.lib.model.Project(
  129. user_id=2, # foo
  130. name='test3',
  131. description='test project #3 with a very long description',
  132. hook_token='aaabbbeeefff',
  133. )
  134. self.session.add(item)
  135. self.session.commit()
  136. user = tests.FakeUser(username='foo')
  137. with tests.user_set(self.app.application, user):
  138. # Before
  139. output = self.app.get('/?acl=commit')
  140. self.assertEqual(output.status_code, 200)
  141. self.assertIn(
  142. 'Projects <span class="label label-default">1</span>',
  143. output.data)
  144. self.assertIn(
  145. 'Forks <span class="label label-default">0</span>',
  146. output.data)
  147. self.assertEqual(
  148. output.data.count('<p>No group found</p>'), 1)
  149. self.assertEqual(
  150. output.data.count('<div class="card-header">'), 6)
  151. # Add foo to test with commit level
  152. project = pagure.lib._get_project(self.session, 'test')
  153. msg = pagure.lib.add_user_to_project(
  154. self.session,
  155. project=project,
  156. new_user='foo',
  157. user='pingou',
  158. access='commit')
  159. self.session.commit()
  160. self.assertEqual(msg, 'User added')
  161. # After
  162. output = self.app.get('/?acl=commit')
  163. self.assertEqual(output.status_code, 200)
  164. self.assertIn(
  165. 'Projects <span class="label label-default">2</span>',
  166. output.data)
  167. self.assertIn(
  168. 'Forks <span class="label label-default">0</span>',
  169. output.data)
  170. self.assertEqual(
  171. output.data.count('<p>No group found</p>'), 1)
  172. self.assertEqual(
  173. output.data.count('<div class="card-header">'), 6)
  174. def test_index_commit_access_while_ticket(self):
  175. """ Test the index endpoint filter for commit access only when user
  176. is has ticket access. """
  177. tests.create_projects(self.session)
  178. # Add a 3rd project just for foo
  179. item = pagure.lib.model.Project(
  180. user_id=2, # foo
  181. name='test3',
  182. description='test project #3 with a very long description',
  183. hook_token='aaabbbeeefff',
  184. )
  185. self.session.add(item)
  186. self.session.commit()
  187. user = tests.FakeUser(username='foo')
  188. with tests.user_set(self.app.application, user):
  189. # Before
  190. output = self.app.get('/?acl=ticket')
  191. self.assertEqual(output.status_code, 200)
  192. self.assertIn(
  193. 'Projects <span class="label label-default">1</span>',
  194. output.data)
  195. self.assertIn(
  196. 'Forks <span class="label label-default">0</span>',
  197. output.data)
  198. self.assertEqual(
  199. output.data.count('<p>No group found</p>'), 1)
  200. self.assertEqual(
  201. output.data.count('<div class="card-header">'), 6)
  202. # Add foo to test with ticket level
  203. project = pagure.lib._get_project(self.session, 'test')
  204. msg = pagure.lib.add_user_to_project(
  205. self.session,
  206. project=project,
  207. new_user='foo',
  208. user='pingou',
  209. access='ticket')
  210. self.session.commit()
  211. self.assertEqual(msg, 'User added')
  212. # After - projects with ticket access aren't shown
  213. output = self.app.get('/?acl=ticket')
  214. self.assertEqual(output.status_code, 200)
  215. self.assertIn(
  216. 'Projects <span class="label label-default">1</span>',
  217. output.data)
  218. self.assertIn(
  219. 'Forks <span class="label label-default">0</span>',
  220. output.data)
  221. self.assertEqual(
  222. output.data.count('<p>No group found</p>'), 1)
  223. self.assertEqual(
  224. output.data.count('<div class="card-header">'), 6)
  225. def test_index_admin_access_while_admin(self):
  226. """ Test the index endpoint filter for admin access only when user
  227. is an admin. """
  228. tests.create_projects(self.session)
  229. # Add a 3rd project just for foo
  230. item = pagure.lib.model.Project(
  231. user_id=2, # foo
  232. name='test3',
  233. description='test project #3 with a very long description',
  234. hook_token='aaabbbeeefff',
  235. )
  236. self.session.add(item)
  237. self.session.commit()
  238. user = tests.FakeUser(username='foo')
  239. with tests.user_set(self.app.application, user):
  240. # Before
  241. output = self.app.get('/?acl=admin')
  242. self.assertEqual(output.status_code, 200)
  243. self.assertIn(
  244. 'Projects <span class="label label-default">1</span>',
  245. output.data)
  246. self.assertIn(
  247. 'Forks <span class="label label-default">0</span>',
  248. output.data)
  249. self.assertEqual(
  250. output.data.count('<p>No group found</p>'), 1)
  251. self.assertEqual(
  252. output.data.count('<div class="card-header">'), 6)
  253. # Add foo to test with commit level
  254. project = pagure.lib._get_project(self.session, 'test')
  255. msg = pagure.lib.add_user_to_project(
  256. self.session,
  257. project=project,
  258. new_user='foo',
  259. user='pingou',
  260. access='admin')
  261. self.session.commit()
  262. self.assertEqual(msg, 'User added')
  263. # After
  264. output = self.app.get('/?acl=admin')
  265. self.assertEqual(output.status_code, 200)
  266. self.assertIn(
  267. 'Projects <span class="label label-default">2</span>',
  268. output.data)
  269. self.assertIn(
  270. 'Forks <span class="label label-default">0</span>',
  271. output.data)
  272. self.assertEqual(
  273. output.data.count('<p>No group found</p>'), 1)
  274. self.assertEqual(
  275. output.data.count('<div class="card-header">'), 6)
  276. def test_index_admin_access_while_commit(self):
  277. """ Test the index endpoint filter for admin access only when user
  278. is an committer. """
  279. tests.create_projects(self.session)
  280. # Add a 3rd project just for foo
  281. item = pagure.lib.model.Project(
  282. user_id=2, # foo
  283. name='test3',
  284. description='test project #3 with a very long description',
  285. hook_token='aaabbbeeefff',
  286. )
  287. self.session.add(item)
  288. self.session.commit()
  289. user = tests.FakeUser(username='foo')
  290. with tests.user_set(self.app.application, user):
  291. # Before
  292. output = self.app.get('/?acl=admin')
  293. self.assertEqual(output.status_code, 200)
  294. self.assertIn(
  295. 'Projects <span class="label label-default">1</span>',
  296. output.data)
  297. self.assertIn(
  298. 'Forks <span class="label label-default">0</span>',
  299. output.data)
  300. self.assertEqual(
  301. output.data.count('<p>No group found</p>'), 1)
  302. self.assertEqual(
  303. output.data.count('<div class="card-header">'), 6)
  304. # Add foo to test with commit level
  305. project = pagure.lib._get_project(self.session, 'test')
  306. msg = pagure.lib.add_user_to_project(
  307. self.session,
  308. project=project,
  309. new_user='foo',
  310. user='pingou',
  311. access='commit')
  312. self.session.commit()
  313. self.assertEqual(msg, 'User added')
  314. # After
  315. output = self.app.get('/?acl=admin')
  316. self.assertEqual(output.status_code, 200)
  317. self.assertIn(
  318. 'Projects <span class="label label-default">1</span>',
  319. output.data)
  320. self.assertIn(
  321. 'Forks <span class="label label-default">0</span>',
  322. output.data)
  323. self.assertEqual(
  324. output.data.count('<p>No group found</p>'), 1)
  325. self.assertEqual(
  326. output.data.count('<div class="card-header">'), 6)
  327. def test_index_main_admin_access_while_commit(self):
  328. """ Test the index endpoint filter for main admin access only when
  329. user is an committer. """
  330. tests.create_projects(self.session)
  331. # Add a 3rd project just for foo
  332. item = pagure.lib.model.Project(
  333. user_id=2, # foo
  334. name='test3',
  335. description='test project #3 with a very long description',
  336. hook_token='aaabbbeeefff',
  337. )
  338. self.session.add(item)
  339. self.session.commit()
  340. user = tests.FakeUser(username='foo')
  341. with tests.user_set(self.app.application, user):
  342. # Before
  343. output = self.app.get('/?acl=main admin')
  344. self.assertEqual(output.status_code, 200)
  345. self.assertIn(
  346. 'Projects <span class="label label-default">1</span>',
  347. output.data)
  348. self.assertIn(
  349. 'Forks <span class="label label-default">0</span>',
  350. output.data)
  351. self.assertEqual(
  352. output.data.count('<p>No group found</p>'), 1)
  353. self.assertEqual(
  354. output.data.count('<div class="card-header">'), 6)
  355. # Add foo to test with commit level
  356. project = pagure.lib._get_project(self.session, 'test')
  357. msg = pagure.lib.add_user_to_project(
  358. self.session,
  359. project=project,
  360. new_user='foo',
  361. user='pingou',
  362. access='commit')
  363. self.session.commit()
  364. self.assertEqual(msg, 'User added')
  365. # After
  366. output = self.app.get('/?acl=main admin')
  367. self.assertEqual(output.status_code, 200)
  368. self.assertIn(
  369. 'Projects <span class="label label-default">1</span>',
  370. output.data)
  371. self.assertIn(
  372. 'Forks <span class="label label-default">0</span>',
  373. output.data)
  374. self.assertEqual(
  375. output.data.count('<p>No group found</p>'), 1)
  376. self.assertEqual(
  377. output.data.count('<div class="card-header">'), 6)
  378. def test_index_fork_without_parent(self):
  379. """ Test the index view: forks should display either their parent
  380. project or mention that it was deleted. """
  381. tests.create_projects(self.session)
  382. # Add a 3rd project just for foo
  383. item = pagure.lib.model.Project(
  384. user_id=2, # foo
  385. name='test3',
  386. description='test project #3',
  387. hook_token='aaabbbeeefff',
  388. is_fork=True,
  389. parent_id=1,
  390. )
  391. self.session.add(item)
  392. self.session.commit()
  393. user = tests.FakeUser(username='foo')
  394. with tests.user_set(self.app.application, user):
  395. # Before
  396. output = self.app.get('/')
  397. self.assertEqual(output.status_code, 200)
  398. self.assertIn(
  399. 'My Forks <span class="label label-default">1</span>',
  400. output.data)
  401. segment = output.data.split('My Forks')[1].split('My Groups')[0]
  402. self.assertRegexpMatches(
  403. segment,
  404. r'foo/test3(\s*<[^>]+?>\s*)*?forked from(\s*<[^>]+?>\s*)*?test'
  405. )
  406. # Delete the parent (or fake it)
  407. proj = pagure.lib._get_project(self.session, 'test3', user='foo')
  408. proj.parent_id = None
  409. self.session.add(proj)
  410. self.session.commit()
  411. # Check page again
  412. output = self.app.get('/')
  413. self.assertEqual(output.status_code, 200)
  414. self.assertIn(
  415. 'My Forks <span class="label label-default">1</span>',
  416. output.data)
  417. segment = output.data.split('My Forks')[1].split('My Groups')[0]
  418. self.assertRegexpMatches(
  419. segment,
  420. r'foo/test3(\s*<[^>]+?>\s*)*?forked from a deleted repository'
  421. )
  422. if __name__ == '__main__':
  423. unittest.main(verbosity=2)