test_pagure_flask_ui_app_give_project.py 13 KB

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