1
0

test_pagure_flask_api_project_delete_project.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. self.session.add(project)
  51. self.session.commit()
  52. def test_delete_project_no_header(self):
  53. output = self.app.post("/api/0/invalid/delete")
  54. self.assertEqual(output.status_code, 401)
  55. data = json.loads(output.get_data(as_text=True))
  56. self.assertEqual(
  57. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  58. )
  59. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  60. def test_delete_project_invalid_project(self):
  61. headers = {"Authorization": "token aaabbbcccddd"}
  62. output = self.app.post("/api/0/invalid/delete", headers=headers)
  63. self.assertEqual(output.status_code, 404)
  64. data = json.loads(output.get_data(as_text=True))
  65. self.assertEqual(
  66. pagure.api.APIERROR.ENOPROJECT.name, data["error_code"]
  67. )
  68. self.assertEqual(pagure.api.APIERROR.ENOPROJECT.value, data["error"])
  69. def test_delete_project_invalid_token_project(self):
  70. headers = {"Authorization": "token aaabbbcccddd"}
  71. output = self.app.post("/api/0/test2/delete", headers=headers)
  72. self.assertEqual(output.status_code, 401)
  73. data = json.loads(output.get_data(as_text=True))
  74. self.assertEqual(
  75. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  76. )
  77. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  78. def test_delete_project_not_allowed(self):
  79. headers = {"Authorization": "token aaabbbcccddd_foo"}
  80. output = self.app.post("/api/0/test/delete", headers=headers)
  81. self.assertEqual(output.status_code, 401)
  82. data = json.loads(output.get_data(as_text=True))
  83. self.assertEqual(
  84. pagure.api.APIERROR.ENOTHIGHENOUGH.name, data["error_code"]
  85. )
  86. self.assertEqual(
  87. pagure.api.APIERROR.ENOTHIGHENOUGH.value, data["error"]
  88. )
  89. @patch.dict("pagure.config.config", {"ENABLE_DEL_PROJECTS": False})
  90. def test_delete_project_not_allowed_by_config(self):
  91. headers = {"Authorization": "token aaabbbcccddd_test2"}
  92. output = self.app.post("/api/0/test2/delete", headers=headers)
  93. self.assertEqual(output.status_code, 404)
  94. data = json.loads(output.get_data(as_text=True))
  95. self.assertEqual(
  96. pagure.api.APIERROR.ENOPROJECT.name, data["error_code"]
  97. )
  98. self.assertEqual(pagure.api.APIERROR.ENOPROJECT.value, data["error"])
  99. def test_delete_project(self):
  100. headers = {"Authorization": "token aaabbbcccddd"}
  101. projects = pagure.lib.query.search_projects(session=self.session)
  102. self.assertEqual(len(projects), 3)
  103. for frag in [".", "docs", "tickets", "requests"]:
  104. self.assertTrue(
  105. os.path.exists(
  106. os.path.join(self.path, "repos", frag, "test.git")
  107. )
  108. )
  109. output = self.app.post("/api/0/test/delete", headers=headers)
  110. self.assertEqual(output.status_code, 200)
  111. data = json.loads(output.get_data(as_text=True))
  112. for key in ["date_created", "date_modified"]:
  113. data["project"][key] = "1595341690"
  114. self.assertEqual(
  115. data,
  116. {
  117. "message": "Project deleted",
  118. "project": {
  119. "access_groups": {
  120. "admin": [],
  121. "collaborator": [],
  122. "commit": [],
  123. "ticket": [],
  124. },
  125. "access_users": {
  126. "admin": [],
  127. "collaborator": [],
  128. "commit": [],
  129. "owner": ["pingou"],
  130. "ticket": [],
  131. },
  132. "close_status": [
  133. "Invalid",
  134. "Insufficient data",
  135. "Fixed",
  136. "Duplicate",
  137. ],
  138. "custom_keys": [],
  139. "date_created": "1595341690",
  140. "date_modified": "1595341690",
  141. "description": "test project #1",
  142. "full_url": "http://localhost.localdomain/test",
  143. "fullname": "test",
  144. "id": 1,
  145. "milestones": {},
  146. "name": "test",
  147. "namespace": None,
  148. "parent": None,
  149. "priorities": {},
  150. "tags": [],
  151. "url_path": "test",
  152. "user": {
  153. "fullname": "PY C",
  154. "name": "pingou",
  155. "url_path": "user/pingou",
  156. "full_url": "http://localhost.localdomain/user/pingou",
  157. },
  158. },
  159. },
  160. )
  161. projects = pagure.lib.query.search_projects(session=self.session)
  162. self.assertEqual(len(projects), 2)
  163. for frag in [".", "docs", "tickets", "requests"]:
  164. self.assertFalse(
  165. os.path.exists(
  166. os.path.join(self.path, "repos", frag, "test.git")
  167. )
  168. )