source: main/waeup.kofa/trunk/src/waeup/kofa/tests/test_smtp.py @ 8685

Last change on this file since 8685 was 8382, checked in by uli, 13 years ago

Handle unusual SMTP address parts more predictable.

  • Property svn:keywords set to Id
File size: 13.3 KB
Line 
1# -*- coding: utf-8 -*-
2## $Id: test_smtp.py 8382 2012-05-07 10:53:25Z uli $
3##
4## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
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.
9##
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.
14##
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
20import base64
21import logging
22import tempfile
23import shutil
24import unittest
25from StringIO import StringIO
26from zope.component import getUtility
27from zope.component.hooks import setSite
28from zope.sendmail.interfaces import IMailDelivery
29from waeup.kofa.app import University
30from waeup.kofa.interfaces import IMailService
31from waeup.kofa.smtp import (
32    encode_header_item, encode_address, encode_body, FakeSMTPDelivery,
33    send_mail)
34from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
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.
43EXTERNAL_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!
47EXTERNAL_MAIL_RECEIVER = 'no-user@waeup.org'
48
49# Names of mail deliveries to use when external mail tests are enabled.
50#   See local mail.zcml for names of available deliveries.
51EXTERNAL_DIRECT_DELIVERY = 'Direct SMTP on localhost'
52EXTERNAL_QUEUED_DELIVERY = 'Queued SMTP on localhost'
53
54#
55# end of SMTP test config
56#
57
58def external_mail_test(func):
59    if not EXTERNAL_MAIL_TESTS:
60        myself = __file__
61        if myself.endswith('.pyc'):
62            myself = myself[:-2]
63        print "WARNING: external mail tests are skipped!"
64        print "WARNING: edit %s to enable them." % myself
65        return
66    return func
67
68class HelperTests(unittest.TestCase):
69
70    def test_encode_header_item(self):
71        # encoding header items works with strings and unicodes, as
72        # long as we pass in utf-8 encoded stuff.
73        result1 = encode_header_item(u'Plain Name'.encode('utf-8'))
74        result2 = encode_header_item(u'Name with umläut'.encode('utf-8'))
75        result3 = encode_header_item(u'Plain Name')
76        result4 = encode_header_item(u'Name with umläut')
77        result5 = encode_header_item(None)
78        self.assertEqual(result1, u'Plain Name')
79        self.assertEqual(result2, u'=?iso-8859-1?q?Name_with_uml=E4ut?=')
80        self.assertEqual(result3, u'Plain Name')
81        self.assertEqual(result4, u'=?iso-8859-1?q?Name_with_uml=E4ut?=')
82        self.assertTrue(result5 is None)
83        return
84
85    def test_encode_address(self):
86        # we can correctly encode address parts
87        result1 = encode_address('foo@bar.baz')
88        result2 = encode_address(u'foo@bar.baz', 'The Foo' )
89        result3 = encode_address('foo@bar.baz', u'The Foo')
90        result4 = encode_address('foo@bar.baz', u'With Umläut')
91        self.assertEqual(result1, 'foo@bar.baz')
92        self.assertEqual(result2, 'The Foo <foo@bar.baz>')
93        self.assertEqual(result3, 'The Foo <foo@bar.baz>')
94        self.assertEqual(result4,
95                         '=?iso-8859-1?q?With_Uml=E4ut?= <foo@bar.baz>')
96        return
97
98    def test_encode_body(self):
99        result1 = encode_body(u'Simple Text')
100        self.assertEqual(
101            str(result1).split('\n')[1:],
102            ['MIME-Version: 1.0',
103             'Content-Type: text/plain; charset="us-ascii"',
104             'Content-Transfer-Encoding: 7bit',
105             '',
106             'Simple Text'])
107        return
108
109    def test_encode_body_latin1_string(self):
110        # utf-8 encoded byte streams are transferred correctly when
111        # they contain chars unrepresentable in ascii but available in latin1
112        text = u'Simple text with Ümläut'.encode('utf-8')
113        result1 = encode_body(text)
114        result1 = str(result1).split('\n')[1:]
115        self.assertEqual(
116            result1,
117            ['MIME-Version: 1.0',
118             'Content-Type: text/plain; charset="iso-8859-1"',
119             'Content-Transfer-Encoding: quoted-printable',
120             '',
121             'Simple text with =DCml=E4ut'])
122        return
123
124    def test_encode_body_latin1_unicode(self):
125        # unicode strings are transferred correctly when they contain
126        # chars unrepresentable in ascii but available in latin1
127        text = u'Simple text with Ümläut'
128        result1 = encode_body(text)
129        result1 = str(result1).split('\n')[1:]
130        self.assertEqual(
131            result1,
132            ['MIME-Version: 1.0',
133             'Content-Type: text/plain; charset="iso-8859-1"',
134             'Content-Transfer-Encoding: quoted-printable',
135             '',
136             'Simple text with =DCml=E4ut'])
137        return
138
139    def test_encode_body_utf8_string(self):
140        # utf-8 encoded byte streams are transferred correctly when
141        # they contain chars unrepresentable in ascii but available in latin1
142        text = u"Simple text with ü and capital pi: " + unichr(0x3a0)
143        text = text.encode('utf-8') # turn unicode into byte stream
144        result1 = encode_body(text)
145        result1 = str(result1).split('\n')[1:]
146        self.assertEqual(
147            result1,
148            ['MIME-Version: 1.0',
149             'Content-Type: text/plain; charset="utf-8"',
150             'Content-Transfer-Encoding: base64',
151             '',
152             'U2ltcGxlIHRleHQgd2l0aCDDvCBhbmQgY2FwaXRhbCBwaTogzqA=', ''])
153        self.assertEqual(
154            base64.b64decode(result1[-2]).decode('utf-8'),
155            u"Simple text with ü and capital pi: " + unichr(0x3a0))
156        return
157
158    def test_encode_body_utf8_unicode(self):
159        # utf-8 encoded byte streams are transferred correctly when
160        # they contain chars unrepresentable in latin1
161        text = u"Simple text with ü and capital pi: " + unichr(0x3a0)
162        result1 = encode_body(text)
163        result1 = str(result1).split('\n')[1:]
164        self.assertEqual(
165            result1,
166            ['MIME-Version: 1.0',
167             'Content-Type: text/plain; charset="utf-8"',
168             'Content-Transfer-Encoding: base64',
169             '',
170             'U2ltcGxlIHRleHQgd2l0aCDDvCBhbmQgY2FwaXRhbCBwaTogzqA=', ''])
171        self.assertEqual(
172            base64.b64decode(result1[-2]).decode('utf-8'),
173            u"Simple text with ü and capital pi: " + unichr(0x3a0))
174        return
175
176class FunctionalMailerTests(FunctionalTestCase):
177
178    layer = FunctionalLayer
179
180    def setUp(self):
181        super(FunctionalMailerTests, self).setUp()
182        self.setup_logging()
183        return
184
185    def tearDown(self):
186        super(FunctionalMailerTests, self).tearDown()
187        self.teardown_logging()
188        return
189
190    def setup_logging(self):
191        # setup a log-handler that catches all fake mailer output
192        self.stream = StringIO()
193        handler = logging.StreamHandler(self.stream)
194        logger = logging.getLogger('test.smtp')
195        logger.addHandler(handler)
196        logger.setLevel(logging.INFO)
197        return
198
199    def get_fake_smtp_output(self):
200        # get output generated by fake mailer
201        self.stream.flush()
202        self.stream.seek(0)
203        return self.stream.read()
204
205    def teardown_logging(self):
206        # remove the log handler for fake mailer output
207        logger = logging.getLogger('test.smtp')
208        handlers = [x for x in logger.handlers]
209        for handler in handlers:
210            logger.removeHandler(handler)
211        return
212
213    def test_get_fake_mailer(self):
214        # we can get the fake mailer if we want
215        mailer = getUtility(IMailDelivery, name='No email service')
216        self.assertTrue(isinstance(mailer, FakeSMTPDelivery))
217        return
218
219    def test_get_default_mailer(self):
220        # we can get a default mailer if we want
221        mailer = getUtility(IMailService)
222        self.assertTrue(isinstance(mailer(), FakeSMTPDelivery))
223        return
224
225    def test_send_mail(self):
226        # we can really send mail.
227        mail_id = send_mail(
228            u'A sender', u'sender@example.com',
229            u'A recipient', u'recpt@example.com',
230            u'A subject',
231            u'This is a test mail.')
232        self.assertEqual(mail_id, 'fake-message-id@example.com')
233        self.assertEqual(
234            self.get_fake_smtp_output().split('\n'),
235            [u'Sending email from sender@example.com to recpt@example.com:',
236             u'Message:',
237             u'msg: MIME-Version: 1.0',
238             u'msg: Content-Type: text/plain; charset="us-ascii"',
239             u'msg: Content-Transfer-Encoding: 7bit',
240             u'msg: From: A sender <sender@example.com>',
241             u'msg: To: A recipient <recpt@example.com>',
242             u'msg: Subject: A subject',
243             u'msg: ',
244             u'msg: This is a test mail.',
245             u'']
246            )
247        return
248
249    def test_send_mail_utf8_strings(self):
250        # we can send mail with utf-8 encoded strings as input
251        mail_id = send_mail(
252            u'A sender', u'sender@example.com',
253            u'A recipient', u'recpt@example.com',
254            u'A subject',
255            u'This is a test mail with ümläut.'.encode('utf-8'))
256        self.assertEqual(mail_id, 'fake-message-id@example.com')
257        self.assertEqual(
258            self.get_fake_smtp_output().split('\n'),
259            [u'Sending email from sender@example.com to recpt@example.com:',
260             u'Message:', u'msg: MIME-Version: 1.0',
261             u'msg: Content-Type: text/plain; charset="iso-8859-1"',
262             u'msg: Content-Transfer-Encoding: quoted-printable',
263             u'msg: From: A sender <sender@example.com>',
264             u'msg: To: A recipient <recpt@example.com>',
265             u'msg: Subject: A subject',
266             u'msg: ',
267             u'msg: This is a test mail with =FCml=E4ut.',
268             u'']
269            )
270        return
271
272    def test_send_mail_utf8_unicode(self):
273        # we can send mail with utf-8 encoded unicode as input
274        mail_id = send_mail(
275            u'A sender', u'sender@example.com',
276            u'A recipient', u'recpt@example.com',
277            u'A subject',
278            u'This is a test mail with ümläut.')
279        self.assertEqual(mail_id, 'fake-message-id@example.com')
280        self.assertEqual(
281            self.get_fake_smtp_output().split('\n'),
282            [u'Sending email from sender@example.com to recpt@example.com:',
283             u'Message:', u'msg: MIME-Version: 1.0',
284             u'msg: Content-Type: text/plain; charset="iso-8859-1"',
285             u'msg: Content-Transfer-Encoding: quoted-printable',
286             u'msg: From: A sender <sender@example.com>',
287             u'msg: To: A recipient <recpt@example.com>',
288             u'msg: Subject: A subject',
289             u'msg: ',
290             u'msg: This is a test mail with =FCml=E4ut.',
291             u'']
292            )
293        return
294
295
296class ExternalMailerTests(FunctionalTestCase):
297
298    layer = FunctionalLayer
299
300    def setUp(self):
301        super(ExternalMailerTests, self).setUp()
302        # Setup a sample site for each test
303        app = University()
304        self.dc_root = tempfile.mkdtemp()
305        app['datacenter'].setStoragePath(self.dc_root)
306
307        # Prepopulate the ZODB...
308        self.getRootFolder()['app'] = app
309        self.app = self.getRootFolder()['app']
310        return
311
312    def tearDown(self):
313        super(ExternalMailerTests, self).tearDown()
314        shutil.rmtree(self.dc_root)
315        return
316
317    def test_config_default_mailer(self):
318        # The default mailer set in config is 'no mailer'.
319        self.assertEqual(
320            getattr(self.app.get('configuration', None), 'smtp_mailer'),
321            u'No email service')
322        return
323
324    @external_mail_test
325    def test_send_direct_mail(self):
326        # send mail using direct mail delivery
327        self.app['configuration'].smtp_mailer = EXTERNAL_DIRECT_DELIVERY
328        setSite(self.app)
329        result = send_mail(
330            'test program', 'no-reply@waeup.org',
331            'test mail receiver', EXTERNAL_MAIL_RECEIVER,
332            'Test Mail from WAeUP Kofa',
333            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
334            )
335        import transaction
336        transaction.commit() # The mail is really sent when transactions is
337                             # committed
338        self.assertTrue('@' in result)
339        return
340
341    @external_mail_test
342    def test_send_queued_mail(self):
343        # send mail using queued mail delivery
344        self.app['configuration'].smtp_mailer = EXTERNAL_QUEUED_DELIVERY
345        setSite(self.app)
346        result = send_mail(
347            'test program', 'no-reply@waeup.org',
348            'test mail receiver', EXTERNAL_MAIL_RECEIVER,
349            'Test Mail from WAeUP Kofa',
350            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
351            )
352        import transaction
353        transaction.commit() # The mail is really sent when transactions is
354                             # committed
355        self.assertTrue('@' in result)
356        return
Note: See TracBrowser for help on using the repository browser.