|
| 1 | +import base64 |
| 2 | +import time |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from fastapi_cloud_cli.utils.auth import ( |
| 8 | + AuthConfig, |
| 9 | + is_logged_in, |
| 10 | + is_token_expired, |
| 11 | + write_auth_config, |
| 12 | +) |
| 13 | + |
| 14 | +from .utils import create_jwt_token |
| 15 | + |
| 16 | + |
| 17 | +def test_is_token_expired_with_valid_token() -> None: |
| 18 | + future_exp = int(time.time()) + 3600 |
| 19 | + |
| 20 | + token = create_jwt_token({"exp": future_exp, "sub": "test_user"}) |
| 21 | + |
| 22 | + assert not is_token_expired(token) |
| 23 | + |
| 24 | + |
| 25 | +def test_is_token_expired_with_expired_token() -> None: |
| 26 | + past_exp = int(time.time()) - 3600 |
| 27 | + token = create_jwt_token({"exp": past_exp, "sub": "test_user"}) |
| 28 | + |
| 29 | + assert is_token_expired(token) |
| 30 | + |
| 31 | + |
| 32 | +def test_is_token_expired_with_no_exp_claim() -> None: |
| 33 | + token = create_jwt_token({"sub": "test_user"}) |
| 34 | + |
| 35 | + # Tokens without exp claim should be considered valid |
| 36 | + assert not is_token_expired(token) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + "token", |
| 41 | + [ |
| 42 | + "not.a.valid.jwt.token", |
| 43 | + "only.two", |
| 44 | + "invalid", |
| 45 | + "", |
| 46 | + "...", |
| 47 | + ], |
| 48 | +) |
| 49 | +def test_is_token_expired_with_malformed_token(token: str) -> None: |
| 50 | + assert is_token_expired(token) |
| 51 | + |
| 52 | + |
| 53 | +def test_is_token_expired_with_invalid_base64() -> None: |
| 54 | + token = "header.!!!invalid_signature!!!.signature" |
| 55 | + assert is_token_expired(token) |
| 56 | + |
| 57 | + |
| 58 | +def test_is_token_expired_with_invalid_json() -> None: |
| 59 | + header_encoded = base64.urlsafe_b64encode(b'{"alg":"HS256"}').decode().rstrip("=") |
| 60 | + payload_encoded = base64.urlsafe_b64encode(b"{invalid json}").decode().rstrip("=") |
| 61 | + signature = base64.urlsafe_b64encode(b"signature").decode().rstrip("=") |
| 62 | + token = f"{header_encoded}.{payload_encoded}.{signature}" |
| 63 | + |
| 64 | + assert is_token_expired(token) |
| 65 | + |
| 66 | + |
| 67 | +def test_is_logged_in_with_no_token(temp_auth_config: Path) -> None: |
| 68 | + assert not temp_auth_config.exists() |
| 69 | + assert not is_logged_in() |
| 70 | + |
| 71 | + |
| 72 | +def test_is_logged_in_with_valid_token(temp_auth_config: Path) -> None: |
| 73 | + future_exp = int(time.time()) + 3600 |
| 74 | + token = create_jwt_token({"exp": future_exp, "sub": "test_user"}) |
| 75 | + |
| 76 | + write_auth_config(AuthConfig(access_token=token)) |
| 77 | + |
| 78 | + assert is_logged_in() |
| 79 | + |
| 80 | + |
| 81 | +def test_is_logged_in_with_expired_token(temp_auth_config: Path) -> None: |
| 82 | + past_exp = int(time.time()) - 3600 |
| 83 | + token = create_jwt_token({"exp": past_exp, "sub": "test_user"}) |
| 84 | + |
| 85 | + write_auth_config(AuthConfig(access_token=token)) |
| 86 | + |
| 87 | + assert not is_logged_in() |
| 88 | + |
| 89 | + |
| 90 | +def test_is_logged_in_with_malformed_token(temp_auth_config: Path) -> None: |
| 91 | + write_auth_config(AuthConfig(access_token="not.a.valid.token")) |
| 92 | + |
| 93 | + assert not is_logged_in() |
| 94 | + |
| 95 | + |
| 96 | +def test_is_token_expired_edge_case_exact_expiration() -> None: |
| 97 | + current_time = int(time.time()) |
| 98 | + token = create_jwt_token({"exp": current_time, "sub": "test_user"}) |
| 99 | + |
| 100 | + assert is_token_expired(token) |
| 101 | + |
| 102 | + |
| 103 | +def test_is_token_expired_edge_case_one_second_before() -> None: |
| 104 | + current_time = int(time.time()) |
| 105 | + token = create_jwt_token({"exp": current_time + 1, "sub": "test_user"}) |
| 106 | + |
| 107 | + assert not is_token_expired(token) |
0 commit comments