test_pagure_flask_ui_app_userdash.py 14 KB

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