test_pagure_flask_ui_plugins_fedmsg.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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(
  13. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  14. )
  15. import tests
  16. import pagure.config
  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. )
  39. self.assertIn(
  40. '<input class="form-check-input mt-2" id="active" name="active" '
  41. 'type="checkbox" value="y">',
  42. output_text,
  43. )
  44. csrf_token = self.get_csrf(output=output)
  45. data = {}
  46. output = self.app.post("/test/settings/Fedmsg", data=data)
  47. self.assertEqual(output.status_code, 200)
  48. output_text = output.get_data(as_text=True)
  49. self.assertIn(
  50. "<title>Settings Fedmsg - test - Pagure</title>", output_text
  51. )
  52. self.assertIn(
  53. '<input class="form-check-input mt-2" id="active" name="active" '
  54. 'type="checkbox" value="y">',
  55. output_text,
  56. )
  57. self.assertFalse(
  58. os.path.exists(
  59. os.path.join(
  60. self.path,
  61. "repos",
  62. "test.git",
  63. "hooks",
  64. "post-receive.fedmsg",
  65. )
  66. )
  67. )
  68. self.assertFalse(
  69. os.path.exists(
  70. os.path.join(
  71. self.path, "docs", "test.git", "hooks", "post-receive"
  72. )
  73. )
  74. )
  75. def test_plugin_fedmsg_no_data(self):
  76. """ Test the setting up the fedmsg plugin when there are no Docs
  77. folder.
  78. """
  79. user = tests.FakeUser(username="pingou")
  80. with tests.user_set(self.app.application, user):
  81. csrf_token = self.get_csrf()
  82. data = {"csrf_token": csrf_token}
  83. # With the git repo
  84. output = self.app.post(
  85. "/test/settings/Fedmsg", data=data, follow_redirects=True
  86. )
  87. self.assertEqual(output.status_code, 200)
  88. output_text = output.get_data(as_text=True)
  89. self.assertIn(
  90. '<h5 class="pl-2 font-weight-bold text-muted">'
  91. "Project Settings</h5>\n",
  92. output_text,
  93. )
  94. self.assertIn("Hook Fedmsg deactivated", output_text)
  95. output = self.app.get("/test/settings/Fedmsg", data=data)
  96. output_text = output.get_data(as_text=True)
  97. self.assertIn(
  98. "<title>Settings Fedmsg - test - Pagure</title>", output_text
  99. )
  100. self.assertIn(
  101. '<input class="form-check-input mt-2" id="active" name="active" '
  102. 'type="checkbox" value="y">',
  103. output_text,
  104. )
  105. self.assertFalse(
  106. os.path.exists(
  107. os.path.join(
  108. self.path,
  109. "repos",
  110. "test.git",
  111. "hooks",
  112. "post-receive.fedmsg",
  113. )
  114. )
  115. )
  116. self.assertFalse(
  117. os.path.exists(
  118. os.path.join(
  119. self.path, "docs", "test.git", "hooks", "post-receive"
  120. )
  121. )
  122. )
  123. def test_plugin_fedmsg_activate(self):
  124. """ Test the setting up the fedmsg plugin when there are no Docs
  125. folder.
  126. """
  127. pagure.config.config["DOCS_FOLDER"] = os.path.join(
  128. self.path, "repos", "docs"
  129. )
  130. user = tests.FakeUser(username="pingou")
  131. with tests.user_set(self.app.application, user):
  132. csrf_token = self.get_csrf()
  133. # Activate hook
  134. data = {"csrf_token": csrf_token, "active": "y"}
  135. output = self.app.post(
  136. "/test/settings/Fedmsg", data=data, follow_redirects=True
  137. )
  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",
  143. output_text,
  144. )
  145. self.assertIn("Hook Fedmsg activated", 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. )
  152. self.assertIn(
  153. '<input checked class="form-check-input mt-2" id="active" name="active" '
  154. 'type="checkbox" value="y">',
  155. output_text,
  156. )
  157. self.assertFalse(
  158. os.path.exists(
  159. os.path.join(
  160. self.path,
  161. "repos",
  162. "test.git",
  163. "hooks",
  164. "post-receive.fedmsg",
  165. )
  166. )
  167. )
  168. self.assertTrue(
  169. os.path.exists(
  170. os.path.join(
  171. self.path,
  172. "repos",
  173. "docs",
  174. "test.git",
  175. "hooks",
  176. "post-receive",
  177. )
  178. )
  179. )
  180. def test_plugin_fedmsg_deactivate(self):
  181. """ Test the setting up the fedmsg plugin when there are no Docs
  182. folder.
  183. """
  184. self.test_plugin_fedmsg_activate()
  185. user = tests.FakeUser(username="pingou")
  186. with tests.user_set(self.app.application, user):
  187. csrf_token = self.get_csrf()
  188. # De-Activate hook
  189. data = {"csrf_token": csrf_token}
  190. output = self.app.post(
  191. "/test/settings/Fedmsg", data=data, follow_redirects=True
  192. )
  193. self.assertEqual(output.status_code, 200)
  194. output_text = output.get_data(as_text=True)
  195. self.assertIn(
  196. '<h5 class="pl-2 font-weight-bold text-muted">'
  197. "Project Settings</h5>\n",
  198. output_text,
  199. )
  200. self.assertIn("Hook Fedmsg deactivated", output_text)
  201. output = self.app.get("/test/settings/Fedmsg", data=data)
  202. self.assertEqual(output.status_code, 200)
  203. output_text = output.get_data(as_text=True)
  204. self.assertIn(
  205. "<title>Settings Fedmsg - test - Pagure</title>", output_text
  206. )
  207. self.assertIn(
  208. '<input class="form-check-input mt-2" id="active" name="active" '
  209. 'type="checkbox" value="y">',
  210. output.get_data(as_text=True),
  211. )
  212. self.assertFalse(
  213. os.path.exists(
  214. os.path.join(
  215. self.path,
  216. "repos",
  217. "test.git",
  218. "hooks",
  219. "post-receive.fedmsg",
  220. )
  221. )
  222. )
  223. self.assertTrue(
  224. os.path.exists(
  225. os.path.join(
  226. self.path,
  227. "repos",
  228. "docs",
  229. "test.git",
  230. "hooks",
  231. "post-receive",
  232. )
  233. )
  234. )
  235. @patch.dict("pagure.config.config", {"DOCS_FOLDER": None})
  236. def test_plugin_fedmsg_no_docs(self):
  237. """ Test the setting up the fedmsg plugin when there are no Docs
  238. folder.
  239. """
  240. user = tests.FakeUser(username="pingou")
  241. with tests.user_set(self.app.application, user):
  242. csrf_token = self.get_csrf()
  243. # Activate hook
  244. data = {"csrf_token": csrf_token, "active": "y"}
  245. output = self.app.post(
  246. "/test/settings/Fedmsg", data=data, follow_redirects=True
  247. )
  248. self.assertEqual(output.status_code, 200)
  249. self.assertFalse(
  250. os.path.exists(
  251. os.path.join(
  252. self.path,
  253. "repos",
  254. "test.git",
  255. "hooks",
  256. "post-receive.fedmsg",
  257. )
  258. )
  259. )
  260. self.assertFalse(
  261. os.path.exists(
  262. os.path.join(
  263. self.path, "docs", "test.git", "hooks", "post-receive"
  264. )
  265. )
  266. )
  267. if __name__ == "__main__":
  268. unittest.main(verbosity=2)