From b553c65d1d114f2a3a4cb3282b4505838f537421 Mon Sep 17 00:00:00 2001 From: Sriram Viswanathan Date: Thu, 16 Mar 2017 17:06:50 -0300 Subject: [bug] fix errBack in _decrypt_doc in incoming mail service with @deniscostadsc --- src/leap/bitmask/mail/incoming/service.py | 9 +++++--- .../mail/incoming/test_incoming_mail.py | 24 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/leap/bitmask/mail/incoming/service.py b/src/leap/bitmask/mail/incoming/service.py index 2ecd587..e1dde02 100644 --- a/src/leap/bitmask/mail/incoming/service.py +++ b/src/leap/bitmask/mail/incoming/service.py @@ -337,10 +337,13 @@ class IncomingMail(Service): "1" if success else "0") return self._process_decrypted_doc(doc, decrdata) + def log_doc_id_and_raise_exception(failure): + logger.error( + '_decrypt_doc: Error decrypting document with ID %s' % doc.doc_id) + self._errback(failure) + d = self._keymanager.decrypt(doc.content[ENC_JSON_KEY], self._userid) - d.addErrback(self._errback) - d.addErrback(lambda _: logger.error( - '_decrypt_doc: Error decrypting document with ID %s' % doc.doc_id)) + d.addErrback(log_doc_id_and_raise_exception) d.addCallback(process_decrypted) d.addCallback(lambda data: (doc, data)) return d diff --git a/tests/integration/mail/incoming/test_incoming_mail.py b/tests/integration/mail/incoming/test_incoming_mail.py index b8d69f1..daef057 100644 --- a/tests/integration/mail/incoming/test_incoming_mail.py +++ b/tests/integration/mail/incoming/test_incoming_mail.py @@ -29,10 +29,12 @@ import uuid from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.parser import Parser -from mock import Mock +from mock import Mock, patch, MagicMock, ANY from twisted.internet import defer from twisted.python import log +from twisted.logger import Logger +from twisted.python.failure import Failure from leap.bitmask.keymanager.errors import KeyAddressMismatch from leap.bitmask.mail.adaptors import soledad_indexes as fields @@ -324,6 +326,26 @@ subject: independence of cyberspace d.addCallback(add_decrypted_header_called) return d + def test_log_error_if_decrypt_fails(self): + + def assert_failure(_): + mock_logger_error.assert_any_call('_decrypt_doc: ' + 'Error decrypting document with ' + 'ID 1') + + with patch.object(Logger, 'error') as mock_logger_error: + doc = SoledadDocument() + doc.doc_id = '1' + doc.content = {'_enc_json': ''} + + self.fetcher._process_decrypted_doc = Mock() + self.km.decrypt = Mock( + return_value=defer.fail(Exception())) + + d = self.fetcher._decrypt_doc(doc) + d.addCallback(assert_failure) + return d + def testValidateSignatureFromEncryptedEmailFromAppleMail(self): enc_signed_file = os.path.join( HERE, 'rfc822.multi-encrypt-signed.message') -- cgit v1.2.3