77import pytest
88from fastapi .exceptions import HTTPException
99from fastapi .security import HTTPBasicCredentials
10- from fastapi .testclient import TestClient
1110
12- from ralph .api import app
1311from ralph .api .auth .basic import (
1412 ServerUsersCredentials ,
1513 UserCredentials ,
3331)
3432
3533
36- client = TestClient (app )
37-
38-
3934def test_api_auth_basic_model_serveruserscredentials ():
4035 """Test api.auth ServerUsersCredentials model."""
4136
@@ -148,23 +143,28 @@ def test_api_auth_basic_no_credential_file_found(fs, monkeypatch):
148143 get_basic_auth_user (credentials )
149144
150145
151- def test_api_auth_basic_get_whoami_no_credentials ():
146+ @pytest .mark .anyio
147+ async def test_api_auth_basic_get_whoami_no_credentials (client ):
152148 """Whoami route returns a 401 error when no credentials are sent."""
153- response = client .get ("/whoami" )
149+ response = await client .get ("/whoami" )
154150 assert response .status_code == 401
155151 assert response .headers ["www-authenticate" ] == "Basic"
156152 assert response .json () == {"detail" : "Invalid authentication credentials" }
157153
158154
159- def test_api_auth_basic_get_whoami_credentials_encoding_error ():
155+ @pytest .mark .anyio
156+ async def test_api_auth_basic_get_whoami_credentials_encoding_error (client ):
160157 """Whoami route returns a 401 error when the credentials encoding is broken."""
161- response = client .get ("/whoami" , headers = {"Authorization" : "Basic not-base64" })
158+ response = await client .get (
159+ "/whoami" , headers = {"Authorization" : "Basic not-base64" }
160+ )
162161 assert response .status_code == 401
163162 assert response .headers ["www-authenticate" ] == "Basic"
164163 assert response .json () == {"detail" : "Invalid authentication credentials" }
165164
166165
167- def test_api_auth_basic_get_whoami_username_not_found (fs ):
166+ @pytest .mark .anyio
167+ async def test_api_auth_basic_get_whoami_username_not_found (fs , client ):
168168 """Whoami route returns a 401 error when the username cannot be found."""
169169 credential_bytes = base64 .b64encode ("john:admin" .encode ("utf-8" ))
170170 credentials = str (credential_bytes , "utf-8" )
@@ -173,14 +173,17 @@ def test_api_auth_basic_get_whoami_username_not_found(fs):
173173 auth_file_path = settings .APP_DIR / "auth.json"
174174 fs .create_file (auth_file_path , contents = STORED_CREDENTIALS )
175175
176- response = client .get ("/whoami" , headers = {"Authorization" : f"Basic { credentials } " })
176+ response = await client .get (
177+ "/whoami" , headers = {"Authorization" : f"Basic { credentials } " }
178+ )
177179
178180 assert response .status_code == 401
179181 assert response .headers ["www-authenticate" ] == "Basic"
180182 assert response .json () == {"detail" : "Invalid authentication credentials" }
181183
182184
183- def test_api_auth_basic_get_whoami_wrong_password (fs ):
185+ @pytest .mark .anyio
186+ async def test_api_auth_basic_get_whoami_wrong_password (fs , client ):
184187 """Whoami route returns a 401 error when the password is wrong."""
185188 credential_bytes = base64 .b64encode ("john:not-admin" .encode ("utf-8" ))
186189 credentials = str (credential_bytes , "utf-8" )
@@ -189,18 +192,21 @@ def test_api_auth_basic_get_whoami_wrong_password(fs):
189192 fs .create_file (auth_file_path , contents = STORED_CREDENTIALS )
190193 get_basic_auth_user .cache_clear ()
191194
192- response = client .get ("/whoami" , headers = {"Authorization" : f"Basic { credentials } " })
195+ response = await client .get (
196+ "/whoami" , headers = {"Authorization" : f"Basic { credentials } " }
197+ )
193198
194199 assert response .status_code == 401
195200 assert response .json () == {"detail" : "Invalid authentication credentials" }
196201
197202
203+ @pytest .mark .anyio
198204@pytest .mark .parametrize (
199205 "runserver_auth_backends" ,
200206 [[AuthBackend .BASIC , AuthBackend .OIDC ], [AuthBackend .BASIC ]],
201207)
202- def test_api_auth_basic_get_whoami_correct_credentials (
203- fs , monkeypatch , runserver_auth_backends
208+ async def test_api_auth_basic_get_whoami_correct_credentials (
209+ fs , monkeypatch , runserver_auth_backends , client
204210):
205211 """Whoami returns a 200 response when the credentials are correct.
206212
@@ -215,7 +221,9 @@ def test_api_auth_basic_get_whoami_correct_credentials(
215221 fs .create_file (auth_file_path , contents = STORED_CREDENTIALS )
216222 get_basic_auth_user .cache_clear ()
217223
218- response = client .get ("/whoami" , headers = {"Authorization" : f"Basic { credentials } " })
224+ response = await client .get (
225+ "/whoami" , headers = {"Authorization" : f"Basic { credentials } " }
226+ )
219227
220228 assert response .status_code == 200
221229
@@ -227,7 +235,8 @@ def test_api_auth_basic_get_whoami_correct_credentials(
227235 ]
228236
229237
230- def test_api_auth_basic_get_whoami_invalid_backend (fs , monkeypatch ):
238+ @pytest .mark .anyio
239+ async def test_api_auth_basic_get_whoami_invalid_backend (fs , monkeypatch , client ):
231240 """Check for an exception when providing valid credentials when Basic
232241 authentication is not supported.
233242 """
@@ -240,7 +249,9 @@ def test_api_auth_basic_get_whoami_invalid_backend(fs, monkeypatch):
240249 fs .create_file (auth_file_path , contents = STORED_CREDENTIALS )
241250 get_basic_auth_user .cache_clear ()
242251
243- response = client .get ("/whoami" , headers = {"Authorization" : f"Basic { credentials } " })
252+ response = await client .get (
253+ "/whoami" , headers = {"Authorization" : f"Basic { credentials } " }
254+ )
244255
245256 assert response .status_code == 401
246257 assert response .json () == {"detail" : "Invalid authentication credentials" }
0 commit comments