Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Non staff user should be able to fetch collections from products (#6623)
Filip Owczarek376da3af89b4
diff --git a/saleor/graphql/product/tests/deprecated/test_product.py b/saleor/graphql/product/tests/deprecated/test_product.pyindex b61ad48f1d..ffb4b95aa4 100644--- a/saleor/graphql/product/tests/deprecated/test_product.py+++ b/saleor/graphql/product/tests/deprecated/test_product.py@@ -80,3 +80,69 @@ def test_fetch_all_products(user_api_client, product):assert any([str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns])+++QUERY_COLLECTION_FROM_PRODUCT = """+ query ($id: ID, $channel:String){+ product(+ id: $id,+ channel: $channel+ ) {+ collections {+ name+ }+ }+ }+ """+++def test_get_collections_from_product_as_customer(+ user_api_client, product_with_collections, channel_USD, published_collection+):+ # given+ product = product_with_collections+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}++ # when+ with warnings.catch_warnings(record=True) as warns:+ response = user_api_client.post_graphql(+ QUERY_COLLECTION_FROM_PRODUCT,+ variables=variables,+ permissions=(),+ check_no_permissions=False,+ )++ # then+ content = get_graphql_content(response)+ collections = content["data"]["product"]["collections"]+ assert len(collections) == 1+ assert {"name": published_collection.name} in collections+ assert any(+ [str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]+ )+++def test_get_collections_from_product_as_anonymous(+ api_client, product_with_collections, channel_USD, published_collection+):+ # given+ product = product_with_collections+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}++ # when+ with warnings.catch_warnings(record=True) as warns:+ response = api_client.post_graphql(+ QUERY_COLLECTION_FROM_PRODUCT,+ variables=variables,+ permissions=(),+ check_no_permissions=False,+ )++ # then+ content = get_graphql_content(response)+ collections = content["data"]["product"]["collections"]+ assert len(collections) == 1+ assert {"name": published_collection.name} in collections+ assert any(+ [str(warning.message) == DEPRECATION_WARNING_MESSAGE for warning in warns]+ )diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex 2a9dba08d3..2faf1c6dc8 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -324,6 +324,116 @@ def test_product_query_by_id_as_app_without_channel_slug(assert product_data["name"] == product.name+QUERY_COLLECTION_FROM_PRODUCT = """+ query ($id: ID, $channel:String){+ product(+ id: $id,+ channel: $channel+ ) {+ collections {+ name+ }+ }+ }+ """+++def test_get_collections_from_product_as_staff(+ staff_api_client, permission_manage_products, product_with_collections, channel_USD,+):+ # given+ product = product_with_collections+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}++ # when+ response = staff_api_client.post_graphql(+ QUERY_COLLECTION_FROM_PRODUCT,+ variables=variables,+ permissions=(permission_manage_products,),+ check_no_permissions=False,+ )++ # then+ content = get_graphql_content(response)+ collections = content["data"]["product"]["collections"]+ assert len(collections) == 3+ for collection in product.collections.all():+ assert {"name": collection.name} in collections+++def test_get_collections_from_product_as_app(+ app_api_client, permission_manage_products, product_with_collections, channel_USD,+):+ # given+ product = product_with_collections+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}++ # when+ response = app_api_client.post_graphql(+ QUERY_COLLECTION_FROM_PRODUCT,+ variables=variables,+ permissions=(permission_manage_products,),+ check_no_permissions=False,+ )++ # then+ content = get_graphql_content(response)+ collections = content["data"]["product"]["collections"]+ assert len(collections) == 3+ for collection in product.collections.all():+ assert {"name": collection.name} in collections+++def test_get_collections_from_product_as_customer(+ user_api_client, product_with_collections, channel_USD, published_collection+):+ # given+ product = product_with_collections+ variables = {+ "id": graphene.Node.to_global_id("Product", product.pk),+ "channel": channel_USD.slug,+ }++ # when+ response = user_api_client.post_graphql(+ QUERY_COLLECTION_FROM_PRODUCT,+ variables=variables,+ permissions=(),+ check_no_permissions=False,+ )++ # then+ content = get_graphql_content(response)+ collections = content["data"]["product"]["collections"]+ assert len(collections) == 1+ assert {"name": published_collection.name} in collections+++def test_get_collections_from_product_as_anonymous(+ api_client, product_with_collections, channel_USD, published_collection+):+ # given+ product = product_with_collections+ variables = {+ "id": graphene.Node.to_global_id("Product", product.pk),+ "channel": channel_USD.slug,+ }++ # when+ response = api_client.post_graphql(+ QUERY_COLLECTION_FROM_PRODUCT,+ variables=variables,+ permissions=(),+ check_no_permissions=False,+ )++ # then+ content = get_graphql_content(response)+ collections = content["data"]["product"]["collections"]+ assert len(collections) == 1+ assert {"name": published_collection.name} in collections++def test_product_query_by_id_available_as_customer(user_api_client, product, channel_USD):@@ -762,7 +872,7 @@ def test_product_query_unpublished_products_by_slug(assert product_data["name"] == product.name-def test_product_query_unpublished_products_by_slug_and_anonympus_user(+def test_product_query_unpublished_products_by_slug_and_anonymous_user(api_client, product, channel_USD):# givendiff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex 06bd9da9a4..36f6f1e8cc 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -64,6 +64,7 @@ from ...warehouse.dataloaders import (from ...warehouse.types import Stockfrom ..dataloaders import (CategoryByIdLoader,+ CollectionChannelListingByCollectionIdAndChannelSlugLoader,CollectionChannelListingByCollectionIdLoader,CollectionsByProductIdLoader,ImagesByProductIdLoader,@@ -727,17 +728,53 @@ class Product(ChannelContextTypeWithMetadata, CountableDjangoObjectType):return ProductChannelListingByProductIdLoader(info.context).load(root.node.id)@staticmethod- @permission_required(ProductPermissions.MANAGE_PRODUCTS)def resolve_collections(root: ChannelContext[models.Product], info, **_kwargs):- return (- CollectionsByProductIdLoader(info.context)- .load(root.node.id)- .then(- lambda collections: [+ requestor = get_user_or_app_from_context(info.context)+ requestor_has_access_to_all = models.Collection.objects.user_has_access_to_all(+ requestor+ )++ def return_collections(collections):+ if requestor_has_access_to_all:+ return [ChannelContext(node=collection, channel_slug=root.channel_slug)for collection in collections]++ dataloader_keys = [+ (collection.id, str(root.channel_slug)) for collection in collections+ ]+ CollectionChannelListingLoader = (+ CollectionChannelListingByCollectionIdAndChannelSlugLoader)+ channel_listings = CollectionChannelListingLoader(info.context).load_many(+ dataloader_keys+ )++ def return_visible_collections(channel_listings):+ visible_collections = []+ channel_listings_dict = {+ channel_listing.collection_id: channel_listing+ for channel_listing in channel_listings+ if channel_listing+ }++ for collection in collections:+ channel_listing = channel_listings_dict.get(collection.id)+ if channel_listing and channel_listing.is_visible:+ visible_collections.append(collection)++ return [+ ChannelContext(node=collection, channel_slug=root.channel_slug)+ for collection in visible_collections+ ]++ return channel_listings.then(return_visible_collections)++ return (+ CollectionsByProductIdLoader(info.context)+ .load(root.node.id)+ .then(return_collections))@staticmethoddiff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex 5999acafa4..3b8835b7c0 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -1102,6 +1102,14 @@ def product(product_type, category, warehouse, channel_USD):return product+def product_with_collections(+ product, published_collection, unpublished_collection, collection+):+ product.collections.add(*[published_collection, unpublished_collection, collection])+ return product++@pytest.fixturedef product_available_in_many_channels(product, channel_PLN):ProductChannelListing.objects.create(