Skip to content

Commit

Permalink
Trigger CGW /flush on GasPrice updates (#352)
Browse files Browse the repository at this point in the history
Triggers the CGW /flush endpoint for Chains whenever there's an update to a GasPrice
  • Loading branch information
fmrsabino authored Dec 9, 2021
1 parent 8193250 commit 4b33cb1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/chains/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand Down
50 changes: 49 additions & 1 deletion src/chains/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

0 comments on commit 4b33cb1

Please sign in to comment.