test_email_service.py becomes a mockup test
This commit is contained in:
parent
6aa7c3b725
commit
932814e4fa
1 changed files with 16 additions and 30 deletions
|
|
@ -1,11 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Sat Jul 5 15:03:24 2025
|
||||
|
||||
@author: madfuckinman
|
||||
"""
|
||||
# Utilisation de mock pour simuler l'envoi de mails mais sans spammer une boîte.
|
||||
|
||||
from unittest.mock import patch, MagicMock
|
||||
import os
|
||||
import sys
|
||||
from dotenv import load_dotenv
|
||||
|
|
@ -15,38 +10,29 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
|
|||
|
||||
from email_service import send_error_report_email
|
||||
|
||||
# Charger le fichier .env situé un niveau au-dessus
|
||||
# Charger les variables d'environnement
|
||||
dotenv_path = '../../../.env'
|
||||
load_dotenv(dotenv_path)
|
||||
|
||||
def test_email_service_with_alias():
|
||||
@patch("smtplib.SMTP")
|
||||
def test_email_service_connection(mock_smtp):
|
||||
"""
|
||||
Teste l'envoi d'un email avec la fonction send_error_report_email
|
||||
Teste la connexion SMTP sans envoyer réellement l'email
|
||||
"""
|
||||
# ⚙️ Vérifier si les variables d'environnement nécessaires sont présentes
|
||||
smtp_server = os.getenv("SMTP_SERVER")
|
||||
smtp_email = os.getenv("SMTP_EMAIL")
|
||||
admin_email = os.getenv("ADMIN_EMAIL")
|
||||
|
||||
assert smtp_server, "SMTP_SERVER non défini"
|
||||
assert smtp_email, "SMTP_EMAIL non défini"
|
||||
assert admin_email, "ADMIN_EMAIL non défini"
|
||||
# 🔧 Configuration du mock
|
||||
mock_server = MagicMock()
|
||||
mock_smtp.return_value.__enter__.return_value = mock_server
|
||||
|
||||
# Configuration de test
|
||||
print("🔧 Configuration Email :")
|
||||
print(f"Serveur SMTP : {smtp_server}")
|
||||
print(f"Authentification : {smtp_email}")
|
||||
print(f"Destinataire : {admin_email}")
|
||||
|
||||
# Cas de test
|
||||
test_reports = {
|
||||
"Service client décevant": "positif",
|
||||
"Erreur à l'embarquement": "negatif",
|
||||
"Vol annulé sans prévenir": "positif"
|
||||
}
|
||||
|
||||
# 📧 Tester la fonction d'envoi
|
||||
# Appel de la fonction
|
||||
result = send_error_report_email(test_reports)
|
||||
|
||||
# ✅ Assertion pour vérifier que la fonction fonctionne correctement
|
||||
assert result is True, "L'email n'a pas été envoyé avec succès"
|
||||
# ✅ Assertions
|
||||
assert result is True
|
||||
mock_smtp.assert_called_with(os.getenv("SMTP_SERVER"), int(os.getenv("SMTP_PORT", 587)))
|
||||
mock_server.starttls.assert_called_once()
|
||||
mock_server.login.assert_called_once_with(os.getenv("SMTP_EMAIL"), os.getenv("SMTP_PASSWORD"))
|
||||
mock_server.send_message.assert_called_once()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue