test_pagure_flask_rebase.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. allow_rebase=True,
  58. )
  59. self.session.commit()
  60. self.assertEqual(req.id, 1)
  61. self.assertEqual(req.title, "PR from the test branch")
  62. self.project = pagure.lib.query.get_authorized_project(
  63. self.session, "test"
  64. )
  65. self.assertEqual(len(project.requests), 1)
  66. self.request = self.project.requests[0]
  67. def test_merge_status_merge(self):
  68. """ Test that the PR can be merged with a merge commit. """
  69. user = tests.FakeUser(username="pingou")
  70. with tests.user_set(self.app.application, user):
  71. data = {
  72. "requestid": self.request.uid,
  73. "csrf_token": self.get_csrf(),
  74. }
  75. output = self.app.post("/pv/pull-request/merge", data=data)
  76. self.assertEqual(output.status_code, 200)
  77. data = json.loads(output.get_data(as_text=True))
  78. self.assertEqual(
  79. data,
  80. {
  81. "code": "MERGE",
  82. "message": "The pull-request can be merged with a "
  83. "merge commit",
  84. "short_code": "With merge",
  85. },
  86. )
  87. def test_merge_status_needsrebase(self):
  88. """ Test that the PR is marked as needing a rebase if the project
  89. disables non-fast-forward merges. """
  90. self.project = pagure.lib.query.get_authorized_project(
  91. self.session, "test"
  92. )
  93. settings = self.project.settings
  94. settings["disable_non_fast-forward_merges"] = True
  95. self.project.settings = settings
  96. self.session.add(self.project)
  97. self.session.commit()
  98. user = tests.FakeUser(username="pingou")
  99. with tests.user_set(self.app.application, user):
  100. data = {
  101. "requestid": self.request.uid,
  102. "csrf_token": self.get_csrf(),
  103. }
  104. output = self.app.post("/pv/pull-request/merge", data=data)
  105. self.assertEqual(output.status_code, 200)
  106. data = json.loads(output.get_data(as_text=True))
  107. self.assertEqual(
  108. data,
  109. {
  110. "code": "NEEDSREBASE",
  111. "message": "The pull-request must be rebased before "
  112. "merging",
  113. "short_code": "Needs rebase",
  114. },
  115. )
  116. def test_rebase_task(self):
  117. """ Test the rebase PR task and its outcome. """
  118. pagure.lib.tasks.rebase_pull_request(
  119. "test",
  120. namespace=None,
  121. user=None,
  122. requestid=self.request.id,
  123. user_rebaser="pingou",
  124. )
  125. user = tests.FakeUser(username="pingou")
  126. with tests.user_set(self.app.application, user):
  127. data = {
  128. "requestid": self.request.uid,
  129. "csrf_token": self.get_csrf(),
  130. }
  131. output = self.app.post("/pv/pull-request/merge", data=data)
  132. self.assertEqual(output.status_code, 200)
  133. data = json.loads(output.get_data(as_text=True))
  134. self.assertEqual(
  135. data,
  136. {
  137. "code": "FFORWARD",
  138. "message": "The pull-request can be merged and "
  139. "fast-forwarded",
  140. "short_code": "Ok",
  141. },
  142. )
  143. def test_rebase_api_ui_logged_in(self):
  144. """ Test the rebase PR API endpoint when logged in from the UI and
  145. its outcome. """
  146. user = tests.FakeUser(username="pingou")
  147. with tests.user_set(self.app.application, user):
  148. # Get the merge status first so it's cached and can be refreshed
  149. csrf_token = self.get_csrf()
  150. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  151. output = self.app.post("/pv/pull-request/merge", data=data)
  152. self.assertEqual(output.status_code, 200)
  153. data = json.loads(output.get_data(as_text=True))
  154. self.assertEqual(
  155. data,
  156. {
  157. "code": "MERGE",
  158. "message": "The pull-request can be merged with "
  159. "a merge commit",
  160. "short_code": "With merge",
  161. },
  162. )
  163. output = self.app.post("/api/0/test/pull-request/1/rebase")
  164. self.assertEqual(output.status_code, 200)
  165. data = json.loads(output.get_data(as_text=True))
  166. self.assertEqual(data, {"message": "Pull-request rebased"})
  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": "FFORWARD",
  175. "message": "The pull-request can be merged and "
  176. "fast-forwarded",
  177. "short_code": "Ok",
  178. },
  179. )
  180. output = self.app.get("/test/pull-request/1")
  181. self.assertEqual(output.status_code, 200)
  182. output_text = output.get_data(as_text=True)
  183. self.assertIn("rebased onto", output_text)
  184. repo = pagure.lib.query._get_project(self.session, "test")
  185. self.assertEqual(
  186. repo.requests[0].comments[0].user.username, "pingou"
  187. )
  188. def test_rebase_api_ui_logged_in_different_user(self):
  189. """ Test the rebase PR API endpoint when logged in from the UI and
  190. its outcome. """
  191. # Add 'foo' to the project 'test' so 'foo' can rebase the PR
  192. repo = pagure.lib.query._get_project(self.session, "test")
  193. msg = pagure.lib.query.add_user_to_project(
  194. session=self.session, project=repo, new_user="foo", user="pingou"
  195. )
  196. self.session.commit()
  197. self.assertEqual(msg, "User added")
  198. user = tests.FakeUser(username="foo")
  199. with tests.user_set(self.app.application, user):
  200. # Get the merge status first so it's cached and can be refreshed
  201. csrf_token = self.get_csrf()
  202. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  203. output = self.app.post("/pv/pull-request/merge", data=data)
  204. self.assertEqual(output.status_code, 200)
  205. data = json.loads(output.get_data(as_text=True))
  206. self.assertEqual(
  207. data,
  208. {
  209. "code": "MERGE",
  210. "message": "The pull-request can be merged with "
  211. "a merge commit",
  212. "short_code": "With merge",
  213. },
  214. )
  215. output = self.app.post("/api/0/test/pull-request/1/rebase")
  216. self.assertEqual(output.status_code, 200)
  217. data = json.loads(output.get_data(as_text=True))
  218. self.assertEqual(data, {"message": "Pull-request rebased"})
  219. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  220. output = self.app.post("/pv/pull-request/merge", data=data)
  221. self.assertEqual(output.status_code, 200)
  222. data = json.loads(output.get_data(as_text=True))
  223. self.assertEqual(
  224. data,
  225. {
  226. "code": "FFORWARD",
  227. "message": "The pull-request can be merged and "
  228. "fast-forwarded",
  229. "short_code": "Ok",
  230. },
  231. )
  232. output = self.app.get("/test/pull-request/1")
  233. self.assertEqual(output.status_code, 200)
  234. output_text = output.get_data(as_text=True)
  235. self.assertIn("rebased onto", output_text)
  236. repo = pagure.lib.query._get_project(self.session, "test")
  237. self.assertEqual(repo.requests[0].comments[0].user.username, "foo")
  238. def test_rebase_api_api_logged_in(self):
  239. """ Test the rebase PR API endpoint when using an API token and
  240. its outcome. """
  241. tests.create_tokens(self.session)
  242. tests.create_tokens_acl(self.session)
  243. headers = {"Authorization": "token aaabbbcccddd"}
  244. output = self.app.post(
  245. "/api/0/test/pull-request/1/rebase", headers=headers
  246. )
  247. self.assertEqual(output.status_code, 200)
  248. data = json.loads(output.get_data(as_text=True))
  249. self.assertEqual(data, {"message": "Pull-request rebased"})
  250. user = tests.FakeUser(username="pingou")
  251. with tests.user_set(self.app.application, user):
  252. data = {
  253. "requestid": self.request.uid,
  254. "csrf_token": self.get_csrf(),
  255. }
  256. output = self.app.post("/pv/pull-request/merge", data=data)
  257. self.assertEqual(output.status_code, 200)
  258. data = json.loads(output.get_data(as_text=True))
  259. self.assertEqual(
  260. data,
  261. {
  262. "code": "FFORWARD",
  263. "message": "The pull-request can be merged and "
  264. "fast-forwarded",
  265. "short_code": "Ok",
  266. },
  267. )
  268. def test_rebase_api_conflicts(self):
  269. """ Test the rebase PR API endpoint when logged in from the UI and
  270. its outcome. """
  271. tests.add_content_to_git(
  272. os.path.join(self.path, "repos", "test.git"),
  273. branch="master",
  274. content="foobar baz",
  275. )
  276. user = tests.FakeUser(username="pingou")
  277. with tests.user_set(self.app.application, user):
  278. output = self.app.post("/api/0/test/pull-request/1/rebase")
  279. self.assertEqual(output.status_code, 400)
  280. data = json.loads(output.get_data(as_text=True))
  281. self.assertEqual(
  282. data,
  283. {
  284. "error": "Did not manage to rebase this pull-request",
  285. "error_code": "ENOCODE",
  286. },
  287. )
  288. data = {
  289. "requestid": self.request.uid,
  290. "csrf_token": self.get_csrf(),
  291. }
  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": "CONFLICTS",
  299. "message": "The pull-request cannot be merged due "
  300. "to conflicts",
  301. "short_code": "Conflicts",
  302. },
  303. )
  304. def test_rebase_api_api_logged_in_unknown_project(self):
  305. """ Test the rebase PR API endpoint when the project doesn't exist """
  306. tests.create_tokens(self.session)
  307. tests.create_tokens_acl(self.session)
  308. headers = {"Authorization": "token aaabbbcccddd"}
  309. output = self.app.post(
  310. "/api/0/unknown/pull-request/1/rebase", headers=headers
  311. )
  312. self.assertEqual(output.status_code, 404)
  313. data = json.loads(output.get_data(as_text=True))
  314. self.assertEqual(
  315. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  316. )
  317. def test_rebase_api_api_logged_in_unknown_pr(self):
  318. """ Test the rebase PR API endpoint when the PR doesn't exist """
  319. tests.create_tokens(self.session)
  320. tests.create_tokens_acl(self.session)
  321. headers = {"Authorization": "token aaabbbcccddd"}
  322. output = self.app.post(
  323. "/api/0/test/pull-request/404/rebase", headers=headers
  324. )
  325. self.assertEqual(output.status_code, 404)
  326. data = json.loads(output.get_data(as_text=True))
  327. self.assertEqual(
  328. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  329. )
  330. def test_rebase_api_api_logged_in_unknown_token(self):
  331. """ Test the rebase PR API endpoint with an invalid API token """
  332. tests.create_tokens(self.session)
  333. tests.create_tokens_acl(self.session)
  334. headers = {"Authorization": "token unknown"}
  335. output = self.app.post(
  336. "/api/0/test/pull-request/1/rebase", headers=headers
  337. )
  338. self.assertEqual(output.status_code, 401)
  339. data = json.loads(output.get_data(as_text=True))
  340. self.assertEqual(
  341. data,
  342. {
  343. "error": "Invalid or expired token. Please visit "
  344. "http://localhost.localdomain/settings#nav-api-tab to get "
  345. "or renew your API token.",
  346. "error_code": "EINVALIDTOK",
  347. "errors": "Invalid token",
  348. },
  349. )
  350. class PagureRebaseNotAllowedtests(tests.Modeltests):
  351. """ Tests rebasing pull-request in pagure """
  352. maxDiff = None
  353. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  354. def setUp(self):
  355. """ Set up the environnment, ran before every tests. """
  356. super(PagureRebaseNotAllowedtests, self).setUp()
  357. pagure.config.config["REQUESTS_FOLDER"] = None
  358. tests.create_projects(self.session)
  359. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  360. tests.create_projects_git(
  361. os.path.join(self.path, "requests"), bare=True
  362. )
  363. tests.add_content_to_git(
  364. os.path.join(self.path, "repos", "test.git"),
  365. branch="master",
  366. content="foobarbaz",
  367. filename="testfile",
  368. )
  369. tests.add_content_to_git(
  370. os.path.join(self.path, "repos", "test.git"),
  371. branch="test",
  372. content="foobar",
  373. filename="sources",
  374. )
  375. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  376. # Create a PR for these changes
  377. project = pagure.lib.query.get_authorized_project(self.session, "test")
  378. req = pagure.lib.query.new_pull_request(
  379. session=self.session,
  380. repo_from=project,
  381. branch_from="test",
  382. repo_to=project,
  383. branch_to="master",
  384. title="PR from the test branch",
  385. user="pingou",
  386. allow_rebase=False,
  387. )
  388. self.session.commit()
  389. self.assertEqual(req.id, 1)
  390. self.assertEqual(req.title, "PR from the test branch")
  391. self.project = pagure.lib.query.get_authorized_project(
  392. self.session, "test"
  393. )
  394. self.assertEqual(len(project.requests), 1)
  395. self.request = self.project.requests[0]
  396. def test_rebase_api_ui_logged_in(self):
  397. """ Test the rebase PR API endpoint when logged in from the UI and
  398. its outcome. """
  399. user = tests.FakeUser(username="pingou")
  400. with tests.user_set(self.app.application, user):
  401. # Get the merge status first so it's cached and can be refreshed
  402. csrf_token = self.get_csrf()
  403. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  404. output = self.app.post("/pv/pull-request/merge", data=data)
  405. self.assertEqual(output.status_code, 200)
  406. data = json.loads(output.get_data(as_text=True))
  407. self.assertEqual(
  408. data,
  409. {
  410. "code": "MERGE",
  411. "message": "The pull-request can be merged with "
  412. "a merge commit",
  413. "short_code": "With merge",
  414. },
  415. )
  416. output = self.app.post("/api/0/test/pull-request/1/rebase")
  417. self.assertEqual(output.status_code, 403)
  418. data = json.loads(output.get_data(as_text=True))
  419. self.assertEqual(
  420. data,
  421. {
  422. "error": "You are not authorized to rebase this pull-request",
  423. "error_code": "EREBASENOTALLOWED",
  424. },
  425. )
  426. def test_rebase_api_ui_logged_in_different_user(self):
  427. """ Test the rebase PR API endpoint when logged in from the UI and
  428. its outcome. """
  429. # Add 'foo' to the project 'test' so 'foo' can rebase the PR
  430. repo = pagure.lib.query._get_project(self.session, "test")
  431. msg = pagure.lib.query.add_user_to_project(
  432. session=self.session, project=repo, new_user="foo", user="pingou"
  433. )
  434. self.session.commit()
  435. self.assertEqual(msg, "User added")
  436. user = tests.FakeUser(username="foo")
  437. with tests.user_set(self.app.application, user):
  438. # Get the merge status first so it's cached and can be refreshed
  439. csrf_token = self.get_csrf()
  440. data = {"requestid": self.request.uid, "csrf_token": csrf_token}
  441. output = self.app.post("/pv/pull-request/merge", data=data)
  442. self.assertEqual(output.status_code, 200)
  443. data = json.loads(output.get_data(as_text=True))
  444. self.assertEqual(
  445. data,
  446. {
  447. "code": "MERGE",
  448. "message": "The pull-request can be merged with "
  449. "a merge commit",
  450. "short_code": "With merge",
  451. },
  452. )
  453. output = self.app.post("/api/0/test/pull-request/1/rebase")
  454. self.assertEqual(output.status_code, 403)
  455. data = json.loads(output.get_data(as_text=True))
  456. self.assertEqual(
  457. data,
  458. {
  459. "error": "You are not authorized to rebase this pull-request",
  460. "error_code": "EREBASENOTALLOWED",
  461. },
  462. )
  463. def test_rebase_api_api_logged_in(self):
  464. """ Test the rebase PR API endpoint when using an API token and
  465. its outcome. """
  466. tests.create_tokens(self.session)
  467. tests.create_tokens_acl(self.session)
  468. headers = {"Authorization": "token aaabbbcccddd"}
  469. output = self.app.post(
  470. "/api/0/test/pull-request/1/rebase", headers=headers
  471. )
  472. self.assertEqual(output.status_code, 403)
  473. data = json.loads(output.get_data(as_text=True))
  474. self.assertEqual(
  475. data,
  476. {
  477. "error": "You are not authorized to rebase this pull-request",
  478. "error_code": "EREBASENOTALLOWED",
  479. },
  480. )
  481. if __name__ == "__main__":
  482. unittest.main(verbosity=2)