test_pagure_flask_ui_plugins_default_hook.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. import json
  11. import unittest
  12. import shutil
  13. import sys
  14. import os
  15. import flask
  16. import pygit2
  17. from mock import patch, MagicMock
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib
  21. import tests
  22. class PagureFlaskPluginDefaultHooktests(tests.Modeltests):
  23. """ Tests for default_hook plugin of pagure """
  24. def test_plugin_default_ui(self):
  25. """ Test the default hook plugin on/off endpoint. """
  26. tests.create_projects(self.session)
  27. tests.create_projects_git(os.path.join(self.path, 'repos'))
  28. user = tests.FakeUser(username='pingou')
  29. with tests.user_set(self.app.application, user):
  30. output = self.app.get('/test/settings/default')
  31. self.assertEqual(output.status_code, 403)
  32. def test_plugin_default_install(self):
  33. """ Check that the default plugin is correctly installed when a
  34. project is created.
  35. """
  36. task = pagure.lib.new_project(
  37. self.session,
  38. user='pingou',
  39. name='test',
  40. blacklist=[],
  41. allowed_prefix=[],
  42. gitfolder=os.path.join(self.path, 'repos'),
  43. docfolder=os.path.join(self.path, 'docs'),
  44. ticketfolder=os.path.join(self.path, 'tickets'),
  45. requestfolder=os.path.join(self.path, 'requests'),
  46. description=None,
  47. url=None, avatar_email=None,
  48. parent_id=None,
  49. add_readme=False,
  50. userobj=None,
  51. prevent_40_chars=False,
  52. namespace=None
  53. )
  54. self.assertEqual(task.get(),
  55. {'endpoint': 'ui_ns.view_repo',
  56. 'repo': 'test',
  57. 'namespace': None})
  58. self.assertTrue(os.path.exists(os.path.join(
  59. self.path, 'repos', 'test.git', 'hooks', 'post-receive.default')))
  60. self.assertTrue(os.path.exists(os.path.join(
  61. self.path, 'repos', 'test.git', 'hooks', 'post-receive')))
  62. def test_plugin_default_remove(self):
  63. """ Check that the default plugin can be correctly removed if
  64. somehow managed.
  65. """
  66. task = pagure.lib.new_project(
  67. self.session,
  68. user='pingou',
  69. name='test',
  70. blacklist=[],
  71. allowed_prefix=[],
  72. gitfolder=os.path.join(self.path, 'repos'),
  73. docfolder=os.path.join(self.path, 'docs'),
  74. ticketfolder=os.path.join(self.path, 'tickets'),
  75. requestfolder=os.path.join(self.path, 'requests'),
  76. description=None,
  77. url=None, avatar_email=None,
  78. parent_id=None,
  79. add_readme=False,
  80. userobj=None,
  81. prevent_40_chars=False,
  82. namespace=None
  83. )
  84. self.assertEqual(task.get(),
  85. {'endpoint': 'ui_ns.view_repo',
  86. 'repo': 'test',
  87. 'namespace': None})
  88. repo = pagure.lib.get_authorized_project(self.session, 'test')
  89. plugin = pagure.lib.plugins.get_plugin('default')
  90. dbobj = plugin.db_object()
  91. plugin.remove(repo)
  92. self.assertFalse(os.path.exists(os.path.join(
  93. self.path, 'repos', 'test.git', 'hooks', 'post-receive.default')))
  94. self.assertTrue(os.path.exists(os.path.join(
  95. self.path, 'repos', 'test.git', 'hooks', 'post-receive')))
  96. def test_plugin_default_form(self):
  97. """ Check that the default plugin's form.
  98. """
  99. with self._app.test_request_context('/') as ctx:
  100. flask.g.session = self.session
  101. flask.g.fas_user = tests.FakeUser(username='foo')
  102. task = pagure.lib.new_project(
  103. self.session,
  104. user='pingou',
  105. name='test',
  106. blacklist=[],
  107. allowed_prefix=[],
  108. gitfolder=os.path.join(self.path, 'repos'),
  109. docfolder=os.path.join(self.path, 'docs'),
  110. ticketfolder=os.path.join(self.path, 'tickets'),
  111. requestfolder=os.path.join(self.path, 'requests'),
  112. description=None,
  113. url=None, avatar_email=None,
  114. parent_id=None,
  115. add_readme=False,
  116. userobj=None,
  117. prevent_40_chars=False,
  118. namespace=None
  119. )
  120. self.assertEqual(task.get(),
  121. {'endpoint': 'ui_ns.view_repo',
  122. 'repo': 'test',
  123. 'namespace': None})
  124. repo = pagure.lib.get_authorized_project(self.session, 'test')
  125. plugin = pagure.lib.plugins.get_plugin('default')
  126. dbobj = plugin.db_object()
  127. form = plugin.form(obj=dbobj)
  128. self.assertEqual(
  129. str(form.active),
  130. '<input id="active" name="active" type="checkbox" value="y">'
  131. )
  132. if __name__ == '__main__':
  133. unittest.main(verbosity=2)