/usr/share/pyshared/zope/sendmail/tests/test_delivery.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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | ##############################################################################
#
# 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_delivery.py 110390 2010-04-01 12:33:34Z mgedmin $
"""
import smtplib
from unittest import TestCase, TestSuite, makeSuite, main
import doctest
import transaction
from zope.interface import implements
from zope.interface.verify import verifyObject
from zope.sendmail.interfaces import IMailer
class MailerStub(object):
implements(IMailer)
def __init__(self, *args, **kw):
self.sent_messages = []
def send(self, fromaddr, toaddrs, message):
self.sent_messages.append((fromaddr, toaddrs, message))
class TestMailDataManager(TestCase):
def testInterface(self):
from transaction.interfaces import IDataManager
from zope.sendmail.delivery import MailDataManager
manager = MailDataManager(object, (1, 2))
verifyObject(IDataManager, manager)
self.assertEqual(manager.callable, object)
self.assertEqual(manager.args, (1, 2))
def print_success(*args):
print "message successfully sent, args: %s" % (args, )
def print_abort():
print "message aborted"
def doctest_successful_commit():
"""Regression test for http://www.zope.org/Collectors/Zope3-dev/590
Let's do a full two-phase commit.
>>> from zope.sendmail.delivery import MailDataManager
>>> manager = MailDataManager(print_success, ('foo', 'bar'),
... onAbort=print_abort)
>>> transaction = object()
>>> manager.tpc_begin(transaction)
>>> manager.commit(transaction)
>>> manager.tpc_vote(transaction)
>>> manager.tpc_finish(transaction)
message successfully sent, args: ('foo', 'bar')
"""
def doctest_unsuccessful_commit():
"""Regression test for http://www.zope.org/Collectors/Zope3-dev/590
Let's start a two-phase commit, then abort it.
>>> from zope.sendmail.delivery import MailDataManager
>>> manager = MailDataManager(print_success, onAbort=print_abort)
>>> manager.tpc_begin(transaction)
>>> manager.commit(transaction)
>>> manager.tpc_vote(transaction)
>>> manager.tpc_abort(transaction)
message aborted
"""
class TestDirectMailDelivery(TestCase):
def testInterface(self):
from zope.sendmail.interfaces import IDirectMailDelivery
from zope.sendmail.delivery import DirectMailDelivery
mailer = MailerStub()
delivery = DirectMailDelivery(mailer)
verifyObject(IDirectMailDelivery, delivery)
self.assertEqual(delivery.mailer, mailer)
def testSend(self):
from zope.sendmail.delivery import DirectMailDelivery
mailer = MailerStub()
delivery = DirectMailDelivery(mailer)
fromaddr = 'Jim <jim@example.com'
toaddrs = ('Guido <guido@example.com>',
'Steve <steve@examplecom>')
opt_headers = ('From: Jim <jim@example.org>\n'
'To: some-zope-coders:;\n'
'Date: Mon, 19 May 2003 10:17:36 -0400\n'
'Message-Id: <20030519.1234@example.org>\n')
message = ('Subject: example\n'
'\n'
'This is just an example\n')
msgid = delivery.send(fromaddr, toaddrs, opt_headers + message)
self.assertEquals(msgid, '20030519.1234@example.org')
self.assertEquals(mailer.sent_messages, [])
transaction.commit()
self.assertEquals(mailer.sent_messages,
[(fromaddr, toaddrs, opt_headers + message)])
mailer.sent_messages = []
msgid = delivery.send(fromaddr, toaddrs, message)
self.assert_('@' in msgid)
self.assertEquals(mailer.sent_messages, [])
transaction.commit()
self.assertEquals(len(mailer.sent_messages), 1)
self.assertEquals(mailer.sent_messages[0][0], fromaddr)
self.assertEquals(mailer.sent_messages[0][1], toaddrs)
self.assert_(mailer.sent_messages[0][2].endswith(message))
new_headers = mailer.sent_messages[0][2][:-len(message)]
self.assert_(new_headers.find('Message-Id: <%s>' % msgid) != -1)
mailer.sent_messages = []
msgid = delivery.send(fromaddr, toaddrs, opt_headers + message)
self.assertEquals(mailer.sent_messages, [])
transaction.abort()
self.assertEquals(mailer.sent_messages, [])
class MaildirWriterStub(object):
data = ''
commited_messages = [] # this list is shared among all instances
aborted_messages = [] # this one too
_closed = False
def write(self, str):
if self._closed:
raise AssertionError('already closed')
self.data += str
def writelines(self, seq):
if self._closed:
raise AssertionError('already closed')
self.data += ''.join(seq)
def close(self):
self._closed = True
def commit(self):
if not self._closed:
raise AssertionError('for this test we want the message explicitly'
' closed before it is committed')
self._commited = True
self.commited_messages.append(self.data)
def abort(self):
if not self._closed:
raise AssertionError('for this test we want the message explicitly'
' closed before it is committed')
self._aborted = True
self.aborted_messages.append(self.data)
class MaildirStub(object):
def __init__(self, path, create=False):
self.path = path
self.create = create
self.msgs = []
self.files = []
def __iter__(self):
return iter(self.files)
def newMessage(self):
m = MaildirWriterStub()
self.msgs.append(m)
return m
class LoggerStub(object):
def __init__(self):
self.infos = []
self.errors = []
def getLogger(self, name):
return self
def error(self, msg, *args, **kwargs):
self.errors.append((msg, args, kwargs))
def info(self, msg, *args, **kwargs):
self.infos.append((msg, args, kwargs))
class BizzarreMailError(IOError):
pass
class BrokenMailerStub(object):
implements(IMailer)
def __init__(self, *args, **kw):
pass
def send(self, fromaddr, toaddrs, message):
raise BizzarreMailError("bad things happened while sending mail")
class SMTPResponseExceptionMailerStub(object):
implements(IMailer)
def __init__(self, code):
self.code = code
def send(self, fromaddr, toaddrs, message):
raise smtplib.SMTPResponseException(self.code, 'Serious Error')
class TestQueuedMailDelivery(TestCase):
def setUp(self):
import zope.sendmail.delivery as mail_delivery_module
self.mail_delivery_module = mail_delivery_module
self.old_Maildir = mail_delivery_module.Maildir
mail_delivery_module.Maildir = MaildirStub
def tearDown(self):
self.mail_delivery_module.Maildir = self.old_Maildir
MaildirWriterStub.commited_messages = []
MaildirWriterStub.aborted_messages = []
def testInterface(self):
from zope.sendmail.interfaces import IQueuedMailDelivery
from zope.sendmail.delivery import QueuedMailDelivery
delivery = QueuedMailDelivery('/path/to/mailbox')
verifyObject(IQueuedMailDelivery, delivery)
self.assertEqual(delivery.queuePath, '/path/to/mailbox')
def testSend(self):
from zope.sendmail.delivery import QueuedMailDelivery
delivery = QueuedMailDelivery('/path/to/mailbox')
fromaddr = 'jim@example.com'
toaddrs = ('guido@example.com',
'steve@examplecom')
zope_headers = ('X-Zope-From: jim@example.com\n'
'X-Zope-To: guido@example.com, steve@examplecom\n')
opt_headers = ('From: Jim <jim@example.org>\n'
'To: some-zope-coders:;\n'
'Date: Mon, 19 May 2003 10:17:36 -0400\n'
'Message-Id: <20030519.1234@example.org>\n')
message = ('Subject: example\n'
'\n'
'This is just an example\n')
msgid = delivery.send(fromaddr, toaddrs, opt_headers + message)
self.assertEquals(msgid, '20030519.1234@example.org')
self.assertEquals(MaildirWriterStub.commited_messages, [])
self.assertEquals(MaildirWriterStub.aborted_messages, [])
transaction.commit()
self.assertEquals(MaildirWriterStub.commited_messages,
[zope_headers + opt_headers + message])
self.assertEquals(MaildirWriterStub.aborted_messages, [])
MaildirWriterStub.commited_messages = []
msgid = delivery.send(fromaddr, toaddrs, message)
self.assert_('@' in msgid)
self.assertEquals(MaildirWriterStub.commited_messages, [])
self.assertEquals(MaildirWriterStub.aborted_messages, [])
transaction.commit()
self.assertEquals(len(MaildirWriterStub.commited_messages), 1)
self.assert_(MaildirWriterStub.commited_messages[0].endswith(message))
new_headers = MaildirWriterStub.commited_messages[0][:-len(message)]
self.assert_(new_headers.find('Message-Id: <%s>' % msgid) != -1)
self.assert_(new_headers.find('X-Zope-From: %s' % fromaddr) != 1)
self.assert_(new_headers.find('X-Zope-To: %s' % ", ".join(toaddrs)) != 1)
self.assertEquals(MaildirWriterStub.aborted_messages, [])
MaildirWriterStub.commited_messages = []
msgid = delivery.send(fromaddr, toaddrs, opt_headers + message)
self.assertEquals(MaildirWriterStub.commited_messages, [])
self.assertEquals(MaildirWriterStub.aborted_messages, [])
transaction.abort()
self.assertEquals(MaildirWriterStub.commited_messages, [])
self.assertEquals(len(MaildirWriterStub.aborted_messages), 1)
def test_suite():
return TestSuite((
makeSuite(TestMailDataManager),
makeSuite(TestDirectMailDelivery),
makeSuite(TestQueuedMailDelivery),
doctest.DocTestSuite(),
))
if __name__ == '__main__':
main()
|