test_pagure_flask_ui_app_userdash.py 16 KB

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