|
| 1 | +# |
| 2 | +# Copyright (C) 2017-2025 Dremio Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +""" |
| 17 | +Integration test for JWKSVerifier against a real JWKS endpoint and token. |
| 18 | +
|
| 19 | +Fill in JWKS_URL and TOKEN below before running: |
| 20 | +
|
| 21 | + uv run pytest tests/servers/test_jwks_integration.py -v -s |
| 22 | +""" |
| 23 | + |
| 24 | +import pytest |
| 25 | + |
| 26 | +from dremioai.servers.jwks_verifier import JWKSVerifier, TokenExpiredError |
| 27 | + |
| 28 | +# ── Fill these in ────────────────────────────────────────────────────────────── |
| 29 | +JWKS_URL = "" # e.g. "https://your-idp.example.com/.well-known/jwks.json" |
| 30 | +TOKEN = "" # a valid (non-expired) JWT signed by the above JWKS endpoint |
| 31 | +# ────────────────────────────────────────────────────────────────────────────── |
| 32 | + |
| 33 | +pytestmark = pytest.mark.skipif( |
| 34 | + not JWKS_URL or not TOKEN, |
| 35 | + reason="JWKS_URL and TOKEN must be set in the test file before running", |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.asyncio |
| 40 | +async def test_valid_token_verifies_and_returns_claims(): |
| 41 | + verifier = JWKSVerifier(JWKS_URL) |
| 42 | + claims = await verifier.verify(TOKEN) |
| 43 | + assert claims is not None, "Expected valid claims but got None" |
| 44 | + print(f"\nclaims: {claims}") |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_claims_contain_expected_fields(): |
| 49 | + verifier = JWKSVerifier(JWKS_URL) |
| 50 | + claims = await verifier.verify(TOKEN) |
| 51 | + assert claims is not None |
| 52 | + # At least one of exp / org_id / user_id should be populated |
| 53 | + assert any( |
| 54 | + v is not None for v in (claims.exp, claims.org_id, claims.user_id) |
| 55 | + ), f"All claims fields are None: {claims}" |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.asyncio |
| 59 | +async def test_tampered_token_returns_none(): |
| 60 | + verifier = JWKSVerifier(JWKS_URL) |
| 61 | + tampered = TOKEN[:-10] + "AAAAAAAAAA" |
| 62 | + result = await verifier.verify(tampered) |
| 63 | + assert result is None, "Expected None for a tampered token" |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.asyncio |
| 67 | +async def test_garbage_token_returns_none(): |
| 68 | + verifier = JWKSVerifier(JWKS_URL) |
| 69 | + result = await verifier.verify("not.a.jwt") |
| 70 | + assert result is None, "Expected None for a completely invalid token" |
0 commit comments