Skip to content

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

Adding test for checkout: cannot purchase product with no payment CORE_0113 (#13551)

Renatad64d8a456221
diff --git a/saleor/tests/e2e/checkout/test_checkout_purchase_with_no_payment.py b/saleor/tests/e2e/checkout/test_checkout_purchase_with_no_payment.py
new file mode 100644
index 0000000000..ccb8797080
--- /dev/null
+++ b/saleor/tests/e2e/checkout/test_checkout_purchase_with_no_payment.py
@@ -0,0 +1,165 @@
+import pytest
+
+from ..channel.utils import create_channel
+from ..product.utils import (
+ create_category,
+ create_product,
+ create_product_channel_listing,
+ create_product_type,
+ create_product_variant,
+ create_product_variant_channel_listing,
+)
+from ..shipping_zone.utils import (
+ create_shipping_method,
+ create_shipping_method_channel_listing,
+ create_shipping_zone,
+)
+from ..utils import assign_permissions
+from ..warehouse.utils import create_warehouse
+from .utils import (
+ checkout_create,
+ checkout_delivery_method_update,
+ raw_checkout_complete,
+)
+
+
+def prepare_product(
+ e2e_staff_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ channel_slug,
+):
+ permissions = [
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ ]
+ assign_permissions(e2e_staff_api_client, permissions)
+
+ warehouse_data = create_warehouse(e2e_staff_api_client)
+ warehouse_id = warehouse_data["id"]
+
+ warehouse_ids = [warehouse_id]
+ channel_data = create_channel(
+ e2e_staff_api_client,
+ slug=channel_slug,
+ warehouse_ids=warehouse_ids,
+ )
+ channel_id = channel_data["id"]
+
+ channel_ids = [channel_id]
+ shipping_zone_data = create_shipping_zone(
+ e2e_staff_api_client,
+ warehouse_ids=warehouse_ids,
+ channel_ids=channel_ids,
+ )
+ shipping_zone_id = shipping_zone_data["id"]
+
+ shipping_method_data = create_shipping_method(
+ e2e_staff_api_client, shipping_zone_id
+ )
+ shipping_method_id = shipping_method_data["id"]
+
+ create_shipping_method_channel_listing(
+ e2e_staff_api_client, shipping_method_id, channel_id
+ )
+
+ product_type_data = create_product_type(
+ e2e_staff_api_client,
+ )
+ product_type_id = product_type_data["id"]
+
+ category_data = create_category(e2e_staff_api_client)
+ category_id = category_data["id"]
+
+ product_data = create_product(e2e_staff_api_client, product_type_id, category_id)
+ product_id = product_data["id"]
+
+ create_product_channel_listing(e2e_staff_api_client, product_id, channel_id)
+
+ stocks = [
+ {
+ "warehouse": warehouse_id,
+ "quantity": 5,
+ }
+ ]
+ product_variant_data = create_product_variant(
+ e2e_staff_api_client,
+ product_id,
+ stocks=stocks,
+ )
+ product_variant_id = product_variant_data["id"]
+
+ create_product_variant_channel_listing(
+ e2e_staff_api_client,
+ product_variant_id,
+ channel_id,
+ )
+
+ return product_variant_id
+
+
+def test_should_not_be_able_to_make_purchase_with_no_payment(
+ e2e_staff_api_client,
+ e2e_not_logged_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+):
+ # Before
+ channel_slug = "test-channel"
+ product_variant_id = prepare_product(
+ e2e_staff_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_shipping,
+ permission_manage_product_types_and_attributes,
+ channel_slug,
+ )
+
+ # Step 1 - Create checkout.
+ lines = [
+ {"variantId": product_variant_id, "quantity": 1},
+ ]
+ checkout_data = checkout_create(
+ e2e_not_logged_api_client,
+ lines,
+ channel_slug,
+ set_default_billing_address=True,
+ set_default_shipping_address=True,
+ )
+ checkout_id = checkout_data["id"]
+
+ assert checkout_data["isShippingRequired"] is True
+ assert checkout_data["deliveryMethod"] is None
+ assert checkout_data["shippingMethod"] is None
+ shipping_method_id = checkout_data["shippingMethods"][0]["id"]
+
+ # Step 2 - Set shipping address and DeliveryMethod for checkout
+
+ checkout_data = checkout_delivery_method_update(
+ e2e_not_logged_api_client,
+ checkout_id,
+ shipping_method_id,
+ )
+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id
+
+ # Step 3 - Unable to complete checkout without payment
+ order_data = raw_checkout_complete(e2e_not_logged_api_client, checkout_id)
+ assert order_data["order"] is None
+ errors = order_data["errors"]
+ assert errors == [
+ {
+ "code": "CHECKOUT_NOT_FULLY_PAID",
+ "field": None,
+ "message": (
+ "Provided payment methods can not cover the checkout's total amount"
+ ),
+ }
+ ]
diff --git a/saleor/tests/e2e/checkout/utils/__init__.py b/saleor/tests/e2e/checkout/utils/__init__.py
index a241841ae0..baf27db28e 100644
--- a/saleor/tests/e2e/checkout/utils/__init__.py
+++ b/saleor/tests/e2e/checkout/utils/__init__.py
@@ -2,7 +2,7 @@ from ...orders.utils.draft_order import draft_order_create
from ...orders.utils.order_lines import order_lines_create
from ...product.utils.product_channel_listing import raw_create_product_channel_listing
from .checkout_billing_address_update import checkout_billing_address_update
-from .checkout_complete import checkout_complete
+from .checkout_complete import checkout_complete, raw_checkout_complete
from .checkout_create import checkout_create, raw_checkout_create
from .checkout_create_from_order import checkout_create_from_order
from .checkout_delivery_method_update import checkout_delivery_method_update
@@ -17,6 +17,7 @@ __all__ = [
"checkout_billing_address_update",
"raw_create_product_channel_listing",
"checkout_complete",
+ "raw_checkout_complete",
"checkout_create",
"raw_checkout_create",
"checkout_delivery_method_update",
diff --git a/saleor/tests/e2e/checkout/utils/checkout_complete.py b/saleor/tests/e2e/checkout/utils/checkout_complete.py
index 6a0aec910f..6a094e1854 100644
--- a/saleor/tests/e2e/checkout/utils/checkout_complete.py
+++ b/saleor/tests/e2e/checkout/utils/checkout_complete.py
@@ -35,7 +35,7 @@ mutation CheckoutComplete($checkoutId: ID!) {
"""
-def checkout_complete(api_client, checkout_id):
+def raw_checkout_complete(api_client, checkout_id):
variables = {
"checkoutId": checkout_id,
}
@@ -45,9 +45,17 @@ def checkout_complete(api_client, checkout_id):
)
content = get_graphql_content(response)
- assert content["data"]["checkoutComplete"]["errors"] == []
+ raw_data = content["data"]["checkoutComplete"]
+
+ return raw_data
+
+
+def checkout_complete(api_client, checkout_id):
+ checkout_response = raw_checkout_complete(api_client, checkout_id)
+
+ assert checkout_response["errors"] == []
- data = content["data"]["checkoutComplete"]["order"]
+ data = checkout_response["order"]
assert data["id"] is not None
assert data["checkoutId"] == checkout_id