3
0
Fork 0

feat: add minimal attachments support for email messages

as of now, caller must provide a fully proper MIME attachment.  later
we will add some magic normalization logic so caller can just provide
attachment file paths
This commit is contained in:
Lance Edgar 2025-08-10 11:03:59 -05:00
parent 763dd510f6
commit 22d3ba97c9
2 changed files with 36 additions and 2 deletions

View file

@ -1,5 +1,6 @@
# -*- coding: utf-8; -*-
from email.mime.text import MIMEText
from unittest import TestCase
from unittest.mock import patch, MagicMock
@ -8,7 +9,7 @@ import pytest
from wuttjamaican import email as mod
from wuttjamaican.util import resource_path
from wuttjamaican.exc import ConfigurationError
from wuttjamaican.testing import ConfigTestCase
from wuttjamaican.testing import ConfigTestCase, FileTestCase
class TestEmailSetting(ConfigTestCase):
@ -24,7 +25,7 @@ class TestEmailSetting(ConfigTestCase):
self.assertEqual(setting.sample_data(), {})
class TestMessage(TestCase):
class TestMessage(FileTestCase):
def make_message(self, **kwargs):
return mod.Message(**kwargs)
@ -77,6 +78,26 @@ class TestMessage(TestCase):
complete = msg.as_string()
self.assertIn('From: bob@example.com', complete)
# html + attachment
csv_part = MIMEText("foo,bar\n1,2", 'csv', 'utf_8')
msg = self.make_message(sender='bob@example.com',
html_body="<p>hello world</p>",
attachments=[csv_part])
complete = msg.as_string()
self.assertIn('Content-Type: multipart/mixed; boundary=', complete)
self.assertIn('Content-Type: text/csv; charset="utf_8"', complete)
# error if improper attachment
csv_path = self.write_file('data.csv', "foo,bar\n1,2")
msg = self.make_message(sender='bob@example.com',
html_body="<p>hello world</p>",
attachments=[csv_path])
self.assertRaises(ValueError, msg.as_string)
try:
msg.as_string()
except ValueError as err:
self.assertIn("must specify valid MIME attachments", str(err))
# everything
msg = self.make_message(sender='bob@example.com',
subject='meeting follow-up',