test_pagure_flask_ui_plugins_mirror.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 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 pagure.utils
  17. import tests
  18. class PagureFlaskPluginMirrortests(tests.Modeltests):
  19. """ Tests for mirror plugin of pagure """
  20. def setUp(self):
  21. """ Set up the environnment, ran before every tests. """
  22. super(PagureFlaskPluginMirrortests, self).setUp()
  23. tests.create_projects(self.session)
  24. tests.create_projects_git(os.path.join(self.path, 'repos'))
  25. def test_valid_ssh_url_pattern(self):
  26. """ Check a number of valide ssh target that the pattern should let
  27. through.
  28. """
  29. entries = [
  30. 'ssh://user@host.lcl:/path/to/repo.git',
  31. 'git@github.com:user/project.git',
  32. 'ssh://user@host.org/target',
  33. 'git+ssh://user@host.org/target',
  34. 'git+ssh://user@host.lcl:/path/to/repo.git',
  35. ]
  36. for el in entries:
  37. print(el)
  38. self.assertIsNotNone(pagure.utils.ssh_urlpattern.match(el))
  39. def test_plugin_mirror_no_csrf(self):
  40. """ Test setting up the mirror plugin with no csrf. """
  41. user = tests.FakeUser(username='pingou')
  42. with tests.user_set(self.app.application, user):
  43. output = self.app.get('/test/settings/Mirroring')
  44. self.assertEqual(output.status_code, 200)
  45. output_text = output.get_data(as_text=True)
  46. self.assertIn(
  47. '<title>Settings Mirroring - test - Pagure</title>',
  48. output_text)
  49. self.assertIn(
  50. '<input class="form-check-input mt-2" id="active" name="active" '
  51. 'type="checkbox" value="y">', output_text)
  52. data = {}
  53. output = self.app.post('/test/settings/Mirroring', data=data)
  54. self.assertEqual(output.status_code, 200)
  55. output_text = output.get_data(as_text=True)
  56. self.assertIn(
  57. '<title>Settings Mirroring - test - Pagure</title>',
  58. output_text)
  59. self.assertIn(
  60. '<input class="form-check-input mt-2" id="active" name="active" '
  61. 'type="checkbox" value="y">', output_text)
  62. self.assertFalse(os.path.exists(os.path.join(
  63. self.path, 'repos', 'test.git', 'hooks',
  64. 'post-receive.mirror')))
  65. def test_plugin_mirror_no_data(self):
  66. """ Test the setting up the mirror plugin when there are no data
  67. provided in the request.
  68. """
  69. user = tests.FakeUser(username='pingou')
  70. with tests.user_set(self.app.application, user):
  71. csrf_token = self.get_csrf()
  72. data = {'csrf_token': csrf_token}
  73. # With the git repo
  74. output = self.app.post(
  75. '/test/settings/Mirroring', data=data, follow_redirects=True)
  76. self.assertEqual(output.status_code, 200)
  77. output_text = output.get_data(as_text=True)
  78. self.assertIn(
  79. '<title>Settings - test - Pagure</title>', output_text)
  80. self.assertIn(
  81. '</i> Hook Mirroring deactivated</div>',
  82. output_text)
  83. output = self.app.get('/test/settings/Mirroring', data=data)
  84. output_text = output.get_data(as_text=True)
  85. self.assertIn(
  86. '<title>Settings Mirroring - 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.mirror')))
  94. def test_plugin_mirror_invalid_target(self):
  95. """ Test the setting up the mirror plugin when there are the target
  96. provided is invalid.
  97. """
  98. user = tests.FakeUser(username='pingou')
  99. with tests.user_set(self.app.application, user):
  100. csrf_token = self.get_csrf()
  101. data = {
  102. 'csrf_token': csrf_token,
  103. 'active': True,
  104. 'target': 'https://host.org/target',
  105. }
  106. # With the git repo
  107. output = self.app.post(
  108. '/test/settings/Mirroring', data=data, follow_redirects=True)
  109. self.assertEqual(output.status_code, 200)
  110. output_text = output.get_data(as_text=True)
  111. self.assertIn(
  112. '<title>Settings Mirroring - test - Pagure</title>',
  113. output_text)
  114. if self.get_wtforms_version() >= (2, 2):
  115. self.assertIn(
  116. '<div class="col-sm-10">\n '
  117. '<input class="form-control pl-0" id="target" name="target" '
  118. 'required type="text" value="https://host.org/target">\n'
  119. ' </div>\n '
  120. '</div>\n <div class="alert alert-danger">Invalid '
  121. 'input.</div>', output_text)
  122. else:
  123. self.assertIn(
  124. '<div class="col-sm-10">\n '
  125. '<input class="form-control pl-0" id="target" name="target" '
  126. 'type="text" value="https://host.org/target">\n </div>\n '
  127. '</div>\n <div class="alert alert-danger">Invalid '
  128. 'input.</div>', output_text)
  129. output = self.app.get('/test/settings/Mirroring', data=data)
  130. output_text = output.get_data(as_text=True)
  131. self.assertIn(
  132. '<title>Settings Mirroring - test - Pagure</title>',
  133. output_text)
  134. self.assertIn(
  135. '<input class="form-check-input mt-2" id="active" name="active" '
  136. 'type="checkbox" value="y">', output_text)
  137. self.assertFalse(os.path.exists(os.path.join(
  138. self.path, 'repos', 'test.git', 'hooks',
  139. 'post-receive.mirror')))
  140. def test_setting_up_mirror(self):
  141. """ Test the setting up the mirror plugin.
  142. """
  143. user = tests.FakeUser(username='pingou')
  144. with tests.user_set(self.app.application, user):
  145. csrf_token = self.get_csrf()
  146. data = {
  147. 'csrf_token': csrf_token,
  148. 'active': True,
  149. 'target': 'ssh://user@host.org/target',
  150. }
  151. # With the git repo
  152. output = self.app.post(
  153. '/test/settings/Mirroring', data=data, follow_redirects=True)
  154. self.assertEqual(output.status_code, 200)
  155. output_text = output.get_data(as_text=True)
  156. self.assertIn(
  157. '<title>Settings - test - Pagure</title>',
  158. output_text)
  159. self.assertIn(
  160. '</i> Hook Mirroring activated</div>',
  161. output_text)
  162. output = self.app.get('/test/settings/Mirroring', data=data)
  163. output_text = output.get_data(as_text=True)
  164. self.assertIn(
  165. '<title>Settings Mirroring - test - Pagure</title>',
  166. output_text)
  167. self.assertIn(
  168. '<input checked class="form-check-input mt-2" id="active" name="active" '
  169. 'type="checkbox" value="y">', output_text)
  170. self.assertTrue(os.path.exists(os.path.join(
  171. self.path, 'repos', 'test.git', 'hooks',
  172. 'post-receive')))
  173. def test_plugin_mirror_deactivate(self):
  174. """ Test the deactivating the mirror plugin.
  175. """
  176. self.test_setting_up_mirror()
  177. user = tests.FakeUser(username='pingou')
  178. with tests.user_set(self.app.application, user):
  179. csrf_token = self.get_csrf()
  180. # De-Activate hook
  181. data = {'csrf_token': csrf_token}
  182. output = self.app.post(
  183. '/test/settings/Mirroring', data=data, follow_redirects=True)
  184. self.assertEqual(output.status_code, 200)
  185. output_text = output.get_data(as_text=True)
  186. self.assertIn(
  187. '<title>Settings - test - Pagure</title>',
  188. output_text)
  189. self.assertIn(
  190. '</i> Hook Mirroring deactivated</div>',
  191. output_text)
  192. output = self.app.get('/test/settings/Mirroring', data=data)
  193. self.assertEqual(output.status_code, 200)
  194. output_text = output.get_data(as_text=True)
  195. self.assertIn(
  196. '<title>Settings Mirroring - test - Pagure</title>',
  197. output_text)
  198. self.assertIn(
  199. '<input class="form-check-input mt-2" id="active" name="active" '
  200. 'type="checkbox" value="y">', output.get_data(as_text=True))
  201. self.assertFalse(os.path.exists(os.path.join(
  202. self.path, 'repos', 'test.git', 'hooks',
  203. 'post-receive.mirror')))
  204. if __name__ == '__main__':
  205. unittest.main(verbosity=2)