diff -r a765a06383af Lib/test/test_smtpd.py --- a/Lib/test/test_smtpd.py Mon Aug 22 18:12:39 2011 +1000 +++ b/Lib/test/test_smtpd.py Mon Aug 22 18:41:39 2011 +1000 @@ -21,6 +21,27 @@ # Pick a non-privileged port to bind to SMTP_DEFAULT_PORT = 4731 +class MockSMTP(SMTP): + ''' + Obviously when testing delivery we don't want to get coupled + to a running remote SMTP server + ''' + raise_on_connect = None + + def connect(self, remote_hostname, remote_port): + + if MockSMTP.raise_on_connect: + raise MockSMTP.raise_on_connect + + pass + + @classmethod + def set_raise_on_connect(cls, exc): + ''' + Allow this to test both success and failure of connection + ''' + MockSMTP.raise_on_connect = exc + class DummyServer(smtpd.SMTPServer): def __init__(self, localaddr, remoteaddr): @@ -304,30 +325,8 @@ ''' Don't go round binding to actual ports during testing ''' - smtpd.socket = asyncore.socket = mock_socket - - class MockSMTP(SMTP): - ''' - Obviously when testing delivery we don't want to get coupled - to a running remote SMTP server - ''' - raise_on_connect = None - - def connect(self, remote_hostname, remote_port): - - if MockSMTP.raise_on_connect: - raise MockSMTP.raise_on_connect - - pass - - @classmethod - def set_raise_on_connect(cls, exc): - ''' - Allow this to test both success and failure of connection - ''' - MockSMTP.raise_on_connect = exc - - smtplib.SMTP = MockSMTP + smtpd.socket = asyncore.socket = mock_socket + smtplib.SMTP = MockSMTP def tearDown(self): ''' @@ -414,10 +413,59 @@ refusingServer.process_message(peer, mailfrom, rcpttos, SAMPLE_MESSAGE_DATA) +class MailmanProxyTest(TestCase): + + def setUp(self): + ''' + Don't go round binding to actual ports during testing + ''' + smtpd.socket = asyncore.socket = mock_socket + smtplib.SMTP = MockSMTP + + def tearDown(self): + ''' + Leave the smtp ports as we found them + ''' + asyncore.close_all() + asyncore.socket = smtpd.socket = socket + + smtplib.SMTP = SMTP + + def test_process_message(self): + + class MockMailmanProxy(smtpd.MailmanProxy): + + def _deliver(self, mailfrom, rcpttos, data): + return {} + + class RefusingMailmanProxy(smtpd.MailmanProxy): + + def _deliver(self, mailfrom, rcpttos, data): + return "A fake refusal" + + localaddress = ('localhost', SMTP_DEFAULT_PORT) + remoteaddress = ('localhost', SMTP_DEFAULT_PORT + 1) + theServer = MockMailmanProxy(localaddress, remoteaddress) + + # Add the X-Peer header that DebuggingServer adds + peer = socket.gethostbyname('localhost') + + # Send a mail from myself + mailfrom = 'tleeuwenburg@gmail.com' + + # To myself + rcpttos = 'tleeuwenburg@gmail.com' + empty_message_data = '' + + # Test on plausible data + theServer.process_message(peer, mailfrom, rcpttos, SAMPLE_MESSAGE_DATA) + + def test_main(): support.run_unittest(SMTPDServerTest, SMTPDChannelTest, - PureProxyTest) + PureProxyTest, + MailmanProxyTest) if __name__ == "__main__": test_main()