From 4a0d63d06316d547a5a564587751d86093d1e478 Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Tue, 27 Jan 2026 12:39:05 -0800 Subject: [PATCH] fix(test): add cleanup to prevent test pollution in CLI JWT expiration tests The custom expiration test was setting LITELLM_CLI_JWT_EXPIRATION_HOURS=48 and reloading modules, but not cleaning up afterwards. This caused subsequent tests to use the cached 48-hour value instead of the default 24 hours. Added request.addfinalizer() to properly reload modules after the test completes, ensuring test isolation. --- .../test_litellm/proxy/auth/test_auth_checks.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/test_litellm/proxy/auth/test_auth_checks.py b/tests/test_litellm/proxy/auth/test_auth_checks.py index c4c3f2c784f..174f424748f 100644 --- a/tests/test_litellm/proxy/auth/test_auth_checks.py +++ b/tests/test_litellm/proxy/auth/test_auth_checks.py @@ -156,17 +156,23 @@ def test_get_cli_jwt_auth_token_default_expiration(valid_sso_user_defined_values def test_get_cli_jwt_auth_token_custom_expiration( - valid_sso_user_defined_values, monkeypatch + valid_sso_user_defined_values, monkeypatch, request ): """Test generating CLI JWT token with custom expiration via environment variable""" + import importlib + from litellm import constants + from litellm.proxy.auth import auth_checks + + # Register cleanup to reload modules after test (monkeypatch restores env var automatically) + def cleanup_modules(): + importlib.reload(constants) + importlib.reload(auth_checks) + request.addfinalizer(cleanup_modules) + # Set custom expiration to 48 hours monkeypatch.setenv("LITELLM_CLI_JWT_EXPIRATION_HOURS", "48") # Reload the constants module to pick up the new env var - import importlib - - from litellm import constants - from litellm.proxy.auth import auth_checks importlib.reload(constants) # Also reload auth_checks to pick up the new constant value importlib.reload(auth_checks)