test_pagure_flask_ui_plugins_rtd_hook.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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'] = self.path
  33. pagure.APP.config['FORK_FOLDER'] = os.path.join(
  34. self.path, 'forks')
  35. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  36. self.path, 'requests')
  37. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  38. self.path, '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. 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/Read the Doc')
  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.get_data(as_text=True))
  51. self.assertIn('<h3>Read the Doc settings</h3>', output.get_data(as_text=True))
  52. self.assertIn(
  53. '<input id="active" name="active" type="checkbox" value="y">',
  54. output.get_data(as_text=True))
  55. csrf_token = output.get_data(as_text=True).split(
  56. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  57. data = {}
  58. output = self.app.post('/test/settings/Read the Doc', 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.get_data(as_text=True))
  63. self.assertIn('<h3>Read the Doc settings</h3>', output.get_data(as_text=True))
  64. self.assertIn(
  65. '<input id="active" name="active" type="checkbox" value="y">',
  66. output.get_data(as_text=True))
  67. data['csrf_token'] = csrf_token
  68. # Create the requests repo
  69. tests.create_projects_git(os.path.join(self.path, 'requests'))
  70. output = self.app.post(
  71. '/test/settings/Read the Doc', data=data,
  72. 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.get_data(as_text=True))
  77. self.assertIn(
  78. '</button>\n Hook Read the Doc inactived',
  79. output.get_data(as_text=True))
  80. output = self.app.get('/test/settings/Read the Doc')
  81. self.assertEqual(output.status_code, 200)
  82. self.assertIn(
  83. '<div class="projectinfo m-t-1 m-b-1">\n'
  84. 'test project #1 </div>', output.get_data(as_text=True))
  85. self.assertIn('<h3>Read the Doc settings</h3>', output.get_data(as_text=True))
  86. self.assertIn(
  87. '<input id="active" name="active" type="checkbox" value="y">',
  88. output.get_data(as_text=True))
  89. self.assertFalse(os.path.exists(os.path.join(
  90. self.path, 'requests', 'test.git', 'hooks',
  91. 'post-receive.pagure')))
  92. # Activate hook
  93. data = {
  94. 'csrf_token': csrf_token,
  95. 'active': 'y',
  96. 'project_name': 'foo',
  97. }
  98. output = self.app.post(
  99. '/test/settings/Read the Doc', data=data,
  100. follow_redirects=True)
  101. self.assertEqual(output.status_code, 200)
  102. self.assertIn(
  103. '<section class="settings">\n <h3>Settings for test</h3>',
  104. output.get_data(as_text=True))
  105. self.assertIn(
  106. '</button>\n Hook Read the Doc activated',
  107. output.get_data(as_text=True))
  108. output = self.app.get('/test/settings/Read the Doc')
  109. self.assertEqual(output.status_code, 200)
  110. self.assertIn(
  111. '<div class="projectinfo m-t-1 m-b-1">\n'
  112. 'test project #1 </div>', output.get_data(as_text=True))
  113. self.assertIn('<h3>Read the Doc settings</h3>', output.get_data(as_text=True))
  114. self.assertIn(
  115. '<input checked id="active" name="active" type="checkbox" '
  116. 'value="y">', output.get_data(as_text=True))
  117. self.assertTrue(os.path.exists(os.path.join(
  118. self.path, 'test.git', 'hooks',
  119. 'post-receive.rtd')))
  120. # De-Activate hook
  121. data = {'csrf_token': csrf_token}
  122. output = self.app.post(
  123. '/test/settings/Read the Doc', data=data,
  124. 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.get_data(as_text=True))
  129. self.assertIn(
  130. '</button>\n Hook Read the Doc inactived',
  131. output.get_data(as_text=True))
  132. output = self.app.get('/test/settings/Read the Doc')
  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.get_data(as_text=True))
  137. self.assertIn('<h3>Read the Doc settings</h3>', output.get_data(as_text=True))
  138. self.assertIn(
  139. '<input id="active" name="active" type="checkbox" '
  140. 'value="y">', output.get_data(as_text=True))
  141. self.assertFalse(os.path.exists(os.path.join(
  142. self.path, 'test.git', 'hooks',
  143. 'post-receive.rtd')))
  144. # Try re-activate hook w/o the git repo
  145. data = {
  146. 'csrf_token': csrf_token,
  147. 'active': 'y',
  148. 'project_name': 'foo',
  149. }
  150. shutil.rmtree(os.path.join(self.path, 'test.git'))
  151. output = self.app.post('/test/settings/Read the Doc', data=data)
  152. self.assertEqual(output.status_code, 404)
  153. if __name__ == '__main__':
  154. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  155. PagureFlaskPluginRtdHooktests)
  156. unittest.TextTestRunner(verbosity=2).run(SUITE)