##// END OF EJS Templates
deps: bumped deps for s3fs
deps: bumped deps for s3fs

File last commit:

r5608:6d33e504 default
r5639:d1746c42 default
Show More
test_client_http.py
122 lines | 3.9 KiB | text/x-python | PythonLexer
/ rhodecode / tests / vcs / test_client_http.py
core: updated copyright to 2024
r5608 # Copyright (C) 2010-2024 RhodeCode GmbH
project: added all source files and assets
r1 #
# 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/
import logging
import mock
import msgpack
import pytest
from rhodecode.lib import vcs
http-backend: catch errors from HTTP calls that are not raising exceptions, but...
r1410 from rhodecode.lib.vcs import client_http, exceptions
project: added all source files and assets
r1
def is_new_connection(logger, level, message):
tests: fixed test suite for celery adoption
r5607 return logger == "requests.packages.urllib3.connectionpool" and message.startswith("Starting new HTTP")
project: added all source files and assets
r1
pytest: use consistent way of creating a fixture by using pytest.fixture()
r3946 @pytest.fixture()
project: added all source files and assets
r1 def stub_session():
"""
Stub of `requests.Session()`.
"""
session = mock.Mock()
http-backend: catch errors from HTTP calls that are not raising exceptions, but...
r1410 post = session.post()
post.content = msgpack.packb({})
post.status_code = 200
session.reset_mock()
return session
pytest: use consistent way of creating a fixture by using pytest.fixture()
r3946 @pytest.fixture()
http-backend: catch errors from HTTP calls that are not raising exceptions, but...
r1410 def stub_fail_session():
"""
Stub of `requests.Session()`.
"""
session = mock.Mock()
post = session.post()
tests: fixed test suite for celery adoption
r5607 post.content = msgpack.packb({"error": "500"})
http-backend: catch errors from HTTP calls that are not raising exceptions, but...
r1410 post.status_code = 500
project: added all source files and assets
r1 session.reset_mock()
return session
pytest: use consistent way of creating a fixture by using pytest.fixture()
r3946 @pytest.fixture()
Martin Bornhold
tests: Add session factory fixture and use it in tests.
r289 def stub_session_factory(stub_session):
"""
Stub of `rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory`.
"""
session_factory = mock.Mock()
session_factory.return_value = stub_session
return session_factory
pytest: use consistent way of creating a fixture by using pytest.fixture()
r3946 @pytest.fixture()
http-backend: catch errors from HTTP calls that are not raising exceptions, but...
r1410 def stub_session_failing_factory(stub_fail_session):
"""
Stub of `rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory`.
"""
session_factory = mock.Mock()
session_factory.return_value = stub_fail_session
return session_factory
def test_uses_persistent_http_connections(caplog, vcsbackend_hg):
repo = vcsbackend_hg.repo
remote_call = repo._remote.branches
with caplog.at_level(logging.INFO):
for x in range(5):
remote_call(normal=True, closed=False)
tests: fixed test suite for celery adoption
r5607 new_connections = [r for r in caplog.record_tuples if is_new_connection(*r)]
http-backend: catch errors from HTTP calls that are not raising exceptions, but...
r1410 assert len(new_connections) <= 1
Martin Bornhold
tests: Add session factory fixture and use it in tests.
r289 def test_repo_maker_uses_session_for_classmethods(stub_session_factory):
tests: fixed test suite for celery adoption
r5607 repo_maker = client_http.RemoteVCSMaker("server_and_port", "endpoint", "test_dummy_scm", stub_session_factory)
project: added all source files and assets
r1 repo_maker.example_call()
tests: fixed test suite for celery adoption
r5607 stub_session_factory().post.assert_called_with("http://server_and_port/endpoint", data=mock.ANY)
project: added all source files and assets
r1
tests: fixed test suite for celery adoption
r5607 def test_repo_maker_uses_session_for_instance_methods(stub_session_factory, config):
repo_maker = client_http.RemoteVCSMaker("server_and_port", "endpoint", "test_dummy_scm", stub_session_factory)
repo = repo_maker("stub_path", "stub_repo_id", config)
project: added all source files and assets
r1 repo.example_call()
tests: fixed test suite for celery adoption
r5607 stub_session_factory().post.assert_called_with("http://server_and_port/endpoint", data=mock.ANY)
project: added all source files and assets
r1
tests: fixed test suite for celery adoption
r5607 @mock.patch("rhodecode.lib.vcs.client_http.ThreadlocalSessionFactory")
@mock.patch("rhodecode.lib.vcs.connection")
def test_connect_passes_in_the_same_session(connection, session_factory_class, stub_session):
Martin Bornhold
tests: Adapt vcs test to check that the session factory returns the same session object.
r288 session_factory = session_factory_class.return_value
session_factory.return_value = stub_session
tests: fixed test suite for celery adoption
r5607 vcs.connect_http("server_and_port")
http-backend: catch errors from HTTP calls that are not raising exceptions, but...
r1410
tests: fixed test suite for celery adoption
r5607 def test_repo_maker_uses_session_that_throws_error(stub_session_failing_factory, config):
dan
file-nodes: added streaming remote attributes for vcsserver....
r3895 repo_maker = client_http.RemoteVCSMaker(
tests: fixed test suite for celery adoption
r5607 "server_and_port", "endpoint", "test_dummy_scm", stub_session_failing_factory
)
repo = repo_maker("stub_path", "stub_repo_id", config)
http-backend: catch errors from HTTP calls that are not raising exceptions, but...
r1410
with pytest.raises(exceptions.HttpVCSCommunicationError):
repo.example_call()