test_pagure_flask_ui_repo_delete_project.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 sys
  11. import os
  12. from mock import patch, MagicMock
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.lib.query
  16. import tests
  17. class PagureFlaskDeleteRepotests(tests.Modeltests):
  18. """ Tests for deleting a project in pagure """
  19. def setUp(self):
  20. """ Set up the environnment, ran before every tests. """
  21. super(PagureFlaskDeleteRepotests, self).setUp()
  22. # Create some projects
  23. tests.create_projects(self.session)
  24. tests.create_projects_git(os.path.join(self.path, 'repos'))
  25. self.session.commit()
  26. # Create all the git repos
  27. tests.create_projects_git(os.path.join(self.path, 'repos'))
  28. tests.create_projects_git(os.path.join(self.path, 'repos', 'docs'))
  29. tests.create_projects_git(
  30. os.path.join(self.path, 'repos', 'tickets'), bare=True)
  31. tests.create_projects_git(
  32. os.path.join(self.path, 'repos', 'requests'), bare=True)
  33. project = pagure.lib.query.get_authorized_project(
  34. self.session, project_name='test')
  35. self.assertIsNotNone(project)
  36. # Ensure the project isn't read-only
  37. project.read_only = False
  38. self.session.add(project)
  39. self.session.commit()
  40. # Create a fork
  41. task = pagure.lib.query.fork_project(
  42. session=self.session,
  43. user='pingou',
  44. repo=project,
  45. )
  46. task.get()
  47. # Ensure everything was correctly created
  48. projects = pagure.lib.query.search_projects(self.session)
  49. self.assertEqual(len(projects), 4)
  50. @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
  51. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  52. @patch('pagure.decorators.admin_session_timedout',
  53. MagicMock(return_value=False))
  54. def test_delete_repo_when_turned_off(self):
  55. """ Test the delete_repo endpoint for a fork when only deleting main
  56. project is forbidden.
  57. """
  58. user = tests.FakeUser(username='pingou')
  59. with tests.user_set(self.app.application, user):
  60. output = self.app.post('/test/delete', follow_redirects=True)
  61. self.assertEqual(output.status_code, 404)
  62. projects = pagure.lib.query.search_projects(self.session)
  63. self.assertEqual(len(projects), 4)
  64. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  65. @patch('pagure.decorators.admin_session_timedout',
  66. MagicMock(return_value=False))
  67. def test_delete_button_present(self):
  68. """ Test that the delete button is present when deletions are
  69. allowed.
  70. """
  71. user = tests.FakeUser(username='pingou')
  72. with tests.user_set(self.app.application, user):
  73. output = self.app.get('/test/settings')
  74. self.assertEqual(output.status_code, 200)
  75. self.assertIn('<form action="/test/delete"', output.get_data(as_text=True))
  76. self.assertIn('&nbsp; Delete the test project', output.get_data(as_text=True))
  77. projects = pagure.lib.query.search_projects(self.session)
  78. self.assertEqual(len(projects), 4)
  79. @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
  80. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  81. @patch('pagure.decorators.admin_session_timedout',
  82. MagicMock(return_value=False))
  83. def test_delete_button_absent(self):
  84. """ Test that the delete button is absent when deletions are not
  85. allowed.
  86. """
  87. user = tests.FakeUser(username='pingou')
  88. with tests.user_set(self.app.application, user):
  89. output = self.app.get('/test/settings')
  90. self.assertEqual(output.status_code, 200)
  91. self.assertNotIn('<form action="/test/delete"', output.get_data(as_text=True))
  92. self.assertNotIn('&nbsp; Delete the test project', output.get_data(as_text=True))
  93. projects = pagure.lib.query.search_projects(self.session)
  94. self.assertEqual(len(projects), 4)
  95. @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
  96. @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': True})
  97. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  98. @patch('pagure.decorators.admin_session_timedout',
  99. MagicMock(return_value=False))
  100. def test_delete_fork_when_project_off_refreshing(self):
  101. """ Test the delete_repo endpoint for a fork when only deleting main
  102. project is forbidden but the fork is being refreshed in the backend
  103. """
  104. project = pagure.lib.query.get_authorized_project(
  105. self.session, project_name='test', user='pingou')
  106. self.assertIsNotNone(project)
  107. # Ensure the project isn't read-only
  108. project.read_only = True
  109. self.session.add(project)
  110. self.session.commit()
  111. user = tests.FakeUser(username='pingou')
  112. with tests.user_set(self.app.application, user):
  113. output = self.app.post(
  114. '/fork/pingou/test/delete', follow_redirects=True)
  115. self.assertEqual(output.status_code, 200)
  116. self.assertIn(
  117. 'The ACLs of this project '
  118. 'are being refreshed in the backend this prevents the '
  119. 'project from being deleted. Please wait for this task to '
  120. 'finish before trying again. Thanks!', output.get_data(as_text=True))
  121. projects = pagure.lib.query.search_projects(self.session)
  122. self.assertEqual(len(projects), 4)
  123. @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
  124. @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': True})
  125. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  126. @patch('pagure.decorators.admin_session_timedout',
  127. MagicMock(return_value=False))
  128. def test_delete_fork_when_project_off(self):
  129. """ Test the delete_repo endpoint for a fork when only deleting main
  130. project is forbidden.
  131. """
  132. project = pagure.lib.query.get_authorized_project(
  133. self.session, project_name='test', user='pingou')
  134. self.assertIsNotNone(project)
  135. # Ensure the project isn't read-only
  136. project.read_only = False
  137. self.session.add(project)
  138. self.session.commit()
  139. user = tests.FakeUser(username='pingou')
  140. with tests.user_set(self.app.application, user):
  141. output = self.app.post(
  142. '/fork/pingou/test/delete', follow_redirects=True)
  143. self.assertEqual(output.status_code, 200)
  144. projects = pagure.lib.query.search_projects(self.session)
  145. self.assertEqual(len(projects), 3)
  146. @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
  147. @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': False})
  148. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  149. @patch('pagure.decorators.admin_session_timedout',
  150. MagicMock(return_value=False))
  151. def test_delete_fork_when_fork_and_project_off(self):
  152. """ Test the delete_repo endpoint for a fork when deleting fork and
  153. project is forbidden.
  154. """
  155. user = tests.FakeUser(username='pingou')
  156. with tests.user_set(self.app.application, user):
  157. output = self.app.post(
  158. '/fork/pingou/test/delete', follow_redirects=True)
  159. self.assertEqual(output.status_code, 404)
  160. projects = pagure.lib.query.search_projects(self.session)
  161. self.assertEqual(len(projects), 4)
  162. @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
  163. @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': False})
  164. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  165. @patch('pagure.decorators.admin_session_timedout',
  166. MagicMock(return_value=False))
  167. def test_delete_fork_button_absent(self):
  168. """ Test that the delete button is absent when deletions are not
  169. allowed.
  170. """
  171. user = tests.FakeUser(username='pingou')
  172. with tests.user_set(self.app.application, user):
  173. output = self.app.get('/fork/pingou/test/settings')
  174. self.assertEqual(output.status_code, 200)
  175. self.assertNotIn(
  176. '<form action="/fork/pingou/test/delete"', output.get_data(as_text=True))
  177. self.assertNotIn(
  178. '&nbsp; Delete the forks/pingou/test project', output.get_data(as_text=True))
  179. projects = pagure.lib.query.search_projects(self.session)
  180. self.assertEqual(len(projects), 4)
  181. @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
  182. @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': True})
  183. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  184. @patch('pagure.decorators.admin_session_timedout',
  185. MagicMock(return_value=False))
  186. def test_delete_fork_button_fork_del_allowed(self):
  187. """ Test that the delete button is present when deletions of projects
  188. is not allowed but deletions of forks is.
  189. """
  190. project = pagure.lib.query.get_authorized_project(
  191. self.session, project_name='test', user='pingou')
  192. self.assertIsNotNone(project)
  193. # Ensure the project isn't read-only
  194. project.read_only = False
  195. self.session.add(project)
  196. self.session.commit()
  197. user = tests.FakeUser(username='pingou')
  198. with tests.user_set(self.app.application, user):
  199. output = self.app.get('/fork/pingou/test/settings')
  200. self.assertEqual(output.status_code, 200)
  201. self.assertIn(
  202. '<form action="/fork/pingou/test/delete"', output.get_data(as_text=True))
  203. self.assertIn(
  204. '&nbsp; Delete the forks/pingou/test project', output.get_data(as_text=True))
  205. projects = pagure.lib.query.search_projects(self.session)
  206. self.assertEqual(len(projects), 4)
  207. @patch.dict('pagure.config.config', {'ENABLE_DEL_PROJECTS': False})
  208. @patch.dict('pagure.config.config', {'ENABLE_DEL_FORKS': True})
  209. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  210. @patch('pagure.decorators.admin_session_timedout',
  211. MagicMock(return_value=False))
  212. def test_delete_fork_button_fork_del_allowed_read_only(self):
  213. """ Test that the delete button is absent when deletions of projects
  214. is not allowed but deletions of forks is but fork is still being
  215. processed.
  216. """
  217. project = pagure.lib.query.get_authorized_project(
  218. self.session, project_name='test', user='pingou')
  219. self.assertIsNotNone(project)
  220. # Ensure the project is read-only
  221. project.read_only = True
  222. self.session.add(project)
  223. self.session.commit()
  224. user = tests.FakeUser(username='pingou')
  225. with tests.user_set(self.app.application, user):
  226. output = self.app.get('/fork/pingou/test/settings')
  227. self.assertEqual(output.status_code, 200)
  228. self.assertNotIn(
  229. '<form action="/fork/pingou/test/delete"', output.get_data(as_text=True))
  230. self.assertIn(
  231. 'title="Action disabled while project\'s ACLs are being '
  232. 'refreshed">', output.get_data(as_text=True))
  233. self.assertIn(
  234. '&nbsp; Delete the forks/pingou/test project', output.get_data(as_text=True))