This commit is contained in:
@@ -7,39 +7,44 @@ 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 sendmail
|
||||||
from slic.utils.sendmail import *
|
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_in_tmux():
|
def test_sendmail_mime_format_to_file():
|
||||||
# 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
|
|
||||||
to_addr=f"| tee {tmux_tty}"
|
|
||||||
from_addr = "robot@local"
|
from_addr = "robot@local"
|
||||||
subject = "MIME Format Test"
|
subject = "MIME Format Test"
|
||||||
body = "This is the MIME body."
|
body = "This is the MIME body."
|
||||||
|
|
||||||
# Send the message via your sendmail()
|
# Create temporary file
|
||||||
sendmail(
|
with tempfile.NamedTemporaryFile("r+", delete=False) as tmp:
|
||||||
to_addr=f"| tee {tmux_tty}", # Write the message to the tmux terminal
|
tmp_path = tmp.name
|
||||||
from_addr=from_addr,
|
|
||||||
subject=subject,
|
|
||||||
body=body
|
|
||||||
)
|
|
||||||
|
|
||||||
# Capture the content from the tmux terminal
|
try:
|
||||||
output = subprocess.check_output(
|
# Send message
|
||||||
["tmux", "capture-pane", "-pt", "test-session"],
|
sendmail(
|
||||||
timeout=2
|
to_addr=f"| tee {tmp_path}",
|
||||||
).decode()
|
from_addr=from_addr,
|
||||||
|
subject=subject,
|
||||||
|
body=body
|
||||||
|
)
|
||||||
|
|
||||||
# Build the expected MIME-formatted message
|
# Read file content
|
||||||
msg = MIMEText(body)
|
with open(tmp_path, "r") as f:
|
||||||
msg["To"] = to_addr
|
output = f.read()
|
||||||
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
|
# Expected message
|
||||||
for line in expected.splitlines():
|
msg = MIMEText(body)
|
||||||
assert line in output, f"Missing line in tmux output: {line!r}"
|
msg["To"] = f"| tee {tmp_path}"
|
||||||
|
msg["From"] = from_addr
|
||||||
|
msg["Subject"] = subject
|
||||||
|
expected = msg.as_string()
|
||||||
|
|
||||||
|
# Check that the good message is in the file
|
||||||
|
for line in expected.splitlines():
|
||||||
|
assert line in output, f"Missing line in output: {line!r}"
|
||||||
|
|
||||||
|
finally:
|
||||||
|
os.remove(tmp_path)
|
||||||
|
|||||||
Reference in New Issue
Block a user