test_pagure_flask_ui_pr_edit.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. # -*- coding: utf-8 -*-
  2. """
  3. Authors:
  4. Julen Landa Alustiza <jlanda@fedoraproject.org>
  5. """
  6. from __future__ import unicode_literals, absolute_import
  7. import sys
  8. import os
  9. sys.path.insert(
  10. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  11. )
  12. import tests
  13. import pagure.lib.query
  14. import pygit2
  15. class PagureFlaskPrEditSimpletests(tests.Modeltests):
  16. def test_pr_edit_no_project(self):
  17. """ Test the edit pull request endpoint """
  18. output = self.app.get("/foo/pull-request/1/edit")
  19. self.assertEqual(output.status_code, 404)
  20. output_text = output.get_data(as_text=True)
  21. self.assertIn(
  22. "<title>Page not found :'( - Pagure</title>", output_text
  23. )
  24. self.assertIn("<h2>Page not found (404)</h2>", output_text)
  25. def test_pr_edit_no_git_repo(self):
  26. """ Test the edit pull request endpoint """
  27. tests.create_projects(self.session)
  28. output = self.app.get("/test/pull-request/1/edit")
  29. self.assertEqual(output.status_code, 404)
  30. output_text = output.get_data(as_text=True)
  31. self.assertIn(
  32. "<title>Page not found :'( - Pagure</title>", output_text
  33. )
  34. self.assertIn("<p>No git repo found</p>", output_text)
  35. def test_pr_edit_no_pull_requests_no_login(self):
  36. """ Test the edit pull request endpoint """
  37. tests.create_projects(self.session)
  38. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  39. output = self.app.get("/test/pull-request/1/edit")
  40. self.assertEqual(output.status_code, 302)
  41. def test_pr_edit_no_pull_requests(self):
  42. """ Test the edit pull request endpoint """
  43. tests.create_projects(self.session)
  44. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  45. user = tests.FakeUser()
  46. with tests.user_set(self.app.application, user):
  47. output = self.app.get("/test/pull-request/1/edit")
  48. self.assertEqual(output.status_code, 404)
  49. output_text = output.get_data(as_text=True)
  50. self.assertIn(
  51. "<title>Page not found :'( - Pagure</title>", output_text
  52. )
  53. self.assertIn("<p>Pull-request not found</p>", output_text)
  54. class PagureFlaskPrEdittests(tests.Modeltests):
  55. def setUp(self):
  56. super(PagureFlaskPrEdittests, self).setUp()
  57. tests.create_projects(self.session)
  58. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  59. # Create foo's fork of pingou's test project
  60. item = pagure.lib.model.Project(
  61. user_id=2, # foo
  62. name="test",
  63. description="test project #1",
  64. hook_token="aaabbb",
  65. is_fork=True,
  66. parent_id=1,
  67. )
  68. self.session.add(item)
  69. self.session.commit()
  70. # Create the fork's git repo
  71. repo_path = os.path.join(self.path, "repos", item.path)
  72. pygit2.init_repository(repo_path, bare=True)
  73. project = pagure.lib.query.get_authorized_project(self.session, "test")
  74. fork = pagure.lib.query.get_authorized_project(
  75. self.session, "test", user="foo"
  76. )
  77. tests.add_pull_request_git_repo(
  78. self.path,
  79. self.session,
  80. project,
  81. fork,
  82. user="foo",
  83. allow_rebase=True,
  84. )
  85. def tearDown(self):
  86. try:
  87. tests.clean_pull_requests_path()
  88. except:
  89. pass
  90. super(PagureFlaskPrEdittests, self).tearDown()
  91. def test_pr_edit_pull_request_unauthenticated(self):
  92. output = self.app.get("/test/pull-request/1/edit")
  93. self.assertEqual(output.status_code, 302)
  94. def test_pr_edit_pull_request_unauthorized(self):
  95. user = tests.FakeUser()
  96. with tests.user_set(self.app.application, user):
  97. output = self.app.get("/test/pull-request/1/edit")
  98. self.assertEqual(output.status_code, 403)
  99. output_text = output.get_data(as_text=True)
  100. self.assertIn("<title>403 Forbidden</title>", output_text)
  101. self.assertIn(
  102. "<p>You are not allowed to edit this pull-request</p>",
  103. output_text,
  104. )
  105. def test_pr_edit_pull_request_view_author(self):
  106. user = tests.FakeUser(username="foo")
  107. with tests.user_set(self.app.application, user):
  108. output = self.app.get("/test/pull-request/1/edit")
  109. self.assertEqual(output.status_code, 200)
  110. output_text = output.get_data(as_text=True)
  111. # Author is editing PR #1
  112. self.assertIn(
  113. "<title>Edit PR#1: PR from the feature branch - test - "
  114. "Pagure</title>",
  115. output_text,
  116. )
  117. # Author has a title input
  118. if self.get_wtforms_version() >= (2, 2):
  119. self.assertIn(
  120. '<input class="form-control" id="title" name="title" '
  121. 'required type="text" value="PR from the feature branch">',
  122. output_text,
  123. )
  124. else:
  125. self.assertIn(
  126. '<input class="form-control" id="title" name="title" '
  127. 'type="text" value="PR from the feature branch">',
  128. output_text,
  129. )
  130. # Author has an initial_commit textarea
  131. self.assertIn(
  132. '<textarea class="form-control width-100per" '
  133. 'id="initial_comment"\n '
  134. 'name="initial_comment"></textarea>',
  135. output_text,
  136. )
  137. # Author has an non-disabled allow_rebase input
  138. self.assertIn(
  139. '<input id="allow_rebase" name="allow_rebase" '
  140. 'type="checkbox" value="y" checked>',
  141. output_text,
  142. )
  143. def test_pr_edit_pull_request_post_author_no_csrf_token(self):
  144. user = tests.FakeUser(username="foo")
  145. with tests.user_set(self.app.application, user):
  146. data = {
  147. "title": "New title",
  148. "initial_comment": "New initial comment",
  149. "allow_rebase": False,
  150. }
  151. output = self.app.post(
  152. "/test/pull-request/1/edit", data=data, follow_redirects=True
  153. )
  154. self.assertEqual(output.status_code, 200)
  155. output_text = output.get_data(as_text=True)
  156. # Without CSRF token, we finish again on the form with new
  157. # values.
  158. self.assertIn(
  159. "<title>Edit PR#1: PR from the feature branch - test - "
  160. "Pagure</title>",
  161. output_text,
  162. )
  163. if self.get_wtforms_version() >= (2, 2):
  164. self.assertIn(
  165. '<input class="form-control" id="title" name="title" '
  166. 'required type="text" value="New title">',
  167. output_text,
  168. )
  169. else:
  170. self.assertIn(
  171. '<input class="form-control" id="title" name="title" '
  172. 'type="text" value="New title">',
  173. output_text,
  174. )
  175. self.assertIn(
  176. '<textarea class="form-control width-100per" '
  177. 'id="initial_comment"\n '
  178. 'name="initial_comment">New initial comment</textarea>',
  179. output_text,
  180. )
  181. self.assertIn(
  182. '<input id="allow_rebase" name="allow_rebase" type="checkbox"'
  183. ' value="y" checked>',
  184. output_text,
  185. )
  186. request = pagure.lib.query.search_pull_requests(
  187. self.session, project_id=1, requestid=1
  188. )
  189. # DB model has not been changed
  190. self.assertEqual("PR from the feature branch", request.title)
  191. self.assertEqual(None, request.initial_comment)
  192. self.assertEqual(True, request.allow_rebase)
  193. def test_pr_edit_pull_request_post_author(self):
  194. user = tests.FakeUser(username="foo")
  195. with tests.user_set(self.app.application, user):
  196. data = {
  197. "title": "New title",
  198. "initial_comment": "New initial comment",
  199. "allow_rebase": False,
  200. "csrf_token": self.get_csrf(),
  201. }
  202. output = self.app.post(
  203. "/test/pull-request/1/edit", data=data, follow_redirects=True
  204. )
  205. self.assertEqual(output.status_code, 200)
  206. output_text = output.get_data(as_text=True)
  207. # After successful edit, we end on pull_request view with new data
  208. self.assertIn(
  209. "<title>PR#1: New title - test\n - Pagure</title>", output_text
  210. )
  211. self.assertIn(
  212. '<span class="font-weight-bold">\n'
  213. " New title\n"
  214. " </span>",
  215. output_text,
  216. )
  217. self.assertIn("<p>New initial comment</p>", output_text)
  218. request = pagure.lib.query.search_pull_requests(
  219. self.session, project_id=1, requestid=1
  220. )
  221. # DB model has been changed
  222. self.assertEqual("New title", request.title)
  223. self.assertEqual("New initial comment", request.initial_comment)
  224. self.assertEqual(False, request.allow_rebase)
  225. def test_pr_edit_pull_request_view_committer(self):
  226. user = tests.FakeUser(username="pingou")
  227. with tests.user_set(self.app.application, user):
  228. output = self.app.get("/test/pull-request/1/edit")
  229. self.assertEqual(output.status_code, 200)
  230. output_text = output.get_data(as_text=True)
  231. # Committer is editing PR #1
  232. self.assertIn(
  233. "<title>Edit PR#1: PR from the feature branch - test - "
  234. "Pagure</title>",
  235. output_text,
  236. )
  237. # Committer has a title input
  238. if self.get_wtforms_version() >= (2, 2):
  239. self.assertIn(
  240. '<input class="form-control" id="title" name="title" '
  241. 'required type="text" value="PR from the feature branch">',
  242. output_text,
  243. )
  244. else:
  245. self.assertIn(
  246. '<input class="form-control" id="title" name="title" '
  247. 'type="text" value="PR from the feature branch">',
  248. output_text,
  249. )
  250. # Committer has an initial_commit textarea
  251. self.assertIn(
  252. '<textarea class="form-control width-100per" '
  253. 'id="initial_comment"\n'
  254. ' name="initial_comment"></textarea>',
  255. output_text,
  256. )
  257. # Committer has an disabled allow_rebase input
  258. self.assertIn(
  259. '<input id="allow_rebase" name="allow_rebase" type="checkbox"'
  260. ' value="y" checked disabled>',
  261. output_text,
  262. )
  263. def test_pr_edit_pull_request_post_committer(self):
  264. user = tests.FakeUser(username="pingou")
  265. with tests.user_set(self.app.application, user):
  266. data = {
  267. "title": "New title",
  268. "initial_comment": "New initial comment",
  269. "allow_rebase": False,
  270. "csrf_token": self.get_csrf(),
  271. }
  272. output = self.app.post(
  273. "/test/pull-request/1/edit", data=data, follow_redirects=True
  274. )
  275. self.assertEqual(output.status_code, 200)
  276. output_text = output.get_data(as_text=True)
  277. # After successful edit, we end on pull_request view with new data
  278. self.assertIn(
  279. "<title>PR#1: New title - test\n - Pagure</title>", output_text
  280. )
  281. self.assertIn(
  282. '<span class="font-weight-bold">\n'
  283. " New title\n"
  284. " </span>",
  285. output_text,
  286. )
  287. self.assertIn("<p>New initial comment</p>", output_text)
  288. request = pagure.lib.query.search_pull_requests(
  289. self.session, project_id=1, requestid=1
  290. )
  291. # DB model has been changed
  292. self.assertEqual("New title", request.title)
  293. self.assertEqual("New initial comment", request.initial_comment)
  294. # But allow_rebase remains unchanged
  295. self.assertEqual(True, request.allow_rebase)