test_pagure_flask_ui_plugins_noff.py 9.1 KB

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