Debian Codegraph @

tests/test_aiohttp_autodetect.py

raw symbols SCIP
1 import importlib
2 import sys
3 import unittest
4 from unittest import mock
5
6 import httpx
7 import pytest
8
9
10 class TestMakeDefaultAsyncClientWithoutAiohttp(unittest.TestCase):
11 """Tests for _make_default_async_client when httpx_aiohttp is NOT installed."""
12
13 def test_returns_httpx_async_client(self) -> None:
14 """When httpx_aiohttp is not installed, returns plain httpx.AsyncClient."""
15 with mock.patch.dict(sys.modules, {"httpx_aiohttp": None}):
16 from auth0.management.client import _make_default_async_client
17
18 client = _make_default_async_client(timeout=60, follow_redirects=True)
19 self.assertIsInstance(client, httpx.AsyncClient)
20 self.assertEqual(client.timeout.read, 60)
21 self.assertTrue(client.follow_redirects)
22
23 def test_follow_redirects_none(self) -> None:
24 """When follow_redirects is None, omits it from httpx.AsyncClient."""
25 with mock.patch.dict(sys.modules, {"httpx_aiohttp": None}):
26 from auth0.management.client import _make_default_async_client
27
28 client = _make_default_async_client(timeout=60, follow_redirects=None)
29 self.assertIsInstance(client, httpx.AsyncClient)
30 self.assertFalse(client.follow_redirects)
31
32 def test_explicit_httpx_client_bypasses_autodetect(self) -> None:
33 """When user passes httpx_client explicitly, _make_default_async_client is not called."""
34
35 explicit_client = httpx.AsyncClient(timeout=120)
36 with mock.patch("auth0.management.client._make_default_async_client") as mock_make:
37 # Replicate the generated conditional: httpx_client if httpx_client is not None else _make_default_async_client(...)
38 result = explicit_client if explicit_client is not None else mock_make(timeout=60, follow_redirects=True)
39 mock_make.assert_not_called()
40 self.assertIs(result, explicit_client)
41
42
43 @pytest.mark.aiohttp
44 class TestMakeDefaultAsyncClientWithAiohttp(unittest.TestCase):
45 """Tests for _make_default_async_client when httpx_aiohttp IS installed."""
46
47 def test_returns_aiohttp_client(self) -> None:
48 """When httpx_aiohttp is installed, returns HttpxAiohttpClient."""
49 import httpx_aiohttp # type: ignore[import-not-found]
50
51 from auth0.management.client import _make_default_async_client
52
53 client = _make_default_async_client(timeout=60, follow_redirects=True)
54 self.assertIsInstance(client, httpx_aiohttp.HttpxAiohttpClient)
55 self.assertEqual(client.timeout.read, 60)
56 self.assertTrue(client.follow_redirects)
57
58 def test_follow_redirects_none(self) -> None:
59 """When httpx_aiohttp is installed and follow_redirects is None, omits it."""
60 import httpx_aiohttp # type: ignore[import-not-found]
61
62 from auth0.management.client import _make_default_async_client
63
64 client = _make_default_async_client(timeout=60, follow_redirects=None)
65 self.assertIsInstance(client, httpx_aiohttp.HttpxAiohttpClient)
66 self.assertFalse(client.follow_redirects)
67
68
69 class TestDefaultClientsWithoutAiohttp(unittest.TestCase):
70 """Tests for _default_clients.py convenience classes (no aiohttp)."""
71
72 def test_default_async_httpx_client_defaults(self) -> None:
73 """DefaultAsyncHttpxClient applies SDK defaults."""
74 from auth0.management._default_clients import SDK_DEFAULT_TIMEOUT, DefaultAsyncHttpxClient
75
76 client = DefaultAsyncHttpxClient()
77 self.assertIsInstance(client, httpx.AsyncClient)
78 self.assertEqual(client.timeout.read, SDK_DEFAULT_TIMEOUT)
79 self.assertTrue(client.follow_redirects)
80
81 def test_default_async_httpx_client_overrides(self) -> None:
82 """DefaultAsyncHttpxClient allows overriding defaults."""
83 from auth0.management._default_clients import DefaultAsyncHttpxClient
84
85 client = DefaultAsyncHttpxClient(timeout=30, follow_redirects=False)
86 self.assertEqual(client.timeout.read, 30)
87 self.assertFalse(client.follow_redirects)
88
89 def test_default_aiohttp_client_raises_without_package(self) -> None:
90 """DefaultAioHttpClient raises RuntimeError when httpx_aiohttp not installed."""
91 import auth0.management._default_clients
92
93 with mock.patch.dict(sys.modules, {"httpx_aiohttp": None}):
94 importlib.reload(auth0.management._default_clients)
95
96 with self.assertRaises(RuntimeError) as ctx:
97 auth0.management._default_clients.DefaultAioHttpClient()
98 self.assertIn("pip install auth0-python[aiohttp]", str(ctx.exception))
99
100 importlib.reload(auth0.management._default_clients)
101
102
103 @pytest.mark.aiohttp
104 class TestDefaultClientsWithAiohttp(unittest.TestCase):
105 """Tests for _default_clients.py when httpx_aiohttp IS installed."""
106
107 def test_default_aiohttp_client_defaults(self) -> None:
108 """DefaultAioHttpClient works when httpx_aiohttp is installed."""
109 import httpx_aiohttp # type: ignore[import-not-found]
110
111 from auth0.management._default_clients import SDK_DEFAULT_TIMEOUT, DefaultAioHttpClient
112
113 client = DefaultAioHttpClient()
114 self.assertIsInstance(client, httpx_aiohttp.HttpxAiohttpClient)
115 self.assertEqual(client.timeout.read, SDK_DEFAULT_TIMEOUT)
116 self.assertTrue(client.follow_redirects)