test_pagure_flask_ui_plugins_fedmsg.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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'] = self.path
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. self.path, 'forks')
  35. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  36. self.path, 'tickets')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. self.path, '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. tests.create_projects_git(self.path)
  44. user = tests.FakeUser(username='pingou')
  45. with tests.user_set(pagure.APP, user):
  46. output = self.app.get('/test/settings/Fedmsg')
  47. self.assertEqual(output.status_code, 200)
  48. self.assertIn(
  49. '<div class="projectinfo m-t-1 m-b-1">\n'
  50. 'test project #1 </div>', output.data)
  51. self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
  52. self.assertTrue(
  53. '<input id="active" name="active" type="checkbox" value="y">'
  54. in output.data)
  55. csrf_token = output.data.split(
  56. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  57. data = {}
  58. output = self.app.post('/test/settings/Fedmsg', data=data)
  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>Fedmsg settings</h3>' in output.data)
  64. self.assertTrue(
  65. '<input id="active" name="active" type="checkbox" value="y">'
  66. in output.data)
  67. self.assertFalse(os.path.exists(os.path.join(
  68. self.path, 'test.git', 'hooks', 'post-receive.fedmsg')))
  69. data['csrf_token'] = csrf_token
  70. # With the git repo
  71. output = self.app.post(
  72. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  73. self.assertEqual(output.status_code, 200)
  74. self.assertIn(
  75. '<section class="settings">\n <h3>Settings for test</h3>',
  76. output.data)
  77. self.assertIn(
  78. '</button>\n Hook Fedmsg inactived',
  79. output.data)
  80. output = self.app.get('/test/settings/Fedmsg', data=data)
  81. self.assertIn(
  82. '<div class="projectinfo m-t-1 m-b-1">\n'
  83. 'test project #1 </div>', output.data)
  84. self.assertIn('<h3>Fedmsg settings</h3>', output.data)
  85. self.assertIn(
  86. '<input id="active" name="active" type="checkbox" value="y">',
  87. output.data)
  88. self.assertFalse(os.path.exists(os.path.join(
  89. self.path, 'test.git', 'hooks', 'post-receive.fedmsg')))
  90. # Activate hook
  91. data = {
  92. 'csrf_token': csrf_token,
  93. 'active': 'y',
  94. }
  95. output = self.app.post(
  96. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  97. self.assertEqual(output.status_code, 200)
  98. self.assertIn(
  99. '<section class="settings">\n <h3>Settings for test</h3>',
  100. output.data)
  101. self.assertIn(
  102. '</button>\n Hook Fedmsg activated',
  103. output.data)
  104. output = self.app.get('/test/settings/Fedmsg', data=data)
  105. self.assertEqual(output.status_code, 200)
  106. self.assertIn(
  107. '<div class="projectinfo m-t-1 m-b-1">\n'
  108. 'test project #1 </div>', output.data)
  109. self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
  110. self.assertTrue(
  111. '<input checked id="active" name="active" type="checkbox" '
  112. 'value="y">' in output.data)
  113. self.assertTrue(os.path.exists(os.path.join(
  114. self.path, 'test.git', 'hooks', 'post-receive.fedmsg')))
  115. # De-Activate hook
  116. data = {'csrf_token': csrf_token}
  117. output = self.app.post(
  118. '/test/settings/Fedmsg', data=data, follow_redirects=True)
  119. self.assertEqual(output.status_code, 200)
  120. self.assertIn(
  121. '<section class="settings">\n <h3>Settings for test</h3>',
  122. output.data)
  123. self.assertIn(
  124. '</button>\n Hook Fedmsg inactived',
  125. output.data)
  126. output = self.app.get('/test/settings/Fedmsg', data=data)
  127. self.assertEqual(output.status_code, 200)
  128. self.assertIn(
  129. '<div class="projectinfo m-t-1 m-b-1">\n'
  130. 'test project #1 </div>', output.data)
  131. self.assertTrue('<h3>Fedmsg settings</h3>' in output.data)
  132. self.assertTrue(
  133. '<input id="active" name="active" type="checkbox" '
  134. 'value="y">' in output.data)
  135. self.assertFalse(os.path.exists(os.path.join(
  136. self.path, 'test.git', 'hooks', 'post-receive.fedmsg')))
  137. if __name__ == '__main__':
  138. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  139. PagureFlaskPluginFedmsgtests)
  140. unittest.TextTestRunner(verbosity=2).run(SUITE)