test_pagure_flask_ui_plugins_pagure_ci.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.assertIn(
  47. '<td><label for="ci_url">URL to the project on the CI '
  48. 'service</label></td>' , output.data)
  49. self.assertIn(
  50. '<input id="active" name="active" type="checkbox" value="y">',
  51. output.data)
  52. csrf_token = output.data.split(
  53. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  54. data = {}
  55. output = self.app.post('/test/settings/Pagure CI', data=data)
  56. self.assertEqual(output.status_code, 200)
  57. self.assertIn(
  58. '<div class="projectinfo m-t-1 m-b-1">\n'
  59. 'test project #1 </div>', output.data)
  60. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  61. self.assertIn(
  62. '<td><label for="ci_url">URL to the project on the CI '
  63. 'service</label></td>' , output.data)
  64. self.assertIn(
  65. '<input id="active" name="active" type="checkbox" value="y">',
  66. output.data)
  67. # Activate hook
  68. data = {
  69. 'active': 'y',
  70. 'ci_url': 'https://jenkins.fedoraproject.org',
  71. 'ci_type': 'jenkins',
  72. }
  73. # CSRF Token missing
  74. output = self.app.post(
  75. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  76. self.assertEqual(output.status_code, 200)
  77. self.assertIn(
  78. '<div class="projectinfo m-t-1 m-b-1">\n'
  79. 'test project #1 </div>', output.data)
  80. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  81. self.assertIn(
  82. '<td><label for="ci_url">URL to the project on the CI '
  83. 'service</label></td>' , output.data)
  84. self.assertIn(
  85. '<input checked id="active" name="active" type="checkbox" '
  86. 'value="y">', output.data)
  87. data['csrf_token'] = csrf_token
  88. # No git found
  89. output = self.app.post('/test/settings/Pagure', data=data)
  90. self.assertEqual(output.status_code, 404)
  91. tests.create_projects_git(tests.HERE)
  92. if not pagure.APP.config.get('PAGURE_CI_SERVICES'):
  93. return
  94. # Activate hook
  95. output = self.app.post(
  96. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  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.assertIn(
  102. '<title>Settings - test - Pagure</title>', output.data)
  103. self.assertIn('<h3>Settings for test</h3>', output.data)
  104. self.assertIn(
  105. '</button>\n Hook Pagure CI activated',
  106. output.data)
  107. output = self.app.get('/test/settings/Pagure CI')
  108. self.assertEqual(output.status_code, 200)
  109. self.assertIn(
  110. '<div class="projectinfo m-t-1 m-b-1">\n'
  111. 'test project #1 </div>', output.data)
  112. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  113. self.assertIn(
  114. '<td><label for="ci_url">URL to the project on the CI '
  115. 'service</label></td>' , output.data)
  116. self.assertTrue(
  117. '<input checked id="active" name="active" type="checkbox" value="y">'
  118. in output.data)
  119. # De-activate the hook
  120. data = {
  121. 'csrf_token': csrf_token,
  122. }
  123. output = self.app.post(
  124. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  125. self.assertEqual(output.status_code, 200)
  126. self.assertIn(
  127. '</button>\n Hook Pagure CI inactived',
  128. output.data)
  129. self.assertIn(
  130. '<section class="settings">\n <h3>Settings for test</h3>',
  131. output.data)
  132. output = self.app.get('/test/settings/Pagure CI')
  133. self.assertEqual(output.status_code, 200)
  134. self.assertIn(
  135. '<div class="projectinfo m-t-1 m-b-1">\n'
  136. 'test project #1 </div>', output.data)
  137. self.assertTrue('<h3>Pagure CI settings</h3>' in output.data)
  138. self.assertIn(
  139. '<td><label for="ci_url">URL to the project on the CI '
  140. 'service</label></td>' , output.data)
  141. self.assertIn(
  142. '<input id="active" name="active" type="checkbox" '
  143. 'value="y">', output.data)
  144. # Missing the required ci_url
  145. data = {'csrf_token': csrf_token, 'active': 'y'}
  146. output = self.app.post(
  147. '/test/settings/Pagure CI', data=data, follow_redirects=True)
  148. self.assertEqual(output.status_code, 200)
  149. self.assertIn(
  150. '<div class="projectinfo m-t-1 m-b-1">\n'
  151. 'test project #1 </div>', output.data)
  152. self.assertIn('<h3>Pagure CI settings</h3>', output.data)
  153. self.assertFalse(
  154. '</button>\n Hook activated' in output.data)
  155. self.assertIn(
  156. '<td><input id="ci_url" name="ci_url" type="text" value="">'
  157. '</td>\n<td class="errors">This field is required.</td>',
  158. output.data)
  159. self.assertIn(
  160. '<input checked id="active" name="active" type="checkbox" '
  161. 'value="y">', output.data)
  162. if __name__ == '__main__':
  163. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  164. PagureFlaskPluginPagureCItests)
  165. unittest.TextTestRunner(verbosity=2).run(SUITE)