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

Last change on this file since 16000 was 14016, checked in by Henrik Bettermann, 8 years ago

Inform applicant how many emails were sent.

  • Property svn:keywords set to Id
File size: 15.6 KB
Line 
1# -*- coding: utf-8 -*-
2## $Id: test_smtp.py 14016 2016-07-07 06:18:06Z henrik $
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-reply@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
58
59def external_mail_test(func):
60    if not EXTERNAL_MAIL_TESTS:
61        myself = __file__
62        if myself.endswith('.pyc'):
63            myself = myself[:-1]
64        print "WARNING: external mail tests are skipped!"
65        print "WARNING: edit %s to enable them." % myself
66        return
67    return func
68
69
70class 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')
79        result5 = encode_header_item(None)
80        self.assertEqual(result1, u'Plain Name')
81        self.assertEqual(result2, u'=?iso-8859-1?q?Name_with_uml=E4ut?=')
82        self.assertEqual(result3, u'Plain Name')
83        self.assertEqual(result4, u'=?iso-8859-1?q?Name_with_uml=E4ut?=')
84        self.assertTrue(result5 is None)
85        return
86
87    def test_encode_address(self):
88        # we can correctly encode address parts
89        result1 = encode_address('foo@bar.baz')
90        result2 = encode_address(u'foo@bar.baz', 'The Foo')
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>')
96        self.assertEqual(result4,
97                         '=?iso-8859-1?q?With_Uml=E4ut?= <foo@bar.baz>')
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)
145        text = text.encode('utf-8')  # turn unicode into byte stream
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
178
179class 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'),
238            [u'Sending email from no-reply@waeup.org to '
239             u'recpt@example.com, sender@example.com:',
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',
244             u'msg: From: A sender <no-reply@waeup.org>',
245             u'msg: To: A recipient <recpt@example.com>',
246             # u'msg: Cc: A sender <sender@example.com>',
247             u'msg: Reply-To: A sender <sender@example.com>',
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_with_cc(self):
256        # with cc=True the message will be CCed to the from address
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.',
262            cc=True)
263        self.assertEqual(mail_id, 'fake-message-id@example.com')
264        self.assertEqual(
265            self.get_fake_smtp_output().split('\n'),
266            [u'Sending email from no-reply@waeup.org to '
267             u'recpt@example.com, sender@example.com:',
268             u'Message:',
269             u'msg: MIME-Version: 1.0',
270             u'msg: Content-Type: text/plain; charset="us-ascii"',
271             u'msg: Content-Transfer-Encoding: 7bit',
272             u'msg: From: A sender <no-reply@waeup.org>',
273             u'msg: To: A recipient <recpt@example.com>',
274             u'msg: Cc: A sender <sender@example.com>',
275             u'msg: Reply-To: A sender <sender@example.com>',
276             u'msg: Subject: A subject',
277             u'msg: ',
278             u'msg: This is a test mail.',
279             u'']
280            )
281        return
282
283    def test_send_mail_utf8_strings(self):
284        # we can send mail with utf-8 encoded strings as input
285        mail_id = send_mail(
286            u'A sender', u'sender@example.com',
287            u'A recipient', u'recpt@example.com',
288            u'A subject',
289            u'This is a test mail with ümläut.'.encode('utf-8'))
290        self.assertEqual(mail_id, 'fake-message-id@example.com')
291        self.assertEqual(
292            self.get_fake_smtp_output().split('\n'),
293            [u'Sending email from no-reply@waeup.org '
294             u'to recpt@example.com, sender@example.com:',
295             u'Message:', u'msg: MIME-Version: 1.0',
296             u'msg: Content-Type: text/plain; charset="iso-8859-1"',
297             u'msg: Content-Transfer-Encoding: quoted-printable',
298             u'msg: From: A sender <no-reply@waeup.org>',
299             u'msg: To: A recipient <recpt@example.com>',
300             # u'msg: Cc: A sender <sender@example.com>',
301             u'msg: Reply-To: A sender <sender@example.com>',
302             u'msg: Subject: A subject',
303             u'msg: ',
304             u'msg: This is a test mail with =FCml=E4ut.',
305             u'']
306            )
307        return
308
309    def test_send_mail_utf8_unicode(self):
310        # we can send mail with utf-8 encoded unicode as input
311        mail_id = send_mail(
312            u'A sender', u'sender@example.com',
313            u'A recipient', u'recpt@example.com',
314            u'A subject',
315            u'This is a test mail with ümläut.')
316        self.assertEqual(mail_id, 'fake-message-id@example.com')
317        self.assertEqual(
318            self.get_fake_smtp_output().split('\n'),
319            [u'Sending email from no-reply@waeup.org '
320             u'to recpt@example.com, sender@example.com:',
321             u'Message:', u'msg: MIME-Version: 1.0',
322             u'msg: Content-Type: text/plain; charset="iso-8859-1"',
323             u'msg: Content-Transfer-Encoding: quoted-printable',
324             u'msg: From: A sender <no-reply@waeup.org>',
325             u'msg: To: A recipient <recpt@example.com>',
326             # u'msg: Cc: A sender <sender@example.com>',
327             u'msg: Reply-To: A sender <sender@example.com>',
328             u'msg: Subject: A subject',
329             u'msg: ',
330             u'msg: This is a test mail with =FCml=E4ut.',
331             u'']
332            )
333        return
334
335
336class ExternalMailerTests(FunctionalTestCase):
337
338    layer = FunctionalLayer
339
340    def setUp(self):
341        super(ExternalMailerTests, self).setUp()
342        # Setup a sample site for each test
343        app = University()
344        self.dc_root = tempfile.mkdtemp()
345        app['datacenter'].setStoragePath(self.dc_root)
346
347        # Prepopulate the ZODB...
348        self.getRootFolder()['app'] = app
349        self.app = self.getRootFolder()['app']
350        return
351
352    def tearDown(self):
353        super(ExternalMailerTests, self).tearDown()
354        shutil.rmtree(self.dc_root)
355        return
356
357    def test_config_default_mailer(self):
358        # The default mailer set in config is 'no mailer'.
359        self.assertEqual(
360            getattr(self.app.get('configuration', None), 'smtp_mailer'),
361            u'No email service')
362        return
363
364    @external_mail_test
365    def test_send_direct_mail(self):
366        # send mail using direct mail delivery
367        self.app['configuration'].smtp_mailer = EXTERNAL_DIRECT_DELIVERY
368        setSite(self.app)
369        result = send_mail(
370            'test program', 'no-reply@waeup.org',
371            'test mail receiver', EXTERNAL_MAIL_RECEIVER,
372            'Test Mail from WAeUP Kofa',
373            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
374            )
375        import transaction
376        transaction.commit()  # The mail is really sent when transactions is
377                              # committed
378        self.assertTrue('@' in result)
379        return
380
381    @external_mail_test
382    def test_send_direct_mails(self):
383        # send several mails using direct mail delivery
384        self.app['configuration'].smtp_mailer = EXTERNAL_DIRECT_DELIVERY
385        setSite(self.app)
386        result = send_mail(
387            'test program', 'no-reply@waeup.org',
388            'test mail receiver',
389            '%s, %s' % (EXTERNAL_MAIL_RECEIVER, EXTERNAL_MAIL_RECEIVER),
390            'Test Mail from WAeUP Kofa',
391            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
392            )
393        import transaction
394        transaction.commit()  # The mail is really sent when transactions is
395                              # committed
396        self.assertTrue('@' in result)
397        return
398
399    @external_mail_test
400    def test_send_queued_mail(self):
401        # send mail using queued mail delivery
402        self.app['configuration'].smtp_mailer = EXTERNAL_QUEUED_DELIVERY
403        setSite(self.app)
404        result = send_mail(
405            'test program', 'no-reply@waeup.org',
406            'test mail receiver', EXTERNAL_MAIL_RECEIVER,
407            'Test Mail from WAeUP Kofa',
408            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
409            )
410        import transaction
411        transaction.commit()  # The mail is really sent when transactions is
412                              # committed
413        self.assertTrue('@' in result)
414        return
Note: See TracBrowser for help on using the repository browser.