test_pagure_flask_ui_plugins_pagure_request_hook.py 5.5 KB

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