| | 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 | |
| | 26 | import email |
| | 27 | import logging |
| | 28 | import unittest |
| | 29 | |
| | 30 | logging.disable(logging.WARNING) |
| | 31 | |
| | 32 | import turbogears |
| | 33 | import turbomail |
| | 34 | |
| | 35 | from turbomail_tests.lib.smtp_mailsink import SMTPMailsink |
| | 36 | from turbomail_tests.lib.utils import get_received_mail, save_config |
| | 37 | |
| | 38 | |
| | 39 | class 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 |
| | 66 | To: foo@xams.example |
| | 67 | Subject: foo bar |
| | 68 | MIME-Version: 1.0 |
| | 69 | Content-Type: text/plain; |
| | 70 | Date: Fri, 15 Dec 2006 01:54:00 +0200 |
| | 71 | |
| | 72 | Hello 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 | |