Skip to content

Commit 0e6ec22

Browse files
committed
update tests to not patch date
1 parent f960ab2 commit 0e6ec22

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

Lib/smtplib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,8 @@ def send_message(self, msg, from_addr=None, to_addrs=None,
938938

939939
# RFC 5322 section 3.6, 4th Paragraph
940940
if msg.get('Date', None) is None:
941-
msg['Date'] = email.utils.formatdate()
941+
# localtime: RFC 5322 section 3.3 4th Paragraph
942+
msg['Date'] = email.utils.formatdate(localtime=True)
942943
if from_addr is None:
943944
# Prefer the sender field per RFC 2822:3.6.2.
944945
from_addr = (msg[header_prefix + 'Sender']

Lib/test/test_smtplib.py

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from test.support import threading_helper
2424
from test.support import asyncore
2525
from test.support import smtpd
26-
from unittest.mock import Mock, patch
26+
from unittest.mock import Mock
2727

2828

2929
support.requires_working_socket(module=True)
@@ -1393,6 +1393,11 @@ class SMTPUTF8SimTests(unittest.TestCase):
13931393

13941394
maxDiff = None
13951395

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+
13961401
def setUp(self):
13971402
self.thread_key = threading_helper.threading_setup()
13981403
self.real_getfqdn = socket.getfqdn
@@ -1468,40 +1473,44 @@ def test_send_unicode_with_SMTPUTF8_via_low_level_API(self):
14681473
self.assertEqual(self.serv.last_rcpt_options, [])
14691474

14701475
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-
From: Páolo <fő[email protected]>
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+
From: Páolo <fő[email protected]>
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, [])
15051514

15061515

15071516
EXPECTED_RESPONSE = encode_base64(b'\0psu\0doesnotexist', eol='')

0 commit comments

Comments
 (0)