Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix check connection. (#99)
Browse files Browse the repository at this point in the history
* Fix check_connection().

* git push force and friday

* Bump patch.

Co-authored-by: tehAgitto <[email protected]>
  • Loading branch information
danielui and kagrski authored Jan 20, 2023
1 parent 1886999 commit 98f71ca
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "vmngclient"
version = "0.5.1"
version = "0.5.2"
description = "Universal vManage API"
authors = ["kagorski <[email protected]>"]
readme = "README.md"
Expand Down
9 changes: 1 addition & 8 deletions vmngclient/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down
13 changes: 2 additions & 11 deletions vmngclient/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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()

0 comments on commit 98f71ca

Please sign in to comment.