summaryrefslogtreecommitdiff
path: root/src/mailman/mta/tests/test_connection.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman/mta/tests/test_connection.py')
-rw-r--r--src/mailman/mta/tests/test_connection.py89
1 files changed, 72 insertions, 17 deletions
diff --git a/src/mailman/mta/tests/test_connection.py b/src/mailman/mta/tests/test_connection.py
index b742f7abc..566e19daa 100644
--- a/src/mailman/mta/tests/test_connection.py
+++ b/src/mailman/mta/tests/test_connection.py
@@ -20,9 +20,11 @@
import unittest
from mailman.config import config
-from mailman.mta.connection import Connection
-from mailman.testing.layers import SMTPLayer
-from smtplib import SMTP, SMTPAuthenticationError
+from mailman.mta.connection import (
+ SMTPConnection, SMTPSConnection, STARTTLSConnection)
+from mailman.testing.helpers import LogFileMark
+from mailman.testing.layers import SMTPLayer, SMTPSLayer, STARTTLSLayer
+from smtplib import SMTPAuthenticationError
class TestConnection(unittest.TestCase):
@@ -32,7 +34,7 @@ class TestConnection(unittest.TestCase):
# Logging in to the MTA with a bad user name and password produces a
# 571 Bad Authentication error.
with self.assertRaises(SMTPAuthenticationError) as cm:
- connection = Connection(
+ connection = SMTPConnection(
config.mta.smtp_host, int(config.mta.smtp_port), 0,
'baduser', 'badpass')
connection.sendmail('anne@example.com', ['bart@example.com'], """\
@@ -46,7 +48,7 @@ Subject: aardvarks
def test_authentication_good_path(self):
# Logging in with the correct user name and password succeeds.
- connection = Connection(
+ connection = SMTPConnection(
config.mta.smtp_host, int(config.mta.smtp_port), 0,
'testuser', 'testpass')
connection.sendmail('anne@example.com', ['bart@example.com'], """\
@@ -59,27 +61,31 @@ Subject: aardvarks
'AHRlc3R1c2VyAHRlc3RwYXNz')
-class TestConnectionCount(unittest.TestCase):
- layer = SMTPLayer
+class _ConnectionCounter:
+ layer = None
def setUp(self):
- self.connection = Connection(
- config.mta.smtp_host, int(config.mta.smtp_port), 0)
+ self.connection = None
self.msg_text = """\
From: anne@example.com
To: bart@example.com
Subject: aardvarks
-
"""
def test_count_0(self):
# So far, no connections.
- self.assertEqual(SMTPLayer.smtpd.get_connection_count(), 0)
+ unittest.TestCase.assertEqual(self,
+ self.layer.smtpd.get_connection_count(),
+ 0)
+ pass
def test_count_1(self):
self.connection.sendmail(
'anne@example.com', ['bart@example.com'], self.msg_text)
- self.assertEqual(SMTPLayer.smtpd.get_connection_count(), 1)
+ unittest.TestCase.assertEqual(self,
+ self.layer.smtpd.get_connection_count(),
+ 1)
+ pass
def test_count_2(self):
self.connection.sendmail(
@@ -88,7 +94,10 @@ Subject: aardvarks
self.connection.sendmail(
'cate@example.com', ['dave@example.com'], self.msg_text)
self.connection.quit()
- self.assertEqual(SMTPLayer.smtpd.get_connection_count(), 2)
+ unittest.TestCase.assertEqual(self,
+ self.layer.smtpd.get_connection_count(),
+ 2)
+ pass
def test_count_reset(self):
self.connection.sendmail(
@@ -98,7 +107,53 @@ Subject: aardvarks
'cate@example.com', ['dave@example.com'], self.msg_text)
self.connection.quit()
# Issue the fake SMTP command to reset the count.
- client = SMTP()
- client.connect(config.mta.smtp_host, int(config.mta.smtp_port))
- client.docmd('RSET')
- self.assertEqual(SMTPLayer.smtpd.get_connection_count(), 0)
+ self.layer.smtpd.reset()
+ unittest.TestCase.assertEqual(self,
+ self.layer.smtpd.get_connection_count(),
+ 0)
+
+
+class TestSMTPConnectionCount(_ConnectionCounter, unittest.TestCase):
+ layer = SMTPLayer
+
+ def setUp(self):
+ super().setUp()
+ self.connection = SMTPConnection(
+ config.mta.smtp_host, int(config.mta.smtp_port), 0)
+
+
+class TestSMTPSConnectionCount(_ConnectionCounter, unittest.TestCase):
+ layer = SMTPSLayer
+
+ def setUp(self):
+ super().setUp()
+ self.connection = SMTPSConnection(
+ config.mta.smtp_host, int(config.mta.smtp_port), 0)
+
+
+class TestSTARTTLSConnectionCount(_ConnectionCounter, unittest.TestCase):
+ layer = STARTTLSLayer
+
+ def setUp(self):
+ super().setUp()
+ self.connection = STARTTLSConnection(
+ config.mta.smtp_host, int(config.mta.smtp_port), 0)
+
+
+class TestSTARTTLSNotSupported(unittest.TestCase):
+ layer = SMTPLayer
+
+ def test_not_supported(self):
+ connection = STARTTLSConnection(
+ config.mta.smtp_host, int(config.mta.smtp_port), 0)
+ msg_text = """\
+From: anne@example.com
+To: bart@example.com
+Subject: aardvarks
+"""
+ smtp_log = LogFileMark('mailman.smtp')
+ connection.sendmail(
+ 'anne@example.com', ['bart@example.com'], msg_text)
+ lines = smtp_log.read()
+ self.assertIn('Starttls failed', lines)
+ connection.quit()