test_pagure_flask_ui_plugins_pagure_hook.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  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 pagure.lib
  15. import tests
  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. self.assertIn(
  35. '<div class="projectinfo m-t-1 m-b-1">\n'
  36. 'test project #1 </div>', output.data)
  37. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  38. self.assertTrue(
  39. '<input class="form-control" id="active" name="active" '
  40. 'type="checkbox" value="y">' in output.data)
  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. self.assertIn(
  49. '<div class="projectinfo m-t-1 m-b-1">\n'
  50. 'test project #1 </div>', output.data)
  51. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  52. self.assertTrue(
  53. '<input class="form-control" id="active" name="active" '
  54. 'type="checkbox" value="y">' in output.data)
  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. self.assertIn(
  76. '<section class="settings">\n <h3>Settings for test</h3>',
  77. output.data)
  78. self.assertTrue(
  79. '</button>\n Hook Pagure deactivated'
  80. in output.data)
  81. output = self.app.get('/test/settings/Pagure')
  82. self.assertEqual(output.status_code, 200)
  83. self.assertIn(
  84. '<div class="projectinfo m-t-1 m-b-1">\n'
  85. 'test project #1 </div>', output.data)
  86. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  87. self.assertTrue(
  88. '<input class="form-control" id="active" name="active" '
  89. 'type="checkbox" value="y">' in output.data)
  90. self.assertFalse(os.path.exists(os.path.join(
  91. self.path, 'repos', 'test.git', 'hooks',
  92. 'post-receive.pagure')))
  93. self.assertFalse(os.path.exists(os.path.join(
  94. self.path, 'repos', 'test.git', 'hooks',
  95. 'post-receive')))
  96. self.assertFalse(os.path.exists(os.path.join(
  97. self.path, 'docs', 'test.git', 'hooks',
  98. 'post-receive')))
  99. def test_plugin_mail_activate_hook(self):
  100. """ Test the pagure hook plugin endpoint when activating the hook.
  101. """
  102. pagure.config.config['DOCS_FOLDER'] = os.path.join(
  103. self.path, 'repos', 'docs')
  104. user = tests.FakeUser(username='pingou')
  105. with tests.user_set(self.app.application, user):
  106. csrf_token = self.get_csrf()
  107. # Activate hook
  108. data = {
  109. 'csrf_token': csrf_token,
  110. 'active': 'y',
  111. }
  112. output = self.app.post(
  113. '/test/settings/Pagure', data=data, follow_redirects=True)
  114. self.assertEqual(output.status_code, 200)
  115. self.assertIn(
  116. '<section class="settings">\n <h3>Settings for test</h3>',
  117. output.data)
  118. self.assertTrue(
  119. '</button>\n Hook Pagure activated'
  120. in output.data)
  121. output = self.app.get('/test/settings/Pagure')
  122. self.assertEqual(output.status_code, 200)
  123. self.assertIn(
  124. '<div class="projectinfo m-t-1 m-b-1">\n'
  125. 'test project #1 </div>', output.data)
  126. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  127. self.assertTrue(
  128. '<input checked class="form-control" id="active" name="active" '
  129. 'type="checkbox" value="y">' in output.data)
  130. self.assertTrue(os.path.exists(os.path.join(
  131. self.path, 'repos', 'test.git', 'hooks',
  132. 'post-receive.pagure')))
  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. self.assertIn(
  152. '<section class="settings">\n <h3>Settings for test</h3>',
  153. output.data)
  154. self.assertTrue(
  155. '</button>\n Hook Pagure deactivated'
  156. in output.data)
  157. output = self.app.get('/test/settings/Pagure')
  158. self.assertEqual(output.status_code, 200)
  159. self.assertIn(
  160. '<div class="projectinfo m-t-1 m-b-1">\n'
  161. 'test project #1 </div>', output.data)
  162. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  163. self.assertTrue(
  164. '<input class="form-control" id="active" name="active" '
  165. 'type="checkbox" value="y">' in output.data)
  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. self.assertTrue(
  191. '</button>\n Hook Pagure activated'
  192. in output.data)
  193. self.assertTrue(os.path.exists(os.path.join(
  194. self.path, 'repos', 'test.git', 'hooks',
  195. 'post-receive.pagure')))
  196. self.assertTrue(os.path.exists(os.path.join(
  197. self.path, 'repos', 'test.git', 'hooks',
  198. 'post-receive')))
  199. self.assertFalse(os.path.exists(os.path.join(
  200. self.path, 'docs', 'test.git', 'hooks',
  201. 'post-receive')))
  202. @patch.dict('pagure.config.config', {'DOCS_FOLDER': None})
  203. def test_plugin_mail_deactivate_hook_no_doc(self):
  204. """ Test the pagure hook plugin endpoint when activating then
  205. deactivating the hook on a pagure instance that de-activated the
  206. doc repos.
  207. """
  208. user = tests.FakeUser(username='pingou')
  209. with tests.user_set(self.app.application, user):
  210. csrf_token = self.get_csrf()
  211. # Activate hook
  212. data = {
  213. 'csrf_token': csrf_token,
  214. 'active': 'y',
  215. }
  216. output = self.app.post(
  217. '/test/settings/Pagure', data=data, follow_redirects=True)
  218. self.assertEqual(output.status_code, 200)
  219. self.assertIn(
  220. '<section class="settings">\n <h3>Settings for test</h3>',
  221. output.data)
  222. self.assertTrue(
  223. '</button>\n Hook Pagure activated'
  224. in output.data)
  225. self.assertTrue(os.path.exists(os.path.join(
  226. self.path, 'repos', 'test.git', 'hooks',
  227. 'post-receive.pagure')))
  228. self.assertTrue(os.path.exists(os.path.join(
  229. self.path, 'repos', 'test.git', 'hooks',
  230. 'post-receive')))
  231. self.assertFalse(os.path.exists(os.path.join(
  232. self.path, 'docs', 'test.git', 'hooks',
  233. 'post-receive')))
  234. # Deactivate hook
  235. data = {
  236. 'csrf_token': csrf_token,
  237. }
  238. output = self.app.post(
  239. '/test/settings/Pagure', data=data, follow_redirects=True)
  240. self.assertTrue(
  241. '</button>\n Hook Pagure deactivated'
  242. in output.data)
  243. self.assertFalse(os.path.exists(os.path.join(
  244. self.path, 'repos', 'test.git', 'hooks',
  245. 'post-receive.pagure')))
  246. self.assertTrue(os.path.exists(os.path.join(
  247. self.path, 'repos', 'test.git', 'hooks',
  248. 'post-receive')))
  249. self.assertFalse(os.path.exists(os.path.join(
  250. self.path, 'docs', 'test.git', 'hooks',
  251. 'post-receive')))
  252. if __name__ == '__main__':
  253. unittest.main(verbosity=2)