Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /Athroniaeth_keyshield /tests /test_regression.py
| """Regression tests for public API stability. | |
| Tests that ensure: | |
| - Public API remains importable | |
| - Optional dependencies raise helpful ImportError | |
| - Security warnings work correctly | |
| - Utility factories behave correctly | |
| """ | |
| import importlib | |
| import sys | |
| import re | |
| import string | |
| from datetime import datetime, timezone | |
| from typing import Optional | |
| import pytest | |
| from keyshield.hasher.base import MockApiKeyHasher, DEFAULT_PEPPER | |
| from keyshield.hasher.argon2 import Argon2ApiKeyHasher | |
| from keyshield.hasher.bcrypt import BcryptApiKeyHasher | |
| from keyshield.utils import ( | |
| uuid_factory, | |
| key_id_factory, | |
| key_secret_factory, | |
| datetime_factory, | |
| ) | |
| class TestPublicApiImports: | |
| """Ensure public API remains importable.""" | |
| def test_import_public_api(self, module_path: Optional[str], attr: str): | |
| """Public API attributes are importable.""" | |
| module_name = "keyshield" if module_path is None else f"keyshield.{module_path}" | |
| module = importlib.import_module(module_name) | |
| assert hasattr(module, attr) | |
| class TestOptionalDependencyErrors: | |
| """Ensure helpful errors when optional dependencies are missing.""" | |
| def test_missing_dependency_raises_helpful_error( | |
| self, | |
| monkeypatch: pytest.MonkeyPatch, | |
| library: str, | |
| module_path: str, | |
| ): | |
| """Missing optional dependency raises ImportError with install hint.""" | |
| monkeypatch.setitem(sys.modules, library, None) # pyrefly: ignore[bad-argument-type] | |
| with pytest.raises(ImportError) as exc_info: | |
| module = importlib.import_module(module_path) | |
| importlib.reload(module) | |
| expected = f"requires '{library}'. Install it with: uv add keyshield[{library}]" | |
| assert expected in str(exc_info.value) | |
| class TestSecurityWarnings: | |
| """Ensure security warnings work correctly.""" | |
| def test_default_pepper_warning(self, hasher_class): | |
| """Using default pepper raises UserWarning.""" | |
| with pytest.warns(UserWarning, match="insecure"): | |
| if hasher_class == BcryptApiKeyHasher: | |
| hasher_class(pepper=DEFAULT_PEPPER, rounds=4) | |
| else: | |
| hasher_class(pepper=DEFAULT_PEPPER) | |
| class TestUtilityFactories: | |
| """Tests for utility factory functions.""" | |
| def test_uuid_factory_format(self): | |
| """uuid_factory returns 32 lowercase hex chars.""" | |
| result = uuid_factory() | |
| assert isinstance(result, str) | |
| assert re.fullmatch(r"[0-9a-f]{32}", result) | |
| def test_uuid_factory_unique(self): | |
| """uuid_factory returns unique values.""" | |
| assert uuid_factory() != uuid_factory() | |
| def test_key_id_factory_format(self): | |
| """key_id_factory returns 16 hex chars.""" | |
| result = key_id_factory() | |
| assert isinstance(result, str) | |
| assert len(result) == 16 | |
| assert re.fullmatch(r"[0-9a-f]{16}", result) | |
| def test_key_id_factory_unique(self): | |
| """key_id_factory returns unique values.""" | |
| assert key_id_factory() != key_id_factory() | |
| def test_key_secret_factory_format(self, length: int): | |
| """key_secret_factory returns alphanumeric string of specified length.""" | |
| result = key_secret_factory(length) | |
| assert isinstance(result, str) | |
| assert len(result) == length | |
| valid_chars = string.ascii_letters + string.digits | |
| assert all(c in valid_chars for c in result) | |
| def test_key_secret_factory_unique(self): | |
| """key_secret_factory returns unique values.""" | |
| assert key_secret_factory() != key_secret_factory() | |
| def test_datetime_factory_utc_aware(self): | |
| """datetime_factory returns UTC-aware datetime.""" | |
| result = datetime_factory() | |
| assert isinstance(result, datetime) | |
| assert result.tzinfo == timezone.utc | |
| def test_datetime_factory_is_current(self): | |
| """datetime_factory returns current time.""" | |
| result = datetime_factory() | |
| now = datetime.now(timezone.utc) | |
| delta = abs((now - result).total_seconds()) | |
| assert delta < 2 | |
Xet Storage Details
- Size:
- 5.26 kB
- Xet hash:
- 83d7b94581f9e4c304bd012594a0f2bc0ea8b6e3943fd4c3f24a78f4142aab62
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.