Skip to content

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

Extrach checkout list-shipping webhook from plugin (#18831)

Maciej Korycinskicdc8b6919e63
diff --git a/saleor/checkout/fetch.py b/saleor/checkout/fetch.py
index 5ebe1bd76b..f07bda2b66 100644
--- a/saleor/checkout/fetch.py
+++ b/saleor/checkout/fetch.py
@@ -555,11 +555,22 @@ def fetch_external_shipping_methods_for_checkout_info(
checkout_info,
available_built_in_methods: list[ShippingMethodData],
) -> list[ShippingMethodData]:
- manager = checkout_info.manager
- return manager.list_shipping_methods_for_checkout(
+ from .webhooks.list_shipping_methods import list_shipping_methods_for_checkout
+
+ allow_replica = not (
+ checkout_info.database_connection_name
+ == settings.DATABASE_CONNECTION_DEFAULT_NAME
+ )
+ requestor = (
+ checkout_info.manager.requestor_getter()
+ if checkout_info.manager.requestor_getter
+ else None
+ )
+ return list_shipping_methods_for_checkout(
checkout=checkout_info.checkout,
- channel_slug=checkout_info.channel.slug,
built_in_shipping_methods=available_built_in_methods,
+ allow_replica=allow_replica,
+ requestor=requestor,
)
diff --git a/saleor/checkout/tests/test_fetch.py b/saleor/checkout/tests/test_fetch.py
index 6ef28017b2..82915e8f68 100644
--- a/saleor/checkout/tests/test_fetch.py
+++ b/saleor/checkout/tests/test_fetch.py
@@ -823,7 +823,9 @@ def _assert_external_shipping_method(
@freeze_time("2024-05-31 12:00:01")
[email protected]("saleor.plugins.manager.PluginsManager.list_shipping_methods_for_checkout")
+ "saleor.checkout.webhooks.list_shipping_methods.list_shipping_methods_for_checkout"
+)
def test_fetch_shipping_methods_for_checkout_with_external_shipping_method(
mocked_webhook,
checkout_with_item,
@@ -875,7 +877,9 @@ def test_fetch_shipping_methods_for_checkout_with_external_shipping_method(
@freeze_time("2024-05-31 12:00:01")
[email protected]("saleor.plugins.manager.PluginsManager.list_shipping_methods_for_checkout")
+ "saleor.checkout.webhooks.list_shipping_methods.list_shipping_methods_for_checkout"
+)
def test_fetch_shipping_methods_for_checkout_updates_existing_external_shipping_method(
mocked_webhook,
checkout_with_item,
@@ -936,7 +940,9 @@ def test_fetch_shipping_methods_for_checkout_updates_existing_external_shipping_
@freeze_time("2024-05-31 12:00:01")
[email protected]("saleor.plugins.manager.PluginsManager.list_shipping_methods_for_checkout")
+ "saleor.checkout.webhooks.list_shipping_methods.list_shipping_methods_for_checkout"
+)
def test_fetch_shipping_methods_for_checkout_removes_non_applicable_external_shipping_method(
mocked_webhook,
checkout_with_item,
@@ -998,7 +1004,9 @@ def test_fetch_shipping_methods_for_checkout_removes_non_applicable_external_shi
@freeze_time("2024-05-31 12:00:01")
[email protected]("saleor.plugins.manager.PluginsManager.list_shipping_methods_for_checkout")
+ "saleor.checkout.webhooks.list_shipping_methods.list_shipping_methods_for_checkout"
+)
def test_fetch_shipping_methods_for_checkout_non_applicable_assigned_external_shipping_method(
mocked_webhook,
checkout_with_item,
@@ -1070,7 +1078,9 @@ def test_fetch_shipping_methods_for_checkout_non_applicable_assigned_external_sh
@mock.patch(
"saleor.checkout.webhooks.exclude_shipping.excluded_shipping_methods_for_checkout"
)
[email protected]("saleor.plugins.manager.PluginsManager.list_shipping_methods_for_checkout")
+ "saleor.checkout.webhooks.list_shipping_methods.list_shipping_methods_for_checkout"
+)
def test_fetch_shipping_methods_for_checkout_with_excluded_external_shipping_method(
mocked_list_shipping_methods,
mocked_exclude_shipping_methods,
@@ -1127,7 +1137,9 @@ def test_fetch_shipping_methods_for_checkout_with_excluded_external_shipping_met
@freeze_time("2024-05-31 12:00:01")
[email protected]("saleor.plugins.manager.PluginsManager.list_shipping_methods_for_checkout")
+ "saleor.checkout.webhooks.list_shipping_methods.list_shipping_methods_for_checkout"
+)
def test_fetch_shipping_methods_for_checkout_with_changed_price_of_external_shipping_method(
mocked_webhook,
checkout_with_item,
diff --git a/saleor/checkout/tests/webhooks/subscriptions/test_exclude_shipping.py b/saleor/checkout/tests/webhooks/subscriptions/test_exclude_shipping.py
index 4f8042033f..24f8024b58 100644
--- a/saleor/checkout/tests/webhooks/subscriptions/test_exclude_shipping.py
+++ b/saleor/checkout/tests/webhooks/subscriptions/test_exclude_shipping.py
@@ -73,26 +73,6 @@ subscription{
"""
-CHECKOUT_SHIPPING_LIST_AND_FILTER = """
- subscription {
- event {
- ... on CheckoutFilterShippingMethods {
- __typename
- checkout {
- id
- }
- }
- ... on ShippingListMethodsForCheckout {
- __typename
- checkout {
- id
- }
- }
- }
- }
-"""
-
-
@pytest.fixture
def subscription_checkout_filter_shipping_methods_webhook(subscription_webhook):
return subscription_webhook(
diff --git a/saleor/checkout/tests/webhooks/subscriptions/test_list_shipping_methods.py b/saleor/checkout/tests/webhooks/subscriptions/test_list_shipping_methods.py
new file mode 100644
index 0000000000..d4fa03b9a8
--- /dev/null
+++ b/saleor/checkout/tests/webhooks/subscriptions/test_list_shipping_methods.py
@@ -0,0 +1,113 @@
+import json
+
+import graphene
+import pytest
+
+from .....shipping.interface import ShippingMethodData
+from .....shipping.models import ShippingMethod
+from .....shipping.utils import convert_to_shipping_method_data
+from .....webhook.event_types import WebhookEventSyncType
+from .....webhook.transport.asynchronous.transport import (
+ create_deliveries_for_subscriptions,
+)
+from .....webhook.transport.synchronous.transport import (
+ create_delivery_for_subscription_sync_event,
+)
+
+SHIPPING_LIST_METHODS_FOR_CHECKOUT = """
+subscription{
+ event{
+ ...on ShippingListMethodsForCheckout{
+ checkout{
+ id
+ }
+ shippingMethods{
+ name
+ id
+ }
+ }
+ }
+}
+"""
+
+
+def subscription_shipping_list_methods_for_checkout_webhook(subscription_webhook):
+ return subscription_webhook(
+ SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ )
+
+
+def subscription_checkout_shipping_filter_and_list_missing_one_in_definition(
+ subscription_webhook,
+):
+ from .....webhook.tests.subscription_webhooks import (
+ subscription_queries as queries,
+ )
+
+ return subscription_webhook(
+ queries.THUMBNAIL_CREATED,
+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ )
+
+
+def test_shipping_list_methods_for_checkout(
+ checkout_with_shipping_required,
+ subscription_shipping_list_methods_for_checkout_webhook,
+ address,
+ shipping_method,
+):
+ # given
+ checkout = checkout_with_shipping_required
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ webhooks = [subscription_shipping_list_methods_for_checkout_webhook]
+ event_type = WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ internal_methods: list[ShippingMethodData] = []
+ for method in ShippingMethod.objects.all():
+ shipping_method_data = convert_to_shipping_method_data(
+ method, method.channel_listings.get(channel=checkout.channel)
+ )
+ internal_methods.append(shipping_method_data)
+
+ # when
+ deliveries = create_deliveries_for_subscriptions(
+ event_type, (checkout, internal_methods), webhooks
+ )
+
+ # then
+ shipping_methods = [
+ {
+ "id": graphene.Node.to_global_id("ShippingMethod", sm.id),
+ "name": sm.name,
+ }
+ for sm in internal_methods
+ ]
+ payload = json.loads(deliveries[0].payload.get_payload())
+
+ assert payload["checkout"] == {"id": checkout_id}
+ for method in shipping_methods:
+ assert method in payload["shippingMethods"]
+ assert len(deliveries) == len(webhooks)
+ assert deliveries[0].webhook == webhooks[0]
+
+
+def test_checkout_list_methods_mismatch_in_subscription_query_definition(
+ checkout_ready_to_complete,
+ subscription_checkout_shipping_filter_and_list_missing_one_in_definition,
+):
+ # 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
diff --git a/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.py b/saleor/checkout/tests/webhooks/test_list_shipping_methods.py
similarity index 53%
rename from saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.py
rename to saleor/checkout/tests/webhooks/test_list_shipping_methods.py
index 61b9c2598c..2b5f103126 100644
--- a/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.py
+++ b/saleor/checkout/tests/webhooks/test_list_shipping_methods.py
@@ -7,30 +7,33 @@ from django.utils import timezone
from ....webhook import const
from ....webhook.event_types import WebhookEventSyncType
from ....webhook.payloads import generate_checkout_payload
-from ....webhook.transport.shipping import (
- get_cache_data_for_shipping_list_methods_for_checkout,
-)
+from ....webhook.response_schemas.utils.annotations import logger as annotations_logger
from ....webhook.transport.utils import generate_cache_key_for_webhook
-from ..plugin import CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT
+from ...utils import get_or_create_checkout_metadata
+from ...webhooks.list_shipping_methods import (
+ CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ _get_cache_data_for_shipping_list_methods_for_checkout,
+ _parse_list_shipping_methods_response,
+ list_shipping_methods_for_checkout,
+)
@mock.patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-def test_get_shipping_methods_for_checkout_webhook_response_none(
+def test_list_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(
+ response = list_shipping_methods_for_checkout(
checkout,
[],
- None,
+ allow_replica=False,
+ requestor=None,
)
# then
@@ -39,10 +42,9 @@ def test_get_shipping_methods_for_checkout_webhook_response_none(
@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(
+def test_list_shipping_methods_for_checkout_set_cache(
mocked_webhook,
mocked_cache_set,
- webhook_plugin,
checkout_with_item,
shipping_app,
):
@@ -55,10 +57,11 @@ def test_get_shipping_methods_for_checkout_set_cache(
"currency": "GBP",
}
]
- plugin = webhook_plugin()
# when
- plugin.get_shipping_methods_for_checkout(checkout_with_item, [], None)
+ list_shipping_methods_for_checkout(
+ checkout_with_item, [], allow_replica=False, requestor=None
+ )
# then
assert mocked_webhook.called
@@ -67,10 +70,9 @@ def test_get_shipping_methods_for_checkout_set_cache(
@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_no_webhook_response_sets_short_term_cache(
+def test_list_shipping_methods_no_webhook_response_sets_short_term_cache(
mocked_webhook,
mocked_cache_set,
- webhook_plugin,
checkout_with_item,
shipping_app,
):
@@ -78,7 +80,7 @@ def test_get_shipping_methods_no_webhook_response_sets_short_term_cache(
mocked_webhook.return_value = None
payload = generate_checkout_payload(checkout_with_item)
- key_data = get_cache_data_for_shipping_list_methods_for_checkout(payload)
+ key_data = _get_cache_data_for_shipping_list_methods_for_checkout(payload)
target_url = shipping_app.webhooks.first().target_url
cache_key = generate_cache_key_for_webhook(
key_data,
@@ -87,10 +89,10 @@ def test_get_shipping_methods_no_webhook_response_sets_short_term_cache(
shipping_app.id,
)
- plugin = webhook_plugin()
-
# when
- plugin.get_shipping_methods_for_checkout(checkout_with_item, [], None)
+ list_shipping_methods_for_checkout(
+ checkout_with_item, [], allow_replica=False, requestor=None
+ )
# then
assert mocked_webhook.called
@@ -103,10 +105,9 @@ def test_get_shipping_methods_no_webhook_response_sets_short_term_cache(
@mock.patch("saleor.webhook.transport.synchronous.transport.cache.get")
@mock.patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-def test_get_shipping_methods_for_checkout_use_cache(
+def test_list_shipping_methods_for_checkout_use_cache(
mocked_webhook,
mocked_cache_get,
- webhook_plugin,
checkout_with_item,
shipping_app,
):
@@ -119,10 +120,11 @@ def test_get_shipping_methods_for_checkout_use_cache(
"currency": "GBP",
}
]
- plugin = webhook_plugin()
# when
- plugin.get_shipping_methods_for_checkout(checkout_with_item, [], None)
+ list_shipping_methods_for_checkout(
+ checkout_with_item, [], allow_replica=False, requestor=None
+ )
# then
assert not mocked_webhook.called
@@ -131,19 +133,19 @@ def test_get_shipping_methods_for_checkout_use_cache(
@mock.patch("saleor.webhook.transport.synchronous.transport.cache.get")
@mock.patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-def test_get_shipping_methods_for_checkout_use_cache_for_empty_list(
+def test_list_shipping_methods_for_checkout_use_cache_for_empty_list(
mocked_webhook,
mocked_cache_get,
- webhook_plugin,
checkout_with_item,
shipping_app,
):
# given
mocked_cache_get.return_value = []
- plugin = webhook_plugin()
# when
- plugin.get_shipping_methods_for_checkout(checkout_with_item, [], None)
+ list_shipping_methods_for_checkout(
+ checkout_with_item, [], allow_replica=False, requestor=None
+ )
# then
assert not mocked_webhook.called
@@ -157,7 +159,6 @@ def test_checkout_change_invalidates_cache_key(
mocked_webhook,
mocked_cache_get,
mocked_cache_set,
- webhook_plugin,
checkout_with_item,
shipping_app,
):
@@ -174,7 +175,7 @@ def test_checkout_change_invalidates_cache_key(
mocked_cache_get.return_value = None
payload = generate_checkout_payload(checkout_with_item)
- key_data = get_cache_data_for_shipping_list_methods_for_checkout(payload)
+ key_data = _get_cache_data_for_shipping_list_methods_for_checkout(payload)
target_url = shipping_app.webhooks.first().target_url
cache_key = generate_cache_key_for_webhook(
key_data,
@@ -182,20 +183,21 @@ def test_checkout_change_invalidates_cache_key(
WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
shipping_app.id,
)
- plugin = webhook_plugin()
# when
checkout_with_item.email = "[email protected]"
checkout_with_item.save(update_fields=["email"])
new_payload = generate_checkout_payload(checkout_with_item)
- new_key_data = get_cache_data_for_shipping_list_methods_for_checkout(new_payload)
+ new_key_data = _get_cache_data_for_shipping_list_methods_for_checkout(new_payload)
new_cache_key = generate_cache_key_for_webhook(
new_key_data,
target_url,
WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
shipping_app.id,
)
- plugin.get_shipping_methods_for_checkout(checkout_with_item, [], None)
+ list_shipping_methods_for_checkout(
+ checkout_with_item, [], allow_replica=False, requestor=None
+ )
# then
assert cache_key != new_cache_key
@@ -214,7 +216,6 @@ def test_ignore_selected_fields_on_generating_cache_key(
mocked_webhook,
mocked_cache_get,
mocked_cache_set,
- webhook_plugin,
checkout_with_item,
shipping_app,
):
@@ -231,7 +232,7 @@ def test_ignore_selected_fields_on_generating_cache_key(
mocked_cache_get.return_value = None
payload = generate_checkout_payload(checkout_with_item)
- key_data = get_cache_data_for_shipping_list_methods_for_checkout(payload)
+ key_data = _get_cache_data_for_shipping_list_methods_for_checkout(payload)
target_url = shipping_app.webhooks.first().target_url
cache_key = generate_cache_key_for_webhook(
key_data,
@@ -239,20 +240,21 @@ def test_ignore_selected_fields_on_generating_cache_key(
WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
shipping_app.id,
)
- plugin = webhook_plugin()
# when
checkout_with_item.last_change = timezone.now() + datetime.timedelta(seconds=30)
checkout_with_item.save(update_fields=["last_change"])
new_payload = generate_checkout_payload(checkout_with_item)
- new_key_data = get_cache_data_for_shipping_list_methods_for_checkout(new_payload)
+ new_key_data = _get_cache_data_for_shipping_list_methods_for_checkout(new_payload)
new_cache_key = generate_cache_key_for_webhook(
new_key_data,
target_url,
WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
shipping_app.id,
)
- plugin.get_shipping_methods_for_checkout(checkout_with_item, [], None)
+ list_shipping_methods_for_checkout(
+ checkout_with_item, [], allow_replica=False, requestor=None
+ )
# then
assert cache_key == new_cache_key
@@ -262,3 +264,137 @@ def test_ignore_selected_fields_on_generating_cache_key(
mocked_webhook_response,
timeout=CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT,
)
+
+
[email protected](annotations_logger, "warning")
+def test_parse_list_shipping_methods_response_response_incorrect_format(
+ mocked_logger, app
+):
+ # given
+ response_data_with_incorrect_format = [[1], 2, "3"]
+
+ # when
+ result = _parse_list_shipping_methods_response(
+ response_data_with_incorrect_format, app, "USD"
+ )
+
+ # then
+ assert result == []
+ assert mocked_logger.call_count == len(response_data_with_incorrect_format)
+ error_msg = mocked_logger.call_args[0][1]
+ assert error_msg == "Skipping invalid shipping method (ListShippingMethodsSchema)"
+
+
+def test_parse_list_shipping_methods_with_metadata(app):
+ # given
+ response_data_with_meta = [
+ {
+ "id": 123,
+ "amount": 10,
+ "currency": "USD",
+ "name": "shipping",
+ "description": "Description",
+ "maximum_delivery_days": 10,
+ "minimum_delivery_days": 2,
+ "metadata": {"field": "value"},
+ }
+ ]
+
+ # when
+ response = _parse_list_shipping_methods_response(
+ response_data_with_meta, app, "USD"
+ )
+
+ # then
+ assert response[0].metadata == response_data_with_meta[0]["metadata"]
+ assert response[0].description == response_data_with_meta[0]["description"]
+
+
+def test_parse_list_shipping_methods_with_metadata_in_incorrect_format(app):
+ # given
+ response_data_with_meta = [
+ {
+ "id": 123,
+ "amount": 10,
+ "currency": "USD",
+ "name": "shipping",
+ "description": "Description",
+ "maximum_delivery_days": 10,
+ "minimum_delivery_days": 2,
+ "metadata": {"field": None},
+ }
+ ]
+
+ # when
+ response = _parse_list_shipping_methods_response(
+ response_data_with_meta, app, "USD"
+ )
+
+ # then
+ assert response[0].metadata == {}
+
+
+def test_parse_list_shipping_methods_metadata_absent_in_response(app):
+ # given
+ response_data_with_meta = [
+ {
+ "id": 123,
+ "amount": 10,
+ "currency": "USD",
+ "name": "shipping",
+ "description": "Description",
+ "maximum_delivery_days": 10,
+ "minimum_delivery_days": 2,
+ }
+ ]
+
+ # when
+ response = _parse_list_shipping_methods_response(
+ response_data_with_meta, app, "USD"
+ )
+
+ # then
+ assert response[0].metadata == {}
+
+
+def test_parse_list_shipping_methods_metadata_is_none(app):
+ # given
+ response_data_with_meta = [
+ {
+ "id": "123",
+ "amount": 10,
+ "currency": "USD",
+ "name": "shipping",
+ "description": "Description",
+ "maximum_delivery_days": 10,
+ "minimum_delivery_days": 2,
+ "metadata": None,
+ }
+ ]
+
+ # when
+ response = _parse_list_shipping_methods_response(
+ response_data_with_meta, app, "USD"
+ )
+
+ # then
+ assert response[0].metadata == {}
+
+
+def test_get_cache_data_for_shipping_list_methods_for_checkout(checkout_with_items):
+ # given
+ metadata = get_or_create_checkout_metadata(checkout_with_items)
+ metadata.store_value_in_private_metadata({"external_app_shipping_id": "something"})
+ metadata.save()
+ payload_str = generate_checkout_payload(checkout_with_items)
+ assert "last_change" in payload_str
+ assert "meta" in payload_str
+ assert "external_app_shipping_id" in payload_str
+
+ # when
+ cache_data = _get_cache_data_for_shipping_list_methods_for_checkout(payload_str)
+
+ # then
+ assert "last_change" not in cache_data[0]
+ assert "meta" not in cache_data[0]
+ assert "external_app_shipping_id" not in cache_data[0]["private_metadata"]
diff --git a/saleor/checkout/webhooks/list_shipping_methods.py b/saleor/checkout/webhooks/list_shipping_methods.py
new file mode 100644
index 0000000000..cc7529c457
--- /dev/null
+++ b/saleor/checkout/webhooks/list_shipping_methods.py
@@ -0,0 +1,100 @@
+import json
+import logging
+from typing import TYPE_CHECKING, Any, Union
+
+from django.conf import settings
+from pydantic import ValidationError
+
+from ...shipping.interface import ShippingMethodData
+from ...webhook.event_types import WebhookEventSyncType
+from ...webhook.payloads import generate_checkout_payload
+from ...webhook.response_schemas.shipping import ListShippingMethodsSchema
+from ...webhook.transport.synchronous.transport import (
+ trigger_webhook_sync_if_not_cached,
+)
+from ...webhook.utils import get_webhooks_for_event
+from ..models import Checkout
+
+if TYPE_CHECKING:
+ from ...account.models import User
+ from ...app.models import App
+
+logger = logging.getLogger(__name__)
+
+CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT = 3600 * 12
+
+
+def list_shipping_methods_for_checkout(
+ checkout: "Checkout",
+ built_in_shipping_methods: list["ShippingMethodData"],
+ allow_replica: bool,
+ requestor: Union["App", "User", None],
+) -> list["ShippingMethodData"]:
+ methods: list[ShippingMethodData] = []
+ event_type = WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT
+ webhooks = get_webhooks_for_event(event_type)
+ if webhooks:
+ payload = generate_checkout_payload(checkout, requestor)
+ cache_data = _get_cache_data_for_shipping_list_methods_for_checkout(payload)
+ for webhook in webhooks:
+ response_data = trigger_webhook_sync_if_not_cached(
+ event_type=event_type,
+ payload=payload,
+ webhook=webhook,
+ cache_data=cache_data,
+ allow_replica=allow_replica,
+ subscribable_object=(checkout, built_in_shipping_methods),
+ request_timeout=settings.WEBHOOK_SYNC_TIMEOUT,
+ cache_timeout=CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ requestor=requestor,
+ )
+
+ if response_data:
+ shipping_methods = _parse_list_shipping_methods_response(
+ response_data, webhook.app, checkout.currency
+ )
+ methods.extend(shipping_methods)
+ return [method for method in methods if method.price.currency == checkout.currency]
+
+
+def _parse_list_shipping_methods_response(
+ response_data: Any, app: "App", object_currency: str
+) -> list["ShippingMethodData"]:
+ try:
+ list_shipping_method_model = ListShippingMethodsSchema.model_validate(
+ response_data,
+ context={
+ "app": app,
+ "currency": object_currency,
+ "custom_message": "Skipping invalid shipping method (ListShippingMethodsSchema)",
+ },
+ )
+ except ValidationError:
+ logger.warning("Skipping invalid shipping method response: %s", response_data)
+ return []
+ return [
+ ShippingMethodData(
+ id=shipping_method.id,
+ name=shipping_method.name,
+ price=shipping_method.price,
+ maximum_delivery_days=shipping_method.maximum_delivery_days,
+ minimum_delivery_days=shipping_method.minimum_delivery_days,
+ description=shipping_method.description,
+ metadata=shipping_method.metadata,
+ private_metadata=shipping_method.private_metadata,
+ )
+ for shipping_method in list_shipping_method_model.root
+ ]
+
+
+def _get_cache_data_for_shipping_list_methods_for_checkout(payload: str) -> dict:
+ key_data = json.loads(payload)
+
+ # drop fields that change between requests but are not relevant for cache key
+ key_data[0].pop("last_change")
+ key_data[0].pop("meta")
+ # Drop the external_app_shipping_id from the cache key as it should not have an
+ # impact on cache invalidation
+ if "external_app_shipping_id" in key_data[0].get("private_metadata", {}):
+ del key_data[0]["private_metadata"]["external_app_shipping_id"]
+ return key_data
diff --git a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py
index a7d4ef9c39..7e6646485f 100644
--- a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py
+++ b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py
@@ -569,7 +569,7 @@ def test_create_checkout_with_order_promotion(
# when
user_api_client.ensure_access_token()
- with django_assert_num_queries(106):
+ with django_assert_num_queries(107):
response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
# then
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
index da926105f7..71f61a7795 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
@@ -1367,7 +1367,9 @@ def test_checkout_lines_add_custom_price_and_catalogue_promotion(
@freeze_time("2024-05-31 12:00:01")
[email protected]("saleor.plugins.manager.PluginsManager.list_shipping_methods_for_checkout")
+ "saleor.checkout.webhooks.list_shipping_methods.list_shipping_methods_for_checkout"
+)
def test_checkout_lines_marks_shipping_methods_as_stale(
mocked_webhook,
app_api_client,
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 926624ffd6..c7a7d28ae3 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -88,6 +88,33 @@ def expected_gift_card_payment_gateway():
}
+SHIPPING_LIST_METHODS_FOR_CHECKOUT_SUBSCRIPTION = """
+subscription{
+ event{
+ ...on ShippingListMethodsForCheckout{
+ checkout{
+ id
+ }
+ shippingMethods{
+ name
+ id
+ }
+ }
+ }
+}
+"""
+
+
+def subscription_shipping_list_methods_for_checkout_webhook(subscription_webhook):
+ from ....webhook.event_types import WebhookEventSyncType
+
+ return subscription_webhook(
+ SHIPPING_LIST_METHODS_FOR_CHECKOUT_SUBSCRIPTION,
+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
+ )
+
+
GET_CHECKOUT_PAYMENTS_QUERY = """
query getCheckoutPayments($id: ID) {
checkout(id: $id) {
@@ -832,6 +859,7 @@ def test_checkout_available_shipping_methods_with_price_displayed(
site_settings,
shipping_app,
):
+ send_webhook_request_sync.return_value = []
shipping_method = shipping_zone.shipping_methods.first()
listing = shipping_zone.shipping_methods.first().channel_listings.first()
expected_shipping_price = Money(10, "USD")
@@ -5326,9 +5354,8 @@ def test_checkout_delivery_returns_none_when_no_delivery_assigned(
@freezegun.freeze_time("2023-01-01 12:00:00")
@mock.patch(
- "saleor.plugins.webhook.plugin.WebhookPlugin.get_shipping_methods_for_checkout"
+ "saleor.checkout.webhooks.list_shipping_methods.list_shipping_methods_for_checkout"
)
-@override_settings(PLUGINS=["saleor.plugins.webhook.plugin.WebhookPlugin"])
def test_checkout_delivery_do_not_trigger_any_webhook_calls(
mocked_shipping_webhook_fetch,
user_api_client,
@@ -5370,7 +5397,7 @@ def test_checkout_delivery_do_not_trigger_any_webhook_calls(
@mock.patch(
- "saleor.plugins.webhook.plugin.WebhookPlugin.get_shipping_methods_for_checkout"
+ "saleor.checkout.webhooks.list_shipping_methods.list_shipping_methods_for_checkout"
)
def test_checkout_delivery_returns_shipping_when_marked_as_invalid(
mocked_shipping_webhook_fetch,
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 5bf52ae26f..8ab37c3998 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -61,7 +61,6 @@ if TYPE_CHECKING:
ProductType,
ProductVariant,
)
- from ..shipping.interface import ShippingMethodData
from ..shipping.models import ShippingMethod, ShippingZone
from ..site.models import SiteSettings
from ..tax.models import TaxClass
@@ -745,12 +744,6 @@ class BasePlugin:
get_order_shipping_tax_rate: Callable[["Order", Any], Any]
get_payment_config: Callable[[Any], Any]
- # Note: This method is deprecated and will be removed in a future release.
- # Webhook-related functionality will be moved from the plugin to core modules.
- get_shipping_methods_for_checkout: Callable[
- ["Checkout", list["ShippingMethodData"], Any], list["ShippingMethodData"]
- ]
-
get_supported_currencies: Callable[[Any], Any]
# Return tax code from object meta.
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index f9d14092bd..c445cf87a8 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -76,7 +76,6 @@ if TYPE_CHECKING:
ProductType,
ProductVariant,
)
- from ..shipping.interface import ShippingMethodData
from ..shipping.models import ShippingMethod, ShippingZone
from ..site.models import SiteSettings
from ..tax.models import TaxClass
@@ -2576,36 +2575,6 @@ class PluginsManager(PaymentInterface):
)
return gateways
- def list_shipping_methods_for_checkout(
- self,
- checkout: "Checkout",
- built_in_shipping_methods: list["ShippingMethodData"],
- channel_slug: str | None = None,
- active_only: bool = True,
- ) -> list["ShippingMethodData"]:
- channel_slug = channel_slug if channel_slug else checkout.channel.slug
- plugins = self.get_plugins(channel_slug=channel_slug, active_only=active_only)
- shipping_plugins = [
- plugin
- for plugin in plugins
- if hasattr(plugin, "get_shipping_methods_for_checkout")
- ]
-
- shipping_methods = []
- for plugin in shipping_plugins:
- shipping_methods.extend(
- # https://github.com/python/mypy/issues/9975
- getattr(plugin, "get_shipping_methods_for_checkout")(
- checkout, built_in_shipping_methods, None
- )
- )
- return list(
- filter(
- lambda method: method.price.currency == checkout.currency,
- shipping_methods,
- )
- )
-
def list_external_authentications(self, active_only: bool = True) -> list[dict]:
auth_basic_method = "external_obtain_access_tokens"
plugins = self.get_plugins(active_only=active_only)
diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.py
index c45ae5047c..8fae3d455a 100644
--- a/saleor/plugins/tests/sample_plugins.py
+++ b/saleor/plugins/tests/sample_plugins.py
@@ -14,7 +14,6 @@ from ...payment.interface import (
TransactionSessionData,
TransactionSessionResult,
)
-from ...shipping.interface import ShippingMethodData
from ..base_plugin import BasePlugin, ConfigurationTypeField, ExternalAccessTokens
if TYPE_CHECKING:
@@ -366,27 +365,6 @@ class PluginSample(BasePlugin):
def payment_method_process_tokenization(self, request_data, previous_value):
return previous_value
- def get_shipping_methods_for_checkout(
- self,
- checkout: "Checkout",
- built_in_shipping_methods: list["ShippingMethodData"],
- previous_value: Any,
- ) -> list["ShippingMethodData"]:
- different_currency = "EUR"
- assert checkout.currency != different_currency
- return [
- ShippingMethodData(
- id="123",
- price=Money(Decimal(10), currency=different_currency),
- name="EUR shipping",
- ),
- ShippingMethodData(
- id="123",
- price=Money(Decimal(10), currency=checkout.currency),
- name="Default shipping",
- ),
- ]
-
class ChannelPluginSample(PluginSample):
PLUGIN_ID = "channel.plugin.sample"
diff --git a/saleor/plugins/tests/test_manager.py b/saleor/plugins/tests/test_manager.py
index 8741cb60af..0f23c14618 100644
--- a/saleor/plugins/tests/test_manager.py
+++ b/saleor/plugins/tests/test_manager.py
@@ -1624,24 +1624,3 @@ def test_run_plugin_method_until_first_success_for_active_plugins_only(
# then
assert result is None
assert mock_run_method.call_count == calls
-
-
-def test_manager_skips_external_shipping_with_different_currency_than_checkout_currency(
- checkout_with_item,
-):
- # given
- plugins = ["saleor.plugins.tests.sample_plugins.PluginSample"]
-
- # when
-
- shipping_methods = PluginsManager(
- plugins=plugins
- ).list_shipping_methods_for_checkout(
- checkout=checkout_with_item,
- channel_slug=checkout_with_item.channel.slug,
- built_in_shipping_methods=[],
- )
-
- # then
- assert len(shipping_methods) == 1
- assert shipping_methods[0].price.currency == checkout_with_item.currency
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.py
index f3d08f8ca5..2692886a2f 100644
--- a/saleor/plugins/webhook/plugin.py
+++ b/saleor/plugins/webhook/plugin.py
@@ -4,7 +4,7 @@ from collections import defaultdict
from collections.abc import Callable, Iterable
from decimal import Decimal
from functools import partial
-from typing import TYPE_CHECKING, Any, Final, Optional, Union, cast
+from typing import TYPE_CHECKING, Any, Optional, Union, cast
import graphene
from django.conf import settings
@@ -111,10 +111,6 @@ from ...webhook.transport.payment import (
parse_list_payment_gateways_response,
parse_payment_action_response,
)
-from ...webhook.transport.shipping import (
- get_cache_data_for_shipping_list_methods_for_checkout,
- parse_list_shipping_methods_response,
-)
from ...webhook.transport.synchronous.transport import (
trigger_taxes_all_webhooks_sync,
trigger_transaction_request,
@@ -151,7 +147,6 @@ if TYPE_CHECKING:
ProductType,
ProductVariant,
)
- from ...shipping.interface import ShippingMethodData
from ...shipping.models import ShippingMethod, ShippingZone
from ...site.models import SiteSettings
from ...tax.models import TaxClass
@@ -159,11 +154,6 @@ if TYPE_CHECKING:
from ...webhook.models import Webhook
-# Set the timeout for the shipping methods cache to 12 hours as it was the lowest
-# time labels were valid for when checking documentation for the carriers
-# (FedEx, UPS, TNT, DHL).
-CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT: Final[int] = 3600 * 12
-
logger = logging.getLogger(__name__)
@@ -3593,38 +3583,6 @@ class WebhookPlugin(BasePlugin):
pregenerated_subscription_payloads=pregenerated_subscription_payloads,
)
- def get_shipping_methods_for_checkout(
- self,
- checkout: "Checkout",
- built_in_shipping_methods: list["ShippingMethodData"],
- previous_value: Any,
- ) -> list["ShippingMethodData"]:
- methods = []
- event_type = WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT
- webhooks = get_webhooks_for_event(event_type)
- if webhooks:
- payload = generate_checkout_payload(checkout, self.requestor)
- cache_data = get_cache_data_for_shipping_list_methods_for_checkout(payload)
- for webhook in webhooks:
- response_data = trigger_webhook_sync_if_not_cached(
- event_type=WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- payload=payload,
- webhook=webhook,
- cache_data=cache_data,
- allow_replica=self.allow_replica,
- subscribable_object=(checkout, built_in_shipping_methods),
- request_timeout=WEBHOOK_SYNC_TIMEOUT,
- cache_timeout=CACHE_TIME_SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- requestor=self.requestor,
- )
-
- if response_data:
- shipping_methods = parse_list_shipping_methods_response(
- response_data, webhook.app, checkout.currency
- )
- methods.extend(shipping_methods)
- return methods
-
def get_tax_code_from_object_meta(
self,
obj: Union["Product", "ProductType", "TaxClass"],
diff --git a/saleor/plugins/webhook/tests/test_shipping_webhook.py b/saleor/plugins/webhook/tests/test_shipping_webhook.py
deleted file mode 100644
index d84bd2a9d8..0000000000
--- a/saleor/plugins/webhook/tests/test_shipping_webhook.py
+++ /dev/null
@@ -1,119 +0,0 @@
-from unittest import mock
-
-from ....core.models import EventDelivery
-from ....webhook.event_types import WebhookEventSyncType
-from ....webhook.response_schemas.utils.annotations import logger as annotations_logger
-from ....webhook.transport.shipping import (
- parse_list_shipping_methods_response,
-)
-from ....webhook.transport.synchronous.transport import trigger_webhook_sync
-
-
[email protected]("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
-def test_trigger_webhook_sync(mock_request, shipping_app):
- data = '{"key": "value"}'
- webhook = shipping_app.webhooks.first()
- trigger_webhook_sync(
- WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT, data, webhook, False
- )
- mock_request.assert_called_once()
- assert not EventDelivery.objects.exists()
-
-
[email protected](annotations_logger, "warning")
-def test_parse_list_shipping_methods_response_response_incorrect_format(
- mocked_logger, app
-):
- # given
- response_data_with_incorrect_format = [[1], 2, "3"]
- # when
- result = parse_list_shipping_methods_response(
- response_data_with_incorrect_format, app, "USD"
- )
- # then
- assert result == []
- # Ensure the warning about invalit method data wa logged
- assert mocked_logger.call_count == len(response_data_with_incorrect_format)
- error_msg = mocked_logger.call_args[0][1]
- assert error_msg == "Skipping invalid shipping method (ListShippingMethodsSchema)"
-
-
-def test_parse_list_shipping_methods_with_metadata(app):
- # given
- response_data_with_meta = [
- {
- "id": 123,
- "amount": 10,
- "currency": "USD",
- "name": "shipping",
- "description": "Description",
- "maximum_delivery_days": 10,
- "minimum_delivery_days": 2,
- "metadata": {"field": "value"},
- }
- ]
- # when
- response = parse_list_shipping_methods_response(response_data_with_meta, app, "USD")
- # then
- assert response[0].metadata == response_data_with_meta[0]["metadata"]
- assert response[0].description == response_data_with_meta[0]["description"]
-
-
-def test_parse_list_shipping_methods_with_metadata_in_incorrect_format(app):
- # given
- response_data_with_meta = [
- {
- "id": 123,
- "amount": 10,
- "currency": "USD",
- "name": "shipping",
- "description": "Description",
- "maximum_delivery_days": 10,
- "minimum_delivery_days": 2,
- "metadata": {"field": None},
- }
- ]
- # when
- response = parse_list_shipping_methods_response(response_data_with_meta, app, "USD")
- # then
- assert response[0].metadata == {}
-
-
-def test_parse_list_shipping_methods_metadata_absent_in_response(app):
- # given
- response_data_with_meta = [
- {
- "id": 123,
- "amount": 10,
- "currency": "USD",
- "name": "shipping",
- "description": "Description",
- "maximum_delivery_days": 10,
- "minimum_delivery_days": 2,
- }
- ]
- # when
- response = parse_list_shipping_methods_response(response_data_with_meta, app, "USD")
-
- # then
- assert response[0].metadata == {}
-
-
-def test_parse_list_shipping_methods_metadata_is_none(app):
- # given
- response_data_with_meta = [
- {
- "id": "123",
- "amount": 10,
- "currency": "USD",
- "name": "shipping",
- "description": "Description",
- "maximum_delivery_days": 10,
- "minimum_delivery_days": 2,
- "metadata": None,
- }
- ]
- # when
- response = parse_list_shipping_methods_response(response_data_with_meta, app, "USD")
- # then
- assert response[0].metadata == {}
diff --git a/saleor/webhook/tests/fixtures/subscription_webhooks.py b/saleor/webhook/tests/fixtures/subscription_webhooks.py
index 39c80dd701..ac80791771 100644
--- a/saleor/webhook/tests/fixtures/subscription_webhooks.py
+++ b/saleor/webhook/tests/fixtures/subscription_webhooks.py
@@ -1301,14 +1301,6 @@ def subscription_payment_list_gateways_webhook(subscription_webhook):
)
-def subscription_shipping_list_methods_for_checkout_webhook(subscription_webhook):
- return subscription_webhook(
- queries.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,
- )
-
-
@pytest.fixture
def subscription_thumbnail_created_webhook(subscription_webhook):
return subscription_webhook(
@@ -1347,13 +1339,3 @@ 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/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/webhook/tests/subscription_webhooks/subscription_queries.py
index 1af3e13c58..067523249e 100644
--- a/saleor/webhook/tests/subscription_webhooks/subscription_queries.py
+++ b/saleor/webhook/tests/subscription_webhooks/subscription_queries.py
@@ -3084,23 +3084,6 @@ PAYMENT_LIST_GATEWAYS = """
"""
-SHIPPING_LIST_METHODS_FOR_CHECKOUT = """
-subscription{
- event{
- ...on ShippingListMethodsForCheckout{
- checkout{
- id
- }
- shippingMethods{
- name
- id
- }
- }
- }
-}
-"""
-
-
INVALID_MULTIPLE_EVENTS = """
subscription{
event{
diff --git a/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
index 5eab3584de..206c445d57 100644
--- a/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
+++ b/saleor/webhook/tests/subscription_webhooks/test_create_deliveries_for_subscription.py
@@ -12,21 +12,16 @@ from ....giftcard.models import GiftCard
from ....graphql.webhook.subscription_query import SubscriptionQuery
from ....menu.models import Menu, MenuItem
from ....product.models import Category
-from ....shipping.interface import ShippingMethodData
from ....shipping.models import (
ShippingMethod,
ShippingZone,
)
-from ....shipping.utils import convert_to_shipping_method_data
from ....site.models import SiteSettings
from ...event_types import WebhookEventAsyncType, WebhookEventSyncType
from ...transport.asynchronous.transport import (
create_deliveries_for_subscriptions,
logger,
)
-from ...transport.synchronous.transport import (
- create_delivery_for_subscription_sync_event,
-)
from . import subscription_queries
from .payloads import (
generate_account_events_payload,
@@ -2871,66 +2866,6 @@ def test_transaction_item_metadata_updated(
assert deliveries[0].webhook == webhooks[0]
-def test_shipping_list_methods_for_checkout(
- checkout_with_shipping_required,
- subscription_shipping_list_methods_for_checkout_webhook,
- address,
- shipping_method,
-):
- # given
- checkout = checkout_with_shipping_required
- checkout.shipping_address = address
- checkout.shipping_method = shipping_method
- webhooks = [subscription_shipping_list_methods_for_checkout_webhook]
- event_type = WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- internal_methods: list[ShippingMethodData] = []
- for method in ShippingMethod.objects.all():
- shipping_method_data = convert_to_shipping_method_data(
- method, method.channel_listings.get(channel=checkout.channel)
- )
- internal_methods.append(shipping_method_data)
-
- # when
- deliveries = create_deliveries_for_subscriptions(
- event_type, (checkout, internal_methods), webhooks
- )
- # then
- shipping_methods = [
- {
- "id": graphene.Node.to_global_id("ShippingMethod", sm.id),
- "name": sm.name,
- }
- for sm in internal_methods
- ]
- payload = json.loads(deliveries[0].payload.get_payload())
-
- assert payload["checkout"] == {"id": checkout_id}
- for method in shipping_methods:
- assert method in payload["shippingMethods"]
- assert len(deliveries) == len(webhooks)
- assert deliveries[0].webhook == webhooks[0]
-
-
-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
-
-
@patch.object(logger, "info")
def test_create_deliveries_for_subscriptions_unsubscribable_event(
mocked_logger, product, subscription_product_updated_webhook, any_webhook
diff --git a/saleor/webhook/transport/shipping.py b/saleor/webhook/transport/shipping.py
deleted file mode 100644
index 675a61ca04..0000000000
--- a/saleor/webhook/transport/shipping.py
+++ /dev/null
@@ -1,56 +0,0 @@
-import json
-import logging
-from typing import Any
-
-from pydantic import ValidationError
-
-from ...app.models import App
-from ...shipping.interface import ShippingMethodData
-from ..response_schemas.shipping import (
- ListShippingMethodsSchema,
-)
-
-logger = logging.getLogger(__name__)
-
-
-def parse_list_shipping_methods_response(
- response_data: Any, app: "App", object_currency: str
-) -> list["ShippingMethodData"]:
- try:
- list_shipping_method_model = ListShippingMethodsSchema.model_validate(
- response_data,
- context={
- "app": app,
- "currency": object_currency,
- "custom_message": "Skipping invalid shipping method (ListShippingMethodsSchema)",
- },
- )
- except ValidationError:
- logger.warning("Skipping invalid shipping method response: %s", response_data)
- return []
- return [
- ShippingMethodData(
- id=shipping_method.id,
- name=shipping_method.name,
- price=shipping_method.price,
- maximum_delivery_days=shipping_method.maximum_delivery_days,
- minimum_delivery_days=shipping_method.minimum_delivery_days,
- description=shipping_method.description,
- metadata=shipping_method.metadata,
- private_metadata=shipping_method.private_metadata,
- )
- for shipping_method in list_shipping_method_model.root
- ]
-
-
-def get_cache_data_for_shipping_list_methods_for_checkout(payload: str) -> dict:
- key_data = json.loads(payload)
-
- # drop fields that change between requests but are not relevant for cache key
- key_data[0].pop("last_change")
- key_data[0].pop("meta")
- # Drop the external_app_shipping_id from the cache key as it should not have an
- # impact on cache invalidation
- if "external_app_shipping_id" in key_data[0].get("private_metadata", {}):
- del key_data[0]["private_metadata"]["external_app_shipping_id"]
- return key_data
diff --git a/saleor/webhook/transport/tests/test_shipping.py b/saleor/webhook/transport/tests/test_shipping.py
deleted file mode 100644
index dd725bf925..0000000000
--- a/saleor/webhook/transport/tests/test_shipping.py
+++ /dev/null
@@ -1,26 +0,0 @@
-from ....checkout.utils import get_or_create_checkout_metadata
-from ...payloads import (
- generate_checkout_payload,
-)
-from ..shipping import (
- get_cache_data_for_shipping_list_methods_for_checkout,
-)
-
-
-def test_get_cache_data_for_shipping_list_methods_for_checkout(checkout_with_items):
- # given
- metadata = get_or_create_checkout_metadata(checkout_with_items)
- metadata.store_value_in_private_metadata({"external_app_shipping_id": "something"})
- metadata.save()
- payload_str = generate_checkout_payload(checkout_with_items)
- assert "last_change" in payload_str
- assert "meta" in payload_str
- assert "external_app_shipping_id" in payload_str
-
- # when
- cache_data = get_cache_data_for_shipping_list_methods_for_checkout(payload_str)
-
- # then
- assert "last_change" not in cache_data[0]
- assert "meta" not in cache_data[0]
- assert "external_app_shipping_id" not in cache_data[0]["private_metadata"]
diff --git a/saleor/webhook/transport/tests/test_shipping_helpers.py b/saleor/webhook/transport/tests/test_shipping_helpers.py
deleted file mode 100644
index 3eed8ad96a..0000000000
--- a/saleor/webhook/transport/tests/test_shipping_helpers.py
+++ /dev/null
@@ -1,62 +0,0 @@
-import base64
-
-from ..shipping_helpers import convert_to_app_id_with_identifier
-
-
-def test_convert_to_app_id_with_identifier_valid(app):
- # given
- method_id = "123"
- shipping_app_id = base64.b64encode(str.encode(f"app:{app.pk}:{method_id}")).decode(
- "utf-8"
- )
-
- # when
- app_id_with_identifier = convert_to_app_id_with_identifier(shipping_app_id)
-
- # then
- assert app_id_with_identifier
- assert (
- base64.b64decode(app_id_with_identifier).decode()
- == f"app:{app.identifier}:{method_id}"
- )
-
-
-def test_convert_to_app_id_with_identifier_missing_shipping_id(app):
- # given
- shipping_app_id = base64.b64encode(str.encode(f"app:{app.pk}")).decode("utf-8")
-
- # when
- app_id_with_identifier = convert_to_app_id_with_identifier(shipping_app_id)
-
- # then
- assert app_id_with_identifier is None
-
-
-def test_convert_to_app_id_with_identifier_missing_app_for_given_id():
- # given
- method_id = "123"
- app_id = 321
- shipping_app_id = base64.b64encode(str.encode(f"app:{app_id}:{method_id}")).decode(
- "utf-8"
- )
-
- # when
- app_id_with_identifier = convert_to_app_id_with_identifier(shipping_app_id)
-
- # then
- assert app_id_with_identifier is None
-
-
-def test_convert_to_app_id_with_identifier_invalid_app_id():
- # given
- method_id = "123"
- app_id = "xyz"
- shipping_app_id = base64.b64encode(str.encode(f"app:{app_id}:{method_id}")).decode(
- "utf-8"
- )
-
- # when
- app_id_with_identifier = convert_to_app_id_with_identifier(shipping_app_id)
-
- # then
- assert app_id_with_identifier is None