Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion azure-kusto-data/tests/aio/test_kusto_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
38 changes: 38 additions & 0 deletions azure-kusto-data/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
54 changes: 1 addition & 53 deletions azure-kusto-data/tests/test_e2e_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import asyncio
import csv
import dataclasses
import json
import os
import platform
Expand All @@ -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


Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions azure-kusto-data/tests/test_kusto_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
Loading