def testLogErrorIfDecryptFails(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 testFlagMessageOnBadJsonWhileDecrypting(self):
        doc = SoledadDocument()
        doc.doc_id = '1'
        doc.content = {'_enc_json': ''}

        err = ValueError('No JSON object could be decoded')

        def assert_failure():
            mock_logger_error.assert_any_call('Error while decrypting 1')
            mock_logger_error.assert_any_call(
                'No JSON object could be decoded')
            self.assertEquals(doc.content['errdecr'], True)

        with patch.object(Logger, 'error') as mock_logger_error:
            with patch.object(utils, 'json_loads') as mock_json_loader:
                self.fetcher._update_incoming_message = Mock()
                mock_json_loader.side_effect = err

                self.fetcher._process_decrypted_doc(doc, '')

                assert_failure()