Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Apply fixes for order grant refund mutations (#14674)
Maciej Korycinski14c47ff9c145
diff --git a/saleor/graphql/order/mutations/order_grant_refund_create.py b/saleor/graphql/order/mutations/order_grant_refund_create.pyindex e23dd2b2fc..f1c771e715 100644--- a/saleor/graphql/order/mutations/order_grant_refund_create.py+++ b/saleor/graphql/order/mutations/order_grant_refund_create.py@@ -183,7 +183,7 @@ class OrderGrantRefundCreate(BaseMutation):input: dict[str, Any],):amount = input.get("amount")- reason = input.get("reason", "")+ reason = input.get("reason") or ""input_lines = input.get("lines", [])grant_refund_for_shipping = input.get("grant_refund_for_shipping", None)diff --git a/saleor/graphql/order/mutations/order_grant_refund_update.py b/saleor/graphql/order/mutations/order_grant_refund_update.pyindex 0881a4a514..24ce07ed10 100644--- a/saleor/graphql/order/mutations/order_grant_refund_update.py+++ b/saleor/graphql/order/mutations/order_grant_refund_update.py@@ -126,13 +126,13 @@ class OrderGrantRefundUpdate(BaseMutation):@classmethoddef validate_input(cls, input: dict[str, Any]):amount = input.get("amount")- reason = input.get("reason", "")+ reason = input.get("reason")input_lines = input.get("add_lines", [])remove_lines = input.get("remove_lines", [])grant_refund_for_shipping = input.get("grant_refund_for_shipping", False)if (- not amount- and not reason+ amount is None+ and reason is Noneand not input_linesand not grant_refund_for_shippingand not remove_linesdiff --git a/saleor/graphql/order/mutations/order_grant_refund_utils.py b/saleor/graphql/order/mutations/order_grant_refund_utils.pyindex c9799433b6..dad9f306bb 100644--- a/saleor/graphql/order/mutations/order_grant_refund_utils.py+++ b/saleor/graphql/order/mutations/order_grant_refund_utils.py@@ -26,11 +26,14 @@ def handle_lines_with_quantity_already_refunded(all_granted_refund_lines = models.OrderGrantedRefundLine.objects.filter(granted_refund_id__in=all_granted_refund_ids)+ lines_to_process = all_granted_refund_linesif granted_refund_lines_to_exclude:- all_granted_refund_lines.exclude(pk__in=granted_refund_lines_to_exclude)+ lines_to_process = all_granted_refund_lines.exclude(+ pk__in=granted_refund_lines_to_exclude+ )lines_with_quantity_already_refunded: dict[uuid.UUID, int] = defaultdict(int)- for line in all_granted_refund_lines:+ for line in lines_to_process:lines_with_quantity_already_refunded[line.order_line_id] += line.quantityfor granted_refund_line in input_lines_data.values():diff --git a/saleor/graphql/order/tests/mutations/test_order_grant_refund_create.py b/saleor/graphql/order/tests/mutations/test_order_grant_refund_create.pyindex 49f90b9a6f..de23c8e5bd 100644--- a/saleor/graphql/order/tests/mutations/test_order_grant_refund_create.py+++ b/saleor/graphql/order/tests/mutations/test_order_grant_refund_create.py@@ -1,6 +1,7 @@from decimal import Decimalimport graphene+import pytestfrom .....core.prices import quantize_pricefrom .....order.utils import update_order_charge_data@@ -84,12 +85,14 @@ mutation OrderGrantRefundCreate("""-def test_grant_refund_by_user(staff_api_client, permission_manage_orders, order):[email protected]("reason", ["", "Reason", None])+def test_grant_refund_by_user(+ reason, staff_api_client, permission_manage_orders, order+):# givenorder_id = to_global_id_or_none(order)staff_api_client.user.user_permissions.add(permission_manage_orders)amount = Decimal("10.00")- reason = "Granted refund reason."variables = {"id": order_id,"input": {"amount": amount, "reason": reason},@@ -114,6 +117,7 @@ def test_grant_refund_by_user(staff_api_client, permission_manage_orders, order)== granted_refund_from_db.amount_value== amount)+ reason = reason or ""assert (granted_refund_assigned_to_order["reason"]== reason@@ -127,12 +131,12 @@ def test_grant_refund_by_user(staff_api_client, permission_manage_orders, order)assert not granted_refund_assigned_to_order["app"]-def test_grant_refund_by_app(app_api_client, permission_manage_orders, order):[email protected]("reason", ["", "Reason", None])+def test_grant_refund_by_app(reason, app_api_client, permission_manage_orders, order):# givenorder_id = to_global_id_or_none(order)app_api_client.app.permissions.set([permission_manage_orders])amount = Decimal("10.00")- reason = "Granted refund reason."variables = {"id": order_id,"input": {"amount": amount, "reason": reason},@@ -157,6 +161,7 @@ def test_grant_refund_by_app(app_api_client, permission_manage_orders, order):== amount== granted_refund_from_db.amount_value)+ reason = reason or ""assert granted_refund["reason"] == reason == granted_refund_from_db.reasonassert not granted_refund["user"]assert (diff --git a/saleor/graphql/order/tests/mutations/test_order_grant_refund_update.py b/saleor/graphql/order/tests/mutations/test_order_grant_refund_update.pyindex 4108576110..b0d9051231 100644--- a/saleor/graphql/order/tests/mutations/test_order_grant_refund_update.py+++ b/saleor/graphql/order/tests/mutations/test_order_grant_refund_update.py@@ -1,5 +1,7 @@from decimal import Decimal+import pytest+from .....core.prices import quantize_pricefrom .....order.utils import update_order_charge_datafrom ....core.utils import to_global_id_or_none@@ -142,8 +144,9 @@ def test_grant_refund_update_by_user()[email protected]("amount", [Decimal("0.00"), Decimal("20.00")])def test_grant_refund_update_only_amount_by_user(- staff_api_client, app, permission_manage_orders, order+ amount, staff_api_client, app, permission_manage_orders, order):# givencurrent_reason = "Granted refund reason."@@ -156,7 +159,6 @@ def test_grant_refund_update_only_amount_by_user()granted_refund_id = to_global_id_or_none(granted_refund)staff_api_client.user.user_permissions.add(permission_manage_orders)- amount = Decimal("20.00")variables = {"id": granted_refund_id,"input": {"amount": amount},@@ -182,8 +184,9 @@ def test_grant_refund_update_only_amount_by_user(assert granted_refund.reason == current_reason[email protected]("reason", ["", "new reason"])def test_grant_refund_update_only_reason_by_user(- staff_api_client, app, permission_manage_orders, order+ reason, staff_api_client, app, permission_manage_orders, order):# givencurrent_reason = "Granted refund reason."@@ -196,7 +199,6 @@ def test_grant_refund_update_only_reason_by_user()granted_refund_id = to_global_id_or_none(granted_refund)staff_api_client.user.user_permissions.add(permission_manage_orders)- reason = "new reason"variables = {"id": granted_refund_id,"input": {"reason": reason},@@ -324,8 +326,9 @@ def test_grant_refund_update_by_app()[email protected]("amount", [Decimal("0.00"), Decimal("20.00")])def test_grant_refund_update_only_amount_by_app(- app_api_client, staff_user, permission_manage_orders, order+ amount, app_api_client, staff_user, permission_manage_orders, order):# givencurrent_reason = "Granted refund reason."@@ -338,7 +341,6 @@ def test_grant_refund_update_only_amount_by_app()granted_refund_id = to_global_id_or_none(granted_refund)app_api_client.app.permissions.set([permission_manage_orders])- amount = Decimal("20.00")variables = {"id": granted_refund_id,"input": {"amount": amount},@@ -367,8 +369,9 @@ def test_grant_refund_update_only_amount_by_app()[email protected]("reason", ["", "new reason"])def test_grant_refund_update_only_reason_by_app(- app_api_client, staff_user, permission_manage_orders, order+ reason, app_api_client, staff_user, permission_manage_orders, order):# givencurrent_reason = "Granted refund reason."@@ -382,7 +385,6 @@ def test_grant_refund_update_only_reason_by_app(granted_refund_id = to_global_id_or_none(granted_refund)app_api_client.app.permissions.set([permission_manage_orders])- reason = "new reason"variables = {"id": granted_refund_id,"input": {"reason": reason},@@ -683,6 +685,74 @@ def test_grant_refund_update_with_add_and_remove_lines()+def test_grant_refund_update_with_same_line_in_add_and_remove(+ app_api_client, staff_user, permission_manage_orders, order_with_lines+):+ # given+ current_reason = "Granted refund reason."+ current_amount = Decimal("10.00")+ granted_refund = order_with_lines.granted_refunds.create(+ amount_value=current_amount,+ currency=order_with_lines.currency,+ reason=current_reason,+ user=staff_user,+ )+ last_order_line = order_with_lines.lines.last()+ last_order_line.quantity = 2+ last_order_line.save()+ granted_refund_line_to_remove = granted_refund.lines.create(+ order_line=last_order_line, quantity=1+ )++ granted_refund_id = to_global_id_or_none(granted_refund)+ app_api_client.app.permissions.set([permission_manage_orders])++ expected_quantity = 2++ variables = {+ "id": granted_refund_id,+ "input": {+ "addLines": [+ {+ "id": to_global_id_or_none(last_order_line),+ "quantity": expected_quantity,+ }+ ],+ "removeLines": [to_global_id_or_none(granted_refund_line_to_remove)],+ },+ }++ # when+ response = app_api_client.post_graphql(ORDER_GRANT_REFUND_UPDATE, variables)++ # then+ granted_refund.refresh_from_db()+ content = get_graphql_content(response)+ data = content["data"]["orderGrantRefundUpdate"]+ assert not data["errors"]+ assert len(data["order"]["grantedRefunds"]) == 1+ granted_refund_data = data["order"]["grantedRefunds"][0]+ assert len(granted_refund_data["lines"]) == 1+ assert granted_refund_data["lines"][0]["orderLine"]["id"] == to_global_id_or_none(+ last_order_line+ )++ assert len(granted_refund.lines.all()) == 1+ granted_refund_line = granted_refund.lines.first()+ assert granted_refund_line.order_line == last_order_line+ assert granted_refund_line.quantity == expected_quantity++ assert (+ granted_refund.amount_value+ == last_order_line.unit_price_gross_amount * expected_quantity+ )+ assert quantize_price(+ granted_refund.amount_value, order_with_lines.currency+ ) == quantize_price(+ Decimal(granted_refund_data["amount"]["amount"]), order_with_lines.currency+ )++def test_grant_refund_update_with_add_and_remove_lines_and_shipping_included(app_api_client, staff_user, permission_manage_orders, order_with_lines):