Skip to content

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

[TEST] Should not be able to buy a product unavailable for purchase (#13390)

Anna Szczęchb0d5ec1a55f2
diff --git a/saleor/tests/e2e/checkout/test_logged_in_customer_should_not_be_able_to_buy_unavailable_product.py b/saleor/tests/e2e/checkout/test_logged_in_customer_should_not_be_able_to_buy_unavailable_product.py
new file mode 100644
index 0000000000..56bf83a8c5
--- /dev/null
+++ b/saleor/tests/e2e/checkout/test_logged_in_customer_should_not_be_able_to_buy_unavailable_product.py
@@ -0,0 +1,112 @@
+import pytest
+
+from ..channel.utils import create_channel
+from ..product.utils import (
+ create_category,
+ create_product,
+ create_product_type,
+ create_product_variant,
+ create_product_variant_channel_listing,
+ raw_create_product_channel_listing,
+)
+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,
+):
+ permissions = [
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_product_types_and_attributes,
+ ]
+
+ assign_permissions(e2e_staff_api_client, permissions)
+ warehouse_data = create_warehouse(e2e_staff_api_client)
+
+ channel_data = create_channel(e2e_staff_api_client, warehouse_data["id"])
+ channel_id = channel_data["id"]
+ channel_slug = channel_data["slug"]
+
+ 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"]
+
+ product_channel_listing = raw_create_product_channel_listing(
+ e2e_staff_api_client,
+ product_id,
+ channel_id,
+ is_available_for_purchase=False,
+ available_for_purchase_datetime=None,
+ )
+
+ channel_listing_data = product_channel_listing["product"]["channelListings"][0]
+ assert channel_listing_data["isAvailableForPurchase"] is False
+ assert channel_listing_data["availableForPurchaseAt"] is None
+
+ 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_should_not_be_able_to_buy_unavailable_product_core_0108(
+ e2e_logged_api_client,
+ e2e_staff_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_product_types_and_attributes,
+):
+ # Before
+ variant_id, channel_slug = prepare_product(
+ e2e_staff_api_client,
+ permission_manage_products,
+ permission_manage_channels,
+ permission_manage_product_types_and_attributes,
+ )
+ # Step 1 - Create checkout with non available for purchase product
+ lines = [
+ {"variantId": variant_id, "quantity": 1},
+ ]
+ checkout_data = raw_checkout_create(e2e_logged_api_client, lines, channel_slug)
+
+ errors = checkout_data["errors"]
+ assert errors[0]["code"] == "PRODUCT_UNAVAILABLE_FOR_PURCHASE"
+ assert errors[0]["field"] == "lines"
+ assert (
+ errors[0]["message"]
+ == "Cannot add lines for unavailable for purchase variants."
+ )
diff --git a/saleor/tests/e2e/product/utils/__init__.py b/saleor/tests/e2e/product/utils/__init__.py
index 024227d0c5..f9a7daf5ed 100644
--- a/saleor/tests/e2e/product/utils/__init__.py
+++ b/saleor/tests/e2e/product/utils/__init__.py
@@ -1,7 +1,10 @@
from .category import create_category
from .digital_content import create_digital_content
from .product import create_product
-from .product_channel_listing import create_product_channel_listing
+from .product_channel_listing import (
+ create_product_channel_listing,
+ raw_create_product_channel_listing,
+)
from .product_type import create_product_type
from .product_variant import create_product_variant
from .product_variant_channel_listing import create_product_variant_channel_listing
@@ -14,4 +17,5 @@ __all__ = [
"create_product_variant_channel_listing",
"create_product_variant",
"create_product",
+ "raw_create_product_channel_listing",
]
diff --git a/saleor/tests/e2e/product/utils/product_channel_listing.py b/saleor/tests/e2e/product/utils/product_channel_listing.py
index 9e0dbb4350..800e08810c 100644
--- a/saleor/tests/e2e/product/utils/product_channel_listing.py
+++ b/saleor/tests/e2e/product/utils/product_channel_listing.py
@@ -33,15 +33,15 @@ mutation UpdateProductChannelListing(
"""
-def create_product_channel_listing(
+def raw_create_product_channel_listing(
staff_api_client,
product_id,
channel_id,
- publication_date=datetime.date(2007, 1, 1),
- is_published=True,
- visible_in_listings=True,
- available_for_purchase_datetime=datetime.datetime(2007, 1, 1, tzinfo=pytz.utc),
- is_available_for_purchase=True,
+ publication_date=None,
+ is_published=False,
+ visible_in_listings=False,
+ available_for_purchase_datetime=None,
+ is_available_for_purchase=False,
):
variables = {
"productId": product_id,
@@ -66,9 +66,34 @@ def create_product_channel_listing(
)
content = get_graphql_content(response)
- assert content["data"]["productChannelListingUpdate"]["errors"] == []
+ data = content["data"]["productChannelListingUpdate"]
+
+ return data
+
+
+def create_product_channel_listing(
+ staff_api_client,
+ product_id,
+ channel_id,
+ publication_date=datetime.date(2007, 1, 1),
+ is_published=True,
+ visible_in_listings=True,
+ available_for_purchase_datetime=datetime.datetime(2007, 1, 1, tzinfo=pytz.utc),
+ is_available_for_purchase=True,
+):
+ response = raw_create_product_channel_listing(
+ staff_api_client,
+ product_id,
+ channel_id,
+ publication_date,
+ is_published,
+ visible_in_listings,
+ available_for_purchase_datetime,
+ is_available_for_purchase,
+ )
- data = content["data"]["productChannelListingUpdate"]["product"]
+ assert response["errors"] == []
+ data = response["product"]
assert data["id"] == product_id
channel_listing_data = data["channelListings"][0]
assert channel_listing_data["channel"]["id"] == channel_id