Skip to content

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

[TEST] Cancel unpaid/partially paid/fully paid orders CORE_0204, CORE_0206, CORE_0207 (#13958)

Renata9f22fa7dae14
diff --git a/saleor/tests/e2e/orders/test_cancel_fully_paid_order.py b/saleor/tests/e2e/orders/test_cancel_fully_paid_order.py
new file mode 100644
index 0000000000..edd9e1d872
--- /dev/null
+++ b/saleor/tests/e2e/orders/test_cancel_fully_paid_order.py
@@ -0,0 +1,121 @@
+import pytest
+
+from .. import DEFAULT_ADDRESS
+from ..product.utils.preparing_product import prepare_product
+from ..shop.utils import prepare_shop
+from ..transactions.utils import create_transaction
+from ..utils import assign_permissions
+from .utils import (
+ draft_order_complete,
+ draft_order_create,
+ draft_order_update,
+ order_cancel,
+ order_lines_create,
+ order_query,
+)
+
+
+def test_cancel_fully_paid_order_CORE_0206(
+ e2e_staff_api_client,
+ e2e_app_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_product_types_and_attributes,
+ permission_manage_shipping,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ # Before
+ permissions = [
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_orders,
+ permission_manage_payments,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+ app_permissions = [permission_manage_payments, permission_manage_orders]
+ assign_permissions(e2e_app_api_client, app_permissions)
+
+ price = 10
+
+ (
+ warehouse_id,
+ channel_id,
+ _channel_slug,
+ shipping_method_id,
+ ) = prepare_shop(e2e_staff_api_client)
+
+ (
+ _product_id,
+ product_variant_id,
+ _product_variant_price,
+ ) = prepare_product(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ price,
+ )
+
+ # Step 1 - Create draft order
+ draft_order_input = {
+ "channelId": channel_id,
+ "userEmail": "[email protected]",
+ "shippingAddress": DEFAULT_ADDRESS,
+ "billingAddress": DEFAULT_ADDRESS,
+ }
+ data = draft_order_create(
+ e2e_staff_api_client,
+ draft_order_input,
+ )
+ order_id = data["order"]["id"]
+ assert order_id is not None
+
+ # Step 2 - Add lines to the order
+ lines = [{"variantId": product_variant_id, "quantity": 1}]
+ order_lines = order_lines_create(e2e_staff_api_client, order_id, lines)
+ order_product_variant_id = order_lines["order"]["lines"][0]["variant"]["id"]
+ assert order_product_variant_id == product_variant_id
+
+ # Step 3 - Update order's shipping method
+ input = {"shippingMethod": shipping_method_id}
+ draft_order = draft_order_update(e2e_staff_api_client, order_id, input)
+ order_shipping_id = draft_order["order"]["deliveryMethod"]["id"]
+ assert order_shipping_id is not None
+
+ # Step 4 - Complete the order
+ order = draft_order_complete(e2e_staff_api_client, order_id)
+ order_complete_id = order["order"]["id"]
+ order_total = order["order"]["total"]["gross"]["amount"]
+ assert order_complete_id == order_id
+ order_line = order["order"]["lines"][0]
+ assert order_line["productVariantId"] == product_variant_id
+ assert order["order"]["status"] == "UNFULFILLED"
+
+ # Step 5 - Create a full payment for the order
+ create_transaction(
+ e2e_app_api_client,
+ order_id,
+ transaction_name="CreditCard",
+ message="Charged",
+ psp_reference="PSP-ref123",
+ available_actions=["REFUND", "CANCEL"],
+ amount=order_total,
+ )
+
+ order = order_query(e2e_staff_api_client, order_id)
+ assert order["paymentStatus"] == "FULLY_CHARGED"
+ assert order["status"] == "UNFULFILLED"
+
+ # Step 6 - Cancel the order
+ cancelled_order = order_cancel(e2e_staff_api_client, order_id)
+ assert cancelled_order["order"]["id"] == order_id
+ assert cancelled_order["order"]["paymentStatus"] == "FULLY_CHARGED"
+ assert cancelled_order["order"]["status"] == "CANCELED"
+ assert cancelled_order["order"]["isPaid"] is True
+ cancelled_order_total = cancelled_order["order"]["total"]["gross"]["amount"]
+ assert cancelled_order_total == order_total
+ remaining_total = cancelled_order["order"]["totalBalance"]["amount"]
+ assert remaining_total == 0
diff --git a/saleor/tests/e2e/orders/test_cancel_partially_paid_order.py b/saleor/tests/e2e/orders/test_cancel_partially_paid_order.py
new file mode 100644
index 0000000000..87d926743d
--- /dev/null
+++ b/saleor/tests/e2e/orders/test_cancel_partially_paid_order.py
@@ -0,0 +1,130 @@
+import pytest
+
+from .. import DEFAULT_ADDRESS
+from ..product.utils.preparing_product import prepare_product
+from ..shop.utils import prepare_shop
+from ..transactions.utils import create_transaction
+from ..utils import assign_permissions
+from .utils import (
+ draft_order_complete,
+ draft_order_create,
+ draft_order_update,
+ order_cancel,
+ order_lines_create,
+ order_query,
+)
+
+
+def test_cancel_partially_paid_order_CORE_0207(
+ e2e_staff_api_client,
+ e2e_app_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_product_types_and_attributes,
+ permission_manage_shipping,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ # Before
+ permissions = [
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_orders,
+ permission_manage_payments,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+ app_permissions = [permission_manage_payments, permission_manage_orders]
+ assign_permissions(e2e_app_api_client, app_permissions)
+
+ price = 10
+
+ (
+ warehouse_id,
+ channel_id,
+ _channel_slug,
+ shipping_method_id,
+ ) = prepare_shop(e2e_staff_api_client)
+
+ (
+ _product_id,
+ product_variant_id,
+ _product_variant_price,
+ ) = prepare_product(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ price,
+ )
+
+ # Step 1 - Create draft order
+ draft_order_input = {
+ "channelId": channel_id,
+ "userEmail": "[email protected]",
+ "shippingAddress": DEFAULT_ADDRESS,
+ "billingAddress": DEFAULT_ADDRESS,
+ }
+ data = draft_order_create(
+ e2e_staff_api_client,
+ draft_order_input,
+ )
+ order_id = data["order"]["id"]
+ assert order_id is not None
+
+ # Step 2 - Add lines to the order
+ lines = [{"variantId": product_variant_id, "quantity": 1}]
+ order_lines = order_lines_create(
+ e2e_staff_api_client,
+ order_id,
+ lines,
+ )
+ order_product_variant_id = order_lines["order"]["lines"][0]["variant"]["id"]
+ assert order_product_variant_id == product_variant_id
+
+ # Step 3 - Update order's shipping method
+ input = {"shippingMethod": shipping_method_id}
+ draft_order = draft_order_update(
+ e2e_staff_api_client,
+ order_id,
+ input,
+ )
+ order_shipping_id = draft_order["order"]["deliveryMethod"]["id"]
+ assert order_shipping_id is not None
+
+ # Step 4 - Complete the order
+ order = draft_order_complete(e2e_staff_api_client, order_id)
+ order_complete_id = order["order"]["id"]
+ order_total = order["order"]["total"]["gross"]["amount"]
+ assert order_complete_id == order_id
+ order_line = order["order"]["lines"][0]
+ assert order_line["productVariantId"] == product_variant_id
+ assert order["order"]["status"] == "UNFULFILLED"
+
+ # Step 5 - Create a partial payment for the order
+ partial_amount = 2
+
+ create_transaction(
+ e2e_app_api_client,
+ order_id,
+ transaction_name="CreditCard",
+ message="Charged",
+ psp_reference="PSP-ref123",
+ available_actions=["REFUND", "CANCEL"],
+ amount=partial_amount,
+ )
+
+ order = order_query(e2e_staff_api_client, order_id)
+ assert order["paymentStatus"] == "PARTIALLY_CHARGED"
+ assert order["status"] == "UNFULFILLED"
+
+ # Step 6 - Cancel the order
+ cancelled_order = order_cancel(e2e_staff_api_client, order_id)
+ assert cancelled_order["order"]["id"] == order_id
+ assert cancelled_order["order"]["paymentStatus"] == "PARTIALLY_CHARGED"
+ assert cancelled_order["order"]["status"] == "CANCELED"
+ assert cancelled_order["order"]["isPaid"] is False
+ remaining_total = cancelled_order["order"]["totalBalance"]["amount"]
+ absolute_remaining_total = abs(remaining_total)
+ assert absolute_remaining_total == order_total - partial_amount
diff --git a/saleor/tests/e2e/orders/test_cancel_unpaid_order.py b/saleor/tests/e2e/orders/test_cancel_unpaid_order.py
new file mode 100644
index 0000000000..21c3c17763
--- /dev/null
+++ b/saleor/tests/e2e/orders/test_cancel_unpaid_order.py
@@ -0,0 +1,101 @@
+import pytest
+
+from .. import DEFAULT_ADDRESS
+from ..product.utils.preparing_product import prepare_product
+from ..shop.utils import prepare_shop
+from ..utils import assign_permissions
+from .utils import (
+ draft_order_complete,
+ draft_order_create,
+ draft_order_update,
+ order_cancel,
+ order_lines_create,
+)
+
+
+def test_cancel_unpaid_order_CORE_0204(
+ e2e_staff_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_product_types_and_attributes,
+ permission_manage_shipping,
+ permission_manage_orders,
+):
+ # Before
+ permissions = [
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ permission_manage_orders,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ price = 10
+
+ (
+ warehouse_id,
+ channel_id,
+ _channel_slug,
+ shipping_method_id,
+ ) = prepare_shop(e2e_staff_api_client)
+
+ (
+ _product_id,
+ product_variant_id,
+ _product_variant_price,
+ ) = prepare_product(
+ e2e_staff_api_client,
+ warehouse_id,
+ channel_id,
+ price,
+ )
+
+ # Step 1 - Create draft order
+ draft_order_input = {
+ "channelId": channel_id,
+ "userEmail": "[email protected]",
+ "shippingAddress": DEFAULT_ADDRESS,
+ "billingAddress": DEFAULT_ADDRESS,
+ }
+ data = draft_order_create(
+ e2e_staff_api_client,
+ draft_order_input,
+ )
+ order_id = data["order"]["id"]
+ assert order_id is not None
+
+ # Step 2 - Add lines to the order
+ lines = [{"variantId": product_variant_id, "quantity": 1}]
+ order_lines = order_lines_create(
+ e2e_staff_api_client,
+ order_id,
+ lines,
+ )
+ order_product_variant_id = order_lines["order"]["lines"][0]["variant"]["id"]
+ assert order_product_variant_id == product_variant_id
+
+ # Step 3 - Update order's shipping method
+ input = {"shippingMethod": shipping_method_id}
+ draft_order = draft_order_update(
+ e2e_staff_api_client,
+ order_id,
+ input,
+ )
+ order_shipping_id = draft_order["order"]["deliveryMethod"]["id"]
+ assert order_shipping_id is not None
+
+ # Step 4 - Complete the order
+ order = draft_order_complete(e2e_staff_api_client, order_id)
+ order_complete_id = order["order"]["id"]
+ assert order_complete_id == order_id
+ order_line = order["order"]["lines"][0]
+ assert order_line["productVariantId"] == product_variant_id
+ assert order["order"]["status"] == "UNFULFILLED"
+
+ # Step 5 - Cancel the order
+ cancelled_order = order_cancel(e2e_staff_api_client, order_id)
+ assert cancelled_order["order"]["id"] == order_id
+ assert cancelled_order["order"]["paymentStatus"] == "NOT_CHARGED"
+ assert cancelled_order["order"]["status"] == "CANCELED"
diff --git a/saleor/tests/e2e/orders/utils/__init__.py b/saleor/tests/e2e/orders/utils/__init__.py
index 4dd03ef5f7..2543e3b358 100644
--- a/saleor/tests/e2e/orders/utils/__init__.py
+++ b/saleor/tests/e2e/orders/utils/__init__.py
@@ -2,6 +2,7 @@ from .draft_order_complete import draft_order_complete, raw_draft_order_complete
from .draft_order_create import draft_order_create
from .draft_order_delete import draft_order_delete
from .draft_order_update import draft_order_update
+from .order_cancel import order_cancel
from .order_lines_create import order_lines_create
from .order_mark_as_paid import mark_order_paid
from .order_query import order_query
@@ -14,5 +15,6 @@ __all__ = [
"draft_order_update",
"order_query",
"mark_order_paid",
+ "order_cancel",
"draft_order_delete",
]
diff --git a/saleor/tests/e2e/orders/utils/order_cancel.py b/saleor/tests/e2e/orders/utils/order_cancel.py
new file mode 100644
index 0000000000..072a769cc4
--- /dev/null
+++ b/saleor/tests/e2e/orders/utils/order_cancel.py
@@ -0,0 +1,42 @@
+from saleor.graphql.tests.utils import get_graphql_content
+
+ORDER_CANCEL_MUTATION = """
+mutation OrderCancel($id: ID!) {
+ orderCancel(id: $id) {
+ errors {
+ message
+ field
+ }
+ order {
+ id
+ status
+ paymentStatus
+ isPaid
+ totalBalance { amount }
+ total {
+ gross {
+ amount
+ }
+ }
+ }
+ }
+}
+"""
+
+
+def order_cancel(api_client, id):
+ variables = {"id": id}
+
+ response = api_client.post_graphql(
+ ORDER_CANCEL_MUTATION,
+ variables=variables,
+ )
+ content = get_graphql_content(response)
+
+ data = content["data"]["orderCancel"]
+
+ errors = data["errors"]
+
+ assert errors == []
+
+ return data
diff --git a/saleor/tests/e2e/orders/utils/order_query.py b/saleor/tests/e2e/orders/utils/order_query.py
index d2bff3a69d..c3a4e05878 100644
--- a/saleor/tests/e2e/orders/utils/order_query.py
+++ b/saleor/tests/e2e/orders/utils/order_query.py
@@ -39,6 +39,8 @@ query OrderDetails($id:ID!) {
streetAddress2
}
statusDisplay
+ status
+ paymentStatus
}
}
"""
diff --git a/saleor/tests/e2e/transactions/utils/__init__.py b/saleor/tests/e2e/transactions/utils/__init__.py
new file mode 100644
index 0000000000..ffa031dd30
--- /dev/null
+++ b/saleor/tests/e2e/transactions/utils/__init__.py
@@ -0,0 +1,5 @@
+from .transaction_create import create_transaction
+
+__all__ = [
+ "create_transaction",
+]
diff --git a/saleor/tests/e2e/transactions/utils.py b/saleor/tests/e2e/transactions/utils/transaction_create.py
similarity index 96%
rename from saleor/tests/e2e/transactions/utils.py
rename to saleor/tests/e2e/transactions/utils/transaction_create.py
index edcf16cb19..9b1faf1efc 100644
--- a/saleor/tests/e2e/transactions/utils.py
+++ b/saleor/tests/e2e/transactions/utils/transaction_create.py
@@ -1,4 +1,4 @@
-from ..utils import get_graphql_content
+from ...utils import get_graphql_content
TRANSACTION_CREATE_MUTATION = """
mutation TransactionCreate($id: ID!, $transactionCreateInput: TransactionCreateInput!) {
@@ -11,6 +11,7 @@ mutation TransactionCreate($id: ID!, $transactionCreateInput: TransactionCreateI
transaction {
id
name
+ order { id }
message
pspReference
actions