Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Skip payload generation when checkout not expired

fowczrek12349642dfdf
diff --git a/saleor/graphql/webhook/dataloaders.py b/saleor/graphql/webhook/dataloaders.py
index 4aaed91fd3..a729d528d1 100644
--- a/saleor/graphql/webhook/dataloaders.py
+++ b/saleor/graphql/webhook/dataloaders.py
@@ -1,6 +1,7 @@
from collections import defaultdict
from typing import Any
+from django.utils import timezone
from promise import Promise
from ...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.py
index 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 defaultdict
from datetime import timedelta
from unittest import mock
from django.test import override_settings
from django.utils import timezone
+from .....checkout.calculations import fetch_checkout_data
from .....tax import TaxCalculationStrategy
from .....tax.models import TaxConfiguration
from ....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 == 2
assert 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)