test_svn.py
103 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
r130 | # RhodeCode VCSServer provides access to different vcs backends via network. | |||
r1126 | # Copyright (C) 2014-2023 RhodeCode GmbH | |||
r130 | # | |||
# This program is free software; you can redistribute it and/or modify | ||||
# it under the terms of the GNU General Public License as published by | ||||
# the Free Software Foundation; either version 3 of the License, or | ||||
# (at your option) any later version. | ||||
# | ||||
# 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 General Public License | ||||
# along with this program; if not, write to the Free Software Foundation, | ||||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
import io | ||||
import mock | ||||
import pytest | ||||
import sys | ||||
r1060 | from vcsserver.str_utils import ascii_bytes | |||
r1048 | ||||
r130 | ||||
r1152 | class MockPopen: | |||
r130 | def __init__(self, stderr): | |||
r1048 | self.stdout = io.BytesIO(b'') | |||
r130 | self.stderr = io.BytesIO(stderr) | |||
self.returncode = 1 | ||||
def wait(self): | ||||
pass | ||||
INVALID_CERTIFICATE_STDERR = '\n'.join([ | ||||
'svnrdump: E230001: Unable to connect to a repository at URL url', | ||||
'svnrdump: E230001: Server SSL certificate verification failed: issuer is not trusted', | ||||
]) | ||||
@pytest.mark.parametrize('stderr,expected_reason', [ | ||||
(INVALID_CERTIFICATE_STDERR, 'INVALID_CERTIFICATE'), | ||||
r522 | ('svnrdump: E123456', 'UNKNOWN:svnrdump: E123456'), | |||
r145 | ], ids=['invalid-cert-stderr', 'svnrdump-err-123456']) | |||
r130 | @pytest.mark.xfail(sys.platform == "cygwin", | |||
reason="SVN not packaged for Cygwin") | ||||
def test_import_remote_repository_certificate_error(stderr, expected_reason): | ||||
r1145 | from vcsserver.remote import svn_remote | |||
r739 | factory = mock.Mock() | |||
factory.repo = mock.Mock(return_value=mock.Mock()) | ||||
r130 | ||||
r1145 | remote = svn_remote.SvnRemote(factory) | |||
r130 | remote.is_path_valid_repository = lambda wire, path: True | |||
with mock.patch('subprocess.Popen', | ||||
r1048 | return_value=MockPopen(ascii_bytes(stderr))): | |||
r130 | with pytest.raises(Exception) as excinfo: | |||
remote.import_remote_repository({'path': 'path'}, 'url') | ||||
r1048 | expected_error_args = 'Failed to dump the remote repository from url. Reason:{}'.format(expected_reason) | |||
r130 | ||||
r1048 | assert excinfo.value.args[0] == expected_error_args | |||
r130 | ||||
def test_svn_libraries_can_be_imported(): | ||||
r1145 | import svn.client # noqa | |||
r130 | assert svn.client is not None | |||
r523 | ||||
@pytest.mark.parametrize('example_url, parts', [ | ||||
r1124 | ('http://server.com', ('', '', 'http://server.com')), | |||
('http://user@server.com', ('user', '', 'http://user@server.com')), | ||||
r523 | ('http://user:pass@server.com', ('user', 'pass', 'http://user:pass@server.com')), | |||
r1124 | ('<script>', ('', '', '<script>')), | |||
('http://', ('', '', 'http://')), | ||||
r523 | ]) | |||
def test_username_password_extraction_from_url(example_url, parts): | ||||
r1145 | from vcsserver.remote import svn_remote | |||
r523 | ||||
r739 | factory = mock.Mock() | |||
factory.repo = mock.Mock(return_value=mock.Mock()) | ||||
r1145 | remote = svn_remote.SvnRemote(factory) | |||
r523 | remote.is_path_valid_repository = lambda wire, path: True | |||
assert remote.get_url_and_credentials(example_url) == parts | ||||
r1048 | ||||
@pytest.mark.parametrize('call_url', [ | ||||
b'https://svn.code.sf.net/p/svnbook/source/trunk/', | ||||
b'https://marcink@svn.code.sf.net/p/svnbook/source/trunk/', | ||||
b'https://marcink:qweqwe@svn.code.sf.net/p/svnbook/source/trunk/', | ||||
]) | ||||
def test_check_url(call_url): | ||||
r1145 | from vcsserver.remote import svn_remote | |||
r1048 | factory = mock.Mock() | |||
factory.repo = mock.Mock(return_value=mock.Mock()) | ||||
r1145 | remote = svn_remote.SvnRemote(factory) | |||
r1048 | remote.is_path_valid_repository = lambda wire, path: True | |||
r1124 | assert remote.check_url(call_url, {'dummy': 'config'}) | |||
r1048 | ||||