Skip to content

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

fix unhandled none cases with shipping method list and add tests (#16… (#16646)

Jakubad050b84d194
diff --git a/saleor/plugins/webhook/tests/test_shipping_webhook.py b/saleor/plugins/webhook/tests/test_shipping_webhook.py
index 1fa5ecfdec..1b6d3a4d2f 100644
--- a/saleor/plugins/webhook/tests/test_shipping_webhook.py
+++ b/saleor/plugins/webhook/tests/test_shipping_webhook.py
@@ -1172,3 +1172,62 @@ def test_parse_list_shipping_methods_with_metadata(app):
# 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)
+ # 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)
+ # 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)
+ # then
+ assert response[0].metadata == {}
diff --git a/saleor/webhook/transport/shipping.py b/saleor/webhook/transport/shipping.py
index 45d09cad2a..7760722ad2 100644
--- a/saleor/webhook/transport/shipping.py
+++ b/saleor/webhook/transport/shipping.py
@@ -74,7 +74,7 @@ def parse_list_shipping_methods_response(
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")
+ method_metadata = shipping_method_data.get("metadata") or {}
if method_metadata:
method_metadata = (
method_metadata if method_metadata_is_valid(method_metadata) else {}