test_pagure_flask_ui_plugins_pagure_hook.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. self.app = pagure.APP.test_client()
  33. def test_plugin_mail(self):
  34. """ Test the pagure hook 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')
  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 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', 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 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. tests.create_projects_git(os.path.join(self.path, 'docs'))
  62. tests.create_projects_git(os.path.join(self.path, 'requests'))
  63. # With the git repo
  64. output = self.app.post(
  65. '/test/settings/Pagure', data=data, 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 deactivated'
  72. in output.data)
  73. output = self.app.get('/test/settings/Pagure')
  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 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, 'repos', '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', data=data, follow_redirects=True)
  92. self.assertEqual(output.status_code, 200)
  93. self.assertIn(
  94. '<section class="settings">\n <h3>Settings for test</h3>',
  95. output.data)
  96. self.assertTrue(
  97. '</button>\n Hook Pagure activated'
  98. in output.data)
  99. output = self.app.get('/test/settings/Pagure')
  100. self.assertEqual(output.status_code, 200)
  101. self.assertIn(
  102. '<div class="projectinfo m-t-1 m-b-1">\n'
  103. 'test project #1 </div>', output.data)
  104. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  105. self.assertTrue(
  106. '<input checked id="active" name="active" type="checkbox" '
  107. 'value="y">' in output.data)
  108. self.assertTrue(os.path.exists(os.path.join(
  109. self.path, 'repos', 'test.git', 'hooks',
  110. 'post-receive.pagure')))
  111. # De-Activate hook
  112. data = {'csrf_token': csrf_token}
  113. output = self.app.post(
  114. '/test/settings/Pagure', data=data, follow_redirects=True)
  115. self.assertEqual(output.status_code, 200)
  116. self.assertIn(
  117. '<section class="settings">\n <h3>Settings for test</h3>',
  118. output.data)
  119. self.assertTrue(
  120. '</button>\n Hook Pagure deactivated'
  121. in output.data)
  122. output = self.app.get('/test/settings/Pagure')
  123. self.assertEqual(output.status_code, 200)
  124. self.assertIn(
  125. '<div class="projectinfo m-t-1 m-b-1">\n'
  126. 'test project #1 </div>', output.data)
  127. self.assertTrue('<h3>Pagure settings</h3>' in output.data)
  128. self.assertTrue(
  129. '<input id="active" name="active" type="checkbox" '
  130. 'value="y">' in output.data)
  131. self.assertFalse(os.path.exists(os.path.join(
  132. self.path, 'repos', 'test.git', 'hooks',
  133. 'post-receive.pagure')))
  134. if __name__ == '__main__':
  135. unittest.main(verbosity=2)