test_pagure_flask_api_issue_custom_fields.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. """
  2. (c) 2017 - Copyright Red Hat Inc
  3. Authors:
  4. Clement Verna <cverna@tutanota.com>
  5. """
  6. from __future__ import unicode_literals
  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 # noqa: E402
  15. import pagure.lib # 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.get_authorized_project(self.session, 'test')
  30. pagure.lib.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. self.assertEqual(output.status_code, 400)
  49. data = json.loads(output.get_data(as_text=True))
  50. self.assertDictEqual(
  51. data,
  52. {
  53. "error": "Invalid or incomplete input submitted",
  54. "error_code": "EINVALIDREQ",
  55. }
  56. )
  57. def test_api_update_custom_field_wrong_field(self):
  58. """ Test the api_update_custom_field method of the flask api.
  59. This test that an invalid field retruns the correct error.
  60. """
  61. headers = {'Authorization': 'token aaabbbcccddd'}
  62. # Project does not have this custom field
  63. payload = {'foo': 'bar'}
  64. output = self.app.post(
  65. '/api/0/test/issue/1/custom', headers=headers, data=payload)
  66. self.assertEqual(output.status_code, 400)
  67. data = json.loads(output.get_data(as_text=True))
  68. self.assertDictEqual(
  69. data,
  70. {
  71. "error": "Invalid custom field submitted",
  72. "error_code": "EINVALIDISSUEFIELD",
  73. }
  74. )
  75. @patch(
  76. 'pagure.lib.set_custom_key_value',
  77. MagicMock(side_effect=pagure.exceptions.PagureException('error')))
  78. def test_api_update_custom_field_raise_error(self):
  79. """ Test the api_update_custom_field method of the flask api.
  80. This test the successful requests scenarii.
  81. """
  82. headers = {'Authorization': 'token aaabbbcccddd'}
  83. # Set some custom fields
  84. repo = pagure.lib.get_authorized_project(self.session, 'test')
  85. msg = pagure.lib.set_custom_key_fields(
  86. self.session, repo,
  87. ['bugzilla', 'upstream', 'reviewstatus'],
  88. ['link', 'boolean', 'list'],
  89. ['unused data for non-list type', '', 'ack', 'nack', 'needs review'],
  90. [None, None, None])
  91. self.session.commit()
  92. self.assertEqual(msg, 'List of custom fields updated')
  93. payload = {'bugzilla': '', 'upstream': True}
  94. output = self.app.post(
  95. '/api/0/test/issue/1/custom', headers=headers, data=payload)
  96. self.assertEqual(output.status_code, 400)
  97. data = json.loads(output.get_data(as_text=True))
  98. self.assertDictEqual(
  99. data, {u'error': u'error', u'error_code': u'ENOCODE'})
  100. def test_api_update_custom_field(self):
  101. """ Test the api_update_custom_field method of the flask api.
  102. This test the successful requests scenarii.
  103. """
  104. headers = {'Authorization': 'token aaabbbcccddd'}
  105. # Set some custom fields
  106. repo = pagure.lib.get_authorized_project(self.session, 'test')
  107. msg = pagure.lib.set_custom_key_fields(
  108. self.session, repo,
  109. ['bugzilla', 'upstream', 'reviewstatus'],
  110. ['link', 'boolean', 'list'],
  111. ['unused data for non-list type', '', 'ack', 'nack', 'needs review'],
  112. [None, None, None])
  113. self.session.commit()
  114. self.assertEqual(msg, 'List of custom fields updated')
  115. payload = {'bugzilla': '', 'upstream': True}
  116. output = self.app.post(
  117. '/api/0/test/issue/1/custom', headers=headers, data=payload)
  118. self.assertEqual(output.status_code, 200)
  119. data = json.loads(output.get_data(as_text=True))
  120. data["messages"].sort(key=lambda d: list(d.keys())[0])
  121. self.assertDictEqual(
  122. data,
  123. {
  124. "messages": [
  125. {"bugzilla": "No changes"},
  126. {"upstream": "Custom field upstream adjusted to True"},
  127. ]
  128. }
  129. )
  130. self.session.commit()
  131. repo = pagure.lib.get_authorized_project(self.session, 'test')
  132. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  133. self.assertEqual(len(issue.other_fields), 1)
  134. payload = {'bugzilla': 'https://bugzilla.redhat.com/1234',
  135. 'upstream': False,
  136. 'reviewstatus': 'ack'}
  137. output = self.app.post(
  138. '/api/0/test/issue/1/custom', headers=headers,
  139. data=payload)
  140. self.assertEqual(output.status_code, 200)
  141. data = json.loads(output.get_data(as_text=True))
  142. data["messages"].sort(key=lambda d: list(d.keys())[0])
  143. self.assertDictEqual(
  144. data,
  145. {
  146. "messages": [
  147. {"bugzilla": "Custom field bugzilla adjusted to "
  148. "https://bugzilla.redhat.com/1234"},
  149. {"reviewstatus": "Custom field reviewstatus adjusted to ack"},
  150. {"upstream": "Custom field upstream adjusted to False (was: True)"},
  151. ]
  152. }
  153. )
  154. self.session.commit()
  155. repo = pagure.lib.get_authorized_project(self.session, 'test')
  156. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  157. self.assertEqual(len(issue.other_fields), 3)
  158. # Reset the value
  159. payload = {'bugzilla': '', 'upstream': '', 'reviewstatus': ''}
  160. output = self.app.post(
  161. '/api/0/test/issue/1/custom', headers=headers,
  162. data=payload)
  163. self.assertEqual(output.status_code, 200)
  164. data = json.loads(output.get_data(as_text=True))
  165. data["messages"].sort(key=lambda d: list(d.keys())[0])
  166. self.assertDictEqual(
  167. data,
  168. {
  169. "messages": [
  170. {"bugzilla": "Custom field bugzilla reset "
  171. "(from https://bugzilla.redhat.com/1234)"},
  172. {"reviewstatus": "Custom field reviewstatus reset (from ack)"},
  173. {"upstream": "Custom field upstream reset (from False)"},
  174. ]
  175. }
  176. )
  177. if __name__ == '__main__':
  178. unittest.main(verbosity=2)