|
1 | 1 | """Tests for OAuth 2.0 shared code.""" |
2 | 2 |
|
3 | | -from mcp.shared.auth import OAuthMetadata |
| 3 | +import pytest |
| 4 | +from pydantic import ValidationError |
| 5 | + |
| 6 | +from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthMetadata |
4 | 7 |
|
5 | 8 |
|
6 | 9 | class TestOAuthMetadata: |
@@ -59,3 +62,80 @@ def test_oauth_with_jarm(self): |
59 | 62 | "token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post"], |
60 | 63 | } |
61 | 64 | ) |
| 65 | + |
| 66 | + |
| 67 | +# RFC 7591 §2 marks client_uri/logo_uri/tos_uri/policy_uri/jwks_uri as OPTIONAL. |
| 68 | +# Some authorization servers echo the client's omitted metadata back as "" |
| 69 | +# instead of dropping the keys; without coercion, AnyHttpUrl rejects "" and |
| 70 | +# the whole registration response is thrown away even though the server |
| 71 | +# returned a valid client_id. |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize( |
| 75 | + "empty_field", |
| 76 | + ["client_uri", "logo_uri", "tos_uri", "policy_uri", "jwks_uri"], |
| 77 | +) |
| 78 | +def test_optional_url_empty_string_coerced_to_none(empty_field: str): |
| 79 | + data = { |
| 80 | + "redirect_uris": ["https://example.com/callback"], |
| 81 | + empty_field: "", |
| 82 | + } |
| 83 | + metadata = OAuthClientMetadata.model_validate(data) |
| 84 | + assert getattr(metadata, empty_field) is None |
| 85 | + |
| 86 | + |
| 87 | +def test_all_optional_urls_empty_together(): |
| 88 | + data = { |
| 89 | + "redirect_uris": ["https://example.com/callback"], |
| 90 | + "client_uri": "", |
| 91 | + "logo_uri": "", |
| 92 | + "tos_uri": "", |
| 93 | + "policy_uri": "", |
| 94 | + "jwks_uri": "", |
| 95 | + } |
| 96 | + metadata = OAuthClientMetadata.model_validate(data) |
| 97 | + assert metadata.client_uri is None |
| 98 | + assert metadata.logo_uri is None |
| 99 | + assert metadata.tos_uri is None |
| 100 | + assert metadata.policy_uri is None |
| 101 | + assert metadata.jwks_uri is None |
| 102 | + |
| 103 | + |
| 104 | +def test_valid_url_passes_through_unchanged(): |
| 105 | + data = { |
| 106 | + "redirect_uris": ["https://example.com/callback"], |
| 107 | + "client_uri": "https://udemy.com/", |
| 108 | + } |
| 109 | + metadata = OAuthClientMetadata.model_validate(data) |
| 110 | + assert str(metadata.client_uri) == "https://udemy.com/" |
| 111 | + |
| 112 | + |
| 113 | +def test_information_full_inherits_coercion(): |
| 114 | + """OAuthClientInformationFull subclasses OAuthClientMetadata, so the |
| 115 | + same coercion applies to DCR responses parsed via the full model.""" |
| 116 | + data = { |
| 117 | + "client_id": "abc123", |
| 118 | + "redirect_uris": ["https://example.com/callback"], |
| 119 | + "client_uri": "", |
| 120 | + "logo_uri": "", |
| 121 | + "tos_uri": "", |
| 122 | + "policy_uri": "", |
| 123 | + "jwks_uri": "", |
| 124 | + } |
| 125 | + info = OAuthClientInformationFull.model_validate(data) |
| 126 | + assert info.client_id == "abc123" |
| 127 | + assert info.client_uri is None |
| 128 | + assert info.logo_uri is None |
| 129 | + assert info.tos_uri is None |
| 130 | + assert info.policy_uri is None |
| 131 | + assert info.jwks_uri is None |
| 132 | + |
| 133 | + |
| 134 | +def test_invalid_non_empty_url_still_rejected(): |
| 135 | + """Coercion must only touch empty strings — garbage URLs still raise.""" |
| 136 | + data = { |
| 137 | + "redirect_uris": ["https://example.com/callback"], |
| 138 | + "client_uri": "not a url", |
| 139 | + } |
| 140 | + with pytest.raises(ValidationError): |
| 141 | + OAuthClientMetadata.model_validate(data) |
0 commit comments