test_pagure_flask_ui_plugins_unsigned.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import unittest
  10. import sys
  11. import os
  12. sys.path.insert(0, os.path.join(os.path.dirname(
  13. os.path.abspath(__file__)), '..'))
  14. import pagure.lib
  15. import tests
  16. class PagureFlaskPluginUnsignedtests(tests.SimplePagureTest):
  17. """ Tests for Block pushes with unsigned commit plugin of pagure """
  18. def test_plugin_unsigned(self):
  19. """ Test the noff plugin on/off endpoint. """
  20. tests.create_projects(self.session)
  21. tests.create_projects_git(os.path.join(self.path, 'repos'))
  22. user = tests.FakeUser(username='pingou')
  23. with tests.user_set(self.app.application, user):
  24. output = self.app.get(
  25. '/test/settings/Block Un-Signed commits')
  26. self.assertEqual(output.status_code, 200)
  27. output_text = output.get_data(as_text=True)
  28. self.assertIn(
  29. '<title>Settings Block Un-Signed commits - test - '
  30. 'Pagure</title>', output_text)
  31. self.assertTrue(
  32. '<input class="form-check-input mt-2" id="active" name="active" '
  33. 'type="checkbox" value="y">' in output_text)
  34. csrf_token = output_text.split(
  35. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  36. data = {}
  37. output = self.app.post(
  38. '/test/settings/Block Un-Signed commits', data=data)
  39. self.assertEqual(output.status_code, 200)
  40. output_text = output.get_data(as_text=True)
  41. self.assertIn(
  42. '<title>Settings Block Un-Signed commits - test - '
  43. 'Pagure</title>', output_text)
  44. self.assertTrue(
  45. '<input class="form-check-input mt-2" id="active" name="active" '
  46. 'type="checkbox" value="y">' in output_text)
  47. data['csrf_token'] = csrf_token
  48. # With the git repo
  49. output = self.app.post(
  50. '/test/settings/Block Un-Signed commits',
  51. data=data, follow_redirects=True)
  52. self.assertEqual(output.status_code, 200)
  53. output_text = output.get_data(as_text=True)
  54. self.assertIn(
  55. '<h5 class="pl-2 font-weight-bold text-muted">'
  56. 'Project Settings</h5>\n', output_text)
  57. self.assertTrue(
  58. 'Hook Block Un-Signed '
  59. 'commits deactivated' in output_text)
  60. output = self.app.get(
  61. '/test/settings/Block Un-Signed commits')
  62. self.assertEqual(output.status_code, 200)
  63. output_text = output.get_data(as_text=True)
  64. self.assertIn(
  65. '<title>Settings Block Un-Signed commits - test - '
  66. 'Pagure</title>', output_text)
  67. self.assertTrue(
  68. '<input class="form-check-input mt-2" id="active" name="active" '
  69. 'type="checkbox" value="y">' in output_text)
  70. self.assertFalse(os.path.exists(os.path.join(
  71. self.path, 'repos', 'test.git', 'hooks',
  72. 'pre-receive.pagureunsignedcommit')))
  73. # Activate the hook
  74. data = {'csrf_token': csrf_token, 'active': 'y'}
  75. output = self.app.post(
  76. '/test/settings/Block Un-Signed commits',
  77. data=data, follow_redirects=True)
  78. self.assertEqual(output.status_code, 200)
  79. output_text = output.get_data(as_text=True)
  80. self.assertIn(
  81. '<h5 class="pl-2 font-weight-bold text-muted">'
  82. 'Project Settings</h5>\n', output_text)
  83. self.assertNotIn(
  84. 'Hook activated',
  85. output_text)
  86. # De-Activate hook
  87. data = {'csrf_token': csrf_token}
  88. output = self.app.post(
  89. '/test/settings/Block Un-Signed commits',
  90. data=data, follow_redirects=True)
  91. self.assertEqual(output.status_code, 200)
  92. output_text = output.get_data(as_text=True)
  93. self.assertIn(
  94. '<h5 class="pl-2 font-weight-bold text-muted">'
  95. 'Project Settings</h5>\n', output_text)
  96. self.assertTrue(
  97. 'Hook Block Un-Signed '
  98. 'commits deactivated' in output_text)
  99. output = self.app.get(
  100. '/test/settings/Block Un-Signed commits')
  101. output_text = output.get_data(as_text=True)
  102. self.assertIn(
  103. '<title>Settings Block Un-Signed commits - test - '
  104. 'Pagure</title>', output_text)
  105. self.assertIn(
  106. '<input class="form-check-input mt-2" id="active" name="active" '
  107. 'type="checkbox" value="y">', output_text)
  108. self.assertFalse(os.path.exists(os.path.join(
  109. self.path, 'repos', 'test.git', 'hooks',
  110. 'pre-receive.pagureunsignedcommit')))
  111. if __name__ == '__main__':
  112. unittest.main(verbosity=2)