summaryrefslogtreecommitdiff
path: root/Lib/test/test_smtplib.py
diff options
context:
space:
mode:
authorPablo Aguiar <scorphus@gmail.com>2018-09-08 00:04:48 +0200
committerPablo Galindo <Pablogsal@gmail.com>2018-09-07 23:04:48 +0100
commitd5fbe9b1a3d65ceeb9159c5ba999ee966a945f76 (patch)
treef2124d01569a59c8bc937f02a0954b9f2f807e1f /Lib/test/test_smtplib.py
parent4e519377b1b84c9414a360961276993d24198825 (diff)
downloadcpython-git-d5fbe9b1a3d65ceeb9159c5ba999ee966a945f76.tar.gz
bpo-34246: Use no mutable default args in smtplib (GH-8554)
Some methods of the SMTP class use mutable default arguments. Specially `send_message` is affected as it mutates one of the args by appending items to it, which has side effects on further calls.
Diffstat (limited to 'Lib/test/test_smtplib.py')
-rw-r--r--Lib/test/test_smtplib.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
index 495764f9ac..8a29e98a4f 100644
--- a/Lib/test/test_smtplib.py
+++ b/Lib/test/test_smtplib.py
@@ -20,6 +20,7 @@ import threading
import unittest
from test import support, mock_socket
from test.support import HOST, HOSTv4, HOSTv6
+from unittest.mock import Mock
if sys.platform == 'darwin':
@@ -578,6 +579,33 @@ class NonConnectingTests(unittest.TestCase):
"localhost:bogus")
+class DefaultArgumentsTests(unittest.TestCase):
+
+ def setUp(self):
+ self.msg = EmailMessage()
+ self.msg['From'] = 'Páolo <főo@bar.com>'
+ self.smtp = smtplib.SMTP()
+ self.smtp.ehlo = Mock(return_value=(200, 'OK'))
+ self.smtp.has_extn, self.smtp.sendmail = Mock(), Mock()
+
+ def testSendMessage(self):
+ expected_mail_options = ('SMTPUTF8', 'BODY=8BITMIME')
+ self.smtp.send_message(self.msg)
+ self.smtp.send_message(self.msg)
+ self.assertEqual(self.smtp.sendmail.call_args_list[0][0][3],
+ expected_mail_options)
+ self.assertEqual(self.smtp.sendmail.call_args_list[1][0][3],
+ expected_mail_options)
+
+ def testSendMessageWithMailOptions(self):
+ mail_options = ['STARTTLS']
+ expected_mail_options = ('STARTTLS', 'SMTPUTF8', 'BODY=8BITMIME')
+ self.smtp.send_message(self.msg, None, None, mail_options)
+ self.assertEqual(mail_options, ['STARTTLS'])
+ self.assertEqual(self.smtp.sendmail.call_args_list[0][0][3],
+ expected_mail_options)
+
+
# test response of client to a non-successful HELO message
class BadHELOServerTests(unittest.TestCase):