Repository: saleor/saleor

Issue:
Field `slug` should not be required in collectionCreate mutation
It should be possible to create a collection with an automatically generated slug based on its name. Unfortunately, at this moment this field is required.


Discussion:
Other places should be checked as well, as I think it happens in more places, like eg. categories

Resolving patch:
diff --git a/saleor/graphql/page/mutations.py b/saleor/graphql/page/mutations.py
--- a/saleor/graphql/page/mutations.py
+++ b/saleor/graphql/page/mutations.py
@@ -1,4 +1,5 @@
 import graphene
+from django.utils.text import slugify
 
 from ...page import models
 from ..core.mutations import ModelDeleteMutation, ModelMutation
@@ -35,6 +36,8 @@ def user_is_allowed(cls, user, input):
     @classmethod
     def clean_input(cls, info, instance, input, errors):
         cleaned_input = super().clean_input(info, instance, input, errors)
+        if 'slug' not in cleaned_input:
+            cleaned_input['slug'] = slugify(cleaned_input['name'])
         clean_seo_fields(cleaned_input)
         return cleaned_input
 
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -107,6 +107,8 @@ def user_is_allowed(cls, user, input):
     @classmethod
     def clean_input(cls, info, instance, input, errors):
         cleaned_input = super().clean_input(info, instance, input, errors)
+        if 'slug' not in cleaned_input:
+            cleaned_input['slug'] = slugify(cleaned_input['name'])
         clean_seo_fields(cleaned_input)
         return cleaned_input
 


Test patch:
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -1,16 +1,16 @@
 from unittest.mock import MagicMock, Mock
 
-import pytest
-
 import graphene
+import pytest
 from django.utils.text import slugify
 from graphql_relay import to_global_id
 from prices import Money
+from tests.api.utils import get_graphql_content
+from tests.utils import create_image, create_pdf_file_with_image_ext
+
 from saleor.graphql.product.utils import update_variants_names
 from saleor.product.models import (
     Category, Collection, Product, ProductImage, ProductType, ProductVariant)
-from tests.api.utils import get_graphql_content
-from tests.utils import create_image, create_pdf_file_with_image_ext
 
 from .utils import assert_no_permission, get_multipart_request_body
 
@@ -1018,6 +1018,30 @@ def test_delete_collection(
         collection.refresh_from_db()
 
 
+def test_auto_create_slug_on_collection(
+        staff_api_client, product_list, permission_manage_products):
+    query = """
+        mutation createCollection(
+            $name: String!, $isPublished: Boolean!) {
+            collectionCreate(
+                input: {name: $name, isPublished: $isPublished}) {
+                collection {
+                    name
+                    slug
+                }
+            }
+        }
+    """
+    name = 'test name123'
+    variables = {'name': name, 'isPublished': True}
+    response = staff_api_client.post_graphql(
+        query, variables, permissions=[permission_manage_products])
+    content = get_graphql_content(response)
+    data = content['data']['collectionCreate']['collection']
+    assert data['name'] == name
+    assert data['slug'] == slugify(name)
+
+
 def test_add_products_to_collection(
         staff_api_client, collection, product_list,
         permission_manage_products):