/usr/share/pyshared/zope/sendmail/tests/test_queue.py is in python-zope.sendmail 3.7.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | ##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Mail Delivery Tests
Simple implementation of the MailDelivery, Mailers and MailEvents.
$Id: test_queue.py 126451 2012-05-23 12:23:18Z tseaver $
"""
import os.path
import shutil
from tempfile import mkdtemp
from unittest import TestCase, TestSuite, makeSuite, main
from zope.sendmail.queue import ConsoleApp
from zope.sendmail.tests.test_delivery import MaildirStub, LoggerStub, \
BrokenMailerStub, SMTPResponseExceptionMailerStub, MailerStub
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: foo@example.com\n'
'X-Zope-To: bar@example.com, baz@example.com\n')
msg = ('Header: value\n'
'\n'
'Body\n')
f, t, m = self.thread._parseMessage(hdr + msg)
self.assertEquals(f, 'foo@example.com')
self.assertEquals(t, ('bar@example.com', 'baz@example.com'))
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: foo@example.com\n'
'X-Zope-To: bar@example.com, baz@example.com\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,
[('foo@example.com',
('bar@example.com', 'baz@example.com'),
'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.',
('foo@example.com',
'bar@example.com, baz@example.com'),
{})])
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: foo@example.com\n'
'X-Zope-To: bar@example.com, baz@example.com\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.',
('foo@example.com',
'bar@example.com, baz@example.com'),
{'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: foo@example.com\n'
'X-Zope-To: bar@example.com, baz@example.com\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.',
('foo@example.com',
'bar@example.com, baz@example.com'),
{'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: foo@example.com\n'
'X-Zope-To: bar@example.com, baz@example.com\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',
('foo@example.com',
'bar@example.com, baz@example.com',
"(550, 'Serious Error')"), {})])
def test_smtp_recipients_refused(self):
# Test a permanent error
self.thread.setMailer(SMTPRecipientsRefusedMailerStub(
['bar@example.com']))
self.filename = os.path.join(self.dir, 'message')
temp = open(self.filename, "w+b")
temp.write('X-Zope-From: foo@example.com\n'
'X-Zope-To: bar@example.com, baz@example.com\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',
('bar@example.com',), {})])
test_ini = """[app:zope-sendmail]
interval = 33
hostname = testhost
port = 2525
username = Chris
password = Rossi
force_tls = False
no_tls = True
queue_path = hammer/dont/hurt/em
"""
class TestConsoleApp(TestCase):
def setUp(self):
from zope.sendmail.delivery import QueuedMailDelivery
from zope.sendmail.maildir import Maildir
self.dir = mkdtemp()
self.queue_dir = os.path.join(self.dir, "queue")
self.delivery = QueuedMailDelivery(self.queue_dir)
self.maildir = Maildir(self.queue_dir, True)
self.mailer = MailerStub()
def tearDown(self):
shutil.rmtree(self.dir)
def test_args_processing(self):
# simplest case that works
cmdline = "zope-sendmail %s" % self.dir
app = ConsoleApp(cmdline.split(), verbose=False)
self.assertEquals("zope-sendmail", app.script_name)
self.assertFalse(app._error)
self.assertEquals(self.dir, app.queue_path)
self.assertFalse(app.daemon)
self.assertEquals(3, app.interval)
self.assertEquals("localhost", app.hostname)
self.assertEquals(25, app.port)
self.assertEquals(None, app.username)
self.assertEquals(None, app.password)
self.assertFalse(app.force_tls)
self.assertFalse(app.no_tls)
# simplest case that doesn't work
cmdline = "zope-sendmail"
app = ConsoleApp(cmdline.split(), verbose=False)
self.assertEquals("zope-sendmail", app.script_name)
self.assertTrue(app._error)
self.assertEquals(None, app.queue_path)
self.assertFalse(app.daemon)
self.assertEquals(3, app.interval)
self.assertEquals("localhost", app.hostname)
self.assertEquals(25, app.port)
self.assertEquals(None, app.username)
self.assertEquals(None, app.password)
self.assertFalse(app.force_tls)
self.assertFalse(app.no_tls)
# use (almost) all of the options
cmdline = "zope-sendmail --daemon --interval 7 --hostname foo --port 75 " \
"--username chris --password rossi --force-tls " \
"%s" % self.dir
app = ConsoleApp(cmdline.split(), verbose=False)
self.assertEquals("zope-sendmail", app.script_name)
self.assertFalse(app._error)
self.assertEquals(self.dir, app.queue_path)
self.assertTrue(app.daemon)
self.assertEquals(7, app.interval)
self.assertEquals("foo", app.hostname)
self.assertEquals(75, app.port)
self.assertEquals("chris", app.username)
self.assertEquals("rossi", app.password)
self.assertTrue(app.force_tls)
self.assertFalse(app.no_tls)
# test username without password
cmdline = "zope-sendmail --username chris %s" % self.dir
app = ConsoleApp(cmdline.split(), verbose=False)
self.assertTrue(app._error)
# test --tls and --no-tls together
cmdline = "zope-sendmail --tls --no-tls %s" % self.dir
app = ConsoleApp(cmdline.split(), verbose=False)
self.assertTrue(app._error)
# test force_tls and no_tls
comdline = "zope-sendmail --force-tls --no-tls %s" % self.dir
self.assertTrue(app._error)
def test_ini_parse(self):
ini_path = os.path.join(self.dir, "zope-sendmail.ini")
f = open(ini_path, "w")
f.write(test_ini)
f.close()
# override most everything
cmdline = """zope-sendmail --config %s""" % ini_path
app = ConsoleApp(cmdline.split(), verbose=False)
self.assertEquals("zope-sendmail", app.script_name)
self.assertFalse(app._error)
self.assertEquals("hammer/dont/hurt/em", app.queue_path)
self.assertFalse(app.daemon)
self.assertEquals(33, app.interval)
self.assertEquals("testhost", app.hostname)
self.assertEquals(2525, app.port)
self.assertEquals("Chris", app.username)
self.assertEquals("Rossi", app.password)
self.assertFalse(app.force_tls)
self.assertTrue(app.no_tls)
# override nothing, make sure defaults come through
f = open(ini_path, "w")
f.write("[app:zope-sendmail]\n\nqueue_path=foo\n")
f.close()
cmdline = """zope-sendmail --config %s %s""" % (ini_path, self.dir)
app = ConsoleApp(cmdline.split(), verbose=False)
self.assertEquals("zope-sendmail", app.script_name)
self.assertFalse(app._error)
self.assertEquals(self.dir, app.queue_path)
self.assertFalse(app.daemon)
self.assertEquals(3, app.interval)
self.assertEquals("localhost", app.hostname)
self.assertEquals(25, app.port)
self.assertEquals(None, app.username)
self.assertEquals(None, app.password)
self.assertFalse(app.force_tls)
self.assertFalse(app.no_tls)
class SMTPRecipientsRefusedMailerStub(object):
def __init__(self, recipients):
self.recipients = recipients
def send(self, fromaddr, toaddrs, message):
import smtplib
raise smtplib.SMTPRecipientsRefused(self.recipients)
def test_suite():
return TestSuite((
makeSuite(TestQueueProcessorThread),
makeSuite(TestConsoleApp),
))
if __name__ == '__main__':
main()
|