test_pagure_flask_ui_plugins_pagure_hook.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-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. from mock import patch
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure.lib
  16. import tests
  17. class PagureFlaskPluginPagureHooktests(tests.SimplePagureTest):
  18. """ Tests for pagure_hook plugin of pagure """
  19. def setUp(self):
  20. """ Set up the environnment, ran before every tests. """
  21. super(PagureFlaskPluginPagureHooktests, self).setUp()
  22. tests.create_projects(self.session)
  23. tests.create_projects_git(os.path.join(self.path, 'repos'))
  24. tests.create_projects_git(os.path.join(self.path, 'repos', 'docs'))
  25. def tearDown(self):
  26. """ Tear Down the environment after the tests. """
  27. super(PagureFlaskPluginPagureHooktests, self).tearDown()
  28. pagure.config.config['DOCS_FOLDER'] = None
  29. def test_plugin_mail_page(self):
  30. """ Test the default page of the pagure hook plugin. """
  31. user = tests.FakeUser(username='pingou')
  32. with tests.user_set(self.app.application, user):
  33. output = self.app.get('/test/settings/Pagure')
  34. self.assertEqual(output.status_code, 200)
  35. output_text = output.get_data(as_text=True)
  36. self.assertIn(
  37. '<title>Settings Pagure - test - Pagure</title>',
  38. output_text)
  39. self.assertIn(
  40. '<input class="form-check-input mt-2" id="active" name="active" '
  41. 'type="checkbox" value="y">', output_text)
  42. def test_plugin_mail_no_data(self):
  43. """ Test the pagure hook plugin endpoint when no data is sent. """
  44. user = tests.FakeUser(username='pingou')
  45. with tests.user_set(self.app.application, user):
  46. data = {}
  47. output = self.app.post('/test/settings/Pagure', data=data)
  48. self.assertEqual(output.status_code, 200)
  49. output_text = output.get_data(as_text=True)
  50. self.assertIn(
  51. '<title>Settings Pagure - test - Pagure</title>',
  52. output_text)
  53. self.assertIn(
  54. '<input class="form-check-input mt-2" id="active" name="active" '
  55. 'type="checkbox" value="y">', output_text)
  56. self.assertFalse(os.path.exists(os.path.join(
  57. self.path, 'repos', 'test.git', 'hooks',
  58. 'post-receive')))
  59. self.assertFalse(os.path.exists(os.path.join(
  60. self.path, 'docs', 'test.git', 'hooks',
  61. 'post-receive')))
  62. def test_plugin_mail_no_data_csrf(self):
  63. """ Test the pagure hook plugin endpoint when no data is sent but
  64. the csrf token.
  65. """
  66. user = tests.FakeUser(username='pingou')
  67. with tests.user_set(self.app.application, user):
  68. csrf_token = self.get_csrf()
  69. data = {'csrf_token': csrf_token}
  70. tests.create_projects_git(os.path.join(self.path, 'repos', 'docs'))
  71. tests.create_projects_git(os.path.join(self.path, 'repos', 'requests'))
  72. # With the git repo
  73. output = self.app.post(
  74. '/test/settings/Pagure', data=data, follow_redirects=True)
  75. self.assertEqual(output.status_code, 200)
  76. output_text = output.get_data(as_text=True)
  77. self.assertIn(
  78. '<h5 class="pl-2 font-weight-bold text-muted">'
  79. 'Project Settings</h5>\n', output_text)
  80. self.assertIn(
  81. 'Hook Pagure deactivated',
  82. output_text)
  83. output = self.app.get('/test/settings/Pagure')
  84. self.assertEqual(output.status_code, 200)
  85. output_text = output.get_data(as_text=True)
  86. self.assertIn(
  87. '<title>Settings Pagure - test - Pagure</title>',
  88. output_text)
  89. self.assertIn(
  90. '<input class="form-check-input mt-2" id="active" name="active" '
  91. 'type="checkbox" value="y">', output_text)
  92. self.assertFalse(os.path.exists(os.path.join(
  93. self.path, 'repos', 'test.git', 'hooks',
  94. 'post-receive.pagure')))
  95. self.assertFalse(os.path.exists(os.path.join(
  96. self.path, 'repos', 'test.git', 'hooks',
  97. 'post-receive')))
  98. self.assertFalse(os.path.exists(os.path.join(
  99. self.path, 'docs', 'test.git', 'hooks',
  100. 'post-receive')))
  101. def test_plugin_mail_activate_hook(self):
  102. """ Test the pagure hook plugin endpoint when activating the hook.
  103. """
  104. pagure.config.config['DOCS_FOLDER'] = os.path.join(
  105. self.path, 'repos', 'docs')
  106. user = tests.FakeUser(username='pingou')
  107. with tests.user_set(self.app.application, user):
  108. csrf_token = self.get_csrf()
  109. # Activate hook
  110. data = {
  111. 'csrf_token': csrf_token,
  112. 'active': 'y',
  113. }
  114. output = self.app.post(
  115. '/test/settings/Pagure', data=data, follow_redirects=True)
  116. self.assertEqual(output.status_code, 200)
  117. output_text = output.get_data(as_text=True)
  118. self.assertIn(
  119. '<h5 class="pl-2 font-weight-bold text-muted">'
  120. 'Project Settings</h5>\n', output_text)
  121. self.assertIn(
  122. 'Hook Pagure activated',
  123. output_text)
  124. output = self.app.get('/test/settings/Pagure')
  125. self.assertEqual(output.status_code, 200)
  126. output_text = output.get_data(as_text=True)
  127. self.assertIn(
  128. '<title>Settings Pagure - test - Pagure</title>',
  129. output_text)
  130. self.assertIn(
  131. '<input checked class="form-check-input mt-2" id="active" name="active" '
  132. 'type="checkbox" value="y">', output_text)
  133. self.assertTrue(os.path.exists(os.path.join(
  134. self.path, 'repos', 'test.git', 'hooks',
  135. 'post-receive')))
  136. self.assertTrue(os.path.exists(os.path.join(
  137. self.path, 'repos', 'docs', 'test.git', 'hooks',
  138. 'post-receive')))
  139. def test_plugin_mail_deactivate_hook(self):
  140. """ Test the pagure hook plugin endpoint when activating the hook.
  141. """
  142. self.test_plugin_mail_activate_hook()
  143. user = tests.FakeUser(username='pingou')
  144. with tests.user_set(self.app.application, user):
  145. csrf_token = self.get_csrf()
  146. # De-Activate hook
  147. data = {'csrf_token': csrf_token}
  148. output = self.app.post(
  149. '/test/settings/Pagure', data=data, follow_redirects=True)
  150. self.assertEqual(output.status_code, 200)
  151. output_text = output.get_data(as_text=True)
  152. self.assertIn(
  153. '<h5 class="pl-2 font-weight-bold text-muted">'
  154. 'Project Settings</h5>\n', output_text)
  155. self.assertIn(
  156. 'Hook Pagure deactivated',
  157. output_text)
  158. output = self.app.get('/test/settings/Pagure')
  159. self.assertEqual(output.status_code, 200)
  160. output_text = output.get_data(as_text=True)
  161. self.assertIn(
  162. '<title>Settings Pagure - test - Pagure</title>',
  163. output_text)
  164. self.assertIn(
  165. '<input class="form-check-input mt-2" id="active" name="active" '
  166. 'type="checkbox" value="y">', output_text)
  167. self.assertFalse(os.path.exists(os.path.join(
  168. self.path, 'repos', 'test.git', 'hooks',
  169. 'post-receive.pagure')))
  170. self.assertTrue(os.path.exists(os.path.join(
  171. self.path, 'repos', 'test.git', 'hooks',
  172. 'post-receive')))
  173. self.assertTrue(os.path.exists(os.path.join(
  174. self.path, 'repos', 'docs', 'test.git', 'hooks',
  175. 'post-receive')))
  176. @patch.dict('pagure.config.config', {'DOCS_FOLDER': None})
  177. def test_plugin_mail_activate_hook_no_doc(self):
  178. """ Test the pagure hook plugin endpoint when activating the hook
  179. on a pagure instance that de-activated the doc repos.
  180. """
  181. user = tests.FakeUser(username='pingou')
  182. with tests.user_set(self.app.application, user):
  183. csrf_token = self.get_csrf()
  184. # Activate hook
  185. data = {
  186. 'csrf_token': csrf_token,
  187. 'active': 'y',
  188. }
  189. output = self.app.post(
  190. '/test/settings/Pagure', data=data, follow_redirects=True)
  191. output_text = output.get_data(as_text=True)
  192. self.assertIn(
  193. 'Hook Pagure activated',
  194. output_text)
  195. self.assertTrue(os.path.exists(os.path.join(
  196. self.path, 'repos', 'test.git', 'hooks',
  197. 'post-receive')))
  198. self.assertFalse(os.path.exists(os.path.join(
  199. self.path, 'docs', 'test.git', 'hooks',
  200. 'post-receive')))
  201. @patch.dict('pagure.config.config', {'DOCS_FOLDER': None})
  202. def test_plugin_mail_deactivate_hook_no_doc(self):
  203. """ Test the pagure hook plugin endpoint when activating then
  204. deactivating the hook on a pagure instance that de-activated the
  205. doc repos.
  206. """
  207. user = tests.FakeUser(username='pingou')
  208. with tests.user_set(self.app.application, user):
  209. csrf_token = self.get_csrf()
  210. # Activate hook
  211. data = {
  212. 'csrf_token': csrf_token,
  213. 'active': 'y',
  214. }
  215. output = self.app.post(
  216. '/test/settings/Pagure', data=data, follow_redirects=True)
  217. self.assertEqual(output.status_code, 200)
  218. output_text = output.get_data(as_text=True)
  219. self.assertIn(
  220. '<h5 class="pl-2 font-weight-bold text-muted">'
  221. 'Project Settings</h5>\n', output_text)
  222. self.assertIn(
  223. 'Hook Pagure activated',
  224. output_text)
  225. self.assertTrue(os.path.exists(os.path.join(
  226. self.path, 'repos', 'test.git', 'hooks',
  227. 'post-receive')))
  228. self.assertFalse(os.path.exists(os.path.join(
  229. self.path, 'docs', 'test.git', 'hooks',
  230. 'post-receive')))
  231. # Deactivate hook
  232. data = {
  233. 'csrf_token': csrf_token,
  234. }
  235. output = self.app.post(
  236. '/test/settings/Pagure', data=data, follow_redirects=True)
  237. output_text = output.get_data(as_text=True)
  238. self.assertIn(
  239. 'Hook Pagure deactivated',
  240. output_text)
  241. self.assertFalse(os.path.exists(os.path.join(
  242. self.path, 'repos', 'test.git', 'hooks',
  243. 'post-receive.pagure')))
  244. self.assertTrue(os.path.exists(os.path.join(
  245. self.path, 'repos', 'test.git', 'hooks',
  246. 'post-receive')))
  247. self.assertFalse(os.path.exists(os.path.join(
  248. self.path, 'docs', 'test.git', 'hooks',
  249. 'post-receive')))
  250. if __name__ == '__main__':
  251. unittest.main(verbosity=2)