test_pagure_flask_ui_app_give_project.py 13 KB

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