test_pagure_flask_ui_plugins_pagure_request_hook.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import pygit2
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskPluginPagureRequestHooktests(tests.Modeltests):
  21. """ Tests for pagure_hook plugin of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskPluginPagureRequestHooktests, self).setUp()
  25. pagure.APP.config['TESTING'] = True
  26. pagure.SESSION = self.session
  27. pagure.ui.SESSION = self.session
  28. pagure.ui.app.SESSION = self.session
  29. pagure.ui.plugins.SESSION = self.session
  30. pagure.ui.repo.SESSION = self.session
  31. pagure.ui.filters.SESSION = self.session
  32. self.app = pagure.APP.test_client()
  33. def test_plugin_pagure_request(self):
  34. """ Test the pagure_request plugin on/off endpoint. """
  35. tests.create_projects(self.session)
  36. tests.create_projects_git(os.path.join(self.path, 'repos'))
  37. user = tests.FakeUser(username='pingou')
  38. with tests.user_set(pagure.APP, user):
  39. output = self.app.get('/test/settings/Pagure requests')
  40. self.assertEqual(output.status_code, 200)
  41. self.assertIn(
  42. '<div class="projectinfo m-t-1 m-b-1">\n'
  43. 'test project #1 </div>', output.data)
  44. self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  45. self.assertTrue(
  46. '<input id="active" name="active" type="checkbox" value="y">'
  47. in output.data)
  48. csrf_token = output.data.split(
  49. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  50. data = {}
  51. output = self.app.post('/test/settings/Pagure requests', data=data)
  52. self.assertEqual(output.status_code, 200)
  53. self.assertIn(
  54. '<div class="projectinfo m-t-1 m-b-1">\n'
  55. 'test project #1 </div>', output.data)
  56. self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  57. self.assertTrue(
  58. '<input id="active" name="active" type="checkbox" value="y">'
  59. in output.data)
  60. data['csrf_token'] = csrf_token
  61. # Create the requests repo
  62. tests.create_projects_git(os.path.join(self.path, 'requests'))
  63. output = self.app.post(
  64. '/test/settings/Pagure requests', data=data,
  65. follow_redirects=True)
  66. self.assertEqual(output.status_code, 200)
  67. self.assertIn(
  68. '<section class="settings">\n <h3>Settings for test</h3>',
  69. output.data)
  70. self.assertTrue(
  71. '</button>\n Hook Pagure requests deactivated'
  72. in output.data)
  73. output = self.app.get('/test/settings/Pagure requests')
  74. self.assertEqual(output.status_code, 200)
  75. self.assertIn(
  76. '<div class="projectinfo m-t-1 m-b-1">\n'
  77. 'test project #1 </div>', output.data)
  78. self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  79. self.assertTrue(
  80. '<input id="active" name="active" type="checkbox" value="y">'
  81. in output.data)
  82. self.assertFalse(os.path.exists(os.path.join(
  83. self.path, 'requests', 'test.git', 'hooks',
  84. 'post-receive.pagure')))
  85. # Activate hook
  86. data = {
  87. 'csrf_token': csrf_token,
  88. 'active': 'y',
  89. }
  90. output = self.app.post(
  91. '/test/settings/Pagure requests', data=data,
  92. follow_redirects=True)
  93. self.assertEqual(output.status_code, 200)
  94. self.assertIn(
  95. '<section class="settings">\n <h3>Settings for test</h3>',
  96. output.data)
  97. self.assertTrue(
  98. '</button>\n Hook Pagure requests activated'
  99. in output.data)
  100. output = self.app.get('/test/settings/Pagure requests')
  101. self.assertEqual(output.status_code, 200)
  102. self.assertIn(
  103. '<div class="projectinfo m-t-1 m-b-1">\n'
  104. 'test project #1 </div>', output.data)
  105. self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  106. self.assertTrue(
  107. '<input checked id="active" name="active" type="checkbox" '
  108. 'value="y">' in output.data)
  109. self.assertTrue(os.path.exists(os.path.join(
  110. self.path, 'requests', 'test.git', 'hooks',
  111. 'post-receive.pagure-requests')))
  112. # De-Activate hook
  113. data = {'csrf_token': csrf_token}
  114. output = self.app.post(
  115. '/test/settings/Pagure requests', data=data,
  116. follow_redirects=True)
  117. self.assertEqual(output.status_code, 200)
  118. self.assertIn(
  119. '<section class="settings">\n <h3>Settings for test</h3>',
  120. output.data)
  121. self.assertTrue(
  122. '</button>\n Hook Pagure requests deactivated'
  123. in output.data)
  124. output = self.app.get('/test/settings/Pagure requests')
  125. self.assertEqual(output.status_code, 200)
  126. self.assertIn(
  127. '<div class="projectinfo m-t-1 m-b-1">\n'
  128. 'test project #1 </div>', output.data)
  129. self.assertTrue('<h3>Pagure requests settings</h3>' in output.data)
  130. self.assertTrue(
  131. '<input id="active" name="active" type="checkbox" '
  132. 'value="y">' in output.data)
  133. self.assertFalse(os.path.exists(os.path.join(
  134. self.path, 'requests', 'test.git', 'hooks',
  135. 'post-receive.pagure-requests')))
  136. # Try re-activate hook w/o the git repo
  137. data = {
  138. 'csrf_token': csrf_token,
  139. 'active': 'y',
  140. }
  141. shutil.rmtree(os.path.join(self.path, 'requests', 'test.git'))
  142. output = self.app.post('/test/settings/Pagure requests', data=data)
  143. self.assertEqual(output.status_code, 404)
  144. if __name__ == '__main__':
  145. unittest.main(verbosity=2)