diff options
-rw-r--r-- | service/pixelated/adapter/services/mail_sender.py | 23 | ||||
-rw-r--r-- | service/pixelated/bitmask_libraries/session.py | 11 | ||||
-rw-r--r-- | service/pixelated/bitmask_libraries/smtp.py | 40 | ||||
-rw-r--r-- | service/pixelated/config/services.py | 8 | ||||
-rw-r--r-- | service/test/unit/adapter/services/test_mail_sender.py | 62 | ||||
-rw-r--r-- | service/test/unit/bitmask_libraries/test_smtp.py | 62 |
6 files changed, 8 insertions, 198 deletions
diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py index 4ffb76b0..bcbf331c 100644 --- a/service/pixelated/adapter/services/mail_sender.py +++ b/service/pixelated/adapter/services/mail_sender.py @@ -84,26 +84,3 @@ class MailSender(object): def _create_twisted_smtp_recipient(self, recipient): return User(str(recipient), NOT_NEEDED, NOT_NEEDED, NOT_NEEDED) - - -class LocalSmtpMailSender(object): - - def __init__(self, account_email_address, smtp): - self.smtp = smtp - self.account_email_address = account_email_address - - def sendmail(self, mail): - if self.smtp.ensure_running(): - recipients = flatten([mail.to, mail.cc, mail.bcc]) - result_deferred = Deferred() - sender_factory = SMTPSenderFactory( - fromEmail=self.account_email_address, - toEmail=set([parseaddr(recipient)[1] for recipient in recipients]), - file=StringIO(mail.to_smtp_format()), - deferred=result_deferred) - - reactor.connectTCP('localhost', self.smtp.local_smtp_port_number, - sender_factory) - - return result_deferred - return fail(SMTPDownException()) diff --git a/service/pixelated/bitmask_libraries/session.py b/service/pixelated/bitmask_libraries/session.py index 3f8e6de6..53f35008 100644 --- a/service/pixelated/bitmask_libraries/session.py +++ b/service/pixelated/bitmask_libraries/session.py @@ -23,10 +23,10 @@ from twisted.internet import reactor, defer from pixelated.bitmask_libraries.certs import LeapCertificate from pixelated.adapter.mailstore import LeapMailStore from leap.mail.incoming.service import IncomingMail -from leap.auth import SRPAuth from leap.mail.imap.account import IMAPAccount +from leap.auth import SRPAuth from .nicknym import NickNym -from .smtp import LeapSmtp, LeapSMTPConfig +from .smtp import LeapSMTPConfig from .soledad import SoledadSessionFactory from leap.common.events import ( @@ -62,8 +62,8 @@ class LeapSession(object): - ``incoming_mail_fetcher`` Background job for fetching incoming mails from LEAP server (LeapIncomingMail) """ - def __init__(self, provider, user_auth, mail_store, soledad_session, nicknym, smtp): - self.smtp = smtp + def __init__(self, provider, user_auth, mail_store, soledad_session, nicknym, smtp_config): + self.smtp_config = smtp_config self.config = provider.config self.provider = provider self.user_auth = user_auth @@ -184,9 +184,8 @@ class LeapSessionFactory(object): smtp_host, smtp_port = self._provider.smtp_info() smtp_config = LeapSMTPConfig(account_email, self._smtp_client_cert_path(), smtp_host, smtp_port) - smtp = LeapSmtp(smtp_config, nicknym.keymanager) - return LeapSession(self._provider, auth, mail_store, soledad, nicknym, smtp) + return LeapSession(self._provider, auth, mail_store, soledad, nicknym, smtp_config) def _download_smtp_cert(self, auth): cert_path = self._smtp_client_cert_path() diff --git a/service/pixelated/bitmask_libraries/smtp.py b/service/pixelated/bitmask_libraries/smtp.py index f4ab00f7..e05ba51a 100644 --- a/service/pixelated/bitmask_libraries/smtp.py +++ b/service/pixelated/bitmask_libraries/smtp.py @@ -14,8 +14,6 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. import logging -import random -from leap.mail.smtp import setup_smtp_gateway logger = logging.getLogger(__name__) @@ -28,41 +26,3 @@ class LeapSMTPConfig(object): self.cert_path = cert_path self.remote_smtp_host = remote_smtp_host self.remote_smtp_port = remote_smtp_port - - -class LeapSmtp(object): - - def __init__(self, smtp_config, keymanager=None): - self.local_smtp_port_number = random.randrange(12000, 16000) - self._smtp_config = smtp_config - self._keymanager = keymanager - self._local_smtp_service_socket = None - self._local_smtp_service = None - - def start(self): - self._local_smtp_service, self._local_smtp_service_socket = setup_smtp_gateway( - port=self.local_smtp_port_number, - userid=str(self._smtp_config.account_email), - keymanager=self._keymanager, - smtp_host=self._smtp_config.remote_smtp_host.encode('UTF-8'), - smtp_port=self._smtp_config.remote_smtp_port, - smtp_cert=self._smtp_config.cert_path, - smtp_key=self._smtp_config.cert_path, - encrypted_only=False - ) - - def ensure_running(self): - if not self._local_smtp_service: - try: - self.start() - except: - logger.warning("Couldn't start the SMTP server now, will try again when the user tries to use it") - return False - return True - - def stop(self): - if self._local_smtp_service is not None: - self._local_smtp_service_socket.stopListening() - self._local_smtp_service.doStop() - self._local_smtp_service_socket = None - self._local_smtp_service = None diff --git a/service/pixelated/config/services.py b/service/pixelated/config/services.py index e4bf394c..65a67a3d 100644 --- a/service/pixelated/config/services.py +++ b/service/pixelated/config/services.py @@ -1,7 +1,7 @@ from pixelated.adapter.mailstore.searchable_mailstore import SearchableMailStore from pixelated.adapter.services.mail_service import MailService from pixelated.adapter.model.mail import InputMail -from pixelated.adapter.services.mail_sender import LocalSmtpMailSender, MailSender # , MailSender +from pixelated.adapter.services.mail_sender import MailSender from pixelated.adapter.search import SearchEngine from pixelated.adapter.services.draft_service import DraftService from pixelated.adapter.listeners.mailbox_indexer_listener import listen_all_mailboxes @@ -55,11 +55,7 @@ class Services(object): self.search_engine = search_engine def setup_mail_service(self, leap_session, search_engine): - # pixelated_mail_sender = LocalSmtpMailSender( - # leap_session.account_email(), - # leap_session.smtp) - - pixelated_mail_sender = MailSender(leap_session.smtp._smtp_config, leap_session.nicknym.keymanager) + pixelated_mail_sender = MailSender(leap_session.smtp_config, leap_session.nicknym.keymanager) return MailService( pixelated_mail_sender, diff --git a/service/test/unit/adapter/services/test_mail_sender.py b/service/test/unit/adapter/services/test_mail_sender.py index 38da726b..f7103b60 100644 --- a/service/test/unit/adapter/services/test_mail_sender.py +++ b/service/test/unit/adapter/services/test_mail_sender.py @@ -18,8 +18,7 @@ from twisted.mail.smtp import User from twisted.trial import unittest from mockito import mock, when, verify, any, unstub -from pixelated.adapter.services.mail_sender import LocalSmtpMailSender, SMTPDownException, MailSender, \ - MailSenderException +from pixelated.adapter.services.mail_sender import MailSender, MailSenderException from pixelated.adapter.model.mail import InputMail from pixelated.bitmask_libraries.smtp import LeapSMTPConfig from pixelated.support.functional import flatten @@ -77,62 +76,3 @@ class MailSenderTest(unittest.TestCase): except MailSenderException, e: for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): self.assertTrue(recipient in e.email_error_map) - - -class LocalSmtpMailSenderTest(unittest.TestCase): - def setUp(self): - self.smtp = mock() - self.smtp.local_smtp_port_number = 4650 - self.smtp.ensure_running = lambda: True - - def test_sendmail(self): - when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None) - input_mail = InputMail.from_dict(mail_dict()) - mail_sender = LocalSmtpMailSender('someone@somedomain.tld', self.smtp) - - return self._succeed(mail_sender.sendmail(input_mail)) - - def tearDown(self): - unstub() - - def test_sendmail_uses_twisted(self): - when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None) - - input_mail = InputMail.from_dict(mail_dict()) - - mail_sender = LocalSmtpMailSender('someone@somedomain.tld', self.smtp) - - sent_deferred = mail_sender.sendmail(input_mail) - - verify(reactor).connectTCP('localhost', 4650, any()) - - return self._succeed(sent_deferred) - - def test_senmail_returns_deffered(self): - when(reactor).connectTCP('localhost', 4650, any()).thenReturn(None) - input_mail = InputMail.from_dict(mail_dict()) - mail_sender = LocalSmtpMailSender('someone@somedomain.tld', self.smtp) - - deferred = mail_sender.sendmail(input_mail) - - self.assertIsNotNone(deferred) - self.assertTrue(isinstance(deferred, Deferred)) - - return self._succeed(deferred) - - def test_doesnt_send_mail_if_smtp_is_not_running(self): - self.smtp.ensure_running = lambda: False - mail_sender = LocalSmtpMailSender('someone@somedomain.tld', self.smtp) - - deferred = mail_sender.sendmail({}) - - def _assert(_): - self.assertTrue(isinstance(deferred.result.value, SMTPDownException)) - - deferred.addErrback(_assert) - - return deferred - - def _succeed(self, deferred): - deferred.callback(None) - return deferred diff --git a/service/test/unit/bitmask_libraries/test_smtp.py b/service/test/unit/bitmask_libraries/test_smtp.py deleted file mode 100644 index 22b69b9e..00000000 --- a/service/test/unit/bitmask_libraries/test_smtp.py +++ /dev/null @@ -1,62 +0,0 @@ -# -# Copyright (c) 2014 ThoughtWorks, Inc. -# -# Pixelated is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Pixelated is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Pixelated. If not, see <http://www.gnu.org/licenses/>. -import os -from mock import MagicMock, patch -from test_abstract_leap import AbstractLeapTest -from pixelated.bitmask_libraries.smtp import LeapSmtp, LeapSMTPConfig - - -class LeapSmtpTest(AbstractLeapTest): - keymanager = MagicMock() - - def setUp(self): - super(LeapSmtpTest, self).setUp() - self.config.timeout_in_s = 15 - self._smtp_config = LeapSMTPConfig('test_user@some-server.test', self._client_cert_path(), 'smtp.some-server.test', 1234) - - def _client_cert_path(self): - return os.path.join(self.leap_home, 'providers', 'some-server.test', 'keys', 'client', 'smtp.pem') - - @patch('pixelated.bitmask_libraries.smtp.setup_smtp_gateway') - def test_that_start_calls_setup_smtp_gateway(self, gateway_mock): - smtp = LeapSmtp(self._smtp_config, self.keymanager) - - port = 500 - smtp.local_smtp_port_number = port - gateway_mock.return_value = (None, None) - smtp.ensure_running() - - cert_path = self._client_cert_path() - gateway_mock.assert_called_with(smtp_cert=cert_path, userid='test_user@some-server.test', smtp_port=1234, smtp_key=cert_path, keymanager=self.keymanager, encrypted_only=False, smtp_host='smtp.some-server.test', port=port) - - def test_that_client_stop_does_nothing_if_not_started(self): - smtp = LeapSmtp(self._smtp_config, self.keymanager) - - smtp.stop() - - @patch('pixelated.bitmask_libraries.smtp.setup_smtp_gateway') - def test_that_running_smtp_sevice_is_stopped(self, gateway_mock): - smtp = LeapSmtp(self._smtp_config, self.keymanager) - - smtp_service = MagicMock() - smtp_port = MagicMock() - gateway_mock.return_value = (smtp_service, smtp_port) - - smtp.ensure_running() - smtp.stop() - - smtp_port.stopListening.assert_called_with() - smtp_service.doStop.assert_called_with() |