[7470] | 1 | # -*- coding: utf-8 -*- |
---|
[7476] | 2 | ## $Id: test_smtp.py 11586 2014-04-10 14:04:32Z uli $ |
---|
[7470] | 3 | ## |
---|
[7476] | 4 | ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann |
---|
[7470] | 5 | ## This program is free software; you can redistribute it and/or modify |
---|
| 6 | ## it under the terms of the GNU General Public License as published by |
---|
| 7 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | ## (at your option) any later version. |
---|
[7476] | 9 | ## |
---|
[7470] | 10 | ## This program is distributed in the hope that it will be useful, |
---|
| 11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | ## GNU General Public License for more details. |
---|
[7476] | 14 | ## |
---|
[7470] | 15 | ## You should have received a copy of the GNU General Public License |
---|
| 16 | ## along with this program; if not, write to the Free Software |
---|
| 17 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 18 | ## |
---|
| 19 | # Tests for email-related components |
---|
| 20 | import base64 |
---|
| 21 | import logging |
---|
| 22 | import tempfile |
---|
| 23 | import shutil |
---|
| 24 | import unittest |
---|
| 25 | from StringIO import StringIO |
---|
| 26 | from zope.component import getUtility |
---|
[7474] | 27 | from zope.component.hooks import setSite |
---|
[7470] | 28 | from zope.sendmail.interfaces import IMailDelivery |
---|
[7811] | 29 | from waeup.kofa.app import University |
---|
| 30 | from waeup.kofa.interfaces import IMailService |
---|
| 31 | from waeup.kofa.smtp import ( |
---|
[7470] | 32 | encode_header_item, encode_address, encode_body, FakeSMTPDelivery, |
---|
[7473] | 33 | send_mail) |
---|
[7811] | 34 | from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase |
---|
[7470] | 35 | |
---|
| 36 | # |
---|
| 37 | # SMTP test config |
---|
| 38 | # |
---|
| 39 | |
---|
| 40 | # Also run tests that send mail to external servers? |
---|
| 41 | # If you enable this, please make sure the external smtp hosts and receivers |
---|
| 42 | # do exist really and are not bothered by being spammed by a test programme. |
---|
| 43 | EXTERNAL_MAIL_TESTS = False |
---|
| 44 | |
---|
| 45 | # Maybe existing receiver of externally sent mail. If this mail account |
---|
| 46 | # exists really, it might receive mail from the tests! |
---|
[9306] | 47 | EXTERNAL_MAIL_RECEIVER = 'no-reply@waeup.org' |
---|
[7470] | 48 | |
---|
| 49 | # Names of mail deliveries to use when external mail tests are enabled. |
---|
| 50 | # See local mail.zcml for names of available deliveries. |
---|
| 51 | EXTERNAL_DIRECT_DELIVERY = 'Direct SMTP on localhost' |
---|
| 52 | EXTERNAL_QUEUED_DELIVERY = 'Queued SMTP on localhost' |
---|
| 53 | |
---|
| 54 | # |
---|
| 55 | # end of SMTP test config |
---|
| 56 | # |
---|
| 57 | |
---|
[11585] | 58 | |
---|
[7470] | 59 | def external_mail_test(func): |
---|
| 60 | if not EXTERNAL_MAIL_TESTS: |
---|
| 61 | myself = __file__ |
---|
| 62 | if myself.endswith('.pyc'): |
---|
| 63 | myself = myself[:-2] |
---|
| 64 | print "WARNING: external mail tests are skipped!" |
---|
| 65 | print "WARNING: edit %s to enable them." % myself |
---|
| 66 | return |
---|
| 67 | return func |
---|
| 68 | |
---|
[11585] | 69 | |
---|
[7470] | 70 | class HelperTests(unittest.TestCase): |
---|
| 71 | |
---|
| 72 | def test_encode_header_item(self): |
---|
| 73 | # encoding header items works with strings and unicodes, as |
---|
| 74 | # long as we pass in utf-8 encoded stuff. |
---|
| 75 | result1 = encode_header_item(u'Plain Name'.encode('utf-8')) |
---|
| 76 | result2 = encode_header_item(u'Name with umläut'.encode('utf-8')) |
---|
| 77 | result3 = encode_header_item(u'Plain Name') |
---|
| 78 | result4 = encode_header_item(u'Name with umläut') |
---|
[8382] | 79 | result5 = encode_header_item(None) |
---|
[7470] | 80 | self.assertEqual(result1, u'Plain Name') |
---|
[7472] | 81 | self.assertEqual(result2, u'=?iso-8859-1?q?Name_with_uml=E4ut?=') |
---|
[7470] | 82 | self.assertEqual(result3, u'Plain Name') |
---|
[7472] | 83 | self.assertEqual(result4, u'=?iso-8859-1?q?Name_with_uml=E4ut?=') |
---|
[8382] | 84 | self.assertTrue(result5 is None) |
---|
[7470] | 85 | return |
---|
| 86 | |
---|
| 87 | def test_encode_address(self): |
---|
| 88 | # we can correctly encode address parts |
---|
| 89 | result1 = encode_address('foo@bar.baz') |
---|
[11585] | 90 | result2 = encode_address(u'foo@bar.baz', 'The Foo') |
---|
[7470] | 91 | result3 = encode_address('foo@bar.baz', u'The Foo') |
---|
| 92 | result4 = encode_address('foo@bar.baz', u'With Umläut') |
---|
| 93 | self.assertEqual(result1, 'foo@bar.baz') |
---|
| 94 | self.assertEqual(result2, 'The Foo <foo@bar.baz>') |
---|
| 95 | self.assertEqual(result3, 'The Foo <foo@bar.baz>') |
---|
[7472] | 96 | self.assertEqual(result4, |
---|
| 97 | '=?iso-8859-1?q?With_Uml=E4ut?= <foo@bar.baz>') |
---|
[7470] | 98 | return |
---|
| 99 | |
---|
| 100 | def test_encode_body(self): |
---|
| 101 | result1 = encode_body(u'Simple Text') |
---|
| 102 | self.assertEqual( |
---|
| 103 | str(result1).split('\n')[1:], |
---|
| 104 | ['MIME-Version: 1.0', |
---|
| 105 | 'Content-Type: text/plain; charset="us-ascii"', |
---|
| 106 | 'Content-Transfer-Encoding: 7bit', |
---|
| 107 | '', |
---|
| 108 | 'Simple Text']) |
---|
| 109 | return |
---|
| 110 | |
---|
| 111 | def test_encode_body_latin1_string(self): |
---|
| 112 | # utf-8 encoded byte streams are transferred correctly when |
---|
| 113 | # they contain chars unrepresentable in ascii but available in latin1 |
---|
| 114 | text = u'Simple text with Ümläut'.encode('utf-8') |
---|
| 115 | result1 = encode_body(text) |
---|
| 116 | result1 = str(result1).split('\n')[1:] |
---|
| 117 | self.assertEqual( |
---|
| 118 | result1, |
---|
| 119 | ['MIME-Version: 1.0', |
---|
| 120 | 'Content-Type: text/plain; charset="iso-8859-1"', |
---|
| 121 | 'Content-Transfer-Encoding: quoted-printable', |
---|
| 122 | '', |
---|
| 123 | 'Simple text with =DCml=E4ut']) |
---|
| 124 | return |
---|
| 125 | |
---|
| 126 | def test_encode_body_latin1_unicode(self): |
---|
| 127 | # unicode strings are transferred correctly when they contain |
---|
| 128 | # chars unrepresentable in ascii but available in latin1 |
---|
| 129 | text = u'Simple text with Ümläut' |
---|
| 130 | result1 = encode_body(text) |
---|
| 131 | result1 = str(result1).split('\n')[1:] |
---|
| 132 | self.assertEqual( |
---|
| 133 | result1, |
---|
| 134 | ['MIME-Version: 1.0', |
---|
| 135 | 'Content-Type: text/plain; charset="iso-8859-1"', |
---|
| 136 | 'Content-Transfer-Encoding: quoted-printable', |
---|
| 137 | '', |
---|
| 138 | 'Simple text with =DCml=E4ut']) |
---|
| 139 | return |
---|
| 140 | |
---|
| 141 | def test_encode_body_utf8_string(self): |
---|
| 142 | # utf-8 encoded byte streams are transferred correctly when |
---|
| 143 | # they contain chars unrepresentable in ascii but available in latin1 |
---|
| 144 | text = u"Simple text with ü and capital pi: " + unichr(0x3a0) |
---|
[11585] | 145 | text = text.encode('utf-8') # turn unicode into byte stream |
---|
[7470] | 146 | result1 = encode_body(text) |
---|
| 147 | result1 = str(result1).split('\n')[1:] |
---|
| 148 | self.assertEqual( |
---|
| 149 | result1, |
---|
| 150 | ['MIME-Version: 1.0', |
---|
| 151 | 'Content-Type: text/plain; charset="utf-8"', |
---|
| 152 | 'Content-Transfer-Encoding: base64', |
---|
| 153 | '', |
---|
| 154 | 'U2ltcGxlIHRleHQgd2l0aCDDvCBhbmQgY2FwaXRhbCBwaTogzqA=', '']) |
---|
| 155 | self.assertEqual( |
---|
| 156 | base64.b64decode(result1[-2]).decode('utf-8'), |
---|
| 157 | u"Simple text with ü and capital pi: " + unichr(0x3a0)) |
---|
| 158 | return |
---|
| 159 | |
---|
| 160 | def test_encode_body_utf8_unicode(self): |
---|
| 161 | # utf-8 encoded byte streams are transferred correctly when |
---|
| 162 | # they contain chars unrepresentable in latin1 |
---|
| 163 | text = u"Simple text with ü and capital pi: " + unichr(0x3a0) |
---|
| 164 | result1 = encode_body(text) |
---|
| 165 | result1 = str(result1).split('\n')[1:] |
---|
| 166 | self.assertEqual( |
---|
| 167 | result1, |
---|
| 168 | ['MIME-Version: 1.0', |
---|
| 169 | 'Content-Type: text/plain; charset="utf-8"', |
---|
| 170 | 'Content-Transfer-Encoding: base64', |
---|
| 171 | '', |
---|
| 172 | 'U2ltcGxlIHRleHQgd2l0aCDDvCBhbmQgY2FwaXRhbCBwaTogzqA=', '']) |
---|
| 173 | self.assertEqual( |
---|
| 174 | base64.b64decode(result1[-2]).decode('utf-8'), |
---|
| 175 | u"Simple text with ü and capital pi: " + unichr(0x3a0)) |
---|
| 176 | return |
---|
| 177 | |
---|
[11585] | 178 | |
---|
[7470] | 179 | class FunctionalMailerTests(FunctionalTestCase): |
---|
| 180 | |
---|
| 181 | layer = FunctionalLayer |
---|
| 182 | |
---|
| 183 | def setUp(self): |
---|
| 184 | super(FunctionalMailerTests, self).setUp() |
---|
| 185 | self.setup_logging() |
---|
| 186 | return |
---|
| 187 | |
---|
| 188 | def tearDown(self): |
---|
| 189 | super(FunctionalMailerTests, self).tearDown() |
---|
| 190 | self.teardown_logging() |
---|
| 191 | return |
---|
| 192 | |
---|
| 193 | def setup_logging(self): |
---|
| 194 | # setup a log-handler that catches all fake mailer output |
---|
| 195 | self.stream = StringIO() |
---|
| 196 | handler = logging.StreamHandler(self.stream) |
---|
| 197 | logger = logging.getLogger('test.smtp') |
---|
| 198 | logger.addHandler(handler) |
---|
| 199 | logger.setLevel(logging.INFO) |
---|
| 200 | return |
---|
| 201 | |
---|
| 202 | def get_fake_smtp_output(self): |
---|
| 203 | # get output generated by fake mailer |
---|
| 204 | self.stream.flush() |
---|
| 205 | self.stream.seek(0) |
---|
| 206 | return self.stream.read() |
---|
| 207 | |
---|
| 208 | def teardown_logging(self): |
---|
| 209 | # remove the log handler for fake mailer output |
---|
| 210 | logger = logging.getLogger('test.smtp') |
---|
| 211 | handlers = [x for x in logger.handlers] |
---|
| 212 | for handler in handlers: |
---|
| 213 | logger.removeHandler(handler) |
---|
| 214 | return |
---|
| 215 | |
---|
| 216 | def test_get_fake_mailer(self): |
---|
| 217 | # we can get the fake mailer if we want |
---|
| 218 | mailer = getUtility(IMailDelivery, name='No email service') |
---|
| 219 | self.assertTrue(isinstance(mailer, FakeSMTPDelivery)) |
---|
| 220 | return |
---|
| 221 | |
---|
| 222 | def test_get_default_mailer(self): |
---|
| 223 | # we can get a default mailer if we want |
---|
| 224 | mailer = getUtility(IMailService) |
---|
| 225 | self.assertTrue(isinstance(mailer(), FakeSMTPDelivery)) |
---|
| 226 | return |
---|
| 227 | |
---|
| 228 | def test_send_mail(self): |
---|
| 229 | # we can really send mail. |
---|
| 230 | mail_id = send_mail( |
---|
| 231 | u'A sender', u'sender@example.com', |
---|
| 232 | u'A recipient', u'recpt@example.com', |
---|
| 233 | u'A subject', |
---|
| 234 | u'This is a test mail.') |
---|
| 235 | self.assertEqual(mail_id, 'fake-message-id@example.com') |
---|
| 236 | self.assertEqual( |
---|
| 237 | self.get_fake_smtp_output().split('\n'), |
---|
[11586] | 238 | [u'Sending email from no-reply@waeup.org to ' |
---|
[11585] | 239 | u'recpt@example.com, sender@example.com:', |
---|
[7470] | 240 | u'Message:', |
---|
| 241 | u'msg: MIME-Version: 1.0', |
---|
| 242 | u'msg: Content-Type: text/plain; charset="us-ascii"', |
---|
| 243 | u'msg: Content-Transfer-Encoding: 7bit', |
---|
[11585] | 244 | u'msg: From: A sender <no-reply@waeup.org>', |
---|
| 245 | u'msg: To: A recipient <recpt@example.com>', |
---|
[10854] | 246 | u'msg: Cc: A sender <sender@example.com>', |
---|
[11585] | 247 | u'msg: Reply-To: A sender <sender@example.com>', |
---|
[7470] | 248 | u'msg: Subject: A subject', |
---|
| 249 | u'msg: ', |
---|
| 250 | u'msg: This is a test mail.', |
---|
| 251 | u''] |
---|
| 252 | ) |
---|
| 253 | return |
---|
| 254 | |
---|
| 255 | def test_send_mail_utf8_strings(self): |
---|
| 256 | # we can send mail with utf-8 encoded strings as input |
---|
| 257 | mail_id = send_mail( |
---|
| 258 | u'A sender', u'sender@example.com', |
---|
| 259 | u'A recipient', u'recpt@example.com', |
---|
| 260 | u'A subject', |
---|
| 261 | u'This is a test mail with ümläut.'.encode('utf-8')) |
---|
| 262 | self.assertEqual(mail_id, 'fake-message-id@example.com') |
---|
| 263 | self.assertEqual( |
---|
| 264 | self.get_fake_smtp_output().split('\n'), |
---|
[11586] | 265 | [u'Sending email from no-reply@waeup.org ' |
---|
[11585] | 266 | u'to recpt@example.com, sender@example.com:', |
---|
[7470] | 267 | u'Message:', u'msg: MIME-Version: 1.0', |
---|
| 268 | u'msg: Content-Type: text/plain; charset="iso-8859-1"', |
---|
| 269 | u'msg: Content-Transfer-Encoding: quoted-printable', |
---|
[11585] | 270 | u'msg: From: A sender <no-reply@waeup.org>', |
---|
| 271 | u'msg: To: A recipient <recpt@example.com>', |
---|
[10854] | 272 | u'msg: Cc: A sender <sender@example.com>', |
---|
[11585] | 273 | u'msg: Reply-To: A sender <sender@example.com>', |
---|
[7470] | 274 | u'msg: Subject: A subject', |
---|
| 275 | u'msg: ', |
---|
| 276 | u'msg: This is a test mail with =FCml=E4ut.', |
---|
| 277 | u''] |
---|
| 278 | ) |
---|
| 279 | return |
---|
| 280 | |
---|
| 281 | def test_send_mail_utf8_unicode(self): |
---|
| 282 | # we can send mail with utf-8 encoded unicode as input |
---|
| 283 | mail_id = send_mail( |
---|
| 284 | u'A sender', u'sender@example.com', |
---|
| 285 | u'A recipient', u'recpt@example.com', |
---|
| 286 | u'A subject', |
---|
| 287 | u'This is a test mail with ümläut.') |
---|
| 288 | self.assertEqual(mail_id, 'fake-message-id@example.com') |
---|
| 289 | self.assertEqual( |
---|
| 290 | self.get_fake_smtp_output().split('\n'), |
---|
[11586] | 291 | [u'Sending email from no-reply@waeup.org ' |
---|
[11585] | 292 | u'to recpt@example.com, sender@example.com:', |
---|
[7470] | 293 | u'Message:', u'msg: MIME-Version: 1.0', |
---|
| 294 | u'msg: Content-Type: text/plain; charset="iso-8859-1"', |
---|
| 295 | u'msg: Content-Transfer-Encoding: quoted-printable', |
---|
[11585] | 296 | u'msg: From: A sender <no-reply@waeup.org>', |
---|
| 297 | u'msg: To: A recipient <recpt@example.com>', |
---|
[10854] | 298 | u'msg: Cc: A sender <sender@example.com>', |
---|
[11585] | 299 | u'msg: Reply-To: A sender <sender@example.com>', |
---|
[7470] | 300 | u'msg: Subject: A subject', |
---|
| 301 | u'msg: ', |
---|
| 302 | u'msg: This is a test mail with =FCml=E4ut.', |
---|
| 303 | u''] |
---|
| 304 | ) |
---|
| 305 | return |
---|
| 306 | |
---|
| 307 | |
---|
| 308 | class ExternalMailerTests(FunctionalTestCase): |
---|
| 309 | |
---|
| 310 | layer = FunctionalLayer |
---|
| 311 | |
---|
| 312 | def setUp(self): |
---|
| 313 | super(ExternalMailerTests, self).setUp() |
---|
| 314 | # Setup a sample site for each test |
---|
| 315 | app = University() |
---|
| 316 | self.dc_root = tempfile.mkdtemp() |
---|
| 317 | app['datacenter'].setStoragePath(self.dc_root) |
---|
| 318 | |
---|
| 319 | # Prepopulate the ZODB... |
---|
| 320 | self.getRootFolder()['app'] = app |
---|
| 321 | self.app = self.getRootFolder()['app'] |
---|
| 322 | return |
---|
| 323 | |
---|
| 324 | def tearDown(self): |
---|
| 325 | super(ExternalMailerTests, self).tearDown() |
---|
| 326 | shutil.rmtree(self.dc_root) |
---|
| 327 | return |
---|
| 328 | |
---|
| 329 | def test_config_default_mailer(self): |
---|
| 330 | # The default mailer set in config is 'no mailer'. |
---|
| 331 | self.assertEqual( |
---|
| 332 | getattr(self.app.get('configuration', None), 'smtp_mailer'), |
---|
| 333 | u'No email service') |
---|
| 334 | return |
---|
| 335 | |
---|
| 336 | @external_mail_test |
---|
| 337 | def test_send_direct_mail(self): |
---|
| 338 | # send mail using direct mail delivery |
---|
| 339 | self.app['configuration'].smtp_mailer = EXTERNAL_DIRECT_DELIVERY |
---|
| 340 | setSite(self.app) |
---|
| 341 | result = send_mail( |
---|
| 342 | 'test program', 'no-reply@waeup.org', |
---|
| 343 | 'test mail receiver', EXTERNAL_MAIL_RECEIVER, |
---|
[7819] | 344 | 'Test Mail from WAeUP Kofa', |
---|
[7470] | 345 | 'Hi from test mailer!\n\nRegards,\nTest Programme\n' |
---|
| 346 | ) |
---|
| 347 | import transaction |
---|
[11585] | 348 | transaction.commit() # The mail is really sent when transactions is |
---|
| 349 | # committed |
---|
[7506] | 350 | self.assertTrue('@' in result) |
---|
[7470] | 351 | return |
---|
| 352 | |
---|
| 353 | @external_mail_test |
---|
[9306] | 354 | def test_send_direct_mails(self): |
---|
| 355 | # send several mails using direct mail delivery |
---|
| 356 | self.app['configuration'].smtp_mailer = EXTERNAL_DIRECT_DELIVERY |
---|
| 357 | setSite(self.app) |
---|
| 358 | result = send_mail( |
---|
| 359 | 'test program', 'no-reply@waeup.org', |
---|
| 360 | 'test mail receiver', |
---|
| 361 | '%s, %s' % (EXTERNAL_MAIL_RECEIVER, EXTERNAL_MAIL_RECEIVER), |
---|
| 362 | 'Test Mail from WAeUP Kofa', |
---|
| 363 | 'Hi from test mailer!\n\nRegards,\nTest Programme\n' |
---|
| 364 | ) |
---|
| 365 | import transaction |
---|
[11585] | 366 | transaction.commit() # The mail is really sent when transactions is |
---|
| 367 | # committed |
---|
[9306] | 368 | self.assertTrue('@' in result) |
---|
| 369 | return |
---|
| 370 | |
---|
| 371 | @external_mail_test |
---|
[7470] | 372 | def test_send_queued_mail(self): |
---|
| 373 | # send mail using queued mail delivery |
---|
| 374 | self.app['configuration'].smtp_mailer = EXTERNAL_QUEUED_DELIVERY |
---|
| 375 | setSite(self.app) |
---|
| 376 | result = send_mail( |
---|
| 377 | 'test program', 'no-reply@waeup.org', |
---|
| 378 | 'test mail receiver', EXTERNAL_MAIL_RECEIVER, |
---|
[7819] | 379 | 'Test Mail from WAeUP Kofa', |
---|
[7470] | 380 | 'Hi from test mailer!\n\nRegards,\nTest Programme\n' |
---|
| 381 | ) |
---|
| 382 | import transaction |
---|
[11585] | 383 | transaction.commit() # The mail is really sent when transactions is |
---|
| 384 | # committed |
---|
[7506] | 385 | self.assertTrue('@' in result) |
---|
[7470] | 386 | return |
---|