test_pagure_flask_rebase.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. # This should be pingou, but we have some bug that adds the
  203. # rebase message as PR author instead of rebaser
  204. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  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. # This should be bar, but we have some bug that adds the
  265. # rebase message as PR author instead of rebaser
  266. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  267. def test_rebase_api_ui_logged_in_pull_request_author(self):
  268. """ Test the rebase PR API endpoint when logged in from the UI and
  269. its outcome. """
  270. user = tests.FakeUser(username="foo")
  271. with tests.user_set(self.app.application, user):
  272. # Get the merge status first so it's cached and can be refreshed
  273. csrf_token = self.get_csrf()
  274. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  275. output = self.app.post("/pv/pull-request/merge", data=data)
  276. self.assertEqual(output.status_code, 200)
  277. data = json.loads(output.get_data(as_text=True))
  278. self.assertEqual(
  279. data,
  280. {
  281. "code": "MERGE",
  282. "message": "The pull-request can be merged with "
  283. "a merge commit",
  284. "short_code": "With merge",
  285. },
  286. )
  287. output = self.app.post("/api/0/test/pull-request/1/rebase")
  288. self.assertEqual(output.status_code, 200)
  289. data = json.loads(output.get_data(as_text=True))
  290. self.assertEqual(data, {"message": "Pull-request rebased"})
  291. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  292. output = self.app.post("/pv/pull-request/merge", data=data)
  293. self.assertEqual(output.status_code, 200)
  294. data = json.loads(output.get_data(as_text=True))
  295. self.assertEqual(
  296. data,
  297. {
  298. "code": "FFORWARD",
  299. "message": "The pull-request can be merged and "
  300. "fast-forwarded",
  301. "short_code": "Ok",
  302. },
  303. )
  304. output = self.app.get("/test/pull-request/1")
  305. self.assertEqual(output.status_code, 200)
  306. output_text = output.get_data(as_text=True)
  307. self.assertIn("rebased onto", output_text)
  308. repo = pagure.lib.query._get_project(self.session, "test")
  309. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  310. def test_rebase_api_api_logged_in(self):
  311. """ Test the rebase PR API endpoint when using an API token and
  312. its outcome. """
  313. tests.create_tokens(self.session)
  314. tests.create_tokens_acl(self.session)
  315. headers = {"Authorization": "token aaabbbcccddd"}
  316. output = self.app.post(
  317. "/api/0/test/pull-request/1/rebase", headers=headers
  318. )
  319. self.assertEqual(output.status_code, 200)
  320. data = json.loads(output.get_data(as_text=True))
  321. self.assertEqual(data, {"message": "Pull-request rebased"})
  322. user = tests.FakeUser(username="pingou")
  323. with tests.user_set(self.app.application, user):
  324. data = {
  325. "requestid": self.request.uid,
  326. "csrf_token": self.get_csrf(),
  327. }
  328. output = self.app.post("/pv/pull-request/merge", data=data)
  329. self.assertEqual(output.status_code, 200)
  330. data = json.loads(output.get_data(as_text=True))
  331. self.assertEqual(
  332. data,
  333. {
  334. "code": "FFORWARD",
  335. "message": "The pull-request can be merged and "
  336. "fast-forwarded",
  337. "short_code": "Ok",
  338. },
  339. )
  340. def test_rebase_api_conflicts(self):
  341. """ Test the rebase PR API endpoint when logged in from the UI and
  342. its outcome. """
  343. tests.add_content_to_git(
  344. os.path.join(self.path, "repos", "test.git"),
  345. branch="master",
  346. content="foobar baz",
  347. )
  348. user = tests.FakeUser(username="pingou")
  349. with tests.user_set(self.app.application, user):
  350. output = self.app.post("/api/0/test/pull-request/1/rebase")
  351. self.assertEqual(output.status_code, 400)
  352. data = json.loads(output.get_data(as_text=True))
  353. self.assertEqual(
  354. data,
  355. {
  356. "error": "Did not manage to rebase this pull-request",
  357. "error_code": "ENOCODE",
  358. },
  359. )
  360. data = {
  361. "requestid": self.request.uid,
  362. "csrf_token": self.get_csrf(),
  363. }
  364. output = self.app.post("/pv/pull-request/merge", data=data)
  365. self.assertEqual(output.status_code, 200)
  366. data = json.loads(output.get_data(as_text=True))
  367. self.assertEqual(
  368. data,
  369. {
  370. "code": "CONFLICTS",
  371. "message": "The pull-request cannot be merged due "
  372. "to conflicts",
  373. "short_code": "Conflicts",
  374. },
  375. )
  376. def test_rebase_api_api_logged_in_unknown_project(self):
  377. """ Test the rebase PR API endpoint when the project doesn't exist """
  378. tests.create_tokens(self.session)
  379. tests.create_tokens_acl(self.session)
  380. headers = {"Authorization": "token aaabbbcccddd"}
  381. output = self.app.post(
  382. "/api/0/unknown/pull-request/1/rebase", headers=headers
  383. )
  384. self.assertEqual(output.status_code, 404)
  385. data = json.loads(output.get_data(as_text=True))
  386. self.assertEqual(
  387. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  388. )
  389. def test_rebase_api_api_logged_in_unknown_pr(self):
  390. """ Test the rebase PR API endpoint when the PR doesn't exist """
  391. tests.create_tokens(self.session)
  392. tests.create_tokens_acl(self.session)
  393. headers = {"Authorization": "token aaabbbcccddd"}
  394. output = self.app.post(
  395. "/api/0/test/pull-request/404/rebase", headers=headers
  396. )
  397. self.assertEqual(output.status_code, 404)
  398. data = json.loads(output.get_data(as_text=True))
  399. self.assertEqual(
  400. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  401. )
  402. def test_rebase_api_api_logged_in_unknown_token(self):
  403. """ Test the rebase PR API endpoint with an invalid API token """
  404. tests.create_tokens(self.session)
  405. tests.create_tokens_acl(self.session)
  406. headers = {"Authorization": "token unknown"}
  407. output = self.app.post(
  408. "/api/0/test/pull-request/1/rebase", headers=headers
  409. )
  410. self.assertEqual(output.status_code, 401)
  411. data = json.loads(output.get_data(as_text=True))
  412. self.assertEqual(
  413. data,
  414. {
  415. "error": "Invalid or expired token. Please visit "
  416. "http://localhost.localdomain/settings#nav-api-tab to get "
  417. "or renew your API token.",
  418. "error_code": "EINVALIDTOK",
  419. "errors": "Invalid token",
  420. },
  421. )
  422. class PagureRebaseNotAllowedtests(tests.Modeltests):
  423. """ Tests rebasing pull-request in pagure """
  424. maxDiff = None
  425. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  426. def setUp(self):
  427. """ Set up the environnment, ran before every tests. """
  428. super(PagureRebaseNotAllowedtests, self).setUp()
  429. pagure.config.config["REQUESTS_FOLDER"] = None
  430. tests.create_projects(self.session)
  431. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  432. tests.create_projects_git(
  433. os.path.join(self.path, "requests"), bare=True
  434. )
  435. tests.add_content_to_git(
  436. os.path.join(self.path, "repos", "test.git"),
  437. branch="master",
  438. content="foobarbaz",
  439. filename="testfile",
  440. )
  441. project = pagure.lib.query.get_authorized_project(self.session, "test")
  442. # Fork the project
  443. task = pagure.lib.query.fork_project(
  444. session=self.session, user="foo", repo=project
  445. )
  446. self.session.commit()
  447. self.assertEqual(
  448. task.get(),
  449. {
  450. "endpoint": "ui_ns.view_repo",
  451. "repo": "test",
  452. "username": "foo",
  453. "namespace": None,
  454. },
  455. )
  456. tests.add_content_to_git(
  457. os.path.join(self.path, "repos", "forks", "foo", "test.git"),
  458. branch="test",
  459. content="foobar",
  460. filename="sources",
  461. )
  462. fork_repo = pagure.lib.query.get_authorized_project(
  463. self.session, "test", user="foo"
  464. )
  465. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  466. # Create a PR for these changes
  467. project = pagure.lib.query.get_authorized_project(self.session, "test")
  468. req = pagure.lib.query.new_pull_request(
  469. session=self.session,
  470. repo_from=fork_repo,
  471. branch_from="test",
  472. repo_to=project,
  473. branch_to="master",
  474. title="PR from the test branch",
  475. user="foo",
  476. allow_rebase=False,
  477. )
  478. self.session.commit()
  479. self.assertEqual(req.id, 1)
  480. self.assertEqual(req.title, "PR from the test branch")
  481. self.project = pagure.lib.query.get_authorized_project(
  482. self.session, "test"
  483. )
  484. self.assertEqual(len(project.requests), 1)
  485. self.request = self.project.requests[0]
  486. def test_rebase_api_ui_logged_in(self):
  487. """ Test the rebase PR API endpoint when logged in from the UI and
  488. its outcome. """
  489. user = tests.FakeUser(username="pingou")
  490. with tests.user_set(self.app.application, user):
  491. # Get the merge status first so it's cached and can be refreshed
  492. csrf_token = self.get_csrf()
  493. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  494. output = self.app.post("/pv/pull-request/merge", data=data)
  495. self.assertEqual(output.status_code, 200)
  496. data = json.loads(output.get_data(as_text=True))
  497. self.assertEqual(
  498. data,
  499. {
  500. "code": "MERGE",
  501. "message": "The pull-request can be merged with "
  502. "a merge commit",
  503. "short_code": "With merge",
  504. },
  505. )
  506. output = self.app.post("/api/0/test/pull-request/1/rebase")
  507. self.assertEqual(output.status_code, 403)
  508. data = json.loads(output.get_data(as_text=True))
  509. self.assertEqual(
  510. data,
  511. {
  512. "error": "You are not authorized to rebase this pull-request",
  513. "error_code": "EREBASENOTALLOWED",
  514. },
  515. )
  516. # Add pingou to fork repo so he can rebase while allow_rebase = False
  517. fork = pagure.lib.query.get_authorized_project(
  518. self.session, "test", user="foo"
  519. )
  520. msg = pagure.lib.query.add_user_to_project(
  521. session=self.session,
  522. project=fork,
  523. new_user="pingou",
  524. user="foo",
  525. )
  526. self.session.commit()
  527. self.assertEqual(msg, "User added")
  528. output = self.app.post("/api/0/test/pull-request/1/rebase")
  529. self.assertEqual(output.status_code, 200)
  530. data = json.loads(output.get_data(as_text=True))
  531. self.assertEqual(data, {"message": "Pull-request rebased"})
  532. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  533. output = self.app.post("/pv/pull-request/merge", data=data)
  534. self.assertEqual(output.status_code, 200)
  535. data = json.loads(output.get_data(as_text=True))
  536. self.assertEqual(
  537. data,
  538. {
  539. "code": "FFORWARD",
  540. "message": "The pull-request can be merged and "
  541. "fast-forwarded",
  542. "short_code": "Ok",
  543. },
  544. )
  545. output = self.app.get("/test/pull-request/1")
  546. self.assertEqual(output.status_code, 200)
  547. output_text = output.get_data(as_text=True)
  548. self.assertIn("rebased onto", output_text)
  549. repo = pagure.lib.query._get_project(self.session, "test")
  550. # This should be pingou, but we have some bug that adds the
  551. # rebase message as PR author instead of rebaser
  552. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  553. def test_rebase_api_ui_logged_in_different_user(self):
  554. """ Test the rebase PR API endpoint when logged in from the UI and
  555. its outcome. """
  556. # Add 'bar' to the project 'test' so 'bar' can rebase the PR
  557. item = pagure.lib.model.User(
  558. user="bar",
  559. fullname="bar foo",
  560. password=b"foo",
  561. default_email="bar@foo.com",
  562. )
  563. self.session.add(item)
  564. item = pagure.lib.model.UserEmail(user_id=2, email="bar@foo.com")
  565. self.session.add(item)
  566. self.session.commit()
  567. repo = pagure.lib.query._get_project(self.session, "test")
  568. msg = pagure.lib.query.add_user_to_project(
  569. session=self.session, project=repo, new_user="bar", user="pingou"
  570. )
  571. self.session.commit()
  572. self.assertEqual(msg, "User added")
  573. user = tests.FakeUser(username="bar")
  574. with tests.user_set(self.app.application, user):
  575. # Get the merge status first so it's cached and can be refreshed
  576. csrf_token = self.get_csrf()
  577. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  578. output = self.app.post("/pv/pull-request/merge", data=data)
  579. self.assertEqual(output.status_code, 200)
  580. data = json.loads(output.get_data(as_text=True))
  581. self.assertEqual(
  582. data,
  583. {
  584. "code": "MERGE",
  585. "message": "The pull-request can be merged with "
  586. "a merge commit",
  587. "short_code": "With merge",
  588. },
  589. )
  590. output = self.app.post("/api/0/test/pull-request/1/rebase")
  591. self.assertEqual(output.status_code, 403)
  592. data = json.loads(output.get_data(as_text=True))
  593. self.assertEqual(
  594. data,
  595. {
  596. "error": "You are not authorized to rebase this pull-request",
  597. "error_code": "EREBASENOTALLOWED",
  598. },
  599. )
  600. # Add bar to fork repo so he can rebase while allow_rebase = False
  601. fork = pagure.lib.query.get_authorized_project(
  602. self.session, "test", user="foo"
  603. )
  604. msg = pagure.lib.query.add_user_to_project(
  605. session=self.session, project=fork, new_user="bar", user="foo"
  606. )
  607. self.session.commit()
  608. self.assertEqual(msg, "User added")
  609. output = self.app.post("/api/0/test/pull-request/1/rebase")
  610. self.assertEqual(output.status_code, 200)
  611. data = json.loads(output.get_data(as_text=True))
  612. self.assertEqual(data, {"message": "Pull-request rebased"})
  613. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  614. output = self.app.post("/pv/pull-request/merge", data=data)
  615. self.assertEqual(output.status_code, 200)
  616. data = json.loads(output.get_data(as_text=True))
  617. self.assertEqual(
  618. data,
  619. {
  620. "code": "FFORWARD",
  621. "message": "The pull-request can be merged and "
  622. "fast-forwarded",
  623. "short_code": "Ok",
  624. },
  625. )
  626. output = self.app.get("/test/pull-request/1")
  627. self.assertEqual(output.status_code, 200)
  628. output_text = output.get_data(as_text=True)
  629. self.assertIn("rebased onto", output_text)
  630. repo = pagure.lib.query._get_project(self.session, "test")
  631. # This should be bar, but we have some bug that adds the
  632. # rebase message as PR author instead of rebaser
  633. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  634. def test_rebase_api_api_logged_in(self):
  635. """ Test the rebase PR API endpoint when using an API token and
  636. its outcome. """
  637. tests.create_tokens(self.session)
  638. tests.create_tokens_acl(self.session)
  639. headers = {"Authorization": "token aaabbbcccddd"}
  640. output = self.app.post(
  641. "/api/0/test/pull-request/1/rebase", headers=headers
  642. )
  643. self.assertEqual(output.status_code, 403)
  644. data = json.loads(output.get_data(as_text=True))
  645. self.assertEqual(
  646. data,
  647. {
  648. "error": "You are not authorized to rebase this pull-request",
  649. "error_code": "EREBASENOTALLOWED",
  650. },
  651. )
  652. # Add pingou to fork repo so he can rebase while allow_rebase = False
  653. fork = pagure.lib.query.get_authorized_project(
  654. self.session, "test", user="foo"
  655. )
  656. msg = pagure.lib.query.add_user_to_project(
  657. session=self.session, project=fork, new_user="pingou", user="foo"
  658. )
  659. self.session.commit()
  660. self.assertEqual(msg, "User added")
  661. output = self.app.post(
  662. "/api/0/test/pull-request/1/rebase", headers=headers
  663. )
  664. self.assertEqual(output.status_code, 200)
  665. data = json.loads(output.get_data(as_text=True))
  666. self.assertEqual(data, {"message": "Pull-request rebased"})
  667. user = tests.FakeUser(username="pingou")
  668. with tests.user_set(self.app.application, user):
  669. data = {
  670. "requestid": self.request.uid,
  671. "csrf_token": self.get_csrf(),
  672. }
  673. output = self.app.post("/pv/pull-request/merge", data=data)
  674. self.assertEqual(output.status_code, 200)
  675. data = json.loads(output.get_data(as_text=True))
  676. self.assertEqual(
  677. data,
  678. {
  679. "code": "FFORWARD",
  680. "message": "The pull-request can be merged and "
  681. "fast-forwarded",
  682. "short_code": "Ok",
  683. },
  684. )
  685. def test_rebase_api_ui_logged_in_pull_request_author(self):
  686. """ Test the rebase PR API endpoint when logged in from the UI and
  687. its outcome. """
  688. user = tests.FakeUser(username="foo")
  689. with tests.user_set(self.app.application, user):
  690. # Get the merge status first so it's cached and can be refreshed
  691. csrf_token = self.get_csrf()
  692. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  693. output = self.app.post("/pv/pull-request/merge", data=data)
  694. self.assertEqual(output.status_code, 200)
  695. data = json.loads(output.get_data(as_text=True))
  696. self.assertEqual(
  697. data,
  698. {
  699. "code": "MERGE",
  700. "message": "The pull-request can be merged with "
  701. "a merge commit",
  702. "short_code": "With merge",
  703. },
  704. )
  705. output = self.app.post("/api/0/test/pull-request/1/rebase")
  706. self.assertEqual(output.status_code, 200)
  707. data = json.loads(output.get_data(as_text=True))
  708. self.assertEqual(data, {"message": "Pull-request rebased"})
  709. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  710. output = self.app.post("/pv/pull-request/merge", data=data)
  711. self.assertEqual(output.status_code, 200)
  712. data = json.loads(output.get_data(as_text=True))
  713. self.assertEqual(
  714. data,
  715. {
  716. "code": "FFORWARD",
  717. "message": "The pull-request can be merged and "
  718. "fast-forwarded",
  719. "short_code": "Ok",
  720. },
  721. )
  722. output = self.app.get("/test/pull-request/1")
  723. self.assertEqual(output.status_code, 200)
  724. output_text = output.get_data(as_text=True)
  725. self.assertIn("rebased onto", output_text)
  726. repo = pagure.lib.query._get_project(self.session, "test")
  727. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  728. if __name__ == "__main__":
  729. unittest.main(verbosity=2)