From c427efc53d2f373e5a149f4d2a4118ab9a85a15b Mon Sep 17 00:00:00 2001 From: Ola Bini Date: Fri, 20 Jan 2017 15:20:51 -0200 Subject: Rebase previous PR on current master - see PR #915 for details --- service/pixelated/config/leap.py | 13 ++++++-- service/pixelated/config/sessions.py | 21 ++----------- service/test/unit/config/test_leap.py | 51 ++++++++++++++++++++++++++++++- service/test/unit/config/test_sessions.py | 15 ++++++++- 4 files changed, 77 insertions(+), 23 deletions(-) diff --git a/service/pixelated/config/leap.py b/service/pixelated/config/leap.py index 6b9ebf28..52a70f3e 100644 --- a/service/pixelated/config/leap.py +++ b/service/pixelated/config/leap.py @@ -90,9 +90,16 @@ class BootstrapUserServices(object): @defer.inlineCallbacks def setup(self, user_auth, password, language='pt-BR'): - leap_session = yield create_leap_session(self._provider, user_auth.username, password, user_auth) - yield self._setup_user_services(leap_session) - yield self._add_welcome_email(leap_session, language) + leap_session = None + try: + leap_session = yield create_leap_session(self._provider, user_auth.username, password, user_auth) + yield self._setup_user_services(leap_session) + yield self._add_welcome_email(leap_session, language) + except Exception as e: + log.warn('{0}: {1}. Closing session for user: {2}'.format(e.__class__.__name__, e, user_auth.username)) + if leap_session: + leap_session.close() + raise e @defer.inlineCallbacks def _setup_user_services(self, leap_session): diff --git a/service/pixelated/config/sessions.py b/service/pixelated/config/sessions.py index eba55100..2bad3e32 100644 --- a/service/pixelated/config/sessions.py +++ b/service/pixelated/config/sessions.py @@ -118,15 +118,6 @@ class LeapSessionFactory(object): cert = SmtpClientCertificate(self._provider, auth, self._user_path(auth.uuid)) return cert.cert_path() - def _create_dir(self, path): - try: - os.makedirs(path) - except OSError as exc: - if exc.errno == errno.EEXIST and os.path.isdir(path): - pass - else: - raise - def _user_path(self, user_uuid): return os.path.join(leap_config.leap_home, user_uuid) @@ -172,13 +163,7 @@ class LeapSession(object): @defer.inlineCallbacks def finish_bootstrap(self): - try: - yield self.keymanager.generate_openpgp_key() - except UploadKeyError as e: - logger.warn('{0}: {1}. Closing session for user: {2}'.format(e.__class__.__name__, e, self.account_email())) - self.close() - raise - + yield self.keymanager.generate_openpgp_key() yield self._create_account(self.soledad, self.user_auth.uuid) self.incoming_mail_fetcher = yield self._create_incoming_mail_fetcher( self.keymanager, @@ -236,8 +221,8 @@ class LeapSession(object): def sync(self): try: return self.soledad.sync() - except: - traceback.print_exc(file=sys.stderr) + except Exception as e: + logger.error(e) raise diff --git a/service/test/unit/config/test_leap.py b/service/test/unit/config/test_leap.py index 5a68c53a..29f19f2f 100644 --- a/service/test/unit/config/test_leap.py +++ b/service/test/unit/config/test_leap.py @@ -19,7 +19,8 @@ from mock import MagicMock, patch, Mock from twisted.trial import unittest from twisted.internet import defer from pixelated.config.leap import create_leap_session, BootstrapUserServices, initialize_leap_single_user -from pixelated.config.sessions import LeapSessionFactory, SessionCache +from pixelated.config.sessions import LeapSessionFactory, SessionCache, SoledadWrongPassphraseException +from leap.soledad.common.crypto import UnknownMacMethodError class TestLeapInit(unittest.TestCase): @@ -143,3 +144,51 @@ class TestUserBootstrap(unittest.TestCase): self._user_bootstrap.setup(self.user_auth, self.password) mock_add_welcome_email.assert_called_once_with(mail_store, default_language) + + @patch('pixelated.config.leap.create_leap_session') + @patch('pixelated.config.leap.log.warn') + @defer.inlineCallbacks + def test__setup__should_log_an_error_raised_from_create_leap_session(self, mock_logger_warn, mock_create_leap_session): + mock_create_leap_session.side_effect = SoledadWrongPassphraseException(UnknownMacMethodError("oh no")) + with self.assertRaises(SoledadWrongPassphraseException): + yield self._user_bootstrap.setup(self.user_auth, self.password, '') + mock_logger_warn.assert_called_once_with("SoledadWrongPassphraseException: oh no. Closing session for user: ayoyo") + + @patch('pixelated.config.leap.BootstrapUserServices._setup_user_services') + @patch('pixelated.config.leap.create_leap_session') + @patch('pixelated.config.leap.log.warn') + @defer.inlineCallbacks + def test__setup__should_log_an_error_raised_from__setup_user_services(self, mock_logger_warn, mock_create_leap_session, mock_setup_user_services): + leap_session = Mock() + mock_create_leap_session.return_value = leap_session + mock_setup_user_services.side_effect = UnknownMacMethodError("oh no") + with self.assertRaises(UnknownMacMethodError): + yield self._user_bootstrap.setup(self.user_auth, self.password, '') + mock_logger_warn.assert_called_once_with("UnknownMacMethodError: oh no. Closing session for user: ayoyo") + + @patch('pixelated.config.leap.BootstrapUserServices._setup_user_services') + @patch('pixelated.config.leap.create_leap_session') + @patch('pixelated.config.leap.log.warn') + @defer.inlineCallbacks + def test__setup__should_close_leap_sesson_on_error_from__setup_user_services(self, mock_logger_warn, mock_create_leap_session, mock_setup_user_services): + leap_session = Mock() + leap_session.close = Mock() + mock_create_leap_session.return_value = leap_session + mock_setup_user_services.side_effect = UnknownMacMethodError("oh no") + with self.assertRaises(UnknownMacMethodError): + yield self._user_bootstrap.setup(self.user_auth, self.password, '') + leap_session.close.assert_called_once_with() + + @patch('pixelated.config.leap.BootstrapUserServices._add_welcome_email') + @patch('pixelated.config.leap.BootstrapUserServices._setup_user_services') + @patch('pixelated.config.leap.create_leap_session') + @patch('pixelated.config.leap.log.warn') + @defer.inlineCallbacks + def test__setup__should_close_leap_sesson_on_error_from__add_welcome_email(self, mock_logger_warn, mock_create_leap_session, _, mock_add_welcome_email): + leap_session = Mock() + leap_session.close = Mock() + mock_create_leap_session.return_value = leap_session + mock_add_welcome_email.side_effect = UnknownMacMethodError("oh no") + with self.assertRaises(UnknownMacMethodError): + yield self._user_bootstrap.setup(self.user_auth, self.password, '') + leap_session.close.assert_called_once_with() diff --git a/service/test/unit/config/test_sessions.py b/service/test/unit/config/test_sessions.py index 91a0ef90..e2254eae 100644 --- a/service/test/unit/config/test_sessions.py +++ b/service/test/unit/config/test_sessions.py @@ -19,10 +19,11 @@ from mock import patch from mock import MagicMock from mockito import when from twisted.internet import defer -from pixelated.config.sessions import LeapSession, SessionCache, LeapSessionFactory +from pixelated.config.sessions import LeapSession, SessionCache, LeapSessionFactory, SoledadWrongPassphraseException from pixelated.bitmask_libraries.keymanager import UploadKeyError from test.unit.bitmask_libraries.test_abstract_leap import AbstractLeapTest from leap.common.events.catalog import KEYMANAGER_FINISHED_KEY_GENERATION +from leap.soledad.common.crypto import WrongMacError, UnknownMacMethodError class SessionTest(AbstractLeapTest): @@ -179,6 +180,18 @@ class SessionTest(AbstractLeapTest): self.assertIs(type(uuid_arg), str, "expected uuid argument to be a string") self.assertIs(type(pass_arg), unicode, "expected passphrase argument to be unicode") + @defer.inlineCallbacks + def test_sessions__setup_soledad__will_raise_wrong_passphrase_exception_on_errors(self): + leap_session_factory = LeapSessionFactory(self.provider) + + with patch('pixelated.config.sessions.Soledad', side_effect=WrongMacError("oh no")): + with self.assertRaises(SoledadWrongPassphraseException): + yield leap_session_factory.setup_soledad('token', u'uuid', 'passphrase', None) + + with patch('pixelated.config.sessions.Soledad', side_effect=UnknownMacMethodError("oh no")): + with self.assertRaises(SoledadWrongPassphraseException): + yield leap_session_factory.setup_soledad('token', u'uuid', 'passphrase', None) + def _create_session(self): return LeapSession(self.provider, self.auth, self.mail_store, self.soledad_session, self.keymanager, self.smtp_mock) -- cgit v1.2.3