test_pagure_flask_ui_plugins_mirror.py 10 KB

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