test_pagure_flask_ui_plugins_unsigned.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 unittest
  9. import sys
  10. import os
  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 PagureFlaskPluginUnsignedtests(tests.SimplePagureTest):
  16. """ Tests for Block pushes with unsigned commit plugin of pagure """
  17. def test_plugin_unsigned(self):
  18. """ Test the noff plugin on/off endpoint. """
  19. tests.create_projects(self.session)
  20. tests.create_projects_git(os.path.join(self.path, 'repos'))
  21. user = tests.FakeUser(username='pingou')
  22. with tests.user_set(self.app.application, user):
  23. output = self.app.get(
  24. '/test/settings/Block Un-Signed commits')
  25. self.assertEqual(output.status_code, 200)
  26. self.assertIn(
  27. '<div class="projectinfo m-t-1 m-b-1">\n'
  28. 'test project #1 </div>', output.data)
  29. self.assertIn(
  30. '<h3>Block Un-Signed commits settings</h3>',
  31. output.data)
  32. self.assertTrue(
  33. '<input class="form-control" id="active" name="active" '
  34. 'type="checkbox" value="y">' in output.data)
  35. csrf_token = output.data.split(
  36. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  37. data = {}
  38. output = self.app.post(
  39. '/test/settings/Block Un-Signed commits', data=data)
  40. self.assertEqual(output.status_code, 200)
  41. self.assertIn(
  42. '<div class="projectinfo m-t-1 m-b-1">\n'
  43. 'test project #1 </div>', output.data)
  44. self.assertIn(
  45. '<h3>Block Un-Signed commits settings</h3>',
  46. output.data)
  47. self.assertTrue(
  48. '<input class="form-control" id="active" name="active" '
  49. 'type="checkbox" value="y">' in output.data)
  50. data['csrf_token'] = csrf_token
  51. # With the git repo
  52. output = self.app.post(
  53. '/test/settings/Block Un-Signed commits',
  54. data=data, follow_redirects=True)
  55. self.assertEqual(output.status_code, 200)
  56. self.assertIn(
  57. '<section class="settings">\n <h3>Settings for test</h3>',
  58. output.data)
  59. self.assertTrue(
  60. '</button>\n Hook Block Un-Signed '
  61. 'commits deactivated' in output.data)
  62. output = self.app.get(
  63. '/test/settings/Block Un-Signed commits')
  64. self.assertEqual(output.status_code, 200)
  65. self.assertIn(
  66. '<div class="projectinfo m-t-1 m-b-1">\n'
  67. 'test project #1 </div>', output.data)
  68. self.assertIn(
  69. '<h3>Block Un-Signed commits settings</h3>',
  70. output.data)
  71. self.assertTrue(
  72. '<input class="form-control" id="active" name="active" '
  73. 'type="checkbox" value="y">' in output.data)
  74. self.assertFalse(os.path.exists(os.path.join(
  75. self.path, 'repos', 'test.git', 'hooks',
  76. 'pre-receive.pagureunsignedcommit')))
  77. # Activate the hook
  78. data = {'csrf_token': csrf_token, 'active': 'y'}
  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. '<div class="projectinfo m-t-1 m-b-1">\n'
  85. 'test project #1 </div>', output.data)
  86. self.assertIn(
  87. '<section class="settings">\n <h3>Settings for test</h3>',
  88. output.data)
  89. self.assertNotIn(
  90. '</button>\n Hook activated',
  91. output.data)
  92. self.assertTrue(os.path.exists(os.path.join(
  93. self.path, 'repos', 'test.git', 'hooks',
  94. 'pre-receive.pagureunsignedcommit')))
  95. # De-Activate hook
  96. data = {'csrf_token': csrf_token}
  97. output = self.app.post(
  98. '/test/settings/Block Un-Signed commits',
  99. data=data, follow_redirects=True)
  100. self.assertEqual(output.status_code, 200)
  101. self.assertIn(
  102. '<section class="settings">\n <h3>Settings for test</h3>',
  103. output.data)
  104. self.assertTrue(
  105. '</button>\n Hook Block Un-Signed '
  106. 'commits deactivated' in output.data)
  107. output = self.app.get(
  108. '/test/settings/Block Un-Signed commits')
  109. self.assertIn(
  110. '<div class="projectinfo m-t-1 m-b-1">\n'
  111. 'test project #1 </div>', output.data)
  112. self.assertIn(
  113. '<h3>Block Un-Signed commits settings</h3>',
  114. output.data)
  115. self.assertIn(
  116. '<input class="form-control" id="active" name="active" '
  117. 'type="checkbox" value="y">', output.data)
  118. self.assertFalse(os.path.exists(os.path.join(
  119. self.path, 'repos', 'test.git', 'hooks',
  120. 'pre-receive.pagureunsignedcommit')))
  121. if __name__ == '__main__':
  122. unittest.main(verbosity=2)