test_pagure_flask_ui_plugins_pagure_hook.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 PagureFlaskPluginPagureHooktests(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(PagureFlaskPluginPagureHooktests, 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. pagure.APP.config['GIT_FOLDER'] = tests.HERE
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. tests.HERE, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. tests.HERE, 'tickets')
  37. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  38. tests.HERE, 'requests')
  39. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  40. tests.HERE, 'docs')
  41. self.app = pagure.APP.test_client()
  42. def test_plugin_mail(self):
  43. """ Test the pagure hook plugin on/off endpoint. """
  44. tests.create_projects(self.session)
  45. user = tests.FakeUser(username='pingou')
  46. with tests.user_set(pagure.APP, user):
  47. output = self.app.get('/test/settings/Pagure')
  48. self.assertEqual(output.status_code, 200)
  49. self.assertIn(
  50. '<div class="projectinfo m-t-1 m-b-1">\n'
  51. 'test project #1 </div>', output.data)
  52. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  53. self.assertTrue(
  54. '<input id="active" name="active" type="checkbox" value="y">'
  55. in output.data)
  56. csrf_token = output.data.split(
  57. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  58. data = {}
  59. output = self.app.post('/test/settings/Pagure', data=data)
  60. self.assertEqual(output.status_code, 200)
  61. self.assertIn(
  62. '<div class="projectinfo m-t-1 m-b-1">\n'
  63. 'test project #1 </div>', output.data)
  64. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  65. self.assertTrue(
  66. '<input id="active" name="active" type="checkbox" value="y">'
  67. in output.data)
  68. data['csrf_token'] = csrf_token
  69. # No git found
  70. output = self.app.post('/test/settings/Pagure', data=data)
  71. self.assertEqual(output.status_code, 404)
  72. tests.create_projects_git(tests.HERE)
  73. tests.create_projects_git(os.path.join(tests.HERE, 'docs'))
  74. tests.create_projects_git(os.path.join(tests.HERE, 'requests'))
  75. # With the git repo
  76. output = self.app.post(
  77. '/test/settings/Pagure', data=data, 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 inactived'
  84. in output.data)
  85. output = self.app.get('/test/settings/Pagure')
  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 settings</h3>' in output.data)
  91. self.assertTrue(
  92. '<input id="active" name="active" type="checkbox" value="y">'
  93. in output.data)
  94. self.assertFalse(os.path.exists(os.path.join(
  95. tests.HERE, 'test.git', 'hooks', 'post-receive.pagure')))
  96. # Activate hook
  97. data = {
  98. 'csrf_token': csrf_token,
  99. 'active': 'y',
  100. }
  101. output = self.app.post(
  102. '/test/settings/Pagure', data=data, 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.assertTrue(
  108. '</button>\n Hook Pagure activated'
  109. in output.data)
  110. output = self.app.get('/test/settings/Pagure')
  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.assertTrue('<h3>Pagure settings</h3>' in output.data)
  116. self.assertTrue(
  117. '<input checked id="active" name="active" type="checkbox" '
  118. 'value="y">' in output.data)
  119. self.assertTrue(os.path.exists(os.path.join(
  120. tests.HERE, 'test.git', 'hooks', 'post-receive.pagure')))
  121. # De-Activate hook
  122. data = {'csrf_token': csrf_token}
  123. output = self.app.post(
  124. '/test/settings/Pagure', data=data, follow_redirects=True)
  125. self.assertEqual(output.status_code, 200)
  126. self.assertIn(
  127. '<section class="settings">\n <h3>Settings for test</h3>',
  128. output.data)
  129. self.assertTrue(
  130. '</button>\n Hook Pagure inactived'
  131. in output.data)
  132. output = self.app.get('/test/settings/Pagure')
  133. self.assertEqual(output.status_code, 200)
  134. self.assertIn(
  135. '<div class="projectinfo m-t-1 m-b-1">\n'
  136. 'test project #1 </div>', output.data)
  137. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  138. self.assertTrue(
  139. '<input id="active" name="active" type="checkbox" '
  140. 'value="y">' in output.data)
  141. self.assertFalse(os.path.exists(os.path.join(
  142. tests.HERE, 'test.git', 'hooks', 'post-receive.pagure')))
  143. if __name__ == '__main__':
  144. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  145. PagureFlaskPluginPagureHooktests)
  146. unittest.TextTestRunner(verbosity=2).run(SUITE)