source: main/waeup.custom/trunk/src/waeup/custom/tests/test_smtp.py @ 8019

Last change on this file since 8019 was 7822, checked in by Henrik Bettermann, 13 years ago

Rename sirp to kofa.

  • Property svn:keywords set to Id
File size: 4.5 KB
RevLine 
[7504]1# -*- coding: utf-8 -*-
2## $Id: test_smtp.py 7822 2012-03-09 07:26:31Z 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
[7822]29from waeup.kofa.app import University
30from waeup.kofa.interfaces import IMailService
31from waeup.kofa.smtp import (
[7504]32    encode_header_item, encode_address, encode_body, FakeSMTPDelivery,
33    send_mail)
[7822]34from waeup.kofa.testing import FunctionalTestCase
[7504]35from waeup.custom.testing import FunctionalLayer
36
37#
38# SMTP test config
39#
40
41# Also run tests that send mail to external servers?
42#   If you enable this, please make sure the external smtp hosts and receivers
43#   do exist really and are not bothered by being spammed by a test programme.
44EXTERNAL_MAIL_TESTS = False
45
46# Maybe existing receiver of externally sent mail. If this mail account
47# exists really, it might receive mail from the tests!
48EXTERNAL_MAIL_RECEIVER = 'existing_address@waeup.org'
49
50# Names of mail deliveries to use when external mail tests are enabled.
51#   See local mail.zcml for names of available deliveries.
52EXTERNAL_DIRECT_DELIVERY = 'Direct SMTP on mail server'
53EXTERNAL_QUEUED_DELIVERY = 'Queued SMTP on mail server'
54
55#
56# end of SMTP test config
57#
58
59def 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
69class ExternalMailerTests(FunctionalTestCase):
70
71    layer = FunctionalLayer
72
73    def setUp(self):
74        super(ExternalMailerTests, self).setUp()
75        # Setup a sample site for each test
76        app = University()
77        self.dc_root = tempfile.mkdtemp()
78        app['datacenter'].setStoragePath(self.dc_root)
79
80        # Prepopulate the ZODB...
81        self.getRootFolder()['app'] = app
82        self.app = self.getRootFolder()['app']
83        return
84
85    def tearDown(self):
86        super(ExternalMailerTests, self).tearDown()
87        shutil.rmtree(self.dc_root)
88        return
89
90    def test_config_default_mailer(self):
91        # The default mailer set in config is 'no mailer'.
92        self.assertEqual(
93            getattr(self.app.get('configuration', None), 'smtp_mailer'),
94            u'No email service')
95        return
96
97    @external_mail_test
98    def test_send_direct_mail(self):
99        # send mail using direct mail delivery
100        self.app['configuration'].smtp_mailer = EXTERNAL_DIRECT_DELIVERY
101        setSite(self.app)
102        result = send_mail(
103            'test program', 'no-reply@waeup.org',
104            'test mail receiver', EXTERNAL_MAIL_RECEIVER,
[7822]105            'Test Mail from WAeUP Kofa',
[7504]106            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
107            )
108        import transaction
109        transaction.commit() # The mail is really sent when transactions is
110                             # committed
111        self.assertTrue('@' in result)
112        return
113
114    @external_mail_test
115    def test_send_queued_mail(self):
116        # send mail using queued mail delivery
117        self.app['configuration'].smtp_mailer = EXTERNAL_QUEUED_DELIVERY
118        setSite(self.app)
119        result = send_mail(
120            'test program', 'no-reply@waeup.org',
121            'test mail receiver', EXTERNAL_MAIL_RECEIVER,
[7822]122            'Test Mail from WAeUP Kofa',
[7504]123            'Hi from test mailer!\n\nRegards,\nTest Programme\n'
124            )
125        import transaction
126        transaction.commit() # The mail is really sent when transactions is
127                             # committed
128        self.assertTrue('@' in result)
129        return
Note: See TracBrowser for help on using the repository browser.