test_pagure_flask_ui_plugins_rtd_hook.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, 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 PagureFlaskPluginRtdHooktests(tests.SimplePagureTest):
  16. """ Tests for rtd_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/Read the Doc')
  24. self.assertEqual(output.status_code, 200)
  25. output_text = output.get_data(as_text=True)
  26. self.assertIn(
  27. '<title>Settings Read the Doc - 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_text.split(
  33. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  34. data = {}
  35. output = self.app.post('/test/settings/Read the Doc', data=data)
  36. self.assertEqual(output.status_code, 200)
  37. output_text = output.get_data(as_text=True)
  38. self.assertIn(
  39. '<title>Settings Read the Doc - 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, 'requests'))
  47. output = self.app.post(
  48. '/test/settings/Read the Doc', 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 Read the Doc deactivated',
  57. output_text)
  58. output = self.app.get('/test/settings/Read the Doc')
  59. self.assertEqual(output.status_code, 200)
  60. output_text = output.get_data(as_text=True)
  61. self.assertIn(
  62. '<title>Settings Read the Doc - 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. 'project_name': 'foo',
  75. }
  76. output = self.app.post(
  77. '/test/settings/Read the Doc', 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 Read the Doc activated',
  86. output_text)
  87. output = self.app.get('/test/settings/Read the Doc')
  88. self.assertEqual(output.status_code, 200)
  89. output_text = output.get_data(as_text=True)
  90. self.assertIn(
  91. '<title>Settings Read the Doc - 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/Read the Doc', 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 Read the Doc deactivated',
  108. output_text)
  109. output = self.app.get('/test/settings/Read the Doc')
  110. self.assertEqual(output.status_code, 200)
  111. output_text = output.get_data(as_text=True)
  112. self.assertIn(
  113. '<title>Settings Read the Doc - 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, 'repos', 'test.git', 'hooks',
  120. 'post-receive.rtd')))
  121. # Try re-activate hook w/o the git repo
  122. data = {
  123. 'csrf_token': csrf_token,
  124. 'active': 'y',
  125. 'project_name': 'foo',
  126. }
  127. shutil.rmtree(os.path.join(self.path, 'repos', 'test.git'))
  128. output = self.app.post('/test/settings/Read the Doc', data=data)
  129. self.assertEqual(output.status_code, 404)
  130. if __name__ == '__main__':
  131. unittest.main(verbosity=2)