test_pagure_flask_ui_plugins_pagure_hook.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. """
  137. pagure.config.config["DOCS_FOLDER"] = os.path.join(
  138. self.path, "repos", "docs"
  139. )
  140. user = tests.FakeUser(username="pingou")
  141. with tests.user_set(self.app.application, user):
  142. csrf_token = self.get_csrf()
  143. # Activate hook
  144. data = {"csrf_token": csrf_token, "active": "y"}
  145. output = self.app.post(
  146. "/test/settings/Pagure", data=data, follow_redirects=True
  147. )
  148. self.assertEqual(output.status_code, 200)
  149. output_text = output.get_data(as_text=True)
  150. self.assertIn(
  151. '<h5 class="pl-2 font-weight-bold text-muted">'
  152. "Project Settings</h5>\n",
  153. output_text,
  154. )
  155. self.assertIn("Hook Pagure activated", output_text)
  156. output = self.app.get("/test/settings/Pagure")
  157. self.assertEqual(output.status_code, 200)
  158. output_text = output.get_data(as_text=True)
  159. self.assertIn(
  160. "<title>Settings Pagure - test - Pagure</title>", output_text
  161. )
  162. self.assertIn(
  163. '<input checked class="form-check-input mt-2" id="active" name="active" '
  164. 'type="checkbox" value="y">',
  165. output_text,
  166. )
  167. self.assertTrue(
  168. os.path.exists(
  169. os.path.join(
  170. self.path, "repos", "test.git", "hooks", "post-receive"
  171. )
  172. )
  173. )
  174. self.assertTrue(
  175. os.path.exists(
  176. os.path.join(
  177. self.path,
  178. "repos",
  179. "docs",
  180. "test.git",
  181. "hooks",
  182. "post-receive",
  183. )
  184. )
  185. )
  186. def test_plugin_mail_deactivate_hook(self):
  187. """ Test the pagure hook plugin endpoint when activating the hook.
  188. """
  189. self.test_plugin_mail_activate_hook()
  190. user = tests.FakeUser(username="pingou")
  191. with tests.user_set(self.app.application, user):
  192. csrf_token = self.get_csrf()
  193. # De-Activate hook
  194. data = {"csrf_token": csrf_token}
  195. output = self.app.post(
  196. "/test/settings/Pagure", data=data, follow_redirects=True
  197. )
  198. self.assertEqual(output.status_code, 200)
  199. output_text = output.get_data(as_text=True)
  200. self.assertIn(
  201. '<h5 class="pl-2 font-weight-bold text-muted">'
  202. "Project Settings</h5>\n",
  203. output_text,
  204. )
  205. self.assertIn("Hook Pagure deactivated", output_text)
  206. output = self.app.get("/test/settings/Pagure")
  207. self.assertEqual(output.status_code, 200)
  208. output_text = output.get_data(as_text=True)
  209. self.assertIn(
  210. "<title>Settings Pagure - test - Pagure</title>", output_text
  211. )
  212. self.assertIn(
  213. '<input class="form-check-input mt-2" id="active" name="active" '
  214. 'type="checkbox" value="y">',
  215. output_text,
  216. )
  217. self.assertFalse(
  218. os.path.exists(
  219. os.path.join(
  220. self.path,
  221. "repos",
  222. "test.git",
  223. "hooks",
  224. "post-receive.pagure",
  225. )
  226. )
  227. )
  228. self.assertTrue(
  229. os.path.exists(
  230. os.path.join(
  231. self.path, "repos", "test.git", "hooks", "post-receive"
  232. )
  233. )
  234. )
  235. self.assertTrue(
  236. os.path.exists(
  237. os.path.join(
  238. self.path,
  239. "repos",
  240. "docs",
  241. "test.git",
  242. "hooks",
  243. "post-receive",
  244. )
  245. )
  246. )
  247. @patch.dict("pagure.config.config", {"DOCS_FOLDER": None})
  248. def test_plugin_mail_activate_hook_no_doc(self):
  249. """ Test the pagure hook plugin endpoint when activating the hook
  250. on a pagure instance that de-activated the doc repos.
  251. """
  252. user = tests.FakeUser(username="pingou")
  253. with tests.user_set(self.app.application, user):
  254. csrf_token = self.get_csrf()
  255. # Activate hook
  256. data = {"csrf_token": csrf_token, "active": "y"}
  257. output = self.app.post(
  258. "/test/settings/Pagure", data=data, follow_redirects=True
  259. )
  260. output_text = output.get_data(as_text=True)
  261. self.assertIn("Hook Pagure activated", output_text)
  262. self.assertTrue(
  263. os.path.exists(
  264. os.path.join(
  265. self.path, "repos", "test.git", "hooks", "post-receive"
  266. )
  267. )
  268. )
  269. self.assertFalse(
  270. os.path.exists(
  271. os.path.join(
  272. self.path, "docs", "test.git", "hooks", "post-receive"
  273. )
  274. )
  275. )
  276. @patch.dict("pagure.config.config", {"DOCS_FOLDER": None})
  277. def test_plugin_mail_deactivate_hook_no_doc(self):
  278. """ Test the pagure hook plugin endpoint when activating then
  279. deactivating the hook on a pagure instance that de-activated the
  280. doc repos.
  281. """
  282. user = tests.FakeUser(username="pingou")
  283. with tests.user_set(self.app.application, user):
  284. csrf_token = self.get_csrf()
  285. # Activate hook
  286. data = {"csrf_token": csrf_token, "active": "y"}
  287. output = self.app.post(
  288. "/test/settings/Pagure", data=data, follow_redirects=True
  289. )
  290. self.assertEqual(output.status_code, 200)
  291. output_text = output.get_data(as_text=True)
  292. self.assertIn(
  293. '<h5 class="pl-2 font-weight-bold text-muted">'
  294. "Project Settings</h5>\n",
  295. output_text,
  296. )
  297. self.assertIn("Hook Pagure activated", output_text)
  298. self.assertTrue(
  299. os.path.exists(
  300. os.path.join(
  301. self.path, "repos", "test.git", "hooks", "post-receive"
  302. )
  303. )
  304. )
  305. self.assertFalse(
  306. os.path.exists(
  307. os.path.join(
  308. self.path, "docs", "test.git", "hooks", "post-receive"
  309. )
  310. )
  311. )
  312. # Deactivate hook
  313. data = {"csrf_token": csrf_token}
  314. output = self.app.post(
  315. "/test/settings/Pagure", data=data, follow_redirects=True
  316. )
  317. output_text = output.get_data(as_text=True)
  318. self.assertIn("Hook Pagure deactivated", output_text)
  319. self.assertFalse(
  320. os.path.exists(
  321. os.path.join(
  322. self.path,
  323. "repos",
  324. "test.git",
  325. "hooks",
  326. "post-receive.pagure",
  327. )
  328. )
  329. )
  330. self.assertTrue(
  331. os.path.exists(
  332. os.path.join(
  333. self.path, "repos", "test.git", "hooks", "post-receive"
  334. )
  335. )
  336. )
  337. self.assertFalse(
  338. os.path.exists(
  339. os.path.join(
  340. self.path, "docs", "test.git", "hooks", "post-receive"
  341. )
  342. )
  343. )
  344. if __name__ == "__main__":
  345. unittest.main(verbosity=2)