Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Skip payload generation when checkout not expired
fowczrek12349642dfdf
diff --git a/saleor/graphql/webhook/dataloaders.py b/saleor/graphql/webhook/dataloaders.pyindex 4aaed91fd3..a729d528d1 100644--- a/saleor/graphql/webhook/dataloaders.py+++ b/saleor/graphql/webhook/dataloaders.py@@ -1,6 +1,7 @@from collections import defaultdictfrom typing import Any+from django.utils import timezonefrom promise import Promisefrom ...core.db.connection import allow_writer_in_context@@ -100,8 +101,8 @@ class PregeneratedCheckoutTaxPayloadsByCheckoutTokenLoader(DataLoader):requestor = get_user_or_app_from_context(self.context)request_context = initialize_request(requestor,- event_type in WebhookEventSyncType.ALL,- False,+ sync_event=True,+ allow_replica=False,event_type=event_type,)webhooks = get_webhooks_for_event(event_type)@@ -122,7 +123,10 @@ class PregeneratedCheckoutTaxPayloadsByCheckoutTokenLoader(DataLoader):tax_configuration, country_tax_configuration)- if tax_strategy == TaxCalculationStrategy.TAX_APP:+ if (+ tax_strategy == TaxCalculationStrategy.TAX_APP+ and checkout_info.checkout.price_expiration <= timezone.now()+ ):tax_app_identifier = get_tax_app_id(tax_configuration, country_tax_configuration)diff --git a/saleor/graphql/webhook/tests/pregenerated_subscription/test_tax_configurations_with_pregenerated_payloads.py b/saleor/graphql/webhook/tests/pregenerated_subscription/test_tax_configurations_with_pregenerated_payloads.pyindex 3c30fd4415..865ed55596 100644--- a/saleor/graphql/webhook/tests/pregenerated_subscription/test_tax_configurations_with_pregenerated_payloads.py+++ b/saleor/graphql/webhook/tests/pregenerated_subscription/test_tax_configurations_with_pregenerated_payloads.py@@ -1,9 +1,11 @@+from collections import defaultdictfrom datetime import timedeltafrom unittest import mockfrom django.test import override_settingsfrom django.utils import timezone+from .....checkout.calculations import fetch_checkout_datafrom .....tax import TaxCalculationStrategyfrom .....tax.models import TaxConfigurationfrom ....core.utils import to_global_id_or_none@@ -318,3 +320,64 @@ def test_pregenerated_payload_with_all_apps_price_entered_without_tax(mock_generate_payload.assert_not_called()assert mock_request.call_count == 2assert content["data"]["checkout"]["id"] == checkout_global_id+++@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")+ "saleor.webhook.transport.synchronous.transport.generate_payload_from_subscription"+)+ "saleor.checkout.calculations.fetch_checkout_data",+ wraps=fetch_checkout_data,+)+def test_pregenerated_payload_skipped_when_checkout_not_expired(+ mock_fetch_checkout_data,+ mock_generate_payload,+ mock_request,+ checkout,+ api_client,+ tax_data_response,+ tax_app,+):+ # given+ tax_app_global_id = to_global_id_or_none(tax_app)+ tax_configuration = TaxConfiguration.objects.get()+ tax_configuration.tax_calculation_strategy = TaxCalculationStrategy.TAX_APP+ tax_configuration.prices_entered_with_tax = True+ tax_configuration.tax_app_id = tax_app_global_id+ tax_configuration.save()+ checkout.price_expiration = timezone.now() + timedelta(days=1)+ checkout.save()+ checkout_global_id = to_global_id_or_none(checkout)+ checkout_total_price_query = """+ query getCheckout($id: ID!) {+ checkout(id: $id) {+ id+ totalPrice {+ net {+ amount+ }+ gross {+ amount+ }+ }+ }+ }+ """+ variables = {"id": checkout_global_id}++ mock_request.return_value = tax_data_response++ # when+ response = api_client.post_graphql(checkout_total_price_query, variables)+ content = get_graphql_content(response)++ # then+ assert content["data"]["checkout"]["id"] == checkout_global_id+ mock_generate_payload.assert_not_called()+ mock_request.assert_not_called()+ mock_fetch_checkout_data.assert_called_once()+ assert mock_fetch_checkout_data.call_args.kwargs[+ "pregenerated_subscription_payloads"+ ] == defaultdict(dict)