test_pagure_flask_ui_plugins_unsigned.py 6.5 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'] = tests.HERE
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. tests.HERE, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. tests.HERE, 'tickets')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. tests.HERE, '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. user = tests.FakeUser(username='pingou')
  44. with tests.user_set(pagure.APP, user):
  45. output = self.app.get(
  46. '/test/settings/Block Un-Signed commits')
  47. self.assertEqual(output.status_code, 200)
  48. self.assertIn(
  49. '<div class="projectinfo m-t-1 m-b-1">\n'
  50. 'test project #1 </div>', output.data)
  51. self.assertIn(
  52. '<h3>Block Un-Signed commits settings</h3>',
  53. output.data)
  54. self.assertTrue(
  55. '<input id="active" name="active" type="checkbox" value="y">'
  56. in output.data)
  57. csrf_token = output.data.split(
  58. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  59. data = {}
  60. output = self.app.post(
  61. '/test/settings/Block Un-Signed commits', data=data)
  62. self.assertEqual(output.status_code, 200)
  63. self.assertIn(
  64. '<div class="projectinfo m-t-1 m-b-1">\n'
  65. 'test project #1 </div>', output.data)
  66. self.assertIn(
  67. '<h3>Block Un-Signed commits settings</h3>',
  68. output.data)
  69. self.assertTrue(
  70. '<input id="active" name="active" type="checkbox" value="y">'
  71. in output.data)
  72. data['csrf_token'] = csrf_token
  73. # No git found
  74. output = self.app.post(
  75. '/test/settings/Block Un-Signed commits', data=data)
  76. self.assertEqual(output.status_code, 404)
  77. tests.create_projects_git(tests.HERE)
  78. # With the git repo
  79. output = self.app.post(
  80. '/test/settings/Block Un-Signed commits',
  81. data=data, follow_redirects=True)
  82. self.assertEqual(output.status_code, 200)
  83. self.assertIn(
  84. '<section class="settings">\n <h3>Settings for test</h3>',
  85. output.data)
  86. self.assertTrue(
  87. '</button>\n Hook Block Un-Signed '
  88. 'commits inactived' in output.data)
  89. output = self.app.get(
  90. '/test/settings/Block Un-Signed commits')
  91. self.assertEqual(output.status_code, 200)
  92. self.assertIn(
  93. '<div class="projectinfo m-t-1 m-b-1">\n'
  94. 'test project #1 </div>', output.data)
  95. self.assertIn(
  96. '<h3>Block Un-Signed commits settings</h3>',
  97. output.data)
  98. self.assertTrue(
  99. '<input id="active" name="active" type="checkbox" value="y">'
  100. in output.data)
  101. self.assertFalse(os.path.exists(os.path.join(
  102. tests.HERE, 'test.git', 'hooks',
  103. 'pre-receive.pagureunsignedcommit')))
  104. # Activate the hook
  105. data = {'csrf_token': csrf_token, 'active': 'y'}
  106. output = self.app.post(
  107. '/test/settings/Block Un-Signed commits',
  108. data=data, follow_redirects=True)
  109. self.assertEqual(output.status_code, 200)
  110. self.assertIn(
  111. '<div class="projectinfo m-t-1 m-b-1">\n'
  112. 'test project #1 </div>', output.data)
  113. self.assertIn(
  114. '<section class="settings">\n <h3>Settings for test</h3>',
  115. output.data)
  116. self.assertNotIn(
  117. '</button>\n Hook activated',
  118. output.data)
  119. self.assertTrue(os.path.exists(os.path.join(
  120. tests.HERE, 'test.git', 'hooks',
  121. 'pre-receive.pagureunsignedcommit')))
  122. # De-Activate hook
  123. data = {'csrf_token': csrf_token}
  124. output = self.app.post(
  125. '/test/settings/Block Un-Signed commits',
  126. data=data, follow_redirects=True)
  127. self.assertEqual(output.status_code, 200)
  128. self.assertIn(
  129. '<section class="settings">\n <h3>Settings for test</h3>',
  130. output.data)
  131. self.assertTrue(
  132. '</button>\n Hook Block Un-Signed '
  133. 'commits inactived' in output.data)
  134. output = self.app.get(
  135. '/test/settings/Block Un-Signed commits')
  136. self.assertIn(
  137. '<div class="projectinfo m-t-1 m-b-1">\n'
  138. 'test project #1 </div>', output.data)
  139. self.assertIn(
  140. '<h3>Block Un-Signed commits settings</h3>',
  141. output.data)
  142. self.assertIn(
  143. '<input id="active" name="active" type="checkbox" '
  144. 'value="y">', output.data)
  145. self.assertFalse(os.path.exists(os.path.join(
  146. tests.HERE, '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)