Skip to content

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

Ensure that only shipping methods from allowed shipping zones can be assigned in checkoutShippingMethodUpdate mutation

IKarbowiak38c982f64dbb
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index 4e24e91f58..646a4ee7e5 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -2838,9 +2838,10 @@ MUTATION_UPDATE_SHIPPING_METHOD = """
$checkoutId:ID!, $shippingMethodId:ID!){
checkoutShippingMethodUpdate(
checkoutId:$checkoutId, shippingMethodId:$shippingMethodId) {
- errors {
+ checkoutErrors {
field
message
+ code
}
checkout {
id
@@ -2882,18 +2883,17 @@ def test_checkout_shipping_method_update(
mock_clean_shipping.assert_called_once_with(
checkout_info=checkout_info, lines=lines, method=shipping_method
)
-
+ errors = data["checkoutErrors"]
if is_valid_shipping_method:
- assert not data["errors"]
+ assert not errors
assert data["checkout"]["id"] == checkout_id
assert checkout.shipping_method == shipping_method
else:
- assert data["errors"] == [
- {
- "field": "shippingMethod",
- "message": "This shipping method is not applicable.",
- }
- ]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "shippingMethod"
+ assert (
+ errors[0]["code"] == CheckoutErrorCode.SHIPPING_METHOD_NOT_APPLICABLE.name
+ )
assert checkout.shipping_method is None
@@ -2921,12 +2921,10 @@ def test_checkout_shipping_method_update_excluded_postal_code(
checkout.refresh_from_db()
- assert data["errors"] == [
- {
- "field": "shippingMethod",
- "message": "This shipping method is not applicable.",
- }
- ]
+ errors = data["checkoutErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "shippingMethod"
+ assert errors[0]["code"] == CheckoutErrorCode.SHIPPING_METHOD_NOT_APPLICABLE.name
assert checkout.shipping_method is None
assert (
mock_is_shipping_method_available.call_count
@@ -2934,6 +2932,64 @@ def test_checkout_shipping_method_update_excluded_postal_code(
)
+def test_checkout_shipping_method_update_shipping_zone_without_channel(
+ staff_api_client,
+ shipping_method,
+ checkout_with_item,
+ address,
+):
+ shipping_method.shipping_zone.channels.clear()
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.save(update_fields=["shipping_address"])
+ query = MUTATION_UPDATE_SHIPPING_METHOD
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ method_id = graphene.Node.to_global_id("ShippingMethod", shipping_method.id)
+
+ response = staff_api_client.post_graphql(
+ query, {"checkoutId": checkout_id, "shippingMethodId": method_id}
+ )
+ data = get_graphql_content(response)["data"]["checkoutShippingMethodUpdate"]
+
+ checkout.refresh_from_db()
+
+ errors = data["checkoutErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "shippingMethod"
+ assert errors[0]["code"] == CheckoutErrorCode.SHIPPING_METHOD_NOT_APPLICABLE.name
+ assert checkout.shipping_method is None
+
+
+def test_checkout_shipping_method_update_shipping_zone_with_channel(
+ staff_api_client,
+ shipping_method,
+ checkout_with_item,
+ address,
+):
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.save(update_fields=["shipping_address"])
+ query = MUTATION_UPDATE_SHIPPING_METHOD
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ method_id = graphene.Node.to_global_id("ShippingMethod", shipping_method.id)
+
+ response = staff_api_client.post_graphql(
+ query, {"checkoutId": checkout_id, "shippingMethodId": method_id}
+ )
+ data = get_graphql_content(response)["data"]["checkoutShippingMethodUpdate"]
+
+ checkout.refresh_from_db()
+
+ checkout.refresh_from_db()
+ errors = data["checkoutErrors"]
+ assert not errors
+ assert data["checkout"]["id"] == checkout_id
+
+ assert checkout.shipping_method == shipping_method
+
+
def test_query_checkout_line(checkout_with_item, user_api_client):
query = """
query checkoutLine($id: ID) {
diff --git a/saleor/graphql/checkout/tests/test_checkout_digital.py b/saleor/graphql/checkout/tests/test_checkout_digital.py
index 26861daa3f..81c6f90bcf 100644
--- a/saleor/graphql/checkout/tests/test_checkout_digital.py
+++ b/saleor/graphql/checkout/tests/test_checkout_digital.py
@@ -143,8 +143,12 @@ def test_checkout_update_shipping_method(
content = get_graphql_content(response)
data = content["data"]["checkoutShippingMethodUpdate"]
- assert data["errors"] == [
- {"field": "shippingMethod", "message": "This checkout doesn't need shipping"}
+ assert data["checkoutErrors"] == [
+ {
+ "field": "shippingMethod",
+ "message": "This checkout doesn't need shipping",
+ "code": CheckoutErrorCode.SHIPPING_NOT_REQUIRED.name,
+ }
]
# Ensure the shipping method was unchanged