test_pagure_flask_api_issue_custom_fields.py 7.5 KB

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