test_pagure_flask_ui_plugins_pagure_ci.py 12 KB

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