1
0

test_pagure_flask_ui_plugins_noff.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016-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. sys.path.insert(
  12. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  13. )
  14. import tests
  15. class PagureFlaskPluginNoFFtests(tests.SimplePagureTest):
  16. """Tests for Block non fast-forward pushes plugin of pagure"""
  17. def test_plugin_noff(self):
  18. """Test the noff plugin on/off endpoint."""
  19. tests.create_projects(self.session)
  20. tests.create_projects_git(os.path.join(self.path, "repos"))
  21. user = tests.FakeUser(username="pingou")
  22. with tests.user_set(self.app.application, user):
  23. output = self.app.get(
  24. "/test/settings/Block non fast-forward pushes"
  25. )
  26. self.assertEqual(output.status_code, 200)
  27. output_text = output.get_data(as_text=True)
  28. self.assertIn(
  29. "<title>Settings Block non fast-forward pushes - test - "
  30. "Pagure</title>",
  31. output_text,
  32. )
  33. if self.get_wtforms_version() >= (2, 2):
  34. self.assertIn(
  35. '<input class="form-control pl-0" id="branches" name="branches" '
  36. 'required type="text" value="">',
  37. output_text,
  38. )
  39. else:
  40. self.assertIn(
  41. '<input class="form-control pl-0" id="branches" name="branches" '
  42. 'type="text" value="">',
  43. output_text,
  44. )
  45. self.assertTrue(
  46. '<input class="form-check-input mt-2" id="active" name="active" '
  47. 'type="checkbox" value="y">' in output_text
  48. )
  49. csrf_token = output_text.split(
  50. 'name="csrf_token" type="hidden" value="'
  51. )[1].split('">')[0]
  52. data = {}
  53. output = self.app.post(
  54. "/test/settings/Block non fast-forward pushes", data=data
  55. )
  56. self.assertEqual(output.status_code, 200)
  57. output_text = output.get_data(as_text=True)
  58. self.assertIn(
  59. "<title>Settings Block non fast-forward pushes - test - "
  60. "Pagure</title>",
  61. output_text,
  62. )
  63. if self.get_wtforms_version() >= (2, 2):
  64. self.assertIn(
  65. '<input class="form-control pl-0" id="branches" name="branches" '
  66. 'required type="text" value="">',
  67. output_text,
  68. )
  69. else:
  70. self.assertIn(
  71. '<input class="form-control pl-0" id="branches" name="branches" '
  72. 'type="text" value="">',
  73. output_text,
  74. )
  75. self.assertTrue(
  76. '<input class="form-check-input mt-2" id="active" name="active" '
  77. 'type="checkbox" value="y">' in output_text
  78. )
  79. data["csrf_token"] = csrf_token
  80. # With the git repo
  81. output = self.app.post(
  82. "/test/settings/Block non fast-forward pushes",
  83. data=data,
  84. follow_redirects=True,
  85. )
  86. self.assertEqual(output.status_code, 200)
  87. output_text = output.get_data(as_text=True)
  88. self.assertIn(
  89. '<h5 class="pl-2 font-weight-bold text-muted">'
  90. "Project Settings</h5>\n",
  91. output_text,
  92. )
  93. self.assertTrue(
  94. "Hook Block non "
  95. "fast-forward pushes deactivated" in output_text
  96. )
  97. output = self.app.get(
  98. "/test/settings/Block non fast-forward pushes"
  99. )
  100. self.assertEqual(output.status_code, 200)
  101. output_text = output.get_data(as_text=True)
  102. self.assertIn(
  103. "<title>Settings Block non fast-forward pushes - test - "
  104. "Pagure</title>",
  105. output_text,
  106. )
  107. if self.get_wtforms_version() >= (2, 2):
  108. self.assertIn(
  109. '<input class="form-control pl-0" id="branches" name="branches" '
  110. 'required type="text" value="">',
  111. output_text,
  112. )
  113. else:
  114. self.assertIn(
  115. '<input class="form-control pl-0" id="branches" name="branches" '
  116. 'type="text" value="">',
  117. output_text,
  118. )
  119. self.assertTrue(
  120. '<input class="form-check-input mt-2" id="active" name="active" '
  121. 'type="checkbox" value="y">' in output_text
  122. )
  123. self.assertFalse(
  124. os.path.exists(
  125. os.path.join(
  126. self.path,
  127. "repos",
  128. "test.git",
  129. "hooks",
  130. "post-receive.mail",
  131. )
  132. )
  133. )
  134. # Missing the required mail_to
  135. data = {"csrf_token": csrf_token, "active": "y"}
  136. output = self.app.post(
  137. "/test/settings/Block non fast-forward pushes",
  138. data=data,
  139. follow_redirects=True,
  140. )
  141. self.assertEqual(output.status_code, 200)
  142. output_text = output.get_data(as_text=True)
  143. self.assertIn(
  144. "<title>Settings Block non fast-forward pushes - test - "
  145. "Pagure</title>",
  146. output_text,
  147. )
  148. self.assertNotIn("Hook activated", output_text)
  149. if self.get_wtforms_version() >= (2, 2):
  150. self.assertIn(
  151. '<input class="form-control pl-0" id="branches" name="branches" '
  152. 'required type="text" value="">',
  153. output_text,
  154. )
  155. else:
  156. self.assertIn(
  157. '<input class="form-control pl-0" id="branches" name="branches" '
  158. 'type="text" value="">',
  159. output_text,
  160. )
  161. self.assertTrue(
  162. '<input checked class="form-check-input mt-2" id="active" name="active" '
  163. 'type="checkbox" value="y">' in output_text
  164. )
  165. self.assertFalse(
  166. os.path.exists(
  167. os.path.join(
  168. self.path,
  169. "repos",
  170. "test.git",
  171. "hooks",
  172. "pre-receive.pagureforcecommit",
  173. )
  174. )
  175. )
  176. # Activate hook
  177. data = {
  178. "csrf_token": csrf_token,
  179. "active": "y",
  180. "branches": "master",
  181. }
  182. output = self.app.post(
  183. "/test/settings/Block non fast-forward pushes",
  184. data=data,
  185. follow_redirects=True,
  186. )
  187. self.assertEqual(output.status_code, 200)
  188. output_text = output.get_data(as_text=True)
  189. self.assertIn(
  190. '<h5 class="pl-2 font-weight-bold text-muted">'
  191. "Project Settings</h5>\n",
  192. output_text,
  193. )
  194. self.assertIn(
  195. "Hook Block non " "fast-forward pushes activated", output_text
  196. )
  197. output = self.app.get(
  198. "/test/settings/Block non fast-forward pushes"
  199. )
  200. output_text = output.get_data(as_text=True)
  201. self.assertIn(
  202. "<title>Settings Block non fast-forward pushes - test - "
  203. "Pagure</title>",
  204. output_text,
  205. )
  206. if self.get_wtforms_version() >= (2, 2):
  207. self.assertIn(
  208. '<input class="form-control pl-0" id="branches" name="branches" '
  209. 'required type="text" value="master">',
  210. output_text,
  211. )
  212. else:
  213. self.assertIn(
  214. '<input class="form-control pl-0" id="branches" name="branches" '
  215. 'type="text" value="master">',
  216. output_text,
  217. )
  218. self.assertIn(
  219. '<input checked class="form-check-input mt-2" id="active" name="active" '
  220. 'type="checkbox" value="y">',
  221. output_text,
  222. )
  223. # De-Activate hook
  224. data = {"csrf_token": csrf_token}
  225. output = self.app.post(
  226. "/test/settings/Block non fast-forward pushes",
  227. data=data,
  228. follow_redirects=True,
  229. )
  230. self.assertEqual(output.status_code, 200)
  231. output_text = output.get_data(as_text=True)
  232. self.assertIn(
  233. '<h5 class="pl-2 font-weight-bold text-muted">'
  234. "Project Settings</h5>\n",
  235. output_text,
  236. )
  237. self.assertIn(
  238. "Hook Block non " "fast-forward pushes deactivated",
  239. output_text,
  240. )
  241. output = self.app.get(
  242. "/test/settings/Block non fast-forward pushes"
  243. )
  244. output_text = output.get_data(as_text=True)
  245. self.assertIn(
  246. "<title>Settings Block non fast-forward pushes - test - "
  247. "Pagure</title>",
  248. output_text,
  249. )
  250. if self.get_wtforms_version() >= (2, 2):
  251. self.assertIn(
  252. '<input class="form-control pl-0" id="branches" name="branches" '
  253. 'required type="text" value="">',
  254. output_text,
  255. )
  256. else:
  257. self.assertIn(
  258. '<input class="form-control pl-0" id="branches" name="branches" '
  259. 'type="text" value="">',
  260. output_text,
  261. )
  262. self.assertIn(
  263. '<input class="form-check-input mt-2" id="active" name="active" '
  264. 'type="checkbox" value="y">',
  265. output_text,
  266. )
  267. self.assertFalse(
  268. os.path.exists(
  269. os.path.join(
  270. self.path,
  271. "repos",
  272. "test.git",
  273. "hooks",
  274. "pre-receive.pagureforcecommit",
  275. )
  276. )
  277. )
  278. if __name__ == "__main__":
  279. unittest.main(verbosity=2)