test_pagure_flask_ui_repo_delete_project.py 11 KB

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