Skip to content

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

Fix error on shipping zone update mutation when countires are missing (#12818)

Krzysztof Kwaśniak6459dd2d9135
diff --git a/SECURITY.md b/SECURITY.md
index 7330ea569a..45b4331eed 100644
--- a/SECURITY.md
+++ b/SECURITY.md
@@ -14,7 +14,7 @@ The following releases of Saleor are currently supported.
Please DO NOT report security vulnerabilities using a public GitHub issue. If you believe you've found a security issue, please contact us through one of the following methods:
- https://github.com/saleor/saleor/security/advisories
- https://huntr.dev/repos/saleor/saleor
-- Alternavely, through our mailing list: [email protected]
+- Alternatively, through our mailing list: [email protected]
At this time we do not have a bounty program in place so we are not able to offer monetary rewards for any problems reported.
You can claim bounty rewards by reporting vulnerabilities using Huntr: https://huntr.dev.
diff --git a/saleor/graphql/shipping/mutations/shippings.py b/saleor/graphql/shipping/mutations/shippings.py
index e07c935a81..047b99613c 100644
--- a/saleor/graphql/shipping/mutations/shippings.py
+++ b/saleor/graphql/shipping/mutations/shippings.py
@@ -279,8 +279,7 @@ class ShippingZoneMixin:
}
)
else:
- countries = get_countries_without_shipping_zone()
- data["countries"].extend([country for country in countries])
+ cls._extend_shipping_zone_countries(data)
else:
data["default"] = False
return data
@@ -372,6 +371,14 @@ class ShippingZoneMixin:
id__in=shipping_zone_warehouses_to_delete
).delete()
+ @classmethod
+ def _extend_shipping_zone_countries(cls, data):
+ countries = get_countries_without_shipping_zone()
+ try:
+ data["countries"].extend([country for country in countries])
+ except (KeyError, AttributeError):
+ data["countries"] = [country for country in countries]
+
class ShippingZoneCreate(ShippingZoneMixin, ModelMutation):
class Arguments:
diff --git a/saleor/graphql/shipping/tests/mutations/test_shipping_zone_update.py b/saleor/graphql/shipping/tests/mutations/test_shipping_zone_update.py
index 2bd92c97a8..f434d6f431 100644
--- a/saleor/graphql/shipping/tests/mutations/test_shipping_zone_update.py
+++ b/saleor/graphql/shipping/tests/mutations/test_shipping_zone_update.py
@@ -2,6 +2,7 @@ import json
from unittest import mock
import graphene
+import pytest
from django.utils.functional import SimpleLazyObject
from freezegun import freeze_time
@@ -59,6 +60,25 @@ UPDATE_SHIPPING_ZONE_MUTATION = """
}
"""
+SHIPPING_ZONE_UPDATE_DEFAULT_TRUE_MUTATION = """
+ mutation ShippingZoneUpdate($id: ID!, $input: ShippingZoneUpdateInput!) {
+ shippingZoneUpdate(id: $id, input: $input) {
+ errors {
+ code
+ field
+ message
+ }
+ shippingZone {
+ id
+ default
+ countries {
+ code
+ }
+ }
+ }
+ }
+"""
+
def test_update_shipping_zone(
staff_api_client, shipping_zone, permission_manage_shipping
@@ -641,3 +661,39 @@ def test_update_shipping_zone_remove_channels_remove_common_warehouse_channel(
)
assert len(shipping_zone_data["warehouses"]) == 1
assert shipping_zone_data["warehouses"][0]["slug"] == warehouses[1].slug
+
+
+ "input, expected_countries",
+ (
+ ({"default": True, "countries": ["PL"]}, [{"code": "PL"}]),
+ ({"default": True, "countries": []}, []),
+ ({"default": True, "countries": None}, []),
+ ({"default": True}, []),
+ ),
+)
+def test_shipping_method_update_countries(
+ staff_api_client,
+ shipping_zone,
+ permission_manage_shipping,
+ input,
+ expected_countries,
+):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("ShippingZone", shipping_zone.id),
+ "input": input,
+ }
+
+ # when
+ staff_api_client.user.user_permissions.add(permission_manage_shipping)
+ response = staff_api_client.post_graphql(
+ SHIPPING_ZONE_UPDATE_DEFAULT_TRUE_MUTATION, variables
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["shippingZoneUpdate"]
+ assert data["errors"] == []
+ assert data["shippingZone"]["default"] is True
+ assert data["shippingZone"]["countries"] == expected_countries