|
| 1 | +"""Tests for JWT local decode (OPE-75).""" |
| 2 | +import pytest |
| 3 | +import jwt as pyjwt |
| 4 | +import time |
| 5 | +from unittest.mock import patch, MagicMock |
| 6 | + |
| 7 | + |
| 8 | +JWT_SECRET = "test-jwt-secret-for-unit-tests" |
| 9 | + |
| 10 | + |
| 11 | +def _make_token(payload: dict, secret: str = JWT_SECRET) -> str: |
| 12 | + """Create a signed JWT for testing.""" |
| 13 | + defaults = { |
| 14 | + "aud": "authenticated", |
| 15 | + "exp": int(time.time()) + 3600, |
| 16 | + "iat": int(time.time()), |
| 17 | + "role": "authenticated", |
| 18 | + } |
| 19 | + return pyjwt.encode({**defaults, **payload}, secret, algorithm="HS256") |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def auth_service(): |
| 24 | + """Create auth service with local JWT secret configured.""" |
| 25 | + with patch("services.auth.create_client") as mock_client: |
| 26 | + mock_client.return_value = MagicMock() |
| 27 | + with patch.dict("os.environ", { |
| 28 | + "SUPABASE_URL": "https://test.supabase.co", |
| 29 | + "SUPABASE_ANON_KEY": "test-key", |
| 30 | + "SUPABASE_JWT_SECRET": JWT_SECRET, |
| 31 | + }): |
| 32 | + from services.auth import SupabaseAuthService |
| 33 | + yield SupabaseAuthService() |
| 34 | + |
| 35 | + |
| 36 | +class TestLocalJWTDecode: |
| 37 | + """Verify local JWT decode works without network calls.""" |
| 38 | + |
| 39 | + def test_valid_token_returns_user_data(self, auth_service): |
| 40 | + token = _make_token({"sub": "user-123", "email": "dev@test.com", "user_metadata": {"tier": "pro"}}) |
| 41 | + result = auth_service.verify_jwt(token) |
| 42 | + |
| 43 | + assert result["user_id"] == "user-123" |
| 44 | + assert result["email"] == "dev@test.com" |
| 45 | + assert result["metadata"]["tier"] == "pro" |
| 46 | + |
| 47 | + def test_bearer_prefix_stripped(self, auth_service): |
| 48 | + token = _make_token({"sub": "user-456", "email": "a@b.com"}) |
| 49 | + result = auth_service.verify_jwt(f"Bearer {token}") |
| 50 | + |
| 51 | + assert result["user_id"] == "user-456" |
| 52 | + |
| 53 | + def test_expired_token_raises_401(self, auth_service): |
| 54 | + token = _make_token({"sub": "user-789", "exp": int(time.time()) - 60}) |
| 55 | + |
| 56 | + from fastapi import HTTPException |
| 57 | + with pytest.raises(HTTPException) as exc: |
| 58 | + auth_service.verify_jwt(token) |
| 59 | + assert exc.value.status_code == 401 |
| 60 | + assert "expired" in exc.value.detail.lower() |
| 61 | + |
| 62 | + def test_wrong_secret_raises_401(self, auth_service): |
| 63 | + token = _make_token({"sub": "user-000"}, secret="wrong-secret") |
| 64 | + |
| 65 | + from fastapi import HTTPException |
| 66 | + with pytest.raises(HTTPException) as exc: |
| 67 | + auth_service.verify_jwt(token) |
| 68 | + assert exc.value.status_code == 401 |
| 69 | + |
| 70 | + def test_missing_sub_claim_raises_401(self, auth_service): |
| 71 | + token = _make_token({"email": "no-sub@test.com"}) |
| 72 | + |
| 73 | + from fastapi import HTTPException |
| 74 | + with pytest.raises(HTTPException) as exc: |
| 75 | + auth_service.verify_jwt(token) |
| 76 | + assert exc.value.status_code == 401 |
| 77 | + assert "subject" in exc.value.detail.lower() |
| 78 | + |
| 79 | + def test_wrong_audience_raises_401(self, auth_service): |
| 80 | + payload = {"sub": "user-aud", "aud": "wrong-audience", "exp": int(time.time()) + 3600} |
| 81 | + token = pyjwt.encode(payload, JWT_SECRET, algorithm="HS256") |
| 82 | + |
| 83 | + from fastapi import HTTPException |
| 84 | + with pytest.raises(HTTPException) as exc: |
| 85 | + auth_service.verify_jwt(token) |
| 86 | + assert exc.value.status_code == 401 |
| 87 | + |
| 88 | + def test_no_network_call_made(self, auth_service): |
| 89 | + """The whole point of OPE-75: verify_jwt should NOT hit the network.""" |
| 90 | + token = _make_token({"sub": "user-net", "email": "net@test.com"}) |
| 91 | + |
| 92 | + auth_service.verify_jwt(token) |
| 93 | + |
| 94 | + # get_user should never be called when jwt_secret is available |
| 95 | + auth_service.client.auth.get_user.assert_not_called() |
| 96 | + |
| 97 | + def test_metadata_defaults_to_empty_dict(self, auth_service): |
| 98 | + token = _make_token({"sub": "user-no-meta", "email": "x@y.com"}) |
| 99 | + result = auth_service.verify_jwt(token) |
| 100 | + |
| 101 | + assert result["metadata"] == {} |
| 102 | + |
| 103 | + def test_metadata_null_coalesced_to_empty_dict(self, auth_service): |
| 104 | + """user_metadata can be explicitly null in Supabase JWTs.""" |
| 105 | + token = _make_token({"sub": "user-null-meta", "user_metadata": None}) |
| 106 | + result = auth_service.verify_jwt(token) |
| 107 | + |
| 108 | + assert result["metadata"] == {} |
| 109 | + |
| 110 | + def test_bearer_prefix_case_insensitive(self, auth_service): |
| 111 | + token = _make_token({"sub": "user-case"}) |
| 112 | + for prefix in ["Bearer ", "bearer ", "BEARER "]: |
| 113 | + result = auth_service.verify_jwt(f"{prefix}{token}") |
| 114 | + assert result["user_id"] == "user-case" |
| 115 | + |
| 116 | + |
| 117 | +class TestAPIFallback: |
| 118 | + """When JWT secret is not configured, fall back to Supabase API.""" |
| 119 | + |
| 120 | + def test_falls_back_to_api_when_no_secret(self): |
| 121 | + with patch("services.auth.create_client") as mock_client: |
| 122 | + client = MagicMock() |
| 123 | + user = MagicMock() |
| 124 | + user.id = "api-user-123" |
| 125 | + user.email = "api@test.com" |
| 126 | + user.user_metadata = {"tier": "free"} |
| 127 | + response = MagicMock() |
| 128 | + response.user = user |
| 129 | + client.auth.get_user.return_value = response |
| 130 | + mock_client.return_value = client |
| 131 | + |
| 132 | + with patch.dict("os.environ", { |
| 133 | + "SUPABASE_URL": "https://test.supabase.co", |
| 134 | + "SUPABASE_ANON_KEY": "test-key", |
| 135 | + "SUPABASE_JWT_SECRET": "", |
| 136 | + }): |
| 137 | + from services.auth import SupabaseAuthService |
| 138 | + service = SupabaseAuthService() |
| 139 | + result = service.verify_jwt("some-token") |
| 140 | + |
| 141 | + assert result["user_id"] == "api-user-123" |
| 142 | + client.auth.get_user.assert_called_once_with("some-token") |
0 commit comments