test_pagure_flask_ui_repo_delete_project.py 11 KB

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