From 4b33cb1c95fd6ad55118a86b9994920905f7a5a6 Mon Sep 17 00:00:00 2001 From: Frederico Sabino <3332770+fmrsabino@users.noreply.github.com> Date: Thu, 9 Dec 2021 17:12:51 +0100 Subject: [PATCH] Trigger CGW /flush on GasPrice updates (#352) Triggers the CGW /flush endpoint for Chains whenever there's an update to a GasPrice --- src/chains/signals.py | 9 +++++- src/chains/tests/test_signals.py | 50 +++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/chains/signals.py b/src/chains/signals.py index fdd267f6..5c8dc5fe 100644 --- a/src/chains/signals.py +++ b/src/chains/signals.py @@ -8,7 +8,7 @@ from django.db.models.signals import post_delete, post_save from django.dispatch import receiver -from .models import Chain, Feature, Wallet +from .models import Chain, Feature, GasPrice, Wallet logger = logging.getLogger(__name__) @@ -47,6 +47,13 @@ def on_chain_update(sender: Chain, **kwargs: Any) -> None: _trigger_client_gateway_flush() +@receiver(post_save, sender=GasPrice) +@receiver(post_delete, sender=GasPrice) +def on_gas_price_update(sender: GasPrice, **kwargs: Any) -> None: + logger.info("GasPrice update. Triggering CGW webhook") + _trigger_client_gateway_flush() + + @receiver(post_save, sender=Feature) @receiver(post_delete, sender=Feature) def on_feature_update(sender: Feature, **kwargs: Any) -> None: diff --git a/src/chains/tests/test_signals.py b/src/chains/tests/test_signals.py index 2517269d..31de5160 100644 --- a/src/chains/tests/test_signals.py +++ b/src/chains/tests/test_signals.py @@ -2,7 +2,7 @@ from django.test import TestCase, override_settings from chains.models import Feature, Wallet -from chains.tests.factories import ChainFactory +from chains.tests.factories import ChainFactory, GasPriceFactory @override_settings( @@ -180,3 +180,51 @@ def test_on_wallet_update_hook_call(self) -> None: # 2 calls: one for creation and one for updating assert len(responses.calls) == 2 + + +@override_settings( + CGW_URL="http://127.0.0.1", + CGW_FLUSH_TOKEN="example-token", +) +class GasPriceHookTestCase(TestCase): + def setUp(self) -> None: + self.chain = ( + ChainFactory.create() + ) # chain creation: a GasPrice requires a chain + + @responses.activate + def test_on_gas_price_create_hook_call(self) -> None: + responses.add( + responses.POST, + "http://127.0.0.1/v1/flush/example-token", + status=200, + match=[responses.matchers.json_params_matcher({"invalidate": "Chains"})], + ) + + GasPriceFactory.create(chain=self.chain) + + assert len(responses.calls) == 1 + assert responses.calls[0].request.body == b'{"invalidate": "Chains"}' + assert ( + responses.calls[0].request.url == "http://127.0.0.1/v1/flush/example-token" + ) + + @responses.activate + def test_on_gas_price_delete_hook_call(self) -> None: + gas_price = GasPriceFactory.create(chain=self.chain) # create + gas_price.delete() # delete + + # 2 calls: one for creation and one for deletion + assert len(responses.calls) == 2 + + @responses.activate + def test_on_gas_price_update_hook_call(self) -> None: + gas_price = GasPriceFactory.create( + chain=self.chain, fixed_wei_value=1000 + ) # create + + gas_price.fixed_wei_value = 2000 + gas_price.save() # update + + # 2 calls: one for creation and one for updating + assert len(responses.calls) == 2