test_pagure_flask_ui_plugins_pagure_hook.py 11 KB

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