test_pagure_flask_ui_plugins_fedmsg.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 PagureFlaskPluginFedmsgtests(tests.Modeltests):
  21. """ Tests for fedmsg plugin of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskPluginFedmsgtests, 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['DOCS_FOLDER'] = os.path.join(
  38. tests.HERE, 'docs')
  39. self.app = pagure.APP.test_client()
  40. def test_plugin_fedmsg(self):
  41. """ Test the fedmsg plugin on/off endpoint. """
  42. tests.create_projects(self.session)
  43. user = tests.FakeUser(username='pingou')
  44. with tests.user_set(pagure.APP, user):
  45. output = self.app.get('/test/settings/Fedmsg')
  46. self.assertEqual(output.status_code, 200)
  47. self.assertIn(
  48. '<div class="projectinfo m-t-1 m-b-1">\n'
  49. 'test project #1 </div>', output.data)
  50. self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
  51. self.assertTrue(
  52. '<input id="active" name="active" type="checkbox" value="y">'
  53. in output.data)
  54. csrf_token = output.data.split(
  55. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  56. data = {}
  57. output = self.app.post('/test/settings/Fedmsg', data=data)
  58. self.assertEqual(output.status_code, 200)
  59. self.assertIn(
  60. '<div class="projectinfo m-t-1 m-b-1">\n'
  61. 'test project #1 </div>', output.data)
  62. self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
  63. self.assertTrue(
  64. '<input id="active" name="active" type="checkbox" value="y">'
  65. in output.data)
  66. self.assertFalse(os.path.exists(os.path.join(
  67. tests.HERE, 'test.git', 'hooks', 'post-receive.fedmsg')))
  68. data['csrf_token'] = csrf_token
  69. # No git found
  70. output = self.app.post('/test/settings/Fedmsg', data=data)
  71. self.assertEqual(output.status_code, 404)
  72. tests.create_projects_git(tests.HERE)
  73. # With the git repo
  74. output = self.app.post(
  75. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  76. self.assertEqual(output.status_code, 200)
  77. self.assertIn(
  78. '<section class="settings">\n <h3>Settings for test</h3>',
  79. output.data)
  80. self.assertIn(
  81. '</button>\n Hook Fedmsg inactived',
  82. output.data)
  83. output = self.app.get('/test/settings/Fedmsg', data=data)
  84. self.assertIn(
  85. '<div class="projectinfo m-t-1 m-b-1">\n'
  86. 'test project #1 </div>', output.data)
  87. self.assertIn('<h3>Fedmsg settings</h3>', output.data)
  88. self.assertIn(
  89. '<input id="active" name="active" type="checkbox" value="y">',
  90. output.data)
  91. self.assertFalse(os.path.exists(os.path.join(
  92. tests.HERE, 'test.git', 'hooks', 'post-receive.fedmsg')))
  93. # Activate hook
  94. data = {
  95. 'csrf_token': csrf_token,
  96. 'active': 'y',
  97. }
  98. output = self.app.post(
  99. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  100. self.assertEqual(output.status_code, 200)
  101. self.assertIn(
  102. '<section class="settings">\n <h3>Settings for test</h3>',
  103. output.data)
  104. self.assertIn(
  105. '</button>\n Hook Fedmsg activated',
  106. output.data)
  107. output = self.app.get('/test/settings/Fedmsg', data=data)
  108. self.assertEqual(output.status_code, 200)
  109. self.assertIn(
  110. '<div class="projectinfo m-t-1 m-b-1">\n'
  111. 'test project #1 </div>', output.data)
  112. self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
  113. self.assertTrue(
  114. '<input checked id="active" name="active" type="checkbox" '
  115. 'value="y">' in output.data)
  116. self.assertTrue(os.path.exists(os.path.join(
  117. tests.HERE, 'test.git', 'hooks', 'post-receive.fedmsg')))
  118. # De-Activate hook
  119. data = {'csrf_token': csrf_token}
  120. output = self.app.post(
  121. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  122. self.assertEqual(output.status_code, 200)
  123. self.assertIn(
  124. '<section class="settings">\n <h3>Settings for test</h3>',
  125. output.data)
  126. self.assertIn(
  127. '</button>\n Hook Fedmsg inactived',
  128. output.data)
  129. output = self.app.get('/test/settings/Fedmsg', data=data)
  130. self.assertEqual(output.status_code, 200)
  131. self.assertIn(
  132. '<div class="projectinfo m-t-1 m-b-1">\n'
  133. 'test project #1 </div>', output.data)
  134. self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
  135. self.assertTrue(
  136. '<input id="active" name="active" type="checkbox" '
  137. 'value="y">' in output.data)
  138. self.assertFalse(os.path.exists(os.path.join(
  139. tests.HERE, 'test.git', 'hooks', 'post-receive.fedmsg')))
  140. if __name__ == '__main__':
  141. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  142. PagureFlaskPluginFedmsgtests)
  143. unittest.TextTestRunner(verbosity=2).run(SUITE)