test_pagure_flask_ui_plugins_pagure_ticket_hook.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 PagureFlaskPluginPagureTicketHooktests(tests.SimplePagureTest):
  17. """ Tests for pagure_hook plugin of pagure """
  18. def test_plugin_pagure_ticket(self):
  19. """ Test the pagure_ticket 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 tickets')
  25. self.assertEqual(output.status_code, 200)
  26. output_text = output.get_data(as_text=True)
  27. self.assertIn(
  28. '<title>Settings Pagure tickets - 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_text.split(
  34. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  35. data = {}
  36. output = self.app.post('/test/settings/Pagure tickets', 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 tickets - 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 tickets repo
  47. tests.create_projects_git(os.path.join(self.path, 'repos', 'tickets'))
  48. output = self.app.post(
  49. '/test/settings/Pagure tickets', 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 tickets deactivated',
  58. output_text)
  59. output = self.app.get('/test/settings/Pagure tickets')
  60. self.assertEqual(output.status_code, 200)
  61. output_text = output.get_data(as_text=True)
  62. self.assertIn(
  63. '<title>Settings Pagure tickets - 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, 'tickets', 'test.git', 'hooks',
  70. 'post-receive.pagure')))
  71. # Activate hook
  72. data = {
  73. 'csrf_token': csrf_token,
  74. 'active': 'y',
  75. }
  76. print(' ==== ACTIVATE ')
  77. output = self.app.post(
  78. '/test/settings/Pagure tickets', 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 tickets activated',
  87. output_text)
  88. print(' ==== CHECK ')
  89. output = self.app.get('/test/settings/Pagure tickets')
  90. self.assertEqual(output.status_code, 200)
  91. output_text = output.get_data(as_text=True)
  92. self.assertIn(
  93. '<title>Settings Pagure tickets - test - Pagure</title>',
  94. output_text)
  95. self.assertIn(
  96. '<input checked class="form-check-input mt-2" id="active" name="active" '
  97. 'type="checkbox" value="y">', output_text)
  98. # De-Activate hook
  99. print(' ==== DEACTIVATE ')
  100. data = {'csrf_token': csrf_token}
  101. output = self.app.post(
  102. '/test/settings/Pagure tickets', data=data,
  103. follow_redirects=True)
  104. self.assertEqual(output.status_code, 200)
  105. output_text = output.get_data(as_text=True)
  106. self.assertIn(
  107. '<h5 class="pl-2 font-weight-bold text-muted">'
  108. 'Project Settings</h5>\n', output_text)
  109. self.assertIn(
  110. 'Hook Pagure tickets deactivated',
  111. output_text)
  112. print(' ==== CHECK ')
  113. output = self.app.get('/test/settings/Pagure tickets')
  114. self.assertEqual(output.status_code, 200)
  115. output_text = output.get_data(as_text=True)
  116. self.assertIn(
  117. '<title>Settings Pagure tickets - test - Pagure</title>',
  118. output_text)
  119. self.assertIn(
  120. '<input class="form-check-input mt-2" id="active" name="active" '
  121. 'type="checkbox" value="y">', output_text)
  122. self.assertFalse(os.path.exists(os.path.join(
  123. self.path, 'tickets', 'test.git', 'hooks',
  124. 'post-receive.pagure-ticket')))
  125. # Try re-activate hook w/o the git repo
  126. data = {
  127. 'csrf_token': csrf_token,
  128. 'active': 'y',
  129. }
  130. shutil.rmtree(os.path.join(self.path, 'repos', 'tickets', 'test.git'))
  131. print(' ==== NO GIT REPO ')
  132. output = self.app.post('/test/settings/Pagure tickets', data=data)
  133. self.assertEqual(output.status_code, 404)
  134. print('*** DONE ***')
  135. if __name__ == '__main__':
  136. unittest.main(verbosity=2)