test_pagure_flask_ui_plugins_pagure_ci.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # -*- coding: utf-8 -*-
  2. __requires__ = ['SQLAlchemy >= 0.8']
  3. import pkg_resources
  4. import json
  5. import unittest
  6. import shutil
  7. import sys
  8. import os
  9. import pygit2
  10. from mock import patch
  11. sys.path.insert(0, os.path.join(os.path.dirname(
  12. os.path.abspath(__file__)), '..'))
  13. import pagure.lib
  14. import tests
  15. class PagureFlaskPluginPagureCItests(tests.Modeltests):
  16. """ Tests for flask plugins controller of pagure """
  17. def setUp(self):
  18. """ Set up the environnment, ran before every tests. """
  19. super(PagureFlaskPluginPagureCItests, self).setUp()
  20. pagure.APP.config['TESTING'] = True
  21. pagure.SESSION = self.session
  22. pagure.ui.SESSION = self.session
  23. pagure.ui.app.SESSION = self.session
  24. pagure.ui.plugins.SESSION = self.session
  25. pagure.ui.repo.SESSION = self.session
  26. pagure.ui.filters.SESSION = self.session
  27. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  28. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  29. tests.HERE, 'forks')
  30. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  31. tests.HERE, 'tickets')
  32. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  33. tests.HERE, 'docs')
  34. self.app = pagure.APP.test_client()
  35. def test_plugin_pagure_ci(self):
  36. """ Test the pagure ci plugin on/off endpoint. """
  37. tests.create_projects(self.session)
  38. user = tests.FakeUser(username='pingou')
  39. with tests.user_set(pagure.APP, user):
  40. output = self.app.get('/test/settings/Pagure CI')
  41. self.assertEqual(output.status_code, 200)
  42. self.assertIn(
  43. '<div class="projectinfo m-t-1 m-b-1">\n'
  44. 'test project #1 </div>', output.data)
  45. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  46. self.assertTrue(
  47. '<td><label for="pagure_name">Name of project in Pagure</label></td>'
  48. in output.data)
  49. self.assertTrue(
  50. '<td><label for="jenkins_name">Name of project in Jenkins</label></td>'
  51. in output.data)
  52. self.assertTrue(
  53. '<td><label for="jenkins_url">Jenkins URL</label></td>'
  54. in output.data)
  55. self.assertTrue(
  56. '<td><label for="jenkins_token">Jenkins token</label></td>'
  57. in output.data)
  58. self.assertTrue(
  59. '<input id="active" name="active" type="checkbox" value="y">'
  60. in output.data)
  61. csrf_token = output.data.split(
  62. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  63. data = {}
  64. output = self.app.post('/test/settings/Pagure CI', data=data)
  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.assertTrue(
  71. '<td><label for="pagure_name">Name of project in Pagure</label></td>'
  72. in output.data)
  73. self.assertTrue(
  74. '<td><label for="jenkins_name">Name of project in Jenkins</label></td>'
  75. in output.data)
  76. self.assertTrue(
  77. '<td><label for="jenkins_url">Jenkins URL</label></td>'
  78. in output.data)
  79. self.assertTrue(
  80. '<td><label for="jenkins_token">Jenkins token</label></td>'
  81. in output.data)
  82. self.assertTrue(
  83. '<input id="active" name="active" type="checkbox" value="y">'
  84. in output.data)
  85. # Activate hook
  86. data = {
  87. 'csrf_token': csrf_token,
  88. 'active': 'y',
  89. 'pagure_name': 'test',
  90. 'jenkins_name': 'jenkins_test',
  91. 'jenkins_url': 'https://jenkins.fedoraproject.org',
  92. 'jenkins_token': 'BEEFCAFE'
  93. }
  94. # No git found
  95. output = self.app.post(
  96. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  97. self.assertEqual(output.status_code, 404)
  98. tests.create_projects_git(tests.HERE)
  99. data = {'csrf_token': csrf_token}
  100. # With the git repo
  101. output = self.app.post(
  102. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  103. self.assertEqual(output.status_code, 200)
  104. self.assertIn(
  105. '<div class="projectinfo m-t-1 m-b-1">\n'
  106. 'test project #1 </div>', output.data)
  107. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  108. self.assertFalse(
  109. '</button>\n Hook activated' in output.data)
  110. self.assertTrue(
  111. '<td><input id="pagure_name" name="pagure_name" type="text" value=""></td>'
  112. '\n<td class="errors">This field is required.</td>'
  113. in output.data)
  114. self.assertTrue(
  115. '<input id="active" name="active" type="checkbox" value="y">' in output.data)
  116. output = self.app.get('/test/settings/Pagure CI')
  117. self.assertEqual(output.status_code, 200)
  118. self.assertIn(
  119. '<div class="projectinfo m-t-1 m-b-1">\n'
  120. 'test project #1 </div>', output.data)
  121. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  122. self.assertTrue(
  123. '<td><label for="pagure_name">Name of project in Pagure</label></td>'
  124. in output.data)
  125. self.assertTrue(
  126. '<td><label for="jenkins_name">Name of project in Jenkins</label></td>'
  127. in output.data)
  128. self.assertTrue(
  129. '<td><label for="jenkins_url">Jenkins URL</label></td>'
  130. in output.data)
  131. self.assertTrue(
  132. '<td><label for="jenkins_token">Jenkins token</label></td>'
  133. in output.data)
  134. self.assertTrue(
  135. '<input checked id="active" name="active" type="checkbox" value="y">'
  136. in output.data)
  137. # Missing the required
  138. data = {'csrf_token': csrf_token, 'active': 'y'}
  139. output = self.app.post(
  140. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  141. self.assertEqual(output.status_code, 200)
  142. self.assertIn(
  143. '<div class="projectinfo m-t-1 m-b-1">\n'
  144. 'test project #1 </div>', output.data)
  145. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  146. self.assertFalse(
  147. '</button>\n Hook activated' in output.data)
  148. self.assertTrue(
  149. '<td><input id="pagure_name" name="pagure_name" type="text" value=""></td>'
  150. '\n<td class="errors">This field is required.</td>'
  151. in output.data)
  152. self.assertTrue(
  153. '<input checked id="active" name="active" type="checkbox" '
  154. 'value="y">' in output.data)
  155. # Activate hook
  156. data = {
  157. 'csrf_token': csrf_token,
  158. 'active': 'y',
  159. 'pagure_name': 'test',
  160. 'jenkins_name': 'jenkins_test',
  161. 'jenkins_url': 'https://jenkins.fedoraproject.org',
  162. 'jenkins_token': 'BEEFCAFE'
  163. }
  164. output = self.app.post(
  165. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  166. self.assertEqual(output.status_code, 200)
  167. self.assertIn(
  168. '<section class="settings">\n <h3>Settings for test</h3>',
  169. output.data)
  170. self.assertTrue(
  171. '</button>\n Hook Pagure CI activated' in output.data)
  172. output = self.app.get('/test/settings/Pagure CI')
  173. self.assertIn(
  174. '<div class="projectinfo m-t-1 m-b-1">\n'
  175. 'test project #1 </div>', output.data)
  176. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  177. self.assertTrue(
  178. '<td><label for="pagure_name">Name of project in Pagure</label></td>'
  179. in output.data)
  180. self.assertTrue(
  181. '<td><input id="pagure_name" name="pagure_name" type="text" value="test"></td>'
  182. in output.data)
  183. self.assertTrue(
  184. '<td><label for="jenkins_name">Name of project in Jenkins</label></td>'
  185. in output.data)
  186. self.assertTrue(
  187. '<td><input id="jenkins_name" name="jenkins_name" type="text" value="jenkins_test"></td>'
  188. in output.data)
  189. self.assertTrue(
  190. '<input checked id="active" name="active" type="checkbox" value="y">'
  191. in output.data)
  192. # De-Activate hook
  193. data = {
  194. 'csrf_token': csrf_token,
  195. 'pagure_name': 'test',
  196. 'jenkins_name': 'jenkins_test',
  197. 'jenkins_url': 'https://jenkins.fedoraproject.org',
  198. 'jenkins_token': 'BEEFCAFE'
  199. }
  200. output = self.app.post(
  201. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  202. self.assertEqual(output.status_code, 200)
  203. self.assertTrue(
  204. '</button>\n Hook Pagure CI inactived' in output.data)
  205. self.assertIn(
  206. '<section class="settings">\n <h3>Settings for test</h3>',
  207. output.data)
  208. if __name__ == '__main__':
  209. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  210. PagureFlaskPluginPagureCItests)
  211. unittest.TextTestRunner(verbosity=2).run(SUITE)