11from json import loads
2+ from re import Pattern , compile
23from typing import Callable , List
34
45import httpx
5- from httpx import Response
6+ from httpx import Request , Response
67from pydantic import SecretStr
78from pyfakefs .fake_filesystem_unittest import Patcher
89from pytest import fixture
910
11+ from firebolt .client .auth import Auth , UsernamePassword
1012from firebolt .common .settings import Settings
1113from firebolt .model .provider import Provider
1214from firebolt .model .region import Region , RegionKey
1315from firebolt .utils .exception import (
16+ AccountNotFoundError ,
1417 DatabaseError ,
1518 DataError ,
1619 Error ,
@@ -51,6 +54,16 @@ def global_fake_fs(request) -> None:
5154 yield
5255
5356
57+ @fixture
58+ def username () -> str :
59+ 60+
61+
62+ @fixture
63+ def password () -> str :
64+ return "*****"
65+
66+
5467@fixture
5568def server () -> str :
5669 return "api.mock.firebolt.io"
@@ -107,16 +120,21 @@ def mock_regions(region_1, region_2) -> List[Region]:
107120
108121
109122@fixture
110- def settings (server , region_1 ) -> Settings :
123+ def settings (server : str , region_1 : str , username : str , password : str ) -> Settings :
111124 return Settings (
112125 server = server ,
113- 114- password = SecretStr ("*****" ),
126+ user = username ,
127+ password = SecretStr (password ),
115128 default_region = region_1 .name ,
116129 account_name = None ,
117130 )
118131
119132
133+ @fixture
134+ def auth (username : str , password : str ) -> Auth :
135+ return UsernamePassword (username , password )
136+
137+
120138@fixture
121139def auth_callback (auth_url : str ) -> Callable :
122140 def do_mock (
@@ -148,30 +166,30 @@ def db_description() -> str:
148166
149167
150168@fixture
151- def account_id_url (settings : Settings ) -> str :
152- if not settings .account_name : # if None or ''
153- return f"https://{ settings .server } { ACCOUNT_URL } "
154- else :
155- return (
156- f"https://{ settings .server } { ACCOUNT_BY_NAME_URL } "
157- f"?account_name={ settings .account_name } "
158- )
169+ def account_id_url (settings : Settings ) -> Pattern :
170+ base = f"https://{ settings .server } { ACCOUNT_BY_NAME_URL } ?account_name="
171+ default_base = f"https://{ settings .server } { ACCOUNT_URL } "
172+ base = base .replace ("/" , "\\ /" ).replace ("?" , "\\ ?" )
173+ default_base = default_base .replace ("/" , "\\ /" ).replace ("?" , "\\ ?" )
174+ return compile (f"(?:{ base } .*|{ default_base } )" )
159175
160176
161177@fixture
162178def account_id_callback (
163- account_id : str , account_id_url : str , settings : Settings
179+ account_id : str ,
180+ settings : Settings ,
164181) -> Callable :
165182 def do_mock (
166- request : httpx . Request = None ,
183+ request : Request ,
167184 ** kwargs ,
168185 ) -> Response :
169- assert request .url == account_id_url
170- if account_id_url .endswith (ACCOUNT_URL ): # account_name shouldn't be specified.
186+ if "account_name" not in request .url .params :
171187 return Response (
172188 status_code = httpx .codes .OK , json = {"account" : {"id" : account_id }}
173189 )
174190 # In this case, an account_name *should* be specified.
191+ if request .url .params ["account_name" ] != settings .account_name :
192+ raise AccountNotFoundError (request .url .params ["account_name" ])
175193 return Response (status_code = httpx .codes .OK , json = {"account_id" : account_id })
176194
177195 return do_mock
@@ -194,7 +212,7 @@ def get_engine_callback(
194212 get_engine_url : str , engine_id : str , settings : Settings
195213) -> Callable :
196214 def do_mock (
197- request : httpx . Request = None ,
215+ request : Request = None ,
198216 ** kwargs ,
199217 ) -> Response :
200218 assert request .url == get_engine_url
@@ -230,7 +248,7 @@ def get_providers_url(settings: Settings, account_id: str, engine_id: str) -> st
230248@fixture
231249def get_providers_callback (get_providers_url : str , provider : Provider ) -> Callable :
232250 def do_mock (
233- request : httpx . Request = None ,
251+ request : Request = None ,
234252 ** kwargs ,
235253 ) -> Response :
236254 assert request .url == get_providers_url
@@ -269,7 +287,7 @@ def database_by_name_url(settings: Settings, account_id: str, db_name: str) -> s
269287@fixture
270288def database_by_name_callback (account_id : str , database_id : str ) -> str :
271289 def do_mock (
272- request : httpx . Request = None ,
290+ request : Request = None ,
273291 ** kwargs ,
274292 ) -> Response :
275293 return Response (
@@ -312,7 +330,7 @@ def db_api_exceptions():
312330
313331@fixture
314332def check_token_callback (access_token : str ) -> Callable :
315- def check_token (request : httpx . Request = None , ** kwargs ) -> Response :
333+ def check_token (request : Request = None , ** kwargs ) -> Response :
316334 prefix = "Bearer "
317335 assert request , "empty request"
318336 assert "authorization" in request .headers , "missing authorization header"
@@ -329,7 +347,7 @@ def check_token(request: httpx.Request = None, **kwargs) -> Response:
329347@fixture
330348def check_credentials_callback (settings : Settings , access_token : str ) -> Callable :
331349 def check_credentials (
332- request : httpx . Request = None ,
350+ request : Request = None ,
333351 ** kwargs ,
334352 ) -> Response :
335353 assert request , "empty request"
0 commit comments