1
0

test_pagure_flask_ui_quick_reply.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Lubomír Sedlář <lsedlar@redhat.com>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import mock
  9. import os
  10. import sys
  11. import unittest
  12. import six
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.model
  17. import pagure.lib.plugins
  18. import pagure.lib.query
  19. import pagure.hooks
  20. import tests
  21. class PagureFlaskQuickReplytest(tests.Modeltests):
  22. """ Tests for configuring and displaying quick replies. """
  23. def setUp(self):
  24. """ Set up the environnment, ran before every tests. """
  25. super(PagureFlaskQuickReplytest, self).setUp()
  26. tests.create_projects(self.session)
  27. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  28. self.admin = tests.FakeUser(username="pingou")
  29. self.user = tests.FakeUser(username="ralph")
  30. self.repo = pagure.lib.query._get_project(self.session, "test")
  31. def disable_issues_and_pull_requests(self):
  32. """Disable both issues and pull requests."""
  33. # This can not use direct access as repo.settings is a property that
  34. # serializes data into JSON. Direct modification is not preserved.
  35. settings = self.repo.settings
  36. settings["issue_tracker"] = False
  37. settings["pull_requests"] = False
  38. self.repo.settings = settings
  39. self.session.add(self.repo)
  40. self.session.commit()
  41. def setup_quick_replies(self):
  42. """Create some quick replies.
  43. The full replies are stored as r1 and r2 attributes, with shortened
  44. versions in sr1 and sr2.
  45. """
  46. self.r1 = "Ship it!"
  47. self.r2 = (
  48. "Nah. I would prefer if you did not submit this, as there "
  49. "are problems."
  50. )
  51. self.sr1 = self.r1
  52. self.sr2 = "Nah. I would prefer if you did not submit this, as..."
  53. # Set some quick replies
  54. self.repo.quick_replies = [self.r1, self.r2]
  55. self.session.add(self.repo)
  56. def assertRedirectToSettings(self, output, project="test", notice=None):
  57. """
  58. Check that user was redirected to settings page of a given project
  59. and that a given notice was printed.
  60. """
  61. self.assertEqual(output.status_code, 200)
  62. output_text = output.get_data(as_text=True)
  63. self.assertIn(
  64. "<title>Settings - %s - Pagure</title>" % project, output_text
  65. )
  66. self.assertIn(
  67. '<h5 class="pl-2 font-weight-bold text-muted">'
  68. "Project Settings</h5>\n",
  69. output_text,
  70. )
  71. if notice:
  72. self.assertIn(notice, output_text)
  73. def assertQuickReplies(self, quick_replies, project="test"):
  74. self.session.commit()
  75. repo = pagure.lib.query._get_project(self.session, project)
  76. self.assertEqual(repo.quick_replies, quick_replies)
  77. def assertQuickReplyLinks(self, output):
  78. """Assert reply links created by setup_quick_replies are present."""
  79. link = r'data-qr="%s">\s*%s\s*</a>'
  80. six.assertRegex(
  81. self, output.get_data(as_text=True), link % (self.r1, self.sr1)
  82. )
  83. six.assertRegex(
  84. self, output.get_data(as_text=True), link % (self.r2, self.sr2)
  85. )
  86. def test_new_project_has_none(self):
  87. self.assertQuickReplies([])
  88. def test_update_quick_reply_without_csrf(self):
  89. with tests.user_set(self.app.application, self.admin):
  90. output = self.app.get("/test/settings")
  91. self.assertEqual(output.status_code, 200)
  92. data = {"quick_reply": "Ship it!"}
  93. output = self.app.post(
  94. "/test/update/quick_replies", data=data, follow_redirects=True
  95. )
  96. self.assertRedirectToSettings(output)
  97. self.assertQuickReplies([])
  98. def test_update_quick_replies_single(self):
  99. with tests.user_set(self.app.application, self.admin):
  100. data = {"quick_reply": "Ship it!", "csrf_token": self.get_csrf()}
  101. output = self.app.post(
  102. "/test/update/quick_replies", data=data, follow_redirects=True
  103. )
  104. self.assertRedirectToSettings(
  105. output, notice="quick replies updated"
  106. )
  107. self.assertQuickReplies(["Ship it!"])
  108. self.assertIn(
  109. ">Ship it!</textarea>", output.get_data(as_text=True)
  110. )
  111. def test_update_quick_replies_multiple(self):
  112. with tests.user_set(self.app.application, self.admin):
  113. data = {
  114. "quick_reply": ["Ship it!", "Nah."],
  115. "csrf_token": self.get_csrf(),
  116. }
  117. output = self.app.post(
  118. "/test/update/quick_replies", data=data, follow_redirects=True
  119. )
  120. self.assertRedirectToSettings(
  121. output, notice="quick replies updated"
  122. )
  123. self.assertQuickReplies(["Ship it!", "Nah."])
  124. # Check page has filled in textarea.
  125. self.assertIn(
  126. ">Ship it!</textarea>", output.get_data(as_text=True)
  127. )
  128. self.assertIn(">Nah.</textarea>", output.get_data(as_text=True))
  129. def test_update_quick_replies_empty_to_reset(self):
  130. # Set some quick replies
  131. repo = pagure.lib.query._get_project(self.session, "test")
  132. repo.quick_replies = ["Ship it!", "Nah."]
  133. self.session.add(repo)
  134. self.session.commit()
  135. with tests.user_set(self.app.application, self.admin):
  136. data = {"quick_reply": [], "csrf_token": self.get_csrf()}
  137. output = self.app.post(
  138. "/test/update/quick_replies", data=data, follow_redirects=True
  139. )
  140. self.assertRedirectToSettings(
  141. output, notice="quick replies updated"
  142. )
  143. self.assertQuickReplies([])
  144. def test_update_quick_replies_unprivileged(self):
  145. with tests.user_set(self.app.application, self.user):
  146. data = {"quick_reply": "Ship it!", "csrf_token": "a guess"}
  147. output = self.app.post(
  148. "/test/update/quick_replies", data=data, follow_redirects=True
  149. )
  150. self.assertEqual(output.status_code, 403)
  151. self.assertQuickReplies([])
  152. def test_no_form_with_disabled_issues_and_pull_requests(self):
  153. self.disable_issues_and_pull_requests()
  154. with tests.user_set(self.app.application, self.admin):
  155. output = self.app.get("/test/settings")
  156. self.assertNotIn("Quick replies", output.get_data(as_text=True))
  157. def test_no_submit_with_disabled_issues_and_pull_requests(self):
  158. self.disable_issues_and_pull_requests()
  159. with tests.user_set(self.app.application, self.admin):
  160. data = {"quick_reply": "Ship it!", "csrf_token": "a guess"}
  161. output = self.app.post(
  162. "/test/update/quick_replies", data=data, follow_redirects=True
  163. )
  164. self.assertEqual(output.status_code, 404)
  165. self.assertQuickReplies([])
  166. def test_submit_for_bad_project(self):
  167. with tests.user_set(self.app.application, self.admin):
  168. data = {"quick_reply": "Ship it!", "csrf_token": "a guess"}
  169. output = self.app.post(
  170. "/boom/update/quick_replies", data=data, follow_redirects=True
  171. )
  172. self.assertEqual(output.status_code, 404)
  173. @mock.patch("pagure.lib.git.update_git")
  174. def test_issue_page_has_quick_replies(self, p_ugt):
  175. self.setup_quick_replies()
  176. issue = pagure.lib.query.new_issue(
  177. self.session,
  178. self.repo,
  179. "Dummy issue",
  180. "Just a lonely issue.",
  181. "pingou",
  182. None,
  183. notify=False,
  184. )
  185. with tests.user_set(self.app.application, self.user):
  186. output = self.app.get("/test/issue/%s" % issue.id)
  187. self.assertEqual(output.status_code, 200)
  188. self.assertQuickReplyLinks(output)
  189. @mock.patch("pagure.lib.git.update_git")
  190. @mock.patch("pagure.lib.git.diff_pull_request")
  191. def test_pull_request_page_has_quick_replies(self, diff, p_ugt):
  192. diff.return_value = ([], [])
  193. self.setup_quick_replies()
  194. pr = pagure.lib.query.new_pull_request(
  195. self.session,
  196. "pr",
  197. self.repo,
  198. "master",
  199. "Dummy PR",
  200. "pingou",
  201. None,
  202. repo_from=self.repo,
  203. notify=False,
  204. )
  205. with tests.user_set(self.app.application, self.user):
  206. output = self.app.get("/test/pull-request/%s" % pr.id)
  207. self.assertEqual(output.status_code, 200)
  208. self.assertQuickReplyLinks(output)
  209. if __name__ == "__main__":
  210. unittest.main(verbosity=2)