Skip to content

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

Fix unit tests

Damian W38e800ef7591
diff --git a/saleor/graphql/product/mutations/channels.py b/saleor/graphql/product/mutations/channels.py
index a16c09c779..37196ba46f 100644
--- a/saleor/graphql/product/mutations/channels.py
+++ b/saleor/graphql/product/mutations/channels.py
@@ -235,7 +235,7 @@ class ProductChannelListingUpdate(BaseChannelListingMutation):
"addVariants": ValidationError(
"One of channel listing already "
"exists for this product variant.",
- code=ProductErrorCode.DUPLICATED_INPUT_ITEM.value,
+ code=ProductErrorCode.ALREADY_EXISTS.value,
)
}
)
diff --git a/saleor/graphql/product/tests/test_product_channel_listing_update.py b/saleor/graphql/product/tests/test_product_channel_listing_update.py
index 61352ec298..a6e3f1d83a 100644
--- a/saleor/graphql/product/tests/test_product_channel_listing_update.py
+++ b/saleor/graphql/product/tests/test_product_channel_listing_update.py
@@ -5,6 +5,7 @@ import graphene
from freezegun import freeze_time
from ....product.error_codes import ProductErrorCode
+from ....product.models import ProductVariant, ProductVariantChannelListing
from ....product.utils.costs import get_product_costs_data
from ...tests.utils import assert_no_permission, get_graphql_content
@@ -19,6 +20,7 @@ mutation UpdateProductChannelListing(
message
code
channels
+ variants
}
product {
slug
@@ -44,6 +46,12 @@ mutation UpdateProductChannelListing(
isAvailableForPurchase
availableForPurchase
}
+ variants {
+ channelListings {
+ channel {
+ slug}
+ }
+ }
}
}
}
@@ -59,7 +67,7 @@ def test_product_channel_listing_update_duplicated_ids_in_add_and_remove(
variables = {
"id": product_id,
"input": {
- "addChannels": [{"channelId": channel_id, "isPublished": True}],
+ "updateChannels": [{"channelId": channel_id, "isPublished": True}],
"removeChannels": [channel_id],
},
}
@@ -91,7 +99,7 @@ def test_product_channel_listing_update_duplicated_channel_in_add(
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{"channelId": channel_id, "isPublished": True},
{"channelId": channel_id, "isPublished": False},
],
@@ -111,7 +119,7 @@ def test_product_channel_listing_update_duplicated_channel_in_add(
"productChannelListingErrors"
]
assert len(errors) == 1
- assert errors[0]["field"] == "addChannels"
+ assert errors[0]["field"] == "updateChannels"
assert errors[0]["code"] == ProductErrorCode.DUPLICATED_INPUT_ITEM.name
assert errors[0]["channels"] == [channel_id]
@@ -177,7 +185,7 @@ def test_product_channel_listing_update_with_empty_lists_in_input(
product_id = graphene.Node.to_global_id("Product", product.pk)
variables = {
"id": product_id,
- "input": {"addChannels": [], "removeChannels": []},
+ "input": {"updateChannels": [], "removeChannels": []},
}
# when
@@ -206,7 +214,7 @@ def test_product_channel_listing_update_as_staff_user(
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{
"channelId": channel_id,
"isPublished": False,
@@ -285,7 +293,7 @@ def test_product_channel_listing_update_trigger_webhook_product_updated(
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{
"channelId": channel_id,
"isPublished": False,
@@ -321,7 +329,7 @@ def test_product_channel_listing_update_as_app(
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{
"channelId": channel_id,
"isPublished": False,
@@ -378,7 +386,7 @@ def test_product_channel_listing_update_as_customer(
channel_id = graphene.Node.to_global_id("Channel", channel_PLN.id)
variables = {
"id": product_id,
- "input": {"addChannels": [{"channelId": channel_id, "isPublished": False}]},
+ "input": {"updateChannels": [{"channelId": channel_id, "isPublished": False}]},
}
# when
@@ -396,7 +404,7 @@ def test_product_channel_listing_update_as_anonymous(api_client, product, channe
channel_id = graphene.Node.to_global_id("Channel", channel_PLN.id)
variables = {
"id": product_id,
- "input": {"addChannels": [{"channelId": channel_id, "isPublished": False}]},
+ "input": {"updateChannels": [{"channelId": channel_id, "isPublished": False}]},
}
# when
@@ -419,7 +427,7 @@ def test_product_channel_listing_update_add_channel(
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{
"channelId": channel_id,
"isPublished": False,
@@ -471,7 +479,7 @@ def test_product_channel_listing_update_add_channel_without_publication_date(
channel_id = graphene.Node.to_global_id("Channel", channel_PLN.id)
variables = {
"id": product_id,
- "input": {"addChannels": [{"channelId": channel_id, "isPublished": True}]},
+ "input": {"updateChannels": [{"channelId": channel_id, "isPublished": True}]},
}
# when
@@ -507,7 +515,7 @@ def test_product_channel_listing_update_unpublished(
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
variables = {
"id": product_id,
- "input": {"addChannels": [{"channelId": channel_id, "isPublished": False}]},
+ "input": {"updateChannels": [{"channelId": channel_id, "isPublished": False}]},
}
# when
@@ -547,7 +555,7 @@ def test_product_channel_listing_update_publish_without_publication_date(
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
variables = {
"id": product_id,
- "input": {"addChannels": [{"channelId": channel_id, "isPublished": True}]},
+ "input": {"updateChannels": [{"channelId": channel_id, "isPublished": True}]},
}
# when
@@ -580,7 +588,9 @@ def test_product_channel_listing_update_remove_publication_date(
channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
variables = {
"id": product_id,
- "input": {"addChannels": [{"channelId": channel_id, "publicationDate": None}]},
+ "input": {
+ "updateChannels": [{"channelId": channel_id, "publicationDate": None}]
+ },
}
# when
@@ -616,7 +626,7 @@ def test_product_channel_listing_update_visible_in_listings(
variables = {
"id": product_id,
"input": {
- "addChannels": [{"channelId": channel_id, "visibleInListings": False}]
+ "updateChannels": [{"channelId": channel_id, "visibleInListings": False}]
},
}
@@ -654,7 +664,7 @@ def test_product_channel_listing_update_update_publication_data(
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{
"channelId": channel_id,
"isPublished": False,
@@ -700,7 +710,9 @@ def test_product_channel_listing_update_update_is_available_for_purchase_false(
variables = {
"id": product_id,
"input": {
- "addChannels": [{"channelId": channel_id, "isAvailableForPurchase": False}]
+ "updateChannels": [
+ {"channelId": channel_id, "isAvailableForPurchase": False}
+ ]
},
}
@@ -734,7 +746,9 @@ def test_product_channel_listing_update_update_is_available_for_purchase_without
variables = {
"id": product_id,
"input": {
- "addChannels": [{"channelId": channel_id, "isAvailableForPurchase": True}]
+ "updateChannels": [
+ {"channelId": channel_id, "isAvailableForPurchase": True}
+ ]
},
}
@@ -772,7 +786,7 @@ def test_product_channel_listing_update_update_is_available_for_purchase_past_da
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{
"channelId": channel_id,
"isAvailableForPurchase": True,
@@ -816,7 +830,7 @@ def test_product_channel_listing_update_update_is_available_for_purchase_future_
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{
"channelId": channel_id,
"isAvailableForPurchase": True,
@@ -860,7 +874,7 @@ def test_product_channel_listing_update_update_is_available_for_purchase_false_a
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{
"channelId": channel_id,
"isAvailableForPurchase": False,
@@ -966,7 +980,7 @@ def test_product_channel_listing_update_publish_product_without_category(
variables = {
"id": product_id,
"input": {
- "addChannels": [
+ "updateChannels": [
{"channelId": channel_usd_id, "isPublished": True},
{"channelId": channel_pln_id, "isPublished": False},
]
@@ -988,3 +1002,227 @@ def test_product_channel_listing_update_publish_product_without_category(
assert errors[0]["code"] == ProductErrorCode.PRODUCT_WITHOUT_CATEGORY.name
assert errors[0]["channels"] == [channel_usd_id]
assert len(errors) == 1
+
+
+def test_product_channel_listing_add_variant_as_staff_user(
+ staff_api_client, product, permission_manage_products, channel_USD
+):
+ # given
+ product.variants.all().delete()
+ variant_1 = ProductVariant.objects.create(product=product, sku="321")
+ variant_2 = ProductVariant.objects.create(product=product, sku="333")
+ variant_1_id = graphene.Node.to_global_id("ProductVariant", variant_1.pk)
+ variant_2_id = graphene.Node.to_global_id("ProductVariant", variant_2.pk)
+ variants = [variant_1_id, variant_2_id]
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ variables = {
+ "id": product_id,
+ "input": {
+ "updateChannels": [{"channelId": channel_id, "addVariants": variants}]
+ },
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["productChannelListingUpdate"]
+ variant_data = data["product"]["variants"]
+
+ assert not data["productChannelListingErrors"]
+ assert variant_data[0]["channelListings"][0]["channel"]["slug"] == channel_USD.slug
+ assert variant_data[1]["channelListings"][0]["channel"]["slug"] == channel_USD.slug
+
+
+def test_product_channel_listing_add_variant_as_app(
+ app_api_client, product, permission_manage_products, channel_USD
+):
+ # given
+ product.variants.all().delete()
+ variant_1 = ProductVariant.objects.create(product=product, sku="321")
+ variant_2 = ProductVariant.objects.create(product=product, sku="333")
+ variant_1_id = graphene.Node.to_global_id("ProductVariant", variant_1.pk)
+ variant_2_id = graphene.Node.to_global_id("ProductVariant", variant_2.pk)
+ variants = [variant_1_id, variant_2_id]
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ variables = {
+ "id": product_id,
+ "input": {
+ "updateChannels": [{"channelId": channel_id, "addVariants": variants}]
+ },
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ PRODUCT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["productChannelListingUpdate"]
+ variant_data = data["product"]["variants"]
+
+ assert not data["productChannelListingErrors"]
+ assert variant_data[0]["channelListings"][0]["channel"]["slug"] == channel_USD.slug
+ assert variant_data[1]["channelListings"][0]["channel"]["slug"] == channel_USD.slug
+
+
+def test_product_channel_listing_remove_variant_as_staff_user(
+ staff_api_client, product, permission_manage_products, channel_USD, channel_PLN
+):
+ # given
+ variant = product.variants.first()
+ ProductVariantChannelListing.objects.create(channel=channel_PLN, variant=variant)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ variables = {
+ "id": product_id,
+ "input": {
+ "updateChannels": [
+ {"channelId": channel_id, "removeVariants": [variant_id]}
+ ]
+ },
+ }
+
+ assert len(variant.channel_listings.all()) == 2
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["productChannelListingUpdate"]
+ assert not data["productChannelListingErrors"]
+
+ assert len(variant.channel_listings.all()) == 1
+
+
+def test_product_channel_listing_remove_variant_as_app(
+ app_api_client, product, permission_manage_products, channel_USD, channel_PLN
+):
+ # given
+ variant = product.variants.first()
+ ProductVariantChannelListing.objects.create(channel=channel_PLN, variant=variant)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ variables = {
+ "id": product_id,
+ "input": {
+ "updateChannels": [
+ {"channelId": channel_id, "removeVariants": [variant_id]}
+ ]
+ },
+ }
+
+ assert len(variant.channel_listings.all()) == 2
+ # when
+ response = app_api_client.post_graphql(
+ PRODUCT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["productChannelListingUpdate"]
+ assert not data["productChannelListingErrors"]
+
+ assert len(variant.channel_listings.all()) == 1
+
+
+def test_product_channel_listing_add_variant_duplicated_ids_in_add_and_remove(
+ staff_api_client, product, permission_manage_products, channel_USD, channel_PLN
+):
+ # given
+ variant = product.variants.first()
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ channel_id = graphene.Node.to_global_id("Channel", channel_PLN.id)
+ variants = [variant_id]
+ variables = {
+ "id": product_id,
+ "input": {
+ "updateChannels": [
+ {
+ "channelId": channel_id,
+ "isPublished": True,
+ "addVariants": variants,
+ "removeVariants": variants,
+ }
+ ],
+ },
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ errors = content["data"]["productChannelListingUpdate"][
+ "productChannelListingErrors"
+ ]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "input"
+ assert errors[0]["code"] == ProductErrorCode.DUPLICATED_INPUT_ITEM.name
+ assert errors[0]["variants"] == variants
+
+
+def test_product_channel_listing_add_variant_with_existing_channel_listing(
+ staff_api_client, product, permission_manage_products, channel_USD
+):
+ # given
+ variant = product.variants.first()
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.id)
+ variants = [variant_id]
+ variables = {
+ "id": product_id,
+ "input": {
+ "updateChannels": [
+ {
+ "channelId": channel_id,
+ "isPublished": True,
+ "addVariants": variants,
+ }
+ ],
+ },
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_CHANNEL_LISTING_UPDATE_MUTATION,
+ variables=variables,
+ permissions=(permission_manage_products,),
+ )
+ content = get_graphql_content(response)
+
+ # then
+ errors = content["data"]["productChannelListingUpdate"][
+ "productChannelListingErrors"
+ ]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "addVariants"
+ assert errors[0]["code"] == ProductErrorCode.ALREADY_EXISTS.name