test_pagure_flask_ui_admin.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 PagureFlaskAdmintests(tests.Modeltests):
  21. """ Tests for flask admin controller of pagure """
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskAdmintests, 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.filters.SESSION = self.session
  30. pagure.ui.repo.SESSION = self.session
  31. pagure.ui.admin.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_admin_index(self):
  41. """ Test the admin_index endpoint. """
  42. output = self.app.get('/admin')
  43. self.assertEqual(output.status_code, 302)
  44. user = tests.FakeUser()
  45. with tests.user_set(pagure.APP, user):
  46. output = self.app.post('/admin', follow_redirects=True)
  47. self.assertEqual(output.status_code, 404)
  48. self.assertIn(
  49. '</button>\n Access restricted',
  50. output.data)
  51. user.username = 'foo'
  52. with tests.user_set(pagure.APP, user):
  53. output = self.app.get('/admin', follow_redirects=True)
  54. self.assertEqual(output.status_code, 200)
  55. self.assertIn(
  56. '</button>\n Access restricted',
  57. output.data)
  58. user = tests.FakeUser(
  59. username='pingou',
  60. groups=pagure.APP.config['ADMIN_GROUP'])
  61. with tests.user_set(pagure.APP, user):
  62. output = self.app.get('/admin', follow_redirects=True)
  63. self.assertEqual(output.status_code, 200)
  64. self.assertTrue('<h2>Admin section</h2>' in output.data)
  65. self.assertTrue('Re-generate gitolite ACLs file' in output.data)
  66. self.assertTrue(
  67. 'Re-generate user ssh key files' in output.data)
  68. @patch('pagure.lib.git.write_gitolite_acls')
  69. def test_admin_generate_acl(self, wga):
  70. """ Test the admin_generate_acl endpoint. """
  71. wga.return_value = True
  72. output = self.app.get('/admin/gitolite')
  73. self.assertEqual(output.status_code, 404)
  74. output = self.app.post('/admin/gitolite')
  75. self.assertEqual(output.status_code, 302)
  76. user = tests.FakeUser()
  77. with tests.user_set(pagure.APP, user):
  78. output = self.app.post('/admin/gitolite', follow_redirects=True)
  79. self.assertEqual(output.status_code, 404)
  80. self.assertIn(
  81. '</button>\n Access restricted',
  82. output.data)
  83. user.username = 'foo'
  84. with tests.user_set(pagure.APP, user):
  85. output = self.app.post('/admin/gitolite', follow_redirects=True)
  86. self.assertEqual(output.status_code, 200)
  87. self.assertIn(
  88. '</button>\n Access restricted',
  89. output.data)
  90. user = tests.FakeUser(
  91. username='pingou',
  92. groups=pagure.APP.config['ADMIN_GROUP'])
  93. with tests.user_set(pagure.APP, user):
  94. output = self.app.post('/admin/gitolite', follow_redirects=True)
  95. self.assertEqual(output.status_code, 200)
  96. self.assertTrue('<h2>Admin section</h2>' in output.data)
  97. self.assertTrue('Re-generate gitolite ACLs file' in output.data)
  98. self.assertTrue(
  99. 'Re-generate user ssh key files' in output.data)
  100. self.assertFalse(
  101. '<li class="message">Gitolite ACLs updated</li>'
  102. in output.data)
  103. csrf_token = output.data.split(
  104. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  105. data = {'csrf_token': csrf_token}
  106. output = self.app.post(
  107. '/admin/gitolite', data=data, follow_redirects=True)
  108. self.assertEqual(output.status_code, 200)
  109. self.assertTrue('<h2>Admin section</h2>' in output.data)
  110. self.assertTrue('Re-generate gitolite ACLs file' in output.data)
  111. self.assertTrue(
  112. 'Re-generate user ssh key files' in output.data)
  113. self.assertTrue(
  114. '</button>\n Gitolite ACLs updated'
  115. in output.data)
  116. @patch('pagure.generate_user_key_files')
  117. def test_admin_refresh_ssh(self, gakf):
  118. """ Test the admin_refresh_ssh endpoint. """
  119. gakf.return_value = True
  120. output = self.app.get('/admin/ssh')
  121. self.assertEqual(output.status_code, 404)
  122. output = self.app.post('/admin/ssh')
  123. self.assertEqual(output.status_code, 302)
  124. user = tests.FakeUser()
  125. with tests.user_set(pagure.APP, user):
  126. output = self.app.post('/admin/ssh', follow_redirects=True)
  127. self.assertEqual(output.status_code, 404)
  128. self.assertIn(
  129. '</button>\n Access restricted',
  130. output.data)
  131. user.username = 'foo'
  132. with tests.user_set(pagure.APP, user):
  133. output = self.app.post('/admin/ssh', follow_redirects=True)
  134. self.assertEqual(output.status_code, 200)
  135. self.assertIn(
  136. '</button>\n Access restricted',
  137. output.data)
  138. user = tests.FakeUser(
  139. username='pingou',
  140. groups=pagure.APP.config['ADMIN_GROUP'])
  141. with tests.user_set(pagure.APP, user):
  142. output = self.app.post('/admin/ssh', follow_redirects=True)
  143. self.assertEqual(output.status_code, 200)
  144. self.assertTrue('<h2>Admin section</h2>' in output.data)
  145. self.assertTrue('Re-generate gitolite ACLs file' in output.data)
  146. self.assertTrue(
  147. 'Re-generate user ssh key files' in output.data)
  148. self.assertFalse(
  149. '<li class="message">Authorized file updated</li>'
  150. in output.data)
  151. csrf_token = output.data.split(
  152. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  153. data = {'csrf_token': csrf_token}
  154. output = self.app.post(
  155. '/admin/ssh', data=data, follow_redirects=True)
  156. self.assertEqual(output.status_code, 200)
  157. self.assertTrue('<h2>Admin section</h2>' in output.data)
  158. self.assertTrue('Re-generate gitolite ACLs file' in output.data)
  159. self.assertTrue(
  160. 'Re-generate user ssh key files' in output.data)
  161. self.assertTrue(
  162. '</button>\n User key files regenerated'
  163. in output.data)
  164. def test_admin_generate_hook_token(self):
  165. """ Test the admin_generate_hook_token endpoint. """
  166. output = self.app.get('/admin/hook_token')
  167. self.assertEqual(output.status_code, 404)
  168. output = self.app.post('/admin/hook_token')
  169. self.assertEqual(output.status_code, 302)
  170. user = tests.FakeUser()
  171. with tests.user_set(pagure.APP, user):
  172. output = self.app.post('/admin/hook_token', follow_redirects=True)
  173. self.assertEqual(output.status_code, 404)
  174. self.assertIn(
  175. '</button>\n Access restricted',
  176. output.data)
  177. user.username = 'foo'
  178. with tests.user_set(pagure.APP, user):
  179. output = self.app.post('/admin/hook_token', follow_redirects=True)
  180. self.assertEqual(output.status_code, 200)
  181. self.assertIn(
  182. '</button>\n Access restricted',
  183. output.data)
  184. user = tests.FakeUser(
  185. username='pingou',
  186. groups=pagure.APP.config['ADMIN_GROUP'])
  187. with tests.user_set(pagure.APP, user):
  188. output = self.app.post('/admin/hook_token', follow_redirects=True)
  189. self.assertEqual(output.status_code, 200)
  190. self.assertTrue('<h2>Admin section</h2>' in output.data)
  191. self.assertTrue('Re-generate gitolite ACLs file' in output.data)
  192. self.assertTrue(
  193. 'Re-generate user ssh key files' in output.data)
  194. self.assertTrue(
  195. 'Re-generate hook-token for every projects' in output.data)
  196. csrf_token = output.data.split(
  197. 'name="csrf_token" type="hidden" value="')[1].split('">')[0]
  198. data = {'csrf_token': csrf_token}
  199. output = self.app.post(
  200. '/admin/hook_token', data=data, follow_redirects=True)
  201. self.assertEqual(output.status_code, 200)
  202. self.assertTrue('<h2>Admin section</h2>' in output.data)
  203. self.assertTrue('Re-generate gitolite ACLs file' in output.data)
  204. self.assertTrue(
  205. 'Re-generate user ssh key files' in output.data)
  206. self.assertTrue(
  207. 'Re-generate hook-token for every projects' in output.data)
  208. self.assertTrue(
  209. '</button>\n Hook token all re-generated'
  210. in output.data)
  211. if __name__ == '__main__':
  212. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskAdmintests)
  213. unittest.TextTestRunner(verbosity=2).run(SUITE)