Return a valid error from gatherResults
authorRuben Pollan <meskio@sindominio.net>
Mon, 29 Dec 2014 06:27:27 +0000 (00:27 -0600)
committerRuben Pollan <meskio@sindominio.net>
Thu, 15 Jan 2015 19:10:57 +0000 (13:10 -0600)
src/leap/keymanager/__init__.py
src/leap/keymanager/tests/test_keymanager.py

index 5324d42..c4050fa 100644 (file)
@@ -485,8 +485,8 @@ class KeyManager(object):
         dpriv = defer.succeed(None)
         if sign is not None:
             dpriv = self.get_key(sign, ktype, private=True)
-        d = defer.gatherResults([dpub, dpriv])
-        d.addCallback(encrypt)
+        d = defer.gatherResults([dpub, dpriv], consumeErrors=True)
+        d.addCallbacks(encrypt, self._extract_first_error)
         return d
 
     def decrypt(self, data, address, ktype, passphrase=None, verify=None,
@@ -542,10 +542,13 @@ class KeyManager(object):
             dpub = self.get_key(verify, ktype, private=False,
                                 fetch_remote=fetch_remote)
             dpub.addErrback(lambda f: None if f.check(KeyNotFound) else f)
-        d = defer.gatherResults([dpub, dpriv])
-        d.addCallback(decrypt)
+        d = defer.gatherResults([dpub, dpriv], consumeErrors=True)
+        d.addCallbacks(decrypt, self._extract_first_error)
         return d
 
+    def _extract_first_error(self, failure):
+        return failure.value.subFailure
+
     def sign(self, data, address, ktype, digest_algo='SHA512', clearsign=False,
              detach=True, binary=False):
         """
index dca89e8..86832ba 100644 (file)
@@ -375,6 +375,14 @@ class KeyManagerCryptoTestCase(KeyManagerWithSoledadTestCase):
                                fetch_remote=False)
         self.assertEqual(signingkey.fingerprint, key.fingerprint)
 
+    def test_keymanager_encrypt_key_not_found(self):
+        km = self._key_manager()
+        d = km._wrapper_map[OpenPGPKey].put_ascii_key(PRIVATE_KEY, ADDRESS)
+        d.addCallback(
+            lambda _: km.encrypt(self.RAW_DATA, ADDRESS_2, OpenPGPKey,
+                                 sign=ADDRESS, fetch_remote=False))
+        return self.assertFailure(d, KeyNotFound)
+
 
 import unittest
 if __name__ == "__main__":