test_pagure_flask_ui_plugins_noff.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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'] = tests.HERE
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. tests.HERE, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. tests.HERE, 'tickets')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. tests.HERE, '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. user = tests.FakeUser(username='pingou')
  44. with tests.user_set(pagure.APP, user):
  45. output = self.app.get(
  46. '/test/settings/Block non fast-forward pushes')
  47. self.assertEqual(output.status_code, 200)
  48. self.assertIn(
  49. '<div class="projectinfo m-t-1 m-b-1">\n'
  50. 'test project #1 </div>', output.data)
  51. self.assertIn(
  52. '<h3>Block non fast-forward pushes settings</h3>',
  53. output.data)
  54. self.assertIn(
  55. '<td><input id="branches" name="branches" type="text" '
  56. 'value=""></td>', output.data)
  57. self.assertTrue(
  58. '<input id="active" name="active" type="checkbox" value="y">'
  59. in output.data)
  60. csrf_token = output.data.split(
  61. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  62. data = {}
  63. output = self.app.post(
  64. '/test/settings/Block non fast-forward pushes', data=data)
  65. self.assertEqual(output.status_code, 200)
  66. self.assertIn(
  67. '<div class="projectinfo m-t-1 m-b-1">\n'
  68. 'test project #1 </div>', output.data)
  69. self.assertIn(
  70. '<h3>Block non fast-forward pushes settings</h3>',
  71. output.data)
  72. self.assertIn(
  73. '<td><input id="branches" name="branches" type="text" '
  74. 'value=""></td>', output.data)
  75. self.assertTrue(
  76. '<input id="active" name="active" type="checkbox" value="y">'
  77. in output.data)
  78. data['csrf_token'] = csrf_token
  79. # No git found
  80. output = self.app.post(
  81. '/test/settings/Block non fast-forward pushes', data=data)
  82. self.assertEqual(output.status_code, 404)
  83. tests.create_projects_git(tests.HERE)
  84. # With the git repo
  85. output = self.app.post(
  86. '/test/settings/Block non fast-forward pushes',
  87. data=data, follow_redirects=True)
  88. self.assertEqual(output.status_code, 200)
  89. self.assertIn(
  90. '<section class="settings">\n <h3>Settings for test</h3>',
  91. output.data)
  92. self.assertTrue(
  93. '</button>\n Hook Block non '
  94. 'fast-forward pushes inactived' in output.data)
  95. output = self.app.get(
  96. '/test/settings/Block non fast-forward pushes')
  97. self.assertEqual(output.status_code, 200)
  98. self.assertIn(
  99. '<div class="projectinfo m-t-1 m-b-1">\n'
  100. 'test project #1 </div>', output.data)
  101. self.assertIn(
  102. '<h3>Block non fast-forward pushes settings</h3>',
  103. output.data)
  104. self.assertIn(
  105. '<td><input id="branches" name="branches" type="text" '
  106. 'value=""></td>', output.data)
  107. self.assertTrue(
  108. '<input id="active" name="active" type="checkbox" value="y">'
  109. in output.data)
  110. self.assertFalse(os.path.exists(os.path.join(
  111. tests.HERE, 'test.git', 'hooks', 'post-receive.mail')))
  112. # Missing the required mail_to
  113. data = {'csrf_token': csrf_token, 'active': 'y'}
  114. output = self.app.post(
  115. '/test/settings/Block non fast-forward pushes',
  116. data=data, follow_redirects=True)
  117. self.assertEqual(output.status_code, 200)
  118. self.assertIn(
  119. '<div class="projectinfo m-t-1 m-b-1">\n'
  120. 'test project #1 </div>', output.data)
  121. self.assertIn(
  122. '<h3>Block non fast-forward pushes settings</h3>',
  123. output.data)
  124. self.assertNotIn(
  125. '</button>\n Hook activated',
  126. output.data)
  127. self.assertIn(
  128. '<td><input id="branches" name="branches" type="text" '
  129. 'value=""></td>', output.data)
  130. self.assertTrue(
  131. '<input checked id="active" name="active" type="checkbox" '
  132. 'value="y">' in output.data)
  133. self.assertFalse(os.path.exists(os.path.join(
  134. tests.HERE, 'test.git', 'hooks',
  135. 'pre-receive.pagureforcecommit')))
  136. # Activate hook
  137. data = {
  138. 'csrf_token': csrf_token,
  139. 'active': 'y',
  140. 'branches': 'master',
  141. }
  142. output = self.app.post(
  143. '/test/settings/Block non fast-forward pushes',
  144. data=data, follow_redirects=True)
  145. self.assertEqual(output.status_code, 200)
  146. self.assertIn(
  147. '<h3>Settings for test</h3>',
  148. output.data)
  149. self.assertIn(
  150. '</button>\n Hook Block non '
  151. 'fast-forward pushes activated', output.data)
  152. output = self.app.get(
  153. '/test/settings/Block non fast-forward pushes')
  154. self.assertIn(
  155. '<div class="projectinfo m-t-1 m-b-1">\n'
  156. 'test project #1 </div>', output.data)
  157. self.assertIn(
  158. '<h3>Block non fast-forward pushes settings</h3>',
  159. output.data)
  160. self.assertIn(
  161. '<td><input id="branches" name="branches" type="text" '
  162. 'value="master"></td>', output.data)
  163. self.assertIn(
  164. '<input checked id="active" name="active" type="checkbox" '
  165. 'value="y">', output.data)
  166. self.assertTrue(os.path.exists(os.path.join(
  167. tests.HERE, 'test.git', 'hooks',
  168. 'pre-receive.pagureforcecommit')))
  169. # De-Activate hook
  170. data = {'csrf_token': csrf_token}
  171. output = self.app.post(
  172. '/test/settings/Block non fast-forward pushes',
  173. data=data, follow_redirects=True)
  174. self.assertEqual(output.status_code, 200)
  175. self.assertIn(
  176. '<section class="settings">\n <h3>Settings for test</h3>',
  177. output.data)
  178. self.assertIn(
  179. '</button>\n Hook Block non '
  180. 'fast-forward pushes inactived', output.data)
  181. output = self.app.get(
  182. '/test/settings/Block non fast-forward pushes')
  183. self.assertIn(
  184. '<div class="projectinfo m-t-1 m-b-1">\n'
  185. 'test project #1 </div>', output.data)
  186. self.assertIn(
  187. '<h3>Block non fast-forward pushes settings</h3>',
  188. output.data)
  189. self.assertIn(
  190. '<td><input id="branches" name="branches" type="text" '
  191. 'value=""></td>', output.data)
  192. self.assertIn(
  193. '<input id="active" name="active" type="checkbox" '
  194. 'value="y">', output.data)
  195. self.assertFalse(os.path.exists(os.path.join(
  196. tests.HERE, 'test.git', 'hooks',
  197. 'pre-receive.pagureforcecommit')))
  198. if __name__ == '__main__':
  199. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  200. PagureFlaskPluginNoFFtests)
  201. unittest.TextTestRunner(verbosity=2).run(SUITE)