test_pagure_flask_ui_plugins_fedmsg.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 PagureFlaskPluginFedmsgtests(tests.SimplePagureTest):
  17. """ Tests for fedmsg plugin of pagure """
  18. def setUp(self):
  19. """ Set up the environnment, ran before every tests. """
  20. super(PagureFlaskPluginFedmsgtests, 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, 'docs'))
  24. def tearDown(self):
  25. """ Tear Down the environment after the tests. """
  26. super(PagureFlaskPluginFedmsgtests, self).tearDown()
  27. pagure.config.config['DOCS_FOLDER'] = None
  28. def test_plugin_fedmsg_defaul_page(self):
  29. """ Test the fedmsg plugin endpoint's default page. """
  30. user = tests.FakeUser(username='pingou')
  31. with tests.user_set(self.app.application, user):
  32. output = self.app.get('/test/settings/Fedmsg')
  33. self.assertEqual(output.status_code, 200)
  34. output_text = output.get_data(as_text=True)
  35. self.assertIn(
  36. '<title>Settings Fedmsg - test - Pagure</title>', output_text)
  37. self.assertIn(
  38. '<input class="form-check-input mt-2" id="active" name="active" '
  39. 'type="checkbox" value="y">', output_text)
  40. csrf_token = self.get_csrf(output=output)
  41. data = {}
  42. output = self.app.post('/test/settings/Fedmsg', data=data)
  43. self.assertEqual(output.status_code, 200)
  44. output_text = output.get_data(as_text=True)
  45. self.assertIn(
  46. '<title>Settings Fedmsg - test - Pagure</title>', output_text)
  47. self.assertIn(
  48. '<input class="form-check-input mt-2" id="active" name="active" '
  49. 'type="checkbox" value="y">', output_text)
  50. self.assertFalse(os.path.exists(os.path.join(
  51. self.path, 'repos', 'test.git', 'hooks',
  52. 'post-receive.fedmsg')))
  53. self.assertFalse(os.path.exists(os.path.join(
  54. self.path, 'docs', 'test.git', 'hooks',
  55. 'post-receive')))
  56. def test_plugin_fedmsg_no_data(self):
  57. """ Test the setting up the fedmsg plugin when there are no Docs
  58. folder.
  59. """
  60. user = tests.FakeUser(username='pingou')
  61. with tests.user_set(self.app.application, user):
  62. csrf_token = self.get_csrf()
  63. data = {'csrf_token': csrf_token}
  64. # With the git repo
  65. output = self.app.post(
  66. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  67. self.assertEqual(output.status_code, 200)
  68. output_text = output.get_data(as_text=True)
  69. self.assertIn(
  70. '<h5 class="pl-2 font-weight-bold text-muted">'
  71. 'Project Settings</h5>\n', output_text)
  72. self.assertIn(
  73. 'Hook Fedmsg deactivated',
  74. output_text)
  75. output = self.app.get('/test/settings/Fedmsg', data=data)
  76. output_text = output.get_data(as_text=True)
  77. self.assertIn(
  78. '<title>Settings Fedmsg - test - Pagure</title>', output_text)
  79. self.assertIn(
  80. '<input class="form-check-input mt-2" id="active" name="active" '
  81. 'type="checkbox" value="y">', output_text)
  82. self.assertFalse(os.path.exists(os.path.join(
  83. self.path, 'repos', 'test.git', 'hooks',
  84. 'post-receive.fedmsg')))
  85. self.assertFalse(os.path.exists(os.path.join(
  86. self.path, 'docs', 'test.git', 'hooks',
  87. 'post-receive')))
  88. def test_plugin_fedmsg_activate(self):
  89. """ Test the setting up the fedmsg plugin when there are no Docs
  90. folder.
  91. """
  92. pagure.config.config['DOCS_FOLDER'] = os.path.join(
  93. self.path, 'repos', 'docs')
  94. user = tests.FakeUser(username='pingou')
  95. with tests.user_set(self.app.application, user):
  96. csrf_token = self.get_csrf()
  97. # Activate hook
  98. data = {
  99. 'csrf_token': csrf_token,
  100. 'active': 'y',
  101. }
  102. output = self.app.post(
  103. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  104. self.assertEqual(output.status_code, 200)
  105. output_text = output.get_data(as_text=True)
  106. self.assertIn(
  107. '<h5 class="pl-2 font-weight-bold text-muted">'
  108. 'Project Settings</h5>\n', output_text)
  109. self.assertIn(
  110. 'Hook Fedmsg activated',
  111. output_text)
  112. output = self.app.get('/test/settings/Fedmsg', data=data)
  113. self.assertEqual(output.status_code, 200)
  114. output_text = output.get_data(as_text=True)
  115. self.assertIn(
  116. '<title>Settings Fedmsg - test - Pagure</title>', output_text)
  117. self.assertIn(
  118. '<input checked class="form-check-input mt-2" id="active" name="active" '
  119. 'type="checkbox" value="y">', output_text)
  120. self.assertFalse(os.path.exists(os.path.join(
  121. self.path, 'repos', 'test.git', 'hooks',
  122. 'post-receive.fedmsg')))
  123. self.assertTrue(os.path.exists(os.path.join(
  124. self.path, 'repos', 'docs', 'test.git', 'hooks',
  125. 'post-receive')))
  126. def test_plugin_fedmsg_deactivate(self):
  127. """ Test the setting up the fedmsg plugin when there are no Docs
  128. folder.
  129. """
  130. self.test_plugin_fedmsg_activate()
  131. user = tests.FakeUser(username='pingou')
  132. with tests.user_set(self.app.application, user):
  133. csrf_token = self.get_csrf()
  134. # De-Activate hook
  135. data = {'csrf_token': csrf_token}
  136. output = self.app.post(
  137. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  138. self.assertEqual(output.status_code, 200)
  139. output_text = output.get_data(as_text=True)
  140. self.assertIn(
  141. '<h5 class="pl-2 font-weight-bold text-muted">'
  142. 'Project Settings</h5>\n', output_text)
  143. self.assertIn(
  144. 'Hook Fedmsg deactivated',
  145. output_text)
  146. output = self.app.get('/test/settings/Fedmsg', data=data)
  147. self.assertEqual(output.status_code, 200)
  148. output_text = output.get_data(as_text=True)
  149. self.assertIn(
  150. '<title>Settings Fedmsg - test - Pagure</title>', output_text)
  151. self.assertIn(
  152. '<input class="form-check-input mt-2" id="active" name="active" '
  153. 'type="checkbox" value="y">', output.get_data(as_text=True))
  154. self.assertFalse(os.path.exists(os.path.join(
  155. self.path, 'repos', 'test.git', 'hooks',
  156. 'post-receive.fedmsg')))
  157. self.assertTrue(os.path.exists(os.path.join(
  158. self.path, 'repos', 'docs', 'test.git', 'hooks',
  159. 'post-receive')))
  160. @patch.dict('pagure.config.config', {'DOCS_FOLDER': None})
  161. def test_plugin_fedmsg_no_docs(self):
  162. """ Test the setting up the fedmsg plugin when there are no Docs
  163. folder.
  164. """
  165. user = tests.FakeUser(username='pingou')
  166. with tests.user_set(self.app.application, user):
  167. csrf_token = self.get_csrf()
  168. # Activate hook
  169. data = {
  170. 'csrf_token': csrf_token,
  171. 'active': 'y',
  172. }
  173. output = self.app.post(
  174. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  175. self.assertEqual(output.status_code, 200)
  176. self.assertFalse(os.path.exists(os.path.join(
  177. self.path, 'repos', 'test.git', 'hooks',
  178. 'post-receive.fedmsg')))
  179. self.assertFalse(os.path.exists(os.path.join(
  180. self.path, 'docs', 'test.git', 'hooks',
  181. 'post-receive')))
  182. if __name__ == '__main__':
  183. unittest.main(verbosity=2)