@dataclass class User: """User entity representing authenticated users""" user_id: str email: str password_hash: str created_at: datetime last_login: Optional[datetime] = None is_active: bool = True failed_attempts: int = 0 locked_until: Optional[datetime] = None
class ValidationError(AuthenticationError): """Raised when input validation fails""" pass andrei neagoie python
def is_locked(self) -> bool: """Check if user account is currently locked""" if self.locked_until and datetime.utcnow() < self.locked_until: return True return False class PasswordHasher: """Handles secure password hashing and verification""" andrei neagoie python
test_auth.py content: """
def test_rate_limiting(self, auth_service): auth_service.register_user("test@example.com", "ValidPass123!") ip = "192.168.1.100" # Try wrong password 5 times for _ in range(5): with pytest.raises(InvalidPasswordError): auth_service.login("test@example.com", "wrong", ip) # 6th attempt should trigger rate limit with pytest.raises(RateLimitExceededError): auth_service.login("test@example.com", "wrong", ip) andrei neagoie python