diff --git a/safe_transaction_service/history/services/webhooks.py b/safe_transaction_service/history/services/webhooks.py index 8afef722f..173f862b2 100644 --- a/safe_transaction_service/history/services/webhooks.py +++ b/safe_transaction_service/history/services/webhooks.py @@ -54,7 +54,7 @@ def build_webhook_payload( payloads = [ { "address": instance.safe, - "type": WebHookType.DELETED_MULTISIG_TRANSACTION, + "type": WebHookType.DELETED_MULTISIG_TRANSACTION.name, "safeTxHash": HexBytes(instance.safe_tx_hash).hex(), } ] diff --git a/safe_transaction_service/history/tests/test_signals.py b/safe_transaction_service/history/tests/test_signals.py index 2c90826f3..c74a63c19 100644 --- a/safe_transaction_service/history/tests/test_signals.py +++ b/safe_transaction_service/history/tests/test_signals.py @@ -1,5 +1,6 @@ from datetime import timedelta from unittest import mock +from unittest.mock import MagicMock from django.db.models.signals import post_save from django.test import TestCase @@ -83,6 +84,14 @@ def test_build_webhook_payload(self): self.assertEqual(payload["type"], WebHookType.PENDING_MULTISIG_TRANSACTION.name) self.assertEqual(payload["chainId"], str(EthereumNetwork.GANACHE.value)) + payload = build_webhook_payload( + MultisigTransaction, + MultisigTransactionFactory(ethereum_tx=None), + deleted=True, + )[0] + self.assertEqual(payload["type"], WebHookType.DELETED_MULTISIG_TRANSACTION.name) + self.assertEqual(payload["chainId"], str(EthereumNetwork.GANACHE.value)) + safe_address = self.deploy_test_safe().address safe_message = SafeMessageFactory(safe=safe_address) payload = build_webhook_payload(SafeMessage, safe_message)[0] @@ -166,3 +175,52 @@ def test_is_relevant_notification_multisig_transaction(self): self.assertFalse( is_relevant_notification(multisig_tx.__class__, multisig_tx, created=False) ) + + @mock.patch.object(send_webhook_task, "apply_async") + @mock.patch.object(send_event_to_queue_task, "delay") + def test_signals_are_correctly_fired( + self, + send_event_to_queue_task_mock: MagicMock, + webhook_task_mock: MagicMock, + ): + # Not trusted txs should not fire any event + MultisigTransactionFactory(trusted=False) + webhook_task_mock.assert_not_called() + send_event_to_queue_task_mock.assert_not_called() + + # Trusted txs should fire an event + multisig_tx: MultisigTransaction = MultisigTransactionFactory(trusted=True) + pending_multisig_transaction_payload = { + "address": multisig_tx.safe, + "safeTxHash": multisig_tx.safe_tx_hash, + "type": WebHookType.EXECUTED_MULTISIG_TRANSACTION.name, + "failed": "false", + "txHash": "0xc8ed8af6e9e19d7e882493f3b79885585110ef406c415e0f9fdcb89ffd46422a", + "chainId": str(EthereumNetwork.GANACHE.value), + } + webhook_task_mock.assert_called_with( + args=(multisig_tx.safe, pending_multisig_transaction_payload), priority=2 + ) + send_event_to_queue_task_mock.assert_called_with( + pending_multisig_transaction_payload + ) + + # Deleting a tx should fire an event + webhook_task_mock.reset_mock() + send_event_to_queue_task_mock.reset_mock() + safe_tx_hash = multisig_tx.safe_tx_hash + multisig_tx.delete() + + deleted_multisig_transaction_payload = { + "address": multisig_tx.safe, + "safeTxHash": safe_tx_hash, + "type": WebHookType.DELETED_MULTISIG_TRANSACTION.name, + "chainId": str(EthereumNetwork.GANACHE.value), + } + + webhook_task_mock.assert_called_with( + args=(multisig_tx.safe, deleted_multisig_transaction_payload), priority=2 + ) + send_event_to_queue_task_mock.assert_called_with( + deleted_multisig_transaction_payload + ) diff --git a/safe_transaction_service/safe_messages/tests/test_signals.py b/safe_transaction_service/safe_messages/tests/test_signals.py index f4351f94b..2ddbc638e 100644 --- a/safe_transaction_service/safe_messages/tests/test_signals.py +++ b/safe_transaction_service/safe_messages/tests/test_signals.py @@ -1,4 +1,5 @@ from unittest import mock +from unittest.mock import MagicMock from django.db.models.signals import post_save from django.test import TestCase @@ -28,8 +29,8 @@ class TestSafeMessageSignals(SafeTestCaseMixin, TestCase): @mock.patch.object(send_event_to_queue_task, "delay") def test_process_webhook( self, - send_event_to_queue_task_mock, - webhook_task_mock, + send_event_to_queue_task_mock: MagicMock, + webhook_task_mock: MagicMock, ): safe_address = self.deploy_test_safe().address safe_message = SafeMessageFactory(safe=safe_address) @@ -64,8 +65,8 @@ def test_process_webhook( @mock.patch.object(send_event_to_queue_task, "delay") def test_signals_are_correctly_fired( self, - send_event_to_queue_task_mock, - webhook_task_mock, + send_event_to_queue_task_mock: MagicMock, + webhook_task_mock: MagicMock, ): safe_address = self.deploy_test_safe().address # Create a confirmation should fire a signal and webhooks should be sended