test_pagure_flask_ui_plugins_rtd_hook.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 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 PagureFlaskPluginRtdHooktests(tests.Modeltests):
  21. """ Tests for rtd_hook plugin of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskPluginRtdHooktests, 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['REQUESTS_FOLDER'] = os.path.join(
  36. tests.HERE, 'requests')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. tests.HERE, 'docs')
  39. self.app = pagure.APP.test_client()
  40. def test_plugin_pagure_request(self):
  41. """ Test the pagure_request 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/Read the Doc')
  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.assertIn('<h3>Read the Doc settings</h3>', output.data)
  51. self.assertIn(
  52. '<input id="active" name="active" type="checkbox" value="y">',
  53. 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/Read the Doc', 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.assertIn('<h3>Read the Doc settings</h3>', output.data)
  63. self.assertIn(
  64. '<input id="active" name="active" type="checkbox" value="y">',
  65. output.data)
  66. data['csrf_token'] = csrf_token
  67. # No git found
  68. output = self.app.post('/test/settings/Read the Doc', data=data)
  69. self.assertEqual(output.status_code, 404)
  70. # Create both the requests repo
  71. tests.create_projects_git(os.path.join(tests.HERE, 'requests'))
  72. # With the git repo
  73. tests.create_projects_git(tests.HERE)
  74. output = self.app.post(
  75. '/test/settings/Read the Doc', data=data,
  76. follow_redirects=True)
  77. self.assertEqual(output.status_code, 200)
  78. self.assertIn(
  79. '<section class="settings">\n <h3>Settings for test</h3>',
  80. output.data)
  81. self.assertIn(
  82. '</button>\n Hook Read the Doc inactived',
  83. output.data)
  84. output = self.app.get('/test/settings/Read the Doc')
  85. self.assertEqual(output.status_code, 200)
  86. self.assertIn(
  87. '<div class="projectinfo m-t-1 m-b-1">\n'
  88. 'test project #1 </div>', output.data)
  89. self.assertIn('<h3>Read the Doc settings</h3>', output.data)
  90. self.assertIn(
  91. '<input id="active" name="active" type="checkbox" value="y">',
  92. output.data)
  93. self.assertFalse(os.path.exists(os.path.join(
  94. tests.HERE, 'requests', 'test.git', 'hooks',
  95. 'post-receive.pagure')))
  96. # Activate hook
  97. data = {
  98. 'csrf_token': csrf_token,
  99. 'active': 'y',
  100. 'project_name': 'foo',
  101. }
  102. output = self.app.post(
  103. '/test/settings/Read the Doc', data=data,
  104. follow_redirects=True)
  105. self.assertEqual(output.status_code, 200)
  106. self.assertIn(
  107. '<section class="settings">\n <h3>Settings for test</h3>',
  108. output.data)
  109. self.assertIn(
  110. '</button>\n Hook Read the Doc activated',
  111. output.data)
  112. output = self.app.get('/test/settings/Read the Doc')
  113. self.assertEqual(output.status_code, 200)
  114. self.assertIn(
  115. '<div class="projectinfo m-t-1 m-b-1">\n'
  116. 'test project #1 </div>', output.data)
  117. self.assertIn('<h3>Read the Doc settings</h3>', output.data)
  118. self.assertIn(
  119. '<input checked id="active" name="active" type="checkbox" '
  120. 'value="y">', output.data)
  121. self.assertTrue(os.path.exists(os.path.join(
  122. tests.HERE, 'test.git', 'hooks',
  123. 'post-receive.rtd')))
  124. # De-Activate hook
  125. data = {'csrf_token': csrf_token}
  126. output = self.app.post(
  127. '/test/settings/Read the Doc', data=data,
  128. follow_redirects=True)
  129. self.assertEqual(output.status_code, 200)
  130. self.assertIn(
  131. '<section class="settings">\n <h3>Settings for test</h3>',
  132. output.data)
  133. self.assertIn(
  134. '</button>\n Hook Read the Doc inactived',
  135. output.data)
  136. output = self.app.get('/test/settings/Read the Doc')
  137. self.assertEqual(output.status_code, 200)
  138. self.assertIn(
  139. '<div class="projectinfo m-t-1 m-b-1">\n'
  140. 'test project #1 </div>', output.data)
  141. self.assertIn('<h3>Read the Doc settings</h3>', output.data)
  142. self.assertIn(
  143. '<input id="active" name="active" type="checkbox" '
  144. 'value="y">', output.data)
  145. self.assertFalse(os.path.exists(os.path.join(
  146. tests.HERE, 'test.git', 'hooks',
  147. 'post-receive.rtd')))
  148. # Try re-activate hook w/o the git repo
  149. data = {
  150. 'csrf_token': csrf_token,
  151. 'active': 'y',
  152. 'project_name': 'foo',
  153. }
  154. shutil.rmtree(os.path.join(tests.HERE, 'test.git'))
  155. output = self.app.post('/test/settings/Read the Doc', data=data)
  156. self.assertEqual(output.status_code, 404)
  157. if __name__ == '__main__':
  158. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  159. PagureFlaskPluginRtdHooktests)
  160. unittest.TextTestRunner(verbosity=2).run(SUITE)