test_pagure_flask_ui_plugins_rtd_hook.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  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 pagure.lib
  15. import tests
  16. class PagureFlaskPluginRtdHooktests(tests.SimplePagureTest):
  17. """ Tests for rtd_hook plugin of pagure """
  18. def test_plugin_pagure_request(self):
  19. """ Test the pagure_request 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/Read the Doc')
  25. self.assertEqual(output.status_code, 200)
  26. self.assertIn(
  27. '<div class="projectinfo m-t-1 m-b-1">\n'
  28. 'test project #1 </div>', output.data)
  29. self.assertIn('<h3>Read the Doc settings</h3>', output.data)
  30. self.assertIn(
  31. '<input class="form-control" id="active" name="active" '
  32. 'type="checkbox" value="y">', output.data)
  33. csrf_token = output.data.split(
  34. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  35. data = {}
  36. output = self.app.post('/test/settings/Read the Doc', data=data)
  37. self.assertEqual(output.status_code, 200)
  38. self.assertIn(
  39. '<div class="projectinfo m-t-1 m-b-1">\n'
  40. 'test project #1 </div>', output.data)
  41. self.assertIn('<h3>Read the Doc settings</h3>', output.data)
  42. self.assertIn(
  43. '<input class="form-control" id="active" name="active" '
  44. 'type="checkbox" value="y">', output.data)
  45. data['csrf_token'] = csrf_token
  46. # Create the requests repo
  47. tests.create_projects_git(os.path.join(self.path, 'requests'))
  48. output = self.app.post(
  49. '/test/settings/Read the Doc', data=data,
  50. follow_redirects=True)
  51. self.assertEqual(output.status_code, 200)
  52. self.assertIn(
  53. '<section class="settings">\n <h3>Settings for test</h3>',
  54. output.data)
  55. self.assertIn(
  56. '</button>\n Hook Read the Doc deactivated',
  57. output.data)
  58. output = self.app.get('/test/settings/Read the Doc')
  59. self.assertEqual(output.status_code, 200)
  60. self.assertIn(
  61. '<div class="projectinfo m-t-1 m-b-1">\n'
  62. 'test project #1 </div>', output.data)
  63. self.assertIn('<h3>Read the Doc settings</h3>', output.data)
  64. self.assertIn(
  65. '<input class="form-control" id="active" name="active" '
  66. 'type="checkbox" value="y">', output.data)
  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. self.assertIn(
  81. '<section class="settings">\n <h3>Settings for test</h3>',
  82. output.data)
  83. self.assertIn(
  84. '</button>\n Hook Read the Doc activated',
  85. output.data)
  86. output = self.app.get('/test/settings/Read the Doc')
  87. self.assertEqual(output.status_code, 200)
  88. self.assertIn(
  89. '<div class="projectinfo m-t-1 m-b-1">\n'
  90. 'test project #1 </div>', output.data)
  91. self.assertIn('<h3>Read the Doc settings</h3>', output.data)
  92. self.assertIn(
  93. '<input checked class="form-control" id="active" name="active" '
  94. 'type="checkbox" value="y">', output.data)
  95. self.assertTrue(os.path.exists(os.path.join(
  96. self.path, 'repos', 'test.git', 'hooks',
  97. 'post-receive.rtd')))
  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. self.assertIn(
  105. '<section class="settings">\n <h3>Settings for test</h3>',
  106. output.data)
  107. self.assertIn(
  108. '</button>\n Hook Read the Doc deactivated',
  109. output.data)
  110. output = self.app.get('/test/settings/Read the Doc')
  111. self.assertEqual(output.status_code, 200)
  112. self.assertIn(
  113. '<div class="projectinfo m-t-1 m-b-1">\n'
  114. 'test project #1 </div>', output.data)
  115. self.assertIn('<h3>Read the Doc settings</h3>', output.data)
  116. self.assertIn(
  117. '<input class="form-control" id="active" name="active" '
  118. 'type="checkbox" value="y">', output.data)
  119. self.assertFalse(os.path.exists(os.path.join(
  120. self.path, 'repos', 'test.git', 'hooks',
  121. 'post-receive.rtd')))
  122. # Try re-activate hook w/o the git repo
  123. data = {
  124. 'csrf_token': csrf_token,
  125. 'active': 'y',
  126. 'project_name': 'foo',
  127. }
  128. shutil.rmtree(os.path.join(self.path, 'repos', 'test.git'))
  129. output = self.app.post('/test/settings/Read the Doc', data=data)
  130. self.assertEqual(output.status_code, 404)
  131. if __name__ == '__main__':
  132. unittest.main(verbosity=2)