test_pagure_flask_ui_plugins_pagure_request_hook.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-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 shutil
  11. import sys
  12. import os
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import tests
  16. class PagureFlaskPluginPagureRequestHooktests(tests.SimplePagureTest):
  17. """ Tests for pagure_hook plugin of pagure """
  18. def test_plugin_pagure_request(self):
  19. """ Test the pagure_request 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('/test/settings/Pagure requests')
  25. self.assertEqual(output.status_code, 200)
  26. output_text = output.get_data(as_text=True)
  27. self.assertIn(
  28. '<title>Settings Pagure requests - test - Pagure</title>',
  29. output_text)
  30. self.assertIn(
  31. '<input class="form-check-input mt-2" id="active" name="active" '
  32. 'type="checkbox" value="y">', output_text)
  33. csrf_token = output.get_data(as_text=True).split(
  34. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  35. data = {}
  36. output = self.app.post('/test/settings/Pagure requests', data=data)
  37. self.assertEqual(output.status_code, 200)
  38. output_text = output.get_data(as_text=True)
  39. self.assertIn(
  40. '<title>Settings Pagure requests - test - Pagure</title>',
  41. output_text)
  42. self.assertIn(
  43. '<input class="form-check-input mt-2" id="active" name="active" '
  44. 'type="checkbox" value="y">', output_text)
  45. data['csrf_token'] = csrf_token
  46. # Create the requests repo
  47. tests.create_projects_git(os.path.join(self.path, 'repos', 'requests'))
  48. output = self.app.post(
  49. '/test/settings/Pagure requests', data=data,
  50. 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.assertIn(
  57. 'Hook Pagure requests deactivated',
  58. output_text)
  59. output = self.app.get('/test/settings/Pagure requests')
  60. self.assertEqual(output.status_code, 200)
  61. output_text = output.get_data(as_text=True)
  62. self.assertIn(
  63. '<title>Settings Pagure requests - test - Pagure</title>',
  64. output_text)
  65. self.assertIn(
  66. '<input class="form-check-input mt-2" id="active" name="active" '
  67. 'type="checkbox" value="y">', output_text)
  68. self.assertFalse(os.path.exists(os.path.join(
  69. self.path, 'requests', 'test.git', 'hooks',
  70. 'post-receive.pagure')))
  71. # Activate hook
  72. data = {
  73. 'csrf_token': csrf_token,
  74. 'active': 'y',
  75. }
  76. output = self.app.post(
  77. '/test/settings/Pagure requests', data=data,
  78. follow_redirects=True)
  79. self.assertEqual(output.status_code, 200)
  80. output_text = output.get_data(as_text=True)
  81. self.assertIn(
  82. '<h5 class="pl-2 font-weight-bold text-muted">'
  83. 'Project Settings</h5>\n', output_text)
  84. self.assertIn(
  85. 'Hook Pagure requests activated',
  86. output_text)
  87. output = self.app.get('/test/settings/Pagure requests')
  88. self.assertEqual(output.status_code, 200)
  89. output_text = output.get_data(as_text=True)
  90. self.assertIn(
  91. '<title>Settings Pagure requests - test - Pagure</title>',
  92. output_text)
  93. self.assertIn(
  94. '<input checked class="form-check-input mt-2" id="active" name="active" '
  95. 'type="checkbox" value="y">', output_text)
  96. # De-Activate hook
  97. data = {'csrf_token': csrf_token}
  98. output = self.app.post(
  99. '/test/settings/Pagure requests', data=data,
  100. follow_redirects=True)
  101. self.assertEqual(output.status_code, 200)
  102. output_text = output.get_data(as_text=True)
  103. self.assertIn(
  104. '<h5 class="pl-2 font-weight-bold text-muted">'
  105. 'Project Settings</h5>\n', output_text)
  106. self.assertIn(
  107. 'Hook Pagure requests deactivated',
  108. output_text)
  109. output = self.app.get('/test/settings/Pagure requests')
  110. self.assertEqual(output.status_code, 200)
  111. output_text = output.get_data(as_text=True)
  112. self.assertIn(
  113. '<title>Settings Pagure requests - test - Pagure</title>',
  114. output_text)
  115. self.assertIn(
  116. '<input class="form-check-input mt-2" id="active" name="active" '
  117. 'type="checkbox" value="y">', output_text)
  118. self.assertFalse(os.path.exists(os.path.join(
  119. self.path, 'requests', 'test.git', 'hooks',
  120. 'post-receive.pagure-requests')))
  121. if __name__ == '__main__':
  122. unittest.main(verbosity=2)