test_pagure_flask_ui_repo_delete_project.py 12 KB

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