test_pagure_flask_ui_plugins_noff.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import pygit2
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskPluginNoFFtests(tests.Modeltests):
  21. """ Tests for Block non fast-forward pushes plugin of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskPluginNoFFtests, self).setUp()
  25. pagure.APP.config['TESTING'] = True
  26. pagure.SESSION = self.session
  27. pagure.ui.SESSION = self.session
  28. pagure.ui.app.SESSION = self.session
  29. pagure.ui.plugins.SESSION = self.session
  30. pagure.ui.repo.SESSION = self.session
  31. pagure.ui.filters.SESSION = self.session
  32. pagure.APP.config['GIT_FOLDER'] = self.path
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. self.path, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. self.path, 'tickets')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. self.path, 'docs')
  39. self.app = pagure.APP.test_client()
  40. def test_plugin_noff(self):
  41. """ Test the noff plugin on/off endpoint. """
  42. tests.create_projects(self.session)
  43. tests.create_projects_git(self.path)
  44. user = tests.FakeUser(username='pingou')
  45. with tests.user_set(pagure.APP, user):
  46. output = self.app.get(
  47. '/test/settings/Block non fast-forward pushes')
  48. self.assertEqual(output.status_code, 200)
  49. self.assertIn(
  50. '<div class="projectinfo m-t-1 m-b-1">\n'
  51. 'test project #1 </div>', output.get_data(as_text=True))
  52. self.assertIn(
  53. '<h3>Block non fast-forward pushes settings</h3>',
  54. output.get_data(as_text=True))
  55. self.assertIn(
  56. '<td><input id="branches" name="branches" type="text" '
  57. 'value=""></td>', output.get_data(as_text=True))
  58. self.assertTrue(
  59. '<input id="active" name="active" type="checkbox" value="y">'
  60. in output.get_data(as_text=True))
  61. csrf_token = output.get_data(as_text=True).split(
  62. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  63. data = {}
  64. output = self.app.post(
  65. '/test/settings/Block non fast-forward pushes', data=data)
  66. self.assertEqual(output.status_code, 200)
  67. self.assertIn(
  68. '<div class="projectinfo m-t-1 m-b-1">\n'
  69. 'test project #1 </div>', output.get_data(as_text=True))
  70. self.assertIn(
  71. '<h3>Block non fast-forward pushes settings</h3>',
  72. output.get_data(as_text=True))
  73. self.assertIn(
  74. '<td><input id="branches" name="branches" type="text" '
  75. 'value=""></td>', output.get_data(as_text=True))
  76. self.assertTrue(
  77. '<input id="active" name="active" type="checkbox" value="y">'
  78. in output.get_data(as_text=True))
  79. data['csrf_token'] = csrf_token
  80. # With the git repo
  81. output = self.app.post(
  82. '/test/settings/Block non fast-forward pushes',
  83. data=data, follow_redirects=True)
  84. self.assertEqual(output.status_code, 200)
  85. self.assertIn(
  86. '<section class="settings">\n <h3>Settings for test</h3>',
  87. output.get_data(as_text=True))
  88. self.assertTrue(
  89. '</button>\n Hook Block non '
  90. 'fast-forward pushes inactived' in output.get_data(as_text=True))
  91. output = self.app.get(
  92. '/test/settings/Block non fast-forward pushes')
  93. self.assertEqual(output.status_code, 200)
  94. self.assertIn(
  95. '<div class="projectinfo m-t-1 m-b-1">\n'
  96. 'test project #1 </div>', output.get_data(as_text=True))
  97. self.assertIn(
  98. '<h3>Block non fast-forward pushes settings</h3>',
  99. output.get_data(as_text=True))
  100. self.assertIn(
  101. '<td><input id="branches" name="branches" type="text" '
  102. 'value=""></td>', output.get_data(as_text=True))
  103. self.assertTrue(
  104. '<input id="active" name="active" type="checkbox" value="y">'
  105. in output.get_data(as_text=True))
  106. self.assertFalse(os.path.exists(os.path.join(
  107. self.path, 'test.git', 'hooks', 'post-receive.mail')))
  108. # Missing the required mail_to
  109. data = {'csrf_token': csrf_token, 'active': 'y'}
  110. output = self.app.post(
  111. '/test/settings/Block non fast-forward pushes',
  112. data=data, follow_redirects=True)
  113. self.assertEqual(output.status_code, 200)
  114. self.assertIn(
  115. '<div class="projectinfo m-t-1 m-b-1">\n'
  116. 'test project #1 </div>', output.get_data(as_text=True))
  117. self.assertIn(
  118. '<h3>Block non fast-forward pushes settings</h3>',
  119. output.get_data(as_text=True))
  120. self.assertNotIn(
  121. '</button>\n Hook activated',
  122. output.get_data(as_text=True))
  123. self.assertIn(
  124. '<td><input id="branches" name="branches" type="text" '
  125. 'value=""></td>', output.get_data(as_text=True))
  126. self.assertTrue(
  127. '<input checked id="active" name="active" type="checkbox" '
  128. 'value="y">' in output.get_data(as_text=True))
  129. self.assertFalse(os.path.exists(os.path.join(
  130. self.path, 'test.git', 'hooks',
  131. 'pre-receive.pagureforcecommit')))
  132. # Activate hook
  133. data = {
  134. 'csrf_token': csrf_token,
  135. 'active': 'y',
  136. 'branches': 'master',
  137. }
  138. output = self.app.post(
  139. '/test/settings/Block non fast-forward pushes',
  140. data=data, follow_redirects=True)
  141. self.assertEqual(output.status_code, 200)
  142. self.assertIn(
  143. '<h3>Settings for test</h3>',
  144. output.get_data(as_text=True))
  145. self.assertIn(
  146. '</button>\n Hook Block non '
  147. 'fast-forward pushes activated', output.get_data(as_text=True))
  148. output = self.app.get(
  149. '/test/settings/Block non fast-forward pushes')
  150. self.assertIn(
  151. '<div class="projectinfo m-t-1 m-b-1">\n'
  152. 'test project #1 </div>', output.get_data(as_text=True))
  153. self.assertIn(
  154. '<h3>Block non fast-forward pushes settings</h3>',
  155. output.get_data(as_text=True))
  156. self.assertIn(
  157. '<td><input id="branches" name="branches" type="text" '
  158. 'value="master"></td>', output.get_data(as_text=True))
  159. self.assertIn(
  160. '<input checked id="active" name="active" type="checkbox" '
  161. 'value="y">', output.get_data(as_text=True))
  162. self.assertTrue(os.path.exists(os.path.join(
  163. self.path, 'test.git', 'hooks',
  164. 'pre-receive.pagureforcecommit')))
  165. # De-Activate hook
  166. data = {'csrf_token': csrf_token}
  167. output = self.app.post(
  168. '/test/settings/Block non fast-forward pushes',
  169. data=data, follow_redirects=True)
  170. self.assertEqual(output.status_code, 200)
  171. self.assertIn(
  172. '<section class="settings">\n <h3>Settings for test</h3>',
  173. output.get_data(as_text=True))
  174. self.assertIn(
  175. '</button>\n Hook Block non '
  176. 'fast-forward pushes inactived', output.get_data(as_text=True))
  177. output = self.app.get(
  178. '/test/settings/Block non fast-forward pushes')
  179. self.assertIn(
  180. '<div class="projectinfo m-t-1 m-b-1">\n'
  181. 'test project #1 </div>', output.get_data(as_text=True))
  182. self.assertIn(
  183. '<h3>Block non fast-forward pushes settings</h3>',
  184. output.get_data(as_text=True))
  185. self.assertIn(
  186. '<td><input id="branches" name="branches" type="text" '
  187. 'value=""></td>', output.get_data(as_text=True))
  188. self.assertIn(
  189. '<input id="active" name="active" type="checkbox" '
  190. 'value="y">', output.get_data(as_text=True))
  191. self.assertFalse(os.path.exists(os.path.join(
  192. self.path, 'test.git', 'hooks',
  193. 'pre-receive.pagureforcecommit')))
  194. if __name__ == '__main__':
  195. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  196. PagureFlaskPluginNoFFtests)
  197. unittest.TextTestRunner(verbosity=2).run(SUITE)