test_pagure_flask_rebase.py 11 KB

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