test_pagure_flask_rebase.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. import pagure.lib.query
  19. import pagure.lib.tasks
  20. import tests
  21. class PagureRebasetests(tests.Modeltests):
  22. """ Tests rebasing pull-request in pagure """
  23. maxDiff = None
  24. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureRebasetests, self).setUp()
  28. pagure.config.config["REQUESTS_FOLDER"] = None
  29. tests.create_projects(self.session)
  30. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  31. tests.create_projects_git(
  32. os.path.join(self.path, "requests"), bare=True
  33. )
  34. tests.add_content_to_git(
  35. os.path.join(self.path, "repos", "test.git"),
  36. branch="master",
  37. content="foobarbaz",
  38. filename="testfile",
  39. )
  40. tests.add_content_to_git(
  41. os.path.join(self.path, "repos", "test.git"),
  42. branch="test",
  43. content="foobar",
  44. filename="sources",
  45. )
  46. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  47. # Create a PR for these changes
  48. project = pagure.lib.query.get_authorized_project(self.session, "test")
  49. req = pagure.lib.query.new_pull_request(
  50. session=self.session,
  51. repo_from=project,
  52. branch_from="test",
  53. repo_to=project,
  54. branch_to="master",
  55. title="PR from the test branch",
  56. user="pingou",
  57. )
  58. self.session.commit()
  59. self.assertEqual(req.id, 1)
  60. self.assertEqual(req.title, "PR from the test branch")
  61. self.project = pagure.lib.query.get_authorized_project(
  62. self.session, "test"
  63. )
  64. self.assertEqual(len(project.requests), 1)
  65. self.request = self.project.requests[0]
  66. def test_merge_status_merge(self):
  67. """ Test that the PR can be merged with a merge commit. """
  68. user = tests.FakeUser(username="pingou")
  69. with tests.user_set(self.app.application, user):
  70. data = {
  71. "requestid": self.request.uid,
  72. "csrf_token": self.get_csrf(),
  73. }
  74. output = self.app.post("/pv/pull-request/merge", data=data)
  75. self.assertEqual(output.status_code, 200)
  76. data = json.loads(output.get_data(as_text=True))
  77. self.assertEqual(
  78. data,
  79. {
  80. "code": "MERGE",
  81. "message": "The pull-request can be merged with a "
  82. "merge commit",
  83. "short_code": "With merge",
  84. },
  85. )
  86. def test_merge_status_needsrebase(self):
  87. """ Test that the PR is marked as needing a rebase if the project
  88. disables non-fast-forward merges. """
  89. self.project = pagure.lib.query.get_authorized_project(
  90. self.session, "test"
  91. )
  92. settings = self.project.settings
  93. settings["disable_non_fast-forward_merges"] = True
  94. self.project.settings = settings
  95. self.session.add(self.project)
  96. self.session.commit()
  97. user = tests.FakeUser(username="pingou")
  98. with tests.user_set(self.app.application, user):
  99. data = {
  100. "requestid": self.request.uid,
  101. "csrf_token": self.get_csrf(),
  102. }
  103. output = self.app.post("/pv/pull-request/merge", data=data)
  104. self.assertEqual(output.status_code, 200)
  105. data = json.loads(output.get_data(as_text=True))
  106. self.assertEqual(
  107. data,
  108. {
  109. "code": "NEEDSREBASE",
  110. "message": "The pull-request must be rebased before "
  111. "merging",
  112. "short_code": "Needs rebase",
  113. },
  114. )
  115. def test_rebase_task(self):
  116. """ Test the rebase PR task and its outcome. """
  117. pagure.lib.tasks.rebase_pull_request(
  118. "test",
  119. namespace=None,
  120. user=None,
  121. requestid=self.request.id,
  122. user_rebaser="pingou",
  123. )
  124. user = tests.FakeUser(username="pingou")
  125. with tests.user_set(self.app.application, user):
  126. data = {
  127. "requestid": self.request.uid,
  128. "csrf_token": self.get_csrf(),
  129. }
  130. output = self.app.post("/pv/pull-request/merge", data=data)
  131. self.assertEqual(output.status_code, 200)
  132. data = json.loads(output.get_data(as_text=True))
  133. self.assertEqual(
  134. data,
  135. {
  136. "code": "FFORWARD",
  137. "message": "The pull-request can be merged and "
  138. "fast-forwarded",
  139. "short_code": "Ok",
  140. },
  141. )
  142. def test_rebase_api_ui_logged_in(self):
  143. """ Test the rebase PR API endpoint when logged in from the UI and
  144. its outcome. """
  145. user = tests.FakeUser(username="pingou")
  146. with tests.user_set(self.app.application, user):
  147. # Get the merge status first so it's cached and can be refreshed
  148. csrf_token = self.get_csrf()
  149. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  150. output = self.app.post("/pv/pull-request/merge", data=data)
  151. self.assertEqual(output.status_code, 200)
  152. data = json.loads(output.get_data(as_text=True))
  153. self.assertEqual(
  154. data,
  155. {
  156. "code": "MERGE",
  157. "message": "The pull-request can be merged with "
  158. "a merge commit",
  159. "short_code": "With merge",
  160. },
  161. )
  162. output = self.app.post("/api/0/test/pull-request/1/rebase")
  163. self.assertEqual(output.status_code, 200)
  164. data = json.loads(output.get_data(as_text=True))
  165. self.assertEqual(data, {"message": "Pull-request rebased"})
  166. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  167. output = self.app.post("/pv/pull-request/merge", data=data)
  168. self.assertEqual(output.status_code, 200)
  169. data = json.loads(output.get_data(as_text=True))
  170. self.assertEqual(
  171. data,
  172. {
  173. "code": "FFORWARD",
  174. "message": "The pull-request can be merged and "
  175. "fast-forwarded",
  176. "short_code": "Ok",
  177. },
  178. )
  179. output = self.app.get("/test/pull-request/1")
  180. self.assertEqual(output.status_code, 200)
  181. output_text = output.get_data(as_text=True)
  182. self.assertIn("rebased onto", output_text)
  183. repo = pagure.lib.query._get_project(self.session, "test")
  184. self.assertEqual(
  185. repo.requests[0].comments[0].user.username, "pingou"
  186. )
  187. def test_rebase_api_ui_logged_in_different_user(self):
  188. """ Test the rebase PR API endpoint when logged in from the UI and
  189. its outcome. """
  190. # Add 'foo' to the project 'test' so 'foo' can rebase the PR
  191. repo = pagure.lib.query._get_project(self.session, "test")
  192. msg = pagure.lib.query.add_user_to_project(
  193. session=self.session, project=repo, new_user="foo", user="pingou"
  194. )
  195. self.session.commit()
  196. self.assertEqual(msg, "User added")
  197. user = tests.FakeUser(username="foo")
  198. with tests.user_set(self.app.application, user):
  199. # Get the merge status first so it's cached and can be refreshed
  200. csrf_token = self.get_csrf()
  201. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  202. output = self.app.post("/pv/pull-request/merge", data=data)
  203. self.assertEqual(output.status_code, 200)
  204. data = json.loads(output.get_data(as_text=True))
  205. self.assertEqual(
  206. data,
  207. {
  208. "code": "MERGE",
  209. "message": "The pull-request can be merged with "
  210. "a merge commit",
  211. "short_code": "With merge",
  212. },
  213. )
  214. output = self.app.post("/api/0/test/pull-request/1/rebase")
  215. self.assertEqual(output.status_code, 200)
  216. data = json.loads(output.get_data(as_text=True))
  217. self.assertEqual(data, {"message": "Pull-request rebased"})
  218. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  219. output = self.app.post("/pv/pull-request/merge", data=data)
  220. self.assertEqual(output.status_code, 200)
  221. data = json.loads(output.get_data(as_text=True))
  222. self.assertEqual(
  223. data,
  224. {
  225. "code": "FFORWARD",
  226. "message": "The pull-request can be merged and "
  227. "fast-forwarded",
  228. "short_code": "Ok",
  229. },
  230. )
  231. output = self.app.get("/test/pull-request/1")
  232. self.assertEqual(output.status_code, 200)
  233. output_text = output.get_data(as_text=True)
  234. self.assertIn("rebased onto", output_text)
  235. repo = pagure.lib.query._get_project(self.session, "test")
  236. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  237. def test_rebase_api_api_logged_in(self):
  238. """ Test the rebase PR API endpoint when using an API token and
  239. its outcome. """
  240. tests.create_tokens(self.session)
  241. tests.create_tokens_acl(self.session)
  242. headers = {"Authorization": "token aaabbbcccddd"}
  243. output = self.app.post(
  244. "/api/0/test/pull-request/1/rebase", headers=headers
  245. )
  246. self.assertEqual(output.status_code, 200)
  247. data = json.loads(output.get_data(as_text=True))
  248. self.assertEqual(data, {"message": "Pull-request rebased"})
  249. user = tests.FakeUser(username="pingou")
  250. with tests.user_set(self.app.application, user):
  251. data = {
  252. "requestid": self.request.uid,
  253. "csrf_token": self.get_csrf(),
  254. }
  255. output = self.app.post("/pv/pull-request/merge", data=data)
  256. self.assertEqual(output.status_code, 200)
  257. data = json.loads(output.get_data(as_text=True))
  258. self.assertEqual(
  259. data,
  260. {
  261. "code": "FFORWARD",
  262. "message": "The pull-request can be merged and "
  263. "fast-forwarded",
  264. "short_code": "Ok",
  265. },
  266. )
  267. def test_rebase_api_conflicts(self):
  268. """ Test the rebase PR API endpoint when logged in from the UI and
  269. its outcome. """
  270. tests.add_content_to_git(
  271. os.path.join(self.path, "repos", "test.git"),
  272. branch="master",
  273. content="foobar baz",
  274. )
  275. user = tests.FakeUser(username="pingou")
  276. with tests.user_set(self.app.application, user):
  277. output = self.app.post("/api/0/test/pull-request/1/rebase")
  278. self.assertEqual(output.status_code, 400)
  279. data = json.loads(output.get_data(as_text=True))
  280. self.assertEqual(
  281. data,
  282. {
  283. "error": "Did not manage to rebase this pull-request",
  284. "error_code": "ENOCODE",
  285. },
  286. )
  287. data = {
  288. "requestid": self.request.uid,
  289. "csrf_token": self.get_csrf(),
  290. }
  291. output = self.app.post("/pv/pull-request/merge", data=data)
  292. self.assertEqual(output.status_code, 200)
  293. data = json.loads(output.get_data(as_text=True))
  294. self.assertEqual(
  295. data,
  296. {
  297. "code": "CONFLICTS",
  298. "message": "The pull-request cannot be merged due "
  299. "to conflicts",
  300. "short_code": "Conflicts",
  301. },
  302. )
  303. def test_rebase_api_api_logged_in_unknown_project(self):
  304. """ Test the rebase PR API endpoint when the project doesn't exist """
  305. tests.create_tokens(self.session)
  306. tests.create_tokens_acl(self.session)
  307. headers = {"Authorization": "token aaabbbcccddd"}
  308. output = self.app.post(
  309. "/api/0/unknown/pull-request/1/rebase", headers=headers
  310. )
  311. self.assertEqual(output.status_code, 404)
  312. data = json.loads(output.get_data(as_text=True))
  313. self.assertEqual(
  314. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  315. )
  316. def test_rebase_api_api_logged_in_unknown_pr(self):
  317. """ Test the rebase PR API endpoint when the PR doesn't exist """
  318. tests.create_tokens(self.session)
  319. tests.create_tokens_acl(self.session)
  320. headers = {"Authorization": "token aaabbbcccddd"}
  321. output = self.app.post(
  322. "/api/0/test/pull-request/404/rebase", headers=headers
  323. )
  324. self.assertEqual(output.status_code, 404)
  325. data = json.loads(output.get_data(as_text=True))
  326. self.assertEqual(
  327. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  328. )
  329. def test_rebase_api_api_logged_in_unknown_token(self):
  330. """ Test the rebase PR API endpoint with an invalid API token """
  331. tests.create_tokens(self.session)
  332. tests.create_tokens_acl(self.session)
  333. headers = {"Authorization": "token unknown"}
  334. output = self.app.post(
  335. "/api/0/test/pull-request/1/rebase", headers=headers
  336. )
  337. self.assertEqual(output.status_code, 401)
  338. data = json.loads(output.get_data(as_text=True))
  339. self.assertEqual(
  340. data,
  341. {
  342. "error": "Invalid or expired token. Please visit "
  343. "http://localhost.localdomain/settings#api-keys to get "
  344. "or renew your API token.",
  345. "error_code": "EINVALIDTOK",
  346. "errors": "Invalid token",
  347. },
  348. )
  349. if __name__ == "__main__":
  350. unittest.main(verbosity=2)