test_pagure_flask_api_issue_custom_fields.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """
  2. (c) 2017 - Copyright Red Hat Inc
  3. Authors:
  4. Clement Verna <cverna@tutanota.com>
  5. """
  6. import unittest
  7. import sys
  8. import os
  9. import json
  10. sys.path.insert(0, os.path.join(os.path.dirname(
  11. os.path.abspath(__file__)), '..'))
  12. import pagure # noqa: E402
  13. import pagure.lib # noqa: E402
  14. import tests # noqa: E402
  15. class PagureFlaskApiCustomFieldIssuetests(tests.Modeltests):
  16. """ Tests for the flask API of pagure for issue's custom fields """
  17. def setUp(self):
  18. """ Set up the environnment, ran before every tests. """
  19. self.maxDiff = None
  20. super(PagureFlaskApiCustomFieldIssuetests, self).setUp()
  21. pagure.config.config['TICKETS_FOLDER'] = None
  22. tests.create_projects(self.session)
  23. tests.create_projects_git(os.path.join(self.path, 'tickets'))
  24. tests.create_tokens(self.session)
  25. tests.create_tokens_acl(self.session)
  26. # Create normal issue
  27. repo = pagure.lib.get_authorized_project(self.session, 'test')
  28. pagure.lib.new_issue(
  29. session=self.session,
  30. repo=repo,
  31. title='Test issue #1',
  32. content='We should work on this',
  33. user='pingou',
  34. ticketfolder=None,
  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.data)
  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.data)
  67. self.assertDictEqual(
  68. data,
  69. {
  70. "error": "Invalid custom field submitted",
  71. "error_code": "EINVALIDISSUEFIELD",
  72. }
  73. )
  74. def test_api_update_custom_field(self):
  75. """ Test the api_update_custom_field method of the flask api.
  76. This test the successful requests scenarii.
  77. """
  78. headers = {'Authorization': 'token aaabbbcccddd'}
  79. # Set some custom fields
  80. repo = pagure.lib.get_authorized_project(self.session, 'test')
  81. msg = pagure.lib.set_custom_key_fields(
  82. self.session, repo,
  83. ['bugzilla', 'upstream', 'reviewstatus'],
  84. ['link', 'boolean', 'list'],
  85. ['unused data for non-list type', '', 'ack', 'nack', 'needs review'],
  86. [None, None, None])
  87. self.session.commit()
  88. self.assertEqual(msg, 'List of custom fields updated')
  89. payload = {'bugzilla': '', 'upstream': True}
  90. output = self.app.post(
  91. '/api/0/test/issue/1/custom', headers=headers, data=payload)
  92. self.assertEqual(output.status_code, 200)
  93. data = json.loads(output.data)
  94. data["messages"].sort()
  95. self.assertDictEqual(
  96. data,
  97. {
  98. "messages": sorted([
  99. {"bugzilla": "No changes"},
  100. {"upstream": "Custom field upstream adjusted to True"},
  101. ])
  102. }
  103. )
  104. self.session.commit()
  105. repo = pagure.lib.get_authorized_project(self.session, 'test')
  106. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  107. self.assertEqual(len(issue.other_fields), 1)
  108. payload = {'bugzilla': 'https://bugzilla.redhat.com/1234',
  109. 'upstream': False,
  110. 'reviewstatus': 'ack'}
  111. output = self.app.post(
  112. '/api/0/test/issue/1/custom', headers=headers,
  113. data=payload)
  114. self.assertEqual(output.status_code, 200)
  115. data = json.loads(output.data)
  116. data["messages"].sort()
  117. self.assertDictEqual(
  118. data,
  119. {
  120. "messages": sorted([
  121. {"bugzilla": "Custom field bugzilla adjusted to "
  122. "https://bugzilla.redhat.com/1234"},
  123. {"reviewstatus": "Custom field reviewstatus adjusted to ack"},
  124. {"upstream": "Custom field upstream adjusted to False (was: True)"},
  125. ])
  126. }
  127. )
  128. self.session.commit()
  129. repo = pagure.lib.get_authorized_project(self.session, 'test')
  130. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  131. self.assertEqual(len(issue.other_fields), 3)
  132. # Reset the value
  133. payload = {'bugzilla': '', 'upstream': '', 'reviewstatus': ''}
  134. output = self.app.post(
  135. '/api/0/test/issue/1/custom', headers=headers,
  136. data=payload)
  137. self.assertEqual(output.status_code, 200)
  138. data = json.loads(output.data)
  139. data["messages"].sort()
  140. self.assertDictEqual(
  141. data,
  142. {
  143. "messages": sorted([
  144. {"bugzilla": "Custom field bugzilla reset "
  145. "(from https://bugzilla.redhat.com/1234)"},
  146. {"reviewstatus": "Custom field reviewstatus reset (from ack)"},
  147. {"upstream": "Custom field upstream reset (from False)"},
  148. ])
  149. }
  150. )
  151. if __name__ == '__main__':
  152. unittest.main(verbosity=2)