218 lines
8.1 KiB
Python
218 lines
8.1 KiB
Python
"""
|
|
Test d'intégration du WhitelistManager avec l'Orchestrateur
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Ajouter le répertoire geniusia2 au path
|
|
sys.path.insert(0, str(Path(__file__).parent / "geniusia2"))
|
|
|
|
from core.whitelist_manager import WhitelistManager
|
|
from core.logger import Logger
|
|
from core.config import ensure_directories
|
|
|
|
def test_whitelist_manager_integration():
|
|
"""Test d'intégration du WhitelistManager"""
|
|
print("Test d'intégration WhitelistManager")
|
|
print("=" * 50)
|
|
|
|
# S'assurer que les répertoires existent
|
|
ensure_directories()
|
|
|
|
# Test 1: Créer un WhitelistManager
|
|
print("\n1. Création du WhitelistManager:")
|
|
logger = Logger()
|
|
whitelist_manager = WhitelistManager(logger=logger, require_admin_confirmation=False)
|
|
print(f" ✓ WhitelistManager créé")
|
|
print(f" - Chemin: {whitelist_manager.whitelist_path}")
|
|
print(f" - Taille initiale: {len(whitelist_manager.get_whitelist())}")
|
|
|
|
# Test 2: Ajouter des fenêtres à la liste blanche
|
|
print("\n2. Ajout de fenêtres à la liste blanche:")
|
|
test_windows = [
|
|
"Dolibarr*",
|
|
"Firefox*",
|
|
"Visual Studio Code",
|
|
"*Terminal"
|
|
]
|
|
|
|
for window in test_windows:
|
|
success = whitelist_manager.add_to_whitelist(window, admin_confirmed=True)
|
|
status = "✓" if success else "✗"
|
|
print(f" {status} Ajouté: {window}")
|
|
|
|
# Test 3: Vérifier is_window_allowed
|
|
print("\n3. Test de vérification des fenêtres:")
|
|
test_cases = [
|
|
("Dolibarr - Facturation", True),
|
|
("Firefox - Mozilla Firefox", True),
|
|
("Visual Studio Code", True),
|
|
("GNOME Terminal", True),
|
|
("Unknown Application", False),
|
|
("Notepad", False)
|
|
]
|
|
|
|
for window, expected in test_cases:
|
|
allowed = whitelist_manager.is_window_allowed(window)
|
|
status = "✓" if allowed == expected else "✗"
|
|
result = "Autorisé" if allowed else "Bloqué"
|
|
print(f" {status} {result}: {window}")
|
|
|
|
# Test 4: Statistiques
|
|
print("\n4. Statistiques de la liste blanche:")
|
|
stats = whitelist_manager.get_statistics()
|
|
print(f" ✓ Total entrées: {stats['total_entries']}")
|
|
print(f" ✓ Avec wildcards: {stats['entries_with_wildcards']}")
|
|
print(f" ✓ Exactes: {stats['entries_exact']}")
|
|
|
|
# Test 5: Métadonnées d'une entrée
|
|
print("\n5. Métadonnées d'une entrée:")
|
|
info = whitelist_manager.get_entry_info("Dolibarr*")
|
|
if info:
|
|
print(f" ✓ Informations pour 'Dolibarr*':")
|
|
print(f" - Ajouté le: {info.get('added_at')}")
|
|
print(f" - Ajouté par: {info.get('added_by')}")
|
|
print(f" - Confirmation admin: {info.get('admin_confirmed')}")
|
|
|
|
# Test 6: Suppression d'une entrée
|
|
print("\n6. Suppression d'une entrée:")
|
|
removed = whitelist_manager.remove_from_whitelist("Firefox*")
|
|
print(f" ✓ Supprimé 'Firefox*': {removed}")
|
|
print(f" - Nouvelle taille: {len(whitelist_manager.get_whitelist())}")
|
|
|
|
# Test 7: Vérifier que Firefox n'est plus autorisé
|
|
print("\n7. Vérification après suppression:")
|
|
allowed = whitelist_manager.is_window_allowed("Firefox - Mozilla")
|
|
status = "✓" if not allowed else "✗"
|
|
print(f" {status} Firefox maintenant bloqué: {not allowed}")
|
|
|
|
# Test 8: Export de la liste blanche
|
|
print("\n8. Export de la liste blanche:")
|
|
export_path = "whitelist_export_test.json"
|
|
success = whitelist_manager.export_whitelist(export_path)
|
|
print(f" ✓ Exporté: {success}")
|
|
|
|
# Nettoyer le fichier d'export
|
|
import os
|
|
if os.path.exists(export_path):
|
|
os.remove(export_path)
|
|
print(f" ✓ Fichier d'export nettoyé")
|
|
|
|
print("\n✓ Tous les tests d'intégration réussis!")
|
|
return True
|
|
|
|
|
|
def test_orchestrator_whitelist_integration():
|
|
"""Test de l'intégration avec l'Orchestrateur"""
|
|
print("\n\nTest d'intégration avec l'Orchestrateur")
|
|
print("=" * 50)
|
|
|
|
try:
|
|
from core.orchestrator import Orchestrator
|
|
from core.learning_manager import LearningManager
|
|
from core.embeddings_manager import EmbeddingsManager
|
|
from core.utils.vision_utils import VisionUtils
|
|
from core.llm_manager import LLMManager
|
|
from core.logger import Logger
|
|
from core.config import get_config
|
|
|
|
# Initialiser les composants
|
|
print("\n1. Initialisation des composants:")
|
|
logger = Logger()
|
|
embeddings_manager = EmbeddingsManager()
|
|
learning_manager = LearningManager(embeddings_manager, logger, get_config())
|
|
vision_utils = VisionUtils()
|
|
llm_manager = LLMManager(logger=logger, fallback_to_vision=True)
|
|
whitelist_manager = WhitelistManager(logger=logger, require_admin_confirmation=False)
|
|
|
|
print(" ✓ Composants initialisés")
|
|
|
|
# Ajouter des fenêtres à la liste blanche
|
|
print("\n2. Configuration de la liste blanche:")
|
|
whitelist_manager.add_to_whitelist("Dolibarr*", admin_confirmed=True)
|
|
whitelist_manager.add_to_whitelist("Firefox*", admin_confirmed=True)
|
|
print(f" ✓ Liste blanche configurée ({len(whitelist_manager.get_whitelist())} entrées)")
|
|
|
|
# Créer l'orchestrateur avec le WhitelistManager
|
|
print("\n3. Création de l'orchestrateur:")
|
|
orchestrator = Orchestrator(
|
|
learning_manager=learning_manager,
|
|
vision_utils=vision_utils,
|
|
llm_manager=llm_manager,
|
|
logger=logger,
|
|
gui=None,
|
|
whitelist_manager=whitelist_manager
|
|
)
|
|
print(" ✓ Orchestrateur créé avec WhitelistManager")
|
|
|
|
# Test de vérification via l'orchestrateur
|
|
print("\n4. Test de vérification via l'orchestrateur:")
|
|
test_cases = [
|
|
("Dolibarr - Facturation", True),
|
|
("Firefox - Mozilla", True),
|
|
("Unknown Application", False)
|
|
]
|
|
|
|
for window, expected in test_cases:
|
|
allowed = orchestrator._check_whitelist(window)
|
|
status = "✓" if allowed == expected else "✗"
|
|
result = "Autorisé" if allowed else "Bloqué"
|
|
print(f" {status} {result}: {window}")
|
|
|
|
# Test des méthodes de l'orchestrateur
|
|
print("\n5. Test des méthodes de l'orchestrateur:")
|
|
|
|
# Ajouter via l'orchestrateur
|
|
success = orchestrator.add_to_whitelist("Visual Studio Code", admin_confirmed=True)
|
|
print(f" ✓ Ajout via orchestrateur: {success}")
|
|
|
|
# Obtenir la liste blanche
|
|
whitelist = orchestrator.get_whitelist()
|
|
print(f" ✓ Liste blanche obtenue: {len(whitelist)} entrées")
|
|
|
|
# Supprimer via l'orchestrateur
|
|
success = orchestrator.remove_from_whitelist("Visual Studio Code")
|
|
print(f" ✓ Suppression via orchestrateur: {success}")
|
|
|
|
# Vérifier le statut
|
|
print("\n6. Statut de l'orchestrateur:")
|
|
status = orchestrator.get_status()
|
|
print(f" ✓ Whitelist size: {status['whitelist_size']}")
|
|
print(f" ✓ Enforce whitelist: {status['enforce_whitelist']}")
|
|
|
|
print("\n✓ Tous les tests d'intégration avec l'orchestrateur réussis!")
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"\n✗ Erreur lors du test d'intégration: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
# Test 1: WhitelistManager standalone
|
|
success1 = test_whitelist_manager_integration()
|
|
|
|
# Test 2: Intégration avec l'orchestrateur
|
|
success2 = test_orchestrator_whitelist_integration()
|
|
|
|
if success1 and success2:
|
|
print("\n" + "=" * 50)
|
|
print("✓ TOUS LES TESTS D'INTÉGRATION RÉUSSIS!")
|
|
print("=" * 50)
|
|
sys.exit(0)
|
|
else:
|
|
print("\n" + "=" * 50)
|
|
print("✗ CERTAINS TESTS ONT ÉCHOUÉ")
|
|
print("=" * 50)
|
|
sys.exit(1)
|
|
|
|
except Exception as e:
|
|
print(f"\n✗ Erreur fatale: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|