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

Last change on this file since 16299 was 16299, checked in by Henrik Bettermann, 4 years ago

Implement bulk emailing.

  • Property svn:keywords set to Id
File size: 15.8 KB
Line 
1# -*- coding: utf-8 -*-
2## $Id: test_smtp.py 16299 2020-11-04 17:52:22Z 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.',None,
262            u'A cc recipient <recpt@example.com>',
263            u'A bcc recipient <recpt@example.com>,A 2nd bcc recipient <recpt@example.com>',
264            )
265        self.assertEqual(mail_id, 'fake-message-id@example.com')
266        self.assertEqual(
267            self.get_fake_smtp_output().split('\n'),
268            [u'Sending email from no-reply@waeup.org to '
269             u'recpt@example.com, sender@example.com:',
270             u'Message:',
271             u'msg: MIME-Version: 1.0',
272             u'msg: Content-Type: text/plain; charset="us-ascii"',
273             u'msg: Content-Transfer-Encoding: 7bit',
274             u'msg: From: A sender <no-reply@waeup.org>',
275             u'msg: To: A recipient <recpt@example.com>',
276             u'msg: Cc: A cc recipient <recpt@example.com>',
277             u'msg: Bcc: A bcc recipient <recpt@example.com>,',
278             u'msg:  A 2nd bcc recipient <recpt@example.com>',
279             u'msg: Reply-To: A sender <sender@example.com>',
280             u'msg: Subject: A subject',
281             u'msg: ',
282             u'msg: This is a test mail.',
283             u'']
284            )
285        return
286
287    def test_send_mail_utf8_strings(self):
288        # we can send mail with utf-8 encoded strings as input
289        mail_id = send_mail(
290            u'A sender', u'sender@example.com',
291            u'A recipient', u'recpt@example.com',
292            u'A subject',
293            u'This is a test mail with ümläut.'.encode('utf-8'))
294        self.assertEqual(mail_id, 'fake-message-id@example.com')
295        self.assertEqual(
296            self.get_fake_smtp_output().split('\n'),
297            [u'Sending email from no-reply@waeup.org '
298             u'to recpt@example.com, sender@example.com:',
299             u'Message:', u'msg: MIME-Version: 1.0',
300             u'msg: Content-Type: text/plain; charset="iso-8859-1"',
301             u'msg: Content-Transfer-Encoding: quoted-printable',
302             u'msg: From: A sender <no-reply@waeup.org>',
303             u'msg: To: A recipient <recpt@example.com>',
304             # u'msg: Cc: A sender <sender@example.com>',
305             u'msg: Reply-To: A sender <sender@example.com>',
306             u'msg: Subject: A subject',
307             u'msg: ',
308             u'msg: This is a test mail with =FCml=E4ut.',
309             u'']
310            )
311        return
312
313    def test_send_mail_utf8_unicode(self):
314        # we can send mail with utf-8 encoded unicode as input
315        mail_id = send_mail(
316            u'A sender', u'sender@example.com',
317            u'A recipient', u'recpt@example.com',
318            u'A subject',
319            u'This is a test mail with ümläut.')
320        self.assertEqual(mail_id, 'fake-message-id@example.com')
321        self.assertEqual(
322            self.get_fake_smtp_output().split('\n'),
323            [u'Sending email from no-reply@waeup.org '
324             u'to recpt@example.com, sender@example.com:',
325             u'Message:', u'msg: MIME-Version: 1.0',
326             u'msg: Content-Type: text/plain; charset="iso-8859-1"',
327             u'msg: Content-Transfer-Encoding: quoted-printable',
328             u'msg: From: A sender <no-reply@waeup.org>',
329             u'msg: To: A recipient <recpt@example.com>',
330             # u'msg: Cc: A sender <sender@example.com>',
331             u'msg: Reply-To: A sender <sender@example.com>',
332             u'msg: Subject: A subject',
333             u'msg: ',
334             u'msg: This is a test mail with =FCml=E4ut.',
335             u'']
336            )
337        return
338
339
340class ExternalMailerTests(FunctionalTestCase):
341
342    layer = FunctionalLayer
343
344    def setUp(self):
345        super(ExternalMailerTests, self).setUp()
346        # Setup a sample site for each test
347        app = University()
348        self.dc_root = tempfile.mkdtemp()
349        app['datacenter'].setStoragePath(self.dc_root)
350
351        # Prepopulate the ZODB...
352        self.getRootFolder()['app'] = app
353        self.app = self.getRootFolder()['app']
354        return
355
356    def tearDown(self):
357        super(ExternalMailerTests, self).tearDown()
358        shutil.rmtree(self.dc_root)
359        return
360
361    def test_config_default_mailer(self):
362        # The default mailer set in config is 'no mailer'.
363        self.assertEqual(
364            getattr(self.app.get('configuration', None), 'smtp_mailer'),
365            u'No email service')
366        return
367
368    @external_mail_test
369    def test_send_direct_mail(self):
370        # send mail using direct mail delivery
371        self.app['configuration'].smtp_mailer = EXTERNAL_DIRECT_DELIVERY
372        setSite(self.app)
373        result = send_mail(
374            'test program', 'no-reply@waeup.org',
375            'test mail receiver', EXTERNAL_MAIL_RECEIVER,
376            'Test Mail from WAeUP Kofa',
377            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
378            )
379        import transaction
380        transaction.commit()  # The mail is really sent when transactions is
381                              # committed
382        self.assertTrue('@' in result)
383        return
384
385    @external_mail_test
386    def test_send_direct_mails(self):
387        # send several mails using direct mail delivery
388        self.app['configuration'].smtp_mailer = EXTERNAL_DIRECT_DELIVERY
389        setSite(self.app)
390        result = send_mail(
391            'test program', 'no-reply@waeup.org',
392            'test mail receiver',
393            '%s, %s' % (EXTERNAL_MAIL_RECEIVER, EXTERNAL_MAIL_RECEIVER),
394            'Test Mail from WAeUP Kofa',
395            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
396            )
397        import transaction
398        transaction.commit()  # The mail is really sent when transactions is
399                              # committed
400        self.assertTrue('@' in result)
401        return
402
403    @external_mail_test
404    def test_send_queued_mail(self):
405        # send mail using queued mail delivery
406        self.app['configuration'].smtp_mailer = EXTERNAL_QUEUED_DELIVERY
407        setSite(self.app)
408        result = send_mail(
409            'test program', 'no-reply@waeup.org',
410            'test mail receiver', EXTERNAL_MAIL_RECEIVER,
411            'Test Mail from WAeUP Kofa',
412            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
413            )
414        import transaction
415        transaction.commit()  # The mail is really sent when transactions is
416                              # committed
417        self.assertTrue('@' in result)
418        return
Note: See TracBrowser for help on using the repository browser.