Skip to content

Commit 4b5080e

Browse files
committed
unit tests
1 parent 07b7c83 commit 4b5080e

File tree

13 files changed

+357
-97
lines changed

13 files changed

+357
-97
lines changed

tests/unit/V1/async_db/test_connection.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
from firebolt.async_db.connection import Connection, connect
1313
from firebolt.client.auth import Auth, Token, UsernamePassword
1414
from firebolt.common._types import ColType
15+
from firebolt.utils.cache import _firebolt_cache
1516
from firebolt.utils.exception import (
1617
AccountNotFoundError,
1718
ConfigurationError,
1819
ConnectionClosedError,
1920
FireboltEngineError,
2021
)
21-
from firebolt.utils.token_storage import TokenSecureStorage
2222
from firebolt.utils.urls import ACCOUNT_ENGINE_ID_BY_NAME_URL
23+
from tests.unit.test_cache_helpers import get_cached_token
2324

2425

2526
async def test_closed_connection(connection: Connection) -> None:
@@ -275,6 +276,7 @@ async def test_connection_token_caching(
275276
access_token: str,
276277
account_id_callback: Callable,
277278
account_id_url: str,
279+
enable_cache: Callable,
278280
) -> None:
279281
httpx_mock.add_callback(check_credentials_callback, url=auth_url)
280282
httpx_mock.add_callback(query_callback, url=query_url)
@@ -295,9 +297,11 @@ async def test_connection_token_caching(
295297
assert await connection.cursor().execute("select*") == len(
296298
python_query_data
297299
)
298-
ts = TokenSecureStorage(username=user, password=password)
299-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
300+
# Verify token was cached using the new cache system
301+
cached_token = get_cached_token(user, password, account_name)
302+
assert cached_token == access_token, "Invalid token value cached"
300303

304+
_firebolt_cache.clear()
301305
# Do the same, but with use_token_cache=False
302306
with Patcher():
303307
async with await connect(
@@ -314,10 +318,9 @@ async def test_connection_token_caching(
314318
assert await connection.cursor().execute("select*") == len(
315319
python_query_data
316320
)
317-
ts = TokenSecureStorage(username=user, password=password)
318-
assert (
319-
ts.get_cached_token() is None
320-
), "Token is cached even though caching is disabled"
321+
# Verify token was not cached when caching is disabled
322+
cached_token = get_cached_token(user, password, account_name)
323+
assert cached_token is None, "Token is cached even though caching is disabled"
321324

322325

323326
async def test_connect_with_auth(

tests/unit/V1/client/test_client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
from firebolt.client import ClientV1 as Client
1111
from firebolt.client.auth import Token, UsernamePassword
1212
from firebolt.client.resource_manager_hooks import raise_on_4xx_5xx
13-
from firebolt.utils.token_storage import TokenSecureStorage
1413
from firebolt.utils.urls import AUTH_URL
1514
from firebolt.utils.util import fix_url_schema
15+
from tests.unit.test_cache_helpers import cache_token
1616

1717

1818
def test_client_retry(
@@ -121,16 +121,19 @@ def test_refresh_with_hooks(
121121
test_username: str,
122122
test_password: str,
123123
test_token: str,
124+
enable_cache: Callable,
124125
) -> None:
125126
"""
126127
When hooks are used, the invalid token, fetched from cache, is refreshed
127128
"""
128129

129-
tss = TokenSecureStorage(test_username, test_password)
130-
tss.cache_token(test_token, 2**32)
130+
cache_token(test_username, test_password, test_token, 2**32)
131+
auth = UsernamePassword(test_username, test_password)
132+
# Simulate what connect() would do
133+
auth.account = None
131134

132135
client = Client(
133-
auth=UsernamePassword(test_username, test_password),
136+
auth=auth,
134137
event_hooks={
135138
"response": [raise_on_4xx_5xx],
136139
},

tests/unit/V1/db/test_connection.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
from firebolt.common._types import ColType
1414
from firebolt.db import Connection, connect
1515
from firebolt.db.cursor import CursorV1 as Cursor
16+
from firebolt.utils.cache import _firebolt_cache
1617
from firebolt.utils.exception import (
1718
AccountNotFoundError,
1819
ConfigurationError,
1920
ConnectionClosedError,
2021
FireboltEngineError,
2122
)
22-
from firebolt.utils.token_storage import TokenSecureStorage
2323
from firebolt.utils.urls import ACCOUNT_ENGINE_ID_BY_NAME_URL
24+
from tests.unit.test_cache_helpers import get_cached_token
2425

2526

2627
def test_closed_connection(connection: Connection) -> None:
@@ -248,6 +249,7 @@ def test_connection_token_caching(
248249
access_token: str,
249250
account_id_callback: Callable,
250251
account_id_url: str,
252+
enable_cache: Callable,
251253
) -> None:
252254
httpx_mock.add_callback(check_credentials_callback, url=auth_url)
253255
httpx_mock.add_callback(query_callback, url=query_url)
@@ -262,9 +264,11 @@ def test_connection_token_caching(
262264
api_endpoint=api_endpoint,
263265
) as connection:
264266
assert connection.cursor().execute("select*") == len(python_query_data)
265-
ts = TokenSecureStorage(username=user, password=password)
266-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
267+
# Verify token was cached using the new cache system
268+
cached_token = get_cached_token(user, password, account_name)
269+
assert cached_token == access_token, "Invalid token value cached"
267270

271+
_firebolt_cache.clear()
268272
# Do the same, but with use_token_cache=False
269273
with Patcher():
270274
with connect(
@@ -275,10 +279,9 @@ def test_connection_token_caching(
275279
api_endpoint=api_endpoint,
276280
) as connection:
277281
assert connection.cursor().execute("select*") == len(python_query_data)
278-
ts = TokenSecureStorage(username=user, password=password)
279-
assert (
280-
ts.get_cached_token() is None
281-
), "Token is cached even though caching is disabled"
282+
# Verify token was not cached when caching is disabled
283+
cached_token = get_cached_token(user, password, account_name)
284+
assert cached_token is None, "Token is cached even though caching is disabled"
282285

283286

284287
def test_connect_with_auth(

tests/unit/V1/service/test_resource_manager.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
from firebolt.client.auth import Auth, Token, UsernamePassword
99
from firebolt.common.settings import Settings
1010
from firebolt.service.manager import ResourceManager
11+
from firebolt.utils.cache import _firebolt_cache
1112
from firebolt.utils.exception import AccountNotFoundError
12-
from firebolt.utils.token_storage import TokenSecureStorage
13+
from tests.unit.test_cache_helpers import get_cached_token
1314

1415

1516
def test_rm_credentials(
@@ -70,6 +71,7 @@ def test_rm_token_cache(
7071
account_id_url: Pattern,
7172
account_id_callback: Callable,
7273
access_token: str,
74+
enable_cache: Callable,
7375
) -> None:
7476
"""Credentials, that are passed to rm are processed properly."""
7577
url = "https://url"
@@ -88,9 +90,11 @@ def test_rm_token_cache(
8890
rm = ResourceManager(local_settings)
8991
rm._client.get(url)
9092

91-
ts = TokenSecureStorage(user, password)
92-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
93+
# Verify token was cached using the new cache system
94+
cached_token = get_cached_token(user, password)
95+
assert cached_token == access_token, "Invalid token value cached"
9396

97+
_firebolt_cache.clear()
9498
# Do the same, but with use_token_cache=False
9599
with Patcher():
96100
local_settings = Settings(
@@ -101,10 +105,9 @@ def test_rm_token_cache(
101105
rm = ResourceManager(local_settings)
102106
rm._client.get(url)
103107

104-
ts = TokenSecureStorage(user, password)
105-
assert (
106-
ts.get_cached_token() is None
107-
), "Token is cached even though caching is disabled"
108+
# Verify token was not cached when caching is disabled
109+
cached_token = get_cached_token(user, password)
110+
assert cached_token is None, "Token is cached even though caching is disabled"
108111

109112

110113
def test_rm_invalid_account_name(

tests/unit/async_db/test_connection.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ConnectionClosedError,
1717
FireboltError,
1818
)
19-
from firebolt.utils.token_storage import TokenSecureStorage
19+
from tests.unit.test_cache_helpers import get_cached_token
2020

2121

2222
@mark.skip("__slots__ is broken on Connection class")
@@ -202,6 +202,8 @@ def system_engine_callback_counter(request, **kwargs):
202202
else:
203203
assert system_engine_call_counter != 1, "System engine URL was cached"
204204

205+
_firebolt_cache.clear()
206+
205207

206208
async def test_connect_engine_failed(
207209
db_name: str,
@@ -290,6 +292,7 @@ async def test_connection_token_caching(
290292
python_query_data: List[List[ColType]],
291293
mock_connection_flow: Callable,
292294
mock_query: Callable,
295+
enable_cache: Callable,
293296
) -> None:
294297
mock_connection_flow()
295298
mock_query()
@@ -306,9 +309,11 @@ async def test_connection_token_caching(
306309
assert await connection.cursor().execute("select*") == len(
307310
python_query_data
308311
)
309-
ts = TokenSecureStorage(username=client_id, password=client_secret)
310-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
312+
# Verify token was cached using the new cache system
313+
cached_token = get_cached_token(client_id, client_secret, account_name)
314+
assert cached_token == access_token, "Invalid token value cached"
311315

316+
_firebolt_cache.clear()
312317
with Patcher():
313318
async with await connect(
314319
database=db_name,
@@ -320,9 +325,11 @@ async def test_connection_token_caching(
320325
assert await connection.cursor().execute("select*") == len(
321326
python_query_data
322327
)
323-
ts = TokenSecureStorage(username=client_id, password=client_secret)
324-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
328+
# Verify token was cached using the new cache system (second check)
329+
cached_token = get_cached_token(client_id, client_secret, account_name)
330+
assert cached_token == access_token, "Invalid token value cached"
325331

332+
_firebolt_cache.clear()
326333
# Do the same, but with use_token_cache=False
327334
with Patcher():
328335
async with await connect(
@@ -335,10 +342,9 @@ async def test_connection_token_caching(
335342
assert await connection.cursor().execute("select*") == len(
336343
python_query_data
337344
)
338-
ts = TokenSecureStorage(username=client_id, password=client_secret)
339-
assert (
340-
ts.get_cached_token() is None
341-
), "Token is cached even though caching is disabled"
345+
# Verify token was not cached when caching is disabled
346+
cached_token = get_cached_token(client_id, client_secret, account_name)
347+
assert cached_token is None, "Token is cached even though caching is disabled"
342348

343349

344350
async def test_connect_with_user_agent(

tests/unit/client/auth/test_auth.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from types import MethodType
2-
from unittest.mock import PropertyMock, patch
2+
from typing import Generator
33

44
from httpx import Request, codes
55
from pyfakefs.fake_filesystem_unittest import Patcher
66
from pytest import mark
77
from pytest_httpx import HTTPXMock
88

9-
from firebolt.client.auth import Auth
10-
from firebolt.utils.token_storage import TokenSecureStorage
9+
from firebolt.client.auth import Auth, ClientCredentials
10+
from firebolt.utils.cache import _firebolt_cache
11+
from tests.unit.test_cache_helpers import get_cached_token
1112
from tests.unit.util import execute_generator_requests
1213

1314

@@ -89,6 +90,7 @@ def test_auth_token_storage(
8990
client_id: str,
9091
client_secret: str,
9192
access_token: str,
93+
enable_cache: Generator,
9294
) -> None:
9395
# Mock auth flow
9496
def set_token(token: str) -> callable:
@@ -101,29 +103,28 @@ def inner(self):
101103

102104
url = "https://host"
103105
httpx_mock.add_response(status_code=codes.OK, url=url)
104-
with Patcher(), patch(
105-
"firebolt.client.auth.base.Auth._token_storage",
106-
new_callable=PropertyMock,
107-
return_value=TokenSecureStorage(client_id, client_secret),
108-
):
109-
auth = Auth(use_token_cache=True)
106+
107+
# Test with caching enabled
108+
with Patcher():
109+
auth = ClientCredentials(client_id, client_secret, use_token_cache=True)
110110
# Get token
111111
auth.get_new_token_generator = MethodType(set_token(access_token), auth)
112112
execute_generator_requests(auth.auth_flow(Request("GET", url)))
113113

114-
st = TokenSecureStorage(client_id, client_secret)
115-
assert st.get_cached_token() == access_token, "Invalid token value cached"
114+
# Verify token was cached using the new cache system
115+
cached_token = get_cached_token(client_id, client_secret, None)
116+
assert cached_token == access_token, "Invalid token value cached"
117+
118+
# Clear cache before second test
119+
_firebolt_cache.clear()
116120

117-
with Patcher(), patch(
118-
"firebolt.client.auth.base.Auth._token_storage",
119-
new_callable=PropertyMock,
120-
return_value=TokenSecureStorage(client_id, client_secret),
121-
):
122-
auth = Auth(use_token_cache=False)
121+
# Test with caching disabled
122+
with Patcher():
123+
auth = ClientCredentials(client_id, client_secret, use_token_cache=False)
123124
# Get token
124125
auth.get_new_token_generator = MethodType(set_token(access_token), auth)
125126
execute_generator_requests(auth.auth_flow(Request("GET", url)))
126-
st = TokenSecureStorage(client_id, client_secret)
127-
assert (
128-
st.get_cached_token() is None
129-
), "Token cached even though caching is disabled"
127+
128+
# Verify token was not cached
129+
cached_token = get_cached_token(client_id, client_secret, None)
130+
assert cached_token is None, "Token cached even though caching is disabled"

0 commit comments

Comments
 (0)