test_pagure_flask_api_fork_assign.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2019 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import arrow
  9. import copy
  10. import datetime
  11. import unittest
  12. import shutil
  13. import sys
  14. import time
  15. import os
  16. import flask
  17. import json
  18. import munch
  19. from mock import patch, MagicMock
  20. from sqlalchemy.exc import SQLAlchemyError
  21. sys.path.insert(
  22. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  23. )
  24. import pagure.lib.query
  25. import tests
  26. class PagureFlaskApiForkAssigntests(tests.SimplePagureTest):
  27. """Tests for the flask API of pagure for assigning a PR"""
  28. maxDiff = None
  29. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  30. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  31. def setUp(self):
  32. """Set up the environnment, ran before every tests."""
  33. super(PagureFlaskApiForkAssigntests, self).setUp()
  34. tests.create_projects(self.session)
  35. tests.add_content_git_repo(
  36. os.path.join(self.path, "repos", "test.git")
  37. )
  38. # Fork
  39. project = pagure.lib.query.get_authorized_project(self.session, "test")
  40. task = pagure.lib.query.fork_project(
  41. session=self.session, user="pingou", repo=project
  42. )
  43. self.session.commit()
  44. self.assertEqual(
  45. task.get(),
  46. {
  47. "endpoint": "ui_ns.view_repo",
  48. "repo": "test",
  49. "namespace": None,
  50. "username": "pingou",
  51. },
  52. )
  53. tests.add_readme_git_repo(
  54. os.path.join(self.path, "repos", "forks", "pingou", "test.git")
  55. )
  56. project = pagure.lib.query.get_authorized_project(self.session, "test")
  57. fork = pagure.lib.query.get_authorized_project(
  58. self.session, "test", user="pingou"
  59. )
  60. tests.create_tokens(self.session)
  61. tests.create_tokens_acl(self.session)
  62. req = pagure.lib.query.new_pull_request(
  63. session=self.session,
  64. repo_from=fork,
  65. branch_from="master",
  66. repo_to=project,
  67. branch_to="master",
  68. title="test pull-request",
  69. user="pingou",
  70. )
  71. self.session.commit()
  72. self.assertEqual(req.id, 1)
  73. self.assertEqual(req.title, "test pull-request")
  74. # Assert the PR is open
  75. self.session = pagure.lib.query.create_session(self.dbpath)
  76. project = pagure.lib.query.get_authorized_project(self.session, "test")
  77. self.assertEqual(len(project.requests), 1)
  78. self.assertEqual(project.requests[0].status, "Open")
  79. # Check how the PR renders in the API and the UI
  80. output = self.app.get("/api/0/test/pull-request/1")
  81. self.assertEqual(output.status_code, 200)
  82. output = self.app.get("/test/pull-request/1")
  83. self.assertEqual(output.status_code, 200)
  84. def test_api_assign_pr_invalid_project_namespace(self):
  85. """Test api_pull_request_assign method when the project doesn't exist."""
  86. headers = {"Authorization": "token aaabbbcccddd"}
  87. # Valid token, wrong project
  88. output = self.app.post(
  89. "/api/0/somenamespace/test3/pull-request/1/assign", headers=headers
  90. )
  91. self.assertEqual(output.status_code, 401)
  92. data = json.loads(output.get_data(as_text=True))
  93. self.assertDictEqual(
  94. data,
  95. {
  96. "error": "Invalid or expired token. Please visit "
  97. "http://localhost.localdomain/settings#nav-api-tab to get or renew your "
  98. "API token.",
  99. "error_code": "EINVALIDTOK",
  100. },
  101. )
  102. def test_api_assign_pr_invalid_project(self):
  103. """Test api_pull_request_assign method when the project doesn't exist."""
  104. headers = {"Authorization": "token aaabbbcccddd"}
  105. # Invalid project
  106. output = self.app.post(
  107. "/api/0/foo/pull-request/1/assign", headers=headers
  108. )
  109. self.assertEqual(output.status_code, 404)
  110. data = json.loads(output.get_data(as_text=True))
  111. self.assertDictEqual(
  112. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  113. )
  114. def test_api_assign_pr_invalid_project_token(self):
  115. """Test api_pull_request_assign method when the token doesn't correspond
  116. to the project.
  117. """
  118. headers = {"Authorization": "token aaabbbcccddd"}
  119. # Valid token, wrong project
  120. output = self.app.post(
  121. "/api/0/test2/pull-request/1/assign", headers=headers
  122. )
  123. self.assertEqual(output.status_code, 401)
  124. data = json.loads(output.get_data(as_text=True))
  125. self.assertEqual(sorted(data.keys()), ["error", "error_code"])
  126. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  127. self.assertEqual(
  128. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  129. )
  130. def test_api_assign_pr_invalid_pr(self):
  131. """Test api_pull_request_assign method when asking for an invalid PR"""
  132. headers = {"Authorization": "token aaabbbcccddd"}
  133. # No input
  134. output = self.app.post(
  135. "/api/0/test/pull-request/404/assign", headers=headers
  136. )
  137. self.assertEqual(output.status_code, 404)
  138. data = json.loads(output.get_data(as_text=True))
  139. self.assertDictEqual(
  140. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  141. )
  142. def test_api_assign_pr_no_input(self):
  143. """Test api_pull_request_assign method when no input is specified"""
  144. headers = {"Authorization": "token aaabbbcccddd"}
  145. # No input
  146. output = self.app.post(
  147. "/api/0/test/pull-request/1/assign", headers=headers
  148. )
  149. self.assertEqual(output.status_code, 200)
  150. data = json.loads(output.get_data(as_text=True))
  151. self.assertDictEqual(data, {"message": "Nothing to change"})
  152. def test_api_assign_pr_assigned(self):
  153. """Test api_pull_request_assign method when with valid input"""
  154. headers = {"Authorization": "token aaabbbcccddd"}
  155. data = {"assignee": "pingou"}
  156. # Valid request
  157. output = self.app.post(
  158. "/api/0/test/pull-request/1/assign", data=data, headers=headers
  159. )
  160. self.assertEqual(output.status_code, 200)
  161. data = json.loads(output.get_data(as_text=True))
  162. self.assertDictEqual(data, {"message": "Request assigned"})
  163. def test_api_assign_pr_unassigned(self):
  164. """Test api_pull_request_assign method when unassigning"""
  165. self.test_api_assign_pr_assigned()
  166. headers = {"Authorization": "token aaabbbcccddd"}
  167. data = {}
  168. # Un-assign
  169. output = self.app.post(
  170. "/api/0/test/pull-request/1/assign", data=data, headers=headers
  171. )
  172. self.assertEqual(output.status_code, 200)
  173. data = json.loads(output.get_data(as_text=True))
  174. self.assertDictEqual(data, {"message": "Request assignee reset"})
  175. def test_api_assign_pr_unassigned_twice(self):
  176. """Test api_pull_request_assign method when unassigning"""
  177. self.test_api_assign_pr_unassigned()
  178. headers = {"Authorization": "token aaabbbcccddd"}
  179. data = {"assignee": None}
  180. # Un-assign
  181. output = self.app.post(
  182. "/api/0/test/pull-request/1/assign", data=data, headers=headers
  183. )
  184. self.assertEqual(output.status_code, 200)
  185. data = json.loads(output.get_data(as_text=True))
  186. self.assertDictEqual(data, {"message": "Nothing to change"})
  187. def test_api_assign_pr_unassigned_empty_string(self):
  188. """Test api_pull_request_assign method when unassigning with an
  189. empty string
  190. """
  191. self.test_api_assign_pr_assigned()
  192. headers = {"Authorization": "token aaabbbcccddd"}
  193. # Un-assign
  194. data = {"assignee": ""}
  195. output = self.app.post(
  196. "/api/0/test/pull-request/1/assign", data=data, headers=headers
  197. )
  198. self.assertEqual(output.status_code, 200)
  199. data = json.loads(output.get_data(as_text=True))
  200. self.assertDictEqual(data, {"message": "Request assignee reset"})
  201. if __name__ == "__main__":
  202. unittest.main(verbosity=2)