Skip to content

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

[TEST] adding test for checkout with exceeded product quantity limit CORE_0110 (#13460)

Renata57fb45e08853
diff --git a/saleor/tests/e2e/checkout/test_logged_customer_is_unable_to_buy_more_products_than_the_limit_per_customer.py b/saleor/tests/e2e/checkout/test_logged_customer_is_unable_to_buy_more_products_than_the_limit_per_customer.py
new file mode 100644
index 0000000000..340725e031
--- /dev/null
+++ b/saleor/tests/e2e/checkout/test_logged_customer_is_unable_to_buy_more_products_than_the_limit_per_customer.py
@@ -0,0 +1,164 @@
+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_lines_update
+
+
+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,
+ quantity_limit_per_customer=3,
+ )
+ product_variant_id = product_variant_data["id"]
+ product_variant_name = product_variant_data["name"]
+ product_variant_quantity_limit_per_customer = product_variant_data[
+ "quantityLimitPerCustomer"
+ ]
+
+ create_product_variant_channel_listing(
+ e2e_staff_api_client,
+ product_variant_id,
+ channel_id,
+ )
+
+ return (
+ product_variant_id,
+ product_variant_name,
+ product_variant_quantity_limit_per_customer,
+ )
+
+
+def test_checkout_with_product_quantity_exceeding_the_limit_per_customer_core_0110(
+ e2e_staff_api_client,
+ e2e_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,
+ product_variant_name,
+ product_variant_quantity_limit_per_customer,
+ ) = 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": 2},
+ ]
+ checkout_data = checkout_create(
+ e2e_logged_api_client,
+ lines,
+ channel_slug,
+ set_default_billing_address=True,
+ )
+ checkout_id = checkout_data["id"]
+
+ # Step 2 - Update checkout lines so quantity exceeds allowed limit per customer
+
+ updatedLines = [
+ {"variantId": product_variant_id, "quantity": 4},
+ ]
+ data = checkout_lines_update(
+ e2e_logged_api_client,
+ checkout_id,
+ updatedLines,
+ )
+ errors = data["errors"]
+
+ assert errors[0]["code"] == "QUANTITY_GREATER_THAN_LIMIT"
+ assert errors[0]["field"] == "quantity"
+ error_message = (
+ f"Cannot add more than {product_variant_quantity_limit_per_customer} "
+ f"times this item: {product_variant_name}."
+ )
+ assert errors[0]["message"] == error_message
diff --git a/saleor/tests/e2e/checkout/test_unlogged_customer_should_not_be_able_to_order_product_quantity_greater_than_stock.py b/saleor/tests/e2e/checkout/test_unlogged_customer_should_not_be_able_to_order_product_quantity_greater_than_stock.py
index 63cd927dd2..cd340ba381 100644
--- a/saleor/tests/e2e/checkout/test_unlogged_customer_should_not_be_able_to_order_product_quantity_greater_than_stock.py
+++ b/saleor/tests/e2e/checkout/test_unlogged_customer_should_not_be_able_to_order_product_quantity_greater_than_stock.py
@@ -6,8 +6,8 @@ from ..product.utils import (
create_product,
create_product_channel_listing,
create_product_type,
- create_product_variant,
create_product_variant_channel_listing,
+ raw_create_product_variant,
)
from ..shipping_zone.utils import create_shipping_zone
from ..utils import assign_permissions
@@ -69,10 +69,10 @@ def prepare_product(
"quantity": 5,
}
]
- variant_data = create_product_variant(
+ variant_data = raw_create_product_variant(
e2e_staff_api_client, product_id, stocks=stocks
)
- variant_id = variant_data["id"]
+ variant_id = variant_data["productVariant"]["id"]
create_product_variant_channel_listing(
e2e_staff_api_client,
@@ -84,7 +84,7 @@ def prepare_product(
@pytest.mark.e2e
-def test_unlogged_customer_cannot_buy_product_in_quantity_grater_than_stock(
+def test_unlogged_customer_cannot_buy_product_in_quantity_grater_than_stock_core_0107(
e2e_not_logged_api_client,
e2e_staff_api_client,
permission_manage_products,
diff --git a/saleor/tests/e2e/checkout/utils/__init__.py b/saleor/tests/e2e/checkout/utils/__init__.py
index bc19816ed3..7903f89165 100644
--- a/saleor/tests/e2e/checkout/utils/__init__.py
+++ b/saleor/tests/e2e/checkout/utils/__init__.py
@@ -3,6 +3,7 @@ from .checkout_billing_address_update import checkout_billing_address_update
from .checkout_complete import checkout_complete
from .checkout_create import checkout_create, raw_checkout_create
from .checkout_delivery_method_update import checkout_delivery_method_update
+from .checkout_lines_update import checkout_lines_update
from .checkout_payment_create import (
checkout_dummy_payment_create,
raw_checkout_dummy_payment_create,
@@ -19,4 +20,5 @@ __all__ = [
"checkout_dummy_payment_create",
"checkout_shipping_address_update",
"raw_checkout_dummy_payment_create",
+ "checkout_lines_update",
]
diff --git a/saleor/tests/e2e/checkout/utils/checkout_lines_update.py b/saleor/tests/e2e/checkout/utils/checkout_lines_update.py
new file mode 100644
index 0000000000..beecf992d8
--- /dev/null
+++ b/saleor/tests/e2e/checkout/utils/checkout_lines_update.py
@@ -0,0 +1,40 @@
+from ...utils import get_graphql_content
+
+CHECKOUT_LINES_UPDATE_MUTATION = """
+mutation checkoutLinesUpdate($checkoutId: ID!, $lines: [CheckoutLineUpdateInput!]! ){
+ checkoutLinesUpdate(lines: $lines, checkoutId: $checkoutId) {
+ checkout {
+ lines {
+ quantity
+ variant {
+ id
+ quantityLimitPerCustomer
+ }
+ }
+ }
+ errors {
+ field
+ lines
+ message
+ variants
+ code
+ }
+ }
+}
+"""
+
+
+def checkout_lines_update(
+ staff_api_client,
+ checkout_id,
+ lines,
+):
+ variables = {
+ "checkoutId": checkout_id,
+ "lines": lines,
+ }
+
+ response = staff_api_client.post_graphql(CHECKOUT_LINES_UPDATE_MUTATION, variables)
+ content = get_graphql_content(response)
+
+ return content["data"]["checkoutLinesUpdate"]
diff --git a/saleor/tests/e2e/product/utils/__init__.py b/saleor/tests/e2e/product/utils/__init__.py
index 10ddac6e08..f47d7edc8e 100644
--- a/saleor/tests/e2e/product/utils/__init__.py
+++ b/saleor/tests/e2e/product/utils/__init__.py
@@ -6,7 +6,7 @@ from .product_channel_listing import (
raw_create_product_channel_listing,
)
from .product_type import create_product_type
-from .product_variant import create_product_variant
+from .product_variant import create_product_variant, raw_create_product_variant
from .product_variant_channel_listing import create_product_variant_channel_listing
__all__ = [
@@ -17,6 +17,7 @@ __all__ = [
"create_product_channel_listing",
"create_product_variant_channel_listing",
"create_product_variant",
+ "raw_create_product_variant",
"create_product",
"raw_create_product_channel_listing",
]
diff --git a/saleor/tests/e2e/product/utils/product_variant.py b/saleor/tests/e2e/product/utils/product_variant.py
index f898f7b0aa..ff1d55bb3f 100644
--- a/saleor/tests/e2e/product/utils/product_variant.py
+++ b/saleor/tests/e2e/product/utils/product_variant.py
@@ -11,6 +11,7 @@ mutation createVariant($input: ProductVariantCreateInput!) {
productVariant {
id
name
+ quantityLimitPerCustomer
product{
id
}
@@ -20,11 +21,12 @@ mutation createVariant($input: ProductVariantCreateInput!) {
"""
-def create_product_variant(
+def raw_create_product_variant(
staff_api_client,
product_id,
variant_name="Test product variant",
stocks=None,
+ quantity_limit_per_customer=10,
):
if not stocks:
stocks = []
@@ -35,6 +37,7 @@ def create_product_variant(
"product": product_id,
"attributes": [],
"stocks": stocks,
+ "quantityLimitPerCustomer": quantity_limit_per_customer,
}
}
@@ -45,9 +48,29 @@ def create_product_variant(
)
content = get_graphql_content(response)
- assert content["data"]["productVariantCreate"]["errors"] == []
+ data = content["data"]["productVariantCreate"]
+
+ return data
+
+
+def create_product_variant(
+ staff_api_client,
+ product_id,
+ variant_name="Test product variant",
+ stocks=None,
+ quantity_limit_per_customer=10,
+):
+ response = raw_create_product_variant(
+ staff_api_client,
+ product_id,
+ variant_name,
+ stocks,
+ quantity_limit_per_customer,
+ )
+
+ assert response["errors"] == []
- data = content["data"]["productVariantCreate"]["productVariant"]
+ data = response["productVariant"]
assert data["id"] is not None
assert data["name"] == variant_name
assert data["product"]["id"] == product_id