This commit is contained in:
@@ -3,100 +3,42 @@ import tempfile
|
||||
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 sendmail
|
||||
from slic.utils.sendmail import *
|
||||
import shutil
|
||||
import getpass
|
||||
import time
|
||||
import glob
|
||||
|
||||
'''
|
||||
def test_sendmail_mime_format_to_file():
|
||||
from_addr = "robot@local"
|
||||
subject = "MIME Format Test"
|
||||
body = "This is the MIME body."
|
||||
def test_sendmail_local_delivery():
|
||||
|
||||
# Create the temporary file to capture output
|
||||
with tempfile.NamedTemporaryFile("r+", delete=False) as tmp_file:
|
||||
tmp_path = tmp_file.name
|
||||
# Get the current system user (will be the local mailbox owner)
|
||||
user = getpass.getuser()
|
||||
|
||||
# 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
|
||||
# Define the recipient and email content
|
||||
to_addr = f"{user}@localhost"
|
||||
subject = "Local sendmail test"
|
||||
body = "Hello from pytest/local!"
|
||||
|
||||
try:
|
||||
# Send email using the script as a fake receiver
|
||||
sendmail(
|
||||
to_addr=f"| {script_path}",
|
||||
from_addr=from_addr,
|
||||
subject=subject,
|
||||
body=body
|
||||
)
|
||||
# Send the email using the function under test
|
||||
sendmail(to_addr, from_addr=f"{user}@localhost", subject=subject, body=body)
|
||||
|
||||
# Read captured output
|
||||
with open(tmp_path, "r") as f:
|
||||
output = f.read()
|
||||
# Path to the Maildir 'new' folder for this user
|
||||
maildir_new = os.path.expanduser("~/Maildir/new")
|
||||
|
||||
# Build expected MIME message
|
||||
msg = MIMEText(body)
|
||||
msg["To"] = f"| {script_path}"
|
||||
msg["From"] = from_addr
|
||||
msg["Subject"] = subject
|
||||
expected = msg.as_string()
|
||||
# Wait a short time for delivery (Postfix is fast, but we ensure robustness)
|
||||
files = []
|
||||
for _ in range(50): # Try for ~5 seconds
|
||||
time.sleep(0.1)
|
||||
files = sorted(glob.glob(os.path.join(maildir_new, "*")))
|
||||
if files:
|
||||
break
|
||||
|
||||
# Validate line-by-line
|
||||
for line in expected.splitlines():
|
||||
assert line in output, f"Missing line in output: {line!r}"
|
||||
# Assert that at least one mail file was delivered
|
||||
assert files, f"No email was delivered to {maildir_new}"
|
||||
|
||||
finally:
|
||||
# Cleanup temp files
|
||||
os.remove(tmp_path)
|
||||
os.remove(script_path)
|
||||
'''
|
||||
# Read the most recent mail file
|
||||
with open(files[-1], "r", encoding="utf-8", errors="replace") as f:
|
||||
content = f.read()
|
||||
|
||||
import subprocess
|
||||
|
||||
def start_postfix():
|
||||
try:
|
||||
subprocess.run(["sudo", "service", "postfix", "start"], check=True)
|
||||
print("✅ Postfix started")
|
||||
except subprocess.CalledProcessError as e:
|
||||
print("❌ Failed to start postfix:", e)
|
||||
|
||||
def setup_postfix():
|
||||
subprocess.run("echo 'postfix postfix/mailname string localhost.localdomain' | sudo debconf-set-selections", shell=True)
|
||||
subprocess.run("echo 'postfix postfix/main_mailer_type string Internet Site' | sudo debconf-set-selections", shell=True)
|
||||
subprocess.run("sudo DEBIAN_FRONTEND=noninteractive apt-get install -y postfix", shell=True)
|
||||
|
||||
|
||||
def test_sendmail_real_local_verbose():
|
||||
start_postfix()
|
||||
setup_postfix()
|
||||
|
||||
to_addr = "yasmine.tligui@psi.ch"
|
||||
subject = "Test mail from pytest"
|
||||
body = "Hello! This is a real test sent using local sendmail."
|
||||
|
||||
message = f"""Subject: {subject}
|
||||
From: test@localhost
|
||||
To: {to_addr}
|
||||
|
||||
{body}
|
||||
"""
|
||||
|
||||
print("=== SENDING MAIL ===")
|
||||
process = subprocess.Popen(
|
||||
["/usr/sbin/sendmail", "-v", to_addr],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
)
|
||||
stdout, stderr = process.communicate(message.encode())
|
||||
|
||||
print("\n=== SENDMAIL STDOUT ===")
|
||||
print(stdout.decode())
|
||||
|
||||
print("\n=== SENDMAIL STDERR ===")
|
||||
print(stderr.decode())
|
||||
|
||||
|
||||
|
||||
# Assert that both the subject and body appear in the delivered email
|
||||
assert subject in content, "Email subject not found in delivered message"
|
||||
assert body in content, "Email body not found in delivered message"
|
||||
Reference in New Issue
Block a user