Skip to content

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

Add test for invalid file type in categoty background image

fowczareka8e0746c8567
diff --git a/tests/api/test_category.py b/tests/api/test_category.py
index 7d41dc03e2..35319d81c2 100644
--- a/tests/api/test_category.py
+++ b/tests/api/test_category.py
@@ -4,10 +4,10 @@ import graphene
import pytest
from django.template.defaultfilters import slugify
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, create_pdf_file_with_image_ext
from saleor.product.models import Category
-from tests.api.utils import get_graphql_content, get_multipart_request_body
-from tests.utils import create_image
def test_category_query(user_api_client, product):
@@ -165,39 +165,40 @@ def test_category_create_mutation_without_background_image(
assert mock_create_thumbnails.call_count == 0
-def test_category_update_mutation(
- monkeypatch, staff_api_client, category, permission_manage_products):
- query = """
- mutation($id: ID!, $name: String, $slug: String, $backgroundImage: Upload, $backgroundImageAlt: String, $description: String) {
- categoryUpdate(
- id: $id
- input: {
- name: $name
- description: $description
- backgroundImage: $backgroundImage
- backgroundImageAlt: $backgroundImageAlt
- slug: $slug
- }
- ) {
- category {
+MUTATION_CATEGORY_UPDATE_MUTATION = """
+ mutation($id: ID!, $name: String, $slug: String, $backgroundImage: Upload, $backgroundImageAlt: String, $description: String) {
+ categoryUpdate(
+ id: $id
+ input: {
+ name: $name
+ description: $description
+ backgroundImage: $backgroundImage
+ backgroundImageAlt: $backgroundImageAlt
+ slug: $slug
+ }
+ ) {
+ category {
+ id
+ name
+ description
+ parent {
id
- name
- description
- parent {
- id
- }
- backgroundImage{
- alt
- }
}
- errors {
- field
- message
+ backgroundImage{
+ alt
}
}
+ errors {
+ field
+ message
+ }
}
+ }
"""
+
+def test_category_update_mutation(
+ monkeypatch, staff_api_client, category, permission_manage_products):
mock_create_thumbnails = Mock(return_value=None)
monkeypatch.setattr(
('saleor.dashboard.category.forms.'
@@ -219,7 +220,8 @@ def test_category_update_mutation(
'name': category_name, 'description': category_description,
'backgroundImage': image_name, 'backgroundImageAlt': image_alt,
'id': category_id, 'slug': category_slug}
- body = get_multipart_request_body(query, variables, image_file, image_name)
+ body = get_multipart_request_body(
+ MUTATION_CATEGORY_UPDATE_MUTATION, variables, image_file, image_name)
response = staff_api_client.post_multipart(
body, permissions=[permission_manage_products])
content = get_graphql_content(response)
@@ -237,6 +239,28 @@ def test_category_update_mutation(
assert data['category']['backgroundImage']['alt'] == image_alt
+def test_category_update_mutation_invalid_background_image(
+ staff_api_client, category, permission_manage_products):
+ image_file, image_name = create_pdf_file_with_image_ext()
+ image_alt = 'Alt text for an image.'
+ variables = {
+ 'name': 'new-name',
+ 'slug': 'new-slug',
+ 'id': to_global_id('Category', category.id),
+ 'backgroundImage': image_name,
+ 'backgroundImageAlt': image_alt,
+ 'isPublished': True}
+ body = get_multipart_request_body(
+ MUTATION_CATEGORY_UPDATE_MUTATION, variables,
+ image_file, image_name)
+ response = staff_api_client.post_multipart(
+ body, permissions=[permission_manage_products])
+ content = get_graphql_content(response)
+ data = content['data']['categoryUpdate']
+ assert data['errors'][0]['field'] == 'image'
+ assert data['errors'][0]['message'] == 'Invalid file type'
+
+
def test_category_update_mutation_without_background_image(
monkeypatch, staff_api_client, category, permission_manage_products):
query = """
@@ -305,19 +329,19 @@ def test_category_delete_mutation(
LEVELED_CATEGORIES_QUERY = """
-query leveled_categories($level: Int) {
- categories(level: $level, first: 20) {
- edges {
- node {
- name
- parent {
+ query leveled_categories($level: Int) {
+ categories(level: $level, first: 20) {
+ edges {
+ node {
name
+ parent {
+ name
+ }
}
}
}
}
-}
-"""
+ """
def test_category_level(user_api_client, category):
@@ -340,16 +364,16 @@ def test_category_level(user_api_client, category):
FETCH_CATEGORY_QUERY = """
-query fetchCategory($id: ID!){
- category(id: $id) {
- name
- backgroundImage(size: 120) {
- url
- alt
+ query fetchCategory($id: ID!){
+ category(id: $id) {
+ name
+ backgroundImage(size: 120) {
+ url
+ alt
+ }
}
}
-}
-"""
+ """
def test_category_image_query(user_api_client, non_default_category):