Rebase previous PR on current master - see PR #915 for details
authorOla Bini <ola@olabini.se>
Fri, 20 Jan 2017 17:20:51 +0000 (15:20 -0200)
committerOla Bini <ola@olabini.se>
Fri, 20 Jan 2017 17:20:51 +0000 (15:20 -0200)
service/pixelated/config/leap.py
service/pixelated/config/sessions.py
service/test/unit/config/test_leap.py
service/test/unit/config/test_sessions.py

index 6b9ebf2..52a70f3 100644 (file)
@@ -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):
index eba5510..2bad3e3 100644 (file)
@@ -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
 
 
index 5a68c53..29f19f2 100644 (file)
@@ -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()
index 91a0ef9..e2254ea 100644 (file)
@@ -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)