From 53e6bffefa6811bdcbf49f5a175cf9293c107ac8 Mon Sep 17 00:00:00 2001
From: moisses89 <7888669+moisses89@users.noreply.github.com>
Date: Tue, 18 Jun 2024 16:43:45 +0200
Subject: [PATCH] Add test for confirm_message
---
.../operators/safe_tx_service_operator.py | 21 ++++++------
tests/test_safe_tx_service_operator.py | 33 ++++++++++++++++++-
2 files changed, 42 insertions(+), 12 deletions(-)
diff --git a/src/safe_cli/operators/safe_tx_service_operator.py b/src/safe_cli/operators/safe_tx_service_operator.py
index a3dd6b02..26430822 100644
--- a/src/safe_cli/operators/safe_tx_service_operator.py
+++ b/src/safe_cli/operators/safe_tx_service_operator.py
@@ -124,20 +124,19 @@ def confirm_message(self, safe_message_hash: bytes, sender: ChecksumAddress):
)
return False
- if self.safe_tx_service.post_message_signature(safe_message_hash, signature):
+ try:
+ self.safe_tx_service.post_message_signature(safe_message_hash, signature)
+ except SafeAPIException as e:
print_formatted_text(
- HTML(
- "Message was correctly confirmed on Safe Transaction Service"
- )
- )
- return True
- else:
- print_formatted_text(
- HTML(
- "Something went wrong confirming message on Safe Transaction Service"
- )
+ HTML(f"Message wasn't confirmed due an error: {e}")
)
return False
+ print_formatted_text(
+ HTML(
+ "Message was correctly confirmed on Safe Transaction Service"
+ )
+ )
+ return True
def get_delegates(self):
delegates = self.safe_tx_service.get_delegates(self.address)
diff --git a/tests/test_safe_tx_service_operator.py b/tests/test_safe_tx_service_operator.py
index 07a7ba88..65e12dbc 100644
--- a/tests/test_safe_tx_service_operator.py
+++ b/tests/test_safe_tx_service_operator.py
@@ -9,7 +9,7 @@
from gnosis.eth import EthereumClient
from gnosis.safe import SafeTx
-from gnosis.safe.api import TransactionServiceApi
+from gnosis.safe.api import SafeAPIException, TransactionServiceApi
from safe_cli.operators import SafeOperatorMode, SafeTxServiceOperator
@@ -551,6 +551,37 @@ def test_data_decoded_to_text(self):
decoded_data_text,
)
+ @mock.patch.object(
+ TransactionServiceApi, "post_message_signature", return_value=True
+ )
+ @mock.patch.object(TransactionServiceApi, "get_message", return_value=None)
+ def test_confirm_message(
+ self, get_message: MagicMock, post_message_signature: MagicMock
+ ):
+ safe_operator = self.setup_operator(
+ number_owners=1, mode=SafeOperatorMode.TX_SERVICE
+ )
+ # confirm_message just need the raw message to print
+ get_message.return_value = {"message": "We just need a message"}
+ safe_message_hash = HexBytes(
+ "0xfbf5d6f7faaec8fd7770cb532f87e38fb82ecb47f58474cbeb29a174f1afd829"
+ )
+ expected_signature = HexBytes(
+ "0x82484cd1f17430915f940d7c589e1687253de7f4062748b6807d29c67af52e3957b90427a9202ba4bc2104a129183387d994be2e4a8623a1f1ba2bad75d3546d1b"
+ )
+ sender = list(safe_operator.accounts)[0]
+ self.assertTrue(
+ safe_operator.confirm_message(safe_message_hash, sender.address)
+ )
+ post_message_signature.assert_called_with(safe_message_hash, expected_signature)
+
+ post_message_signature.side_effect = SafeAPIException(
+ "Error posting message signature"
+ )
+ self.assertFalse(
+ safe_operator.confirm_message(safe_message_hash, sender.address)
+ )
+
def test_drain(self):
# TODO Drain is a complex to mock
pass