test_pagure_merge_pr_no_fork.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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>Overview - test - Pagure</title>", output_text
  158. )
  159. self.session = pagure.lib.query.create_session(self.dbpath)
  160. project = pagure.lib.query.get_authorized_project(self.session, "test")
  161. self.assertEqual(project.requests[0].status, "Merged")
  162. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  163. def test_ui_pull_request_merge_no_fork(self):
  164. """ Test the api_pull_request_merge method of the flask UI. """
  165. pagure.lib.tasks.delete_project(
  166. namespace=None, name="test", user="pingou", action_user="pingou"
  167. )
  168. user = tests.FakeUser(username="pingou")
  169. with tests.user_set(self.app.application, user):
  170. data = {"csrf_token": self.get_csrf()}
  171. # Merge PR
  172. output = self.app.post(
  173. "/test/pull-request/1/merge", data=data, follow_redirects=True
  174. )
  175. self.assertEqual(output.status_code, 200)
  176. output_text = output.get_data(as_text=True)
  177. self.assertIn(
  178. "<title>Overview - test - Pagure</title>", output_text
  179. )
  180. self.session = pagure.lib.query.create_session(self.dbpath)
  181. project = pagure.lib.query.get_authorized_project(self.session, "test")
  182. self.assertEqual(project.requests[0].status, "Merged")
  183. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  184. def test_internal_merge_status(self):
  185. """ Test the api_pull_request_merge method of the flask UI. """
  186. self.session = pagure.lib.query.create_session(self.dbpath)
  187. project = pagure.lib.query.get_authorized_project(self.session, "test")
  188. user = tests.FakeUser(username="pingou")
  189. with tests.user_set(self.app.application, user):
  190. data = {
  191. "csrf_token": self.get_csrf(),
  192. "requestid": project.requests[0].uid,
  193. }
  194. # Merge PR
  195. output = self.app.post("/pv/pull-request/merge", data=data)
  196. self.assertEqual(output.status_code, 200)
  197. data = json.loads(output.get_data(as_text=True))
  198. self.assertDictEqual(
  199. data,
  200. {
  201. "code": "FFORWARD",
  202. "message": "The pull-request can be merged and "
  203. "fast-forwarded",
  204. "short_code": "Ok",
  205. },
  206. )
  207. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  208. def test_internal_merge_status_no_fork(self):
  209. """ Test the api_pull_request_merge method of the flask UI. """
  210. pagure.lib.tasks.delete_project(
  211. namespace=None, name="test", user="pingou", action_user="pingou"
  212. )
  213. self.session = pagure.lib.query.create_session(self.dbpath)
  214. project = pagure.lib.query.get_authorized_project(self.session, "test")
  215. user = tests.FakeUser(username="pingou")
  216. with tests.user_set(self.app.application, user):
  217. data = {
  218. "csrf_token": self.get_csrf(),
  219. "requestid": project.requests[0].uid,
  220. }
  221. # Merge PR
  222. output = self.app.post("/pv/pull-request/merge", data=data)
  223. self.assertEqual(output.status_code, 200)
  224. data = json.loads(output.get_data(as_text=True))
  225. self.assertDictEqual(
  226. data,
  227. {
  228. "code": "FFORWARD",
  229. "message": "The pull-request can be merged and "
  230. "fast-forwarded",
  231. "short_code": "Ok",
  232. },
  233. )
  234. if __name__ == "__main__":
  235. unittest.main(verbosity=2)