test_pagure_flask_ui_plugins_pagure_request_hook.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-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 PagureFlaskPluginPagureRequestHooktests(tests.SimplePagureTest):
  17. """ Tests for pagure_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/Pagure requests')
  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.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  30. self.assertTrue(
  31. '<input class="form-control" id="active" name="active" '
  32. 'type="checkbox" value="y">' in 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/Pagure requests', 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.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  42. self.assertTrue(
  43. '<input class="form-control" id="active" name="active" '
  44. 'type="checkbox" value="y">' in output.data)
  45. data['csrf_token'] = csrf_token
  46. # Create the requests repo
  47. tests.create_projects_git(os.path.join(self.path, 'repos', 'requests'))
  48. output = self.app.post(
  49. '/test/settings/Pagure requests', 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.assertTrue(
  56. '</button>\n Hook Pagure requests deactivated'
  57. in output.data)
  58. output = self.app.get('/test/settings/Pagure requests')
  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.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  64. self.assertTrue(
  65. '<input class="form-control" id="active" name="active" '
  66. 'type="checkbox" value="y">' in 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. }
  75. output = self.app.post(
  76. '/test/settings/Pagure requests', data=data,
  77. follow_redirects=True)
  78. self.assertEqual(output.status_code, 200)
  79. self.assertIn(
  80. '<section class="settings">\n <h3>Settings for test</h3>',
  81. output.data)
  82. self.assertTrue(
  83. '</button>\n Hook Pagure requests activated'
  84. in output.data)
  85. output = self.app.get('/test/settings/Pagure requests')
  86. self.assertEqual(output.status_code, 200)
  87. self.assertIn(
  88. '<div class="projectinfo m-t-1 m-b-1">\n'
  89. 'test project #1 </div>', output.data)
  90. self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  91. self.assertTrue(
  92. '<input checked class="form-control" id="active" name="active" '
  93. 'type="checkbox" value="y">' in output.data)
  94. self.assertTrue(os.path.exists(os.path.join(
  95. self.path, 'repos', 'requests', 'test.git', 'hooks',
  96. 'post-receive.pagure-requests')))
  97. # De-Activate hook
  98. data = {'csrf_token': csrf_token}
  99. output = self.app.post(
  100. '/test/settings/Pagure requests', data=data,
  101. follow_redirects=True)
  102. self.assertEqual(output.status_code, 200)
  103. self.assertIn(
  104. '<section class="settings">\n <h3>Settings for test</h3>',
  105. output.data)
  106. self.assertTrue(
  107. '</button>\n Hook Pagure requests deactivated'
  108. in output.data)
  109. output = self.app.get('/test/settings/Pagure requests')
  110. self.assertEqual(output.status_code, 200)
  111. self.assertIn(
  112. '<div class="projectinfo m-t-1 m-b-1">\n'
  113. 'test project #1 </div>', output.data)
  114. self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  115. self.assertTrue(
  116. '<input class="form-control" id="active" name="active" '
  117. 'type="checkbox" value="y">' in output.data)
  118. self.assertFalse(os.path.exists(os.path.join(
  119. self.path, 'requests', 'test.git', 'hooks',
  120. 'post-receive.pagure-requests')))
  121. # Try re-activate hook w/o the git repo
  122. data = {
  123. 'csrf_token': csrf_token,
  124. 'active': 'y',
  125. }
  126. shutil.rmtree(os.path.join(self.path, 'repos', 'requests', 'test.git'))
  127. output = self.app.post('/test/settings/Pagure requests', data=data)
  128. self.assertEqual(output.status_code, 404)
  129. if __name__ == '__main__':
  130. unittest.main(verbosity=2)