test_pagure_flask_ui_quick_reply.py 8.4 KB

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