|
| 1 | +import pytest |
| 2 | +from pydantic import ValidationError |
| 3 | +from fastapi import Depends |
| 4 | + |
| 5 | +from fastapi_mcp.types import ( |
| 6 | + OAuthMetadata, |
| 7 | + AuthConfig, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +class TestOAuthMetadata: |
| 12 | + def test_non_empty_lists_validation(self): |
| 13 | + for field in [ |
| 14 | + "scopes_supported", |
| 15 | + "response_types_supported", |
| 16 | + "grant_types_supported", |
| 17 | + "token_endpoint_auth_methods_supported", |
| 18 | + "code_challenge_methods_supported", |
| 19 | + ]: |
| 20 | + with pytest.raises(ValidationError, match=f"{field} cannot be empty"): |
| 21 | + OAuthMetadata( |
| 22 | + issuer="https://example.com", |
| 23 | + authorization_endpoint="https://example.com/auth", |
| 24 | + token_endpoint="https://example.com/token", |
| 25 | + **{field: []}, |
| 26 | + ) |
| 27 | + |
| 28 | + def test_authorization_endpoint_required_for_authorization_code(self): |
| 29 | + with pytest.raises(ValidationError) as exc_info: |
| 30 | + OAuthMetadata( |
| 31 | + issuer="https://example.com", |
| 32 | + token_endpoint="https://example.com/token", |
| 33 | + grant_types_supported=["authorization_code", "client_credentials"], |
| 34 | + ) |
| 35 | + assert "authorization_endpoint is required when authorization_code grant type is supported" in str( |
| 36 | + exc_info.value |
| 37 | + ) |
| 38 | + |
| 39 | + OAuthMetadata( |
| 40 | + issuer="https://example.com", |
| 41 | + token_endpoint="https://example.com/token", |
| 42 | + authorization_endpoint="https://example.com/auth", |
| 43 | + grant_types_supported=["client_credentials"], |
| 44 | + ) |
| 45 | + |
| 46 | + def test_model_dump_excludes_none(self): |
| 47 | + metadata = OAuthMetadata( |
| 48 | + issuer="https://example.com", |
| 49 | + authorization_endpoint="https://example.com/auth", |
| 50 | + token_endpoint="https://example.com/token", |
| 51 | + ) |
| 52 | + |
| 53 | + dumped = metadata.model_dump() |
| 54 | + |
| 55 | + assert "registration_endpoint" not in dumped |
| 56 | + |
| 57 | + |
| 58 | +class TestAuthConfig: |
| 59 | + def test_required_fields_validation(self): |
| 60 | + with pytest.raises( |
| 61 | + ValidationError, match="at least one of 'issuer', 'custom_oauth_metadata' or 'dependencies' is required" |
| 62 | + ): |
| 63 | + AuthConfig() |
| 64 | + |
| 65 | + AuthConfig(issuer="https://example.com") |
| 66 | + |
| 67 | + AuthConfig( |
| 68 | + custom_oauth_metadata={ |
| 69 | + "issuer": "https://example.com", |
| 70 | + "authorization_endpoint": "https://example.com/auth", |
| 71 | + "token_endpoint": "https://example.com/token", |
| 72 | + }, |
| 73 | + ) |
| 74 | + |
| 75 | + def dummy_dependency(): |
| 76 | + pass |
| 77 | + |
| 78 | + AuthConfig(dependencies=[Depends(dummy_dependency)]) |
| 79 | + |
| 80 | + def test_client_id_required_for_setup_proxies(self): |
| 81 | + with pytest.raises(ValidationError, match="'client_id' is required when 'setup_proxies' is True"): |
| 82 | + AuthConfig( |
| 83 | + issuer="https://example.com", |
| 84 | + setup_proxies=True, |
| 85 | + ) |
| 86 | + |
| 87 | + AuthConfig( |
| 88 | + issuer="https://example.com", |
| 89 | + setup_proxies=True, |
| 90 | + client_id="test-client-id", |
| 91 | + client_secret="test-client-secret", |
| 92 | + ) |
| 93 | + |
| 94 | + def test_client_secret_required_for_fake_registration(self): |
| 95 | + with pytest.raises( |
| 96 | + ValidationError, match="'client_secret' is required when 'setup_fake_dynamic_registration' is True" |
| 97 | + ): |
| 98 | + AuthConfig( |
| 99 | + issuer="https://example.com", |
| 100 | + setup_proxies=True, |
| 101 | + client_id="test-client-id", |
| 102 | + setup_fake_dynamic_registration=True, |
| 103 | + ) |
| 104 | + |
| 105 | + AuthConfig( |
| 106 | + issuer="https://example.com", |
| 107 | + setup_proxies=True, |
| 108 | + client_id="test-client-id", |
| 109 | + client_secret="test-client-secret", |
| 110 | + setup_fake_dynamic_registration=True, |
| 111 | + ) |
0 commit comments