Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Do not call filter-shipping-methods webhook for non-editable orders (#16498)
Maciej Korycinski9c9e7f8b1721
diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex d089c315a3..90188fb3b9 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -16,14 +16,8 @@ from ..core.utils.country import get_active_countryfrom ..core.utils.translations import get_translationfrom ..core.weight import zero_weightfrom ..discount import DiscountType, DiscountValueType-from ..discount.models import (- OrderDiscount,- OrderLineDiscount,- VoucherType,-)-from ..discount.utils.manual_discount import (- apply_discount_to_value,-)+from ..discount.models import OrderDiscount, OrderLineDiscount, VoucherType+from ..discount.utils.manual_discount import apply_discount_to_valuefrom ..discount.utils.promotion import (get_discount_name,get_discount_translated_name,@@ -698,8 +692,11 @@ def get_valid_shipping_methods_for_order(if not valid_methods:return []- excluded_methods = manager.excluded_shipping_methods_for_order(order, valid_methods)- initialize_shipping_method_active_status(valid_methods, excluded_methods)+ if order.status in ORDER_EDITABLE_STATUS:+ excluded_methods = manager.excluded_shipping_methods_for_order(+ order, valid_methods+ )+ initialize_shipping_method_active_status(valid_methods, excluded_methods)return valid_methodsdiff --git a/saleor/plugins/webhook/tests/test_shipping_webhook.py b/saleor/plugins/webhook/tests/test_shipping_webhook.pyindex 4588ebe685..1fa5ecfdec 100644--- a/saleor/plugins/webhook/tests/test_shipping_webhook.py+++ b/saleor/plugins/webhook/tests/test_shipping_webhook.py@@ -7,6 +7,7 @@ import pytestfrom ....core.models import EventDeliveryfrom ....graphql.tests.utils import get_graphql_content+from ....order import OrderStatusfrom ....webhook.const import CACHE_EXCLUDED_SHIPPING_TIMEfrom ....webhook.event_types import WebhookEventSyncTypefrom ....webhook.models import Webhook@@ -435,6 +436,8 @@ def test_order_shipping_methods(settings,):# given+ order_with_lines.status = OrderStatus.UNCONFIRMED+ order_with_lines.save(update_fields=["status"])settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]webhook_reason = "spanish-inquisition"excluded_shipping_method_id = order_with_lines.shipping_method.id@@ -454,6 +457,97 @@ def test_order_shipping_methods(assert shipping_methods[0]["message"] == webhook_reason+ "saleor.plugins.webhook.plugin.WebhookPlugin.excluded_shipping_methods_for_order"+)+def test_draft_order_shipping_methods(+ mocked_webhook,+ staff_api_client,+ order_with_lines,+ permission_group_manage_orders,+ settings,+):+ # given+ order_with_lines.status = OrderStatus.DRAFT+ order_with_lines.save(update_fields=["status"])+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ webhook_reason = "spanish-inquisition"+ excluded_shipping_method_id = order_with_lines.shipping_method.id+ mocked_webhook.return_value = [+ ExcludedShippingMethod(excluded_shipping_method_id, webhook_reason)+ ]+ permission_group_manage_orders.user_set.add(staff_api_client.user)+ query = """+ query DraftOrdersQuery {+ draftOrders(first: 1) {+ edges {+ node {+ shippingMethods {+ id+ name+ active+ message+ }+ }+ }+ }+ }+ """+ # when+ response = staff_api_client.post_graphql(query)+ content = get_graphql_content(response)+ order_data = content["data"]["draftOrders"]["edges"][0]["node"]++ shipping_methods = order_data["shippingMethods"]+ # then+ assert len(shipping_methods) == 1+ assert not shipping_methods[0]["active"]+ assert shipping_methods[0]["message"] == webhook_reason+++ "order_status",+ [+ OrderStatus.UNFULFILLED,+ OrderStatus.PARTIALLY_FULFILLED,+ OrderStatus.FULFILLED,+ OrderStatus.CANCELED,+ OrderStatus.EXPIRED,+ OrderStatus.RETURNED,+ OrderStatus.PARTIALLY_RETURNED,+ ],+)+ "saleor.plugins.webhook.plugin.WebhookPlugin.excluded_shipping_methods_for_order"+)+def test_order_shipping_methods_skips_sync_webhook_for_non_editable_statuses(+ mocked_webhook,+ order_status,+ staff_api_client,+ order_with_lines,+ permission_group_manage_orders,+ settings,+):+ # given+ order_with_lines.status = order_status+ order_with_lines.save(update_fields=["status"])+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]++ permission_group_manage_orders.user_set.add(staff_api_client.user)++ # when+ response = staff_api_client.post_graphql(ORDER_QUERY_SHIPPING_METHOD)+ content = get_graphql_content(response)+ order_data = content["data"]["orders"]["edges"][0]["node"]++ shipping_methods = order_data["shippingMethods"]++ # then+ assert not mocked_webhook.called+ assert len(shipping_methods) == 1+ assert shipping_methods[0]["active"]++@pytest.mark.parametrize(("webhook_response", "expected_count"),[(lambda s: [ExcludedShippingMethod(s.id, "")], 0), (lambda s: [], 1)],@@ -472,6 +566,8 @@ def test_order_available_shipping_methods():# givensettings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ order_with_lines.status = OrderStatus.UNCONFIRMED+ order_with_lines.save(update_fields=["status"])shipping_method = order_with_lines.shipping_methoddef respond(*args, **kwargs):