test_pagure_flask_ui_plugins_default_hook.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. repospanner_region=None,
  41. blacklist=[],
  42. allowed_prefix=[],
  43. description=None,
  44. url=None, avatar_email=None,
  45. parent_id=None,
  46. add_readme=False,
  47. userobj=None,
  48. prevent_40_chars=False,
  49. namespace=None
  50. )
  51. self.assertEqual(task.get(),
  52. {'endpoint': 'ui_ns.view_repo',
  53. 'repo': 'test',
  54. 'namespace': None})
  55. self.assertTrue(os.path.exists(os.path.join(
  56. self.path, 'repos', 'test.git', 'hooks', 'post-receive')))
  57. def test_plugin_default_remove(self):
  58. """ Check that the default plugin can be correctly removed if
  59. somehow managed.
  60. """
  61. task = pagure.lib.new_project(
  62. self.session,
  63. user='pingou',
  64. name='test',
  65. repospanner_region=None,
  66. blacklist=[],
  67. allowed_prefix=[],
  68. description=None,
  69. url=None, avatar_email=None,
  70. parent_id=None,
  71. add_readme=False,
  72. userobj=None,
  73. prevent_40_chars=False,
  74. namespace=None
  75. )
  76. self.assertEqual(task.get(),
  77. {'endpoint': 'ui_ns.view_repo',
  78. 'repo': 'test',
  79. 'namespace': None})
  80. repo = pagure.lib.get_authorized_project(self.session, 'test')
  81. plugin = pagure.lib.plugins.get_plugin('default')
  82. dbobj = plugin.db_object()
  83. plugin.remove(repo)
  84. self.assertFalse(os.path.exists(os.path.join(
  85. self.path, 'repos', 'test.git', 'hooks', 'post-receive.default')))
  86. self.assertTrue(os.path.exists(os.path.join(
  87. self.path, 'repos', 'test.git', 'hooks', 'post-receive')))
  88. def test_plugin_default_form(self):
  89. """ Check that the default plugin's form.
  90. """
  91. with self._app.test_request_context('/') as ctx:
  92. flask.g.session = self.session
  93. flask.g.fas_user = tests.FakeUser(username='foo')
  94. task = pagure.lib.new_project(
  95. self.session,
  96. user='pingou',
  97. name='test',
  98. repospanner_region=None,
  99. blacklist=[],
  100. allowed_prefix=[],
  101. description=None,
  102. url=None, avatar_email=None,
  103. parent_id=None,
  104. add_readme=False,
  105. userobj=None,
  106. prevent_40_chars=False,
  107. namespace=None
  108. )
  109. self.assertEqual(task.get(),
  110. {'endpoint': 'ui_ns.view_repo',
  111. 'repo': 'test',
  112. 'namespace': None})
  113. repo = pagure.lib.get_authorized_project(self.session, 'test')
  114. plugin = pagure.lib.plugins.get_plugin('default')
  115. dbobj = plugin.db_object()
  116. form = plugin.form(obj=dbobj)
  117. self.assertEqual(
  118. str(form.active),
  119. '<input id="active" name="active" type="checkbox" value="y">'
  120. )
  121. if __name__ == '__main__':
  122. unittest.main(verbosity=2)