test_server_git.py
174 lines
| 6.3 KiB
| text/x-python
|
PythonLexer
r5088 | # Copyright (C) 2016-2023 RhodeCode GmbH | |||
r2043 | # | |||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
r4654 | import os | |||
r2187 | import mock | |||
r2043 | import pytest | |||
r2186 | from rhodecode.apps.ssh_support.lib.backends.git import GitServer | |||
r3098 | from rhodecode.apps.ssh_support.tests.conftest import plain_dummy_env, plain_dummy_user | |||
r5087 | from rhodecode.lib.ext_json import json | |||
r2043 | ||||
r5325 | ||||
r2043 | class GitServerCreator(object): | |||
root = '/tmp/repo/path/' | ||||
r2187 | git_path = '/usr/local/bin/git' | |||
r2043 | config_data = { | |||
'app:main': { | ||||
r2187 | 'ssh.executable.git': git_path, | |||
r5496 | 'vcs.hooks.protocol.v2': 'celery', | |||
r5607 | 'app.service_api.host': 'http://localhost', | |||
'app.service_api.token': 'secret4', | ||||
'rhodecode.api.url': '/_admin/api', | ||||
r2043 | } | |||
} | ||||
repo_name = 'test_git' | ||||
repo_mode = 'receive-pack' | ||||
r3098 | user = plain_dummy_user() | |||
r2043 | ||||
r5607 | def __init__(self, service_api_url, ini_file): | |||
self.service_api_url = service_api_url | ||||
self.ini_file = ini_file | ||||
r2043 | ||||
def create(self, **kwargs): | ||||
r5607 | self.config_data['app:main']['app.service_api.host'] = self.service_api_url | |||
r2043 | parameters = { | |||
r2187 | 'store': self.root, | |||
r5607 | 'ini_path': self.ini_file, | |||
r2043 | 'user': self.user, | |||
'repo_name': self.repo_name, | ||||
'repo_mode': self.repo_mode, | ||||
'user_permissions': { | ||||
r2187 | self.repo_name: 'repository.admin' | |||
r2043 | }, | |||
r5325 | 'settings': self.config_data['app:main'], | |||
r3098 | 'env': plain_dummy_env() | |||
r2043 | } | |||
parameters.update(kwargs) | ||||
server = GitServer(**parameters) | ||||
return server | ||||
r5607 | @pytest.fixture(scope='module') | |||
def git_server(request, module_app, rhodecode_factory, available_port_factory): | ||||
ini_file = module_app._pyramid_settings['__file__'] | ||||
vcsserver_host = module_app._pyramid_settings['vcs.server'] | ||||
store_dir = os.path.dirname(ini_file) | ||||
# start rhodecode for service API | ||||
rc = rhodecode_factory( | ||||
request, | ||||
store_dir=store_dir, | ||||
port=available_port_factory(), | ||||
overrides=( | ||||
{'handler_console': {'level': 'DEBUG'}}, | ||||
{'app:main': {'vcs.server': vcsserver_host}}, | ||||
{'app:main': {'repo_store.path': store_dir}} | ||||
)) | ||||
service_api_url = f'http://{rc.bind_addr}' | ||||
return GitServerCreator(service_api_url, ini_file) | ||||
r2187 | ||||
r5607 | class TestGitServer: | |||
r2187 | ||||
r2043 | def test_command(self, git_server): | |||
server = git_server.create() | ||||
expected_command = ( | ||||
r2187 | 'cd {root}; {git_path} {repo_mode} \'{root}{repo_name}\''.format( | |||
r2043 | root=git_server.root, git_path=git_server.git_path, | |||
repo_mode=git_server.repo_mode, repo_name=git_server.repo_name) | ||||
) | ||||
r2187 | assert expected_command == server.tunnel.command() | |||
@pytest.mark.parametrize('permissions, action, code', [ | ||||
({}, 'pull', -2), | ||||
({'test_git': 'repository.read'}, 'pull', 0), | ||||
({'test_git': 'repository.read'}, 'push', -2), | ||||
({'test_git': 'repository.write'}, 'push', 0), | ||||
({'test_git': 'repository.admin'}, 'push', 0), | ||||
r2043 | ||||
r2187 | ]) | |||
def test_permission_checks(self, git_server, permissions, action, code): | ||||
server = git_server.create(user_permissions=permissions) | ||||
result = server._check_permissions(action) | ||||
assert result is code | ||||
@pytest.mark.parametrize('permissions, value', [ | ||||
({}, False), | ||||
({'test_git': 'repository.read'}, False), | ||||
({'test_git': 'repository.write'}, True), | ||||
({'test_git': 'repository.admin'}, True), | ||||
]) | ||||
def test_has_write_permissions(self, git_server, permissions, value): | ||||
server = git_server.create(user_permissions=permissions) | ||||
result = server.has_write_perm() | ||||
assert result is value | ||||
def test_run_returns_executes_command(self, git_server): | ||||
r5607 | from rhodecode.apps.ssh_support.lib.backends.git import GitTunnelWrapper | |||
r2043 | server = git_server.create() | |||
r4654 | ||||
os.environ['SSH_CLIENT'] = '127.0.0.1' | ||||
r2187 | with mock.patch.object(GitTunnelWrapper, 'create_hooks_env') as _patch: | |||
_patch.return_value = 0 | ||||
with mock.patch.object(GitTunnelWrapper, 'command', return_value='date'): | ||||
r5607 | exit_code = server.run(tunnel_extras={'config': server.ini_path}) | |||
r2043 | ||||
r2187 | assert exit_code == (0, False) | |||
r2043 | ||||
@pytest.mark.parametrize( | ||||
'repo_mode, action', [ | ||||
['receive-pack', 'push'], | ||||
['upload-pack', 'pull'] | ||||
]) | ||||
def test_update_environment(self, git_server, repo_mode, action): | ||||
server = git_server.create(repo_mode=repo_mode) | ||||
r3094 | store = server.store | |||
r2187 | with mock.patch('os.environ', {'SSH_CLIENT': '10.10.10.10 b'}): | |||
with mock.patch('os.putenv') as putenv_mock: | ||||
server.update_environment(action) | ||||
r2112 | ||||
r2043 | expected_data = { | |||
r2187 | 'username': git_server.user.username, | |||
r2411 | 'user_id': git_server.user.user_id, | |||
r2187 | 'scm': 'git', | |||
'repository': git_server.repo_name, | ||||
'make_lock': None, | ||||
'action': action, | ||||
'ip': '10.10.10.10', | ||||
'locked_by': [None, None], | ||||
r5607 | 'config': git_server.ini_file, | |||
r3094 | 'repo_store': store, | |||
r2187 | 'server_url': None, | |||
'hooks': ['push', 'pull'], | ||||
'is_shadow_repo': False, | ||||
r5325 | 'hooks_module': 'rhodecode.lib.hook_daemon.hook_module', | |||
r2982 | 'check_branch_perms': False, | |||
'detect_force_push': False, | ||||
r4858 | 'user_agent': u'git/ssh-user-agent', | |||
r2187 | 'SSH': True, | |||
'SSH_PERMISSIONS': 'repository.admin', | ||||
r2043 | } | |||
r2112 | args, kwargs = putenv_mock.call_args | |||
assert json.loads(args[1]) == expected_data | ||||