Ticket #16: messages_as_strings.patch

File messages_as_strings.patch, 4.0 kB (added by fs, 1 year ago)
  • turbomail_tests/test_message_as_string.py

     
     1#!/usr/bin/env python 
     2# -*- coding: UTF-8 -*- 
     3 
     4# Copyright (c) 2007 Felix Schwarz <felix.schwarz@schwarz.eu> 
     5# 
     6# This code is placed under the MIT license: 
     7# 
     8# Permission is hereby granted, free of charge, to any person obtaining a copy  
     9# of this software and associated documentation files (the "Software"), to deal  
     10# in the Software without restriction, including without limitation the rights  
     11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
     12# copies of the Software, and to permit persons to whom the Software is  
     13# furnished to do so, subject to the following conditions: 
     14# 
     15# The above copyright notice and this permission notice shall be included in  
     16# all copies or substantial portions of the Software. 
     17# 
     18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
     19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
     20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
     21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
     22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
     23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  
     24# SOFTWARE. 
     25 
     26import email 
     27import logging 
     28import unittest 
     29 
     30logging.disable(logging.WARNING) 
     31 
     32import turbogears 
     33import turbomail 
     34 
     35from turbomail_tests.lib.smtp_mailsink import SMTPMailsink 
     36from turbomail_tests.lib.utils import get_received_mail, save_config 
     37 
     38 
     39class TestMessageAsString(unittest.TestCase): 
     40    "Test cases for sending emails as plain strings with TurboMail." 
     41 
     42    def setUp(self): 
     43        server_port = 42042 
     44        test_config = {'mail.on': True, 'mail.timeout': 1, 
     45                       'mail.server': 'localhost:%d' % server_port} 
     46        self._original_config = save_config(test_config.keys()) 
     47        turbogears.config.update(test_config) 
     48         
     49        self.sink = SMTPMailsink(host='localhost', port=server_port) 
     50        self.sink.start() 
     51        turbogears.startup.startTurboGears() 
     52 
     53 
     54    def tearDown(self): 
     55        turbogears.startup.stopTurboGears() 
     56        turbogears.config.update(self._original_config) 
     57        self.sink.stop() 
     58 
     59 
     60    def test_message_string(self): 
     61        """Test that a message can be submitted as string (not as  
     62        turbomail.Message). This is important when existing (e.g. from a  
     63        mailbox) or digitally signed messages should be sent through  
     64        TurboMail.""" 
     65        msg_string = """From: bar@xams.example 
     66To: foo@xams.example 
     67Subject: foo bar 
     68MIME-Version: 1.0 
     69Content-Type: text/plain; 
     70Date: Fri, 15 Dec 2006 01:54:00 +0200 
     71 
     72Hello World!""" 
     73        sender = 'sender@foobar.example' 
     74        recipient = 'recipient@foo.example' 
     75        msginfo = dict(sender=sender, recipients=[recipient], message=msg_string) 
     76        turbomail.enqueue(msginfo) 
     77 
     78        msginfo = get_received_mail(self.sink) 
     79        self.assertEqual(sender, msginfo['from']) 
     80        self.assertEqual([recipient], msginfo['recipients']) 
     81        self.assertEqual(msg_string, msginfo['mail']) 
     82 
  • turbomail/turbomail/dispatch.py

     
    155155                 
    156156                pack.update(packUpdate) 
    157157                 
     158                if not pack.has_key('to'): 
     159                        pack['to'] = pack['recipients'] 
     160 
    158161                if len(pack['to']) > 1: 
    159162                        pack.update(dict(addrto="- (%d)" % len(pack['to']))) 
    160163                else: 
     
    162165                                pack.update(dict(addrto='"%s" %s' % pack['to'][0])) 
    163166                        else: 
    164167                                pack.update(dict(addrto='- %s' % pack['to'][0])) 
     168                                 
     169                if not pack.has_key('subject'): 
     170                        pack['subject'] = '' 
    165171 
    166172                log.info( 
    167173                                "%(user)s%(server)s %(size)d %(addrfrom)s %(addrto)s - %(subject)s" % pack