Skip to content

Commit

Permalink
test_pyemail.py : added more tests to improve coverage from 76% to 89%
Browse files Browse the repository at this point in the history
Signed-off-by: SONIABHISHEK121 <[email protected]>
  • Loading branch information
ABHISHEKSONI121 committed Jul 10, 2024
1 parent 90ef00f commit 48a17e6
Showing 1 changed file with 130 additions and 20 deletions.
150 changes: 130 additions & 20 deletions tests/test_pyemail.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

from pyafipws.pyemail import PyEmail, main
import pytest
import traceback
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import sys, os
import sys
import os
import smtplib
import base64
from configparser import SafeConfigParser

pytestmark = [pytest.mark.dontusefix]
Expand All @@ -33,44 +31,48 @@
config.read("rece.ini")
conf_mail = dict(config.items("MAIL"))


def test_Connectar_Enviar(mocker):
"""Test de conexion"""
mocker.patch("smtplib.SMTP")
pyemail.Conectar(
conf_mail["servidor"],
conf_mail["usuario"],
conf_mail["clave"],
25,
)
pyemail.Enviar(
conf_mail["remitente"], "prueba", "[email protected]", None
conf_mail["servidor"],
conf_mail["usuario"],
conf_mail["clave"],
25,
)
pyemail.Enviar(conf_mail["remitente"], "prueba", "[email protected]", None)

pyemail.Salir()
pyemail.Salir()

smtplib.SMTP.assert_called_with(conf_mail["servidor"], 25)


def test_Crear():
ok =pyemail.Crear()
ok = pyemail.Crear()
assert ok


def test_Agreagar_Destinatario():
ok =pyemail.AgregarDestinatario("[email protected]")
ok = pyemail.AgregarDestinatario("[email protected]")
assert ok


def test_AgregarCC():
ok =pyemail.AgregarCC("[email protected]")
ok = pyemail.AgregarCC("[email protected]")
assert ok


def test_AgregarBCC():
ok =pyemail.AgregarBCC("[email protected]")
ok = pyemail.AgregarBCC("[email protected]")
assert ok


def test_Adjuntar():
ok =pyemail.Adjuntar("[email protected]")
ok = pyemail.Adjuntar("[email protected]")
assert ok


def test_main(mocker):
"""Test de funcion main"""
mocker.patch("smtplib.SMTP")
Expand All @@ -83,13 +85,121 @@ def test_main(mocker):

smtplib.SMTP.assert_called_with(conf_mail["servidor"], 25)

def test_main_prueba(mocker):

def test_main_prueba(mocker):
mocker.patch("smtplib.SMTP")
sys.argv = []
sys.argv.append("/prueba")
sys.argv.append("[email protected]")
sys.argv.append("pass123")
main()

smtplib.SMTP.assert_called_with('smtp.gmail.com', 587)
smtplib.SMTP.assert_called_with("smtp.gmail.com", 587)


def test_Conectar_SSL(mocker):
mocker.patch("smtplib.SMTP_SSL")
pyemail.Conectar(
conf_mail["servidor"], conf_mail["usuario"], conf_mail["clave"], 465
)
smtplib.SMTP_SSL.assert_called_with(conf_mail["servidor"], 465)


def test_Conectar_TLS(mocker):
mock_smtp = mocker.patch("smtplib.SMTP")
pyemail.Conectar(
conf_mail["servidor"], conf_mail["usuario"], conf_mail["clave"], 587
)
mock_smtp.return_value.starttls.assert_called_once()


def test_Conectar_Exception(mocker):
mocker.patch("smtplib.SMTP", side_effect=Exception("Connection error"))
result = pyemail.Conectar(
conf_mail["servidor"], conf_mail["usuario"], conf_mail["clave"], 25
)
assert result is False
assert pyemail.Excepcion != ""
assert pyemail.Traceback != ""


def test_Enviar_CC_BCC(mocker):
mocker.patch("smtplib.SMTP")
pyemail.Conectar(
conf_mail["servidor"], conf_mail["usuario"], conf_mail["clave"], 25
)
pyemail.CC = [] # Clear CC list
pyemail.BCC = [] # Clear BCC list
pyemail.AgregarCC("[email protected]")
pyemail.AgregarBCC("[email protected]")
pyemail.Enviar(conf_mail["remitente"], "prueba", "[email protected]", "Test message")
assert len(pyemail.CC) == 1
assert len(pyemail.BCC) == 1


def test_Salir_Exception(mocker):
mock_smtp = mocker.patch("smtplib.SMTP")
mock_smtp.return_value.quit.side_effect = Exception("Quit error")
pyemail.Conectar(
conf_mail["servidor"], conf_mail["usuario"], conf_mail["clave"], 25
)
result = pyemail.Salir()
assert result is False
assert pyemail.Excepcion != ""
assert pyemail.Traceback != ""


def test_Enviar_HTML(mocker):
mock_smtp = mocker.patch("smtplib.SMTP")
pyemail.Conectar(
conf_mail["servidor"], conf_mail["usuario"], conf_mail["clave"], 25
)
pyemail.MensajeHTML = "<html><body>Test HTML</body></html>"
pyemail.MensajeTexto = "Test plain text"
pyemail.Crear(conf_mail["remitente"], "prueba")
pyemail.AgregarDestinatario("[email protected]")
pyemail.Enviar()

assert mock_smtp.return_value.sendmail.called
call_args = mock_smtp.return_value.sendmail.call_args[0][2]

assert "Content-Type: multipart/related" in call_args
assert "Content-Type: multipart/alternative" in call_args
assert "Content-Type: text/text" in call_args
assert "Content-Type: text/html" in call_args
assert "Test plain text" in call_args
assert "<html><body>Test HTML</body></html>" in call_args

# Reset for next tests
pyemail.MensajeHTML = None
pyemail.MensajeTexto = None


def test_Enviar_Con_Adjunto(mocker):
mock_smtp = mocker.patch("smtplib.SMTP")
pyemail.Conectar(
conf_mail["servidor"], conf_mail["usuario"], conf_mail["clave"], 25
)
pyemail.Crear(conf_mail["remitente"], "prueba")
pyemail.AgregarDestinatario("[email protected]")

attachment_content = "This is a test attachment"
with open("test_attachment.txt", "w") as f:
f.write(attachment_content)

pyemail.Adjuntar("test_attachment.txt")
pyemail.MensajeTexto = "Test message"
pyemail.Enviar()

assert mock_smtp.return_value.sendmail.called
call_args = mock_smtp.return_value.sendmail.call_args[0][2]

assert (
'Content-Disposition: attachment; filename="test_attachment.txt"' in call_args
)
encoded_content = base64.b64encode(attachment_content.encode()).decode()
assert encoded_content in call_args

os.remove("test_attachment.txt")
pyemail.adjuntos = []
pyemail.MensajeTexto = None

0 comments on commit 48a17e6

Please sign in to comment.