Andrei Neagoie Python 🆒

def test_register_user_success(self, auth_service): user = auth_service.register_user("test@example.com", "ValidPass123!") assert user.email == "test@example.com" assert user.user_id is not None

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

# Register user try: user = auth_service.register_user("user@example.com", "MySecurePass123!") print(f"✅ User registered: user.email") except ValidationError as e: print(f"❌ Registration failed: e") "192.168.1.1") def __init__( self

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") max_failed_attempts: int = 5

def __init__( self, secret_key: str, max_failed_attempts: int = 5, lockout_minutes: int = 15 ): """ Initialize authentication service Args: secret_key: Secret key for JWT max_failed_attempts: Number of failed attempts before lockout lockout_minutes: Lockout duration in minutes """ self.users: Dict[str, User] = {} self.token_manager = TokenManager(secret_key) self.password_hasher = PasswordHasher() self.rate_limiter = RateLimiter() self.max_failed_attempts = max_failed_attempts self.lockout_minutes = lockout_minutes

def test_login_success(self, auth_service): auth_service.register_user("test@example.com", "ValidPass123!") token, user = auth_service.login("test@example.com", "ValidPass123!", "192.168.1.1") assert token is not None assert user.email == "test@example.com"

Мультичат
Закрыть