|
| 1 | +"""Tests relating to the module smtp.smtp_util.""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from io import BytesIO |
| 5 | +import os |
| 6 | + |
| 7 | +import requests |
| 8 | + |
| 9 | +from itk_dev_shared_components.smtp import smtp_util |
| 10 | +from itk_dev_shared_components.smtp.smtp_util import EmailAttachment |
| 11 | + |
| 12 | + |
| 13 | +class TestTreeUtil(unittest.TestCase): |
| 14 | + """Tests relating to the module smtp.smtp_util.""" |
| 15 | + smtp_server = os.getenv("mailpit_host", "localhost") |
| 16 | + smtp_port = os.getenv("mailpit_smtp_port", "1025") |
| 17 | + http_port = os.getenv("mailpit_http_port", "8025") |
| 18 | + |
| 19 | + def setUp(self) -> None: |
| 20 | + url = url = f"http://localhost:{self.http_port}/api/v1/messages" |
| 21 | + response = requests.delete(url, timeout=2) |
| 22 | + response.raise_for_status() |
| 23 | + |
| 24 | + def test_send_simple(self): |
| 25 | + """Test sending a simple email.""" |
| 26 | + smtp_util.send_email("test@receiver.com", "idsc@test.dk", "Test simple", "Test body", smtp_server=self.smtp_server, smtp_port=self.smtp_port) |
| 27 | + |
| 28 | + message = self.get_message("Test simple") |
| 29 | + self.assertEqual(message["From"]["Address"], "idsc@test.dk") |
| 30 | + self.assertEqual(message["To"][0]["Address"], "test@receiver.com") |
| 31 | + self.assertEqual(message["Snippet"], "Test body") |
| 32 | + |
| 33 | + def test_send_html(self): |
| 34 | + """Test sending an html email.""" |
| 35 | + html = "<html><head><style> table {font-family: arial, sans-serif; border-collapse: collapse; width: 100%;} td, th { border: 1px solid #dddddd; text-align: left; padding: 8px;}</style></head><body><h2>HTML Table</h2><table><tr><th>Company</th><th>Contact</th><th>Country</th></tr><tr><td>Alfreds Futterkiste</td><td>Maria Anders</td><td>Germany</td></tr><tr><td>Centro comercial Moctezuma</td><td>Francisco Chang</td><td>Mexico</td></tr></table></body></html>" |
| 36 | + smtp_util.send_email("test@receiver.com", "idsc@test.dk", "Test html", html, html_body=True, smtp_server=self.smtp_server, smtp_port=self.smtp_port) |
| 37 | + |
| 38 | + message = self.get_message("Test html") |
| 39 | + self.assertTrue(message["Snippet"].startswith("HTML Table")) |
| 40 | + |
| 41 | + def test_send_attachments(self): |
| 42 | + """Test sending an email with multiple attachments.""" |
| 43 | + attachments = [] |
| 44 | + |
| 45 | + # Generate some attachment files |
| 46 | + for i in range(3): |
| 47 | + file = BytesIO(b"Hello"*(i+1)) |
| 48 | + file_name = f"file{i}.txt" |
| 49 | + |
| 50 | + attachments.append( |
| 51 | + EmailAttachment(file, file_name) |
| 52 | + ) |
| 53 | + |
| 54 | + smtp_util.send_email("test@receiver.com", "idsc@test.dk", "Test files", "This email has three attached txt files.", attachments=attachments, smtp_server=self.smtp_server, smtp_port=self.smtp_port) |
| 55 | + |
| 56 | + message = self.get_message("Test files") |
| 57 | + self.assertEqual(message["Attachments"], 3) |
| 58 | + |
| 59 | + def test_send_multiple(self): |
| 60 | + """Test sending to multiple receivers.""" |
| 61 | + smtp_util.send_email(["test@receiver.com", "test@receiver.com"], "idsc@test.dk", "Test multiple", "This email has multiple receivers.", smtp_server=self.smtp_server, smtp_port=self.smtp_port) |
| 62 | + |
| 63 | + message = self.get_message("Test multiple") |
| 64 | + self.assertEqual(len(message["To"]), 2) |
| 65 | + |
| 66 | + def get_message(self, subject: str): |
| 67 | + """Get a message from the Mailpit api with the given subject. |
| 68 | + https://mailpit.axllent.org/docs/api-v1/view.html#tag--messages |
| 69 | +
|
| 70 | + Args: |
| 71 | + subject: The email subject to search for. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + A dict representing the message from Mailpit. |
| 75 | + """ |
| 76 | + url = f"http://localhost:{self.http_port}/api/v1/messages" |
| 77 | + response = requests.get(url, timeout=2).json() |
| 78 | + |
| 79 | + message = None |
| 80 | + for msg in response["messages"]: |
| 81 | + if msg["Subject"] == subject: |
| 82 | + message = msg |
| 83 | + break |
| 84 | + |
| 85 | + self.assertIsNotNone(message) |
| 86 | + return message |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + unittest.main() |
0 commit comments