7
7
import pytest
8
8
from fastapi .exceptions import HTTPException
9
9
from fastapi .security import HTTPBasicCredentials
10
- from fastapi .testclient import TestClient
11
10
12
- from ralph .api import app
13
11
from ralph .api .auth .basic import (
14
12
ServerUsersCredentials ,
15
13
UserCredentials ,
33
31
)
34
32
35
33
36
- client = TestClient (app )
37
-
38
-
39
34
def test_api_auth_basic_model_serveruserscredentials ():
40
35
"""Test api.auth ServerUsersCredentials model."""
41
36
@@ -148,23 +143,28 @@ def test_api_auth_basic_no_credential_file_found(fs, monkeypatch):
148
143
get_basic_auth_user (credentials )
149
144
150
145
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 ):
152
148
"""Whoami route returns a 401 error when no credentials are sent."""
153
- response = client .get ("/whoami" )
149
+ response = await client .get ("/whoami" )
154
150
assert response .status_code == 401
155
151
assert response .headers ["www-authenticate" ] == "Basic"
156
152
assert response .json () == {"detail" : "Invalid authentication credentials" }
157
153
158
154
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 ):
160
157
"""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
+ )
162
161
assert response .status_code == 401
163
162
assert response .headers ["www-authenticate" ] == "Basic"
164
163
assert response .json () == {"detail" : "Invalid authentication credentials" }
165
164
166
165
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 ):
168
168
"""Whoami route returns a 401 error when the username cannot be found."""
169
169
credential_bytes = base64 .b64encode ("john:admin" .encode ("utf-8" ))
170
170
credentials = str (credential_bytes , "utf-8" )
@@ -173,14 +173,17 @@ def test_api_auth_basic_get_whoami_username_not_found(fs):
173
173
auth_file_path = settings .APP_DIR / "auth.json"
174
174
fs .create_file (auth_file_path , contents = STORED_CREDENTIALS )
175
175
176
- response = client .get ("/whoami" , headers = {"Authorization" : f"Basic { credentials } " })
176
+ response = await client .get (
177
+ "/whoami" , headers = {"Authorization" : f"Basic { credentials } " }
178
+ )
177
179
178
180
assert response .status_code == 401
179
181
assert response .headers ["www-authenticate" ] == "Basic"
180
182
assert response .json () == {"detail" : "Invalid authentication credentials" }
181
183
182
184
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 ):
184
187
"""Whoami route returns a 401 error when the password is wrong."""
185
188
credential_bytes = base64 .b64encode ("john:not-admin" .encode ("utf-8" ))
186
189
credentials = str (credential_bytes , "utf-8" )
@@ -189,18 +192,21 @@ def test_api_auth_basic_get_whoami_wrong_password(fs):
189
192
fs .create_file (auth_file_path , contents = STORED_CREDENTIALS )
190
193
get_basic_auth_user .cache_clear ()
191
194
192
- response = client .get ("/whoami" , headers = {"Authorization" : f"Basic { credentials } " })
195
+ response = await client .get (
196
+ "/whoami" , headers = {"Authorization" : f"Basic { credentials } " }
197
+ )
193
198
194
199
assert response .status_code == 401
195
200
assert response .json () == {"detail" : "Invalid authentication credentials" }
196
201
197
202
203
+ @pytest .mark .anyio
198
204
@pytest .mark .parametrize (
199
205
"runserver_auth_backends" ,
200
206
[[AuthBackend .BASIC , AuthBackend .OIDC ], [AuthBackend .BASIC ]],
201
207
)
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
204
210
):
205
211
"""Whoami returns a 200 response when the credentials are correct.
206
212
@@ -215,7 +221,9 @@ def test_api_auth_basic_get_whoami_correct_credentials(
215
221
fs .create_file (auth_file_path , contents = STORED_CREDENTIALS )
216
222
get_basic_auth_user .cache_clear ()
217
223
218
- response = client .get ("/whoami" , headers = {"Authorization" : f"Basic { credentials } " })
224
+ response = await client .get (
225
+ "/whoami" , headers = {"Authorization" : f"Basic { credentials } " }
226
+ )
219
227
220
228
assert response .status_code == 200
221
229
@@ -227,7 +235,8 @@ def test_api_auth_basic_get_whoami_correct_credentials(
227
235
]
228
236
229
237
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 ):
231
240
"""Check for an exception when providing valid credentials when Basic
232
241
authentication is not supported.
233
242
"""
@@ -240,7 +249,9 @@ def test_api_auth_basic_get_whoami_invalid_backend(fs, monkeypatch):
240
249
fs .create_file (auth_file_path , contents = STORED_CREDENTIALS )
241
250
get_basic_auth_user .cache_clear ()
242
251
243
- response = client .get ("/whoami" , headers = {"Authorization" : f"Basic { credentials } " })
252
+ response = await client .get (
253
+ "/whoami" , headers = {"Authorization" : f"Basic { credentials } " }
254
+ )
244
255
245
256
assert response .status_code == 401
246
257
assert response .json () == {"detail" : "Invalid authentication credentials" }
0 commit comments