test_pagure_flask_ui_plugins_fedmsg.py 8.3 KB

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