test_pagure_flask_ui_quick_reply.py 8.7 KB

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