Skip to content

Commit

Permalink
Errors handling fix for orderFulfillApprove mutation (#9491)
Browse files Browse the repository at this point in the history
* errors handling fix for orderFulfillApprove mutation

* ignore errors order in fulfillment approve tests

* get product names from instance in fulfillment approve tests
  • Loading branch information
szdrasiak committed Apr 11, 2022
1 parent 65d4c83 commit c622023
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
24 changes: 15 additions & 9 deletions saleor/graphql/order/mutations/fulfillments.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,21 @@ def perform_mutation(cls, _root, info, **data):
cls.clean_input(info, fulfillment)

order = fulfillment.order
fulfillment = approve_fulfillment(
fulfillment,
info.context.user,
info.context.app,
info.context.plugins,
info.context.site.settings,
notify_customer=data["notify_customer"],
allow_stock_to_be_exceeded=data.get("allow_stock_to_be_exceeded"),
)

try:
fulfillment = approve_fulfillment(
fulfillment,
info.context.user,
info.context.app,
info.context.plugins,
info.context.site.settings,
notify_customer=data["notify_customer"],
allow_stock_to_be_exceeded=data.get("allow_stock_to_be_exceeded"),
)
except InsufficientStock as exc:
errors = prepare_insufficient_stock_order_validation_errors(exc)
raise ValidationError({"stocks": errors})

order.refresh_from_db(fields=["status"])
return FulfillmentApprove(fulfillment=fulfillment, order=order)

Expand Down
53 changes: 42 additions & 11 deletions saleor/graphql/order/tests/test_fulfillment.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ def test_order_fulfill_with_stock_exceeded_with_flag_disabled(

errors = data["errors"]
assert errors[0]["code"] == "INSUFFICIENT_STOCK"
assert errors[0]["message"] == "Insufficient product stock: SKU_AA"
assert errors[0]["message"] == f"Insufficient product stock: {order_line}"

assert errors[1]["code"] == "INSUFFICIENT_STOCK"
assert errors[1]["message"] == "Insufficient product stock: SKU_B"
assert errors[1]["message"] == f"Insufficient product stock: {order_line2}"


def test_order_fulfill_with_stock_exceeded_with_flag_enabled(
Expand Down Expand Up @@ -1714,6 +1714,7 @@ def test_create_digital_fulfillment(
errors {
field
code
message
}
}
}
Expand Down Expand Up @@ -1811,13 +1812,26 @@ def test_fulfillment_approve_delete_products_before_approval_allow_stock_exceede
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response, ignore_errors=True)
errors = content["errors"]
assert len(errors) == 1
assert (
errors[0]["message"]
== "Insufficient stock for Test product (SKU_AA), Test product 2 (SKU_B)"
)

content = get_graphql_content(response)
errors = content["data"]["orderFulfillmentApprove"]["errors"]

assert len(errors) == 2

error_field_and_code = {
"field": "stocks",
"code": "INSUFFICIENT_STOCK",
}
expected_errors = [
{
**error_field_and_code,
"message": f"Insufficient product stock: {line.order_line}",
}
for line in fulfillment.lines.all()
]

for expected_error in expected_errors:
assert expected_error in errors

fulfillment.refresh_from_db()
assert fulfillment.status == FulfillmentStatus.WAITING_FOR_APPROVAL
Expand Down Expand Up @@ -1965,8 +1979,25 @@ def test_fulfillment_approve_when_stock_is_exceeded_and_flag_disabled(
query, variables, permissions=[permission_manage_orders]
)
content = get_graphql_content(response, ignore_errors=True)
assert content["errors"]
assert content["errors"][0]["message"] == "Insufficient stock for SKU_AA, SKU_B"
errors = content["data"]["orderFulfillmentApprove"]["errors"]

assert len(errors) == 2

error_field_and_code = {
"field": "stocks",
"code": "INSUFFICIENT_STOCK",
}

expected_errors = [
{
**error_field_and_code,
"message": f"Insufficient product stock: {line.order_line}",
}
for line in fulfillment.lines.all()
]

for expected_error in expected_errors:
assert expected_error in errors


@patch("saleor.order.actions.send_fulfillment_confirmation_to_customer", autospec=True)
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/order/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def prepare_insufficient_stock_order_validation_errors(exc):
)
errors.append(
ValidationError(
f"Insufficient product stock: {item.variant}",
f"Insufficient product stock: {item.order_line or item.variant}",
code=OrderErrorCode.INSUFFICIENT_STOCK,
params={
"order_lines": [order_line_global_id]
Expand Down

0 comments on commit c622023

Please sign in to comment.