| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- #!/usr/bin/env python3
- """
- Integration tests for the Telegram notification service.
- All configuration is read from the database via telegram_config_service.
- Run: pytest tests/test_telegram_integration.py -v
- """
- import asyncio
- import sys
- from pathlib import Path
- import pytest
- # Add the project root to path so imports work when executed directly.
- sys.path.insert(0, str(Path(__file__).parent.parent))
- from dotenv import load_dotenv
- load_dotenv()
- from services.notifications.telegram_config_service import load_config # noqa: E402
- from services.notifications.telegram_integration import ( # noqa: E402
- initialize_telegram_service,
- send_security_alert_telegram,
- send_system_alert_telegram,
- send_telegram_notification,
- send_user_activity_alert_telegram,
- telegram_health_check,
- )
- from services.notifications.telegram_service import ( # noqa: E402
- NotificationPriority,
- get_telegram_service,
- parse_http_proxy_url,
- )
- def print_separator(title=""):
- """Imprimir separador visual"""
- print("\n" + "=" * 60)
- if title:
- print(f" {title}")
- print("=" * 60)
- print()
- def check_configuration():
- """Verify Telegram configuration from the database."""
- print_separator("Verificación de Configuración")
- cfg = load_config()
- print(f"TELEGRAM_ENABLED: {cfg['enabled']}")
- print(
- f"TELEGRAM_API_ID: {'✓ Configurado' if cfg.get('api_id') else '✗ No configurado'}"
- )
- print(
- f"TELEGRAM_API_HASH: {'✓ Configurado' if cfg.get('api_hash') else '✗ No configurado'}"
- )
- print(
- f"TELEGRAM_BOT_TOKEN: {'✓ Configurado' if cfg.get('bot_token') else '✗ No configurado'}"
- )
- print(
- f"TELEGRAM_PHONE: {'✓ Configurado' if cfg.get('phone') else '✗ No configurado'}"
- )
- print(f"TELEGRAM_SESSION_NAME: {cfg.get('session_name')}")
- recipients = cfg.get("recipients", [])
- print(f"TELEGRAM_RECIPIENTS: {recipients if recipients else '✗ No configurado'}")
- if not cfg["enabled"]:
- print(
- "\n⚠️ Telegram deshabilitado — actívalo desde Admin → Telegram Configuration"
- )
- return False
- if not cfg.get("api_id") or not cfg.get("api_hash"):
- print("\n❌ Faltan credenciales de API")
- print(" Obtén tus credenciales en: https://my.telegram.org/apps")
- return False
- if not cfg.get("bot_token") and not cfg.get("phone"):
- print("\n❌ Falta BOT_TOKEN o PHONE")
- print(" Configura bot_token o phone desde el panel de administración")
- return False
- if not cfg.get("recipients") or not any(r.strip() for r in cfg["recipients"]):
- print("\n❌ Faltan destinatarios")
- print(" Configura los destinatarios desde Admin → Telegram Configuration")
- return False
- print("\n✅ Configuración completa")
- return True
- def _require_telegram_config():
- """Skip the test if Telegram is not configured in the DB."""
- cfg = load_config()
- if not cfg.get("enabled") or not cfg.get("api_id") or not cfg.get("api_hash"):
- pytest.skip(
- "Telegram not configured in DB — set via Admin → Telegram Configuration"
- )
- return cfg
- @pytest.mark.asyncio
- async def test_connection():
- """Probar conexión a Telegram"""
- print_separator("Test de Conexión")
- cfg = _require_telegram_config()
- try:
- service = get_telegram_service(
- api_id=int(cfg["api_id"]),
- api_hash=cfg["api_hash"],
- bot_token=cfg.get("bot_token") or None,
- phone=cfg.get("phone") or None,
- session_name=cfg.get("session_name") or "squidstats_bot",
- enabled=True,
- )
- print("Conectando a Telegram...")
- await service.connect()
- print("✅ Conexión establecida")
- print("\nObteniendo información de la cuenta...")
- health = await service.health_check()
- print("\n📊 Estado del servicio:")
- print(f" Habilitado: {health['enabled']}")
- print(f" Conectado: {health['connected']}")
- print(f" Modo Bot: {health['bot_mode']}")
- print(f" Sesión: {health['session']}")
- if health["connected"]:
- print(f" User ID: {health.get('user_id', 'N/A')}")
- print(f" Username: {health.get('username', 'N/A')}")
- print("\n✅ Test de conexión exitoso")
- else:
- error = health.get("error", "Unknown error")
- print(f"\n❌ No se pudo conectar: {error}")
- return False
- await service.disconnect()
- return True
- except Exception as e:
- print(f"\n❌ Error en test de conexión: {e}")
- import traceback
- traceback.print_exc()
- return False
- @pytest.mark.asyncio
- async def test_send_basic_notification():
- """Probar envío de notificación básica"""
- print_separator("Test de Notificación Básica")
- cfg = _require_telegram_config()
- recipients = cfg.get("recipients", [])
- if not recipients:
- pytest.skip("No recipients configured in DB")
- try:
- service = get_telegram_service()
- await service.connect()
- recipient = recipients[0]
- print(f"Enviando notificación de prueba a: {recipient}")
- success = await service.send_notification(
- recipient=recipient,
- message="🧪 Test de notificación desde SquidStats\n\nSi recibes este mensaje, la integración funciona correctamente.",
- priority=NotificationPriority.NORMAL,
- source="Test Suite",
- )
- if success:
- print("✅ Notificación enviada exitosamente")
- else:
- print("❌ No se pudo enviar la notificación")
- await service.disconnect()
- return success
- except Exception as e:
- print(f"❌ Error enviando notificación: {e}")
- import traceback
- traceback.print_exc()
- return False
- @pytest.mark.asyncio
- async def test_priority_notifications():
- """Probar notificaciones con diferentes prioridades"""
- print_separator("Test de Prioridades")
- cfg = _require_telegram_config()
- recipients = cfg.get("recipients", [])
- if not recipients:
- pytest.skip("No recipients configured in DB")
- try:
- service = get_telegram_service()
- await service.connect()
- recipient = recipients[0]
- priorities = [
- (NotificationPriority.LOW, "Información de baja prioridad"),
- (NotificationPriority.NORMAL, "Notificación estándar"),
- (NotificationPriority.HIGH, "Advertencia importante"),
- (NotificationPriority.CRITICAL, "¡Alerta crítica!"),
- ]
- for priority, message in priorities:
- print(f"Enviando {priority.name}...")
- await service.send_notification(
- recipient=recipient,
- message=message,
- priority=priority,
- source="Priority Test",
- )
- await asyncio.sleep(1) # Evitar rate limiting
- print("✅ Todas las prioridades enviadas")
- await service.disconnect()
- return True
- except Exception as e:
- print(f"❌ Error en test de prioridades: {e}")
- return False
- @pytest.mark.asyncio
- async def test_formatted_notifications():
- """Probar notificaciones con datos extra"""
- print_separator("Test de Mensajes Formateados")
- cfg = _require_telegram_config()
- recipients = cfg.get("recipients", [])
- if not recipients:
- pytest.skip("No recipients configured in DB")
- try:
- service = get_telegram_service()
- await service.connect()
- recipient = recipients[0]
- print("Enviando notificación con metadata...")
- await service.send_notification(
- recipient=recipient,
- message="Alerta del sistema detectada",
- priority=NotificationPriority.HIGH,
- source="System Monitor",
- extra_data={
- "CPU": "85%",
- "RAM": "7.2 GB / 16 GB",
- "Disco": "45.3 GB disponibles",
- "Uptime": "5 días, 3 horas",
- },
- )
- print("✅ Notificación formateada enviada")
- await service.disconnect()
- return True
- except Exception as e:
- print(f"❌ Error en test de formato: {e}")
- return False
- def test_integration_wrapper():
- """Probar wrapper de integración (funciones síncronas)"""
- print_separator("Test de Wrapper de Integración")
- try:
- # Inicializar servicio
- print("Inicializando servicio de Telegram...")
- if not initialize_telegram_service():
- print(
- "⚠️ Telegram no está configurado o está deshabilitado en este entorno de pruebas"
- )
- pytest.skip("Telegram integration not configured")
- print("✅ Servicio inicializado")
- # Health check
- print("\nVerificando estado...")
- health = telegram_health_check()
- print(f"Estado: {health}")
- if not health.get("connected"):
- print("⚠️ No conectado - algunas funciones no estarán disponibles")
- # Enviar notificación genérica
- print("\nEnviando notificación genérica...")
- success1 = send_telegram_notification(
- notification_type="info",
- message="Test de integración wrapper",
- source="Integration Test",
- )
- print(f"Resultado: {'✅ Enviado' if success1 else '❌ Fallo'}")
- # Enviar alerta de seguridad
- print("\nEnviando alerta de seguridad...")
- success2 = send_security_alert_telegram(
- alert_type="Acceso sospechoso detectado",
- details={
- "IP": "192.168.1.100",
- "Usuario": "test_user",
- "Timestamp": "2026-01-17 15:00:00",
- },
- )
- print(f"Resultado: {'✅ Enviado' if success2 else '❌ Fallo'}")
- # Enviar alerta del sistema
- print("\nEnviando alerta del sistema...")
- success3 = send_system_alert_telegram(
- alert_message="Uso elevado de recursos detectado",
- metrics={"CPU": "92%", "Memoria": "14.8 GB / 16 GB", "Temperatura": "78°C"},
- )
- print(f"Resultado: {'✅ Enviado' if success3 else '❌ Fallo'}")
- # Enviar alerta de actividad de usuario
- print("\nEnviando alerta de actividad de usuario...")
- success4 = send_user_activity_alert_telegram(
- username="john_doe",
- activity_data={
- "Requests": "1,234",
- "Datos descargados": "45.3 GB",
- "Sitios únicos": "87",
- },
- )
- print(f"Resultado: {'✅ Enviado' if success4 else '❌ Fallo'}")
- all_success = all([success1, success2, success3, success4])
- if all_success:
- print("\n✅ Todos los tests del wrapper exitosos")
- else:
- print("\n⚠️ Algunos tests fallaron")
- assert all_success, "Al menos un test del wrapper falló"
- except Exception as e:
- print(f"\n❌ Error en test de wrapper: {e}")
- import traceback
- traceback.print_exc()
- pytest.fail(f"Error en test de wrapper: {e}")
- async def run_async_tests():
- """Ejecutar tests asíncronos"""
- results = []
- # Test de conexión
- results.append(await test_connection())
- await asyncio.sleep(2)
- # Test de notificación básica
- results.append(await test_send_basic_notification())
- await asyncio.sleep(2)
- # Test de prioridades
- results.append(await test_priority_notifications())
- await asyncio.sleep(2)
- # Test de formato
- results.append(await test_formatted_notifications())
- return results
- def test_proxy_parsing():
- """Test parsing de URLs de proxy HTTP"""
- print_separator("🧪 Test de Parsing de Proxy")
- test_cases = [
- # (input, expected_output_keys)
- ("", None),
- ("http://proxy.example.com:8080", ["proxy_type", "addr", "port"]),
- (
- "http://user:pass@proxy.example.com:8080",
- ["proxy_type", "addr", "port", "username", "password"],
- ),
- ("https://proxy.example.com:8080", None), # Solo HTTP soportado
- ("invalid-url", None),
- ]
- all_passed = True
- for i, (proxy_url, expected_keys) in enumerate(test_cases, 1):
- try:
- result = parse_http_proxy_url(proxy_url)
- if expected_keys is None:
- if result is None:
- print(f"✅ Test {i}: '{proxy_url}' → None (correcto)")
- else:
- print(f"❌ Test {i}: '{proxy_url}' → {result} (esperaba None)")
- all_passed = False
- else:
- if result and all(key in result for key in expected_keys):
- print(f"✅ Test {i}: '{proxy_url}' → {result}")
- else:
- print(
- f"❌ Test {i}: '{proxy_url}' → {result} (faltan keys: {expected_keys})"
- )
- all_passed = False
- except Exception as e:
- print(f"❌ Test {i}: Error parsing '{proxy_url}': {e}")
- all_passed = False
- if all_passed:
- print("\n✅ Todos los tests de proxy pasaron")
- else:
- print("\n❌ Algunos tests de proxy fallaron")
- assert all_passed, "El parsing de proxy falló en uno o más casos"
- def main():
- """Función principal"""
- print_separator("🧪 Test de Integración de Telegram")
- # Verificar configuración
- if not check_configuration():
- print("\n❌ Configuración incompleta. Corrige los errores y vuelve a intentar.")
- sys.exit(1)
- # Test de parsing de proxy
- proxy_test_passed = test_proxy_parsing()
- # Ejecutar tests asíncronos
- print("\n🚀 Iniciando tests asíncronos...")
- async_results = asyncio.run(run_async_tests())
- # Ejecutar tests síncronos (wrapper)
- print("\n🚀 Iniciando tests de wrapper...")
- sync_result = True
- try:
- test_integration_wrapper()
- except AssertionError as e:
- print(f"\n❌ Test de wrapper falló: {e}")
- sync_result = False
- except Exception as e:
- print(f"\n❌ Error inesperado en el test de wrapper: {e}")
- import traceback
- traceback.print_exc()
- sync_result = False
- # Resumen
- print_separator("📊 Resumen de Tests")
- tests = [
- ("Parsing de Proxy", proxy_test_passed),
- ("Conexión", async_results[0] if len(async_results) > 0 else False),
- ("Notificación básica", async_results[1] if len(async_results) > 1 else False),
- ("Prioridades", async_results[2] if len(async_results) > 2 else False),
- ("Formato", async_results[3] if len(async_results) > 3 else False),
- ("Wrapper de integración", sync_result),
- ]
- for test_name, result in tests:
- status = "✅ PASS" if result else "❌ FAIL"
- print(f"{status} - {test_name}")
- total = len(tests)
- passed = sum(1 for _, result in tests if result)
- print(f"\n📈 Resultados: {passed}/{total} tests exitosos")
- if passed == total:
- print(
- "\n🎉 ¡Todos los tests pasaron! La integración de Telegram está funcionando correctamente."
- )
- return 0
- else:
- print("\n⚠️ Algunos tests fallaron. Revisa los logs para más detalles.")
- return 1
- if __name__ == "__main__":
- try:
- exit_code = main()
- sys.exit(exit_code)
- except KeyboardInterrupt:
- print("\n\n⚠️ Test interrumpido por el usuario")
- sys.exit(130)
- except Exception as e:
- print(f"\n\n❌ Error fatal: {e}")
- import traceback
- traceback.print_exc()
- sys.exit(1)
|