test_pagure_flask_ui_plugins_noff.py 9.1 KB

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