|

Andrei Neagoie Python [better] -

def test_login_wrong_password(self, auth_service): auth_service.register_user("test@example.com", "ValidPass123!") with pytest.raises(InvalidPasswordError): auth_service.login("test@example.com", "WrongPass456!", "192.168.1.1")

def record_attempt(self, key: str) -> None: """Record a new authentication attempt""" if key not in self.attempts: self.attempts[key] = [] self.attempts[key].append(time.time()) class AuthenticationService: """Main authentication service coordinating all components""" andrei neagoie python

def validate_token(self, token: str) -> Dict: """ Validate and decode JWT token Args: token: JWT token string Returns: Decoded token payload Raises: AuthenticationError: If token is invalid or expired """ try: payload = jwt.decode( token, self.secret_key, algorithms=['HS256'] ) return payload except ExpiredSignatureError: raise AuthenticationError("Token has expired") except InvalidTokenError as e: raise AuthenticationError(f"Invalid token: str(e)") class RateLimiter: """Simple in-memory rate limiter for authentication attempts""" "192.168.1.1") def record_attempt(self

import jwt from jwt.exceptions import InvalidTokenError, ExpiredSignatureError class AuthenticationError(Exception): """Base exception for authentication errors""" pass key: str) -&gt

def test_verify_wrong_password(self): hasher = PasswordHasher() hashed = hasher.hash_password("Correct123!") assert not hasher.verify_password("Wrong456!", hashed) class TestAuthenticationService: @pytest.fixture def auth_service(self): return AuthenticationService(secret_key="test-secret-key-123")

class UserNotFoundError(AuthenticationError): """Raised when user doesn't exist""" pass