test_pagure_flask_api_issue_update.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # -*- coding: utf-8 -*-
  2. """
  3. Authors:
  4. Julen Landa Alustiza <jlanda@fedoraproject.org>
  5. """
  6. from __future__ import unicode_literals, absolute_import
  7. import datetime
  8. import unittest
  9. import sys
  10. import os
  11. import json
  12. from mock import patch, MagicMock
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.query # noqa: E402
  17. import tests # noqa: E402
  18. class PagureFlaskApiIssueUpdatetests(tests.Modeltests):
  19. """ Tests for the flask API of pagure for updating an issue """
  20. def setUp(self):
  21. """ Set up the environnment, ran before every tests. """
  22. super(PagureFlaskApiIssueUpdatetests, self).setUp()
  23. pagure.config.config["TICKETS_FOLDER"] = None
  24. tests.create_projects(self.session)
  25. tests.create_tokens(self.session)
  26. def test_api_issue_update_wrong_token(self):
  27. """ Test the api_issue_update method of flask API """
  28. tests.create_tokens_acl(self.session)
  29. headers = {"Authorization": "token aaa"}
  30. output = self.app.post("/api/0/foo/issue/1", headers=headers)
  31. self.assertEqual(output.status_code, 401)
  32. expected_rv = {
  33. "error": "Invalid or expired token. Please visit "
  34. "http://localhost.localdomain/settings#nav-api-tab to get or renew "
  35. "your API token.",
  36. "error_code": "EINVALIDTOK",
  37. "errors": "Invalid token",
  38. }
  39. data = json.loads(output.get_data(as_text=True))
  40. self.assertDictEqual(data, expected_rv)
  41. def test_api_issue_update_wrong_project(self):
  42. """ Test the api_issue_update method of flask API """
  43. tests.create_tokens_acl(self.session)
  44. headers = {"Authorization": "token aaabbbcccddd"}
  45. output = self.app.post("/api/0/foo/issue/1", headers=headers)
  46. self.assertEqual(output.status_code, 404)
  47. expected_rv = {
  48. "error": "Project not found",
  49. "error_code": "ENOPROJECT",
  50. }
  51. data = json.loads(output.get_data(as_text=True))
  52. self.assertDictEqual(data, expected_rv)
  53. def test_api_issue_update_wrong_acls(self):
  54. """ Test the api_issue_update method of flask API """
  55. tests.create_tokens_acl(self.session, acl_name="issue_create")
  56. headers = {"Authorization": "token aaabbbcccddd"}
  57. output = self.app.post("/api/0/test/issue/1", headers=headers)
  58. self.assertEqual(output.status_code, 401)
  59. expected_rv = {
  60. "error": "Invalid or expired token. Please visit "
  61. "http://localhost.localdomain/settings#nav-api-tab to get or renew "
  62. "your API token.",
  63. "error_code": "EINVALIDTOK",
  64. "errors": "Missing ACLs: issue_update",
  65. }
  66. data = json.loads(output.get_data(as_text=True))
  67. self.assertDictEqual(data, expected_rv)
  68. @patch.dict("pagure.config.config", {"ENABLE_TICKETS": False})
  69. def test_api_issue_update_instance_tickets_disabled(self):
  70. """ Test the api_issue_update method of flask API """
  71. tests.create_tokens_acl(self.session)
  72. headers = {"Authorization": "token aaabbbcccddd"}
  73. output = self.app.post("/api/0/test/issue/1", headers=headers)
  74. self.assertEqual(output.status_code, 404)
  75. expected_rv = {
  76. "error": "Issue tracker disabled",
  77. "error_code": "ETRACKERDISABLED",
  78. }
  79. data = json.loads(output.get_data(as_text=True))
  80. self.assertDictEqual(data, expected_rv)
  81. def test_api_issue_update_project_tickets_disabled(self):
  82. """ Test the api_issue_update method of flask API """
  83. tests.create_tokens_acl(self.session)
  84. # disable tickets on this repo
  85. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  86. settings = repo.settings
  87. settings["issue_tracker"] = False
  88. repo.settings = settings
  89. self.session.add(repo)
  90. self.session.commit()
  91. headers = {"Authorization": "token aaabbbcccddd"}
  92. output = self.app.post("/api/0/test/issue/1", headers=headers)
  93. self.assertEqual(output.status_code, 404)
  94. expected_rv = {
  95. "error": "Issue tracker disabled",
  96. "error_code": "ETRACKERDISABLED",
  97. }
  98. data = json.loads(output.get_data(as_text=True))
  99. self.assertDictEqual(data, expected_rv)
  100. def test_api_issue_update_project_read_only_issue_tracker(self):
  101. """ Test the api_issue_update method of flask API """
  102. tests.create_tokens_acl(self.session)
  103. # set read only issue tracke on this repo
  104. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  105. settings = repo.settings
  106. settings["issue_tracker_read_only"] = True
  107. repo.settings = settings
  108. self.session.add(repo)
  109. self.session.commit()
  110. headers = {"Authorization": "token aaabbbcccddd"}
  111. output = self.app.post("/api/0/test/issue/1", headers=headers)
  112. self.assertEqual(output.status_code, 401)
  113. expected_rv = {
  114. "error": "The issue tracker of this project is read-only",
  115. "error_code": "ETRACKERREADONLY",
  116. }
  117. data = json.loads(output.get_data(as_text=True))
  118. self.assertDictEqual(data, expected_rv)
  119. def test_api_issue_update_wrong_issue(self):
  120. """ Test the api_issue_update method of flask API """
  121. tests.create_tokens_acl(self.session)
  122. tests.create_projects_git(os.path.join(self.path, "tickets"))
  123. headers = {"Authorization": "token aaabbbcccddd"}
  124. output = self.app.post("/api/0/test/issue/1", headers=headers)
  125. self.assertEqual(output.status_code, 404)
  126. expected_rv = {"error": "Issue not found", "error_code": "ENOISSUE"}
  127. data = json.loads(output.get_data(as_text=True))
  128. self.assertDictEqual(data, expected_rv)
  129. def test_api_issue_update_no_input(self):
  130. """ Test the api_issue_update method of flask API """
  131. tests.create_tokens_acl(self.session)
  132. tests.create_projects_git(os.path.join(self.path, "tickets"))
  133. headers = {"Authorization": "token aaabbbcccddd"}
  134. # Create an issue
  135. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  136. msg = pagure.lib.query.new_issue(
  137. session=self.session,
  138. repo=repo,
  139. title="Test issue #1",
  140. content="We should work on this",
  141. user="pingou",
  142. private=False,
  143. )
  144. self.session.commit()
  145. self.assertEqual(msg.title, "Test issue #1")
  146. headers = {"Authorization": "token aaabbbcccddd"}
  147. output = self.app.post("/api/0/test/issue/1", headers=headers)
  148. self.assertEqual(output.status_code, 400)
  149. expected_rv = {
  150. "error": "Invalid or incomplete input submitted",
  151. "error_code": "EINVALIDREQ",
  152. "errors": {
  153. "issue_content": ["This field is required."],
  154. "title": ["This field is required."],
  155. },
  156. }
  157. data = json.loads(output.get_data(as_text=True))
  158. self.assertDictEqual(data, expected_rv)
  159. def test_api_issue_update_partial_input(self):
  160. """ Test the api_issue_update method of flask API """
  161. tests.create_tokens_acl(self.session)
  162. tests.create_projects_git(os.path.join(self.path, "tickets"))
  163. headers = {"Authorization": "token aaabbbcccddd"}
  164. # Create an issue
  165. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  166. msg = pagure.lib.query.new_issue(
  167. session=self.session,
  168. repo=repo,
  169. title="Test issue #1",
  170. content="We should work on this",
  171. user="pingou",
  172. private=False,
  173. )
  174. self.session.commit()
  175. self.assertEqual(msg.title, "Test issue #1")
  176. headers = {"Authorization": "token aaabbbcccddd"}
  177. # missing issue_content
  178. data = {"title": "New title"}
  179. output = self.app.post(
  180. "/api/0/test/issue/1", data=data, headers=headers
  181. )
  182. self.assertEqual(output.status_code, 400)
  183. expected_rv = {
  184. "error": "Invalid or incomplete input submitted",
  185. "error_code": "EINVALIDREQ",
  186. "errors": {"issue_content": ["This field is required."]},
  187. }
  188. data = json.loads(output.get_data(as_text=True))
  189. self.assertDictEqual(data, expected_rv)
  190. # missing title
  191. data = {"issue_content": "New content"}
  192. output = self.app.post(
  193. "/api/0/test/issue/1", data=data, headers=headers
  194. )
  195. self.assertEqual(output.status_code, 400)
  196. expected_rv = {
  197. "error": "Invalid or incomplete input submitted",
  198. "error_code": "EINVALIDREQ",
  199. "errors": {"title": ["This field is required."]},
  200. }
  201. data = json.loads(output.get_data(as_text=True))
  202. self.assertDictEqual(data, expected_rv)
  203. def test_api_issue_update(self):
  204. """ Test the api_issue_update method of flask API """
  205. tests.create_tokens_acl(self.session)
  206. tests.create_projects_git(os.path.join(self.path, "tickets"))
  207. headers = {"Authorization": "token aaabbbcccddd"}
  208. # Create an issue
  209. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  210. msg = pagure.lib.query.new_issue(
  211. session=self.session,
  212. repo=repo,
  213. title="Test issue #1",
  214. content="We should work on this",
  215. user="pingou",
  216. private=False,
  217. )
  218. self.session.commit()
  219. self.assertEqual(msg.title, "Test issue #1")
  220. headers = {"Authorization": "token aaabbbcccddd"}
  221. data = {"title": "New title", "issue_content": "New content"}
  222. output = self.app.post(
  223. "/api/0/test/issue/1", data=data, headers=headers
  224. )
  225. self.assertEqual(output.status_code, 200)
  226. data = json.loads(output.get_data(as_text=True))
  227. self.assertEqual(data["message"], "Issue edited")
  228. self.assertEqual(data["issue"]["title"], "New title")
  229. self.assertEqual(data["issue"]["content"], "New content")
  230. output = self.app.get("/api/0/test/issue/1", headers=headers)
  231. self.assertEqual(output.status_code, 200)
  232. data = json.loads(output.get_data(as_text=True))
  233. self.assertEqual(data["title"], "New title")
  234. self.assertEqual(data["content"], "New content")