Update tests/test_utils_sendmail.py
Run CI Tests / test (push) Successful in 1m50s

This commit is contained in:
2025-07-30 20:57:22 +02:00
parent 89a56b7016
commit 4ba69f91ae
+17 -15
View File
@@ -7,44 +7,46 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')
from slic.utils.sendmail import sendmail
from slic.utils.sendmail import *
import os
import tempfile
from email.mime.text import MIMEText
from slic.utils.sendmail import sendmail
def test_sendmail_mime_format_to_file():
from_addr = "robot@local"
subject = "MIME Format Test"
body = "This is the MIME body."
# Create temporary file
with tempfile.NamedTemporaryFile("r+", delete=False) as tmp:
tmp_path = tmp.name
# Create the temporary file to capture output
with tempfile.NamedTemporaryFile("r+", delete=False) as tmp_file:
tmp_path = tmp_file.name
# Create a temporary shell script to act as a pipe receiver
with tempfile.NamedTemporaryFile("w", suffix=".sh", delete=False) as script_file:
script_path = script_file.name
script_file.write(f"#!/bin/sh\ncat > {tmp_path}\n")
os.chmod(script_path, 0o755) # Make the script executable
try:
# Send message
# Send email using the script as a fake receiver
sendmail(
to_addr=f"| cat > {tmp_path}",
to_addr=f"| {script_path}",
from_addr=from_addr,
subject=subject,
body=body
)
# Read file content
# Read captured output
with open(tmp_path, "r") as f:
output = f.read()
# Expected message
# Build expected MIME message
msg = MIMEText(body)
msg["To"] = f"| cat > {tmp_path}"
msg["To"] = f"| {script_path}"
msg["From"] = from_addr
msg["Subject"] = subject
expected = msg.as_string()
# Check that the good message is in the file
# Validate line-by-line
for line in expected.splitlines():
assert line in output, f"Missing line in output: {line!r}"
finally:
# Cleanup temp files
os.remove(tmp_path)
os.remove(script_path)