From 03cc5060a5bf25605696af9e1bc5996bd24a5565 Mon Sep 17 00:00:00 2001 From: Jorge Esteban Quilcate Otoya Date: Thu, 21 Dec 2023 07:07:20 -0500 Subject: [PATCH] fixup! apply suggestions Remote log leftovers, extend http and connection string building, and extend testing --- rohmu/object_storage/azure.py | 19 +++++++++---------- test/object_storage/test_azure.py | 26 +++++++++++++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/rohmu/object_storage/azure.py b/rohmu/object_storage/azure.py index 3e32b4da..f0f3a0c6 100644 --- a/rohmu/object_storage/azure.py +++ b/rohmu/object_storage/azure.py @@ -122,6 +122,12 @@ def conn_string( port: Optional[int], is_secure: bool, ) -> str: + protocol = "https" if is_secure else "http" + conn_str = ( + f"DefaultEndpointsProtocol={protocol};" + f"AccountName={account_name};" + f"AccountKey={account_key};" + ) if not host and not port: try: endpoint_suffix = ENDPOINT_SUFFIXES[azure_cloud] @@ -129,22 +135,15 @@ def conn_string( raise InvalidConfigurationError(f"Unknown azure cloud {repr(azure_cloud)}") conn_str = ( - "DefaultEndpointsProtocol=https;" - f"AccountName={account_name};" - f"AccountKey={account_key};" - f"EndpointSuffix={endpoint_suffix}" + f"{conn_str}" + f"EndpointSuffix={endpoint_suffix};" ) else: - print(host) - print(port) if not host or not port: raise InvalidConfigurationError("Custom host and port must be specified together") - protocol = "https" if is_secure else "http" conn_str = ( - f"DefaultEndpointsProtocol={protocol};" - f"AccountName={account_name};" - f"AccountKey={account_key};" + f"{conn_str}" f"BlobEndpoint={protocol}://{host}:{port}/{account_name};" ) return conn_str diff --git a/test/object_storage/test_azure.py b/test/object_storage/test_azure.py index 2a9f9078..e14be78a 100644 --- a/test/object_storage/test_azure.py +++ b/test/object_storage/test_azure.py @@ -4,7 +4,7 @@ from rohmu.errors import InvalidByteRangeError from tempfile import NamedTemporaryFile from types import ModuleType -from typing import Any, Tuple +from typing import Any, Tuple, Optional from unittest.mock import MagicMock, patch import pytest @@ -105,7 +105,22 @@ def test_get_contents_to_fileobj_raises_error_on_invalid_byte_range(azure_module ) -def test_conn_string_custom_host_port() -> None: +@pytest.mark.parametrize( + "host,port,is_secured,expected", + [ + (None, None, True, + "DefaultEndpointsProtocol=https;AccountName=test_name;AccountKey=test_key;EndpointSuffix=core.windows.net;"), + (None, None, False, + "DefaultEndpointsProtocol=http;AccountName=test_name;AccountKey=test_key;EndpointSuffix=core.windows.net;"), + ("localhost", 10000, True, + "DefaultEndpointsProtocol=https;AccountName=test_name;AccountKey=test_key;" + "BlobEndpoint=https://localhost:10000/test_name;"), + ("localhost", 10000, False, + "DefaultEndpointsProtocol=http;AccountName=test_name;AccountKey=test_key;" + "BlobEndpoint=http://localhost:10000/test_name;"), + ] +) +def test_conn_string(host: Optional[str], port: Optional[int], is_secured: bool, expected: str) -> None: get_blob_client_mock = MagicMock() blob_client = MagicMock(get_blob_client=get_blob_client_mock) service_client = MagicMock(from_connection_string=MagicMock(return_value=blob_client)) @@ -118,9 +133,6 @@ def test_conn_string_custom_host_port() -> None: from rohmu.object_storage.azure import AzureTransfer conn_string = AzureTransfer.conn_string( - account_name="test_name", account_key="test_key", azure_cloud=None, host="localhost", port=10000, is_secure=False + account_name="test_name", account_key="test_key", azure_cloud=None, host=host, port=port, is_secure=is_secured ) - assert ( - "DefaultEndpointsProtocol=http;AccountName=test_name;AccountKey=test_key;" - "BlobEndpoint=http://localhost:10000/test_name;" - ) == conn_string + assert expected == conn_string