Skip to content

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

Test remove background image from collection and category

fowczarek479a4387729f
diff --git a/tests/api/test_category.py b/tests/api/test_category.py
index 45c6797557..2e3a7bc3a8 100644
--- a/tests/api/test_category.py
+++ b/tests/api/test_category.py
@@ -1,12 +1,13 @@
-import pytest
+from unittest.mock import Mock
import graphene
+import pytest
from django.template.defaultfilters import slugify
-from unittest.mock import Mock
+from graphql_relay import to_global_id
+from tests.api.utils import get_graphql_content, get_multipart_request_body
+from tests.utils import create_image
from saleor.product.models import Category
-from tests.utils import create_image
-from tests.api.utils import get_graphql_content, get_multipart_request_body
def test_category_query(user_api_client, product):
@@ -361,3 +362,37 @@ def test_category_image_query_without_associated_file(
data = content['data']['category']
assert data['name'] == category.name
assert data['backgroundImage'] is None
+
+
+def test_update_category_mutation_remove_background_image(
+ staff_api_client, category_with_image, permission_manage_products):
+ query = """
+ mutation updateCategory($id: ID!, $backgroundImage: Upload) {
+ categoryUpdate(
+ id: $id, input: {
+ backgroundImage: $backgroundImage
+ }
+ ) {
+ category {
+ backgroundImage{
+ url
+ }
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+ assert category_with_image.background_image
+ variables = {
+ 'id': to_global_id('Category', category_with_image.id),
+ 'backgroundImage': None}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products])
+ content = get_graphql_content(response)
+ data = content['data']['categoryUpdate']['category']
+ assert not data['backgroundImage']
+ category_with_image.refresh_from_db()
+ assert not category_with_image.background_image
diff --git a/tests/api/test_collection.py b/tests/api/test_collection.py
index 365320678e..49721e5044 100644
--- a/tests/api/test_collection.py
+++ b/tests/api/test_collection.py
@@ -1,11 +1,13 @@
+from unittest.mock import Mock
+
import graphene
import pytest
from django.utils.text import slugify
from graphql_relay import to_global_id
-from unittest.mock import Mock
+from tests.utils import create_image
from saleor.product.models import Collection
-from tests.utils import create_image
+
from .utils import get_graphql_content, get_multipart_request_body
@@ -173,7 +175,7 @@ def test_update_collection_with_background_image(
collectionUpdate(
id: $id, input: {
name: $name,
- slug: $slug,
+ slug: $slug,
backgroundImage: $backgroundImage,
isPublished: $isPublished
}
@@ -371,3 +373,36 @@ def test_collection_image_query_without_associated_file(
assert data['name'] == collection.name
assert data['backgroundImage'] is None
+
+def test_update_collection_mutation_remove_background_image(
+ staff_api_client, collection_with_image, permission_manage_products):
+ query = """
+ mutation updateCollection($id: ID!, $backgroundImage: Upload) {
+ collectionUpdate(
+ id: $id, input: {
+ backgroundImage: $backgroundImage
+ }
+ ) {
+ collection {
+ backgroundImage{
+ url
+ }
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+ assert collection_with_image.background_image
+ variables = {
+ 'id': to_global_id('Collection', collection_with_image.id),
+ 'backgroundImage': None}
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products])
+ content = get_graphql_content(response)
+ data = content['data']['collectionUpdate']['collection']
+ assert not data['backgroundImage']
+ collection_with_image.refresh_from_db()
+ assert not collection_with_image.background_image
diff --git a/tests/conftest.py b/tests/conftest.py
index 491ed17b25..8a4ffc425e 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -236,11 +236,25 @@ def size_attribute(db): # pylint: disable=W0613
return attribute
+def image():
+ img_data = BytesIO()
+ image = Image.new('RGB', size=(1, 1))
+ image.save(img_data, format='JPEG')
+ return SimpleUploadedFile('product.jpg', img_data.getvalue())
+
+
@pytest.fixture
def category(db): # pylint: disable=W0613
return Category.objects.create(name='Default', slug='default')
+def category_with_image(db, image): # pylint: disable=W0613
+ return Category.objects.create(
+ name='Default', slug='default', background_image=image)
+
+
@pytest.fixture
def categories_tree(db):
parent = Category.objects.create(name='Parent', slug='parent')
@@ -347,16 +361,8 @@ def order_list(customer_user):
@pytest.fixture
-def product_image():
- img_data = BytesIO()
- image = Image.new('RGB', size=(1, 1))
- image.save(img_data, format='JPEG')
- return SimpleUploadedFile('product.jpg', img_data.getvalue())
-
-
-def product_with_image(product, product_image):
- ProductImage.objects.create(product=product, image=product_image)
+def product_with_image(product, image):
+ ProductImage.objects.create(product=product, image=image)
return product
@@ -598,6 +604,14 @@ def collection(db):
return collection
+def collection_with_image(db, image):
+ collection = Collection.objects.create(
+ name='Collection', slug='collection', is_published=True,
+ description='Test description', background_image=image)
+ return collection
+
+
@pytest.fixture
def draft_collection(db):
collection = Collection.objects.create(
diff --git a/tests/test_core.py b/tests/test_core.py
index da524f6dc3..4c33b3cfb1 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -112,11 +112,11 @@ def test_create_address(db):
assert Address.objects.all().count() == 1
-def test_create_fake_order(db, monkeypatch, product_image):
+def test_create_fake_order(db, monkeypatch, image):
# Tests shouldn't depend on images present in placeholder folder
monkeypatch.setattr(
'saleor.core.utils.random_data.get_image',
- Mock(return_value=product_image))
+ Mock(return_value=image))
for _ in random_data.create_shipping_zones():
pass
for _ in random_data.create_users(3):