From 3994843bf1e2f3740b47e005beabddbfd62a2488 Mon Sep 17 00:00:00 2001 From: Aasav Badera Date: Thu, 5 Nov 2026 12:50:21 +0530 Subject: [PATCH 1/3] test: replace statusreturner with local HTTP fixtures Share a local redirect server between synchronous and asynchronous client tests, use dummy credentials, and remove live Function App E2E references. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 40145fed-85f7-45a5-814a-2beea3c6a507 --- .../tests/aio/test_kusto_client.py | 16 +++++- azure-kusto-data/tests/conftest.py | 38 +++++++++++++ azure-kusto-data/tests/test_e2e_data.py | 54 +------------------ azure-kusto-data/tests/test_kusto_client.py | 11 ++++ 4 files changed, 65 insertions(+), 54 deletions(-) diff --git a/azure-kusto-data/tests/aio/test_kusto_client.py b/azure-kusto-data/tests/aio/test_kusto_client.py index 1bf7fc6d7..2c928bff5 100644 --- a/azure-kusto-data/tests/aio/test_kusto_client.py +++ b/azure-kusto-data/tests/aio/test_kusto_client.py @@ -4,11 +4,13 @@ from unittest.mock import patch import pytest +from aiohttp import ClientResponse +from azure.kusto.data import KustoConnectionStringBuilder from azure.kusto.data._cloud_settings import CloudSettings from azure.kusto.data._decorators import aio_documented_by from azure.kusto.data.client_request_properties import ClientRequestProperties -from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError +from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError, KustoServiceError from azure.kusto.data.helpers import dataframe_from_result_table from ..kusto_client_common import KustoClientTestsMixin, mocked_requests_post from ..test_kusto_client import TestKustoClient as KustoClientTestsSync @@ -18,6 +20,18 @@ @aio_documented_by(KustoClientTestsSync) class TestKustoClient(KustoClientTestsMixin): + @aio_documented_by(KustoClientTestsSync.test_no_redirects) + @pytest.mark.asyncio + async def test_no_redirects(self, redirect_server: tuple[str, int, list[str]]) -> None: + cluster, code, requests_seen = redirect_server + kcsb = KustoConnectionStringBuilder.with_aad_user_token_authentication(cluster, "test-token") + async with KustoClient(kcsb) as client: + with pytest.raises(KustoServiceError) as error: + _ = await client.execute("db", "table") + assert isinstance(error.value.http_response, ClientResponse) + assert error.value.http_response.status == code + assert requests_seen == ["/v2/rest/query"] + @staticmethod def _mock_callback(url, **kwargs): body = json.dumps(mocked_requests_post(str(url), **kwargs).json()) diff --git a/azure-kusto-data/tests/conftest.py b/azure-kusto-data/tests/conftest.py index 3c5eb5f16..027f07ad3 100644 --- a/azure-kusto-data/tests/conftest.py +++ b/azure-kusto-data/tests/conftest.py @@ -1,12 +1,50 @@ import asyncio +from collections.abc import Iterator +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from threading import Thread from typing import Tuple import pytest from azure.kusto.data import KustoConnectionStringBuilder +from azure.kusto.data._cloud_settings import CloudSettings from .kusto_client_common import KustoClientTestsMixin +@pytest.fixture(params=[301, 302, 307, 308]) +def redirect_server(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> Iterator[tuple[str, int, list[str]]]: + code: int = request.param + requests_seen: list[str] = [] + + class RedirectHandler(BaseHTTPRequestHandler): + def do_GET(self) -> None: + self._respond() + + def do_POST(self) -> None: + self._respond() + + def _respond(self) -> None: + requests_seen.append(self.path) + self.send_response(200 if self.path == "/redirected" else code) + if self.path != "/redirected": + self.send_header("Location", "/redirected") + self.send_header("Content-Length", "0") + self.end_headers() + + with ThreadingHTTPServer(("127.0.0.1", 0), RedirectHandler) as server: + cluster = f"http://127.0.0.1:{server.server_port}" + monkeypatch.setattr(CloudSettings, "_cloud_cache", {cluster: CloudSettings.DEFAULT_CLOUD}) + monkeypatch.setenv("NO_PROXY", "127.0.0.1") + monkeypatch.setenv("no_proxy", "127.0.0.1") + thread = Thread(target=server.serve_forever, daemon=True) + thread.start() + try: + yield cluster, code, requests_seen + finally: + server.shutdown() + thread.join() + + @pytest.fixture( params=[ "user_password", diff --git a/azure-kusto-data/tests/test_e2e_data.py b/azure-kusto-data/tests/test_e2e_data.py index 2076eec34..d7b55230c 100644 --- a/azure-kusto-data/tests/test_e2e_data.py +++ b/azure-kusto-data/tests/test_e2e_data.py @@ -3,7 +3,6 @@ import asyncio import csv -import dataclasses import json import os import platform @@ -13,15 +12,13 @@ from urllib.parse import urljoin import pytest -from requests import Response from azure.identity import DefaultAzureCredential from azure.kusto.data import KustoClient, KustoConnectionStringBuilder -from azure.kusto.data._cloud_settings import CloudSettings, DEFAULT_DEV_KUSTO_SERVICE_RESOURCE_ID +from azure.kusto.data._cloud_settings import CloudSettings from azure.kusto.data._models import WellKnownDataSet from azure.kusto.data.aio import KustoClient as AsyncKustoClient from azure.kusto.data.env_utils import get_env, set_env, prepare_app_key_auth from azure.kusto.data.exceptions import KustoServiceError -from azure.kusto.data.kusto_trusted_endpoints import MatchRule, well_known_kusto_endpoints from azure.kusto.data.streaming_response import FrameType @@ -337,55 +334,6 @@ def test_cloud_info(self): assert cloud_info is CloudSettings.get_cloud_info_for_cluster(self.engine_cs) assert cloud_info == CloudSettings.get_cloud_info_for_cluster(urljoin(self.engine_cs, "/test1/test2/test3")) - def test_cloud_info_404(self): - pytest.skip("This test is currently wrong - until all cluster are updated to the redirect uri, this test will fail") - cloud_info = CloudSettings.get_cloud_info_for_cluster("https://statusreturner.azurewebsites.net/404/test") - default_dev_cloud = dataclasses.replace(CloudSettings.DEFAULT_CLOUD, kusto_service_resource_id=DEFAULT_DEV_KUSTO_SERVICE_RESOURCE_ID) - assert cloud_info == CloudSettings.DEFAULT_CLOUD or cloud_info == default_dev_cloud - - @pytest.mark.parametrize("code", [301, 302, 307, 308]) - def test_no_redirects_fail_in_cloud(self, code): - pytest.skip("This test is currently not supported as it relies on the URI path witch we now ignore") - with KustoClient( - KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/nocloud", self.__class__.cred()) - ) as client: - with pytest.raises(KustoServiceError) as ex: - client.execute("db", "table") - assert ex.value.http_response.status_code == code - - @pytest.mark.parametrize("code", [301, 302, 307, 308]) - def test_no_redirects_fail_in_client(self, code): - well_known_kusto_endpoints.add_trusted_hosts([MatchRule("statusreturner.azurewebsites.net", False)], False) - with KustoClient( - KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/segment", self.__class__.cred()) - ) as client: - with pytest.raises(KustoServiceError) as ex: - client.execute("db", "table") - assert isinstance(ex.value.http_response, Response) - assert ex.value.http_response.status_code == code - - @pytest.mark.asyncio - @pytest.mark.parametrize("code", [301, 302, 307, 308]) - async def test_no_redirects_fail_in_cloud_async(self, code): - pytest.skip("This test is currently not supported as it relies on the URI path witch we now ignore") - async with AsyncKustoClient( - KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/nocloud", self.__class__.async_cred()) - ) as client: - with pytest.raises(KustoServiceError) as ex: - await client.execute("db", "table") - assert ex.value.http_response.status_code == code - - @pytest.mark.asyncio - @pytest.mark.parametrize("code", [301, 302, 307, 308]) - async def test_no_redirects_fail_in_client_async(self, code): - well_known_kusto_endpoints.add_trusted_hosts([MatchRule("statusreturner.azurewebsites.net", False)], False) - async with AsyncKustoClient( - KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/segment", self.__class__.async_cred()) - ) as client: - with pytest.raises(KustoServiceError) as ex: - await client.execute("db", "table") - assert ex.value.http_response.status == code - def test_semantic_error(self): with self.get_client() as client: with pytest.raises(KustoServiceError) as ex: diff --git a/azure-kusto-data/tests/test_kusto_client.py b/azure-kusto-data/tests/test_kusto_client.py index 72a6a706a..2997e916b 100644 --- a/azure-kusto-data/tests/test_kusto_client.py +++ b/azure-kusto-data/tests/test_kusto_client.py @@ -5,6 +5,7 @@ import pandas import pytest +from requests import Response from azure.kusto.data import ClientRequestProperties, KustoClient, KustoConnectionStringBuilder from azure.kusto.data._cloud_settings import CloudSettings @@ -22,6 +23,16 @@ def method(request): class TestKustoClient(KustoClientTestsMixin): """Tests class for KustoClient API""" + def test_no_redirects(self, redirect_server: tuple[str, int, list[str]]) -> None: + cluster, code, requests_seen = redirect_server + kcsb = KustoConnectionStringBuilder.with_aad_user_token_authentication(cluster, "test-token") + with KustoClient(kcsb) as client: + with pytest.raises(KustoServiceError) as error: + _ = client.execute("db", "table") + assert isinstance(error.value.http_response, Response) + assert error.value.http_response.status_code == code + assert requests_seen == ["/v2/rest/query"] + def test_throws_on_close(self): """Test query V2.""" with KustoClient(self.HOST) as client: From a585dfa4dfe231f7742eced5ce2700d2277b26d2 Mon Sep 17 00:00:00 2001 From: Aasav Badera Date: Mon, 16 Nov 2026 12:30:31 +0530 Subject: [PATCH 2/3] Add poll_interval to server thread in tests Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- azure-kusto-data/tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-kusto-data/tests/conftest.py b/azure-kusto-data/tests/conftest.py index 027f07ad3..4ae919130 100644 --- a/azure-kusto-data/tests/conftest.py +++ b/azure-kusto-data/tests/conftest.py @@ -36,7 +36,7 @@ def _respond(self) -> None: monkeypatch.setattr(CloudSettings, "_cloud_cache", {cluster: CloudSettings.DEFAULT_CLOUD}) monkeypatch.setenv("NO_PROXY", "127.0.0.1") monkeypatch.setenv("no_proxy", "127.0.0.1") - thread = Thread(target=server.serve_forever, daemon=True) + thread = Thread(target=server.serve_forever, kwargs={"poll_interval": 0.05}, daemon=True) thread.start() try: yield cluster, code, requests_seen From 5be063d4e7ca342a1899b4740674553896004b02 Mon Sep 17 00:00:00 2001 From: Aasav Badera Date: Mon, 16 Nov 2026 15:51:43 +0530 Subject: [PATCH 3/3] test: isolate cloud cache and mock unauthorized authentication Restore cache state between tests and make unauthorized authentication deterministic without external HTTP. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: da942e81-4999-4a78-94ff-6703369ade85 --- .../tests/aio/test_kusto_client.py | 5 +- azure-kusto-data/tests/conftest.py | 9 +- azure-kusto-data/tests/test_cloud_settings.py | 17 +--- azure-kusto-data/tests/test_kusto_client.py | 5 +- azure-kusto-data/tests/test_security.py | 83 +++++++++++-------- 5 files changed, 64 insertions(+), 55 deletions(-) diff --git a/azure-kusto-data/tests/aio/test_kusto_client.py b/azure-kusto-data/tests/aio/test_kusto_client.py index 2c928bff5..dab6ab582 100644 --- a/azure-kusto-data/tests/aio/test_kusto_client.py +++ b/azure-kusto-data/tests/aio/test_kusto_client.py @@ -7,7 +7,6 @@ from aiohttp import ClientResponse from azure.kusto.data import KustoConnectionStringBuilder -from azure.kusto.data._cloud_settings import CloudSettings from azure.kusto.data._decorators import aio_documented_by from azure.kusto.data.client_request_properties import ClientRequestProperties from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError, KustoServiceError @@ -175,7 +174,7 @@ async def test_request_id(self): @aio_documented_by(KustoClientTestsSync.test_proxy_token_providers) @pytest.mark.asyncio - async def test_proxy_token_providers(self, proxy_kcsb): + async def test_proxy_token_providers(self, proxy_kcsb, isolated_cloud_cache): """Test query V2.""" proxy = "https://my_proxy.sample" kcsb, auth_supports_proxy = proxy_kcsb @@ -190,7 +189,7 @@ async def test_proxy_token_providers(self, proxy_kcsb): assert client._aad_helper.token_provider._proxy_dict == expected_dict - CloudSettings._cloud_cache.clear() + isolated_cloud_cache.clear() with patch("requests.get", side_effect=mocked_requests_post) as mock_get: client._aad_helper.token_provider._init_resources() diff --git a/azure-kusto-data/tests/conftest.py b/azure-kusto-data/tests/conftest.py index 4ae919130..23cf54f93 100644 --- a/azure-kusto-data/tests/conftest.py +++ b/azure-kusto-data/tests/conftest.py @@ -6,11 +6,18 @@ import pytest from azure.kusto.data import KustoConnectionStringBuilder -from azure.kusto.data._cloud_settings import CloudSettings +from azure.kusto.data._cloud_settings import CloudInfo, CloudSettings from .kusto_client_common import KustoClientTestsMixin +@pytest.fixture +def isolated_cloud_cache(monkeypatch: pytest.MonkeyPatch) -> dict[str, CloudInfo]: + cache: dict[str, CloudInfo] = {} + monkeypatch.setattr(CloudSettings, "_cloud_cache", cache) + return cache + + @pytest.fixture(params=[301, 302, 307, 308]) def redirect_server(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> Iterator[tuple[str, int, list[str]]]: code: int = request.param diff --git a/azure-kusto-data/tests/test_cloud_settings.py b/azure-kusto-data/tests/test_cloud_settings.py index dc98cb8a6..231ae7da9 100644 --- a/azure-kusto-data/tests/test_cloud_settings.py +++ b/azure-kusto-data/tests/test_cloud_settings.py @@ -1,21 +1,8 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License -import pytest - from azure.kusto.data._cloud_settings import CloudSettings, CloudInfo -@pytest.fixture -def clear_cache(): - """Fixture to clear the CloudSettings cache before each test""" - with CloudSettings._cloud_cache_lock: - CloudSettings._cloud_cache.clear() - yield - # Clean up after test if needed - with CloudSettings._cloud_cache_lock: - CloudSettings._cloud_cache.clear() - - def test_normalize_uri_extracts_authority(): """Test that _normalize_uri extracts only the authority part (schema, host, port) from a URI.""" # Test with various URI formats @@ -32,7 +19,7 @@ def test_normalize_uri_extracts_authority(): assert CloudSettings._normalize_uri(input_uri) == expected_authority -def test_cloud_info_cached_by_authority(clear_cache): +def test_cloud_info_cached_by_authority(isolated_cloud_cache): """Test that CloudInfo is cached by authority part of the URI (schema, host, port).""" # Create a test CloudInfo object test_cloud_info = CloudInfo( @@ -67,7 +54,7 @@ def test_cloud_info_cached_by_authority(clear_cache): assert retrieved_info is test_cloud_info -def test_cloud_info_cached_with_port(clear_cache): +def test_cloud_info_cached_with_port(isolated_cloud_cache): """Test that URIs with ports are cached separately from those without.""" # Create two different CloudInfo objects cloud_info_default = CloudInfo( diff --git a/azure-kusto-data/tests/test_kusto_client.py b/azure-kusto-data/tests/test_kusto_client.py index 2997e916b..750db46e8 100644 --- a/azure-kusto-data/tests/test_kusto_client.py +++ b/azure-kusto-data/tests/test_kusto_client.py @@ -8,7 +8,6 @@ from requests import Response from azure.kusto.data import ClientRequestProperties, KustoClient, KustoConnectionStringBuilder -from azure.kusto.data._cloud_settings import CloudSettings from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError, KustoServiceError from azure.kusto.data.helpers import dataframe_from_result_table from azure.kusto.data.response import KustoStreamingResponseDataSet @@ -193,7 +192,7 @@ def test_custom_request_id(self, mock_post, method): self._assert_client_request_id(mock_post.call_args[-1], value=request_id) @patch("requests.Session.get", side_effect=mocked_requests_post) - def test_proxy_token_providers(self, mock_get, proxy_kcsb): + def test_proxy_token_providers(self, mock_get, proxy_kcsb, isolated_cloud_cache): """Test query V2.""" proxy = "https://my_proxy.sample" kcsb, auth_supports_proxy = proxy_kcsb @@ -209,7 +208,7 @@ def test_proxy_token_providers(self, mock_get, proxy_kcsb): assert client._aad_helper.token_provider._proxy_dict == expected_dict assert client._session.proxies == expected_dict - CloudSettings._cloud_cache.clear() + isolated_cloud_cache.clear() client._aad_helper.token_provider._init_resources() diff --git a/azure-kusto-data/tests/test_security.py b/azure-kusto-data/tests/test_security.py index fb9fdd930..4dc47fed3 100644 --- a/azure-kusto-data/tests/test_security.py +++ b/azure-kusto-data/tests/test_security.py @@ -1,8 +1,10 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License +from unittest.mock import patch + import pytest -from azure.kusto.data._cloud_settings import CloudSettings +from azure.kusto.data._cloud_settings import CloudInfo, CloudSettings from azure.kusto.data._token_providers import CallbackTokenProvider, MsiTokenProvider, UserPassTokenProvider from azure.kusto.data import KustoConnectionStringBuilder from azure.kusto.data.exceptions import KustoAuthenticationError @@ -11,26 +13,44 @@ KUSTO_TEST_URI = "https://thisclusterdoesnotexist.kusto.windows.net" TEST_INTERACTIVE_AUTH = False # User interaction required, enable this when running test manually -CloudSettings.add_to_cache(KUSTO_TEST_URI, CloudSettings.DEFAULT_CLOUD) -CloudSettings.add_to_cache("https://somecluster.kusto.windows.net", CloudSettings.DEFAULT_CLOUD) + +@pytest.fixture(autouse=True) +def cached_cloud_settings(isolated_cloud_cache: dict[str, CloudInfo]) -> None: + isolated_cloud_cache[KUSTO_TEST_URI] = CloudSettings.DEFAULT_CLOUD + isolated_cloud_cache["https://somecluster.kusto.windows.net"] = CloudSettings.DEFAULT_CLOUD def test_unauthorized_exception(): """Test the exception thrown when authorization fails.""" cluster = "https://somecluster.kusto.windows.net" username = "username@microsoft.com" - kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication(cluster, username, "StrongestPasswordEver", "authorityName") - aad_helper = _AadHelper(kcsb, False) - aad_helper.token_provider._init_resources() - - try: - aad_helper.acquire_authorization_header() - except KustoAuthenticationError as error: - assert error.authentication_method == UserPassTokenProvider.name() - assert error.authority == "https://login.microsoftonline.com/authorityName" - assert error.kusto_cluster == cluster - assert error.kwargs["username"] == username - assert error.kwargs["client_id"] == CloudSettings.DEFAULT_CLOUD.kusto_client_app_id + password = "StrongestPasswordEver" + auth_error = {"error": "invalid_grant", "error_description": "Invalid test credentials"} + with ( + patch("azure.kusto.data._token_providers.PublicClientApplication") as public_client, + patch("requests.sessions.Session.request", side_effect=AssertionError("Unexpected HTTP request")) as http_request, + ): + msal_client = public_client.return_value + msal_client.get_accounts.return_value = [] + msal_client.acquire_token_silent.return_value = None + msal_client.acquire_token_by_username_password.return_value = auth_error + kcsb = KustoConnectionStringBuilder.with_aad_user_password_authentication(cluster, username, password, "authorityName") + aad_helper = _AadHelper(kcsb, False) + aad_helper.token_provider._init_resources() + + with pytest.raises(KustoAuthenticationError) as error: + aad_helper.acquire_authorization_header() + + assert error.value.authentication_method == UserPassTokenProvider.name() + assert error.value.authority == "https://login.microsoftonline.com/authorityName" + assert error.value.kusto_cluster == cluster + assert error.value.kwargs["username"] == username + assert error.value.kwargs["client_id"] == CloudSettings.DEFAULT_CLOUD.kusto_client_app_id + assert auth_error["error_description"] in str(error.value.exception) + msal_client.acquire_token_by_username_password.assert_called_once_with( + username=username, password=password, scopes=[CloudSettings.DEFAULT_CLOUD.kusto_service_resource_id + "/.default"] + ) + http_request.assert_not_called() # TODO: remove this once we can control the timeout @@ -60,23 +80,21 @@ def test_msi_auth(): for h in helpers: h.token_provider._init_resources() - try: + with pytest.raises(KustoAuthenticationError) as error: helpers[0].acquire_authorization_header() - except KustoAuthenticationError as e: - assert e.authentication_method == MsiTokenProvider.name() - assert "client_id" not in e.kwargs - assert "object_id" not in e.kwargs - assert "msi_res_id" not in e.kwargs + assert error.value.authentication_method == MsiTokenProvider.name() + assert "client_id" not in error.value.kwargs + assert "object_id" not in error.value.kwargs + assert "msi_res_id" not in error.value.kwargs - try: + with pytest.raises(KustoAuthenticationError) as error: helpers[1].acquire_authorization_header() - except KustoAuthenticationError as e: - assert e.authentication_method == MsiTokenProvider.name() - assert e.kwargs["client_id"] == client_guid - assert "object_id" not in e.kwargs - assert "msi_res_id" not in e.kwargs - assert str(e.exception).index("client_id") > -1 - assert str(e.exception).index(client_guid) > -1 + assert error.value.authentication_method == MsiTokenProvider.name() + assert error.value.kwargs["client_id"] == client_guid + assert "object_id" not in error.value.kwargs + assert "msi_res_id" not in error.value.kwargs + assert str(error.value.exception).index("client_id") > -1 + assert str(error.value.exception).index(client_guid) > -1 def test_token_provider_auth(): @@ -94,11 +112,10 @@ def test_token_provider_auth(): auth_header = valid_helper.acquire_authorization_header() assert auth_header.index(valid_token_provider()) > -1 - try: + with pytest.raises(KustoAuthenticationError) as error: invalid_helper.acquire_authorization_header() - except KustoAuthenticationError as e: - assert e.authentication_method == CallbackTokenProvider.name() - assert str(e.exception).index(str(type(invalid_token_provider()))) > -1 + assert error.value.authentication_method == CallbackTokenProvider.name() + assert str(error.value.exception).index(str(type(invalid_token_provider()))) > -1 def test_user_app_token_auth():