diff --git a/tests/test_utils_sendmail.py b/tests/test_utils_sendmail.py new file mode 100644 index 00000000..8bb00055 --- /dev/null +++ b/tests/test_utils_sendmail.py @@ -0,0 +1,42 @@ +import os +import subprocess +import getpass +from email.mime.text import MIMEText +import sys +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +from slic.utils.sendmail import * + +def test_sendmail_mime_format_in_tmux(): + # Retrieve the tmux TTY from the environment + tmux_tty = os.environ.get("TMUX_TTY") + assert tmux_tty and os.path.exists(tmux_tty), f"Invalid TMUX_TTY: {tmux_tty}" + + # Define test message parameters + from_addr = "robot@local" + subject = "MIME Format Test" + body = "This is the MIME body." + + # Send the message via your sendmail() + sendmail( + to_addr=f"| tee {tmux_tty}", # Write the message to the tmux terminal + from_addr=from_addr, + subject=subject, + body=body + ) + + # Capture the content from the tmux terminal + output = subprocess.check_output( + ["tmux", "capture-pane", "-pt", "test-session"], + timeout=2 + ).decode() + + # Build the expected MIME-formatted message + msg = MIMEText(body) + msg["To"] = to_addr + msg["From"] = from_addr or getpass.getuser() + msg["Subject"] = subject + expected = msg.as_string() + + # Verify that each line of the expected MIME message is present in the tmux output + for line in expected.splitlines(): + assert line in output, f"Missing line in tmux output: {line!r}" \ No newline at end of file