Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix handling subscription with empty data (#14989)
Jakub4317aa6066e4
diff --git a/saleor/graphql/checkout/mutations/checkout_add_promo_code.py b/saleor/graphql/checkout/mutations/checkout_add_promo_code.pyindex e0bff6eeb6..855a84d8bc 100644--- a/saleor/graphql/checkout/mutations/checkout_add_promo_code.py+++ b/saleor/graphql/checkout/mutations/checkout_add_promo_code.py@@ -70,7 +70,6 @@ class CheckoutAddPromoCode(BaseMutation):token=None,):checkout = get_checkout(cls, info, checkout_id=checkout_id, token=token, id=id)-manager = get_plugin_manager_promise(info.context).get()lines, unavailable_variant_pks = fetch_checkout_lines(checkout)diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.pyindex a316737ed1..480bfb9997 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/fixtures.py@@ -1201,3 +1201,13 @@ def subscription_app_status_changed_webhook_removed_app(WebhookEventAsyncType.APP_STATUS_CHANGED,app=removed_app,)+++def subscription_checkout_shipping_filter_and_list_missing_one_in_definition(+ subscription_webhook,+):+ return subscription_webhook(+ queries.THUMBNAIL_CREATED,+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,+ )diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.pyindex d1e0befe13..be9810d3e4 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py@@ -2655,3 +2655,22 @@ ORDER_CALCULATE_TAXES = """}}"""++CHECKOUT_SHIPPING_LIST_AND_FILTER = """+ subscription {+ event {+ ... on CheckoutFilterShippingMethods {+ __typename+ checkout {+ id+ }+ }+ ... on ShippingListMethodsForCheckout {+ __typename+ checkout {+ id+ }+ }+ }+ }+"""diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.pyindex efb06ccdf4..85ba07f770 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py@@ -19,6 +19,9 @@ from .....webhook.transport.asynchronous.transport import (create_deliveries_for_subscriptions,logger,)+from .....webhook.transport.synchronous.transport import (+ create_delivery_for_subscription_sync_event,+)from . import subscription_queriesfrom .payloads import (generate_account_events_payload,@@ -2725,6 +2728,24 @@ def test_checkout_filter_shipping_methods_with_circular_call_for_available_gatewassert payload["checkout"] is None+def test_checkout_list_methods_mismatch_in_subscription_query_definition(+ checkout_ready_to_complete,+ subscription_checkout_shipping_filter_and_list_missing_one_in_definition,+):+ # This test ensures that subscription returns None and does not raise an error+ # given+ webhook = subscription_checkout_shipping_filter_and_list_missing_one_in_definition+ event_type = WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT++ # when+ deliveries = create_delivery_for_subscription_sync_event(+ event_type, checkout_ready_to_complete, webhook+ )++ # then+ assert not deliveries++def test_order_filter_shipping_methods(order_line_with_one_allocation,subscription_order_filter_shipping_methods_webhook,diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.pyindex a20f24572a..43f48088a6 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/test_create_deliveries_for_taxes.py@@ -93,7 +93,6 @@ def test_checkout_calculate_taxes():# givenwebhook_app.permissions.add(permission_handle_taxes)- event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXESwebhook = Webhook.objects.create(name="Webhook",app=webhook_app,@@ -159,7 +158,6 @@ def test_checkout_calculate_taxes_with_free_shipping_voucher(# givencheckout = checkout_with_voucher_free_shippingwebhook_app.permissions.add(permission_handle_taxes)- event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXESwebhook = Webhook.objects.create(name="Webhook",app=webhook_app,@@ -201,7 +199,6 @@ def test_checkout_calculate_taxes_with_voucher():# givenwebhook_app.permissions.add(permission_handle_taxes)- event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXESwebhook = Webhook.objects.create(name="Webhook",app=webhook_app,@@ -259,7 +256,6 @@ def test_checkout_calculate_taxes_with_shipping_voucher(# givenvoucher.type = VoucherType.SHIPPINGwebhook_app.permissions.add(permission_handle_taxes)- event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXESwebhook = Webhook.objects.create(name="Webhook",app=webhook_app,@@ -315,7 +311,6 @@ def test_checkout_calculate_taxes_empty_checkout():# givenwebhook_app.permissions.add(permission_handle_taxes)- event_type = WebhookEventSyncType.CHECKOUT_CALCULATE_TAXESwebhook = Webhook.objects.create(name="Webhook",app=webhook_app,@@ -362,7 +357,6 @@ def test_order_calculate_taxes(shipping_method = shipping_zone.shipping_methods.first()order.shipping_method = shipping_methodwebhook_app.permissions.add(permission_handle_taxes)- event_type = WebhookEventSyncType.ORDER_CALCULATE_TAXESwebhook = Webhook.objects.create(name="Webhook",app=webhook_app,@@ -444,7 +438,6 @@ def test_order_calculate_taxes_with_discounts(order.refresh_from_db()webhook_app.permissions.add(permission_handle_taxes)- event_type = WebhookEventSyncType.ORDER_CALCULATE_TAXESwebhook = Webhook.objects.create(name="Webhook",app=webhook_app,@@ -494,7 +487,6 @@ def test_order_calculate_taxes_empty_order(# givenorder = Order.objects.create(channel=channel_USD, currency="USD")webhook_app.permissions.add(permission_handle_taxes)- event_type = WebhookEventSyncType.ORDER_CALCULATE_TAXESwebhook = Webhook.objects.create(name="Webhook",app=webhook_app,diff --git a/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.py b/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.pyindex ef8f4f8971..280f2455d9 100644--- a/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.py+++ b/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.py@@ -13,6 +13,25 @@ from ....webhook.transport.utils import generate_cache_key_for_webhookfrom ..plugin import CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")+def test_get_shipping_methods_for_checkout_webhook_response_none(+ mocked_webhook,+ webhook_plugin,+ checkout_ready_to_complete,+ shipping_app,+):+ # given+ checkout = checkout_ready_to_complete+ plugin = webhook_plugin()+ mocked_webhook.return_value = None++ # when+ response = plugin.get_shipping_methods_for_checkout(checkout, None)++ # then+ assert not response++@mock.patch("saleor.webhook.transport.synchronous.transport.cache.set")@mock.patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")def test_get_shipping_methods_for_checkout_set_cache(diff --git a/saleor/plugins/webhook/tests/test_shipping_webhook.py b/saleor/plugins/webhook/tests/test_shipping_webhook.pyindex a10074f43c..b3c30d81e1 100644--- a/saleor/plugins/webhook/tests/test_shipping_webhook.py+++ b/saleor/plugins/webhook/tests/test_shipping_webhook.py@@ -15,6 +15,8 @@ from ....webhook.payloads import ()from ....webhook.transport.shipping import (get_excluded_shipping_methods_from_response,+ get_excluded_shipping_methods_or_fetch,+ parse_list_shipping_methods_response,to_shipping_app_id,)from ....webhook.transport.synchronous.transport import trigger_webhook_sync@@ -894,3 +896,44 @@ def test_generate_excluded_shipping_methods_for_checkout_payload()assert "checkout" in json_payloadassert "channel" in json_payload["checkout"]++[email protected]("saleor.webhook.transport.shipping.parse_excluded_shipping_methods")[email protected]("saleor.webhook.transport.shipping.trigger_webhook_sync")+ "saleor.webhook.transport.shipping.get_excluded_shipping_methods_from_response"+)+def test_get_excluded_shipping_methods_or_fetch_invalid_response_type(+ mocked_get_excluded,+ mocked_webhook_sync_trigger,+ mocked_parse,+ app,+ checkout,+):+ # given+ mocked_webhook_sync_trigger.return_value = ["incorrect_type"]+ webhook = Webhook.objects.create(+ name="Simple webhook", app=app, target_url="http://www.example.com/test"+ )+ event_type = WebhookEventSyncType.ORDER_FILTER_SHIPPING_METHODS+ webhook.events.create(event_type=event_type)+ webhooks = Webhook.objects.all()++ # when+ get_excluded_shipping_methods_or_fetch(+ webhooks, event_type, '{"test":"payload"}', "test", checkout, False+ )+ # then+ mocked_get_excluded.asssert_not_called()+ mocked_parse.assert_called_once_with([])+++def test_parse_list_shipping_methods_response_response_incorrect_format(app):+ # given+ response_data_with_incorrect_format = [[1], 2, "3"]+ # when+ result = parse_list_shipping_methods_response(+ response_data_with_incorrect_format, app+ )+ # then+ assert result == []diff --git a/saleor/webhook/transport/shipping.py b/saleor/webhook/transport/shipping.pyindex 8733daf094..75976e42c4 100644--- a/saleor/webhook/transport/shipping.py+++ b/saleor/webhook/transport/shipping.py@@ -57,6 +57,8 @@ def parse_list_shipping_methods_response() -> list["ShippingMethodData"]:shipping_methods = []for shipping_method_data in response_data:+ if not validate_shipping_method_data(shipping_method_data):+ continuemethod_id = shipping_method_data.get("id")method_name = shipping_method_data.get("name")method_amount = shipping_method_data.get("amount")@@ -74,6 +76,13 @@ def parse_list_shipping_methods_response(return shipping_methods+def validate_shipping_method_data(shipping_method_data):+ if not isinstance(shipping_method_data, dict):+ return False+ keys = ["id", "name", "amount", "currency"]+ return all(key in shipping_method_data for key in keys)++def _compare_order_payloads(payload: str, cached_payload: str) -> bool:"""Compare two strings of order payloads ignoring meta."""EXCLUDED_KEY = "meta"@@ -121,7 +130,7 @@ def get_excluded_shipping_methods_or_fetch(subscribable_object=subscribable_object,timeout=settings.WEBHOOK_SYNC_TIMEOUT,)- if response_data:+ if response_data and isinstance(response_data, dict):excluded_methods.extend(get_excluded_shipping_methods_from_response(response_data))diff --git a/saleor/webhook/transport/synchronous/transport.py b/saleor/webhook/transport/synchronous/transport.pyindex a703ee194c..7f76283855 100644--- a/saleor/webhook/transport/synchronous/transport.py+++ b/saleor/webhook/transport/synchronous/transport.py@@ -18,7 +18,6 @@ from ....graphql.webhook.subscription_payload import (initialize_request,)from ....graphql.webhook.subscription_types import WEBHOOK_TYPES_MAP-from ....payment import PaymentErrorfrom ....payment.models import TransactionEventfrom ....payment.utils import (create_transaction_event_from_request_and_webhook_response,@@ -247,12 +246,12 @@ def create_delivery_for_subscription_sync_event(app=webhook.app,)if not data:- # PaymentError is a temporary exception type. New type will be implemented- # in separate PR to ensure proper handling for all sync events.- # It was implemented when sync webhooks were handling payment events only.- raise PaymentError(- f"No payload was generated with subscription for event: {event_type}"+ logger.info(+ "No payload was generated with subscription for event: %s", event_type)+ # Return None so if subscription query returns no data Saleor will not crash but+ # log the issue and continue without creating a delivery.+ return Noneevent_payload = EventPayload.objects.create(payload=json.dumps({**data}))event_delivery = EventDelivery.objects.create(status=EventDeliveryStatus.PENDING,