Skip to content

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

Add more fields to external shipping method payload parsing (#15935) (#16075)

Jakub05515a27b9df
diff --git a/saleor/checkout/tests/test_checkout.py b/saleor/checkout/tests/test_checkout.py
index baa1ff931f..2f8f165b7e 100644
--- a/saleor/checkout/tests/test_checkout.py
+++ b/saleor/checkout/tests/test_checkout.py
@@ -126,6 +126,116 @@ def test_is_valid_delivery_method_external_method(
assert delivery_method_info.is_method_in_valid_methods(checkout_info)
+@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
+def test_is_valid_delivery_method_external_method_with_metadata_and_description(
+ mock_send_request, checkout_with_item, address, settings, shipping_app
+):
+ # given
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ assert not shipping_app.identifier
+ response_method_id = "abcd"
+ metadata = {"key": "value", "key_2": "value_2"}
+ description = "Shipping Method Description"
+ mock_response = [
+ {
+ "id": response_method_id,
+ "name": "Provider - Economy",
+ "amount": "10",
+ "currency": "USD",
+ "maximum_delivery_days": "7",
+ "minimum_delivery_days": "1",
+ "description": description,
+ "metadata": metadata,
+ }
+ ]
+ method_id = graphene.Node.to_global_id(
+ "app", f"{shipping_app.id}:{response_method_id}"
+ )
+
+ mock_send_request.return_value = mock_response
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.metadata_storage.private_metadata = {
+ PRIVATE_META_APP_SHIPPING_ID: method_id
+ }
+ checkout.save()
+ checkout.metadata_storage.save()
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+
+ # when
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ delivery_method_info = checkout_info.delivery_method_info
+
+ # then
+ assert delivery_method_info.delivery_method.metadata == metadata
+ assert delivery_method_info.delivery_method.description == description
+ assert delivery_method_info.is_method_in_valid_methods(checkout_info)
+
+
+ "invalid_metadata",
+ [
+ ("invalid", "format", "tuple"),
+ {9: "invalid key"},
+ ("", "invalid empty key"),
+ "invalid format string",
+ ],
+)
+@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
+def test_is_valid_delivery_method_external_method_with_invalid_metadata(
+ mock_send_request,
+ invalid_metadata,
+ checkout_with_item,
+ address,
+ settings,
+ shipping_app,
+):
+ # given
+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]
+ assert not shipping_app.identifier
+ response_method_id = "abcd"
+ metadata = invalid_metadata
+ description = "Shipping Method Description"
+ mock_response = [
+ {
+ "id": response_method_id,
+ "name": "Provider - Economy",
+ "amount": "10",
+ "currency": "USD",
+ "maximum_delivery_days": "7",
+ "minimum_delivery_days": "1",
+ "description": description,
+ "metadata": metadata,
+ }
+ ]
+ method_id = graphene.Node.to_global_id(
+ "app", f"{shipping_app.id}:{response_method_id}"
+ )
+
+ mock_send_request.return_value = mock_response
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.metadata_storage.private_metadata = {
+ PRIVATE_META_APP_SHIPPING_ID: method_id
+ }
+ checkout.save()
+ checkout.metadata_storage.save()
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+
+ # when
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ delivery_method_info = checkout_info.delivery_method_info
+
+ # then
+ assert delivery_method_info.delivery_method.metadata == {}
+ assert delivery_method_info.delivery_method.description == description
+ assert delivery_method_info.is_method_in_valid_methods(checkout_info)
+
+
@patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")
def test_is_valid_delivery_method_external_method_shipping_app_id_with_identifier(
mock_send_request, checkout_with_item, address, settings, shipping_app
diff --git a/saleor/plugins/webhook/tests/test_shipping_webhook.py b/saleor/plugins/webhook/tests/test_shipping_webhook.py
index 7e1f5bb3dc..d11aa0a3e5 100644
--- a/saleor/plugins/webhook/tests/test_shipping_webhook.py
+++ b/saleor/plugins/webhook/tests/test_shipping_webhook.py
@@ -938,3 +938,24 @@ def test_parse_list_shipping_methods_response_response_incorrect_format(app):
)
# then
assert result == []
+
+
+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)
+ # then
+ assert response[0].metadata == response_data_with_meta[0]["metadata"]
+ assert response[0].description == response_data_with_meta[0]["description"]
diff --git a/saleor/webhook/transport/shipping.py b/saleor/webhook/transport/shipping.py
index 63e465b7c3..273ec48c2f 100644
--- a/saleor/webhook/transport/shipping.py
+++ b/saleor/webhook/transport/shipping.py
@@ -52,6 +52,15 @@ def convert_to_app_id_with_identifier(shipping_app_id: str):
return to_shipping_app_id(app, splitted_id[2])
+def method_metadata_is_valid(metadata) -> bool:
+ if not isinstance(metadata, dict):
+ return False
+ for key, value in metadata.items():
+ if not isinstance(key, str) or not isinstance(value, str) or not key.strip():
+ return False
+ return True
+
+
def parse_list_shipping_methods_response(
response_data: Any, app: "App"
) -> list["ShippingMethodData"]:
@@ -64,6 +73,13 @@ def parse_list_shipping_methods_response(
method_amount = shipping_method_data.get("amount")
method_currency = shipping_method_data.get("currency")
method_maximum_delivery_days = shipping_method_data.get("maximum_delivery_days")
+ method_minimum_delivery_days = shipping_method_data.get("minimum_delivery_days")
+ method_description = shipping_method_data.get("description")
+ method_metadata = shipping_method_data.get("metadata")
+ if method_metadata:
+ method_metadata = (
+ method_metadata if method_metadata_is_valid(method_metadata) else {}
+ )
shipping_methods.append(
ShippingMethodData(
@@ -71,6 +87,9 @@ def parse_list_shipping_methods_response(
name=method_name,
price=Money(method_amount, method_currency),
maximum_delivery_days=method_maximum_delivery_days,
+ minimum_delivery_days=method_minimum_delivery_days,
+ description=method_description,
+ metadata=method_metadata,
)
)
return shipping_methods