test_pagure_flask_ui_plugins_pagure_ci.py 12 KB

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