diff options
Diffstat (limited to 'src/mailman/mta/tests')
| -rw-r--r-- | src/mailman/mta/tests/test_base.py | 71 | ||||
| -rw-r--r-- | src/mailman/mta/tests/test_connection.py | 89 |
2 files changed, 143 insertions, 17 deletions
diff --git a/src/mailman/mta/tests/test_base.py b/src/mailman/mta/tests/test_base.py new file mode 100644 index 000000000..52fa63430 --- /dev/null +++ b/src/mailman/mta/tests/test_base.py @@ -0,0 +1,71 @@ +# Copyright (C) 2014-2017 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# GNU Mailman. If not, see <http://www.gnu.org/licenses/>. + +"""Test BaseDelivery.""" + +import unittest + +from mailman.config import config +from mailman.interfaces.configuration import InvalidConfigurationError +from mailman.mta.base import BaseDelivery +from mailman.mta.connection import SMTPSConnection, STARTTLSConnection +from mailman.testing.layers import SMTPLayer, SMTPSLayer, STARTTLSLayer + + +class BaseDeliveryTester(BaseDelivery): + @property + def connection(self): + return self._connection + + +class TestSMTPSDelivery(unittest.TestCase): + layer = SMTPSLayer + + def test_smtps_config(self): + config.push('smtps_config', """\ +[mta] +smtp_protocol: smtps +""") + delivery = BaseDeliveryTester() + self.assertIsInstance(delivery.connection, SMTPSConnection) + config.pop('smtps_config') + + +class TestSTARTTLSDelivery(unittest.TestCase): + layer = STARTTLSLayer + + def test_starttls_config(self): + config.push('starttls_config', """\ +[mta] +smtp_protocol: starttls +""") + delivery = BaseDeliveryTester() + self.assertIsInstance(delivery.connection, STARTTLSConnection) + config.pop('starttls_config') + + +class TestInvalidDelivery(unittest.TestCase): + layer = SMTPLayer + + def test_invalid_config(self): + config.push('invalid_config', """\ +[mta] +smtp_protocol: invalid +""") + with self.assertRaises(InvalidConfigurationError): + BaseDeliveryTester() + config.pop('invalid_config') 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() |
