test_pagure_flask_api_project_delete_project.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2020 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import tempfile
  14. import os
  15. import pygit2
  16. from celery.result import EagerResult
  17. from mock import patch, Mock
  18. sys.path.insert(
  19. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  20. )
  21. import pagure.api
  22. import pagure.flask_app
  23. import pagure.lib.query
  24. import tests
  25. from pagure.lib.repo import PagureRepo
  26. class PagureFlaskApiProjectDeleteProjecttests(tests.Modeltests):
  27. """ Tests for the flask API of pagure for deleting projects """
  28. maxDiff = None
  29. def setUp(self):
  30. super(PagureFlaskApiProjectDeleteProjecttests, self).setUp()
  31. tests.create_projects(self.session)
  32. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  33. tests.create_projects_git(
  34. os.path.join(self.path, "repos", "docs"), bare=True
  35. )
  36. tests.create_projects_git(
  37. os.path.join(self.path, "repos", "tickets"), bare=True
  38. )
  39. tests.create_projects_git(
  40. os.path.join(self.path, "repos", "requests"), bare=True
  41. )
  42. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  43. tests.create_tokens(self.session)
  44. tests.create_tokens_acl(self.session)
  45. tests.create_tokens(self.session, project_id=2, suffix="_test2")
  46. tests.create_tokens_acl(self.session, token_id="aaabbbcccddd_test2")
  47. tests.create_tokens(self.session, user_id=2, suffix="_foo")
  48. tests.create_tokens_acl(self.session, token_id="aaabbbcccddd_foo")
  49. project = pagure.lib.query.get_authorized_project(self.session, "test")
  50. project.read_only = False
  51. self.session.add(project)
  52. self.session.commit()
  53. def test_delete_project_no_header(self):
  54. output = self.app.post("/api/0/invalid/delete")
  55. self.assertEqual(output.status_code, 401)
  56. data = json.loads(output.get_data(as_text=True))
  57. self.assertEqual(
  58. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  59. )
  60. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  61. def test_delete_project_invalid_project(self):
  62. headers = {"Authorization": "token aaabbbcccddd"}
  63. output = self.app.post("/api/0/invalid/delete", headers=headers)
  64. self.assertEqual(output.status_code, 404)
  65. data = json.loads(output.get_data(as_text=True))
  66. self.assertEqual(
  67. pagure.api.APIERROR.ENOPROJECT.name, data["error_code"]
  68. )
  69. self.assertEqual(pagure.api.APIERROR.ENOPROJECT.value, data["error"])
  70. def test_delete_project_invalid_token_project(self):
  71. headers = {"Authorization": "token aaabbbcccddd"}
  72. output = self.app.post("/api/0/test2/delete", headers=headers)
  73. self.assertEqual(output.status_code, 401)
  74. data = json.loads(output.get_data(as_text=True))
  75. self.assertEqual(
  76. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  77. )
  78. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  79. def test_delete_project_read_only_project(self):
  80. headers = {"Authorization": "token aaabbbcccddd_test2"}
  81. output = self.app.post("/api/0/test2/delete", headers=headers)
  82. self.assertEqual(output.status_code, 400)
  83. data = json.loads(output.get_data(as_text=True))
  84. self.assertEqual(pagure.api.APIERROR.ENOCODE.name, data["error_code"])
  85. error = "The ACLs of this project are being refreshed in the backend this prevents the project from being deleted. Please wait for this task to finish before trying again. Thanks!"
  86. self.assertEqual(data["error"], error)
  87. def test_delete_project_not_allowed(self):
  88. headers = {"Authorization": "token aaabbbcccddd_foo"}
  89. output = self.app.post("/api/0/test/delete", headers=headers)
  90. self.assertEqual(output.status_code, 401)
  91. data = json.loads(output.get_data(as_text=True))
  92. self.assertEqual(
  93. pagure.api.APIERROR.ENOTHIGHENOUGH.name, data["error_code"]
  94. )
  95. self.assertEqual(
  96. pagure.api.APIERROR.ENOTHIGHENOUGH.value, data["error"]
  97. )
  98. @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
  99. def test_delete_project_not_allowed_by_config(self):
  100. headers = {"Authorization": "token aaabbbcccddd_test2"}
  101. output = self.app.post("/api/0/test2/delete", headers=headers)
  102. self.assertEqual(output.status_code, 404)
  103. data = json.loads(output.get_data(as_text=True))
  104. self.assertEqual(
  105. pagure.api.APIERROR.ENOPROJECT.name, data["error_code"]
  106. )
  107. self.assertEqual(pagure.api.APIERROR.ENOPROJECT.value, data["error"])
  108. def test_delete_project(self):
  109. headers = {"Authorization": "token aaabbbcccddd"}
  110. projects = pagure.lib.query.search_projects(session=self.session)
  111. self.assertEqual(len(projects), 3)
  112. for frag in [".", "docs", "tickets", "requests"]:
  113. self.assertTrue(
  114. os.path.exists(
  115. os.path.join(self.path, "repos", frag, "test.git")
  116. )
  117. )
  118. output = self.app.post("/api/0/test/delete", headers=headers)
  119. self.assertEqual(output.status_code, 200)
  120. data = json.loads(output.get_data(as_text=True))
  121. for key in ["date_created", "date_modified"]:
  122. data["project"][key] = "1595341690"
  123. self.assertEqual(
  124. data,
  125. {
  126. "message": "Project deleted",
  127. "project": {
  128. "access_groups": {
  129. "admin": [],
  130. "collaborator": [],
  131. "commit": [],
  132. "ticket": [],
  133. },
  134. "access_users": {
  135. "admin": [],
  136. "collaborator": [],
  137. "commit": [],
  138. "owner": ["pingou"],
  139. "ticket": [],
  140. },
  141. "close_status": [
  142. "Invalid",
  143. "Insufficient data",
  144. "Fixed",
  145. "Duplicate",
  146. ],
  147. "custom_keys": [],
  148. "date_created": "1595341690",
  149. "date_modified": "1595341690",
  150. "description": "test project #1",
  151. "fullname": "test",
  152. "id": 1,
  153. "milestones": {},
  154. "name": "test",
  155. "namespace": None,
  156. "parent": None,
  157. "priorities": {},
  158. "tags": [],
  159. "url_path": "test",
  160. "user": {
  161. "fullname": "PY C",
  162. "name": "pingou",
  163. "url_path": "user/pingou",
  164. },
  165. },
  166. },
  167. )
  168. projects = pagure.lib.query.search_projects(session=self.session)
  169. self.assertEqual(len(projects), 2)
  170. for frag in [".", "docs", "tickets", "requests"]:
  171. self.assertFalse(
  172. os.path.exists(
  173. os.path.join(self.path, "repos", frag, "test.git")
  174. )
  175. )