test_pagure_flask_rebase.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. project = pagure.lib.query.get_authorized_project(self.session, "test")
  41. # Fork the project
  42. task = pagure.lib.query.fork_project(
  43. session=self.session, user="foo", repo=project
  44. )
  45. self.session.commit()
  46. self.assertEqual(
  47. task.get(),
  48. {
  49. "endpoint": "ui_ns.view_repo",
  50. "repo": "test",
  51. "username": "foo",
  52. "namespace": None,
  53. },
  54. )
  55. tests.add_content_to_git(
  56. os.path.join(self.path, "repos", "forks", "foo", "test.git"),
  57. branch="test",
  58. content="foobar",
  59. filename="sources",
  60. )
  61. fork_repo = pagure.lib.query.get_authorized_project(
  62. self.session, "test", user="foo"
  63. )
  64. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  65. # Create a PR for these changes
  66. req = pagure.lib.query.new_pull_request(
  67. session=self.session,
  68. repo_from=fork_repo,
  69. branch_from="test",
  70. repo_to=project,
  71. branch_to="master",
  72. title="PR from the test branch",
  73. user="foo",
  74. allow_rebase=True,
  75. )
  76. self.session.commit()
  77. self.assertEqual(req.id, 1)
  78. self.assertEqual(req.title, "PR from the test branch")
  79. self.project = pagure.lib.query.get_authorized_project(
  80. self.session, "test"
  81. )
  82. self.assertEqual(len(project.requests), 1)
  83. self.request = self.project.requests[0]
  84. def test_merge_status_merge(self):
  85. """ Test that the PR can be merged with a merge commit. """
  86. user = tests.FakeUser(username="pingou")
  87. with tests.user_set(self.app.application, user):
  88. data = {
  89. "requestid": self.request.uid,
  90. "csrf_token": self.get_csrf(),
  91. }
  92. output = self.app.post("/pv/pull-request/merge", data=data)
  93. self.assertEqual(output.status_code, 200)
  94. data = json.loads(output.get_data(as_text=True))
  95. self.assertEqual(
  96. data,
  97. {
  98. "code": "MERGE",
  99. "message": "The pull-request can be merged with a "
  100. "merge commit",
  101. "short_code": "With merge",
  102. },
  103. )
  104. def test_merge_status_needsrebase(self):
  105. """ Test that the PR is marked as needing a rebase if the project
  106. disables non-fast-forward merges. """
  107. self.project = pagure.lib.query.get_authorized_project(
  108. self.session, "test"
  109. )
  110. settings = self.project.settings
  111. settings["disable_non_fast-forward_merges"] = True
  112. self.project.settings = settings
  113. self.session.add(self.project)
  114. self.session.commit()
  115. user = tests.FakeUser(username="pingou")
  116. with tests.user_set(self.app.application, user):
  117. data = {
  118. "requestid": self.request.uid,
  119. "csrf_token": self.get_csrf(),
  120. }
  121. output = self.app.post("/pv/pull-request/merge", data=data)
  122. self.assertEqual(output.status_code, 200)
  123. data = json.loads(output.get_data(as_text=True))
  124. self.assertEqual(
  125. data,
  126. {
  127. "code": "NEEDSREBASE",
  128. "message": "The pull-request must be rebased before "
  129. "merging",
  130. "short_code": "Needs rebase",
  131. },
  132. )
  133. def test_rebase_task(self):
  134. """ Test the rebase PR task and its outcome. """
  135. pagure.lib.tasks.rebase_pull_request(
  136. "test",
  137. namespace=None,
  138. user=None,
  139. requestid=self.request.id,
  140. user_rebaser="pingou",
  141. )
  142. user = tests.FakeUser(username="pingou")
  143. with tests.user_set(self.app.application, user):
  144. data = {
  145. "requestid": self.request.uid,
  146. "csrf_token": self.get_csrf(),
  147. }
  148. output = self.app.post("/pv/pull-request/merge", data=data)
  149. self.assertEqual(output.status_code, 200)
  150. data = json.loads(output.get_data(as_text=True))
  151. self.assertEqual(
  152. data,
  153. {
  154. "code": "FFORWARD",
  155. "message": "The pull-request can be merged and "
  156. "fast-forwarded",
  157. "short_code": "Ok",
  158. },
  159. )
  160. def test_rebase_api_ui_logged_in(self):
  161. """ Test the rebase PR API endpoint when logged in from the UI and
  162. its outcome. """
  163. user = tests.FakeUser(username="pingou")
  164. with tests.user_set(self.app.application, user):
  165. # Get the merge status first so it's cached and can be refreshed
  166. csrf_token = self.get_csrf()
  167. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  168. output = self.app.post("/pv/pull-request/merge", data=data)
  169. self.assertEqual(output.status_code, 200)
  170. data = json.loads(output.get_data(as_text=True))
  171. self.assertEqual(
  172. data,
  173. {
  174. "code": "MERGE",
  175. "message": "The pull-request can be merged with "
  176. "a merge commit",
  177. "short_code": "With merge",
  178. },
  179. )
  180. output = self.app.post("/api/0/test/pull-request/1/rebase")
  181. self.assertEqual(output.status_code, 200)
  182. data = json.loads(output.get_data(as_text=True))
  183. self.assertEqual(data, {"message": "Pull-request rebased"})
  184. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  185. output = self.app.post("/pv/pull-request/merge", data=data)
  186. self.assertEqual(output.status_code, 200)
  187. data = json.loads(output.get_data(as_text=True))
  188. self.assertEqual(
  189. data,
  190. {
  191. "code": "FFORWARD",
  192. "message": "The pull-request can be merged and "
  193. "fast-forwarded",
  194. "short_code": "Ok",
  195. },
  196. )
  197. output = self.app.get("/test/pull-request/1")
  198. self.assertEqual(output.status_code, 200)
  199. output_text = output.get_data(as_text=True)
  200. self.assertIn("rebased onto", output_text)
  201. repo = pagure.lib.query._get_project(self.session, "test")
  202. self.assertEqual(
  203. repo.requests[0].comments[0].user.username, "pingou"
  204. )
  205. def test_rebase_api_ui_logged_in_different_user(self):
  206. """ Test the rebase PR API endpoint when logged in from the UI and
  207. its outcome. """
  208. # Add 'bar' to the project 'test' so 'bar' can rebase the PR
  209. item = pagure.lib.model.User(
  210. user="bar",
  211. fullname="bar foo",
  212. password=b"foo",
  213. default_email="bar@foo.com",
  214. )
  215. self.session.add(item)
  216. item = pagure.lib.model.UserEmail(user_id=2, email="bar@foo.com")
  217. self.session.add(item)
  218. self.session.commit()
  219. repo = pagure.lib.query._get_project(self.session, "test")
  220. msg = pagure.lib.query.add_user_to_project(
  221. session=self.session, project=repo, new_user="bar", user="pingou"
  222. )
  223. self.session.commit()
  224. self.assertEqual(msg, "User added")
  225. user = tests.FakeUser(username="bar")
  226. with tests.user_set(self.app.application, user):
  227. # Get the merge status first so it's cached and can be refreshed
  228. csrf_token = self.get_csrf()
  229. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  230. output = self.app.post("/pv/pull-request/merge", data=data)
  231. self.assertEqual(output.status_code, 200)
  232. data = json.loads(output.get_data(as_text=True))
  233. self.assertEqual(
  234. data,
  235. {
  236. "code": "MERGE",
  237. "message": "The pull-request can be merged with "
  238. "a merge commit",
  239. "short_code": "With merge",
  240. },
  241. )
  242. output = self.app.post("/api/0/test/pull-request/1/rebase")
  243. self.assertEqual(output.status_code, 200)
  244. data = json.loads(output.get_data(as_text=True))
  245. self.assertEqual(data, {"message": "Pull-request rebased"})
  246. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  247. output = self.app.post("/pv/pull-request/merge", data=data)
  248. self.assertEqual(output.status_code, 200)
  249. data = json.loads(output.get_data(as_text=True))
  250. self.assertEqual(
  251. data,
  252. {
  253. "code": "FFORWARD",
  254. "message": "The pull-request can be merged and "
  255. "fast-forwarded",
  256. "short_code": "Ok",
  257. },
  258. )
  259. output = self.app.get("/test/pull-request/1")
  260. self.assertEqual(output.status_code, 200)
  261. output_text = output.get_data(as_text=True)
  262. self.assertIn("rebased onto", output_text)
  263. repo = pagure.lib.query._get_project(self.session, "test")
  264. self.assertEqual(repo.requests[0].comments[0].user.username, "bar")
  265. def test_rebase_api_ui_logged_in_pull_request_author(self):
  266. """ Test the rebase PR API endpoint when logged in from the UI and
  267. its outcome. """
  268. user = tests.FakeUser(username="foo")
  269. with tests.user_set(self.app.application, user):
  270. # Get the merge status first so it's cached and can be refreshed
  271. csrf_token = self.get_csrf()
  272. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  273. output = self.app.post("/pv/pull-request/merge", data=data)
  274. self.assertEqual(output.status_code, 200)
  275. data = json.loads(output.get_data(as_text=True))
  276. self.assertEqual(
  277. data,
  278. {
  279. "code": "MERGE",
  280. "message": "The pull-request can be merged with "
  281. "a merge commit",
  282. "short_code": "With merge",
  283. },
  284. )
  285. output = self.app.post("/api/0/test/pull-request/1/rebase")
  286. self.assertEqual(output.status_code, 200)
  287. data = json.loads(output.get_data(as_text=True))
  288. self.assertEqual(data, {"message": "Pull-request rebased"})
  289. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  290. output = self.app.post("/pv/pull-request/merge", data=data)
  291. self.assertEqual(output.status_code, 200)
  292. data = json.loads(output.get_data(as_text=True))
  293. self.assertEqual(
  294. data,
  295. {
  296. "code": "FFORWARD",
  297. "message": "The pull-request can be merged and "
  298. "fast-forwarded",
  299. "short_code": "Ok",
  300. },
  301. )
  302. output = self.app.get("/test/pull-request/1")
  303. self.assertEqual(output.status_code, 200)
  304. output_text = output.get_data(as_text=True)
  305. self.assertIn("rebased onto", output_text)
  306. repo = pagure.lib.query._get_project(self.session, "test")
  307. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  308. def test_rebase_api_api_logged_in(self):
  309. """ Test the rebase PR API endpoint when using an API token and
  310. its outcome. """
  311. tests.create_tokens(self.session)
  312. tests.create_tokens_acl(self.session)
  313. headers = {"Authorization": "token aaabbbcccddd"}
  314. output = self.app.post(
  315. "/api/0/test/pull-request/1/rebase", headers=headers
  316. )
  317. self.assertEqual(output.status_code, 200)
  318. data = json.loads(output.get_data(as_text=True))
  319. self.assertEqual(data, {"message": "Pull-request rebased"})
  320. user = tests.FakeUser(username="pingou")
  321. with tests.user_set(self.app.application, user):
  322. data = {
  323. "requestid": self.request.uid,
  324. "csrf_token": self.get_csrf(),
  325. }
  326. output = self.app.post("/pv/pull-request/merge", data=data)
  327. self.assertEqual(output.status_code, 200)
  328. data = json.loads(output.get_data(as_text=True))
  329. self.assertEqual(
  330. data,
  331. {
  332. "code": "FFORWARD",
  333. "message": "The pull-request can be merged and "
  334. "fast-forwarded",
  335. "short_code": "Ok",
  336. },
  337. )
  338. def test_rebase_api_conflicts(self):
  339. """ Test the rebase PR API endpoint when logged in from the UI and
  340. its outcome. """
  341. tests.add_content_to_git(
  342. os.path.join(self.path, "repos", "test.git"),
  343. branch="master",
  344. content="foobar baz",
  345. )
  346. user = tests.FakeUser(username="pingou")
  347. with tests.user_set(self.app.application, user):
  348. output = self.app.post("/api/0/test/pull-request/1/rebase")
  349. self.assertEqual(output.status_code, 400)
  350. data = json.loads(output.get_data(as_text=True))
  351. self.assertEqual(
  352. data,
  353. {
  354. "error": "Did not manage to rebase this pull-request",
  355. "error_code": "ENOCODE",
  356. },
  357. )
  358. data = {
  359. "requestid": self.request.uid,
  360. "csrf_token": self.get_csrf(),
  361. }
  362. output = self.app.post("/pv/pull-request/merge", data=data)
  363. self.assertEqual(output.status_code, 200)
  364. data = json.loads(output.get_data(as_text=True))
  365. self.assertEqual(
  366. data,
  367. {
  368. "code": "CONFLICTS",
  369. "message": "The pull-request cannot be merged due "
  370. "to conflicts",
  371. "short_code": "Conflicts",
  372. },
  373. )
  374. def test_rebase_api_api_logged_in_unknown_project(self):
  375. """ Test the rebase PR API endpoint when the project doesn't exist """
  376. tests.create_tokens(self.session)
  377. tests.create_tokens_acl(self.session)
  378. headers = {"Authorization": "token aaabbbcccddd"}
  379. output = self.app.post(
  380. "/api/0/unknown/pull-request/1/rebase", headers=headers
  381. )
  382. self.assertEqual(output.status_code, 404)
  383. data = json.loads(output.get_data(as_text=True))
  384. self.assertEqual(
  385. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  386. )
  387. def test_rebase_api_api_logged_in_unknown_pr(self):
  388. """ Test the rebase PR API endpoint when the PR doesn't exist """
  389. tests.create_tokens(self.session)
  390. tests.create_tokens_acl(self.session)
  391. headers = {"Authorization": "token aaabbbcccddd"}
  392. output = self.app.post(
  393. "/api/0/test/pull-request/404/rebase", headers=headers
  394. )
  395. self.assertEqual(output.status_code, 404)
  396. data = json.loads(output.get_data(as_text=True))
  397. self.assertEqual(
  398. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  399. )
  400. def test_rebase_api_api_logged_in_unknown_token(self):
  401. """ Test the rebase PR API endpoint with an invalid API token """
  402. tests.create_tokens(self.session)
  403. tests.create_tokens_acl(self.session)
  404. headers = {"Authorization": "token unknown"}
  405. output = self.app.post(
  406. "/api/0/test/pull-request/1/rebase", headers=headers
  407. )
  408. self.assertEqual(output.status_code, 401)
  409. data = json.loads(output.get_data(as_text=True))
  410. self.assertEqual(
  411. data,
  412. {
  413. "error": "Invalid or expired token. Please visit "
  414. "http://localhost.localdomain/settings#nav-api-tab to get "
  415. "or renew your API token.",
  416. "error_code": "EINVALIDTOK",
  417. "errors": "Invalid token",
  418. },
  419. )
  420. class PagureRebaseNotAllowedtests(tests.Modeltests):
  421. """ Tests rebasing pull-request in pagure """
  422. maxDiff = None
  423. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  424. def setUp(self):
  425. """ Set up the environnment, ran before every tests. """
  426. super(PagureRebaseNotAllowedtests, self).setUp()
  427. pagure.config.config["REQUESTS_FOLDER"] = None
  428. tests.create_projects(self.session)
  429. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  430. tests.create_projects_git(
  431. os.path.join(self.path, "requests"), bare=True
  432. )
  433. tests.add_content_to_git(
  434. os.path.join(self.path, "repos", "test.git"),
  435. branch="master",
  436. content="foobarbaz",
  437. filename="testfile",
  438. )
  439. project = pagure.lib.query.get_authorized_project(self.session, "test")
  440. # Fork the project
  441. task = pagure.lib.query.fork_project(
  442. session=self.session, user="foo", repo=project
  443. )
  444. self.session.commit()
  445. self.assertEqual(
  446. task.get(),
  447. {
  448. "endpoint": "ui_ns.view_repo",
  449. "repo": "test",
  450. "username": "foo",
  451. "namespace": None,
  452. },
  453. )
  454. tests.add_content_to_git(
  455. os.path.join(self.path, "repos", "forks", "foo", "test.git"),
  456. branch="test",
  457. content="foobar",
  458. filename="sources",
  459. )
  460. fork_repo = pagure.lib.query.get_authorized_project(
  461. self.session, "test", user="foo"
  462. )
  463. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  464. # Create a PR for these changes
  465. project = pagure.lib.query.get_authorized_project(self.session, "test")
  466. req = pagure.lib.query.new_pull_request(
  467. session=self.session,
  468. repo_from=fork_repo,
  469. branch_from="test",
  470. repo_to=project,
  471. branch_to="master",
  472. title="PR from the test branch",
  473. user="foo",
  474. allow_rebase=False,
  475. )
  476. self.session.commit()
  477. self.assertEqual(req.id, 1)
  478. self.assertEqual(req.title, "PR from the test branch")
  479. self.project = pagure.lib.query.get_authorized_project(
  480. self.session, "test"
  481. )
  482. self.assertEqual(len(project.requests), 1)
  483. self.request = self.project.requests[0]
  484. def test_rebase_api_ui_logged_in(self):
  485. """ Test the rebase PR API endpoint when logged in from the UI and
  486. its outcome. """
  487. user = tests.FakeUser(username="pingou")
  488. with tests.user_set(self.app.application, user):
  489. # Get the merge status first so it's cached and can be refreshed
  490. csrf_token = self.get_csrf()
  491. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  492. output = self.app.post("/pv/pull-request/merge", data=data)
  493. self.assertEqual(output.status_code, 200)
  494. data = json.loads(output.get_data(as_text=True))
  495. self.assertEqual(
  496. data,
  497. {
  498. "code": "MERGE",
  499. "message": "The pull-request can be merged with "
  500. "a merge commit",
  501. "short_code": "With merge",
  502. },
  503. )
  504. output = self.app.post("/api/0/test/pull-request/1/rebase")
  505. self.assertEqual(output.status_code, 403)
  506. data = json.loads(output.get_data(as_text=True))
  507. self.assertEqual(
  508. data,
  509. {
  510. "error": "You are not authorized to rebase this pull-request",
  511. "error_code": "EREBASENOTALLOWED",
  512. },
  513. )
  514. # Add pingou to fork repo so he can rebase while allow_rebase = False
  515. fork = pagure.lib.query.get_authorized_project(
  516. self.session, "test", user="foo"
  517. )
  518. msg = pagure.lib.query.add_user_to_project(
  519. session=self.session,
  520. project=fork,
  521. new_user="pingou",
  522. user="foo",
  523. )
  524. self.session.commit()
  525. self.assertEqual(msg, "User added")
  526. output = self.app.post("/api/0/test/pull-request/1/rebase")
  527. self.assertEqual(output.status_code, 200)
  528. data = json.loads(output.get_data(as_text=True))
  529. self.assertEqual(data, {"message": "Pull-request rebased"})
  530. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  531. output = self.app.post("/pv/pull-request/merge", data=data)
  532. self.assertEqual(output.status_code, 200)
  533. data = json.loads(output.get_data(as_text=True))
  534. self.assertEqual(
  535. data,
  536. {
  537. "code": "FFORWARD",
  538. "message": "The pull-request can be merged and "
  539. "fast-forwarded",
  540. "short_code": "Ok",
  541. },
  542. )
  543. output = self.app.get("/test/pull-request/1")
  544. self.assertEqual(output.status_code, 200)
  545. output_text = output.get_data(as_text=True)
  546. self.assertIn("rebased onto", output_text)
  547. repo = pagure.lib.query._get_project(self.session, "test")
  548. self.assertEqual(
  549. repo.requests[0].comments[0].user.username, "pingou"
  550. )
  551. def test_rebase_api_ui_logged_in_different_user(self):
  552. """ Test the rebase PR API endpoint when logged in from the UI and
  553. its outcome. """
  554. # Add 'bar' to the project 'test' so 'bar' can rebase the PR
  555. item = pagure.lib.model.User(
  556. user="bar",
  557. fullname="bar foo",
  558. password=b"foo",
  559. default_email="bar@foo.com",
  560. )
  561. self.session.add(item)
  562. item = pagure.lib.model.UserEmail(user_id=2, email="bar@foo.com")
  563. self.session.add(item)
  564. self.session.commit()
  565. repo = pagure.lib.query._get_project(self.session, "test")
  566. msg = pagure.lib.query.add_user_to_project(
  567. session=self.session, project=repo, new_user="bar", user="pingou"
  568. )
  569. self.session.commit()
  570. self.assertEqual(msg, "User added")
  571. user = tests.FakeUser(username="bar")
  572. with tests.user_set(self.app.application, user):
  573. # Get the merge status first so it's cached and can be refreshed
  574. csrf_token = self.get_csrf()
  575. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  576. output = self.app.post("/pv/pull-request/merge", data=data)
  577. self.assertEqual(output.status_code, 200)
  578. data = json.loads(output.get_data(as_text=True))
  579. self.assertEqual(
  580. data,
  581. {
  582. "code": "MERGE",
  583. "message": "The pull-request can be merged with "
  584. "a merge commit",
  585. "short_code": "With merge",
  586. },
  587. )
  588. output = self.app.post("/api/0/test/pull-request/1/rebase")
  589. self.assertEqual(output.status_code, 403)
  590. data = json.loads(output.get_data(as_text=True))
  591. self.assertEqual(
  592. data,
  593. {
  594. "error": "You are not authorized to rebase this pull-request",
  595. "error_code": "EREBASENOTALLOWED",
  596. },
  597. )
  598. # Add bar to fork repo so he can rebase while allow_rebase = False
  599. fork = pagure.lib.query.get_authorized_project(
  600. self.session, "test", user="foo"
  601. )
  602. msg = pagure.lib.query.add_user_to_project(
  603. session=self.session, project=fork, new_user="bar", user="foo"
  604. )
  605. self.session.commit()
  606. self.assertEqual(msg, "User added")
  607. output = self.app.post("/api/0/test/pull-request/1/rebase")
  608. self.assertEqual(output.status_code, 200)
  609. data = json.loads(output.get_data(as_text=True))
  610. self.assertEqual(data, {"message": "Pull-request rebased"})
  611. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  612. output = self.app.post("/pv/pull-request/merge", data=data)
  613. self.assertEqual(output.status_code, 200)
  614. data = json.loads(output.get_data(as_text=True))
  615. self.assertEqual(
  616. data,
  617. {
  618. "code": "FFORWARD",
  619. "message": "The pull-request can be merged and "
  620. "fast-forwarded",
  621. "short_code": "Ok",
  622. },
  623. )
  624. output = self.app.get("/test/pull-request/1")
  625. self.assertEqual(output.status_code, 200)
  626. output_text = output.get_data(as_text=True)
  627. self.assertIn("rebased onto", output_text)
  628. repo = pagure.lib.query._get_project(self.session, "test")
  629. self.assertEqual(repo.requests[0].comments[0].user.username, "bar")
  630. def test_rebase_api_api_logged_in(self):
  631. """ Test the rebase PR API endpoint when using an API token and
  632. its outcome. """
  633. tests.create_tokens(self.session)
  634. tests.create_tokens_acl(self.session)
  635. headers = {"Authorization": "token aaabbbcccddd"}
  636. output = self.app.post(
  637. "/api/0/test/pull-request/1/rebase", headers=headers
  638. )
  639. self.assertEqual(output.status_code, 403)
  640. data = json.loads(output.get_data(as_text=True))
  641. self.assertEqual(
  642. data,
  643. {
  644. "error": "You are not authorized to rebase this pull-request",
  645. "error_code": "EREBASENOTALLOWED",
  646. },
  647. )
  648. # Add pingou to fork repo so he can rebase while allow_rebase = False
  649. fork = pagure.lib.query.get_authorized_project(
  650. self.session, "test", user="foo"
  651. )
  652. msg = pagure.lib.query.add_user_to_project(
  653. session=self.session, project=fork, new_user="pingou", user="foo"
  654. )
  655. self.session.commit()
  656. self.assertEqual(msg, "User added")
  657. output = self.app.post(
  658. "/api/0/test/pull-request/1/rebase", headers=headers
  659. )
  660. self.assertEqual(output.status_code, 200)
  661. data = json.loads(output.get_data(as_text=True))
  662. self.assertEqual(data, {"message": "Pull-request rebased"})
  663. user = tests.FakeUser(username="pingou")
  664. with tests.user_set(self.app.application, user):
  665. data = {
  666. "requestid": self.request.uid,
  667. "csrf_token": self.get_csrf(),
  668. }
  669. output = self.app.post("/pv/pull-request/merge", data=data)
  670. self.assertEqual(output.status_code, 200)
  671. data = json.loads(output.get_data(as_text=True))
  672. self.assertEqual(
  673. data,
  674. {
  675. "code": "FFORWARD",
  676. "message": "The pull-request can be merged and "
  677. "fast-forwarded",
  678. "short_code": "Ok",
  679. },
  680. )
  681. def test_rebase_api_ui_logged_in_pull_request_author(self):
  682. """ Test the rebase PR API endpoint when logged in from the UI and
  683. its outcome. """
  684. user = tests.FakeUser(username="foo")
  685. with tests.user_set(self.app.application, user):
  686. # Get the merge status first so it's cached and can be refreshed
  687. csrf_token = self.get_csrf()
  688. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  689. output = self.app.post("/pv/pull-request/merge", data=data)
  690. self.assertEqual(output.status_code, 200)
  691. data = json.loads(output.get_data(as_text=True))
  692. self.assertEqual(
  693. data,
  694. {
  695. "code": "MERGE",
  696. "message": "The pull-request can be merged with "
  697. "a merge commit",
  698. "short_code": "With merge",
  699. },
  700. )
  701. output = self.app.post("/api/0/test/pull-request/1/rebase")
  702. self.assertEqual(output.status_code, 200)
  703. data = json.loads(output.get_data(as_text=True))
  704. self.assertEqual(data, {"message": "Pull-request rebased"})
  705. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  706. output = self.app.post("/pv/pull-request/merge", data=data)
  707. self.assertEqual(output.status_code, 200)
  708. data = json.loads(output.get_data(as_text=True))
  709. self.assertEqual(
  710. data,
  711. {
  712. "code": "FFORWARD",
  713. "message": "The pull-request can be merged and "
  714. "fast-forwarded",
  715. "short_code": "Ok",
  716. },
  717. )
  718. output = self.app.get("/test/pull-request/1")
  719. self.assertEqual(output.status_code, 200)
  720. output_text = output.get_data(as_text=True)
  721. self.assertIn("rebased onto", output_text)
  722. repo = pagure.lib.query._get_project(self.session, "test")
  723. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  724. if __name__ == "__main__":
  725. unittest.main(verbosity=2)