Skip to content

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

Do not delete checkout lines where product-channel-listing is missing (#17027)

Maciej Korycinski66f8801eb080
diff --git a/saleor/checkout/fetch.py b/saleor/checkout/fetch.py
index 83badea256..c7f46a97b6 100644
--- a/saleor/checkout/fetch.py
+++ b/saleor/checkout/fetch.py
@@ -379,36 +379,22 @@ def fetch_checkout_lines(
checkout, product, variant_channel_listing, product_channel_listing_mapping
):
unavailable_variant_pks.append(variant.pk)
- if (
- not skip_lines_with_unavailable_variants
- # variant price is denormalized on checkout line object. We have enough
- # information to include the variant without listing in the
- # calculations. This will allow to display a proper prices but we won't
- # allow to finalize the checkout without removing `unavailable_variant`
- # from the checkout.
- or _only_variant_listing_is_missing(
- checkout,
- product,
- variant_channel_listing,
- product_channel_listing_mapping,
- )
- ):
- lines_info.append(
- CheckoutLineInfo(
- line=line,
- variant=variant,
- channel_listing=variant_channel_listing,
- product=product,
- product_type=product_type,
- collections=collections,
- tax_class=product.tax_class or product_type.tax_class,
- discounts=discounts,
- rules_info=rules_info,
- channel=channel,
- voucher=None,
- voucher_code=None,
- )
+ lines_info.append(
+ CheckoutLineInfo(
+ line=line,
+ variant=variant,
+ channel_listing=variant_channel_listing,
+ product=product,
+ product_type=product_type,
+ collections=collections,
+ tax_class=product.tax_class or product_type.tax_class,
+ discounts=discounts,
+ rules_info=rules_info,
+ channel=channel,
+ voucher=None,
+ voucher_code=None,
)
+ )
continue
lines_info.append(
@@ -491,23 +477,6 @@ def _is_variant_valid(
return True
-def _only_variant_listing_is_missing(
- checkout: "Checkout",
- product: "Product",
- variant_channel_listing: Optional["ProductVariantChannelListing"],
- product_channel_listing_mapping: dict,
-):
- if not _product_channel_listing_is_valid(
- checkout,
- product,
- product_channel_listing_mapping,
- ):
- return False
- if not variant_channel_listing or variant_channel_listing.price is None:
- return True
- return False
-
-
def _get_product_channel_listing(
product_channel_listing_mapping: dict, channel_id: int, product: "Product"
):
diff --git a/saleor/checkout/tests/test_fetch.py b/saleor/checkout/tests/test_fetch.py
index a143859d92..6fea8ad0fe 100644
--- a/saleor/checkout/tests/test_fetch.py
+++ b/saleor/checkout/tests/test_fetch.py
@@ -1,3 +1,6 @@
+import pytest
+
+from ...product.models import ProductChannelListing, ProductVariantChannelListing
from ..fetch import CheckoutLineInfo, fetch_checkout_lines
@@ -245,17 +248,31 @@ def test_fetch_checkout_lines_info_when_product_not_available(
)
# then
- assert len(line_infos) == 0
+ assert len(line_infos) == 1
+ assert line_infos[0].line.pk == line.pk
assert unavailable_variants == [line.variant_id]
-def test_fetch_checkout_lines_info_when_variant_without_channel_listing(
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_fetch_checkout_lines_info_when_line_without_channel_listing(
+ channel_listing_model,
+ listing_filter_field,
checkout_with_item_on_promotion,
):
# given
lines = list(checkout_with_item_on_promotion.lines.all())
line = lines[0]
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout_with_item_on_promotion.channel_id,
+ **{listing_filter_field: line.variant_id},
+ ).delete()
# when
line_infos, unavailable_variants = fetch_checkout_lines(
diff --git a/saleor/checkout/tests/test_tasks.py b/saleor/checkout/tests/test_tasks.py
index ca4ba64493..013fddd83e 100644
--- a/saleor/checkout/tests/test_tasks.py
+++ b/saleor/checkout/tests/test_tasks.py
@@ -10,6 +10,7 @@ from django.utils import timezone
from ...order import OrderEvents
from ...order.models import Order
+from ...product.models import ProductChannelListing, ProductVariantChannelListing
from ..models import Checkout, CheckoutLine
from ..tasks import (
automatic_checkout_completion_task,
@@ -739,6 +740,60 @@ def test_automatic_checkout_completion_task_unavailable_variant(
assert caplog.records[0].levelno == logging.INFO
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_automatic_checkout_completion_task_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ checkout_with_prices,
+ transaction_item_generator,
+ app,
+ caplog,
+ django_capture_on_commit_callbacks,
+):
+ # given
+ checkout = checkout_with_prices
+ checkout_pk = checkout.pk
+
+ # make the checkout line unavailable
+ line = checkout.lines.first()
+ variant = line.variant
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: variant.id},
+ ).delete()
+
+ # allow catching the log in caplog
+ parent_logger = task_logger.parent
+ parent_logger.propagate = True
+
+ transaction_item_generator(
+ checkout_id=checkout.pk, charged_value=checkout.total.gross.amount
+ )
+
+ # when
+ with django_capture_on_commit_callbacks(execute=True):
+ automatic_checkout_completion_task(checkout_pk, None, app.id)
+
+ # then
+ assert Checkout.objects.filter(pk=checkout_pk).exists()
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout_pk)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ assert len(caplog.records) == 1
+ assert caplog.records[0].message == (
+ "The automatic checkout completion not triggered, as the checkout "
+ f"{checkout_id} contains unavailable variants: {variant_id}."
+ )
+ assert caplog.records[0].checkout_id == checkout_id
+ assert caplog.records[0].levelno == logging.INFO
+
+
def test_automatic_checkout_completion_task_error_raised(checkout, app, caplog):
# given
checkout_pk = checkout.pk
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
index e4f39455cd..c9f2a9b9ce 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_add_promo_code.py
@@ -18,7 +18,11 @@ from .....checkout.utils import add_variant_to_checkout, set_external_shipping_i
from .....core.models import EventDelivery
from .....discount import VoucherType
from .....plugins.manager import get_plugins_manager
-from .....product.models import Collection, ProductVariantChannelListing
+from .....product.models import (
+ Collection,
+ ProductChannelListing,
+ ProductVariantChannelListing,
+)
from .....warehouse.models import Stock
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
@@ -272,15 +276,24 @@ def test_checkout_add_voucher_code_without_display_gross_prices(
assert checkout_with_item.last_change == previous_checkout_last_change
-def test_checkout_add_voucher_code_variant_unavailable(
- api_client, checkout_with_item, voucher
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_add_voucher_code_line_without_listing(
+ channel_listing_model, listing_filter_field, api_client, checkout_with_item, voucher
):
variables = {
"id": to_global_id_or_none(checkout_with_item),
"promoCode": voucher.code,
}
- checkout_with_item.lines.first().variant.channel_listings.filter(
- channel=checkout_with_item.channel
+ line = checkout_with_item.lines.first()
+ channel_listing_model.objects.filter(
+ channel_id=checkout_with_item.channel_id,
+ **{listing_filter_field: line.variant_id},
).delete()
data = _mutate_checkout_add_promo_code(api_client, variables)
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py
index 88cafe6e82..b582ef3ce7 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_billing_address_update.py
@@ -7,6 +7,7 @@ from django.test import override_settings
from .....checkout.actions import call_checkout_info_event
from .....checkout.utils import invalidate_checkout
from .....core.models import EventDelivery
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -72,12 +73,28 @@ def test_checkout_billing_address_update_by_id(
assert checkout.billing_address.validation_skipped is False
-def test_checkout_billing_address_update_when_variant_without_listing(
- user_api_client, checkout_with_item, graphql_address_data
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_billing_address_update_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item,
+ graphql_address_data,
):
checkout = checkout_with_item
- line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+ line_without_listing = checkout.lines.first()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
+
assert checkout.shipping_address is None
query = MUTATION_CHECKOUT_BILLING_ADDRESS_UPDATE
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
index 9f7518712a..637bf748cb 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
@@ -29,6 +29,7 @@ from .....payment.gateways.dummy_credit_card import TOKEN_VALIDATION_MAPPING
from .....payment.interface import GatewayResponse
from .....payment.model_helpers import get_subtotal
from .....plugins.manager import PluginsManager, get_plugins_manager
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
from .....warehouse.tests.utils import get_available_quantity_for_stock
from ....core.utils import to_global_id_or_none
@@ -811,7 +812,16 @@ def test_checkout_complete_with_variant_without_price(
assert not checkout.completing_started_at
-def test_checkout_complete_with_variant_without_channel_listing(
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_complete_with_line_without_channel_listing(
+ channel_listing_model,
+ listing_filter_field,
site_settings,
user_api_client,
checkout_with_item,
@@ -828,7 +838,11 @@ def test_checkout_complete_with_variant_without_channel_listing(
checkout_line = checkout.lines.first()
checkout_line_variant = checkout_line.variant
- checkout_line_variant.channel_listings.filter(channel=checkout.channel).delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: checkout_line.variant_id},
+ ).delete()
variant_id = graphene.Node.to_global_id("ProductVariant", checkout_line_variant.pk)
redirect_url = "https://www.example.com"
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
index e162c89c31..67d5368736 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
@@ -28,7 +28,11 @@ from .....payment import TransactionEventType
from .....payment.model_helpers import get_subtotal
from .....payment.transaction_item_calculations import recalculate_transaction_amounts
from .....plugins.manager import PluginsManager, get_plugins_manager
-from .....product.models import VariantChannelListingPromotionRule
+from .....product.models import (
+ ProductChannelListing,
+ ProductVariantChannelListing,
+ VariantChannelListingPromotionRule,
+)
from .....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
from .....warehouse.tests.utils import get_available_quantity_for_stock
from ....core.utils import to_global_id_or_none
@@ -1145,7 +1149,16 @@ def test_checkout_with_variant_without_price(
assert errors[0]["variants"] == [variant_id]
-def test_checkout_with_variant_without_channel_listing(
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_with_line_without_channel_listing(
+ channel_listing_model,
+ listing_filter_field,
site_settings,
user_api_client,
checkout_with_item,
@@ -1167,7 +1180,11 @@ def test_checkout_with_variant_without_channel_listing(
checkout_line = checkout.lines.first()
checkout_line_variant = checkout_line.variant
- checkout_line_variant.channel_listings.filter(channel=checkout.channel).delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: checkout_line.variant_id},
+ ).delete()
variant_id = to_global_id_or_none(checkout_line_variant)
redirect_url = "https://www.example.com"
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_create.py b/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
index 63cb6ef8d7..58ca2709fc 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
@@ -16,7 +16,7 @@ from .....checkout.fetch import fetch_checkout_lines
from .....checkout.models import Checkout
from .....checkout.utils import calculate_checkout_quantity
from .....core.models import EventDelivery
-from .....product.models import ProductChannelListing
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....warehouse.models import Reservation, Stock
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -205,11 +205,36 @@ def test_checkout_create_with_unavailable_variant(
assert error["variants"] == [variant_id]
-def test_checkout_create_with_variant_without_channel_listing(
- api_client, stock, graphql_address_data, channel_USD
+ ("channel_listing_model", "listing_filter_field", "expected_error_code"),
+ [
+ (
+ ProductVariantChannelListing,
+ "variant_id",
+ CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name,
+ ),
+ (
+ ProductChannelListing,
+ "product__variants__id",
+ CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE.name,
+ ),
+ ],
+)
+def test_checkout_create_with_line_without_channel_listing(
+ channel_listing_model,
+ listing_filter_field,
+ expected_error_code,
+ api_client,
+ stock,
+ graphql_address_data,
+ channel_USD,
):
variant = stock.product_variant
- variant.channel_listings.filter(channel=channel_USD).delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=channel_USD.id, **{listing_filter_field: variant.id}
+ ).delete()
+
variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
test_email = "[email protected]"
shipping_address = graphql_address_data
@@ -227,7 +252,7 @@ def test_checkout_create_with_variant_without_channel_listing(
error = get_graphql_content(response)["data"]["checkoutCreate"]["errors"][0]
assert error["field"] == "lines"
- assert error["code"] == CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name
+ assert error["code"] == expected_error_code
assert error["variants"] == [variant_id]
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_create_from_order.py b/saleor/graphql/checkout/tests/mutations/test_checkout_create_from_order.py
index 79f83beb70..ff38880307 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_create_from_order.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_create_from_order.py
@@ -1,10 +1,11 @@
import datetime
import graphene
+import pytest
from .....checkout.error_codes import CheckoutCreateFromOrderUnavailableVariantErrorCode
from .....checkout.models import Checkout
-from .....product.models import ProductVariantChannelListing
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....warehouse.models import Stock
from ....tests.utils import get_graphql_content
from ...enums import CheckoutCreateFromOrderErrorCode
@@ -314,15 +315,38 @@ def test_checkout_create_from_order_variant_not_found(
assert unavailable_variant["variantId"] == first_line.product_variant_id
-def test_checkout_create_from_order_variant_without_channel_listing(
- user_api_client, order_with_lines
+ ("channel_listing_model", "listing_filter_field", "expected_error_code"),
+ [
+ (
+ ProductVariantChannelListing,
+ "variant_id",
+ CheckoutCreateFromOrderUnavailableVariantErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name,
+ ),
+ (
+ ProductChannelListing,
+ "product__variants__id",
+ CheckoutCreateFromOrderUnavailableVariantErrorCode.PRODUCT_NOT_PUBLISHED.name,
+ ),
+ ],
+)
+def test_checkout_create_from_order_line_without_channel_listing(
+ channel_listing_model,
+ listing_filter_field,
+ expected_error_code,
+ user_api_client,
+ order_with_lines,
):
# given
order_with_lines.user = user_api_client.user
order_with_lines.save()
Stock.objects.update(quantity=10)
first_line = order_with_lines.lines.first()
- first_line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=order_with_lines.channel_id,
+ **{listing_filter_field: first_line.variant_id},
+ ).delete()
variables = {"id": graphene.Node.to_global_id("Order", order_with_lines.pk)}
# when
@@ -354,12 +378,9 @@ def test_checkout_create_from_order_variant_without_channel_listing(
order_lines_map, checkout_lines, checkout_lines_from_response_map
)
- error_codes = CheckoutCreateFromOrderUnavailableVariantErrorCode
assert len(data["unavailableVariants"]) == 1
unavailable_variant = data["unavailableVariants"][0]
- assert (
- unavailable_variant["code"] == error_codes.UNAVAILABLE_VARIANT_IN_CHANNEL.name
- )
+ assert unavailable_variant["code"] == expected_error_code
assert unavailable_variant["lineId"] == graphene.Node.to_global_id(
"OrderLine", first_line.pk
)
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_customer_attach.py b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_attach.py
index a45ba34c4b..50784103ca 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_customer_attach.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_attach.py
@@ -1,12 +1,14 @@
from unittest.mock import patch
import graphene
+import pytest
from django.test import override_settings
from .....account.models import User
from .....checkout.actions import call_checkout_event
from .....checkout.error_codes import CheckoutErrorCode
from .....core.models import EventDelivery
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -53,8 +55,20 @@ def test_checkout_customer_attach(
assert checkout.last_change != previous_last_change
-def test_checkout_customer_attach_when_variant_without_channel_listing(
- user_api_client, checkout_with_item, customer_user2, permission_impersonate_user
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_customer_attach_when_line_without_channel_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item,
+ customer_user2,
+ permission_impersonate_user,
):
# given
checkout = checkout_with_item
@@ -62,7 +76,10 @@ def test_checkout_customer_attach_when_variant_without_channel_listing(
checkout.save()
line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: line.variant_id}
+ ).delete()
assert checkout.user is None
previous_last_change = checkout.last_change
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_customer_detach.py b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_detach.py
index bcee710a83..0f9395a175 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_customer_detach.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_detach.py
@@ -1,10 +1,12 @@
from unittest.mock import patch
+import pytest
from django.test import override_settings
from .....account.models import User
from .....checkout.actions import call_checkout_event
from .....core.models import EventDelivery
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -53,8 +55,19 @@ def test_checkout_customer_detach(user_api_client, checkout_with_item, customer_
assert_no_permission(response)
-def test_checkout_customer_detach_when_variant_without_listing(
- user_api_client, checkout_with_item, customer_user
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_customer_detach_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item,
+ customer_user,
):
# given
checkout = checkout_with_item
@@ -62,7 +75,9 @@ def test_checkout_customer_detach_when_variant_without_listing(
checkout.save(update_fields=["user"])
line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: line.variant_id}
+ ).delete()
previous_last_change = checkout.last_change
@@ -81,15 +96,6 @@ def test_checkout_customer_detach_when_variant_without_listing(
assert checkout.user is None
assert checkout.last_change != previous_last_change
- # Mutation should fail when user calling it doesn't own the checkout.
- other_user = User.objects.create_user("[email protected]", "password")
- checkout.user = other_user
- checkout.save()
- response = user_api_client.post_graphql(
- MUTATION_CHECKOUT_CUSTOMER_DETACH, variables
- )
- assert_no_permission(response)
-
def test_checkout_customer_detach_by_app(
app_api_client, checkout_with_item, customer_user, permission_impersonate_user
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_customer_note_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_note_update.py
index 3bcbc5854d..d172efc57e 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_customer_note_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_customer_note_update.py
@@ -1,11 +1,12 @@
from unittest.mock import patch
+import pytest
from django.test import override_settings
-from saleor.checkout.actions import call_checkout_event
-from saleor.core.models import EventDelivery
-from saleor.webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
-
+from .....checkout.actions import call_checkout_event
+from .....core.models import EventDelivery
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
+from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
from ....tests.utils import get_graphql_content
@@ -56,8 +57,15 @@ def test_checkout_customer_note_update(user_api_client, checkout_with_item):
assert checkout.last_change != previous_last_change
-def test_checkout_customer_note_update_when_variant_without_listing(
- user_api_client, checkout_with_item
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_customer_note_update_when_line_without_listing(
+ channel_listing_model, listing_filter_field, user_api_client, checkout_with_item
):
# given
checkout = checkout_with_item
@@ -66,7 +74,10 @@ def test_checkout_customer_note_update_when_variant_without_listing(
previous_last_change = checkout.last_change
line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: line.variant_id}
+ ).delete()
customer_note = "New customer note value"
variables = {"id": to_global_id_or_none(checkout), "customerNote": customer_note}
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py
index aeb26d2c73..cf96d7f2c1 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_delivery_method_update.py
@@ -12,6 +12,7 @@ from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from .....checkout.utils import PRIVATE_META_APP_SHIPPING_ID, invalidate_checkout
from .....core.models import EventDelivery
from .....plugins.manager import get_plugins_manager
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....shipping import models as shipping_models
from .....shipping.utils import convert_to_shipping_method_data
from .....warehouse import WarehouseClickAndCollectOption
@@ -134,6 +135,13 @@ def test_checkout_delivery_method_update(
assert checkout.collection_point is None
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
@pytest.mark.parametrize(
("delivery_method", "node_name", "attribute_name"),
[
@@ -142,16 +150,22 @@ def test_checkout_delivery_method_update(
],
indirect=("delivery_method",),
)
-def test_checkout_delivery_method_update_when_variant_without_channel_listing(
+def test_checkout_delivery_method_update_when_line_without_channel_listing(
api_client,
delivery_method,
node_name,
attribute_name,
+ channel_listing_model,
+ listing_filter_field,
checkout_with_item_for_cc,
):
# given
line = checkout_with_item_for_cc.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout_with_item_for_cc.channel_id,
+ **{listing_filter_field: line.variant_id},
+ ).delete()
checkout = checkout_with_item_for_cc
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_email_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_email_update.py
index ff89cfce48..b0d1857429 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_email_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_email_update.py
@@ -1,10 +1,12 @@
from unittest.mock import patch
+import pytest
from django.test import override_settings
from .....checkout.actions import call_checkout_event
from .....checkout.error_codes import CheckoutErrorCode
from .....core.models import EventDelivery
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
from ....tests.utils import get_graphql_content
@@ -48,8 +50,15 @@ def test_checkout_email_update(user_api_client, checkout_with_item):
assert checkout.last_change != previous_last_change
-def test_checkout_email_update_when_variant_without_channel_listing(
- user_api_client, checkout_with_item
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_email_update_when_line_without_channel_listing(
+ channel_listing_model, listing_filter_field, user_api_client, checkout_with_item
):
# given
checkout = checkout_with_item
@@ -58,7 +67,10 @@ def test_checkout_email_update_when_variant_without_channel_listing(
previous_last_change = checkout.last_change
line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: line.variant_id}
+ ).delete()
variables = {"id": to_global_id_or_none(checkout), "email": email}
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_language_code_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_language_code_update.py
index 140940ccc2..297f25e4be 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_language_code_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_language_code_update.py
@@ -1,9 +1,11 @@
from unittest.mock import patch
+import pytest
from django.test import override_settings
from .....checkout.actions import call_checkout_event
from .....core.models import EventDelivery
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
from ....tests.utils import get_graphql_content
@@ -48,7 +50,16 @@ def test_checkout_update_language_code(
assert checkout.last_change != previous_last_change
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
def test_checkout_update_language_code_when_variant_without_channel_listing(
+ channel_listing_model,
+ listing_filter_field,
user_api_client,
checkout_with_gift_card,
):
@@ -58,7 +69,10 @@ def test_checkout_update_language_code_when_variant_without_channel_listing(
previous_last_change = checkout.last_change
line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: line.variant_id}
+ ).delete()
variables = {"id": to_global_id_or_none(checkout), "languageCode": language_code}
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_line_delete.py b/saleor/graphql/checkout/tests/mutations/test_checkout_line_delete.py
index 2172d89a11..94b59f3fec 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_line_delete.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_line_delete.py
@@ -2,6 +2,7 @@ from unittest import mock
from unittest.mock import patch
import graphene
+import pytest
from django.test import override_settings
from .....checkout import base_calculations
@@ -16,6 +17,7 @@ from .....checkout.utils import (
)
from .....core.models import EventDelivery
from .....plugins.manager import get_plugins_manager
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....warehouse.models import Reservation
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
@@ -88,6 +90,13 @@ def test_checkout_line_delete(
assert mocked_invalidate_checkout.call_count == 1
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
@mock.patch(
"saleor.graphql.checkout.mutations.checkout_line_delete."
"update_checkout_shipping_method_if_invalid",
@@ -100,6 +109,8 @@ def test_checkout_line_delete(
def test_checkout_line_delete_when_variant_without_channel_listing(
mocked_invalidate_checkout,
mocked_update_shipping_method,
+ channel_listing_model,
+ listing_filter_field,
user_api_client,
checkout_line_with_reservation_in_many_stocks,
):
@@ -111,7 +122,11 @@ def test_checkout_line_delete_when_variant_without_channel_listing(
assert calculate_checkout_quantity(lines) == 3
assert checkout.lines.count() == 1
line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: line.variant_id}
+ ).delete()
+
assert line.quantity == 3
line_id = graphene.Node.to_global_id("CheckoutLine", line.pk)
@@ -137,6 +152,13 @@ def test_checkout_line_delete_when_variant_without_channel_listing(
assert mocked_invalidate_checkout.call_count == 1
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
@mock.patch(
"saleor.graphql.checkout.mutations.checkout_line_delete."
"update_checkout_shipping_method_if_invalid",
@@ -146,9 +168,11 @@ def test_checkout_line_delete_when_variant_without_channel_listing(
"saleor.graphql.checkout.mutations.checkout_line_delete.invalidate_checkout",
wraps=invalidate_checkout,
)
-def test_checkout_line_delete_when_checkout_has_variant_without_channel_listing(
+def test_checkout_line_delete_when_checkout_has_line_without_channel_listing(
mocked_invalidate_checkout,
mocked_update_shipping_method,
+ channel_listing_model,
+ listing_filter_field,
user_api_client,
checkout_with_items,
):
@@ -160,7 +184,11 @@ def test_checkout_line_delete_when_checkout_has_variant_without_channel_listing(
assert calculate_checkout_quantity(lines) == 4
assert checkout.lines.count() == 4
line_without_listing = checkout.lines.last()
- line_without_listing.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
line = checkout.lines.first()
line_id = graphene.Node.to_global_id("CheckoutLine", line.pk)
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
index ea3b6c46be..986e3127fa 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
@@ -134,6 +134,13 @@ def test_checkout_lines_add(
assert mocked_invalidate_checkout.call_count == 1
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
@mock.patch(
"saleor.graphql.checkout.mutations.checkout_lines_add."
"update_checkout_shipping_method_if_invalid",
@@ -143,19 +150,24 @@ def test_checkout_lines_add(
"saleor.graphql.checkout.mutations.checkout_lines_add.invalidate_checkout",
wraps=invalidate_checkout,
)
-def test_checkout_lines_add_when_checkout_has_line_without_variant_listing(
+def test_checkout_lines_add_when_checkout_has_line_without_listing(
mocked_invalidate_checkout,
mocked_update_shipping_method,
+ channel_listing_model,
+ listing_filter_field,
user_api_client,
checkout_with_item,
- stock,
+ variant_without_inventory_tracking,
):
# given
- variant = stock.product_variant
+ variant = variant_without_inventory_tracking
checkout = checkout_with_item
line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: line.variant_id}
+ ).delete()
lines, _ = fetch_checkout_lines(checkout)
assert calculate_checkout_quantity(lines) == 3
@@ -1412,12 +1424,36 @@ def test_checkout_lines_add_with_unavailable_variant(
assert errors[0]["variants"] == [variant_id]
+ ("channel_listing_model", "listing_filter_field", "expected_error_code"),
+ [
+ (
+ ProductVariantChannelListing,
+ "variant_id",
+ CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name,
+ ),
+ (
+ ProductChannelListing,
+ "product__variants__id",
+ CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE.name,
+ ),
+ ],
+)
def test_checkout_lines_add_variant_without_channel_listing(
- user_api_client, checkout_with_item, stock
+ channel_listing_model,
+ listing_filter_field,
+ expected_error_code,
+ user_api_client,
+ checkout_with_item,
+ stock,
):
# given
variant = stock.product_variant
- variant.channel_listings.filter(channel=checkout_with_item.channel).delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout_with_item.channel_id, **{listing_filter_field: variant.id}
+ ).delete()
+
checkout = checkout_with_item
line = checkout.lines.first()
assert line.quantity == 3
@@ -1435,7 +1471,7 @@ def test_checkout_lines_add_variant_without_channel_listing(
# then
content = get_graphql_content(response)
errors = content["data"]["checkoutLinesAdd"]["errors"]
- assert errors[0]["code"] == CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name
+ assert errors[0]["code"] == expected_error_code
assert errors[0]["field"] == "lines"
assert errors[0]["variants"] == [variant_id]
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_delete.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_delete.py
index 8b56ebb120..6d05049c1d 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_delete.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_delete.py
@@ -2,6 +2,7 @@ from unittest import mock
from unittest.mock import patch
import graphene
+import pytest
from django.test import override_settings
from .....checkout.actions import call_checkout_info_event
@@ -11,6 +12,7 @@ from .....checkout.models import CheckoutLine
from .....checkout.utils import invalidate_checkout
from .....core.models import EventDelivery
from .....plugins.manager import get_plugins_manager
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
from ....tests.utils import get_graphql_content
@@ -84,6 +86,13 @@ def test_checkout_lines_delete(
assert mocked_invalidate_checkout.call_count == 1
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
@mock.patch(
"saleor.graphql.checkout.mutations.checkout_lines_delete."
"update_checkout_shipping_method_if_invalid",
@@ -93,9 +102,11 @@ def test_checkout_lines_delete(
"saleor.graphql.checkout.mutations.checkout_lines_delete.invalidate_checkout",
wraps=invalidate_checkout,
)
-def test_checkout_lines_delete_when_variant_without_channel_listing(
+def test_checkout_lines_delete_when_line_without_channel_listing(
mocked_invalidate_checkout,
mocked_update_shipping_method,
+ channel_listing_model,
+ listing_filter_field,
user_api_client,
checkout_with_items,
):
@@ -104,7 +115,11 @@ def test_checkout_lines_delete_when_variant_without_channel_listing(
checkout_lines_count = checkout.lines.count()
previous_last_change = checkout.last_change
line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: line.variant_id}
+ ).delete()
+
second_line = checkout.lines.last()
first_line_id = graphene.Node.to_global_id("CheckoutLine", line.pk)
@@ -134,6 +149,13 @@ def test_checkout_lines_delete_when_variant_without_channel_listing(
assert mocked_invalidate_checkout.call_count == 1
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
@mock.patch(
"saleor.graphql.checkout.mutations.checkout_lines_delete."
"update_checkout_shipping_method_if_invalid",
@@ -146,6 +168,8 @@ def test_checkout_lines_delete_when_variant_without_channel_listing(
def test_checkout_lines_delete_when_checkout_has_variant_without_channel_listing(
mocked_invalidate_checkout,
mocked_update_shipping_method,
+ channel_listing_model,
+ listing_filter_field,
user_api_client,
checkout_with_items,
):
@@ -157,7 +181,10 @@ def test_checkout_lines_delete_when_checkout_has_variant_without_channel_listing
line = checkout.lines.first()
line_without_listing = checkout.lines.last()
- line_without_listing.variant.channel_listings.all().delete()
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
first_line_id = graphene.Node.to_global_id("CheckoutLine", line.pk)
lines_list = [
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py
index fef400301d..d91746d828 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_update.py
@@ -21,7 +21,7 @@ from .....checkout.utils import (
from .....core.models import EventDelivery
from .....discount import RewardValueType
from .....plugins.manager import get_plugins_manager
-from .....product.models import ProductChannelListing
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....warehouse.models import Reservation, Stock
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from .....webhook.transport.asynchronous.transport import send_webhook_request_async
@@ -127,6 +127,13 @@ def test_checkout_lines_update(
assert mocked_invalidate_checkout.call_count == 1
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
@mock.patch(
"saleor.graphql.checkout.mutations.checkout_lines_add."
"update_checkout_shipping_method_if_invalid",
@@ -136,9 +143,11 @@ def test_checkout_lines_update(
"saleor.graphql.checkout.mutations.checkout_lines_add.invalidate_checkout",
wraps=invalidate_checkout,
)
-def test_checkout_lines_update_when_checkout_has_variant_without_listing(
+def test_checkout_lines_update_when_checkout_has_line_without_listing(
mocked_invalidate_checkout,
mocked_update_shipping_method,
+ channel_listing_model,
+ listing_filter_field,
user_api_client,
checkout_with_items,
):
@@ -153,7 +162,11 @@ def test_checkout_lines_update_when_checkout_has_variant_without_listing(
assert line.quantity == 1
line_without_listing = checkout.lines.last()
- line_without_listing.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
previous_last_change = checkout.last_change
@@ -1086,15 +1099,38 @@ def test_checkout_lines_update_with_unavailable_variant(
assert checkout.last_change == previous_last_change
-def test_checkout_lines_update_with_variant_without_channel_listing(
- user_api_client, checkout_with_item
+ ("channel_listing_model", "listing_filter_field", "expected_error_code"),
+ [
+ (
+ ProductVariantChannelListing,
+ "variant_id",
+ CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name,
+ ),
+ (
+ ProductChannelListing,
+ "product__variants__id",
+ CheckoutErrorCode.PRODUCT_UNAVAILABLE_FOR_PURCHASE.name,
+ ),
+ ],
+)
+def test_checkout_lines_update_with_line_without_channel_listing(
+ channel_listing_model,
+ listing_filter_field,
+ expected_error_code,
+ user_api_client,
+ checkout_with_item,
):
checkout = checkout_with_item
previous_last_change = checkout.last_change
assert checkout.lines.count() == 1
line = checkout.lines.first()
variant = line.variant
- variant.channel_listings.filter(channel=checkout_with_item.channel).delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: variant.id}
+ ).delete()
+
assert line.quantity == 3
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
@@ -1107,7 +1143,7 @@ def test_checkout_lines_update_with_variant_without_channel_listing(
content = get_graphql_content(response)
errors = content["data"]["checkoutLinesUpdate"]["errors"]
- assert errors[0]["code"] == CheckoutErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.name
+ assert errors[0]["code"] == expected_error_code
assert errors[0]["field"] == "lines"
assert errors[0]["variants"] == [variant_id]
checkout.refresh_from_db()
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_remove_promo_code.py b/saleor/graphql/checkout/tests/mutations/test_checkout_remove_promo_code.py
index 7f9d5f8aaf..79a4f8018e 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_remove_promo_code.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_remove_promo_code.py
@@ -3,6 +3,7 @@ from decimal import Decimal
from unittest.mock import patch
import graphene
+import pytest
from django.test import override_settings
from django.utils import timezone
from prices import Money
@@ -15,6 +16,7 @@ from .....core.models import EventDelivery
from .....discount import RewardValueType
from .....discount.models import Voucher, VoucherChannelListing, VoucherCode
from .....plugins.manager import get_plugins_manager
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from .....webhook.transport.asynchronous.transport import send_webhook_request_async
from .....webhook.transport.utils import WebhookResponse
@@ -93,13 +95,28 @@ def test_checkout_remove_voucher_code(
)
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
@patch("saleor.plugins.manager.PluginsManager.checkout_updated")
-def test_checkout_remove_voucher_code_when_variant_without_listing(
- checkout_updated_webhook_mock, api_client, checkout_with_voucher
+def test_checkout_remove_voucher_code_when_line_without_listing(
+ checkout_updated_webhook_mock,
+ channel_listing_model,
+ listing_filter_field,
+ api_client,
+ checkout_with_voucher,
):
# given
line = checkout_with_voucher.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout_with_voucher.channel_id,
+ **{listing_filter_field: line.variant_id},
+ ).delete()
assert checkout_with_voucher.voucher_code is not None
previous_checkout_last_change = checkout_with_voucher.last_change
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
index 2b36626df8..5fae6976ae 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_shipping_address_update.py
@@ -18,6 +18,7 @@ from .....checkout.utils import (
from .....core.models import EventDelivery
from .....plugins.base_plugin import ExcludedShippingMethod
from .....plugins.manager import get_plugins_manager
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....warehouse.models import Reservation, Stock
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
from ....core.utils import to_global_id_or_none
@@ -145,6 +146,13 @@ def test_checkout_shipping_address_with_metadata_update(
assert mocked_invalidate_checkout.call_count == 1
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
@mock.patch(
"saleor.graphql.checkout.mutations.checkout_shipping_address_update."
"update_checkout_shipping_method_if_invalid",
@@ -158,6 +166,8 @@ def test_checkout_shipping_address_with_metadata_update(
def test_checkout_shipping_address_when_variant_without_listing(
mocked_invalidate_checkout,
mocked_update_shipping_method,
+ channel_listing_model,
+ listing_filter_field,
user_api_client,
checkout_with_item,
graphql_address_data,
@@ -165,7 +175,10 @@ def test_checkout_shipping_address_when_variant_without_listing(
# given
checkout = checkout_with_item
line = checkout.lines.first()
- line.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: line.variant_id}
+ ).delete()
assert checkout.shipping_address is None
previous_last_change = checkout.last_change
diff --git a/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py b/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
index 5da19ba2cc..4c1e2eb3e6 100644
--- a/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
+++ b/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
@@ -21,7 +21,7 @@ from .....order import OrderOrigin, OrderStatus
from .....order.models import Fulfillment, Order
from .....payment.model_helpers import get_subtotal
from .....plugins.manager import PluginsManager, get_plugins_manager
-from .....product.models import ProductVariantChannelListing
+from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....warehouse.models import Reservation, Stock, WarehouseClickAndCollectOption
from .....warehouse.tests.utils import get_available_quantity_for_stock
from ....tests.utils import assert_no_permission, get_graphql_content
@@ -2143,7 +2143,16 @@ def test_order_from_draft_create_click_collect_preorder_fails_for_disabled_wareh
assert Order.objects.count() == initial_order_count
-def test_order_from_draft_create_variant_channel_listing_does_not_exist(
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_order_from_checkout_create_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
checkout_with_items,
address,
shipping_method,
@@ -2164,7 +2173,11 @@ def test_order_from_draft_create_variant_channel_listing_does_not_exist(
checkout_line = checkout.lines.first()
checkout_line_variant = checkout_line.variant
- checkout_line_variant.channel_listings.get(channel__id=checkout.channel_id).delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: checkout_line_variant.id},
+ ).delete()
lines, _ = fetch_checkout_lines(checkout)
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index a700320f89..ff98705d81 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -31,7 +31,11 @@ from ....payment.interface import (
)
from ....plugins.manager import get_plugins_manager
from ....plugins.tests.sample_plugins import ActiveDummyPaymentGateway
-from ....product.models import ProductVariant, ProductVariantChannelListing
+from ....product.models import (
+ ProductChannelListing,
+ ProductVariant,
+ ProductVariantChannelListing,
+)
from ....shipping.models import ShippingMethodTranslation
from ....shipping.utils import convert_to_shipping_method_data
from ....tests.utils import dummy_editorjs
@@ -1770,6 +1774,43 @@ query getCheckout($id: ID) {
}
}
}
+ product {
+ id
+ isAvailable
+ isAvailableForPurchase
+ pricing{
+ onSale
+ discount{
+ gross{
+ amount
+ }
+ }
+ priceRange{
+ start{
+ gross{
+ amount
+ }
+ }
+ stop{
+ gross{
+ amount
+ }
+ }
+ }
+ priceRangeUndiscounted{
+ start{
+ gross{
+ amount
+ }
+ }
+ stop{
+ gross{
+ amount
+ }
+ }
+ }
+ }
+ }
}
unitPrice {
gross {
@@ -1801,11 +1842,25 @@ query getCheckout($id: ID) {
"""
-def test_checkout_prices_when_variant_without_listing(
- user_api_client, checkout_with_line_without_listing
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_when_line_without_listing(
+ channel_listing_model, listing_filter_field, user_api_client, checkout_with_item
):
# given
- checkout, line_without_listing = checkout_with_line_without_listing
+ checkout = checkout_with_item
+ line_without_listing = checkout_with_item.lines.first()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
+
query = QUERY_CHECKOUT_PRICES
variables = {"id": to_global_id_or_none(checkout)}
checkout.price_expiration = timezone.now()
@@ -1972,11 +2027,26 @@ def test_checkout_prices_checkout_with_custom_prices(
)
-def test_checkout_prices_checkout_with_custom_prices_when_variant_without_listing(
- user_api_client, checkout_with_line_without_listing
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_checkout_with_custom_prices_when_line_without_listing(
+ channel_listing_model, listing_filter_field, user_api_client, checkout_with_item
):
# given
- checkout, line_without_listing = checkout_with_line_without_listing
+ checkout = checkout_with_item
+
+ line_without_listing = checkout_with_item.lines.first()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
+
price_override = Decimal("20.00")
line_without_listing.price_override = price_override
line_without_listing.undiscounted_unit_price_amount = price_override
@@ -2100,8 +2170,19 @@ def test_checkout_prices_with_sales(user_api_client, checkout_with_item_on_sale)
assert line_total_price.gross.amount < undiscounted_total_price
-def test_checkout_prices_with_sales_when_variant_without_listing(
- user_api_client, checkout_with_item, promotion_converted_from_sale
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_with_sales_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item,
+ promotion_converted_from_sale,
):
# given
checkout = checkout_with_item
@@ -2126,6 +2207,12 @@ def test_checkout_prices_with_sales_when_variant_without_listing(
)
channel_listing.save(update_fields=["discounted_price_amount"])
+ channel_listing.variantlistingpromotionrule.create(
+ promotion_rule=rule,
+ discount_amount=discount_amount,
+ currency=channel.currency_code,
+ )
+
CheckoutLineDiscount.objects.create(
line=line_without_listing,
promotion_rule=rule,
@@ -2141,8 +2228,8 @@ def test_checkout_prices_with_sales_when_variant_without_listing(
checkout = checkout_with_item
- line_without_listing.variant.channel_listings.get(
- channel_id=checkout.channel_id
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id, **{listing_filter_field: variant.id}
).delete()
query = QUERY_CHECKOUT_PRICES
@@ -2287,8 +2374,18 @@ def test_checkout_prices_with_promotion(
assert line_total_price.gross.amount < undiscounted_total_price
-def test_checkout_prices_with_promotion_when_variant_without_listing(
- user_api_client, checkout_with_item_on_promotion
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_with_promotion_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item_on_promotion,
):
# given
query = QUERY_CHECKOUT_PRICES
@@ -2298,8 +2395,10 @@ def test_checkout_prices_with_promotion_when_variant_without_listing(
variables = {"id": to_global_id_or_none(checkout)}
line_without_listing = checkout.lines.first()
- line_without_listing.variant.channel_listings.get(
- channel_id=checkout.channel_id
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
).delete()
# when
@@ -2423,8 +2522,18 @@ def test_checkout_prices_with_order_promotion(
assert data["lines"][0]["undiscountedTotalPrice"]["amount"] == subtotal_price.amount
-def test_checkout_prices_with_order_promotion_when_variant_without_listing(
- user_api_client, checkout_with_item_and_order_discount
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_with_order_promotion_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item_and_order_discount,
):
# given
query = QUERY_CHECKOUT_PRICES
@@ -2432,8 +2541,10 @@ def test_checkout_prices_with_order_promotion_when_variant_without_listing(
variables = {"id": to_global_id_or_none(checkout)}
line_without_listing = checkout.lines.first()
- line_without_listing.variant.channel_listings.get(
- channel_id=checkout.channel_id
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
).delete()
checkout.price_expiration = timezone.now()
@@ -2570,8 +2681,19 @@ def test_checkout_prices_with_gift_promotion(
)
-def test_checkout_prices_with_gift_promotion_when_variant_without_listing(
- user_api_client, checkout_with_item_and_gift_promotion, gift_promotion_rule
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_with_gift_promotion_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item_and_gift_promotion,
+ gift_promotion_rule,
):
# given
query = QUERY_CHECKOUT_PRICES
@@ -2599,7 +2721,10 @@ def test_checkout_prices_with_gift_promotion_when_variant_without_listing(
checkout_line_info=line_info,
)
- variant_listings.delete()
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
# when
response = user_api_client.post_graphql(query, variables)
@@ -2741,13 +2866,28 @@ def test_checkout_prices_with_specific_voucher(
)
-def test_checkout_prices_with_specific_voucher_when_variant_without_listing(
- user_api_client, checkout_with_item_and_voucher_specific_products
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_with_specific_voucher_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item_and_voucher_specific_products,
):
# given
checkout = checkout_with_item_and_voucher_specific_products
line_without_listing = checkout.lines.first()
- line_without_listing.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
+
query = QUERY_CHECKOUT_PRICES
variables = {"id": to_global_id_or_none(checkout)}
@@ -2887,14 +3027,28 @@ def test_checkout_prices_with_voucher_once_per_order(
)
-def test_checkout_prices_with_voucher_once_per_order_when_variant_without_listing(
- user_api_client, checkout_with_item_and_voucher_once_per_order
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_with_voucher_once_per_order_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item_and_voucher_once_per_order,
):
# given
checkout = checkout_with_item_and_voucher_once_per_order
line_without_listing = checkout.lines.first()
- line_without_listing.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
query = QUERY_CHECKOUT_PRICES
variables = {"id": to_global_id_or_none(checkout)}
@@ -3038,14 +3192,28 @@ def test_checkout_prices_with_voucher(user_api_client, checkout_with_item_and_vo
)
-def test_checkout_prices_with_voucher_when_variant_without_listing(
- user_api_client, checkout_with_item_and_voucher
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_with_voucher_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item_and_voucher,
):
# given
checkout = checkout_with_item_and_voucher
line_without_listing = checkout.lines.first()
- line_without_listing.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
query = QUERY_CHECKOUT_PRICES
variables = {"id": to_global_id_or_none(checkout)}
@@ -3192,14 +3360,29 @@ def test_checkout_prices_with_voucher_code_that_doesnt_exist(
)
-def test_checkout_prices_voucher_code_that_doesnt_exist_when_variant_without_listing(
- user_api_client, checkout_with_item_and_voucher, voucher
+ ("channel_listing_model", "listing_filter_field"),
+ [
+ (ProductVariantChannelListing, "variant_id"),
+ (ProductChannelListing, "product__variants__id"),
+ ],
+)
+def test_checkout_prices_voucher_code_that_doesnt_exist_when_line_without_listing(
+ channel_listing_model,
+ listing_filter_field,
+ user_api_client,
+ checkout_with_item_and_voucher,
+ voucher,
):
# given
checkout = checkout_with_item_and_voucher
line_without_listing = checkout.lines.first()
- line_without_listing.variant.channel_listings.all().delete()
+
+ channel_listing_model.objects.filter(
+ channel_id=checkout.channel_id,
+ **{listing_filter_field: line_without_listing.variant_id},
+ ).delete()
query = QUERY_CHECKOUT_PRICES
variables = {"id": to_global_id_or_none(checkout)}
diff --git a/saleor/graphql/product/mutations/channels.py b/saleor/graphql/product/mutations/channels.py
index b68a0da01b..d041960961 100644
--- a/saleor/graphql/product/mutations/channels.py
+++ b/saleor/graphql/product/mutations/channels.py
@@ -6,7 +6,6 @@ import graphene
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
-from ....checkout.models import CheckoutLine
from ....core.tracing import traced_atomic_transaction
from ....core.utils.date_time import convert_to_utc_date_time
from ....permission.enums import ProductPermissions
@@ -318,17 +317,6 @@ class ProductChannelListingUpdate(BaseChannelListingMutation):
).exists():
product_channel_listing.delete()
- @classmethod
- def perform_checkout_lines_delete(cls, variants, channel_id):
- lines_id_and_checkout_id = list(
- CheckoutLine.objects.filter(
- variant__in=variants, checkout__channel__id__in=channel_id
- ).values("id", "checkout__pk")
- )
- lines_ids = {line["id"] for line in lines_id_and_checkout_id}
-
- CheckoutLine.objects.filter(id__in=lines_ids).delete()
-
@classmethod
def remove_channels(cls, product: "ProductModel", remove_channels: list[dict]):
ProductChannelListing.objects.filter(
@@ -337,8 +325,6 @@ class ProductChannelListingUpdate(BaseChannelListingMutation):
ProductVariantChannelListing.objects.filter(
variant__product_id=product.pk, channel_id__in=remove_channels
).delete()
- variant_ids = product.variants.all().values_list("id", flat=True)
- cls.perform_checkout_lines_delete(variant_ids, remove_channels)
@classmethod
def save(cls, info: ResolveInfo, product: "ProductModel", cleaned_input: dict):
diff --git a/saleor/graphql/product/tests/mutations/test_product_channel_listing_update.py b/saleor/graphql/product/tests/mutations/test_product_channel_listing_update.py
index 607f6c054c..997dbb1c17 100644
--- a/saleor/graphql/product/tests/mutations/test_product_channel_listing_update.py
+++ b/saleor/graphql/product/tests/mutations/test_product_channel_listing_update.py
@@ -943,7 +943,7 @@ def test_product_channel_listing_update_remove_channel(
assert variant.channel_listings.get() == variant_channel_listing_pln
-def test_product_channel_listing_update_remove_channel_removes_checkout_lines(
+def test_product_channel_listing_update_remove_channel_dont_remove_checkout_lines(
staff_api_client,
product_available_in_many_channels,
permission_manage_products,
@@ -978,7 +978,7 @@ def test_product_channel_listing_update_remove_channel_removes_checkout_lines(
# then
data = content["data"]["productChannelListingUpdate"]
assert not data["errors"]
- assert not checkout.lines.all().exists()
+ assert checkout.lines.all().exists()
def test_product_channel_listing_update_remove_not_assigned_channel(