test_pagure_flask_ui_plugins_unsigned.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 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. output_text = output.get_data(as_text=True)
  27. self.assertIn(
  28. '<title>Settings Block Un-Signed commits - test - '
  29. 'Pagure</title>', output_text)
  30. self.assertTrue(
  31. '<input class="form-check-input mt-2" id="active" name="active" '
  32. 'type="checkbox" value="y">' in output_text)
  33. csrf_token = output_text.split(
  34. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  35. data = {}
  36. output = self.app.post(
  37. '/test/settings/Block Un-Signed commits', data=data)
  38. self.assertEqual(output.status_code, 200)
  39. output_text = output.get_data(as_text=True)
  40. self.assertIn(
  41. '<title>Settings Block Un-Signed commits - test - '
  42. 'Pagure</title>', output_text)
  43. self.assertTrue(
  44. '<input class="form-check-input mt-2" id="active" name="active" '
  45. 'type="checkbox" value="y">' in output_text)
  46. data['csrf_token'] = csrf_token
  47. # With the git repo
  48. output = self.app.post(
  49. '/test/settings/Block Un-Signed commits',
  50. data=data, follow_redirects=True)
  51. self.assertEqual(output.status_code, 200)
  52. output_text = output.get_data(as_text=True)
  53. self.assertIn(
  54. '<h5 class="pl-2 font-weight-bold text-muted">'
  55. 'Project Settings</h5>\n', output_text)
  56. self.assertTrue(
  57. 'Hook Block Un-Signed '
  58. 'commits deactivated' in output_text)
  59. output = self.app.get(
  60. '/test/settings/Block Un-Signed commits')
  61. self.assertEqual(output.status_code, 200)
  62. output_text = output.get_data(as_text=True)
  63. self.assertIn(
  64. '<title>Settings Block Un-Signed commits - test - '
  65. 'Pagure</title>', output_text)
  66. self.assertTrue(
  67. '<input class="form-check-input mt-2" id="active" name="active" '
  68. 'type="checkbox" value="y">' in output_text)
  69. self.assertFalse(os.path.exists(os.path.join(
  70. self.path, 'repos', 'test.git', 'hooks',
  71. 'pre-receive.pagureunsignedcommit')))
  72. # Activate the hook
  73. data = {'csrf_token': csrf_token, 'active': 'y'}
  74. output = self.app.post(
  75. '/test/settings/Block Un-Signed commits',
  76. data=data, follow_redirects=True)
  77. self.assertEqual(output.status_code, 200)
  78. output_text = output.get_data(as_text=True)
  79. self.assertIn(
  80. '<h5 class="pl-2 font-weight-bold text-muted">'
  81. 'Project Settings</h5>\n', output_text)
  82. self.assertNotIn(
  83. 'Hook activated',
  84. output_text)
  85. # De-Activate hook
  86. data = {'csrf_token': csrf_token}
  87. output = self.app.post(
  88. '/test/settings/Block Un-Signed commits',
  89. data=data, follow_redirects=True)
  90. self.assertEqual(output.status_code, 200)
  91. output_text = output.get_data(as_text=True)
  92. self.assertIn(
  93. '<h5 class="pl-2 font-weight-bold text-muted">'
  94. 'Project Settings</h5>\n', output_text)
  95. self.assertTrue(
  96. 'Hook Block Un-Signed '
  97. 'commits deactivated' in output_text)
  98. output = self.app.get(
  99. '/test/settings/Block Un-Signed commits')
  100. output_text = output.get_data(as_text=True)
  101. self.assertIn(
  102. '<title>Settings Block Un-Signed commits - test - '
  103. 'Pagure</title>', output_text)
  104. self.assertIn(
  105. '<input class="form-check-input mt-2" id="active" name="active" '
  106. 'type="checkbox" value="y">', output_text)
  107. self.assertFalse(os.path.exists(os.path.join(
  108. self.path, 'repos', 'test.git', 'hooks',
  109. 'pre-receive.pagureunsignedcommit')))
  110. if __name__ == '__main__':
  111. unittest.main(verbosity=2)