test_pagure_flask_ui_plugins_pagure_ci.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals, absolute_import
  3. import unittest
  4. import sys
  5. import os
  6. import tests
  7. class PagureFlaskPluginPagureCItests(tests.SimplePagureTest):
  8. """ Tests for flask plugins controller of pagure """
  9. def test_plugin_pagure_ci(self):
  10. """ Test the pagure ci plugin on/off endpoint. """
  11. tests.create_projects(self.session)
  12. tests.create_projects_git(os.path.join(self.path, 'repos'))
  13. user = tests.FakeUser(username='pingou')
  14. with tests.user_set(self.app.application, user):
  15. output = self.app.get('/test/settings/Pagure CI')
  16. self.assertEqual(output.status_code, 200)
  17. output_text = output.get_data(as_text=True)
  18. self.assertIn(
  19. '<title>Settings Pagure CI - test - Pagure</title>',
  20. output_text)
  21. self.assertIn(
  22. '<label for="ci_url">URL to the project on the CI '
  23. 'service</label>', output_text)
  24. self.assertIn(
  25. '<label for="ci_job">Name of the job to trigger'
  26. '</label>', output_text)
  27. self.assertIn(
  28. '<input class="form-check-input mt-2" id="active_commit" '
  29. 'name="active_commit" type="checkbox" value="y">', output_text)
  30. self.assertIn(
  31. '<input class="form-check-input mt-2" id="active_pr" '
  32. 'name="active_pr" type="checkbox" value="y">', output_text)
  33. csrf_token = output_text.split(
  34. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  35. data = {}
  36. output = self.app.post('/test/settings/Pagure CI', data=data)
  37. self.assertEqual(output.status_code, 200)
  38. output_text = output.get_data(as_text=True)
  39. self.assertIn(
  40. '<title>Settings Pagure CI - test - Pagure</title>',
  41. output_text)
  42. self.assertIn(
  43. '<label for="ci_url">URL to the project on the CI '
  44. 'service</label>', output_text)
  45. self.assertIn(
  46. '<label for="ci_job">Name of the job to trigger'
  47. '</label>', output_text)
  48. self.assertIn(
  49. '<input class="form-check-input mt-2" id="active_commit" '
  50. 'name="active_commit" type="checkbox" value="y">', output_text)
  51. self.assertIn(
  52. '<input class="form-check-input mt-2" id="active_pr" '
  53. 'name="active_pr" type="checkbox" value="y">', output_text)
  54. # Activate hook
  55. data = {
  56. 'active_commit': 'y',
  57. 'ci_url': 'https://jenkins.fedoraproject.org',
  58. 'ci_type': 'jenkins',
  59. 'ci_job': 'test/job'
  60. }
  61. # CSRF Token missing
  62. output = self.app.post(
  63. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  64. self.assertEqual(output.status_code, 200)
  65. output_text = output.get_data(as_text=True)
  66. self.assertIn(
  67. '<title>Settings Pagure CI - test - Pagure</title>',
  68. output_text)
  69. self.assertIn(
  70. '<label for="ci_url">URL to the project on the CI '
  71. 'service</label>', output_text)
  72. self.assertIn(
  73. '<label for="ci_job">Name of the job to trigger'
  74. '</label>', output_text)
  75. self.assertIn(
  76. '<input checked class="form-check-input mt-2" id="active_commit" '
  77. 'name="active_commit" type="checkbox" value="y">', output_text)
  78. self.assertIn(
  79. '<input class="form-check-input mt-2" id="active_pr" '
  80. 'name="active_pr" type="checkbox" value="y">', output_text)
  81. data['csrf_token'] = csrf_token
  82. # Activate hook
  83. output = self.app.post(
  84. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  85. self.assertEqual(output.status_code, 200)
  86. output_text = output.get_data(as_text=True)
  87. self.assertIn(
  88. '<h5 class="pl-2 font-weight-bold text-muted">'
  89. 'Project Settings</h5>\n', output_text)
  90. self.assertIn(
  91. '<title>Settings - test - Pagure</title>', output_text)
  92. self.assertIn(
  93. '<h5 class="pl-2 font-weight-bold text-muted">'
  94. 'Project Settings</h5>\n', output_text)
  95. self.assertIn(
  96. 'Hook Pagure CI activated',
  97. output_text)
  98. output = self.app.get('/test/settings/Pagure CI')
  99. self.assertEqual(output.status_code, 200)
  100. output_text = output.get_data(as_text=True)
  101. self.assertIn(
  102. '<title>Settings Pagure CI - test - Pagure</title>',
  103. output_text)
  104. self.assertIn(
  105. '<label for="ci_url">URL to the project on the CI '
  106. 'service</label>', output_text)
  107. self.assertIn(
  108. '<label for="ci_job">Name of the job to trigger'
  109. '</label>', output_text)
  110. self.assertIn(
  111. '<input checked class="form-check-input mt-2" id="active_commit" '
  112. 'name="active_commit" type="checkbox" value="y">', output_text)
  113. self.assertIn(
  114. '<pre>\nhttp://localhost.localdomain/api/0/ci/jenkins/test/',
  115. output_text)
  116. # De-activate the hook
  117. data = {
  118. 'csrf_token': csrf_token,
  119. }
  120. output = self.app.post(
  121. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  122. self.assertEqual(output.status_code, 200)
  123. output_text = output.get_data(as_text=True)
  124. self.assertIn(
  125. '<h5 class="pl-2 font-weight-bold text-muted">'
  126. 'Project Settings</h5>\n', output_text)
  127. self.assertIn(
  128. 'Hook Pagure CI deactivated',
  129. output_text)
  130. output = self.app.get('/test/settings/Pagure CI')
  131. self.assertEqual(output.status_code, 200)
  132. output_text = output.get_data(as_text=True)
  133. self.assertIn(
  134. '<title>Settings Pagure CI - test - Pagure</title>',
  135. output_text)
  136. self.assertIn(
  137. '<label for="ci_url">URL to the project on the CI '
  138. 'service</label>', output_text)
  139. self.assertIn(
  140. '<label for="ci_job">Name of the job to trigger'
  141. '</label>', output_text)
  142. self.assertIn(
  143. '<input class="form-check-input mt-2" id="active_commit" '
  144. 'name="active_commit" type="checkbox" value="y">', output_text)
  145. # Missing the required ci_url
  146. data = {'csrf_token': csrf_token, 'active_commit': 'y'}
  147. output = self.app.post(
  148. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  149. self.assertEqual(output.status_code, 200)
  150. output_text = output.get_data(as_text=True)
  151. self.assertIn(
  152. '<title>Settings Pagure CI - test - Pagure</title>',
  153. output_text)
  154. self.assertNotIn(
  155. 'Hook Pagure CI activated',
  156. output_text)
  157. if self.get_wtforms_version() >= (2, 2):
  158. self.assertIn(
  159. '<div class="col-sm-10">\n '
  160. '<input class="form-control pl-0" id="ci_url" name="ci_url" '
  161. 'required type="text" value="">\n </div>\n </div>\n '
  162. '<div class="alert alert-danger">This field is required.</div>',
  163. output_text)
  164. self.assertIn(
  165. '<div class="col-sm-10">\n '
  166. '<input class="form-control pl-0" id="ci_job" name="ci_job" '
  167. 'required type="text" value="">\n </div>\n </div>\n '
  168. '<div class="alert alert-danger">This field is required.</div>',
  169. output_text)
  170. else:
  171. self.assertIn(
  172. '<div class="col-sm-10">\n '
  173. '<input class="form-control pl-0" id="ci_url" name="ci_url" '
  174. 'type="text" value="">\n </div>\n </div>\n '
  175. '<div class="alert alert-danger">This field is required.</div>',
  176. output_text)
  177. self.assertIn(
  178. '<div class="col-sm-10">\n '
  179. '<input class="form-control pl-0" id="ci_job" name="ci_job" '
  180. 'type="text" value="">\n </div>\n </div>\n '
  181. '<div class="alert alert-danger">This field is required.</div>',
  182. output_text)
  183. self.assertIn(
  184. '<input checked class="form-check-input mt-2" id="active_commit" '
  185. 'name="active_commit" type="checkbox" value="y">', output_text)
  186. def test_plugin_pagure_ci_namespaced(self):
  187. """ Test the pagure ci plugin on/off endpoint. """
  188. tests.create_projects(self.session)
  189. tests.create_projects_git(os.path.join(self.path, 'repos'))
  190. user = tests.FakeUser(username='pingou')
  191. with tests.user_set(self.app.application, user):
  192. output = self.app.get('/somenamespace/test3/settings/Pagure CI')
  193. self.assertEqual(output.status_code, 200)
  194. output_text = output.get_data(as_text=True)
  195. self.assertIn(
  196. '<title>Settings Pagure CI - somenamespace/test3 - '
  197. 'Pagure</title>', output_text)
  198. self.assertIn(
  199. '<label for="ci_url">URL to the project on the CI '
  200. 'service</label>', output_text)
  201. self.assertIn(
  202. '<label for="ci_job">Name of the job to trigger'
  203. '</label>', output_text)
  204. self.assertIn(
  205. '<input class="form-check-input mt-2" id="active_pr" name="active_pr" '
  206. 'type="checkbox" value="y">', output_text)
  207. csrf_token = output_text.split(
  208. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  209. # Activate hook
  210. data = {
  211. 'active_pr': 'y',
  212. 'ci_url': 'https://jenkins.fedoraproject.org',
  213. 'ci_job': 'test/job',
  214. 'ci_type': 'jenkins',
  215. 'csrf_token': csrf_token,
  216. }
  217. # Activate hook
  218. output = self.app.post(
  219. '/somenamespace/test3/settings/Pagure CI', data=data, follow_redirects=True)
  220. self.assertEqual(output.status_code, 200)
  221. output_text = output.get_data(as_text=True)
  222. self.assertIn(
  223. '<h5 class="pl-2 font-weight-bold text-muted">'
  224. 'Project Settings</h5>\n', output_text)
  225. self.assertIn(
  226. '<title>Settings - somenamespace/test3 - Pagure</title>',
  227. output_text)
  228. self.assertIn(
  229. '<h5 class="pl-2 font-weight-bold text-muted">'
  230. 'Project Settings</h5>\n', output_text)
  231. self.assertIn(
  232. 'Hook Pagure CI activated',
  233. output_text)
  234. output = self.app.get('/somenamespace/test3/settings/Pagure CI')
  235. self.assertEqual(output.status_code, 200)
  236. output_text = output.get_data(as_text=True)
  237. self.assertIn(
  238. '<title>Settings Pagure CI - somenamespace/test3 - '
  239. 'Pagure</title>', output_text)
  240. self.assertIn(
  241. '<label for="ci_url">URL to the project on the CI '
  242. 'service</label>', output_text)
  243. self.assertIn(
  244. '<label for="ci_job">Name of the job to trigger'
  245. '</label>', output_text)
  246. self.assertIn(
  247. '<input checked class="form-check-input mt-2" id="active_pr" name="active_pr" '
  248. 'type="checkbox" value="y">', output_text)
  249. self.assertIn(
  250. '<pre>\nhttp://localhost.localdomain/api/0/ci/jenkins/somenamespace/test3/',
  251. output_text)
  252. if __name__ == '__main__':
  253. unittest.main(verbosity=2)