test_pagure_flask_ui_repo_delete_project.py 11 KB

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