test_pagure_flask_ui_repo_delete_project.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. import pagure_messages
  11. from mock import ANY, patch, MagicMock
  12. from fedora_messaging import testing
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.query
  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. )
  33. tests.create_projects_git(
  34. os.path.join(self.path, "repos", "requests"), bare=True
  35. )
  36. project = pagure.lib.query.get_authorized_project(
  37. self.session, project_name="test"
  38. )
  39. self.assertIsNotNone(project)
  40. # Ensure the project isn't read-only
  41. project.read_only = False
  42. self.session.add(project)
  43. self.session.commit()
  44. # Create a fork
  45. task = pagure.lib.query.fork_project(
  46. session=self.session, user="pingou", repo=project
  47. )
  48. task.get()
  49. # Ensure everything was correctly created
  50. projects = pagure.lib.query.search_projects(self.session)
  51. self.assertEqual(len(projects), 4)
  52. @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
  53. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  54. @patch(
  55. "pagure.decorators.admin_session_timedout",
  56. MagicMock(return_value=False),
  57. )
  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.query.search_projects(self.session)
  67. self.assertEqual(len(projects), 4)
  68. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  69. @patch(
  70. "pagure.decorators.admin_session_timedout",
  71. MagicMock(return_value=False),
  72. )
  73. def test_delete_button_present(self):
  74. """Test that the delete button is present when deletions are
  75. allowed.
  76. """
  77. user = tests.FakeUser(username="pingou")
  78. with tests.user_set(self.app.application, user):
  79. output = self.app.get("/test/settings")
  80. self.assertEqual(output.status_code, 200)
  81. self.assertIn(
  82. '<form action="/test/delete"', output.get_data(as_text=True)
  83. )
  84. self.assertIn(
  85. "&nbsp; Delete the test project", output.get_data(as_text=True)
  86. )
  87. projects = pagure.lib.query.search_projects(self.session)
  88. self.assertEqual(len(projects), 4)
  89. @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
  90. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  91. @patch(
  92. "pagure.decorators.admin_session_timedout",
  93. MagicMock(return_value=False),
  94. )
  95. def test_delete_button_absent(self):
  96. """Test that the delete button is absent when deletions are not
  97. allowed.
  98. """
  99. user = tests.FakeUser(username="pingou")
  100. with tests.user_set(self.app.application, user):
  101. output = self.app.get("/test/settings")
  102. self.assertEqual(output.status_code, 200)
  103. self.assertNotIn(
  104. '<form action="/test/delete"', output.get_data(as_text=True)
  105. )
  106. self.assertNotIn(
  107. "&nbsp; Delete the test project", output.get_data(as_text=True)
  108. )
  109. projects = pagure.lib.query.search_projects(self.session)
  110. self.assertEqual(len(projects), 4)
  111. @patch.dict(
  112. "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
  113. )
  114. @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
  115. @patch.dict("pagure.config.config", {"ENABLE_DEL_FORKS": True})
  116. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  117. @patch(
  118. "pagure.decorators.admin_session_timedout",
  119. MagicMock(return_value=False),
  120. )
  121. def test_delete_fork_when_project_off(self):
  122. """Test the delete_repo endpoint for a fork when only deleting main
  123. project is forbidden.
  124. """
  125. project = pagure.lib.query.get_authorized_project(
  126. self.session, project_name="test", user="pingou"
  127. )
  128. self.assertIsNotNone(project)
  129. # Ensure the project isn't read-only
  130. project.read_only = False
  131. self.session.add(project)
  132. self.session.commit()
  133. user = tests.FakeUser(username="pingou")
  134. with tests.user_set(self.app.application, user):
  135. with testing.mock_sends(
  136. pagure_messages.ProjectDeletedV1(
  137. topic="pagure.project.deleted",
  138. body={
  139. "project": {
  140. "id": 4,
  141. "name": "test",
  142. "fullname": "forks/pingou/test",
  143. "url_path": "fork/pingou/test",
  144. "full_url": "http://localhost.localdomain/fork/pingou/test",
  145. "description": "test project #1",
  146. "namespace": None,
  147. "parent": {
  148. "id": 1,
  149. "name": "test",
  150. "fullname": "test",
  151. "url_path": "test",
  152. "full_url": "http://localhost.localdomain/test",
  153. "description": "test project #1",
  154. "namespace": None,
  155. "parent": None,
  156. "date_created": ANY,
  157. "date_modified": ANY,
  158. "user": {
  159. "name": "pingou",
  160. "fullname": "PY C",
  161. "url_path": "user/pingou",
  162. "full_url": "http://localhost.localdomain/user/pingou",
  163. },
  164. "access_users": {
  165. "owner": ["pingou"],
  166. "admin": [],
  167. "commit": [],
  168. "collaborator": [],
  169. "ticket": [],
  170. },
  171. "access_groups": {
  172. "admin": [],
  173. "commit": [],
  174. "collaborator": [],
  175. "ticket": [],
  176. },
  177. "tags": [],
  178. "priorities": {},
  179. "custom_keys": [],
  180. "close_status": [
  181. "Invalid",
  182. "Insufficient data",
  183. "Fixed",
  184. "Duplicate",
  185. ],
  186. "milestones": {},
  187. },
  188. "date_created": ANY,
  189. "date_modified": ANY,
  190. "user": {
  191. "name": "pingou",
  192. "fullname": "PY C",
  193. "url_path": "user/pingou",
  194. "full_url": "http://localhost.localdomain/user/pingou",
  195. },
  196. "access_users": {
  197. "owner": ["pingou"],
  198. "admin": [],
  199. "commit": [],
  200. "collaborator": [],
  201. "ticket": [],
  202. },
  203. "access_groups": {
  204. "admin": [],
  205. "commit": [],
  206. "collaborator": [],
  207. "ticket": [],
  208. },
  209. "tags": [],
  210. "priorities": {},
  211. "custom_keys": [],
  212. "close_status": [],
  213. "milestones": {},
  214. },
  215. "agent": "pingou",
  216. },
  217. )
  218. ):
  219. output = self.app.post(
  220. "/fork/pingou/test/delete", follow_redirects=True
  221. )
  222. self.assertEqual(output.status_code, 200)
  223. projects = pagure.lib.query.search_projects(self.session)
  224. self.assertEqual(len(projects), 3)
  225. @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
  226. @patch.dict("pagure.config.config", {"ENABLE_DEL_FORKS": False})
  227. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  228. @patch(
  229. "pagure.decorators.admin_session_timedout",
  230. MagicMock(return_value=False),
  231. )
  232. def test_delete_fork_when_fork_and_project_off(self):
  233. """Test the delete_repo endpoint for a fork when deleting fork and
  234. project is forbidden.
  235. """
  236. user = tests.FakeUser(username="pingou")
  237. with tests.user_set(self.app.application, user):
  238. output = self.app.post(
  239. "/fork/pingou/test/delete", follow_redirects=True
  240. )
  241. self.assertEqual(output.status_code, 404)
  242. projects = pagure.lib.query.search_projects(self.session)
  243. self.assertEqual(len(projects), 4)
  244. @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
  245. @patch.dict("pagure.config.config", {"ENABLE_DEL_FORKS": False})
  246. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  247. @patch(
  248. "pagure.decorators.admin_session_timedout",
  249. MagicMock(return_value=False),
  250. )
  251. def test_delete_fork_button_absent(self):
  252. """Test that the delete button is absent when deletions are not
  253. allowed.
  254. """
  255. user = tests.FakeUser(username="pingou")
  256. with tests.user_set(self.app.application, user):
  257. output = self.app.get("/fork/pingou/test/settings")
  258. self.assertEqual(output.status_code, 200)
  259. self.assertNotIn(
  260. '<form action="/fork/pingou/test/delete"',
  261. output.get_data(as_text=True),
  262. )
  263. self.assertNotIn(
  264. "&nbsp; Delete the forks/pingou/test project",
  265. output.get_data(as_text=True),
  266. )
  267. projects = pagure.lib.query.search_projects(self.session)
  268. self.assertEqual(len(projects), 4)
  269. @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
  270. @patch.dict("pagure.config.config", {"ENABLE_DEL_FORKS": True})
  271. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  272. @patch(
  273. "pagure.decorators.admin_session_timedout",
  274. MagicMock(return_value=False),
  275. )
  276. def test_delete_fork_button_fork_del_allowed(self):
  277. """Test that the delete button is present when deletions of projects
  278. is not allowed but deletions of forks is.
  279. """
  280. project = pagure.lib.query.get_authorized_project(
  281. self.session, project_name="test", user="pingou"
  282. )
  283. self.assertIsNotNone(project)
  284. # Ensure the project isn't read-only
  285. project.read_only = False
  286. self.session.add(project)
  287. self.session.commit()
  288. user = tests.FakeUser(username="pingou")
  289. with tests.user_set(self.app.application, user):
  290. output = self.app.get("/fork/pingou/test/settings")
  291. self.assertEqual(output.status_code, 200)
  292. self.assertIn(
  293. '<form action="/fork/pingou/test/delete"',
  294. output.get_data(as_text=True),
  295. )
  296. self.assertIn(
  297. "&nbsp; Delete the forks/pingou/test project",
  298. output.get_data(as_text=True),
  299. )
  300. projects = pagure.lib.query.search_projects(self.session)
  301. self.assertEqual(len(projects), 4)