test_pagure_flask_ui_plugins_rtd_hook.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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-control" 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-control" 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-control" 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-control" id="active" name="active" '
  97. 'type="checkbox" value="y">', output_text)
  98. self.assertTrue(os.path.exists(os.path.join(
  99. self.path, 'repos', 'test.git', 'hooks',
  100. 'post-receive.rtd')))
  101. # De-Activate hook
  102. data = {'csrf_token': csrf_token}
  103. output = self.app.post(
  104. '/test/settings/Read the Doc', data=data,
  105. follow_redirects=True)
  106. self.assertEqual(output.status_code, 200)
  107. output_text = output.get_data(as_text=True)
  108. self.assertIn(
  109. '<h5 class="pl-2 font-weight-bold text-muted">'
  110. 'Project Settings</h5>\n', output_text)
  111. self.assertIn(
  112. 'Hook Read the Doc deactivated',
  113. output_text)
  114. output = self.app.get('/test/settings/Read the Doc')
  115. self.assertEqual(output.status_code, 200)
  116. output_text = output.get_data(as_text=True)
  117. self.assertIn(
  118. '<title>Settings Read the Doc - test - Pagure</title>',
  119. output_text)
  120. self.assertIn(
  121. '<input class="form-control" id="active" name="active" '
  122. 'type="checkbox" value="y">', output_text)
  123. self.assertFalse(os.path.exists(os.path.join(
  124. self.path, 'repos', 'test.git', 'hooks',
  125. 'post-receive.rtd')))
  126. # Try re-activate hook w/o the git repo
  127. data = {
  128. 'csrf_token': csrf_token,
  129. 'active': 'y',
  130. 'project_name': 'foo',
  131. }
  132. shutil.rmtree(os.path.join(self.path, 'repos', 'test.git'))
  133. output = self.app.post('/test/settings/Read the Doc', data=data)
  134. self.assertEqual(output.status_code, 404)
  135. if __name__ == '__main__':
  136. unittest.main(verbosity=2)