test_pagure_flask_ui_plugins_rtd_hook.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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
  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 PagureFlaskPluginRtdHooktests(tests.SimplePagureTest):
  18. """ Tests for rtd_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/Read the Doc')
  26. self.assertEqual(output.status_code, 200)
  27. output_text = output.get_data(as_text=True)
  28. self.assertIn(
  29. '<title>Settings Read the Doc - 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_text.split(
  35. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  36. data = {}
  37. output = self.app.post('/test/settings/Read the Doc', data=data)
  38. self.assertEqual(output.status_code, 200)
  39. output_text = output.get_data(as_text=True)
  40. self.assertIn(
  41. '<title>Settings Read the Doc - 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, 'requests'))
  49. output = self.app.post(
  50. '/test/settings/Read the Doc', 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 Read the Doc deactivated',
  59. output_text)
  60. output = self.app.get('/test/settings/Read the Doc')
  61. self.assertEqual(output.status_code, 200)
  62. output_text = output.get_data(as_text=True)
  63. self.assertIn(
  64. '<title>Settings Read the Doc - 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. 'project_name': 'foo',
  77. }
  78. output = self.app.post(
  79. '/test/settings/Read the Doc', data=data,
  80. follow_redirects=True)
  81. self.assertEqual(output.status_code, 200)
  82. output_text = output.get_data(as_text=True)
  83. self.assertIn(
  84. '<h5 class="pl-2 font-weight-bold text-muted">'
  85. 'Project Settings</h5>\n', output_text)
  86. self.assertIn(
  87. 'Hook Read the Doc activated',
  88. output_text)
  89. output = self.app.get('/test/settings/Read the Doc')
  90. self.assertEqual(output.status_code, 200)
  91. output_text = output.get_data(as_text=True)
  92. self.assertIn(
  93. '<title>Settings Read the Doc - 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. data = {'csrf_token': csrf_token}
  100. output = self.app.post(
  101. '/test/settings/Read the Doc', data=data,
  102. follow_redirects=True)
  103. self.assertEqual(output.status_code, 200)
  104. output_text = output.get_data(as_text=True)
  105. self.assertIn(
  106. '<h5 class="pl-2 font-weight-bold text-muted">'
  107. 'Project Settings</h5>\n', output_text)
  108. self.assertIn(
  109. 'Hook Read the Doc deactivated',
  110. output_text)
  111. output = self.app.get('/test/settings/Read the Doc')
  112. self.assertEqual(output.status_code, 200)
  113. output_text = output.get_data(as_text=True)
  114. self.assertIn(
  115. '<title>Settings Read the Doc - test - Pagure</title>',
  116. output_text)
  117. self.assertIn(
  118. '<input class="form-check-input mt-2" id="active" name="active" '
  119. 'type="checkbox" value="y">', output_text)
  120. self.assertFalse(os.path.exists(os.path.join(
  121. self.path, 'repos', 'test.git', 'hooks',
  122. 'post-receive.rtd')))
  123. # Try re-activate hook w/o the git repo
  124. data = {
  125. 'csrf_token': csrf_token,
  126. 'active': 'y',
  127. 'project_name': 'foo',
  128. }
  129. shutil.rmtree(os.path.join(self.path, 'repos', 'test.git'))
  130. output = self.app.post('/test/settings/Read the Doc', data=data)
  131. self.assertEqual(output.status_code, 404)
  132. if __name__ == '__main__':
  133. unittest.main(verbosity=2)