|
23 | 23 | from test.support import threading_helper |
24 | 24 | from test.support import asyncore |
25 | 25 | from test.support import smtpd |
26 | | -from unittest.mock import Mock, patch |
| 26 | +from unittest.mock import Mock |
27 | 27 |
|
28 | 28 |
|
29 | 29 | support.requires_working_socket(module=True) |
@@ -1393,6 +1393,11 @@ class SMTPUTF8SimTests(unittest.TestCase): |
1393 | 1393 |
|
1394 | 1394 | maxDiff = None |
1395 | 1395 |
|
| 1396 | + def _is_local(self, date: str) -> bool: |
| 1397 | + dt = email.utils.parsedate_to_datetime(date) |
| 1398 | + local_offset = dt.now().astimezone().utcoffset() |
| 1399 | + return dt.utcoffset() == local_offset |
| 1400 | + |
1396 | 1401 | def setUp(self): |
1397 | 1402 | self.thread_key = threading_helper.threading_setup() |
1398 | 1403 | self.real_getfqdn = socket.getfqdn |
@@ -1468,40 +1473,44 @@ def test_send_unicode_with_SMTPUTF8_via_low_level_API(self): |
1468 | 1473 | self.assertEqual(self.serv.last_rcpt_options, []) |
1469 | 1474 |
|
1470 | 1475 | def test_send_message_uses_smtputf8_if_addrs_non_ascii(self): |
1471 | | - expected_date = "Thu, 19 Mar 2020 00:59:43 -0000" |
1472 | | - with patch("email.utils.formatdate") as date_mock: |
1473 | | - date_mock.return_value = expected_date |
1474 | | - msg = EmailMessage() |
1475 | | - msg[ 'From'] = "Páolo <fő[email protected]>" |
1476 | | - msg['To'] = 'Dinsdale' |
1477 | | - msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' |
1478 | | - # XXX I don't know why I need two \n's here, but this is an existing |
1479 | | - # bug (if it is one) and not a problem with the new functionality. |
1480 | | - msg.set_content("oh là là, know what I mean, know what I mean?\n\n") |
1481 | | - # XXX smtpd converts received /r/n to /n, so we can't easily test that |
1482 | | - # we are successfully sending /r/n :(. |
1483 | | - expected = textwrap.dedent("""\ |
1484 | | - |
1485 | | - To: Dinsdale |
1486 | | - Subject: Nudge nudge, wink, wink \u1F609 |
1487 | | - Content-Type: text/plain; charset="utf-8" |
1488 | | - Content-Transfer-Encoding: 8bit |
1489 | | - MIME-Version: 1.0 |
1490 | | - Date: {} |
1491 | | -
|
1492 | | - oh là là, know what I mean, know what I mean? |
1493 | | - """.format(expected_date)) |
1494 | | - smtp = smtplib.SMTP( |
1495 | | - HOST, self.port, local_hostname='localhost', |
1496 | | - timeout=support.LOOPBACK_TIMEOUT) |
1497 | | - self.addCleanup(smtp.close) |
1498 | | - self.assertEqual(smtp.send_message(msg), {}) |
1499 | | - self. assertEqual( self. serv. last_mailfrom, 'fő[email protected]') |
1500 | | - self.assertEqual(self.serv.last_rcpttos, ['Dinsdale']) |
1501 | | - self.assertEqual(self.serv.last_message.decode(), expected) |
1502 | | - self.assertIn('BODY=8BITMIME', self.serv.last_mail_options) |
1503 | | - self.assertIn('SMTPUTF8', self.serv.last_mail_options) |
1504 | | - self.assertEqual(self.serv.last_rcpt_options, []) |
| 1476 | + msg = EmailMessage() |
| 1477 | + msg[ 'From'] = "Páolo <fő[email protected]>" |
| 1478 | + msg['To'] = 'Dinsdale' |
| 1479 | + msg['Subject'] = 'Nudge nudge, wink, wink \u1F609' |
| 1480 | + # XXX I don't know why I need two \n's here, but this is an existing |
| 1481 | + # bug (if it is one) and not a problem with the new functionality. |
| 1482 | + msg.set_content("oh là là, know what I mean, know what I mean?\n\n") |
| 1483 | + # XXX smtpd converts received /r/n to /n, so we can't easily test that |
| 1484 | + # we are successfully sending /r/n :(. |
| 1485 | + smtp = smtplib.SMTP( |
| 1486 | + HOST, self.port, local_hostname='localhost', |
| 1487 | + timeout=support.LOOPBACK_TIMEOUT) |
| 1488 | + self.addCleanup(smtp.close) |
| 1489 | + self.assertEqual(smtp.send_message(msg), {}) |
| 1490 | + self. assertEqual( self. serv. last_mailfrom, 'fő[email protected]') |
| 1491 | + self.assertEqual(self.serv.last_rcpttos, ['Dinsdale']) |
| 1492 | + |
| 1493 | + last_message = self.serv.last_message.decode() |
| 1494 | + date = email.message_from_string(last_message)['Date'] |
| 1495 | + # asserts RFC 5322 section 3.3 4th Paragraph |
| 1496 | + self.assertTrue(self._is_local(date)) |
| 1497 | + |
| 1498 | + expected = textwrap.dedent("""\ |
| 1499 | + |
| 1500 | + To: Dinsdale |
| 1501 | + Subject: Nudge nudge, wink, wink \u1F609 |
| 1502 | + Content-Type: text/plain; charset="utf-8" |
| 1503 | + Content-Transfer-Encoding: 8bit |
| 1504 | + MIME-Version: 1.0 |
| 1505 | + Date: {} |
| 1506 | +
|
| 1507 | + oh là là, know what I mean, know what I mean? |
| 1508 | + """.format(date)) |
| 1509 | + |
| 1510 | + self.assertEqual(last_message, expected) |
| 1511 | + self.assertIn('BODY=8BITMIME', self.serv.last_mail_options) |
| 1512 | + self.assertIn('SMTPUTF8', self.serv.last_mail_options) |
| 1513 | + self.assertEqual(self.serv.last_rcpt_options, []) |
1505 | 1514 |
|
1506 | 1515 |
|
1507 | 1516 | EXPECTED_RESPONSE = encode_base64(b'\0psu\0doesnotexist', eol='') |
|
0 commit comments