|
| 1 | +"""Unit tests for DebugTokenVerifier.""" |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | +from fastmcp.server.auth.providers.debug import DebugTokenVerifier |
| 6 | + |
| 7 | + |
| 8 | +class TestDebugTokenVerifier: |
| 9 | + """Test DebugTokenVerifier initialization and validation.""" |
| 10 | + |
| 11 | + def test_init_defaults(self): |
| 12 | + """Test initialization with default parameters.""" |
| 13 | + verifier = DebugTokenVerifier() |
| 14 | + |
| 15 | + assert verifier.client_id == "debug-client" |
| 16 | + assert verifier.scopes == [] |
| 17 | + assert verifier.required_scopes == [] |
| 18 | + assert callable(verifier.validate) |
| 19 | + |
| 20 | + def test_init_custom_parameters(self): |
| 21 | + """Test initialization with custom parameters.""" |
| 22 | + verifier = DebugTokenVerifier( |
| 23 | + validate=lambda t: t.startswith("valid-"), |
| 24 | + client_id="custom-client", |
| 25 | + scopes=["read", "write"], |
| 26 | + required_scopes=["admin"], |
| 27 | + ) |
| 28 | + |
| 29 | + assert verifier.client_id == "custom-client" |
| 30 | + assert verifier.scopes == ["read", "write"] |
| 31 | + assert verifier.required_scopes == ["admin"] |
| 32 | + |
| 33 | + async def test_verify_token_default_accepts_all(self): |
| 34 | + """Test that default verifier accepts all non-empty tokens.""" |
| 35 | + verifier = DebugTokenVerifier() |
| 36 | + |
| 37 | + result = await verifier.verify_token("any-token") |
| 38 | + |
| 39 | + assert result is not None |
| 40 | + assert result.token == "any-token" |
| 41 | + assert result.client_id == "debug-client" |
| 42 | + assert result.scopes == [] |
| 43 | + assert result.expires_at is None |
| 44 | + assert result.claims == {"token": "any-token"} |
| 45 | + |
| 46 | + async def test_verify_token_rejects_empty(self): |
| 47 | + """Test that empty tokens are rejected even with default verifier.""" |
| 48 | + verifier = DebugTokenVerifier() |
| 49 | + |
| 50 | + # Empty string |
| 51 | + assert await verifier.verify_token("") is None |
| 52 | + |
| 53 | + # Whitespace only |
| 54 | + assert await verifier.verify_token(" ") is None |
| 55 | + |
| 56 | + async def test_verify_token_sync_callable_success(self): |
| 57 | + """Test token verification with custom sync callable that passes.""" |
| 58 | + verifier = DebugTokenVerifier( |
| 59 | + validate=lambda t: t.startswith("valid-"), |
| 60 | + client_id="test-client", |
| 61 | + scopes=["read"], |
| 62 | + ) |
| 63 | + |
| 64 | + result = await verifier.verify_token("valid-token-123") |
| 65 | + |
| 66 | + assert result is not None |
| 67 | + assert result.token == "valid-token-123" |
| 68 | + assert result.client_id == "test-client" |
| 69 | + assert result.scopes == ["read"] |
| 70 | + assert result.expires_at is None |
| 71 | + assert result.claims == {"token": "valid-token-123"} |
| 72 | + |
| 73 | + async def test_verify_token_sync_callable_failure(self): |
| 74 | + """Test token verification with custom sync callable that fails.""" |
| 75 | + verifier = DebugTokenVerifier(validate=lambda t: t.startswith("valid-")) |
| 76 | + |
| 77 | + result = await verifier.verify_token("invalid-token") |
| 78 | + |
| 79 | + assert result is None |
| 80 | + |
| 81 | + async def test_verify_token_async_callable_success(self): |
| 82 | + """Test token verification with custom async callable that passes.""" |
| 83 | + |
| 84 | + async def async_validator(token: str) -> bool: |
| 85 | + # Simulate async operation (e.g., database check) |
| 86 | + return token in {"token1", "token2", "token3"} |
| 87 | + |
| 88 | + verifier = DebugTokenVerifier( |
| 89 | + validate=async_validator, |
| 90 | + client_id="async-client", |
| 91 | + scopes=["admin"], |
| 92 | + ) |
| 93 | + |
| 94 | + result = await verifier.verify_token("token2") |
| 95 | + |
| 96 | + assert result is not None |
| 97 | + assert result.token == "token2" |
| 98 | + assert result.client_id == "async-client" |
| 99 | + assert result.scopes == ["admin"] |
| 100 | + |
| 101 | + async def test_verify_token_async_callable_failure(self): |
| 102 | + """Test token verification with custom async callable that fails.""" |
| 103 | + |
| 104 | + async def async_validator(token: str) -> bool: |
| 105 | + return token in {"token1", "token2", "token3"} |
| 106 | + |
| 107 | + verifier = DebugTokenVerifier(validate=async_validator) |
| 108 | + |
| 109 | + result = await verifier.verify_token("token99") |
| 110 | + |
| 111 | + assert result is None |
| 112 | + |
| 113 | + async def test_verify_token_callable_exception(self): |
| 114 | + """Test that exceptions in validate callable are handled gracefully.""" |
| 115 | + |
| 116 | + def failing_validator(token: str) -> bool: |
| 117 | + raise ValueError("Something went wrong") |
| 118 | + |
| 119 | + verifier = DebugTokenVerifier(validate=failing_validator) |
| 120 | + |
| 121 | + result = await verifier.verify_token("any-token") |
| 122 | + |
| 123 | + assert result is None |
| 124 | + |
| 125 | + async def test_verify_token_async_callable_exception(self): |
| 126 | + """Test that exceptions in async validate callable are handled gracefully.""" |
| 127 | + |
| 128 | + async def failing_async_validator(token: str) -> bool: |
| 129 | + raise ValueError("Async validation failed") |
| 130 | + |
| 131 | + verifier = DebugTokenVerifier(validate=failing_async_validator) |
| 132 | + |
| 133 | + result = await verifier.verify_token("any-token") |
| 134 | + |
| 135 | + assert result is None |
| 136 | + |
| 137 | + async def test_verify_token_whitelist_pattern(self): |
| 138 | + """Test using verifier with a whitelist of allowed tokens.""" |
| 139 | + allowed_tokens = {"secret-token-1", "secret-token-2", "admin-token"} |
| 140 | + |
| 141 | + verifier = DebugTokenVerifier(validate=lambda t: t in allowed_tokens) |
| 142 | + |
| 143 | + # Allowed tokens |
| 144 | + assert await verifier.verify_token("secret-token-1") is not None |
| 145 | + assert await verifier.verify_token("admin-token") is not None |
| 146 | + |
| 147 | + # Disallowed tokens |
| 148 | + assert await verifier.verify_token("unknown-token") is None |
| 149 | + assert await verifier.verify_token("hacker-token") is None |
| 150 | + |
| 151 | + async def test_verify_token_pattern_matching(self): |
| 152 | + """Test using verifier with regex-like pattern matching.""" |
| 153 | + |
| 154 | + pattern = re.compile(r"^[A-Z]{3}-\d{4}-[a-z]{2}$") |
| 155 | + |
| 156 | + verifier = DebugTokenVerifier( |
| 157 | + validate=lambda t: bool(pattern.match(t)), |
| 158 | + client_id="pattern-client", |
| 159 | + ) |
| 160 | + |
| 161 | + # Valid patterns |
| 162 | + result = await verifier.verify_token("ABC-1234-xy") |
| 163 | + assert result is not None |
| 164 | + assert result.client_id == "pattern-client" |
| 165 | + |
| 166 | + # Invalid patterns |
| 167 | + assert await verifier.verify_token("abc-1234-xy") is None # Wrong case |
| 168 | + assert await verifier.verify_token("ABC-123-xy") is None # Wrong digits |
| 169 | + assert await verifier.verify_token("ABC-1234-xyz") is None # Too many chars |
0 commit comments