From 98f71caedbfd62ab28779e56e06d3ab2a94ce754 Mon Sep 17 00:00:00 2001 From: danielui <58788881+danielui@users.noreply.github.com> Date: Fri, 20 Jan 2023 15:36:06 +0100 Subject: [PATCH] Fix check connection. (#99) * Fix check_connection(). * git push force and friday * Bump patch. Co-authored-by: tehAgitto --- pyproject.toml | 2 +- vmngclient/session.py | 9 +-------- vmngclient/tests/test_session.py | 13 ++----------- 3 files changed, 4 insertions(+), 20 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9e085190c..17a18e4f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "vmngclient" -version = "0.5.1" +version = "0.5.2" description = "Universal vManage API" authors = ["kagorski "] readme = "README.md" diff --git a/vmngclient/session.py b/vmngclient/session.py index 7ff8c1844..4cafd0def 100644 --- a/vmngclient/session.py +++ b/vmngclient/session.py @@ -138,16 +138,12 @@ def __init__( self.base_url = self.__create_base_url() self.username = username self.password = password - self.port = port self.subdomain = subdomain self.session_type = SessionType.NOT_DEFINED self.server_name = None self.logger = logging.getLogger(__name__) - if not self.check_vmanage_server_connection(): - raise ConnectionError("Vmanage server is not available") - super(vManageSession, self).__init__() self.__prepare_session(verify, auth) @@ -275,10 +271,7 @@ def __prepare_session(self, verify: bool, auth: Optional[AuthBase]) -> None: def check_vmanage_server_connection(self) -> bool: try: - url = str(self.base_url).replace("https", "http") - if self.port: - url = url.replace(str(f":{self.port}"), "") - head(url, timeout=2) + head(self.base_url, timeout=15, verify=False) except ConnectionError: return False else: diff --git a/vmngclient/tests/test_session.py b/vmngclient/tests/test_session.py index 1a0be645a..d2bc23eab 100644 --- a/vmngclient/tests/test_session.py +++ b/vmngclient/tests/test_session.py @@ -3,7 +3,6 @@ from unittest.mock import patch from parameterized import parameterized # type: ignore -from requests.exceptions import ConnectionError from vmngclient.session import vManageSession @@ -30,7 +29,6 @@ def test_session_str(self): def test_base_url(self, port: Optional[int], base_url: str): # Arrange, Act session = vManageSession(self.url, self.username, self.password, port=port) - # Assert self.assertEqual(session.base_url, base_url) @@ -98,9 +96,9 @@ def test_get_full_url(self, port: Optional[int], url: str, full_url: str): self.assertEqual(session.get_full_url(url), full_url) @patch("vmngclient.session.head") - def test_check_vmanage_server_with_port(self, mock_requests): + def test_check_vmanage_server_with_port(self, mock_head): # Arrange, Act - mock_requests.return_value = None + mock_head.return_value = None session = vManageSession("domain.com", "user1", "$password", port=111) answer = session.check_vmanage_server_connection() # Assert @@ -115,13 +113,6 @@ def test_check_vmanage_server_no_port(self, mock_requests): # Assert self.assertEqual(answer, True) - @patch("vmngclient.session.head") - def test_check_vmanage_server_connection_error(self, mock_requests): - # Arrange - mock_requests.side_effect = ConnectionError() - # Assert - self.assertRaises(ConnectionError, vManageSession, "domain.com", "user1", "$password") - if __name__ == "__main__": unittest.main()