test_pagure_flask_api_issue_custom_fields.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. """
  2. (c) 2017 - Copyright Red Hat Inc
  3. Authors:
  4. Clement Verna <cverna@tutanota.com>
  5. """
  6. from __future__ import unicode_literals, absolute_import
  7. import unittest
  8. import sys
  9. import os
  10. import json
  11. from mock import patch, MagicMock
  12. sys.path.insert(
  13. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  14. )
  15. import pagure.lib.query # noqa: E402
  16. import tests # noqa: E402
  17. class PagureFlaskApiCustomFieldIssuetests(tests.Modeltests):
  18. """ Tests for the flask API of pagure for issue's custom fields """
  19. def setUp(self):
  20. """ Set up the environnment, ran before every tests. """
  21. self.maxDiff = None
  22. super(PagureFlaskApiCustomFieldIssuetests, self).setUp()
  23. pagure.config.config["TICKETS_FOLDER"] = None
  24. tests.create_projects(self.session)
  25. tests.create_projects_git(os.path.join(self.path, "tickets"))
  26. tests.create_tokens(self.session)
  27. tests.create_tokens_acl(self.session)
  28. # Create normal issue
  29. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  30. pagure.lib.query.new_issue(
  31. session=self.session,
  32. repo=repo,
  33. title="Test issue #1",
  34. content="We should work on this",
  35. user="pingou",
  36. private=False,
  37. )
  38. self.session.commit()
  39. def test_api_update_custom_field_bad_request(self):
  40. """ Test the api_update_custom_field method of the flask api.
  41. This test that a badly form request returns the correct error.
  42. """
  43. headers = {"Authorization": "token aaabbbcccddd"}
  44. # Request is not formated correctly (EMPTY)
  45. payload = {}
  46. output = self.app.post(
  47. "/api/0/test/issue/1/custom", headers=headers, data=payload
  48. )
  49. self.assertEqual(output.status_code, 400)
  50. data = json.loads(output.get_data(as_text=True))
  51. self.assertDictEqual(
  52. data,
  53. {
  54. "error": "Invalid or incomplete input submitted",
  55. "error_code": "EINVALIDREQ",
  56. },
  57. )
  58. def test_api_update_custom_field_wrong_field(self):
  59. """ Test the api_update_custom_field method of the flask api.
  60. This test that an invalid field retruns the correct error.
  61. """
  62. headers = {"Authorization": "token aaabbbcccddd"}
  63. # Project does not have this custom field
  64. payload = {"foo": "bar"}
  65. output = self.app.post(
  66. "/api/0/test/issue/1/custom", headers=headers, data=payload
  67. )
  68. self.assertEqual(output.status_code, 400)
  69. data = json.loads(output.get_data(as_text=True))
  70. self.assertDictEqual(
  71. data,
  72. {
  73. "error": "Invalid custom field submitted",
  74. "error_code": "EINVALIDISSUEFIELD",
  75. },
  76. )
  77. @patch(
  78. "pagure.lib.query.set_custom_key_value",
  79. MagicMock(side_effect=pagure.exceptions.PagureException("error")),
  80. )
  81. def test_api_update_custom_field_raise_error(self):
  82. """ Test the api_update_custom_field method of the flask api.
  83. This test the successful requests scenarii.
  84. """
  85. headers = {"Authorization": "token aaabbbcccddd"}
  86. # Set some custom fields
  87. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  88. msg = pagure.lib.query.set_custom_key_fields(
  89. self.session,
  90. repo,
  91. ["bugzilla", "upstream", "reviewstatus"],
  92. ["link", "boolean", "list"],
  93. [
  94. "unused data for non-list type",
  95. "",
  96. "ack",
  97. "nack",
  98. "needs review",
  99. ],
  100. [None, None, None],
  101. )
  102. self.session.commit()
  103. self.assertEqual(msg, "List of custom fields updated")
  104. payload = {"bugzilla": "", "upstream": True}
  105. output = self.app.post(
  106. "/api/0/test/issue/1/custom", headers=headers, data=payload
  107. )
  108. self.assertEqual(output.status_code, 400)
  109. data = json.loads(output.get_data(as_text=True))
  110. self.assertDictEqual(data, {"error": "error", "error_code": "ENOCODE"})
  111. def test_api_update_custom_field(self):
  112. """ Test the api_update_custom_field method of the flask api.
  113. This test the successful requests scenarii.
  114. """
  115. headers = {"Authorization": "token aaabbbcccddd"}
  116. # Set some custom fields
  117. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  118. msg = pagure.lib.query.set_custom_key_fields(
  119. self.session,
  120. repo,
  121. ["bugzilla", "upstream", "reviewstatus"],
  122. ["link", "boolean", "list"],
  123. [
  124. "unused data for non-list type",
  125. "",
  126. "ack",
  127. "nack",
  128. "needs review",
  129. ],
  130. [None, None, None],
  131. )
  132. self.session.commit()
  133. self.assertEqual(msg, "List of custom fields updated")
  134. payload = {"bugzilla": "", "upstream": True}
  135. output = self.app.post(
  136. "/api/0/test/issue/1/custom", headers=headers, data=payload
  137. )
  138. self.assertEqual(output.status_code, 200)
  139. data = json.loads(output.get_data(as_text=True))
  140. data["messages"].sort(key=lambda d: list(d.keys())[0])
  141. self.assertDictEqual(
  142. data,
  143. {
  144. "messages": [
  145. {"bugzilla": "No changes"},
  146. {"upstream": "Custom field upstream adjusted to True"},
  147. ]
  148. },
  149. )
  150. self.session.commit()
  151. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  152. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  153. self.assertEqual(len(issue.other_fields), 1)
  154. payload = {
  155. "bugzilla": "https://bugzilla.redhat.com/1234",
  156. "upstream": False,
  157. "reviewstatus": "ack",
  158. }
  159. output = self.app.post(
  160. "/api/0/test/issue/1/custom", headers=headers, data=payload
  161. )
  162. self.assertEqual(output.status_code, 200)
  163. data = json.loads(output.get_data(as_text=True))
  164. data["messages"].sort(key=lambda d: list(d.keys())[0])
  165. self.assertDictEqual(
  166. data,
  167. {
  168. "messages": [
  169. {
  170. "bugzilla": "Custom field bugzilla adjusted to "
  171. "https://bugzilla.redhat.com/1234"
  172. },
  173. {
  174. "reviewstatus": "Custom field reviewstatus adjusted to ack"
  175. },
  176. {
  177. "upstream": "Custom field upstream adjusted to False (was: True)"
  178. },
  179. ]
  180. },
  181. )
  182. self.session.commit()
  183. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  184. issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
  185. self.assertEqual(len(issue.other_fields), 3)
  186. # Reset the value
  187. payload = {"bugzilla": "", "upstream": "", "reviewstatus": ""}
  188. output = self.app.post(
  189. "/api/0/test/issue/1/custom", headers=headers, data=payload
  190. )
  191. self.assertEqual(output.status_code, 200)
  192. data = json.loads(output.get_data(as_text=True))
  193. data["messages"].sort(key=lambda d: list(d.keys())[0])
  194. self.assertDictEqual(
  195. data,
  196. {
  197. "messages": [
  198. {
  199. "bugzilla": "Custom field bugzilla reset "
  200. "(from https://bugzilla.redhat.com/1234)"
  201. },
  202. {
  203. "reviewstatus": "Custom field reviewstatus reset (from ack)"
  204. },
  205. {"upstream": "Custom field upstream reset (from False)"},
  206. ]
  207. },
  208. )
  209. if __name__ == "__main__":
  210. unittest.main(verbosity=2)