Skip to content

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

Prevent from fetching unpublished collections by slug and anonymous user

IKarbowiak3839df75a31c
diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.py
index f61973ce0d..38d767c834 100644
--- a/saleor/graphql/product/resolvers.py
+++ b/saleor/graphql/product/resolvers.py
@@ -33,8 +33,11 @@ def resolve_categories(info, level=None, **_kwargs):
return qs.distinct()
-def resolve_collection_by_slug(slug):
- return models.Collection.objects.filter(slug=slug).first()
+def resolve_collection_by_slug(info, slug):
+ requestor = get_user_or_app_from_context(info.context)
+ return (
+ models.Collection.objects.visible_to_user(requestor).filter(slug=slug).first()
+ )
def resolve_collections(info, **_kwargs):
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index a938fb4052..7c3312f1e2 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -264,7 +264,7 @@ class ProductQueries(graphene.ObjectType):
if id:
return graphene.Node.get_node_from_global_id(info, id, Collection)
if slug:
- return resolve_collection_by_slug(slug=slug)
+ return resolve_collection_by_slug(info, slug=slug)
def resolve_collections(self, info, **kwargs):
return resolve_collections(info, **kwargs)
diff --git a/saleor/graphql/product/tests/test_collection.py b/saleor/graphql/product/tests/test_collection.py
index c5efe4accb..01b65edcaa 100644
--- a/saleor/graphql/product/tests/test_collection.py
+++ b/saleor/graphql/product/tests/test_collection.py
@@ -47,6 +47,23 @@ def test_collection_query_by_slug(
assert collection_data["name"] == collection.name
+def test_collection_query_unpublished_collection_by_slug_and_anonympus_user(
+ api_client, collection,
+):
+ # given
+ collection.is_published = False
+ collection.save(update_fields=["is_published"])
+ variables = {"slug": collection.slug}
+
+ # when
+ response = api_client.post_graphql(QUERY_COLLECTION, variables=variables)
+
+ # then
+ content = get_graphql_content(response)
+ collection_data = content["data"]["collection"]
+ assert collection_data is None
+
+
def test_collection_query_error_when_id_and_slug_provided(
user_api_client, collection, graphql_log_handler,
):