1
0

test_pagure_merge_pr_no_fork.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. import unittest
  9. import sys
  10. import os
  11. import json
  12. from mock import patch, MagicMock
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.config
  16. import pagure.lib.query
  17. import pagure.lib.tasks
  18. import tests
  19. class PagureMergePrNoForkTest(tests.Modeltests):
  20. """ Tests merging a PR in pagure when the fork no longer exists """
  21. maxDiff = None
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureMergePrNoForkTest, self).setUp()
  25. tests.create_projects(self.session)
  26. tests.create_projects_git(
  27. os.path.join(self.path, "repos"),
  28. bare=True
  29. )
  30. tests.add_content_git_repo(
  31. os.path.join(self.path, "repos", "test.git"))
  32. tests.create_projects(
  33. self.session,
  34. is_fork=True,
  35. hook_token_suffix='fork')
  36. tests.create_projects_git(
  37. os.path.join(self.path, "repos", "forks", "pingou"),
  38. bare=True
  39. )
  40. tests.add_content_git_repo(
  41. os.path.join(self.path, "repos", "forks", "pingou", "test.git"))
  42. tests.add_readme_git_repo(
  43. os.path.join(self.path, "repos", "forks", "pingou", "test.git"))
  44. project = pagure.lib.query.get_authorized_project(
  45. self.session, 'test')
  46. fork = pagure.lib.query.get_authorized_project(
  47. self.session,
  48. 'test',
  49. user='pingou',
  50. )
  51. tests.create_tokens(self.session)
  52. tests.create_tokens_acl(self.session)
  53. req = pagure.lib.query.new_pull_request(
  54. session=self.session,
  55. repo_from=fork,
  56. branch_from='master',
  57. repo_to=project,
  58. branch_to='master',
  59. title='test pull-request',
  60. user='pingou',
  61. )
  62. self.session.commit()
  63. self.assertEqual(req.id, 1)
  64. self.assertEqual(req.title, 'test pull-request')
  65. # Assert the PR is open
  66. self.session = pagure.lib.query.create_session(self.dbpath)
  67. project = pagure.lib.query.get_authorized_project(
  68. self.session, 'test')
  69. self.assertEqual(len(project.requests), 1)
  70. self.assertEqual(project.requests[0].status, "Open")
  71. # Check how the PR renders in the API and the UI
  72. output = self.app.get('/api/0/test/pull-request/1')
  73. self.assertEqual(output.status_code, 200)
  74. output = self.app.get('/test/pull-request/1')
  75. self.assertEqual(output.status_code, 200)
  76. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  77. def test_api_pull_request_diffstats(self):
  78. """ Test the api_pull_request_merge method of the flask api. """
  79. # Check the PR stats in the API
  80. output = self.app.get(
  81. '/api/0/test/pull-request/1/diffstats')
  82. self.assertEqual(output.status_code, 200)
  83. data = json.loads(output.get_data(as_text=True))
  84. self.assertDictEqual(
  85. data,
  86. {
  87. "README.rst": {
  88. "lines_added": 16,
  89. "lines_removed": 0,
  90. "new_id": "fb7093d2ba1cf8f80d10b45e4f15b10240727db5",
  91. "old_id": "0000000000000000000000000000000000000000",
  92. "old_path": "README.rst",
  93. "status": "A"
  94. }
  95. }
  96. )
  97. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  98. def test_api_pull_request_diffstats_no_fork(self):
  99. """ Test the api_pull_request_merge method of the flask api. """
  100. pagure.lib.tasks.delete_project(
  101. namespace=None,
  102. name="test",
  103. user="pingou",
  104. action_user="pingou",
  105. )
  106. # Check the PR stats in the API
  107. output = self.app.get(
  108. '/api/0/test/pull-request/1/diffstats')
  109. self.assertEqual(output.status_code, 200)
  110. data = json.loads(output.get_data(as_text=True))
  111. self.assertDictEqual(
  112. data,
  113. {
  114. "README.rst": {
  115. "lines_added": 16,
  116. "lines_removed": 0,
  117. "new_id": "fb7093d2ba1cf8f80d10b45e4f15b10240727db5",
  118. "old_id": "0000000000000000000000000000000000000000",
  119. "old_path": "README.rst",
  120. "status": "A"
  121. }
  122. }
  123. )
  124. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  125. def test_api_pull_request_merge(self):
  126. """ Test the api_pull_request_merge method of the flask api. """
  127. headers = {'Authorization': 'token aaabbbcccddd'}
  128. # Merge PR
  129. output = self.app.post(
  130. '/api/0/test/pull-request/1/merge', headers=headers)
  131. self.assertEqual(output.status_code, 200)
  132. data = json.loads(output.get_data(as_text=True))
  133. self.assertDictEqual(
  134. data,
  135. {"message": "Changes merged!"}
  136. )
  137. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  138. def test_api_pull_request_merge_no_fork(self):
  139. """ Test the api_pull_request_merge method of the flask api. """
  140. pagure.lib.tasks.delete_project(
  141. namespace=None,
  142. name="test",
  143. user="pingou",
  144. action_user="pingou",
  145. )
  146. headers = {'Authorization': 'token aaabbbcccddd'}
  147. # Merge PR
  148. output = self.app.post(
  149. '/api/0/test/pull-request/1/merge', headers=headers)
  150. self.assertEqual(output.status_code, 200)
  151. data = json.loads(output.get_data(as_text=True))
  152. self.assertDictEqual(
  153. data,
  154. {"message": "Changes merged!"}
  155. )
  156. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  157. def test_ui_pull_request_merge(self):
  158. """ Test the api_pull_request_merge method of the flask UI. """
  159. user = tests.FakeUser(username='pingou')
  160. with tests.user_set(self.app.application, user):
  161. data = {
  162. 'csrf_token': self.get_csrf()
  163. }
  164. # Merge PR
  165. output = self.app.post(
  166. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  167. self.assertEqual(output.status_code, 200)
  168. output_text = output.get_data(as_text=True)
  169. self.assertIn(
  170. "<title>Overview - test - Pagure</title>",
  171. output_text
  172. )
  173. self.session = pagure.lib.query.create_session(self.dbpath)
  174. project = pagure.lib.query.get_authorized_project(
  175. self.session, 'test')
  176. self.assertEqual(project.requests[0].status, "Merged")
  177. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  178. def test_ui_pull_request_merge_no_fork(self):
  179. """ Test the api_pull_request_merge method of the flask UI. """
  180. pagure.lib.tasks.delete_project(
  181. namespace=None,
  182. name="test",
  183. user="pingou",
  184. action_user="pingou",
  185. )
  186. user = tests.FakeUser(username='pingou')
  187. with tests.user_set(self.app.application, user):
  188. data = {
  189. 'csrf_token': self.get_csrf()
  190. }
  191. # Merge PR
  192. output = self.app.post(
  193. '/test/pull-request/1/merge', data=data, follow_redirects=True)
  194. self.assertEqual(output.status_code, 200)
  195. output_text = output.get_data(as_text=True)
  196. self.assertIn(
  197. "<title>Overview - test - Pagure</title>",
  198. output_text
  199. )
  200. self.session = pagure.lib.query.create_session(self.dbpath)
  201. project = pagure.lib.query.get_authorized_project(
  202. self.session, 'test')
  203. self.assertEqual(project.requests[0].status, "Merged")
  204. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  205. def test_internal_merge_status(self):
  206. """ Test the api_pull_request_merge method of the flask UI. """
  207. self.session = pagure.lib.query.create_session(self.dbpath)
  208. project = pagure.lib.query.get_authorized_project(
  209. self.session, 'test')
  210. user = tests.FakeUser(username='pingou')
  211. with tests.user_set(self.app.application, user):
  212. data = {
  213. 'csrf_token': self.get_csrf(),
  214. 'requestid': project.requests[0].uid,
  215. }
  216. # Merge PR
  217. output = self.app.post(
  218. '/pv/pull-request/merge', data=data)
  219. self.assertEqual(output.status_code, 200)
  220. data = json.loads(output.get_data(as_text=True))
  221. self.assertDictEqual(
  222. data,
  223. {
  224. u'code': u'FFORWARD',
  225. u'message': u'The pull-request can be merged and '
  226. 'fast-forwarded',
  227. u'short_code': u'Ok'
  228. }
  229. )
  230. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  231. def test_internal_merge_status_no_fork(self):
  232. """ Test the api_pull_request_merge method of the flask UI. """
  233. pagure.lib.tasks.delete_project(
  234. namespace=None,
  235. name="test",
  236. user="pingou",
  237. action_user="pingou",
  238. )
  239. self.session = pagure.lib.query.create_session(self.dbpath)
  240. project = pagure.lib.query.get_authorized_project(
  241. self.session, 'test')
  242. user = tests.FakeUser(username='pingou')
  243. with tests.user_set(self.app.application, user):
  244. data = {
  245. 'csrf_token': self.get_csrf(),
  246. 'requestid': project.requests[0].uid,
  247. }
  248. # Merge PR
  249. output = self.app.post(
  250. '/pv/pull-request/merge', data=data)
  251. self.assertEqual(output.status_code, 200)
  252. data = json.loads(output.get_data(as_text=True))
  253. self.assertDictEqual(
  254. data,
  255. {
  256. u'code': u'FFORWARD',
  257. u'message': u'The pull-request can be merged and '
  258. 'fast-forwarded',
  259. u'short_code': u'Ok'
  260. }
  261. )
  262. if __name__ == '__main__':
  263. unittest.main(verbosity=2)