|
3 | 3 | import pytest |
4 | 4 | from pydantic import ValidationError |
5 | 5 |
|
6 | | -from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthMetadata |
| 6 | +from mcp.shared.auth import InvalidScopeError, OAuthClientInformationFull, OAuthClientMetadata, OAuthMetadata |
7 | 7 |
|
8 | 8 |
|
9 | 9 | def test_oauth(): |
@@ -138,3 +138,43 @@ def test_invalid_non_empty_url_still_rejected(): |
138 | 138 | } |
139 | 139 | with pytest.raises(ValidationError): |
140 | 140 | OAuthClientMetadata.model_validate(data) |
| 141 | + |
| 142 | + |
| 143 | +def test_validate_scope_returns_none_when_no_scope_requested(): |
| 144 | + client = OAuthClientInformationFull( |
| 145 | + client_id="client", |
| 146 | + redirect_uris=["https://example.com/callback"], |
| 147 | + scope="read write", |
| 148 | + ) |
| 149 | + |
| 150 | + assert client.validate_scope(None) is None |
| 151 | + |
| 152 | + |
| 153 | +def test_validate_scope_allows_registered_scopes(): |
| 154 | + client = OAuthClientInformationFull( |
| 155 | + client_id="client", |
| 156 | + redirect_uris=["https://example.com/callback"], |
| 157 | + scope="read write", |
| 158 | + ) |
| 159 | + |
| 160 | + assert client.validate_scope("read write") == ["read", "write"] |
| 161 | + |
| 162 | + |
| 163 | +def test_validate_scope_rejects_unregistered_scopes(): |
| 164 | + client = OAuthClientInformationFull( |
| 165 | + client_id="client", |
| 166 | + redirect_uris=["https://example.com/callback"], |
| 167 | + scope="read", |
| 168 | + ) |
| 169 | + |
| 170 | + with pytest.raises(InvalidScopeError, match="Client was not registered with scope write"): |
| 171 | + client.validate_scope("write") |
| 172 | + |
| 173 | + |
| 174 | +def test_validate_scope_allows_requested_scopes_when_registered_scope_omitted(): |
| 175 | + client = OAuthClientInformationFull( |
| 176 | + client_id="client", |
| 177 | + redirect_uris=["https://example.com/callback"], |
| 178 | + ) |
| 179 | + |
| 180 | + assert client.validate_scope("read write") == ["read", "write"] |
0 commit comments