Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /allen0099_FastAPI-CacheX /tests /session /test_security.py
| """Tests for security utilities.""" | |
| import pytest | |
| from fastapi_cachex.session.models import Session | |
| from fastapi_cachex.session.security import SecurityManager | |
| def test_security_manager_initialization() -> None: | |
| """Test security manager initialization: signing works with a valid key.""" | |
| manager = SecurityManager("a" * 32) | |
| assert manager.sign_session_id("test") is not None | |
| def test_security_manager_short_key() -> None: | |
| """Test security manager with short key.""" | |
| with pytest.raises(ValueError, match="at least 32 characters"): | |
| SecurityManager("short") | |
| def test_sign_session_id() -> None: | |
| """Test session ID signing.""" | |
| manager = SecurityManager("a" * 32) | |
| signature = manager.sign_session_id("test-session-id") | |
| assert signature is not None | |
| assert len(signature) == 64 # SHA256 hex digest | |
| def test_verify_signature() -> None: | |
| """Test signature verification.""" | |
| manager = SecurityManager("a" * 32) | |
| session_id = "test-session-id" | |
| signature = manager.sign_session_id(session_id) | |
| # Valid signature | |
| assert manager.verify_signature(session_id, signature) | |
| # Invalid signature | |
| assert not manager.verify_signature(session_id, "invalid") | |
| # Different session ID | |
| assert not manager.verify_signature("different-id", signature) | |
| def test_check_ip_match() -> None: | |
| """Test IP address matching.""" | |
| manager = SecurityManager("a" * 32) | |
| # Session without IP binding | |
| session = Session() | |
| assert manager.check_ip_match(session, "192.168.1.1") | |
| assert manager.check_ip_match(session, None) | |
| # Session with IP binding | |
| session.ip_address = "192.168.1.1" | |
| assert manager.check_ip_match(session, "192.168.1.1") | |
| assert not manager.check_ip_match(session, "192.168.1.2") | |
| assert not manager.check_ip_match(session, None) | |
| def test_check_user_agent_match() -> None: | |
| """Test User-Agent matching.""" | |
| manager = SecurityManager("a" * 32) | |
| # Session without UA binding | |
| session = Session() | |
| assert manager.check_user_agent_match(session, "Mozilla/5.0") | |
| assert manager.check_user_agent_match(session, None) | |
| # Session with UA binding | |
| session.user_agent = "Mozilla/5.0" | |
| assert manager.check_user_agent_match(session, "Mozilla/5.0") | |
| assert not manager.check_user_agent_match(session, "Chrome/91.0") | |
| assert not manager.check_user_agent_match(session, None) | |
| def test_hash_data() -> None: | |
| """Test data hashing.""" | |
| manager = SecurityManager("a" * 32) | |
| hash1 = manager.hash_data("test data") | |
| hash2 = manager.hash_data("test data") | |
| hash3 = manager.hash_data("different data") | |
| assert hash1 == hash2 | |
| assert hash1 != hash3 | |
| assert len(hash1) == 64 # SHA256 hex digest | |
Xet Storage Details
- Size:
- 2.75 kB
- Xet hash:
- 01a918a801449ff654f9f5a6e5d966ce2641b6c5a589ad1292d69ccaa7f5c3d6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.