class TestQueueProcessorThread(TestCase): def setUp(self): from zope.sendmail.queue import QueueProcessorThread self.md = MaildirStub('/foo/bar/baz') self.thread = QueueProcessorThread() self.thread.setMaildir(self.md) self.mailer = MailerStub() self.thread.setMailer(self.mailer) self.thread.log = LoggerStub() self.dir = mkdtemp() def tearDown(self): shutil.rmtree(self.dir) def test_parseMessage(self): hdr = ('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n') msg = ('Header: value\n' '\n' 'Body\n') f, t, m = self.thread._parseMessage(hdr + msg) self.assertEquals(f, '*****@*****.**') self.assertEquals(t, ('*****@*****.**', '*****@*****.**')) self.assertEquals(m, msg) def test_deliveration(self): self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) self.assertEquals(self.mailer.sent_messages, [('*****@*****.**', ('*****@*****.**', '*****@*****.**'), 'Header: value\n\nBody\n')]) self.failIf(os.path.exists(self.filename), 'File exists') self.assertEquals(self.thread.log.infos, [('Mail from %s to %s sent.', ('*****@*****.**', '[email protected], [email protected]'), {})]) def test_error_logging(self): self.thread.setMailer(BrokenMailerStub()) self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) self.assertEquals(self.thread.log.errors, [('Error while sending mail from %s to %s.', ('*****@*****.**', '[email protected], [email protected]'), {'exc_info': 1})]) def test_smtp_response_error_transient(self): # Test a transient error self.thread.setMailer(SMTPResponseExceptionMailerStub(451)) self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) # File must remail were it was, so it will be retried self.failUnless(os.path.exists(self.filename)) self.assertEquals(self.thread.log.errors, [('Error while sending mail from %s to %s.', ('*****@*****.**', '[email protected], [email protected]'), {'exc_info': 1})]) def test_smtp_response_error_permanent(self): # Test a permanent error self.thread.setMailer(SMTPResponseExceptionMailerStub(550)) self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) # File must be moved aside self.failIf(os.path.exists(self.filename)) self.failUnless(os.path.exists(os.path.join(self.dir, '.rejected-message'))) self.assertEquals(self.thread.log.errors, [('Discarding email from %s to %s due to a ' 'permanent error: %s', ('*****@*****.**', '[email protected], [email protected]', "(550, 'Serious Error')"), {})]) def test_smtp_recipients_refused(self): # Test a permanent error self.thread.setMailer(SMTPRecipientsRefusedMailerStub( ['*****@*****.**'])) self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) # File must be moved aside self.failIf(os.path.exists(self.filename)) self.failUnless(os.path.exists(os.path.join(self.dir, '.rejected-message'))) self.assertEquals(self.thread.log.errors, [('Email recipients refused: %s', ('*****@*****.**',), {})])