test_pagure_merge_pr_no_fork.py 10 KB

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