test_pagure_flask_rebase.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. import json
  14. from mock import patch, MagicMock
  15. sys.path.insert(0, os.path.join(os.path.dirname(
  16. os.path.abspath(__file__)), '..'))
  17. import pagure.lib.query
  18. import pagure.lib.tasks
  19. import tests
  20. class PagureRebasetests(tests.Modeltests):
  21. """ Tests rebasing pull-request in pagure """
  22. maxDiff = None
  23. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  24. def setUp(self):
  25. """ Set up the environnment, ran before every tests. """
  26. super(PagureRebasetests, self).setUp()
  27. pagure.config.config['REQUESTS_FOLDER'] = None
  28. tests.create_projects(self.session)
  29. tests.create_projects_git(
  30. os.path.join(self.path, 'repos'), bare=True)
  31. tests.create_projects_git(
  32. os.path.join(self.path, 'requests'), bare=True)
  33. tests.add_content_to_git(
  34. os.path.join(self.path, 'repos', 'test.git'),
  35. branch='test', content="foobar")
  36. tests.add_readme_git_repo(
  37. os.path.join(self.path, 'repos', 'test.git'))
  38. # Create a PR for these changes
  39. project = pagure.lib.query.get_authorized_project(
  40. self.session, 'test')
  41. req = pagure.lib.query.new_pull_request(
  42. session=self.session,
  43. repo_from=project,
  44. branch_from='test',
  45. repo_to=project,
  46. branch_to='master',
  47. title='PR from the test branch',
  48. user='pingou',
  49. )
  50. self.session.commit()
  51. self.assertEqual(req.id, 1)
  52. self.assertEqual(req.title, 'PR from the test branch')
  53. self.project = pagure.lib.query.get_authorized_project(
  54. self.session, 'test')
  55. self.assertEqual(len(project.requests), 1)
  56. self.request = self.project.requests[0]
  57. def test_merge_status_merge(self):
  58. """ Test that the PR can be merged with a merge commit. """
  59. user = tests.FakeUser(username='pingou')
  60. with tests.user_set(self.app.application, user):
  61. data = {'requestid': self.request.uid, 'csrf_token': self.get_csrf()}
  62. output = self.app.post('/pv/pull-request/merge', data=data)
  63. self.assertEqual(output.status_code, 200)
  64. data = json.loads(output.get_data(as_text=True))
  65. self.assertEqual(
  66. data,
  67. {
  68. u'code': u'MERGE',
  69. u'message': u'The pull-request can be merged with a '
  70. u'merge commit',
  71. u'short_code': u'With merge'
  72. }
  73. )
  74. def test_merge_status_needsrebase(self):
  75. """ Test that the PR is marked as needing a rebase if the project
  76. disables non-fast-forward merges. """
  77. self.project = pagure.lib.query.get_authorized_project(
  78. self.session, 'test')
  79. settings = self.project.settings
  80. settings['disable_non_fast-forward_merges'] = True
  81. self.project.settings = settings
  82. self.session.add(self.project)
  83. self.session.commit()
  84. user = tests.FakeUser(username='pingou')
  85. with tests.user_set(self.app.application, user):
  86. data = {'requestid': self.request.uid, 'csrf_token': self.get_csrf()}
  87. output = self.app.post('/pv/pull-request/merge', data=data)
  88. self.assertEqual(output.status_code, 200)
  89. data = json.loads(output.get_data(as_text=True))
  90. self.assertEqual(
  91. data,
  92. {
  93. u'code': u'NEEDSREBASE',
  94. u'message': u'The pull-request must be rebased before '
  95. u'merging',
  96. u'short_code': u'Needs rebase'
  97. }
  98. )
  99. def test_rebase_task(self):
  100. """ Test the rebase PR task and its outcome. """
  101. pagure.lib.tasks.rebase_pull_request(
  102. 'test', namespace=None, user=None, requestid=self.request.id,
  103. user_rebaser='pingou')
  104. user = tests.FakeUser(username='pingou')
  105. with tests.user_set(self.app.application, user):
  106. data = {'requestid': self.request.uid, 'csrf_token': self.get_csrf()}
  107. output = self.app.post('/pv/pull-request/merge', data=data)
  108. self.assertEqual(output.status_code, 200)
  109. data = json.loads(output.get_data(as_text=True))
  110. self.assertEqual(
  111. data,
  112. {
  113. u'code': u'FFORWARD',
  114. u'message': u'The pull-request can be merged and '
  115. u'fast-forwarded',
  116. u'short_code': u'Ok'
  117. }
  118. )
  119. def test_rebase_api_ui_logged_in(self):
  120. """ Test the rebase PR API endpoint when logged in from the UI and
  121. its outcome. """
  122. user = tests.FakeUser(username='pingou')
  123. with tests.user_set(self.app.application, user):
  124. output = self.app.post('/api/0/test/pull-request/1/rebase')
  125. self.assertEqual(output.status_code, 200)
  126. data = json.loads(output.get_data(as_text=True))
  127. self.assertEqual(
  128. data,
  129. {u'message': u'Pull-request rebased'}
  130. )
  131. data = {'requestid': self.request.uid, 'csrf_token': self.get_csrf()}
  132. output = self.app.post('/pv/pull-request/merge', data=data)
  133. self.assertEqual(output.status_code, 200)
  134. data = json.loads(output.get_data(as_text=True))
  135. self.assertEqual(
  136. data,
  137. {
  138. u'code': u'FFORWARD',
  139. u'message': u'The pull-request can be merged and '
  140. u'fast-forwarded',
  141. u'short_code': u'Ok'
  142. }
  143. )
  144. def test_rebase_api_api_logged_in(self):
  145. """ Test the rebase PR API endpoint when using an API token and
  146. its outcome. """
  147. tests.create_tokens(self.session)
  148. tests.create_tokens_acl(self.session)
  149. headers = {'Authorization': 'token aaabbbcccddd'}
  150. output = self.app.post('/api/0/test/pull-request/1/rebase', headers=headers)
  151. self.assertEqual(output.status_code, 200)
  152. data = json.loads(output.get_data(as_text=True))
  153. self.assertEqual(
  154. data,
  155. {u'message': u'Pull-request rebased'}
  156. )
  157. user = tests.FakeUser(username='pingou')
  158. with tests.user_set(self.app.application, user):
  159. data = {'requestid': self.request.uid, 'csrf_token': self.get_csrf()}
  160. output = self.app.post('/pv/pull-request/merge', data=data)
  161. self.assertEqual(output.status_code, 200)
  162. data = json.loads(output.get_data(as_text=True))
  163. self.assertEqual(
  164. data,
  165. {
  166. u'code': u'FFORWARD',
  167. u'message': u'The pull-request can be merged and '
  168. u'fast-forwarded',
  169. u'short_code': u'Ok'
  170. }
  171. )
  172. def test_rebase_api_conflicts(self):
  173. """ Test the rebase PR API endpoint when logged in from the UI and
  174. its outcome. """
  175. tests.add_content_to_git(
  176. os.path.join(self.path, 'repos', 'test.git'),
  177. branch='master', content="foobar baz")
  178. user = tests.FakeUser(username='pingou')
  179. with tests.user_set(self.app.application, user):
  180. output = self.app.post('/api/0/test/pull-request/1/rebase')
  181. self.assertEqual(output.status_code, 400)
  182. data = json.loads(output.get_data(as_text=True))
  183. self.assertEqual(
  184. data,
  185. {
  186. u'error': u'Did not manage to rebase this pull-request',
  187. u'error_code': u'ENOCODE'
  188. }
  189. )
  190. data = {'requestid': self.request.uid, 'csrf_token': self.get_csrf()}
  191. output = self.app.post('/pv/pull-request/merge', data=data)
  192. self.assertEqual(output.status_code, 200)
  193. data = json.loads(output.get_data(as_text=True))
  194. self.assertEqual(
  195. data,
  196. {
  197. u'code': u'CONFLICTS',
  198. u'message': u'The pull-request cannot be merged due '
  199. u'to conflicts',
  200. u'short_code': u'Conflicts'
  201. }
  202. )
  203. def test_rebase_api_api_logged_in_unknown_project(self):
  204. """ Test the rebase PR API endpoint when the project doesn't exist """
  205. tests.create_tokens(self.session)
  206. tests.create_tokens_acl(self.session)
  207. headers = {'Authorization': 'token aaabbbcccddd'}
  208. output = self.app.post('/api/0/unknown/pull-request/1/rebase', headers=headers)
  209. self.assertEqual(output.status_code, 404)
  210. data = json.loads(output.get_data(as_text=True))
  211. self.assertEqual(
  212. data,
  213. {u'error': u'Project not found', u'error_code': u'ENOPROJECT'}
  214. )
  215. def test_rebase_api_api_logged_in_unknown_pr(self):
  216. """ Test the rebase PR API endpoint when the PR doesn't exist """
  217. tests.create_tokens(self.session)
  218. tests.create_tokens_acl(self.session)
  219. headers = {'Authorization': 'token aaabbbcccddd'}
  220. output = self.app.post('/api/0/test/pull-request/404/rebase', headers=headers)
  221. self.assertEqual(output.status_code, 404)
  222. data = json.loads(output.get_data(as_text=True))
  223. self.assertEqual(
  224. data,
  225. {u'error': u'Pull-Request not found', u'error_code': u'ENOREQ'}
  226. )
  227. def test_rebase_api_api_logged_in_unknown_token(self):
  228. """ Test the rebase PR API endpoint with an invalid API token """
  229. tests.create_tokens(self.session)
  230. tests.create_tokens_acl(self.session)
  231. headers = {'Authorization': 'token unknown'}
  232. output = self.app.post('/api/0/test/pull-request/1/rebase', headers=headers)
  233. self.assertEqual(output.status_code, 401)
  234. data = json.loads(output.get_data(as_text=True))
  235. self.assertEqual(
  236. data,
  237. {
  238. u'error': u'Invalid or expired token. Please visit '
  239. 'http://localhost.localdomain/settings#api-keys to get '
  240. 'or renew your API token.',
  241. u'error_code': u'EINVALIDTOK'
  242. }
  243. )
  244. if __name__ == '__main__':
  245. unittest.main(verbosity=2)