Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Dec 20, 2023
1 parent f8857a3 commit 089a027
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion safe_transaction_service/history/services/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
]
Expand Down
58 changes: 58 additions & 0 deletions safe_transaction_service/history/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
)
9 changes: 5 additions & 4 deletions safe_transaction_service/safe_messages/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 089a027

Please sign in to comment.