test_pagure_flask_ui_plugins_noff.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016-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 unittest
  9. import sys
  10. import os
  11. sys.path.insert(0, os.path.join(os.path.dirname(
  12. os.path.abspath(__file__)), '..'))
  13. import tests
  14. class PagureFlaskPluginNoFFtests(tests.SimplePagureTest):
  15. """ Tests for Block non fast-forward pushes plugin of pagure """
  16. def test_plugin_noff(self):
  17. """ Test the noff plugin on/off endpoint. """
  18. tests.create_projects(self.session)
  19. tests.create_projects_git(os.path.join(self.path, 'repos'))
  20. user = tests.FakeUser(username='pingou')
  21. with tests.user_set(self.app.application, user):
  22. output = self.app.get(
  23. '/test/settings/Block non fast-forward pushes')
  24. self.assertEqual(output.status_code, 200)
  25. output_text = output.get_data(as_text=True)
  26. self.assertIn(
  27. '<title>Settings Block non fast-forward pushes - test - '
  28. 'Pagure</title>', output_text)
  29. if self.get_wtforms_version() >= (2, 2):
  30. self.assertIn(
  31. '<input class="form-control pl-0" id="branches" name="branches" '
  32. 'required type="text" value="">', output_text)
  33. else:
  34. self.assertIn(
  35. '<input class="form-control pl-0" id="branches" name="branches" '
  36. 'type="text" value="">', output_text)
  37. self.assertTrue(
  38. '<input class="form-check-input mt-2" id="active" name="active" '
  39. 'type="checkbox" value="y">' in output_text)
  40. csrf_token = output_text.split(
  41. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  42. data = {}
  43. output = self.app.post(
  44. '/test/settings/Block non fast-forward pushes', data=data)
  45. self.assertEqual(output.status_code, 200)
  46. output_text = output.get_data(as_text=True)
  47. self.assertIn(
  48. '<title>Settings Block non fast-forward pushes - test - '
  49. 'Pagure</title>', output_text)
  50. if self.get_wtforms_version() >= (2, 2):
  51. self.assertIn(
  52. '<input class="form-control pl-0" id="branches" name="branches" '
  53. 'required type="text" value="">', output_text)
  54. else:
  55. self.assertIn(
  56. '<input class="form-control pl-0" id="branches" name="branches" '
  57. 'type="text" value="">', output_text)
  58. self.assertTrue(
  59. '<input class="form-check-input mt-2" id="active" name="active" '
  60. 'type="checkbox" value="y">' in output_text)
  61. data['csrf_token'] = csrf_token
  62. # With the git repo
  63. output = self.app.post(
  64. '/test/settings/Block non fast-forward pushes',
  65. data=data, follow_redirects=True)
  66. self.assertEqual(output.status_code, 200)
  67. output_text = output.get_data(as_text=True)
  68. self.assertIn(
  69. '<h5 class="pl-2 font-weight-bold text-muted">'
  70. 'Project Settings</h5>\n', output_text)
  71. self.assertTrue(
  72. 'Hook Block non '
  73. 'fast-forward pushes deactivated' in output_text)
  74. output = self.app.get(
  75. '/test/settings/Block non fast-forward pushes')
  76. self.assertEqual(output.status_code, 200)
  77. output_text = output.get_data(as_text=True)
  78. self.assertIn(
  79. '<title>Settings Block non fast-forward pushes - test - '
  80. 'Pagure</title>', output_text)
  81. if self.get_wtforms_version() >= (2, 2):
  82. self.assertIn(
  83. '<input class="form-control pl-0" id="branches" name="branches" '
  84. 'required type="text" value="">', output_text)
  85. else:
  86. self.assertIn(
  87. '<input class="form-control pl-0" id="branches" name="branches" '
  88. 'type="text" value="">', output_text)
  89. self.assertTrue(
  90. '<input class="form-check-input mt-2" id="active" name="active" '
  91. 'type="checkbox" value="y">' in output_text)
  92. self.assertFalse(os.path.exists(os.path.join(
  93. self.path, 'repos', 'test.git', 'hooks', 'post-receive.mail')))
  94. # Missing the required mail_to
  95. data = {'csrf_token': csrf_token, 'active': 'y'}
  96. output = self.app.post(
  97. '/test/settings/Block non fast-forward pushes',
  98. data=data, follow_redirects=True)
  99. self.assertEqual(output.status_code, 200)
  100. output_text = output.get_data(as_text=True)
  101. self.assertIn(
  102. '<title>Settings Block non fast-forward pushes - test - '
  103. 'Pagure</title>', output_text)
  104. self.assertNotIn(
  105. 'Hook activated',
  106. output_text)
  107. if self.get_wtforms_version() >= (2, 2):
  108. self.assertIn(
  109. '<input class="form-control pl-0" id="branches" name="branches" '
  110. 'required type="text" value="">', output_text)
  111. else:
  112. self.assertIn(
  113. '<input class="form-control pl-0" id="branches" name="branches" '
  114. 'type="text" value="">', output_text)
  115. self.assertTrue(
  116. '<input checked class="form-check-input mt-2" id="active" name="active" '
  117. 'type="checkbox" value="y">' in output_text)
  118. self.assertFalse(os.path.exists(os.path.join(
  119. self.path, 'repos', 'test.git', 'hooks',
  120. 'pre-receive.pagureforcecommit')))
  121. # Activate hook
  122. data = {
  123. 'csrf_token': csrf_token,
  124. 'active': 'y',
  125. 'branches': 'master',
  126. }
  127. output = self.app.post(
  128. '/test/settings/Block non fast-forward pushes',
  129. data=data, follow_redirects=True)
  130. self.assertEqual(output.status_code, 200)
  131. output_text = output.get_data(as_text=True)
  132. self.assertIn(
  133. '<h5 class="pl-2 font-weight-bold text-muted">'
  134. 'Project Settings</h5>\n', output_text)
  135. self.assertIn(
  136. 'Hook Block non '
  137. 'fast-forward pushes activated', output_text)
  138. output = self.app.get(
  139. '/test/settings/Block non fast-forward pushes')
  140. output_text = output.get_data(as_text=True)
  141. self.assertIn(
  142. '<title>Settings Block non fast-forward pushes - test - '
  143. 'Pagure</title>', output_text)
  144. if self.get_wtforms_version() >= (2, 2):
  145. self.assertIn(
  146. '<input class="form-control pl-0" id="branches" name="branches" '
  147. 'required type="text" value="master">', output_text)
  148. else:
  149. self.assertIn(
  150. '<input class="form-control pl-0" id="branches" name="branches" '
  151. 'type="text" value="master">', output_text)
  152. self.assertIn(
  153. '<input checked class="form-check-input mt-2" id="active" name="active" '
  154. 'type="checkbox" value="y">', output_text)
  155. # De-Activate hook
  156. data = {'csrf_token': csrf_token}
  157. output = self.app.post(
  158. '/test/settings/Block non fast-forward pushes',
  159. data=data, follow_redirects=True)
  160. self.assertEqual(output.status_code, 200)
  161. output_text = output.get_data(as_text=True)
  162. self.assertIn(
  163. '<h5 class="pl-2 font-weight-bold text-muted">'
  164. 'Project Settings</h5>\n', output_text)
  165. self.assertIn(
  166. 'Hook Block non '
  167. 'fast-forward pushes deactivated', output_text)
  168. output = self.app.get(
  169. '/test/settings/Block non fast-forward pushes')
  170. output_text = output.get_data(as_text=True)
  171. self.assertIn(
  172. '<title>Settings Block non fast-forward pushes - test - '
  173. 'Pagure</title>', output_text)
  174. if self.get_wtforms_version() >= (2, 2):
  175. self.assertIn(
  176. '<input class="form-control pl-0" id="branches" name="branches" '
  177. 'required type="text" value="">', output_text)
  178. else:
  179. self.assertIn(
  180. '<input class="form-control pl-0" id="branches" name="branches" '
  181. 'type="text" value="">', output_text)
  182. self.assertIn(
  183. '<input class="form-check-input mt-2" id="active" name="active" '
  184. 'type="checkbox" value="y">', output_text)
  185. self.assertFalse(os.path.exists(os.path.join(
  186. self.path, 'repos', 'test.git', 'hooks',
  187. 'pre-receive.pagureforcecommit')))
  188. if __name__ == '__main__':
  189. unittest.main(verbosity=2)