44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
import os
|
|
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 *
|
|
import getpass
|
|
import time
|
|
import glob
|
|
|
|
def test_sendmail_local_delivery():
|
|
|
|
# Get the current system user (will be the local mailbox owner)
|
|
user = getpass.getuser()
|
|
|
|
# Define the recipient and email content
|
|
to_addr = f"{user}@localhost"
|
|
subject = "Local sendmail test"
|
|
body = "Hello from pytest/local!"
|
|
|
|
# Send the email using the function under test
|
|
sendmail(to_addr, from_addr=f"{user}@localhost", subject=subject, body=body)
|
|
|
|
# Path to the Maildir 'new' folder for this user
|
|
maildir_new = os.path.expanduser("~/Maildir/new")
|
|
|
|
# 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
|
|
|
|
# Assert that at least one mail file was delivered
|
|
assert files, f"No email was delivered to {maildir_new}"
|
|
|
|
# Read the most recent mail file
|
|
with open(files[-1], "r", encoding="utf-8", errors="replace") as f:
|
|
content = f.read()
|
|
|
|
# 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" |