test_pagure_flask_ui_plugins_pagure_ci.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # -*- coding: utf-8 -*-
  2. __requires__ = ['SQLAlchemy >= 0.8']
  3. import unittest
  4. import sys
  5. import os
  6. import pagure.lib
  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. self.assertIn(
  19. '<div class="projectinfo m-t-1 m-b-1">\n'
  20. 'test project #1 </div>', output.data)
  21. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  22. self.assertIn(
  23. '<label for="ci_url">URL to the project on the CI '
  24. 'service</label>', output.data)
  25. self.assertIn(
  26. '<label for="ci_job">Name of the job to trigger'
  27. '</label>', output.data)
  28. self.assertIn(
  29. '<input class="form-control" id="active_commit" '
  30. 'name="active_commit" type="checkbox" value="y">', output.data)
  31. self.assertIn(
  32. '<input class="form-control" id="active_pr" '
  33. 'name="active_pr" type="checkbox" value="y">', output.data)
  34. csrf_token = output.data.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. self.assertIn(
  40. '<div class="projectinfo m-t-1 m-b-1">\n'
  41. 'test project #1 </div>', output.data)
  42. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  43. self.assertIn(
  44. '<label for="ci_url">URL to the project on the CI '
  45. 'service</label>', output.data)
  46. self.assertIn(
  47. '<label for="ci_job">Name of the job to trigger'
  48. '</label>', output.data)
  49. self.assertIn(
  50. '<input class="form-control" id="active_commit" '
  51. 'name="active_commit" type="checkbox" value="y">', output.data)
  52. self.assertIn(
  53. '<input class="form-control" id="active_pr" '
  54. 'name="active_pr" type="checkbox" value="y">', output.data)
  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. self.assertIn(
  67. '<div class="projectinfo m-t-1 m-b-1">\n'
  68. 'test project #1 </div>', output.data)
  69. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  70. self.assertIn(
  71. '<label for="ci_url">URL to the project on the CI '
  72. 'service</label>', output.data)
  73. self.assertIn(
  74. '<label for="ci_job">Name of the job to trigger'
  75. '</label>', output.data)
  76. self.assertIn(
  77. '<input checked class="form-control" id="active_commit" '
  78. 'name="active_commit" type="checkbox" value="y">', output.data)
  79. self.assertIn(
  80. '<input class="form-control" id="active_pr" '
  81. 'name="active_pr" type="checkbox" value="y">', output.data)
  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. self.assertIn(
  88. '<div class="projectinfo m-t-1 m-b-1">\n'
  89. 'test project #1 </div>', output.data)
  90. self.assertIn(
  91. '<title>Settings - test - Pagure</title>', output.data)
  92. self.assertIn('<h3>Settings for test</h3>', output.data)
  93. self.assertIn(
  94. '</button>\n Hook Pagure CI activated',
  95. output.data)
  96. output = self.app.get('/test/settings/Pagure CI')
  97. self.assertEqual(output.status_code, 200)
  98. self.assertIn(
  99. '<div class="projectinfo m-t-1 m-b-1">\n'
  100. 'test project #1 </div>', output.data)
  101. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  102. self.assertIn(
  103. '<label for="ci_url">URL to the project on the CI '
  104. 'service</label>', output.data)
  105. self.assertIn(
  106. '<label for="ci_job">Name of the job to trigger'
  107. '</label>', output.data)
  108. self.assertIn(
  109. '<input checked class="form-control" id="active_commit" '
  110. 'name="active_commit" type="checkbox" value="y">', output.data)
  111. self.assertIn(
  112. '<pre>\nhttps://pagure.org/api/0/ci/jenkins/test/',
  113. output.data)
  114. # De-activate the hook
  115. data = {
  116. 'csrf_token': csrf_token,
  117. }
  118. output = self.app.post(
  119. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  120. self.assertEqual(output.status_code, 200)
  121. self.assertIn(
  122. '</button>\n Hook Pagure CI deactivated',
  123. output.data)
  124. self.assertIn(
  125. '<section class="settings">\n <h3>Settings for test</h3>',
  126. output.data)
  127. output = self.app.get('/test/settings/Pagure CI')
  128. self.assertEqual(output.status_code, 200)
  129. self.assertIn(
  130. '<div class="projectinfo m-t-1 m-b-1">\n'
  131. 'test project #1 </div>', output.data)
  132. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  133. self.assertIn(
  134. '<label for="ci_url">URL to the project on the CI '
  135. 'service</label>', output.data)
  136. self.assertIn(
  137. '<label for="ci_job">Name of the job to trigger'
  138. '</label>', output.data)
  139. self.assertIn(
  140. '<input class="form-control" id="active_commit" '
  141. 'name="active_commit" type="checkbox" value="y">', output.data)
  142. # Missing the required ci_url
  143. data = {'csrf_token': csrf_token, 'active_commit': 'y'}
  144. output = self.app.post(
  145. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  146. self.assertEqual(output.status_code, 200)
  147. self.assertIn(
  148. '<div class="projectinfo m-t-1 m-b-1">\n'
  149. 'test project #1 </div>', output.data)
  150. self.assertIn('<h3>Pagure CI settings</h3>', output.data)
  151. self.assertFalse(
  152. '</button>\n Hook activated' in output.data)
  153. self.assertIn(
  154. '<td><input class="form-control" id="ci_url" name="ci_url" type="text" value="">'
  155. '</td>\n<td class="errors">This field is required.</td>',
  156. output.data)
  157. self.assertIn(
  158. '<td><input class="form-control" id="ci_job" name="ci_job" type="text" value="">'
  159. '</td>\n<td class="errors">This field is required.</td>',
  160. output.data)
  161. self.assertIn(
  162. '<input checked class="form-control" id="active_commit" '
  163. 'name="active_commit" type="checkbox" value="y">', output.data)
  164. def test_plugin_pagure_ci_namespaced(self):
  165. """ Test the pagure ci plugin on/off endpoint. """
  166. tests.create_projects(self.session)
  167. tests.create_projects_git(os.path.join(self.path, 'repos'))
  168. user = tests.FakeUser(username='pingou')
  169. with tests.user_set(self.app.application, user):
  170. output = self.app.get('/somenamespace/test3/settings/Pagure CI')
  171. self.assertEqual(output.status_code, 200)
  172. self.assertIn(
  173. '<div class="projectinfo m-t-1 m-b-1">\n'
  174. 'namespaced test project </div>', output.data)
  175. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  176. self.assertIn(
  177. '<label for="ci_url">URL to the project on the CI '
  178. 'service</label>', output.data)
  179. self.assertIn(
  180. '<label for="ci_job">Name of the job to trigger'
  181. '</label>', output.data)
  182. self.assertIn(
  183. '<input class="form-control" id="active_pr" name="active_pr" '
  184. 'type="checkbox" value="y">', output.data)
  185. csrf_token = output.data.split(
  186. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  187. # Activate hook
  188. data = {
  189. 'active_pr': 'y',
  190. 'ci_url': 'https://jenkins.fedoraproject.org',
  191. 'ci_job': 'test/job',
  192. 'ci_type': 'jenkins',
  193. 'csrf_token': csrf_token,
  194. }
  195. # Activate hook
  196. output = self.app.post(
  197. '/somenamespace/test3/settings/Pagure CI', data=data, follow_redirects=True)
  198. self.assertEqual(output.status_code, 200)
  199. self.assertIn(
  200. '<div class="projectinfo m-t-1 m-b-1">\n'
  201. 'namespaced test project </div>', output.data)
  202. self.assertIn(
  203. '<title>Settings - somenamespace/test3 - Pagure</title>', output.data)
  204. self.assertIn('<h3>Settings for somenamespace/test3</h3>', output.data)
  205. self.assertIn(
  206. '</button>\n Hook Pagure CI activated',
  207. output.data)
  208. output = self.app.get('/somenamespace/test3/settings/Pagure CI')
  209. self.assertEqual(output.status_code, 200)
  210. self.assertIn(
  211. '<div class="projectinfo m-t-1 m-b-1">\n'
  212. 'namespaced test project </div>', output.data)
  213. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  214. self.assertIn(
  215. '<label for="ci_url">URL to the project on the CI '
  216. 'service</label>', output.data)
  217. self.assertIn(
  218. '<label for="ci_job">Name of the job to trigger'
  219. '</label>', output.data)
  220. self.assertTrue(
  221. '<input checked class="form-control" id="active_pr" name="active_pr" '
  222. 'type="checkbox" value="y">' in output.data)
  223. self.assertIn(
  224. '<pre>\nhttps://pagure.org/api/0/ci/jenkins/somenamespace/test3/',
  225. output.data)
  226. if __name__ == '__main__':
  227. unittest.main(verbosity=2)