| | 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 time |
| | 29 | import unittest |
| | 30 | |
| | 31 | logging.disable(logging.ERROR) |
| | 32 | |
| | 33 | import turbogears |
| | 34 | import turbomail |
| | 35 | |
| | 36 | from turbomail_tests.lib.smtp_mailsink import SMTPMailsink |
| | 37 | from turbomail_tests.lib.utils import get_received_mail, save_config |
| | 38 | |
| | 39 | |
| | 40 | class TestErrorHandling(unittest.TestCase): |
| | 41 | "Test cases for error conditions and error handling inside of TurboMail." |
| | 42 | |
| | 43 | def setUp(self): |
| | 44 | self.server_port = 42042 |
| | 45 | test_config = {'mail.on': True, 'mail.timeout': 1, |
| | 46 | 'mail.server': 'localhost:%d' % self.server_port} |
| | 47 | self._original_config = save_config(test_config.keys()) |
| | 48 | turbogears.config.update(test_config) |
| | 49 | self.sink = None |
| | 50 | turbogears.startup.startTurboGears() |
| | 51 | |
| | 52 | |
| | 53 | def tearDown(self): |
| | 54 | turbogears.startup.stopTurboGears() |
| | 55 | turbogears.config.update(self._original_config) |
| | 56 | if self.sink != None: |
| | 57 | self.sink.stop() |
| | 58 | |
| | 59 | |
| | 60 | def test_connection_refused(self): |
| | 61 | """Test that a message is not lost if the mail hub was unavailable for |
| | 62 | a short period of time.""" |
| | 63 | message = turbomail.Message('sender@foo.example', |
| | 64 | 'recipient@foo.example', 'foo bar') |
| | 65 | message.plain = 'Hello World!' |
| | 66 | turbomail.enqueue(message) |
| | 67 | time.sleep(1) |
| | 68 | self.sink = SMTPMailsink(host='localhost', port=self.server_port) |
| | 69 | self.sink.start() |
| | 70 | msginfo = get_received_mail(self.sink) |
| | 71 | msg = email.message_from_string(msginfo['mail']) |
| | 72 | assert 'Hello World' in msg.get_payload() |
| | 73 | |