Skip to content

Commit 1fe8b22

Browse files
committed
use client from fixtures in auth tests
1 parent 9d8d6eb commit 1fe8b22

File tree

3 files changed

+54
-37
lines changed

3 files changed

+54
-37
lines changed

tests/api/auth/test_basic.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import pytest
88
from fastapi.exceptions import HTTPException
99
from fastapi.security import HTTPBasicCredentials
10-
from fastapi.testclient import TestClient
1110

12-
from ralph.api import app
1311
from ralph.api.auth.basic import (
1412
ServerUsersCredentials,
1513
UserCredentials,
@@ -33,9 +31,6 @@
3331
)
3432

3533

36-
client = TestClient(app)
37-
38-
3934
def 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"}

tests/api/auth/test_oidc.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
"""Tests for the api.auth.oidc module."""
22
import pytest
33
import responses
4-
from fastapi.testclient import TestClient
54
from pydantic import parse_obj_as
65

7-
from ralph.api import app
86
from ralph.api.auth.oidc import discover_provider, get_public_keys
97
from ralph.conf import AuthBackend
108
from ralph.models.xapi.base.agents import BaseXapiAgentWithOpenId
119

1210
from tests.fixtures.auth import ISSUER_URI, mock_oidc_user
1311
from tests.helpers import configure_env_for_mock_oidc_auth
1412

15-
client = TestClient(app)
16-
1713

14+
@pytest.mark.anyio
1815
@pytest.mark.parametrize(
1916
"runserver_auth_backends",
2017
[[AuthBackend.BASIC, AuthBackend.OIDC], [AuthBackend.OIDC]],
2118
)
2219
@responses.activate
23-
def test_api_auth_oidc_get_whoami_valid(monkeypatch, runserver_auth_backends):
20+
async def test_api_auth_oidc_get_whoami_valid(
21+
client, monkeypatch, runserver_auth_backends
22+
):
2423
"""Test a valid OpenId Connect authentication."""
2524

2625
configure_env_for_mock_oidc_auth(monkeypatch, runserver_auth_backends)
2726

2827
oidc_token = mock_oidc_user(scopes=["all", "profile/read"])
2928

3029
headers = {"Authorization": f"Bearer {oidc_token}"}
31-
response = client.get(
30+
response = await client.get(
3231
"/whoami",
3332
headers=headers,
3433
)
@@ -39,17 +38,18 @@ def test_api_auth_oidc_get_whoami_valid(monkeypatch, runserver_auth_backends):
3938
assert sorted(response.json()["scopes"]) == ["all", "profile/read"]
4039

4140

41+
@pytest.mark.anyio
4242
@responses.activate
43-
def test_api_auth_oidc_get_whoami_invalid_token(
44-
monkeypatch, mock_discovery_response, mock_oidc_jwks
43+
async def test_api_auth_oidc_get_whoami_invalid_token(
44+
client, monkeypatch, mock_discovery_response, mock_oidc_jwks
4545
):
4646
"""Test API with an invalid audience."""
4747

4848
configure_env_for_mock_oidc_auth(monkeypatch)
4949

5050
mock_oidc_user()
5151

52-
response = client.get(
52+
response = await client.get(
5353
"/whoami",
5454
headers={"Authorization": "Bearer wrong_token"},
5555
)
@@ -59,8 +59,11 @@ def test_api_auth_oidc_get_whoami_invalid_token(
5959
assert response.json() == {"detail": "Could not validate credentials"}
6060

6161

62+
@pytest.mark.anyio
6263
@responses.activate
63-
def test_api_auth_oidc_get_whoami_invalid_discovery(monkeypatch, encoded_token):
64+
async def test_api_auth_oidc_get_whoami_invalid_discovery(
65+
client, monkeypatch, encoded_token
66+
):
6467
"""Test API with an invalid provider discovery."""
6568

6669
configure_env_for_mock_oidc_auth(monkeypatch)
@@ -77,7 +80,7 @@ def test_api_auth_oidc_get_whoami_invalid_discovery(monkeypatch, encoded_token):
7780
status=500,
7881
)
7982

80-
response = client.get(
83+
response = await client.get(
8184
"/whoami",
8285
headers={"Authorization": f"Bearer {encoded_token}"},
8386
)
@@ -87,9 +90,10 @@ def test_api_auth_oidc_get_whoami_invalid_discovery(monkeypatch, encoded_token):
8790
assert response.json() == {"detail": "Could not validate credentials"}
8891

8992

93+
@pytest.mark.anyio
9094
@responses.activate
91-
def test_api_auth_oidc_get_whoami_invalid_keys(
92-
monkeypatch, mock_discovery_response, mock_oidc_jwks, encoded_token
95+
async def test_api_auth_oidc_get_whoami_invalid_keys(
96+
client, monkeypatch, mock_discovery_response, mock_oidc_jwks, encoded_token
9397
):
9498
"""Test API with an invalid request for keys."""
9599

@@ -115,7 +119,7 @@ def test_api_auth_oidc_get_whoami_invalid_keys(
115119
status=500,
116120
)
117121

118-
response = client.get(
122+
response = await client.get(
119123
"/whoami",
120124
headers={"Authorization": f"Bearer {encoded_token}"},
121125
)
@@ -125,15 +129,16 @@ def test_api_auth_oidc_get_whoami_invalid_keys(
125129
assert response.json() == {"detail": "Could not validate credentials"}
126130

127131

132+
@pytest.mark.anyio
128133
@responses.activate
129-
def test_api_auth_oidc_get_whoami_invalid_header(monkeypatch):
134+
async def test_api_auth_oidc_get_whoami_invalid_header(client, monkeypatch):
130135
"""Test API with an invalid request header."""
131136

132137
configure_env_for_mock_oidc_auth(monkeypatch)
133138

134139
oidc_token = mock_oidc_user()
135140

136-
response = client.get(
141+
response = await client.get(
137142
"/whoami",
138143
headers={"Authorization": f"Wrong header {oidc_token}"},
139144
)
@@ -143,7 +148,8 @@ def test_api_auth_oidc_get_whoami_invalid_header(monkeypatch):
143148
assert response.json() == {"detail": "Invalid authentication credentials"}
144149

145150

146-
def test_api_auth_oidc_get_whoami_invalid_backend(fs, monkeypatch):
151+
@pytest.mark.anyio
152+
async def test_api_auth_oidc_get_whoami_invalid_backend(client, fs, monkeypatch):
147153
"""Check for an exception when providing valid OIDC credentials while
148154
OIDC authentication is not supported.
149155
"""
@@ -153,7 +159,7 @@ def test_api_auth_oidc_get_whoami_invalid_backend(fs, monkeypatch):
153159
oidc_token = mock_oidc_user(scopes=["all", "profile/read"])
154160

155161
headers = {"Authorization": f"Bearer {oidc_token}"}
156-
response = client.get(
162+
response = await client.get(
157163
"/whoami",
158164
headers=headers,
159165
)

tests/api/test_statements_post.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ async def test_api_statements_post_scopes( # noqa: PLR0913
720720
oidc_token = mock_oidc_user(sub=sub, scopes=scopes)
721721
headers = {"Authorization": f"Bearer {oidc_token}"}
722722

723-
monkeypatch.setenv("RUNSERVER_AUTH_BACKENDS", [AuthBackend.OIDC])
723+
monkeypatch.setenv("RUNSERVER_AUTH_BACKENDS", "oidc")
724724
monkeypatch.setattr(
725725
"ralph.api.auth.settings.RUNSERVER_AUTH_BACKENDS", [AuthBackend.OIDC]
726726
)

0 commit comments

Comments
 (0)