test_pagure_flask_ui_plugins_pagure_hook.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 PagureFlaskPluginPagureHooktests(tests.SimplePagureTest):
  18. """ Tests for pagure_hook plugin of pagure """
  19. def setUp(self):
  20. """ Set up the environnment, ran before every tests. """
  21. super(PagureFlaskPluginPagureHooktests, 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, "repos", "docs"))
  25. def tearDown(self):
  26. """ Tear Down the environment after the tests. """
  27. super(PagureFlaskPluginPagureHooktests, self).tearDown()
  28. pagure.config.config["DOCS_FOLDER"] = None
  29. def test_plugin_mail_page(self):
  30. """ Test the default page of the pagure hook plugin. """
  31. user = tests.FakeUser(username="pingou")
  32. with tests.user_set(self.app.application, user):
  33. output = self.app.get("/test/settings/Pagure")
  34. self.assertEqual(output.status_code, 200)
  35. output_text = output.get_data(as_text=True)
  36. self.assertIn(
  37. "<title>Settings Pagure - 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. def test_plugin_mail_no_data(self):
  45. """ Test the pagure hook plugin endpoint when no data is sent. """
  46. user = tests.FakeUser(username="pingou")
  47. with tests.user_set(self.app.application, user):
  48. data = {}
  49. output = self.app.post("/test/settings/Pagure", data=data)
  50. self.assertEqual(output.status_code, 200)
  51. output_text = output.get_data(as_text=True)
  52. self.assertIn(
  53. "<title>Settings Pagure - test - Pagure</title>", output_text
  54. )
  55. self.assertIn(
  56. '<input class="form-check-input mt-2" id="active" name="active" '
  57. 'type="checkbox" value="y">',
  58. output_text,
  59. )
  60. self.assertFalse(
  61. os.path.exists(
  62. os.path.join(
  63. self.path, "repos", "test.git", "hooks", "post-receive"
  64. )
  65. )
  66. )
  67. self.assertFalse(
  68. os.path.exists(
  69. os.path.join(
  70. self.path, "docs", "test.git", "hooks", "post-receive"
  71. )
  72. )
  73. )
  74. def test_plugin_mail_no_data_csrf(self):
  75. """Test the pagure hook plugin endpoint when no data is sent but
  76. the csrf token.
  77. """
  78. user = tests.FakeUser(username="pingou")
  79. with tests.user_set(self.app.application, user):
  80. csrf_token = self.get_csrf()
  81. data = {"csrf_token": csrf_token}
  82. tests.create_projects_git(os.path.join(self.path, "repos", "docs"))
  83. tests.create_projects_git(
  84. os.path.join(self.path, "repos", "requests")
  85. )
  86. # With the git repo
  87. output = self.app.post(
  88. "/test/settings/Pagure", 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. '<h5 class="pl-2 font-weight-bold text-muted">'
  94. "Project Settings</h5>\n",
  95. output_text,
  96. )
  97. self.assertIn("Hook Pagure deactivated", output_text)
  98. output = self.app.get("/test/settings/Pagure")
  99. self.assertEqual(output.status_code, 200)
  100. output_text = output.get_data(as_text=True)
  101. self.assertIn(
  102. "<title>Settings Pagure - test - Pagure</title>", output_text
  103. )
  104. self.assertIn(
  105. '<input class="form-check-input mt-2" id="active" name="active" '
  106. 'type="checkbox" value="y">',
  107. output_text,
  108. )
  109. self.assertFalse(
  110. os.path.exists(
  111. os.path.join(
  112. self.path,
  113. "repos",
  114. "test.git",
  115. "hooks",
  116. "post-receive.pagure",
  117. )
  118. )
  119. )
  120. self.assertFalse(
  121. os.path.exists(
  122. os.path.join(
  123. self.path, "repos", "test.git", "hooks", "post-receive"
  124. )
  125. )
  126. )
  127. self.assertFalse(
  128. os.path.exists(
  129. os.path.join(
  130. self.path, "docs", "test.git", "hooks", "post-receive"
  131. )
  132. )
  133. )
  134. def test_plugin_mail_activate_hook(self):
  135. """Test the pagure hook plugin endpoint when activating the hook."""
  136. pagure.config.config["DOCS_FOLDER"] = os.path.join(
  137. self.path, "repos", "docs"
  138. )
  139. user = tests.FakeUser(username="pingou")
  140. with tests.user_set(self.app.application, user):
  141. csrf_token = self.get_csrf()
  142. # Activate hook
  143. data = {"csrf_token": csrf_token, "active": "y"}
  144. output = self.app.post(
  145. "/test/settings/Pagure", data=data, follow_redirects=True
  146. )
  147. self.assertEqual(output.status_code, 200)
  148. output_text = output.get_data(as_text=True)
  149. self.assertIn(
  150. '<h5 class="pl-2 font-weight-bold text-muted">'
  151. "Project Settings</h5>\n",
  152. output_text,
  153. )
  154. self.assertIn("Hook Pagure activated", output_text)
  155. output = self.app.get("/test/settings/Pagure")
  156. self.assertEqual(output.status_code, 200)
  157. output_text = output.get_data(as_text=True)
  158. self.assertIn(
  159. "<title>Settings Pagure - test - Pagure</title>", output_text
  160. )
  161. self.assertIn(
  162. '<input checked class="form-check-input mt-2" id="active" name="active" '
  163. 'type="checkbox" value="y">',
  164. output_text,
  165. )
  166. self.assertTrue(
  167. os.path.exists(
  168. os.path.join(
  169. self.path, "repos", "test.git", "hooks", "post-receive"
  170. )
  171. )
  172. )
  173. self.assertTrue(
  174. os.path.exists(
  175. os.path.join(
  176. self.path,
  177. "repos",
  178. "docs",
  179. "test.git",
  180. "hooks",
  181. "post-receive",
  182. )
  183. )
  184. )
  185. def test_plugin_mail_deactivate_hook(self):
  186. """Test the pagure hook plugin endpoint when activating the hook."""
  187. self.test_plugin_mail_activate_hook()
  188. user = tests.FakeUser(username="pingou")
  189. with tests.user_set(self.app.application, user):
  190. csrf_token = self.get_csrf()
  191. # De-Activate hook
  192. data = {"csrf_token": csrf_token}
  193. output = self.app.post(
  194. "/test/settings/Pagure", 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. '<h5 class="pl-2 font-weight-bold text-muted">'
  200. "Project Settings</h5>\n",
  201. output_text,
  202. )
  203. self.assertIn("Hook Pagure deactivated", output_text)
  204. output = self.app.get("/test/settings/Pagure")
  205. self.assertEqual(output.status_code, 200)
  206. output_text = output.get_data(as_text=True)
  207. self.assertIn(
  208. "<title>Settings Pagure - test - Pagure</title>", output_text
  209. )
  210. self.assertIn(
  211. '<input class="form-check-input mt-2" id="active" name="active" '
  212. 'type="checkbox" value="y">',
  213. output_text,
  214. )
  215. self.assertFalse(
  216. os.path.exists(
  217. os.path.join(
  218. self.path,
  219. "repos",
  220. "test.git",
  221. "hooks",
  222. "post-receive.pagure",
  223. )
  224. )
  225. )
  226. self.assertTrue(
  227. os.path.exists(
  228. os.path.join(
  229. self.path, "repos", "test.git", "hooks", "post-receive"
  230. )
  231. )
  232. )
  233. self.assertTrue(
  234. os.path.exists(
  235. os.path.join(
  236. self.path,
  237. "repos",
  238. "docs",
  239. "test.git",
  240. "hooks",
  241. "post-receive",
  242. )
  243. )
  244. )
  245. @patch.dict("pagure.config.config", {"DOCS_FOLDER": None})
  246. def test_plugin_mail_activate_hook_no_doc(self):
  247. """Test the pagure hook plugin endpoint when activating the hook
  248. on a pagure instance that de-activated the doc repos.
  249. """
  250. user = tests.FakeUser(username="pingou")
  251. with tests.user_set(self.app.application, user):
  252. csrf_token = self.get_csrf()
  253. # Activate hook
  254. data = {"csrf_token": csrf_token, "active": "y"}
  255. output = self.app.post(
  256. "/test/settings/Pagure", data=data, follow_redirects=True
  257. )
  258. output_text = output.get_data(as_text=True)
  259. self.assertIn("Hook Pagure activated", output_text)
  260. self.assertTrue(
  261. os.path.exists(
  262. os.path.join(
  263. self.path, "repos", "test.git", "hooks", "post-receive"
  264. )
  265. )
  266. )
  267. self.assertFalse(
  268. os.path.exists(
  269. os.path.join(
  270. self.path, "docs", "test.git", "hooks", "post-receive"
  271. )
  272. )
  273. )
  274. @patch.dict("pagure.config.config", {"DOCS_FOLDER": None})
  275. def test_plugin_mail_deactivate_hook_no_doc(self):
  276. """Test the pagure hook plugin endpoint when activating then
  277. deactivating the hook on a pagure instance that de-activated the
  278. doc repos.
  279. """
  280. user = tests.FakeUser(username="pingou")
  281. with tests.user_set(self.app.application, user):
  282. csrf_token = self.get_csrf()
  283. # Activate hook
  284. data = {"csrf_token": csrf_token, "active": "y"}
  285. output = self.app.post(
  286. "/test/settings/Pagure", data=data, follow_redirects=True
  287. )
  288. self.assertEqual(output.status_code, 200)
  289. output_text = output.get_data(as_text=True)
  290. self.assertIn(
  291. '<h5 class="pl-2 font-weight-bold text-muted">'
  292. "Project Settings</h5>\n",
  293. output_text,
  294. )
  295. self.assertIn("Hook Pagure activated", output_text)
  296. self.assertTrue(
  297. os.path.exists(
  298. os.path.join(
  299. self.path, "repos", "test.git", "hooks", "post-receive"
  300. )
  301. )
  302. )
  303. self.assertFalse(
  304. os.path.exists(
  305. os.path.join(
  306. self.path, "docs", "test.git", "hooks", "post-receive"
  307. )
  308. )
  309. )
  310. # Deactivate hook
  311. data = {"csrf_token": csrf_token}
  312. output = self.app.post(
  313. "/test/settings/Pagure", data=data, follow_redirects=True
  314. )
  315. output_text = output.get_data(as_text=True)
  316. self.assertIn("Hook Pagure deactivated", output_text)
  317. self.assertFalse(
  318. os.path.exists(
  319. os.path.join(
  320. self.path,
  321. "repos",
  322. "test.git",
  323. "hooks",
  324. "post-receive.pagure",
  325. )
  326. )
  327. )
  328. self.assertTrue(
  329. os.path.exists(
  330. os.path.join(
  331. self.path, "repos", "test.git", "hooks", "post-receive"
  332. )
  333. )
  334. )
  335. self.assertFalse(
  336. os.path.exists(
  337. os.path.join(
  338. self.path, "docs", "test.git", "hooks", "post-receive"
  339. )
  340. )
  341. )
  342. if __name__ == "__main__":
  343. unittest.main(verbosity=2)