test_pagure_flask_ui_app_give_project.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 unittest
  10. import shutil
  11. import sys
  12. import tempfile
  13. import os
  14. from mock import patch, MagicMock
  15. sys.path.insert(0, os.path.join(os.path.dirname(
  16. os.path.abspath(__file__)), '..'))
  17. import pagure
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskGiveRepotests(tests.SimplePagureTest):
  21. """ Tests for give a project on pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskGiveRepotests, self).setUp()
  25. pagure.config.config['VIRUS_SCAN_ATTACHMENTS'] = False
  26. pagure.config.config['UPLOAD_FOLDER_URL'] = '/releases/'
  27. pagure.config.config['UPLOAD_FOLDER_PATH'] = os.path.join(
  28. self.path, 'releases')
  29. tests.create_projects(self.session)
  30. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  31. self._check_user(user='pingou')
  32. def _check_user(self, user='pingou'):
  33. self.session.commit()
  34. project = pagure.lib.get_authorized_project(
  35. self.session, project_name='test')
  36. self.assertEqual(project.user.user, user)
  37. def test_give_project_no_project(self):
  38. """ Test the give_project endpoint. """
  39. # No such project
  40. output = self.app.post('/test42/give')
  41. self.assertEqual(output.status_code, 404)
  42. def test_give_project_no_csrf(self):
  43. """ Test the give_project endpoint. """
  44. user = tests.FakeUser()
  45. user.username = 'pingou'
  46. with tests.user_set(self.app.application, user):
  47. self._check_user()
  48. # Missing CSRF
  49. data = {
  50. 'user': 'foo',
  51. }
  52. output = self.app.post(
  53. '/test/give', data=data, follow_redirects=True)
  54. self.assertEqual(output.status_code, 200)
  55. self.assertIn(
  56. '<title>Overview - test - Pagure</title>',
  57. output.data)
  58. self._check_user()
  59. def test_give_project_invalid_user(self):
  60. """ Test the give_project endpoint. """
  61. user = tests.FakeUser()
  62. user.username = 'pingou'
  63. with tests.user_set(self.app.application, user):
  64. csrf_token = self.get_csrf()
  65. self._check_user()
  66. # Invalid user
  67. data = {
  68. 'user': 'foobar',
  69. 'csrf_token': csrf_token,
  70. }
  71. output = self.app.post(
  72. '/test/give', data=data, follow_redirects=True)
  73. self.assertEqual(output.status_code, 404)
  74. self.assertIn(
  75. '<p>No such user foobar found</p>',
  76. output.data)
  77. self._check_user()
  78. def test_give_project_no_user(self):
  79. """ Test the give_project endpoint. """
  80. user = tests.FakeUser()
  81. user.username = 'pingou'
  82. with tests.user_set(self.app.application, user):
  83. csrf_token = self.get_csrf()
  84. self._check_user()
  85. # No user
  86. data = {
  87. 'csrf_token': csrf_token,
  88. }
  89. output = self.app.post(
  90. '/test/give', data=data, follow_redirects=True)
  91. self.assertEqual(output.status_code, 404)
  92. self.assertIn('<p>No user specified</p>', output.data)
  93. self._check_user()
  94. def test_give_project_not_owner(self):
  95. """ Test the give_project endpoint. """
  96. user = tests.FakeUser()
  97. user.username = 'foo'
  98. with tests.user_set(self.app.application, user):
  99. csrf_token = self.get_csrf()
  100. self._check_user()
  101. # User isn't the admin
  102. data = {
  103. 'user': 'foo',
  104. 'csrf_token': csrf_token,
  105. }
  106. output = self.app.post(
  107. '/test/give', data=data, follow_redirects=True)
  108. self.assertEqual(output.status_code, 403)
  109. self.assertIn(
  110. '<p>You are not allowed to change the settings for this '
  111. 'project</p>', output.data)
  112. self._check_user()
  113. def test_give_project_not_admin(self):
  114. """ Test the give_project endpoint. """
  115. user = tests.FakeUser()
  116. user.username = 'foo'
  117. with tests.user_set(self.app.application, user):
  118. csrf_token = self.get_csrf()
  119. self._check_user()
  120. # User isn't the admin
  121. data = {
  122. 'user': 'foo',
  123. 'csrf_token': csrf_token,
  124. }
  125. output = self.app.post(
  126. '/test/give', data=data, follow_redirects=True)
  127. self.assertEqual(output.status_code, 403)
  128. self.assertIn(
  129. '<p>You are not allowed to change the settings for this '
  130. 'project</p>', output.data)
  131. self._check_user()
  132. def test_give_project_not_owner_but_is_admin(self):
  133. """ Test the give_project endpoint. """
  134. project = pagure.lib.get_authorized_project(
  135. self.session, project_name='test')
  136. msg = pagure.lib.add_user_to_project(
  137. self.session,
  138. project=project,
  139. new_user='foo',
  140. user='pingou',
  141. access='admin')
  142. self.session.commit()
  143. self.assertEqual(msg, 'User added')
  144. user = tests.FakeUser()
  145. user.username = 'foo'
  146. with tests.user_set(self.app.application, user):
  147. csrf_token = self.get_csrf()
  148. self._check_user()
  149. # User isn't the owner
  150. data = {
  151. 'user': 'foo',
  152. 'csrf_token': csrf_token,
  153. }
  154. output = self.app.post(
  155. '/test/give', data=data, follow_redirects=True)
  156. self.assertEqual(output.status_code, 403)
  157. self.assertIn(
  158. '<p>You are not allowed to give this project</p>',
  159. output.data)
  160. self._check_user()
  161. @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': 'foo'})
  162. @patch('pagure.lib.git.generate_gitolite_acls', MagicMock())
  163. def test_give_project_not_owner_but_admin(self):
  164. """ Test the give_project endpoint.
  165. Test giving a project when the person giving the project is a pagure
  166. admin (instance wide admin) but not a project admin.
  167. """
  168. user = tests.FakeUser()
  169. user.username = 'foo'
  170. user.cla_done = True
  171. user.groups = ['foo']
  172. with tests.user_set(self.app.application, user):
  173. csrf_token = self.get_csrf()
  174. self._check_user()
  175. # User isn't the owner but is an instance admin
  176. data = {
  177. 'user': 'foo',
  178. 'csrf_token': csrf_token,
  179. }
  180. output = self.app.post(
  181. '/test/give', data=data, follow_redirects=True)
  182. self.assertEqual(output.status_code, 200)
  183. self.assertIn(
  184. '</button>\n The project has been '
  185. 'transferred to foo\n',
  186. output.data)
  187. self._check_user('foo')
  188. @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': 'foo'})
  189. @patch('pagure.lib.git.generate_gitolite_acls', MagicMock())
  190. def test_give_project(self):
  191. """ Test the give_project endpoint. """
  192. user = tests.FakeUser()
  193. user.username = 'pingou'
  194. with tests.user_set(self.app.application, user):
  195. csrf_token = self.get_csrf()
  196. self._check_user()
  197. # All good
  198. data = {
  199. 'user': 'foo',
  200. 'csrf_token': csrf_token,
  201. }
  202. output = self.app.post(
  203. '/test/give', data=data, follow_redirects=True)
  204. self.assertEqual(output.status_code, 200)
  205. self.assertIn(
  206. '</button>\n The project has been '
  207. 'transferred to foo\n',
  208. output.data)
  209. self._check_user('foo')
  210. # Make sure that the user giving the project is still an admin
  211. project = pagure.lib.get_authorized_project(
  212. self.session, project_name='test')
  213. self.assertEqual(len(project.users), 1)
  214. self.assertEqual(project.users[0].user, 'pingou')
  215. @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': 'foo'})
  216. @patch('pagure.lib.git.generate_gitolite_acls', MagicMock())
  217. def test_give_project_already_user(self):
  218. """ Test the give_project endpoint when the new main_admin is already
  219. a committer on the project. """
  220. project = pagure.lib._get_project(self.session, 'test')
  221. pagure.lib.add_user_to_project(
  222. self.session, project,
  223. new_user='foo',
  224. user='pingou',
  225. access='commit'
  226. )
  227. self.session.commit()
  228. user = tests.FakeUser()
  229. user.username = 'pingou'
  230. with tests.user_set(self.app.application, user):
  231. csrf_token = self.get_csrf()
  232. self._check_user()
  233. # All good
  234. data = {
  235. 'user': 'foo',
  236. 'csrf_token': csrf_token,
  237. }
  238. output = self.app.post(
  239. '/test/give', data=data, follow_redirects=True)
  240. self.assertEqual(output.status_code, 200)
  241. self.assertIn(
  242. '</button>\n The project has been '
  243. 'transferred to foo\n',
  244. output.data)
  245. self._check_user('foo')
  246. # Make sure that the user giving the project is still an admin
  247. project = pagure.lib.get_authorized_project(
  248. self.session, project_name='test')
  249. self.assertEqual(len(project.users), 1)
  250. self.assertEqual(project.users[0].user, 'pingou')
  251. @patch.dict('pagure.config.config', {'REQUIRED_GROUPS': {'*': ['packager']}})
  252. @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': 'foo'})
  253. @patch('pagure.lib.git.generate_gitolite_acls', MagicMock())
  254. def test_give_project_not_in_required_group(self):
  255. """ Test the give_project endpoint. """
  256. user = tests.FakeUser()
  257. user.username = 'pingou'
  258. with tests.user_set(self.app.application, user):
  259. csrf_token = self.get_csrf()
  260. self._check_user()
  261. # User not a packager
  262. data = {
  263. 'user': 'foo',
  264. 'csrf_token': csrf_token,
  265. }
  266. output = self.app.post(
  267. '/test/give', data=data, follow_redirects=True)
  268. self.assertEqual(output.status_code, 200)
  269. self.assertIn(
  270. '</button>\n '
  271. 'This user must be in one of the following groups to be '
  272. 'allowed to be added to this project: packager'
  273. '\n </div>\n',
  274. output.get_data(as_text=True))
  275. self._check_user(user='pingou')
  276. @patch.dict('pagure.config.config', {'REQUIRED_GROUPS': {'*': ['packager']}})
  277. @patch.dict('pagure.config.config', {'PAGURE_ADMIN_USERS': 'foo'})
  278. @patch('pagure.lib.git.generate_gitolite_acls', MagicMock())
  279. def test_give_project_in_required_group(self):
  280. """ Test the give_project endpoint. """
  281. # Create the packager group
  282. msg = pagure.lib.add_group(
  283. self.session,
  284. group_name='packager',
  285. display_name='packager group',
  286. description=None,
  287. group_type='user',
  288. user='pingou',
  289. is_admin=False,
  290. blacklist=[],
  291. )
  292. self.session.commit()
  293. self.assertEqual(msg, 'User `pingou` added to the group `packager`.')
  294. # Add foo to the packager group
  295. group = pagure.lib.search_groups(self.session, group_name='packager')
  296. msg = pagure.lib.add_user_to_group(
  297. self.session,
  298. username='foo',
  299. group=group,
  300. user='pingou',
  301. is_admin=False,
  302. )
  303. self.session.commit()
  304. self.assertEqual(msg, 'User `foo` added to the group `packager`.')
  305. # pingou transferts test to foo
  306. user = tests.FakeUser()
  307. user.username = 'pingou'
  308. with tests.user_set(self.app.application, user):
  309. csrf_token = self.get_csrf()
  310. self._check_user()
  311. # User not a packager
  312. data = {
  313. 'user': 'foo',
  314. 'csrf_token': csrf_token,
  315. }
  316. output = self.app.post(
  317. '/test/give', data=data, follow_redirects=True)
  318. self.assertEqual(output.status_code, 200)
  319. self.assertIn(
  320. '</button>\n '
  321. 'The project has been transferred to foo'
  322. '\n </div>\n',
  323. output.get_data(as_text=True))
  324. self._check_user('foo')
  325. # Make sure that the user giving the project is still an admin
  326. project = pagure.lib.get_authorized_project(
  327. self.session, project_name='test')
  328. self.assertEqual(len(project.users), 1)
  329. self.assertEqual(project.users[0].user, 'pingou')
  330. if __name__ == '__main__':
  331. unittest.main(verbosity=2)