test_pagure_flask_api_issue_custom_fields.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. ticketfolder=None,
  37. private=False,
  38. )
  39. self.session.commit()
  40. def test_api_update_custom_field_bad_request(self):
  41. """ Test the api_update_custom_field method of the flask api.
  42. This test that a badly form request returns the correct error.
  43. """
  44. headers = {'Authorization': 'token aaabbbcccddd'}
  45. # Request is not formated correctly (EMPTY)
  46. payload = {}
  47. output = self.app.post(
  48. '/api/0/test/issue/1/custom', headers=headers, data=payload)
  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. self.assertEqual(output.status_code, 400)
  68. data = json.loads(output.get_data(as_text=True))
  69. self.assertDictEqual(
  70. data,
  71. {
  72. "error": "Invalid custom field submitted",
  73. "error_code": "EINVALIDISSUEFIELD",
  74. }
  75. )
  76. @patch(
  77. 'pagure.lib.set_custom_key_value',
  78. MagicMock(side_effect=pagure.exceptions.PagureException('error')))
  79. def test_api_update_custom_field_raise_error(self):
  80. """ Test the api_update_custom_field method of the flask api.
  81. This test the successful requests scenarii.
  82. """
  83. headers = {'Authorization': 'token aaabbbcccddd'}
  84. # Set some custom fields
  85. repo = pagure.lib.get_authorized_project(self.session, 'test')
  86. msg = pagure.lib.set_custom_key_fields(
  87. self.session, repo,
  88. ['bugzilla', 'upstream', 'reviewstatus'],
  89. ['link', 'boolean', 'list'],
  90. ['unused data for non-list type', '', 'ack', 'nack', 'needs review'],
  91. [None, None, None])
  92. self.session.commit()
  93. self.assertEqual(msg, 'List of custom fields updated')
  94. payload = {'bugzilla': '', 'upstream': True}
  95. output = self.app.post(
  96. '/api/0/test/issue/1/custom', headers=headers, data=payload)
  97. self.assertEqual(output.status_code, 400)
  98. data = json.loads(output.get_data(as_text=True))
  99. self.assertDictEqual(
  100. data, {u'error': u'error', u'error_code': u'ENOCODE'})
  101. def test_api_update_custom_field(self):
  102. """ Test the api_update_custom_field method of the flask api.
  103. This test the successful requests scenarii.
  104. """
  105. headers = {'Authorization': 'token aaabbbcccddd'}
  106. # Set some custom fields
  107. repo = pagure.lib.get_authorized_project(self.session, 'test')
  108. msg = pagure.lib.set_custom_key_fields(
  109. self.session, repo,
  110. ['bugzilla', 'upstream', 'reviewstatus'],
  111. ['link', 'boolean', 'list'],
  112. ['unused data for non-list type', '', 'ack', 'nack', 'needs review'],
  113. [None, None, None])
  114. self.session.commit()
  115. self.assertEqual(msg, 'List of custom fields updated')
  116. payload = {'bugzilla': '', 'upstream': True}
  117. output = self.app.post(
  118. '/api/0/test/issue/1/custom', headers=headers, data=payload)
  119. self.assertEqual(output.status_code, 200)
  120. data = json.loads(output.get_data(as_text=True))
  121. data["messages"].sort(key=lambda d: list(d.keys())[0])
  122. self.assertDictEqual(
  123. data,
  124. {
  125. "messages": [
  126. {"bugzilla": "No changes"},
  127. {"upstream": "Custom field upstream adjusted to True"},
  128. ]
  129. }
  130. )
  131. self.session.commit()
  132. repo = pagure.lib.get_authorized_project(self.session, 'test')
  133. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  134. self.assertEqual(len(issue.other_fields), 1)
  135. payload = {'bugzilla': 'https://bugzilla.redhat.com/1234',
  136. 'upstream': False,
  137. 'reviewstatus': 'ack'}
  138. output = self.app.post(
  139. '/api/0/test/issue/1/custom', headers=headers,
  140. data=payload)
  141. self.assertEqual(output.status_code, 200)
  142. data = json.loads(output.get_data(as_text=True))
  143. data["messages"].sort(key=lambda d: list(d.keys())[0])
  144. self.assertDictEqual(
  145. data,
  146. {
  147. "messages": [
  148. {"bugzilla": "Custom field bugzilla adjusted to "
  149. "https://bugzilla.redhat.com/1234"},
  150. {"reviewstatus": "Custom field reviewstatus adjusted to ack"},
  151. {"upstream": "Custom field upstream adjusted to False (was: True)"},
  152. ]
  153. }
  154. )
  155. self.session.commit()
  156. repo = pagure.lib.get_authorized_project(self.session, 'test')
  157. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  158. self.assertEqual(len(issue.other_fields), 3)
  159. # Reset the value
  160. payload = {'bugzilla': '', 'upstream': '', 'reviewstatus': ''}
  161. output = self.app.post(
  162. '/api/0/test/issue/1/custom', headers=headers,
  163. data=payload)
  164. self.assertEqual(output.status_code, 200)
  165. data = json.loads(output.get_data(as_text=True))
  166. data["messages"].sort(key=lambda d: list(d.keys())[0])
  167. self.assertDictEqual(
  168. data,
  169. {
  170. "messages": [
  171. {"bugzilla": "Custom field bugzilla reset "
  172. "(from https://bugzilla.redhat.com/1234)"},
  173. {"reviewstatus": "Custom field reviewstatus reset (from ack)"},
  174. {"upstream": "Custom field upstream reset (from False)"},
  175. ]
  176. }
  177. )
  178. if __name__ == '__main__':
  179. unittest.main(verbosity=2)