Skip to content

Commit 60403dd

Browse files
committed
unit tests
1 parent eed8a07 commit 60403dd

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:
@@ -303,6 +304,7 @@ async def test_connection_token_caching(
303304
access_token: str,
304305
account_id_callback: Callable,
305306
account_id_url: str,
307+
enable_cache: Callable,
306308
) -> None:
307309
httpx_mock.add_callback(check_credentials_callback, url=auth_url, is_reusable=True)
308310
httpx_mock.add_callback(
@@ -331,9 +333,11 @@ async def test_connection_token_caching(
331333
assert await connection.cursor().execute("select*") == len(
332334
python_query_data
333335
)
334-
ts = TokenSecureStorage(username=user, password=password)
335-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
336+
# Verify token was cached using the new cache system
337+
cached_token = get_cached_token(user, password, account_name)
338+
assert cached_token == access_token, "Invalid token value cached"
336339

340+
_firebolt_cache.clear()
337341
# Do the same, but with use_token_cache=False
338342
with Patcher():
339343
async with await connect(
@@ -350,10 +354,9 @@ async def test_connection_token_caching(
350354
assert await connection.cursor().execute("select*") == len(
351355
python_query_data
352356
)
353-
ts = TokenSecureStorage(username=user, password=password)
354-
assert (
355-
ts.get_cached_token() is None
356-
), "Token is cached even though caching is disabled"
357+
# Verify token was not cached when caching is disabled
358+
cached_token = get_cached_token(user, password, account_name)
359+
assert cached_token is None, "Token is cached even though caching is disabled"
357360

358361

359362
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(
@@ -130,16 +130,19 @@ def test_refresh_with_hooks(
130130
test_username: str,
131131
test_password: str,
132132
test_token: str,
133+
enable_cache: Callable,
133134
) -> None:
134135
"""
135136
When hooks are used, the invalid token, fetched from cache, is refreshed
136137
"""
137138

138-
tss = TokenSecureStorage(test_username, test_password)
139-
tss.cache_token(test_token, 2**32)
139+
cache_token(test_username, test_password, test_token, 2**32)
140+
auth = UsernamePassword(test_username, test_password)
141+
# Simulate what connect() would do
142+
auth.account = None
140143

141144
client = Client(
142-
auth=UsernamePassword(test_username, test_password),
145+
auth=auth,
143146
event_hooks={
144147
"response": [raise_on_4xx_5xx],
145148
},

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:
@@ -272,6 +273,7 @@ def test_connection_token_caching(
272273
access_token: str,
273274
account_id_callback: Callable,
274275
account_id_url: str,
276+
enable_cache: Callable,
275277
) -> None:
276278
httpx_mock.add_callback(check_credentials_callback, url=auth_url, is_reusable=True)
277279
httpx_mock.add_callback(
@@ -294,9 +296,11 @@ def test_connection_token_caching(
294296
api_endpoint=api_endpoint,
295297
) as connection:
296298
assert connection.cursor().execute("select*") == len(python_query_data)
297-
ts = TokenSecureStorage(username=user, password=password)
298-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
299+
# Verify token was cached using the new cache system
300+
cached_token = get_cached_token(user, password, account_name)
301+
assert cached_token == access_token, "Invalid token value cached"
299302

303+
_firebolt_cache.clear()
300304
# Do the same, but with use_token_cache=False
301305
with Patcher():
302306
with connect(
@@ -307,10 +311,9 @@ def test_connection_token_caching(
307311
api_endpoint=api_endpoint,
308312
) as connection:
309313
assert connection.cursor().execute("select*") == len(python_query_data)
310-
ts = TokenSecureStorage(username=user, password=password)
311-
assert (
312-
ts.get_cached_token() is None
313-
), "Token is cached even though caching is disabled"
314+
# Verify token was not cached when caching is disabled
315+
cached_token = get_cached_token(user, password, account_name)
316+
assert cached_token is None, "Token is cached even though caching is disabled"
314317

315318

316319
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(
@@ -82,6 +83,7 @@ def test_rm_token_cache(
8283
account_id_url: Pattern,
8384
account_id_callback: Callable,
8485
access_token: str,
86+
enable_cache: Callable,
8587
) -> None:
8688
"""Credentials, that are passed to rm are processed properly."""
8789
url = "https://url"
@@ -112,9 +114,11 @@ def test_rm_token_cache(
112114
rm = ResourceManager(local_settings)
113115
rm._client.get(url)
114116

115-
ts = TokenSecureStorage(user, password)
116-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
117+
# Verify token was cached using the new cache system
118+
cached_token = get_cached_token(user, password)
119+
assert cached_token == access_token, "Invalid token value cached"
117120

121+
_firebolt_cache.clear()
118122
# Do the same, but with use_token_cache=False
119123
with Patcher():
120124
local_settings = Settings(
@@ -125,10 +129,9 @@ def test_rm_token_cache(
125129
rm = ResourceManager(local_settings)
126130
rm._client.get(url)
127131

128-
ts = TokenSecureStorage(user, password)
129-
assert (
130-
ts.get_cached_token() is None
131-
), "Token is cached even though caching is disabled"
132+
# Verify token was not cached when caching is disabled
133+
cached_token = get_cached_token(user, password)
134+
assert cached_token is None, "Token is cached even though caching is disabled"
132135

133136

134137
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")
@@ -213,6 +213,8 @@ def system_engine_callback_counter(request, **kwargs):
213213
else:
214214
assert system_engine_call_counter != 1, "System engine URL was cached"
215215

216+
_firebolt_cache.clear()
217+
216218

217219
async def test_connect_engine_failed(
218220
db_name: str,
@@ -307,6 +309,7 @@ async def test_connection_token_caching(
307309
python_query_data: List[List[ColType]],
308310
mock_connection_flow: Callable,
309311
mock_query: Callable,
312+
enable_cache: Callable,
310313
) -> None:
311314
mock_connection_flow()
312315
mock_query()
@@ -323,9 +326,11 @@ async def test_connection_token_caching(
323326
assert await connection.cursor().execute("select*") == len(
324327
python_query_data
325328
)
326-
ts = TokenSecureStorage(username=client_id, password=client_secret)
327-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
329+
# Verify token was cached using the new cache system
330+
cached_token = get_cached_token(client_id, client_secret, account_name)
331+
assert cached_token == access_token, "Invalid token value cached"
328332

333+
_firebolt_cache.clear()
329334
with Patcher():
330335
async with await connect(
331336
database=db_name,
@@ -337,9 +342,11 @@ async def test_connection_token_caching(
337342
assert await connection.cursor().execute("select*") == len(
338343
python_query_data
339344
)
340-
ts = TokenSecureStorage(username=client_id, password=client_secret)
341-
assert ts.get_cached_token() == access_token, "Invalid token value cached"
345+
# Verify token was cached using the new cache system (second check)
346+
cached_token = get_cached_token(client_id, client_secret, account_name)
347+
assert cached_token == access_token, "Invalid token value cached"
342348

349+
_firebolt_cache.clear()
343350
# Do the same, but with use_token_cache=False
344351
with Patcher():
345352
async with await connect(
@@ -352,10 +359,9 @@ async def test_connection_token_caching(
352359
assert await connection.cursor().execute("select*") == len(
353360
python_query_data
354361
)
355-
ts = TokenSecureStorage(username=client_id, password=client_secret)
356-
assert (
357-
ts.get_cached_token() is None
358-
), "Token is cached even though caching is disabled"
362+
# Verify token was not cached when caching is disabled
363+
cached_token = get_cached_token(client_id, client_secret, account_name)
364+
assert cached_token is None, "Token is cached even though caching is disabled"
359365

360366

361367
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, is_reusable=True)
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)