test_pagure_flask_ui_app_userdash.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. Ryan Lerch <rlerch@redhat.com>
  7. """
  8. from __future__ import unicode_literals
  9. __requires__ = ['SQLAlchemy >= 0.8']
  10. import pkg_resources
  11. import datetime
  12. import unittest
  13. import shutil
  14. import sys
  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 PagureFlaskAppUserdashTests(tests.Modeltests):
  25. """ Tests for the index page of flask app controller of pagure """
  26. def test_index_commit_access_while_admin(self):
  27. """ Test the index endpoint filter for commit access only when user
  28. is an admin. """
  29. tests.create_projects(self.session)
  30. # Add a 3rd project just for foo
  31. item = pagure.lib.model.Project(
  32. user_id=2, # foo
  33. name='test3',
  34. description='test project #3 with a very long description',
  35. hook_token='aaabbbeeefff',
  36. )
  37. self.session.add(item)
  38. self.session.commit()
  39. user = tests.FakeUser(username='foo')
  40. with tests.user_set(self.app.application, user):
  41. # Before
  42. output = self.app.get('/dashboard/projects?acl=commit')
  43. self.assertEqual(output.status_code, 200)
  44. output_text = output.get_data(as_text=True)
  45. self.assertIn(
  46. '<span class="btn btn-outline-secondary disabled opacity-100'
  47. ' border-0 ml-auto font-weight-bold">1 Projects</span>',
  48. output_text)
  49. self.assertIn(
  50. '<div class="text-center">No Projects match this filter</div>',
  51. output_text)
  52. # Add foo to test with admin level
  53. project = pagure.lib.query._get_project(self.session, 'test')
  54. msg = pagure.lib.query.add_user_to_project(
  55. self.session,
  56. project=project,
  57. new_user='foo',
  58. user='pingou',
  59. access='admin')
  60. self.session.commit()
  61. self.assertEqual(msg, 'User added')
  62. # After
  63. self.assertIn(
  64. '<span class="btn btn-outline-secondary disabled opacity-100'
  65. ' border-0 ml-auto font-weight-bold">1 Projects</span>',
  66. output_text)
  67. self.assertIn(
  68. '<div class="text-center">No Projects match this filter</div>',
  69. output_text)
  70. def test_index_commit_access_while_commit(self):
  71. """ Test the index endpoint filter for commit access only when user
  72. is an committer. """
  73. tests.create_projects(self.session)
  74. # Add a 3rd project just for foo
  75. item = pagure.lib.model.Project(
  76. user_id=2, # foo
  77. name='test3',
  78. description='test project #3 with a very long description',
  79. hook_token='aaabbbeeefff',
  80. )
  81. self.session.add(item)
  82. self.session.commit()
  83. user = tests.FakeUser(username='foo')
  84. with tests.user_set(self.app.application, user):
  85. # Before
  86. output = self.app.get('/dashboard/projects?acl=commit')
  87. self.assertEqual(output.status_code, 200)
  88. output_text = output.get_data(as_text=True)
  89. self.assertIn(
  90. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  91. ' <span class="btn btn-outline-secondary disabled'
  92. ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
  93. output_text)
  94. # Add foo to test with commit level
  95. project = pagure.lib.query._get_project(self.session, 'test')
  96. msg = pagure.lib.query.add_user_to_project(
  97. self.session,
  98. project=project,
  99. new_user='foo',
  100. user='pingou',
  101. access='commit')
  102. self.session.commit()
  103. self.assertEqual(msg, 'User added')
  104. # After
  105. output = self.app.get('/dashboard/projects?acl=commit')
  106. self.assertEqual(output.status_code, 200)
  107. output_text = output.get_data(as_text=True)
  108. self.assertIn(
  109. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  110. ' <span class="btn btn-outline-secondary disabled'
  111. ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
  112. output_text)
  113. def test_index_commit_access_while_ticket(self):
  114. """ Test the index endpoint filter for commit access only when user
  115. is has ticket access. """
  116. tests.create_projects(self.session)
  117. # Add a 3rd project just for foo
  118. item = pagure.lib.model.Project(
  119. user_id=2, # foo
  120. name='test3',
  121. description='test project #3 with a very long description',
  122. hook_token='aaabbbeeefff',
  123. )
  124. self.session.add(item)
  125. self.session.commit()
  126. user = tests.FakeUser(username='foo')
  127. with tests.user_set(self.app.application, user):
  128. # Before
  129. output = self.app.get('/dashboard/projects?acl=ticket')
  130. self.assertEqual(output.status_code, 200)
  131. output_text = output.get_data(as_text=True)
  132. self.assertIn(
  133. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  134. ' <span class="btn btn-outline-secondary disabled'
  135. ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
  136. output_text)
  137. # Add foo to test with ticket level
  138. project = pagure.lib.query._get_project(self.session, 'test')
  139. msg = pagure.lib.query.add_user_to_project(
  140. self.session,
  141. project=project,
  142. new_user='foo',
  143. user='pingou',
  144. access='ticket')
  145. self.session.commit()
  146. self.assertEqual(msg, 'User added')
  147. # After - projects with ticket access aren't shown
  148. output = self.app.get('/dashboard/projects?acl=ticket')
  149. self.assertEqual(output.status_code, 200)
  150. output_text = output.get_data(as_text=True)
  151. self.assertIn(
  152. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  153. ' <span class="btn btn-outline-secondary disabled'
  154. ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
  155. output_text)
  156. def test_index_admin_access_while_admin(self):
  157. """ Test the index endpoint filter for admin access only when user
  158. is an admin. """
  159. tests.create_projects(self.session)
  160. # Add a 3rd project just for foo
  161. item = pagure.lib.model.Project(
  162. user_id=2, # foo
  163. name='test3',
  164. description='test project #3 with a very long description',
  165. hook_token='aaabbbeeefff',
  166. )
  167. self.session.add(item)
  168. self.session.commit()
  169. user = tests.FakeUser(username='foo')
  170. with tests.user_set(self.app.application, user):
  171. # Before
  172. output = self.app.get('/dashboard/projects?acl=admin')
  173. self.assertEqual(output.status_code, 200)
  174. output_text = output.get_data(as_text=True)
  175. self.assertIn(
  176. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  177. ' <span class="btn btn-outline-secondary disabled'
  178. ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
  179. output_text)
  180. # Add foo to test with admin level
  181. project = pagure.lib.query._get_project(self.session, 'test')
  182. msg = pagure.lib.query.add_user_to_project(
  183. self.session,
  184. project=project,
  185. new_user='foo',
  186. user='pingou',
  187. access='admin')
  188. self.session.commit()
  189. self.assertEqual(msg, 'User added')
  190. # After
  191. output = self.app.get('/dashboard/projects?acl=admin')
  192. self.assertEqual(output.status_code, 200)
  193. output_text = output.get_data(as_text=True)
  194. self.assertIn(
  195. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  196. ' <span class="btn btn-outline-secondary disabled'
  197. ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
  198. output_text)
  199. def test_index_admin_access_while_commit(self):
  200. """ Test the index endpoint filter for admin access only when user
  201. is an committer. """
  202. tests.create_projects(self.session)
  203. # Add a 3rd project just for foo
  204. item = pagure.lib.model.Project(
  205. user_id=2, # foo
  206. name='test3',
  207. description='test project #3 with a very long description',
  208. hook_token='aaabbbeeefff',
  209. )
  210. self.session.add(item)
  211. self.session.commit()
  212. user = tests.FakeUser(username='foo')
  213. with tests.user_set(self.app.application, user):
  214. # Before
  215. output = self.app.get('/dashboard/projects?acl=commit')
  216. self.assertEqual(output.status_code, 200)
  217. output_text = output.get_data(as_text=True)
  218. self.assertIn(
  219. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  220. ' <span class="btn btn-outline-secondary disabled'
  221. ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
  222. output_text)
  223. # Add foo to test with commit level
  224. project = pagure.lib.query._get_project(self.session, 'test')
  225. msg = pagure.lib.query.add_user_to_project(
  226. self.session,
  227. project=project,
  228. new_user='foo',
  229. user='pingou',
  230. access='commit')
  231. self.session.commit()
  232. self.assertEqual(msg, 'User added')
  233. # After
  234. output = self.app.get('/dashboard/projects?acl=commit')
  235. self.assertEqual(output.status_code, 200)
  236. output_text = output.get_data(as_text=True)
  237. # The total number no longer changes
  238. self.assertIn(
  239. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  240. ' <span class="btn btn-outline-secondary disabled'
  241. ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
  242. output_text)
  243. def test_index_main_admin_access_while_commit(self):
  244. """ Test the index endpoint filter for main admin access only when
  245. user is an committer. """
  246. tests.create_projects(self.session)
  247. # Add a 3rd project just for foo
  248. item = pagure.lib.model.Project(
  249. user_id=2, # foo
  250. name='test3',
  251. description='test project #3 with a very long description',
  252. hook_token='aaabbbeeefff',
  253. )
  254. self.session.add(item)
  255. self.session.commit()
  256. user = tests.FakeUser(username='foo')
  257. with tests.user_set(self.app.application, user):
  258. # Before
  259. output = self.app.get('/dashboard/projects?acl=main admin')
  260. self.assertEqual(output.status_code, 200)
  261. output_text = output.get_data(as_text=True)
  262. self.assertIn(
  263. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  264. ' <span class="btn btn-outline-secondary disabled'
  265. ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
  266. output_text)
  267. # Add foo to test with commit level
  268. project = pagure.lib.query._get_project(self.session, 'test')
  269. msg = pagure.lib.query.add_user_to_project(
  270. self.session,
  271. project=project,
  272. new_user='foo',
  273. user='pingou',
  274. access='commit')
  275. self.session.commit()
  276. self.assertEqual(msg, 'User added')
  277. # After
  278. output = self.app.get('/dashboard/projects?acl=main admin')
  279. self.assertEqual(output.status_code, 200)
  280. output_text = output.get_data(as_text=True)
  281. self.assertIn(
  282. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  283. ' <span class="btn btn-outline-secondary disabled'
  284. ' opacity-100 border-0 ml-auto font-weight-bold">2 Projects</span>\n',
  285. output_text)
  286. @patch.dict('pagure.config.config', {'PRIVATE_PROJECTS': True})
  287. def test_index_logged_in_private_project(self):
  288. """ Test the index endpoint when logged in with a private project. """
  289. tests.create_projects(self.session)
  290. # Add a 3rd project with a long description
  291. item = pagure.lib.model.Project(
  292. user_id=2, # foo
  293. name='test3',
  294. description='test project #3 with a very long description',
  295. hook_token='aaabbbeeefff',
  296. private=True,
  297. )
  298. self.session.add(item)
  299. self.session.commit()
  300. user = tests.FakeUser(username='foo')
  301. with tests.user_set(self.app.application, user):
  302. output = self.app.get('/dashboard/projects')
  303. self.assertEqual(output.status_code, 200)
  304. output_text = output.get_data(as_text=True)
  305. self.assertIn(
  306. '<h4 class="font-weight-bold mb-0">My Projects</h4>\n'
  307. ' <span class="btn btn-outline-secondary disabled'
  308. ' opacity-100 border-0 ml-auto font-weight-bold">1 Projects</span>\n',
  309. output_text)
  310. self.assertIn(
  311. '<span title="Private project" class="text-danger '
  312. 'fa fa-fw fa-lock"></span>',
  313. output_text)
  314. self.assertEqual(output_text.count('title="Private project"'), 1)
  315. if __name__ == '__main__':
  316. unittest.main(verbosity=2)