test_pagure_flask_ui_plugins_unsigned.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import pygit2
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskPluginUnsignedtests(tests.Modeltests):
  21. """ Tests for Block pushes with unsigned commit plugin of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskPluginUnsignedtests, self).setUp()
  25. pagure.APP.config['TESTING'] = True
  26. pagure.SESSION = self.session
  27. pagure.ui.SESSION = self.session
  28. pagure.ui.app.SESSION = self.session
  29. pagure.ui.plugins.SESSION = self.session
  30. pagure.ui.repo.SESSION = self.session
  31. pagure.ui.filters.SESSION = self.session
  32. pagure.APP.config['GIT_FOLDER'] = self.path
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. self.path, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. self.path, 'tickets')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. self.path, 'docs')
  39. self.app = pagure.APP.test_client()
  40. def test_plugin_unsigned(self):
  41. """ Test the noff plugin on/off endpoint. """
  42. tests.create_projects(self.session)
  43. tests.create_projects_git(self.path)
  44. user = tests.FakeUser(username='pingou')
  45. with tests.user_set(pagure.APP, user):
  46. output = self.app.get(
  47. '/test/settings/Block Un-Signed commits')
  48. self.assertEqual(output.status_code, 200)
  49. self.assertIn(
  50. '<div class="projectinfo m-t-1 m-b-1">\n'
  51. 'test project #1 </div>', output.get_data(as_text=True))
  52. self.assertIn(
  53. '<h3>Block Un-Signed commits settings</h3>',
  54. output.get_data(as_text=True))
  55. self.assertTrue(
  56. '<input id="active" name="active" type="checkbox" value="y">'
  57. in output.get_data(as_text=True))
  58. csrf_token = output.get_data(as_text=True).split(
  59. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  60. data = {}
  61. output = self.app.post(
  62. '/test/settings/Block Un-Signed commits', data=data)
  63. self.assertEqual(output.status_code, 200)
  64. self.assertIn(
  65. '<div class="projectinfo m-t-1 m-b-1">\n'
  66. 'test project #1 </div>',
  67. output.get_data(as_text=True))
  68. self.assertIn(
  69. '<h3>Block Un-Signed commits settings</h3>',
  70. output.get_data(as_text=True))
  71. self.assertTrue(
  72. '<input id="active" name="active" type="checkbox" value="y">'
  73. in output.get_data(as_text=True))
  74. data['csrf_token'] = csrf_token
  75. # With the git repo
  76. output = self.app.post(
  77. '/test/settings/Block Un-Signed commits',
  78. data=data, follow_redirects=True)
  79. self.assertEqual(output.status_code, 200)
  80. self.assertIn(
  81. '<section class="settings">\n <h3>Settings for test</h3>',
  82. output.get_data(as_text=True))
  83. self.assertTrue(
  84. '</button>\n Hook Block Un-Signed '
  85. 'commits inactived' in output.get_data(as_text=True))
  86. output = self.app.get(
  87. '/test/settings/Block Un-Signed commits')
  88. self.assertEqual(output.status_code, 200)
  89. self.assertIn(
  90. '<div class="projectinfo m-t-1 m-b-1">\n'
  91. 'test project #1 </div>',
  92. output.get_data(as_text=True))
  93. self.assertIn(
  94. '<h3>Block Un-Signed commits settings</h3>',
  95. output.get_data(as_text=True))
  96. self.assertTrue(
  97. '<input id="active" name="active" type="checkbox" value="y">'
  98. in output.get_data(as_text=True))
  99. self.assertFalse(os.path.exists(os.path.join(
  100. self.path, 'test.git', 'hooks',
  101. 'pre-receive.pagureunsignedcommit')))
  102. # Activate the hook
  103. data = {'csrf_token': csrf_token, 'active': 'y'}
  104. output = self.app.post(
  105. '/test/settings/Block Un-Signed commits',
  106. data=data, follow_redirects=True)
  107. self.assertEqual(output.status_code, 200)
  108. self.assertIn(
  109. '<div class="projectinfo m-t-1 m-b-1">\n'
  110. 'test project #1 </div>',
  111. output.get_data(as_text=True))
  112. self.assertIn(
  113. '<section class="settings">\n <h3>Settings for test</h3>',
  114. output.get_data(as_text=True))
  115. self.assertNotIn(
  116. '</button>\n Hook activated',
  117. output.get_data(as_text=True))
  118. self.assertTrue(os.path.exists(os.path.join(
  119. self.path, 'test.git', 'hooks',
  120. 'pre-receive.pagureunsignedcommit')))
  121. # De-Activate hook
  122. data = {'csrf_token': csrf_token}
  123. output = self.app.post(
  124. '/test/settings/Block Un-Signed commits',
  125. data=data, follow_redirects=True)
  126. self.assertEqual(output.status_code, 200)
  127. self.assertIn(
  128. '<section class="settings">\n <h3>Settings for test</h3>',
  129. output.get_data(as_text=True))
  130. self.assertTrue(
  131. '</button>\n Hook Block Un-Signed '
  132. 'commits inactived' in output.get_data(as_text=True))
  133. output = self.app.get(
  134. '/test/settings/Block Un-Signed commits')
  135. self.assertIn(
  136. '<div class="projectinfo m-t-1 m-b-1">\n'
  137. 'test project #1 </div>',
  138. output.get_data(as_text=True))
  139. self.assertIn(
  140. '<h3>Block Un-Signed commits settings</h3>',
  141. output.get_data(as_text=True))
  142. self.assertIn(
  143. '<input id="active" name="active" type="checkbox" '
  144. 'value="y">', output.get_data(as_text=True))
  145. self.assertFalse(os.path.exists(os.path.join(
  146. self.path, 'test.git', 'hooks',
  147. 'pre-receive.pagureunsignedcommit')))
  148. if __name__ == '__main__':
  149. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  150. PagureFlaskPluginUnsignedtests)
  151. unittest.TextTestRunner(verbosity=2).run(SUITE)