|
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 AnyUrl |
| 5 | + |
| 6 | +from mcp.shared.auth import InvalidScopeError, OAuthClientMetadata, OAuthMetadata |
4 | 7 |
|
5 | 8 |
|
6 | 9 | def test_oauth(): |
@@ -58,3 +61,43 @@ def test_oauth_with_jarm(): |
58 | 61 | "token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post"], |
59 | 62 | } |
60 | 63 | ) |
| 64 | + |
| 65 | + |
| 66 | +def test_validate_scope_none_required_scopes_accepts_all(): |
| 67 | + """When client has no scope restrictions (scope=None), all requested scopes should be accepted.""" |
| 68 | + client = OAuthClientMetadata( |
| 69 | + redirect_uris=[AnyUrl("http://localhost:3030/callback")], |
| 70 | + scope=None, |
| 71 | + ) |
| 72 | + result = client.validate_scope("read write admin") |
| 73 | + assert result == ["read", "write", "admin"] |
| 74 | + |
| 75 | + |
| 76 | +def test_validate_scope_none_requested_scope_returns_none(): |
| 77 | + """When no scope is requested, validate_scope should return None.""" |
| 78 | + client = OAuthClientMetadata( |
| 79 | + redirect_uris=[AnyUrl("http://localhost:3030/callback")], |
| 80 | + scope="read write", |
| 81 | + ) |
| 82 | + result = client.validate_scope(None) |
| 83 | + assert result is None |
| 84 | + |
| 85 | + |
| 86 | +def test_validate_scope_rejects_unauthorized_scope(): |
| 87 | + """When client has specific allowed scopes, unauthorized scopes should be rejected.""" |
| 88 | + client = OAuthClientMetadata( |
| 89 | + redirect_uris=[AnyUrl("http://localhost:3030/callback")], |
| 90 | + scope="read", |
| 91 | + ) |
| 92 | + with pytest.raises(InvalidScopeError, match="write"): |
| 93 | + client.validate_scope("read write") |
| 94 | + |
| 95 | + |
| 96 | +def test_validate_scope_accepts_authorized_scope(): |
| 97 | + """When client has specific allowed scopes, authorized scopes should be accepted.""" |
| 98 | + client = OAuthClientMetadata( |
| 99 | + redirect_uris=[AnyUrl("http://localhost:3030/callback")], |
| 100 | + scope="read write", |
| 101 | + ) |
| 102 | + result = client.validate_scope("read write") |
| 103 | + assert result == ["read", "write"] |
0 commit comments