Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
[TEST] Should not be possible to create checkout with product quantity greater than stock CORE_0107 (#13362)
Renatabe5d70448a73
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.pynew file mode 100644index 0000000000..63cd927dd2--- /dev/null+++ b/saleor/tests/e2e/checkout/test_unlogged_customer_should_not_be_able_to_order_product_quantity_greater_than_stock.py@@ -0,0 +1,122 @@+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_zone+from ..utils import assign_permissions+from ..warehouse.utils import create_warehouse+from .utils import raw_checkout_create+++def prepare_product(+ e2e_staff_api_client,+ permission_manage_products,+ permission_manage_channels,+ permission_manage_product_types_and_attributes,+ permission_manage_shipping,+):+ permissions = [+ permission_manage_products,+ permission_manage_channels,+ permission_manage_product_types_and_attributes,+ permission_manage_shipping,+ ]++ assign_permissions(e2e_staff_api_client, permissions)+ warehouse_data = create_warehouse(e2e_staff_api_client)+ warehouse_id = warehouse_data["id"]++ channel_data = create_channel(e2e_staff_api_client, warehouse_data["id"])+ channel_id = channel_data["id"]+ channel_slug = channel_data["slug"]++ create_shipping_zone(+ e2e_staff_api_client,+ warehouse_ids=[warehouse_id],+ channel_ids=[channel_id],+ )++ product_type_data = create_product_type(+ e2e_staff_api_client,+ is_shipping_required=True,+ )+ 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_data["id"],+ "quantity": 5,+ }+ ]+ variant_data = create_product_variant(+ e2e_staff_api_client, product_id, stocks=stocks+ )+ variant_id = variant_data["id"]++ create_product_variant_channel_listing(+ e2e_staff_api_client,+ variant_id,+ channel_id,+ )++ return variant_id, channel_slug+++def test_unlogged_customer_cannot_buy_product_in_quantity_grater_than_stock(+ e2e_not_logged_api_client,+ e2e_staff_api_client,+ permission_manage_products,+ permission_manage_channels,+ permission_manage_product_types_and_attributes,+ permission_manage_shipping,+):+ # Before+ variant_id, channel_slug = prepare_product(+ e2e_staff_api_client,+ permission_manage_products,+ permission_manage_channels,+ permission_manage_product_types_and_attributes,+ permission_manage_shipping,+ )+ # Step 1 - Create checkout with product quantity greater than the available stock+ lines = [+ {"variantId": variant_id, "quantity": 10},+ ]+ checkout_data = raw_checkout_create(+ e2e_not_logged_api_client,+ lines,+ channel_slug,+ email="[email protected]",+ set_default_billing_address=True,+ set_default_shipping_address=True,+ )++ errors = checkout_data["errors"]+ assert errors[0]["code"] == "INSUFFICIENT_STOCK"+ assert errors[0]["field"] == "quantity"+ assert (+ errors[0]["message"]+ == "Could not add items Test product variant. Only 5 remaining in stock."+ )diff --git a/saleor/tests/e2e/checkout/utils/__init__.py b/saleor/tests/e2e/checkout/utils/__init__.pyindex 52be9ce848..10fbcfea8a 100644--- a/saleor/tests/e2e/checkout/utils/__init__.py+++ b/saleor/tests/e2e/checkout/utils/__init__.py@@ -1,6 +1,6 @@from .checkout_billing_address_update import checkout_billing_address_updatefrom .checkout_complete import checkout_complete-from .checkout_create import checkout_create+from .checkout_create import checkout_create, raw_checkout_createfrom .checkout_delivery_method_update import checkout_delivery_method_updatefrom .checkout_payment_create import (checkout_dummy_payment_create,@@ -12,6 +12,7 @@ __all__ = ["checkout_billing_address_update","checkout_complete","checkout_create",+ "raw_checkout_create","checkout_delivery_method_update","checkout_dummy_payment_create","checkout_shipping_address_update",diff --git a/saleor/tests/e2e/checkout/utils/checkout_create.py b/saleor/tests/e2e/checkout/utils/checkout_create.pyindex 9974af3411..ac878571ff 100644--- a/saleor/tests/e2e/checkout/utils/checkout_create.py+++ b/saleor/tests/e2e/checkout/utils/checkout_create.py@@ -51,7 +51,7 @@ mutation CreateCheckout($input: CheckoutCreateInput!) {"""-def checkout_create(+def raw_checkout_create(api_client,lines,channel_slug,@@ -79,9 +79,30 @@ def checkout_create()content = get_graphql_content(response)- assert content["data"]["checkoutCreate"]["errors"] == []+ checkout_data = content["data"]["checkoutCreate"]++ return checkout_data+++def checkout_create(+ api_client,+ lines,+ channel_slug,+ email=None,+ set_default_billing_address=False,+ set_default_shipping_address=False,+):+ checkout_response = raw_checkout_create(+ api_client,+ lines,+ channel_slug,+ email,+ set_default_billing_address,+ set_default_shipping_address,+ )+ assert checkout_response["errors"] == []- data = content["data"]["checkoutCreate"]["checkout"]+ data = checkout_response["checkout"]assert data["id"] is not Noneassert data["channel"]["slug"] == channel_slug