diff --git a/at_client/atclient.py b/at_client/atclient.py index 810f354..adf4fea 100644 --- a/at_client/atclient.py +++ b/at_client/atclient.py @@ -145,7 +145,7 @@ def get_encryption_key_shared_by_me(self, key: SharedKey): try: return EncryptionUtil.rsa_decrypt_from_base64(response.get_raw_data_response(), self.keys[KeysUtil.encryption_private_key_name]) except Exception as e: - raise AtDecryptionException(f"Failed to decrypt {to_lookup} - e") + raise AtDecryptionException(f"Failed to decrypt {to_lookup} - {e}") def get_encryption_key_shared_by_other(self, shared_key: SharedKey): shared_shared_key_name = shared_key.get_shared_shared_key_name() diff --git a/test/decrypt_error_test.py b/test/decrypt_error_test.py new file mode 100644 index 0000000..38fe2d3 --- /dev/null +++ b/test/decrypt_error_test.py @@ -0,0 +1,35 @@ +import unittest +from unittest.mock import MagicMock + +from at_client import AtClient +from at_client.common import AtSign +from at_client.common.keys import SharedKey +from at_client.util.keysutil import KeysUtil +from at_client.exception.atexception import AtDecryptionException + + +class DecryptErrorDetailTest(unittest.TestCase): + """The shared-key decrypt error must include the real exception, not literal 'e'.""" + + def test_error_includes_exception_detail(self): + client = AtClient.__new__(AtClient) # bypass network __init__ + client.atsign = AtSign("@alice") + client.keys = {KeysUtil.encryption_private_key_name: "not-a-valid-private-key"} + + resp = MagicMock() + resp.is_error.return_value = False + resp.get_raw_data_response.return_value = "not-valid-rsa-ciphertext" + client.secondary_connection = MagicMock() + client.secondary_connection.execute_command.return_value = resp + + key = SharedKey("k", AtSign("@alice"), AtSign("@bob")) + with self.assertRaises(AtDecryptionException) as ctx: + client.get_encryption_key_shared_by_me(key) + + msg = str(ctx.exception) + self.assertFalse(msg.rstrip().endswith("- e")) # the bug printed a literal 'e' + self.assertIn("Failed to decrypt", msg) + + +if __name__ == "__main__": + unittest.main()