|
6 | 6 |
|
7 | 7 | import base64 |
8 | 8 | import json |
| 9 | +import logging # allow-direct-logging |
9 | 10 | from unittest.mock import AsyncMock, Mock, patch |
10 | 11 |
|
11 | 12 | import pytest |
|
27 | 28 | ) |
28 | 29 |
|
29 | 30 |
|
| 31 | +@pytest.fixture |
| 32 | +def suppress_auth_errors(caplog): |
| 33 | + """Suppress expected ERROR/WARNING logs for tests that deliberately trigger authentication errors""" |
| 34 | + caplog.set_level(logging.CRITICAL, logger="llama_stack.core.server.auth") |
| 35 | + caplog.set_level(logging.CRITICAL, logger="llama_stack.core.server.auth_providers") |
| 36 | + |
| 37 | + |
30 | 38 | class MockResponse: |
31 | 39 | def __init__(self, status_code, json_data): |
32 | 40 | self.status_code = status_code |
@@ -237,20 +245,20 @@ def test_valid_http_authentication(http_client, valid_api_key): |
237 | 245 |
|
238 | 246 |
|
239 | 247 | @patch("httpx.AsyncClient.post", new=mock_post_failure) |
240 | | -def test_invalid_http_authentication(http_client, invalid_api_key): |
| 248 | +def test_invalid_http_authentication(http_client, invalid_api_key, suppress_auth_errors): |
241 | 249 | response = http_client.get("/test", headers={"Authorization": f"Bearer {invalid_api_key}"}) |
242 | 250 | assert response.status_code == 401 |
243 | 251 | assert "Authentication failed" in response.json()["error"]["message"] |
244 | 252 |
|
245 | 253 |
|
246 | 254 | @patch("httpx.AsyncClient.post", new=mock_post_exception) |
247 | | -def test_http_auth_service_error(http_client, valid_api_key): |
| 255 | +def test_http_auth_service_error(http_client, valid_api_key, suppress_auth_errors): |
248 | 256 | response = http_client.get("/test", headers={"Authorization": f"Bearer {valid_api_key}"}) |
249 | 257 | assert response.status_code == 401 |
250 | 258 | assert "Authentication service error" in response.json()["error"]["message"] |
251 | 259 |
|
252 | 260 |
|
253 | | -def test_http_auth_request_payload(http_client, valid_api_key, mock_auth_endpoint): |
| 261 | +def test_http_auth_request_payload(http_client, valid_api_key, mock_auth_endpoint, suppress_auth_errors): |
254 | 262 | with patch("httpx.AsyncClient.post") as mock_post: |
255 | 263 | mock_response = MockResponse(200, {"message": "Authentication successful"}) |
256 | 264 | mock_post.return_value = mock_response |
@@ -420,7 +428,7 @@ def test_valid_oauth2_authentication(oauth2_client, jwt_token_valid, mock_jwks_u |
420 | 428 |
|
421 | 429 |
|
422 | 430 | @patch("httpx.AsyncClient.get", new=mock_jwks_response) |
423 | | -def test_invalid_oauth2_authentication(oauth2_client, invalid_token): |
| 431 | +def test_invalid_oauth2_authentication(oauth2_client, invalid_token, suppress_auth_errors): |
424 | 432 | response = oauth2_client.get("/test", headers={"Authorization": f"Bearer {invalid_token}"}) |
425 | 433 | assert response.status_code == 401 |
426 | 434 | assert "Invalid JWT token" in response.json()["error"]["message"] |
@@ -465,7 +473,7 @@ def oauth2_client_with_jwks_token(oauth2_app_with_jwks_token): |
465 | 473 |
|
466 | 474 |
|
467 | 475 | @patch("httpx.AsyncClient.get", new=mock_auth_jwks_response) |
468 | | -def test_oauth2_with_jwks_token_expected(oauth2_client, jwt_token_valid): |
| 476 | +def test_oauth2_with_jwks_token_expected(oauth2_client, jwt_token_valid, suppress_auth_errors): |
469 | 477 | response = oauth2_client.get("/test", headers={"Authorization": f"Bearer {jwt_token_valid}"}) |
470 | 478 | assert response.status_code == 401 |
471 | 479 |
|
@@ -726,21 +734,21 @@ def test_valid_introspection_authentication(introspection_client, valid_api_key) |
726 | 734 |
|
727 | 735 |
|
728 | 736 | @patch("httpx.AsyncClient.post", new=mock_introspection_inactive) |
729 | | -def test_inactive_introspection_authentication(introspection_client, invalid_api_key): |
| 737 | +def test_inactive_introspection_authentication(introspection_client, invalid_api_key, suppress_auth_errors): |
730 | 738 | response = introspection_client.get("/test", headers={"Authorization": f"Bearer {invalid_api_key}"}) |
731 | 739 | assert response.status_code == 401 |
732 | 740 | assert "Token not active" in response.json()["error"]["message"] |
733 | 741 |
|
734 | 742 |
|
735 | 743 | @patch("httpx.AsyncClient.post", new=mock_introspection_invalid) |
736 | | -def test_invalid_introspection_authentication(introspection_client, invalid_api_key): |
| 744 | +def test_invalid_introspection_authentication(introspection_client, invalid_api_key, suppress_auth_errors): |
737 | 745 | response = introspection_client.get("/test", headers={"Authorization": f"Bearer {invalid_api_key}"}) |
738 | 746 | assert response.status_code == 401 |
739 | 747 | assert "Not JSON" in response.json()["error"]["message"] |
740 | 748 |
|
741 | 749 |
|
742 | 750 | @patch("httpx.AsyncClient.post", new=mock_introspection_failed) |
743 | | -def test_failed_introspection_authentication(introspection_client, invalid_api_key): |
| 751 | +def test_failed_introspection_authentication(introspection_client, invalid_api_key, suppress_auth_errors): |
744 | 752 | response = introspection_client.get("/test", headers={"Authorization": f"Bearer {invalid_api_key}"}) |
745 | 753 | assert response.status_code == 401 |
746 | 754 | assert "Token introspection failed: 500" in response.json()["error"]["message"] |
@@ -957,20 +965,22 @@ def test_valid_kubernetes_auth_authentication(kubernetes_auth_client, valid_toke |
957 | 965 |
|
958 | 966 |
|
959 | 967 | @patch("httpx.AsyncClient.post", new=mock_kubernetes_selfsubjectreview_failure) |
960 | | -def test_invalid_kubernetes_auth_authentication(kubernetes_auth_client, invalid_token): |
| 968 | +def test_invalid_kubernetes_auth_authentication(kubernetes_auth_client, invalid_token, suppress_auth_errors): |
961 | 969 | response = kubernetes_auth_client.get("/test", headers={"Authorization": f"Bearer {invalid_token}"}) |
962 | 970 | assert response.status_code == 401 |
963 | 971 | assert "Invalid token" in response.json()["error"]["message"] |
964 | 972 |
|
965 | 973 |
|
966 | 974 | @patch("httpx.AsyncClient.post", new=mock_kubernetes_selfsubjectreview_http_error) |
967 | | -def test_kubernetes_auth_http_error(kubernetes_auth_client, valid_token): |
| 975 | +def test_kubernetes_auth_http_error(kubernetes_auth_client, valid_token, suppress_auth_errors): |
968 | 976 | response = kubernetes_auth_client.get("/test", headers={"Authorization": f"Bearer {valid_token}"}) |
969 | 977 | assert response.status_code == 401 |
970 | 978 | assert "Token validation failed" in response.json()["error"]["message"] |
971 | 979 |
|
972 | 980 |
|
973 | | -def test_kubernetes_auth_request_payload(kubernetes_auth_client, valid_token, mock_kubernetes_api_server): |
| 981 | +def test_kubernetes_auth_request_payload( |
| 982 | + kubernetes_auth_client, valid_token, mock_kubernetes_api_server, suppress_auth_errors |
| 983 | +): |
974 | 984 | with patch("httpx.AsyncClient.post") as mock_post: |
975 | 985 | mock_response = MockResponse( |
976 | 986 | 200, |
|
0 commit comments