test_pagure_flask_ui_plugins_pagure_request_hook.py 5.5 KB

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