def test_routes_deliver_to_not_existing_address(self): Router.load(['inboxen.router.app.server']) message = MailRequest("locahost", "test@localhost", "root1@localhost", TEST_MSG) with self.assertRaises(SMTPError) as excp: Router.deliver(message) self.assertEqual(excp.exception.code, 550) self.assertEqual(excp.exception.message, "No such address")
def test_routes(self): from salmon.routing import Router user = factories.UserFactory() inbox = factories.InboxFactory(user=user) Router.load(['app.server']) with mock.patch("router.app.server.Relay") as relay_mock, \ mock.patch("router.app.server.make_email") as mock_make_email, \ mock.patch("salmon.server.smtplib.SMTP") as smtp_mock: deliver_mock = mock.Mock() relay_mock.return_value.deliver = deliver_mock message = MailRequest("locahost", "test@localhost", str(inbox), TEST_MSG) Router.deliver(message) self.assertEqual(mock_make_email.call_count, 1) self.assertEqual(relay_mock.call_count, 0) self.assertEqual(deliver_mock.call_count, 0) mock_make_email.reset_mock() relay_mock.reset_mock() deliver_mock.reset_mock() message = MailRequest("locahost", "test@localhost", "root@localhost", TEST_MSG) Router.deliver(message) self.assertEqual(mock_make_email.call_count, 0) self.assertEqual(relay_mock.call_count, 1) self.assertEqual(deliver_mock.call_count, 1) self.assertEqual(message, deliver_mock.call_args[0][0]) mock_make_email.reset_mock() relay_mock.reset_mock() deliver_mock.reset_mock() message = MailRequest("locahost", "test@localhost", "root1@localhost", TEST_MSG) with self.assertRaises(SMTPError) as excp: Router.deliver(message) self.assertEqual(excp.exception.code, 550) self.assertEqual(excp.exception.message, "No such address") self.assertEqual(mock_make_email.call_count, 0) self.assertEqual(relay_mock.call_count, 0) self.assertEqual(deliver_mock.call_count, 0) mock_make_email.reset_mock() relay_mock.reset_mock() deliver_mock.reset_mock() smtp_mock.return_value.sendmail.side_effect = Exception() message = MailRequest("locahost", "test@localhost", "root@localhost", TEST_MSG) with self.assertRaises(SMTPError) as excp: Router.deliver(message) self.assertEqual(excp.exception.code, 450) self.assertEqual(excp.exception.message, "Error while forwarding admin message %s" % id(message)) self.assertEqual(mock_make_email.call_count, 0) self.assertEqual(relay_mock.call_count, 0) self.assertEqual(deliver_mock.call_count, 0)
def test_routes_deliver_to_admin_raise_smtperror_on_other_errors(self): Router.load(['inboxen.router.app.server']) with mock.patch("salmon.server.smtplib.SMTP") as smtp_mock: smtp_mock.return_value.sendmail.side_effect = Exception() message = MailRequest("locahost", "test@localhost", "root@localhost", TEST_MSG) with self.assertRaises(SMTPError) as excp: Router.deliver(message) self.assertEqual(excp.exception.code, 450) self.assertEqual(excp.exception.message, "Error while forwarding admin message %s" % id(message))
def test_routes_deliver_to_admin_raise_smtperror_on_other_errors(self): Router.load(['inboxen.router.app.server']) with mock.patch("salmon.server.smtplib.SMTP") as smtp_mock: smtp_mock.return_value.sendmail.side_effect = Exception() message = MailRequest("locahost", "test@localhost", "root@localhost", TEST_MSG) with self.assertRaises(SMTPError) as excp: Router.deliver(message) self.assertEqual(excp.exception.code, 450) self.assertEqual( excp.exception.message, "Error while forwarding admin message %s" % id(message))
def test_routes_deliver_to_admin(self): Router.load(['inboxen.router.app.server']) with mock.patch("inboxen.router.app.server.Relay") as relay_mock, \ mock.patch("inboxen.router.app.server.make_email") as mock_make_email: deliver_mock = mock.Mock() relay_mock.return_value.deliver = deliver_mock message = MailRequest("locahost", "test@localhost", "root@localhost", TEST_MSG) Router.deliver(message) self.assertEqual(mock_make_email.call_count, 0) self.assertEqual(relay_mock.call_count, 1) self.assertEqual(deliver_mock.call_count, 1) self.assertEqual(message, deliver_mock.call_args[0][0])
def test_routes_deliver_to_inbox(self): user = factories.UserFactory() inbox = factories.InboxFactory(user=user) Router.load(['inboxen.router.app.server']) with mock.patch("inboxen.router.app.server.Relay") as relay_mock, \ mock.patch("inboxen.router.app.server.make_email") as mock_make_email: deliver_mock = mock.Mock() relay_mock.return_value.deliver = deliver_mock message = MailRequest("locahost", "test@localhost", str(inbox), TEST_MSG) Router.deliver(message) self.assertEqual(mock_make_email.call_count, 1) self.assertEqual(relay_mock.call_count, 0) self.assertEqual(deliver_mock.call_count, 0)
def test_reload_raises(): Router.LOG_EXCEPTIONS = True Router.reload() assert routing.LOG.exception.called Router.LOG_EXCEPTIONS = False routing.LOG.exception.reset_mock() assert_raises(ImportError, Router.reload) assert not routing.LOG.exception.called routing.LOG.exception.reset_mock() Router.LOG_EXCEPTIONS = True Router.load(['fake.handler']) assert routing.LOG.exception.called Router.LOG_EXCEPTIONS = False routing.LOG.exception.reset_mock() assert_raises(ImportError, Router.load, ['fake.handler']) assert not routing.LOG.exception.called
def test_reload_raises(): Router.LOG_EXCEPTIONS = True Router.reload() assert_equal(routing.LOG.exception.call_count, 1) Router.LOG_EXCEPTIONS = False routing.LOG.exception.reset_mock() assert_raises(ImportError, Router.reload) assert_equal(routing.LOG.exception.call_count, 0) routing.LOG.exception.reset_mock() Router.LOG_EXCEPTIONS = True Router.load(['fake.handler']) assert_equal(routing.LOG.exception.call_count, 1) Router.LOG_EXCEPTIONS = False routing.LOG.exception.reset_mock() assert_raises(ImportError, Router.load, ['fake.handler']) assert_equal(routing.LOG.exception.call_count, 0)
def test_reload_raises(self): Router.LOG_EXCEPTIONS = True Router.reload() self.assertEqual(routing.LOG.exception.call_count, 1) Router.LOG_EXCEPTIONS = False routing.LOG.exception.reset_mock() with self.assertRaises(ImportError): Router.reload() self.assertEqual(routing.LOG.exception.call_count, 0) routing.LOG.exception.reset_mock() Router.LOG_EXCEPTIONS = True Router.load(['fake.handler']) self.assertEqual(routing.LOG.exception.call_count, 1) Router.LOG_EXCEPTIONS = False routing.LOG.exception.reset_mock() with self.assertRaises(ImportError): Router.load(['fake.handler']) self.assertEqual(routing.LOG.exception.call_count, 0)
def test_routes(self): from salmon.routing import Router user = factories.UserFactory() inbox = factories.InboxFactory(user=user) Router.load(['app.server']) with mock.patch("router.app.server.Relay") as relay_mock, \ mock.patch("router.app.server.make_email") as mock_make_email: deliver_mock = mock.Mock() relay_mock.return_value.deliver = deliver_mock message = MailRequest("locahost", "test@localhost", str(inbox), TEST_MSG) Router.deliver(message) self.assertEqual(mock_make_email.call_count, 1) self.assertEqual(relay_mock.call_count, 0) self.assertEqual(deliver_mock.call_count, 0) mock_make_email.reset_mock() relay_mock.reset_mock(); deliver_mock.reset_mock(); message = MailRequest("locahost", "test@localhost", "root@localhost", TEST_MSG) Router.deliver(message) self.assertEqual(mock_make_email.call_count, 0) self.assertEqual(relay_mock.call_count, 1) self.assertEqual(deliver_mock.call_count, 1) self.assertEqual(message, deliver_mock.call_args[0][0]) mock_make_email.reset_mock() relay_mock.reset_mock(); deliver_mock.reset_mock(); message = MailRequest("locahost", "test@localhost", "root1@localhost", TEST_MSG) with self.assertRaises(SMTPError): Router.deliver(message) self.assertEqual(mock_make_email.call_count, 0) self.assertEqual(relay_mock.call_count, 0) self.assertEqual(deliver_mock.call_count, 0)
def setup(): Router.clear_routes() Router.clear_states() Router.load(['salmon_tests.simple_fsm_mod'])
from salmon import queue from salmon.routing import Router import logging import logging.config import os logging.config.fileConfig("config/logging.conf") Router.defaults(**{}) Router.load(['app.server']) Router.RELOAD=False Router.LOG_EXCEPTIONS=True Router.UNDELIVERABLE_QUEUE=queue.Queue("run/undeliverable")
from config import settings from salmon import queue from salmon.routing import Router from salmon.server import SMTPReceiver import logging import logging.config logging.config.fileConfig("config/logging.conf") # where to listen for incoming messages settings.receiver = SMTPReceiver(settings.receiver_config['host'], settings.receiver_config['port']) Router.defaults(**settings.router_defaults) Router.load(settings.handlers) Router.RELOAD=False Router.LOG_EXCEPTIONS=True Router.UNDELIVERABLE_QUEUE=queue.Queue("run/undeliverable")
logging.config.fileConfig("config/logging.conf") settings.receiver = QueueReceiver(settings.SENDER_QUEUE_PATH, settings.sender_queue_sleep) # the relay host to actually send the final message to settings.relay = Relay(host=settings.relay_config['host'], port=settings.relay_config['port'], debug=1) # when silent, it won't reply to emails it doesn't know settings.silent = settings.is_silent_config # owner email to send all unknown emails settings.owner_email = settings.owner_email_config # server for email settings.server_name = settings.server_name_config # server for website settings.web_server_name = settings.server_name_config # start backend table.r = table.start_table() Router.defaults(**settings.router_defaults) Router.load(settings.sender_handlers) Router.RELOAD=True Router.SEND_QUEUE=queue.Queue(settings.SENDER_QUEUE_PATH) # mails to send Router.UNDELIVERABLE_QUEUE=queue.Queue("run/error_send") Router.STATE_STORE=state_storage.UserStateStorage()
from config import settings from salmon.routing import Router from salmon.server import Relay, QueueReceiver from salmon import view import logging import logging.config import jinja2 # configure logging to go to a log file logging.config.fileConfig("config/logging.conf") # the relay host to actually send the final message to settings.relay = Relay(host=settings.relay_config['host'], port=settings.relay_config['port'], debug=1) # where to listen for incoming messages settings.receiver = QueueReceiver('run/undeliverable', settings.queue_config['sleep']) Router.defaults(**settings.router_defaults) Router.load(['salmon.handlers.forward']) Router.RELOAD=True Router.UNDELIVERABLE_QUEUE=None view.LOADER = jinja2.Environment( loader=jinja2.PackageLoader(settings.template_config['dir'], settings.template_config['module']))
# Fake boot module for tests from salmon.routing import Router Router.defaults(host="localhost") Router.load([]) Router.RELOAD = False Router.LOG_EXCEPTIONS = False
import logging import logging.config from salmon import queue from salmon.routing import Router from salmon.server import SMTPReceiver from . import settings logging.config.fileConfig("tests/config/logging.conf") settings.receiver = SMTPReceiver(**settings.receiver_config) Router.defaults(**settings.router_defaults) Router.load(settings.dump_handlers) Router.RELOAD = True Router.UNDELIVERABLE_QUEUE = queue.Queue(settings.UNDELIVERABLE_QUEUE)
import jinja2 import logging import logging.config import os # configure logging to go to a log file logging.config.fileConfig("config/test_logging.conf") # the relay host to actually send the final message to (set debug=1 to see what # the relay is saying to the log server). settings.relay = Relay(host=settings.relay_config['host'], port=settings.relay_config['port'], debug=0) settings.receiver = None Router.defaults(**settings.router_defaults) Router.load(settings.handlers + settings.queue_handlers) Router.RELOAD=True view.LOADER = jinja2.Environment( loader=jinja2.PackageLoader(settings.template_config['dir'], settings.template_config['module'])) # if you have pyenchant and enchant installed then the template tests will do # spell checking for you, but you need to tell pyenchant where to find itself if 'PYENCHANT_LIBRARY_PATH' not in os.environ: os.environ['PYENCHANT_LIBRARY_PATH'] = '/opt/local/lib/libenchant.dylib'
def setUp(self): super(BounceTestCase, self).setUp() Router.load(["tests.handlers.bounce_filtered_mod"]) Router.reload()
def setup_router(handlers): Router.clear_routes() Router.clear_states() Router.HANDLERS.clear() Router.load(handlers) Router.reload()
def setup_router(handlers): Router.clear_routes() Router.clear_states() Router.HANDLERS.clear() Router.load(handlers)
## import logging import logging.config import os from django.conf import settings as dj_settings from salmon import queue from salmon.routing import Router from inboxen.router.config import settings __all__ = ["settings"] try: os.mkdir("logs", 0o700) except OSError: pass try: os.mkdir("run", 0o710) # group can access files in "run" except OSError: pass logging.config.dictConfig(dj_settings.SALMON_LOGGING) Router.load(['inboxen.router.app.server']) Router.RELOAD = False Router.LOG_EXCEPTIONS = True Router.UNDELIVERABLE_QUEUE = queue.Queue("run/undeliverable")
import logging import logging.config import os import sys from salmon import queue from salmon.routing import Router from salmon.server import SMTPReceiver, Relay, QueueReceiver if "PERMEABILITY_ENV" in os.environ: import testing_settings else: from tests import testing_settings logging.config.fileConfig("../config/test_logging_relay.conf") testing_settings.relay = Relay(host=testing_settings.relay_config['host'], port=testing_settings.relay_config['port'], debug=1) testing_settings.receiver = QueueReceiver(testing_settings.receiver_config['maildir']) Router.defaults(**testing_settings.router_defaults) Router.load(testing_settings.handlers) Router.RELOAD = True Router.UNDELIVERABLE_QUEUE = queue.Queue("./undeliverable")
from config import settings from salmon.routing import Router from salmon.server import Relay, QueueReceiver from salmon import view import logging import logging.config import jinja2 # configure logging to go to a log file logging.config.fileConfig("config/logging.conf") # the relay host to actually send the final message to settings.relay = Relay(host=settings.relay_config['host'], port=settings.relay_config['port'], debug=1) # where to listen for incoming messages settings.receiver = QueueReceiver('run/undeliverable', settings.queue_config['sleep']) Router.defaults(**settings.router_defaults) Router.load(['salmon.handlers.forward']) Router.RELOAD = True Router.UNDELIVERABLE_QUEUE = None view.LOADER = jinja2.Environment(loader=jinja2.PackageLoader( settings.template_config['dir'], settings.template_config['module']))
import logging.config from salmon.routing import Router from salmon.server import Relay from config import settings logging.config.fileConfig("config/test_logging.conf") # the relay host to actually send the final message to (set debug=1 to see what # the relay is saying to the log server). settings.relay = Relay(host=settings.relay_config['host'], port=settings.relay_config['port'], debug=0) settings.receiver = None Router.defaults(**settings.router_defaults) Router.load(settings.handlers) Router.RELOAD = True Router.LOG_EXCEPTIONS = False