| 1 | diff -r 87ae6cd83bb3 tests/test_custom_provider_injection.py |
|---|
| 2 | --- /dev/null Thu Jan 01 00:00:00 1970 +0000 |
|---|
| 3 | +++ b/tests/test_custom_provider_injection.py Mon Jul 14 08:41:31 2008 +0200 |
|---|
| 4 | @@ -0,0 +1,49 @@ |
|---|
| 5 | + |
|---|
| 6 | + |
|---|
| 7 | +'''Test the injection of custom TurboMail providers without setuptools.''' |
|---|
| 8 | + |
|---|
| 9 | +import logging |
|---|
| 10 | +import unittest |
|---|
| 11 | + |
|---|
| 12 | +from turbomail.api import Manager, Provider |
|---|
| 13 | +from turbomail.control import interface |
|---|
| 14 | +from turbomail.message import Message |
|---|
| 15 | + |
|---|
| 16 | +logging.disable(logging.WARNING) |
|---|
| 17 | + |
|---|
| 18 | + |
|---|
| 19 | +class DummyProvider(Provider): |
|---|
| 20 | + def deliver(self, message): |
|---|
| 21 | + return True |
|---|
| 22 | + |
|---|
| 23 | + |
|---|
| 24 | +class DummyManager(Manager): |
|---|
| 25 | + def deliver(self, message): |
|---|
| 26 | + return True |
|---|
| 27 | + |
|---|
| 28 | + |
|---|
| 29 | +class TestCustomProviderInjection(unittest.TestCase): |
|---|
| 30 | + """Test the injection of custom TurboMail providers without setuptools.""" |
|---|
| 31 | + |
|---|
| 32 | + def setUp(self): |
|---|
| 33 | + self.manager = DummyManager() |
|---|
| 34 | + self.provider = DummyProvider() |
|---|
| 35 | + self.message = Message( |
|---|
| 36 | + from_=("Author", "author@example.com"), |
|---|
| 37 | + to=("Recipient", "recipient@example.com"), |
|---|
| 38 | + subject="Test message subject.", |
|---|
| 39 | + plain="This is a test message plain text body." |
|---|
| 40 | + ) |
|---|
| 41 | + interface.config = {'mail.on': True} |
|---|
| 42 | + |
|---|
| 43 | + def tearDown(self): |
|---|
| 44 | + interface.stop(force=True) |
|---|
| 45 | + interface.config = {'mail.on': False} |
|---|
| 46 | + |
|---|
| 47 | + def test_inject_custom_manager_and_provider(self): |
|---|
| 48 | + interface.start(manager=self.manager, provider=self.provider) |
|---|
| 49 | + self.assertEqual(self.manager, interface.manager) |
|---|
| 50 | + self.assertEqual(self.provider, interface.provider) |
|---|
| 51 | + interface.send(self.message) |
|---|
| 52 | + |
|---|
| 53 | + |
|---|
| 54 | diff -r 87ae6cd83bb3 turbomail/control.py |
|---|
| 55 | --- a/turbomail/control.py Sun Jul 13 19:37:04 2008 +0200 |
|---|
| 56 | +++ b/turbomail/control.py Mon Jul 14 08:41:31 2008 +0200 |
|---|
| 57 | @@ -50,29 +50,33 @@ |
|---|
| 58 | return entrypoint.load() |
|---|
| 59 | return None |
|---|
| 60 | |
|---|
| 61 | - def start(self): |
|---|
| 62 | + def start(self, manager=None, provider=None): |
|---|
| 63 | if not self.config.get("mail.on", False): |
|---|
| 64 | return |
|---|
| 65 | |
|---|
| 66 | log.info("TurboMail extension starting up.") |
|---|
| 67 | |
|---|
| 68 | - def load(t, default): |
|---|
| 69 | - extension = self.config.get("mail.%s" % t, default) |
|---|
| 70 | - controller = self.__load_single_entry("turbomail.%ss" % t, extension) |
|---|
| 71 | - if not controller: |
|---|
| 72 | - self.config.update({"mail.on": False}) |
|---|
| 73 | - log.error("Unable to locate %s %s, TurboMail disabled." % (extension, t)) |
|---|
| 74 | - self.stop(force=True) |
|---|
| 75 | - return |
|---|
| 76 | - setattr(self, t, controller) |
|---|
| 77 | - if hasattr(getattr(self, t), 'load'): |
|---|
| 78 | - setattr(self, t, getattr(self, t).load()) |
|---|
| 79 | - if hasattr(getattr(self, t), 'start'): |
|---|
| 80 | - getattr(self, t).start() |
|---|
| 81 | + def load(t, default, custom_controller): |
|---|
| 82 | + if custom_controller != None: |
|---|
| 83 | + setattr(self, t, custom_controller) |
|---|
| 84 | + else: |
|---|
| 85 | + extension = self.config.get("mail.%s" % t, default) |
|---|
| 86 | + entry_point = "turbomail.%ss" % t |
|---|
| 87 | + controller = self.__load_single_entry(entry_point, extension) |
|---|
| 88 | + if not controller: |
|---|
| 89 | + self.config.update({"mail.on": False}) |
|---|
| 90 | + log.error("Unable to locate %s %s, TurboMail disabled." % (extension, t)) |
|---|
| 91 | + self.stop(force=True) |
|---|
| 92 | + return |
|---|
| 93 | + setattr(self, t, controller) |
|---|
| 94 | + if hasattr(getattr(self, t), 'load'): |
|---|
| 95 | + setattr(self, t, getattr(self, t).load()) |
|---|
| 96 | + if hasattr(getattr(self, t), 'start'): |
|---|
| 97 | + getattr(self, t).start() |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | - load('manager', 'immediate') |
|---|
| 101 | - load('provider', 'debug') |
|---|
| 102 | + load('manager', 'immediate', manager) |
|---|
| 103 | + load('provider', 'debug', provider) |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | extensions = pkg_resources.iter_entry_points("turbomail.extensions") |
|---|