Skip to content

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

Review changes

Michał Sipad05a703212b3
diff --git a/saleor/graphql/core/validators.py b/saleor/graphql/core/validators.py
index 035c464b81..44436fcddf 100644
--- a/saleor/graphql/core/validators.py
+++ b/saleor/graphql/core/validators.py
@@ -1,12 +1,12 @@
from graphql.error import GraphQLError
-def validate_query_args(**kwargs):
- id = kwargs.get("id")
- slug = kwargs.get("slug")
- name = kwargs.get("name")
-
- if id and slug:
- raise GraphQLError("Argument 'id' cannot be combined with 'slug'")
- if id and name:
- raise GraphQLError("Argument 'id' cannot be combined with 'name'")
+def validate_one_of_args_is_in_query(arg1_name, arg1, arg2_name, arg2):
+ if arg1 and arg2:
+ raise GraphQLError(
+ f"Argument '{arg1_name}' cannot be combined with '{arg2_name}'"
+ )
+ if not arg1 and not arg2:
+ raise GraphQLError(
+ f"Either '{arg1_name}' or '{arg2_name}' argument is required"
+ )
diff --git a/saleor/graphql/menu/resolvers.py b/saleor/graphql/menu/resolvers.py
index 08362f5bb2..ba8e80e4ec 100644
--- a/saleor/graphql/menu/resolvers.py
+++ b/saleor/graphql/menu/resolvers.py
@@ -1,8 +1,7 @@
import graphene
-from graphql.error import GraphQLError
from ...menu import models
-from ..core.validators import validate_query_args
+from ..core.validators import validate_one_of_args_is_in_query
from ..utils.filters import filter_by_query_param
from .types import Menu
@@ -11,12 +10,11 @@ MENU_ITEM_SEARCH_FIELDS = ("name",)
def resolve_menu(info, menu_id=None, name=None):
- validate_query_args(id=menu_id, name=name)
+ validate_one_of_args_is_in_query("id", menu_id, "name", name)
if menu_id:
return graphene.Node.get_node_from_global_id(info, menu_id, Menu)
if name:
return models.Menu.objects.filter(name=name).first()
- raise GraphQLError("Either 'id' or 'slug' argument is required")
def resolve_menus(info, query, **_kwargs):
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index 6c7b414382..08ffe07972 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -1,10 +1,9 @@
import graphene
-from graphql.error import GraphQLError
from ...core.permissions import ProductPermissions
from ..core.enums import ReportingPeriod
from ..core.fields import FilterInputConnectionField, PrefetchingConnectionField
-from ..core.validators import validate_query_args
+from ..core.validators import validate_one_of_args_is_in_query
from ..decorators import permission_required
from ..translations.mutations import (
AttributeTranslate,
@@ -254,20 +253,18 @@ class ProductQueries(graphene.ObjectType):
return resolve_categories(info, level=level, **kwargs)
def resolve_category(self, info, id=None, slug=None):
- validate_query_args(id=id, slug=slug)
+ validate_one_of_args_is_in_query("id", id, "slug", slug)
if id:
return graphene.Node.get_node_from_global_id(info, id, Category)
if slug:
return resolve_category_by_slug(slug=slug)
- raise GraphQLError("Either 'id' or 'slug' argument is required")
def resolve_collection(self, info, id=None, slug=None):
- validate_query_args(id=id, slug=slug)
+ validate_one_of_args_is_in_query("id", id, "slug", slug)
if id:
return graphene.Node.get_node_from_global_id(info, id, Collection)
if slug:
return resolve_collection_by_slug(slug=slug)
- raise GraphQLError("Either 'id' or 'slug' argument is required")
def resolve_collections(self, info, **kwargs):
return resolve_collections(info, **kwargs)
@@ -281,12 +278,11 @@ class ProductQueries(graphene.ObjectType):
return resolve_digital_contents(info)
def resolve_product(self, info, id=None, slug=None):
- validate_query_args(id=id, slug=slug)
+ validate_one_of_args_is_in_query("id", id, "slug", slug)
if id:
return graphene.Node.get_node_from_global_id(info, id, Product)
if slug:
return resolve_product_by_slug(slug=slug)
- raise GraphQLError("Either 'id' or 'slug' argument is required")
def resolve_products(self, info, **kwargs):
return resolve_products(info, **kwargs)
diff --git a/tests/api/test_category.py b/tests/api/test_category.py
index bd230e7b48..3b602e606b 100644
--- a/tests/api/test_category.py
+++ b/tests/api/test_category.py
@@ -8,65 +8,91 @@ from graphql_relay import to_global_id
from saleor.product.error_codes import ProductErrorCode
from saleor.product.models import Category
-from tests.api.utils import (
- construct_query_input,
- get_graphql_content,
- get_multipart_request_body,
-)
+from tests.api.utils import get_graphql_content, get_multipart_request_body
from tests.utils import create_image, create_pdf_file_with_image_ext
-
- "arguments, expected_error",
- ((["id"], False), (["slug"], False), ([], True), (["id", "slug"], True)),
-)
-def test_category_query(
- arguments, expected_error, user_api_client, product, graphql_log_handler
-):
- category = Category.objects.first()
- query_input = construct_query_input(arguments=arguments, obj=category)
- query = f"""
- query {{
- category{query_input} {{
+QUERY_CATEGORY = """
+ query ($id: ID, $slug: String){
+ category(
+ id: $id,
+ slug: $slug,
+ ) {
id
name
- ancestors(first: 20) {{
- edges {{
- node {{
+ ancestors(first: 20) {
+ edges {
+ node {
name
- }}
- }}
- }}
- children(first: 20) {{
- edges {{
- node {{
+ }
+ }
+ }
+ children(first: 20) {
+ edges {
+ node {
name
- }}
- }}
- }}
- }}
- }}
+ }
+ }
+ }
+ }
+ }
"""
- if expected_error:
- response = user_api_client.post_graphql(query)
- assert graphql_log_handler.messages == [
- "saleor.graphql.errors.handled[ERROR].GraphQLError"
- ]
- content = get_graphql_content(response, ignore_errors=True)
- assert len(content["errors"]) == 1
- else:
- response = user_api_client.post_graphql(query)
- content = get_graphql_content(response)
- category_data = content["data"]["category"]
- assert category_data is not None
- assert category_data["name"] == category.name
- assert (
- len(category_data["ancestors"]["edges"]) == category.get_ancestors().count()
- )
- assert (
- len(category_data["children"]["edges"]) == category.get_children().count()
- )
+
+def test_category_query_by_id(
+ user_api_client, product,
+):
+ category = Category.objects.first()
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ response = user_api_client.post_graphql(QUERY_CATEGORY, variables=variables)
+ content = get_graphql_content(response)
+ category_data = content["data"]["category"]
+ assert category_data is not None
+ assert category_data["name"] == category.name
+ assert len(category_data["ancestors"]["edges"]) == category.get_ancestors().count()
+ assert len(category_data["children"]["edges"]) == category.get_children().count()
+
+
+def test_category_query_by_slug(
+ user_api_client, product,
+):
+ category = Category.objects.first()
+ variables = {"slug": category.slug}
+ response = user_api_client.post_graphql(QUERY_CATEGORY, variables=variables)
+ content = get_graphql_content(response)
+ category_data = content["data"]["category"]
+ assert category_data is not None
+ assert category_data["name"] == category.name
+ assert len(category_data["ancestors"]["edges"]) == category.get_ancestors().count()
+ assert len(category_data["children"]["edges"]) == category.get_children().count()
+
+
+def test_category_query_error_when_id_and_slug_provided(
+ user_api_client, product, graphql_log_handler,
+):
+ category = Category.objects.first()
+ variables = {
+ "id": graphene.Node.to_global_id("Category", category.pk),
+ "slug": category.slug,
+ }
+ response = user_api_client.post_graphql(QUERY_CATEGORY, variables=variables)
+ assert graphql_log_handler.messages == [
+ "saleor.graphql.errors.handled[ERROR].GraphQLError"
+ ]
+ content = get_graphql_content(response, ignore_errors=True)
+ assert len(content["errors"]) == 1
+
+
+def test_category_query_error_when_no_param(
+ user_api_client, product, graphql_log_handler,
+):
+ variables = {}
+ response = user_api_client.post_graphql(QUERY_CATEGORY, variables=variables)
+ assert graphql_log_handler.messages == [
+ "saleor.graphql.errors.handled[ERROR].GraphQLError"
+ ]
+ content = get_graphql_content(response, ignore_errors=True)
+ assert len(content["errors"]) == 1
def test_category_create_mutation(
diff --git a/tests/api/test_collection.py b/tests/api/test_collection.py
index 65230d0371..0abe6f87bd 100644
--- a/tests/api/test_collection.py
+++ b/tests/api/test_collection.py
@@ -10,43 +10,69 @@ from saleor.product.error_codes import ProductErrorCode
from saleor.product.models import Collection
from tests.utils import create_image, create_pdf_file_with_image_ext
-from .utils import (
- construct_query_input,
- get_graphql_content,
- get_multipart_request_body,
-)
-
+from .utils import get_graphql_content, get_multipart_request_body
- "arguments, expected_error",
- ((["id"], False), (["slug"], False), ([], True), (["id", "slug"], True)),
-)
-def test_collection_query(
- arguments, expected_error, user_api_client, collection, graphql_log_handler
-):
- query_input = construct_query_input(arguments=arguments, obj=collection)
- query = f"""
- query {{
- collection{query_input} {{
+QUERY_COLLECTION = """
+ query ($id: ID, $slug: String){
+ collection(
+ id: $id,
+ slug: $slug,
+ ) {
id
name
- }}
- }}
+ }
+ }
"""
- if expected_error:
- response = user_api_client.post_graphql(query)
- assert graphql_log_handler.messages == [
- "saleor.graphql.errors.handled[ERROR].GraphQLError"
- ]
- content = get_graphql_content(response, ignore_errors=True)
- assert len(content["errors"]) == 1
- else:
- response = user_api_client.post_graphql(query)
- content = get_graphql_content(response)
- collection_data = content["data"]["collection"]
- assert collection_data is not None
- assert collection_data["name"] == collection.name
+
+def test_collection_query_by_id(
+ user_api_client, collection,
+):
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.pk)}
+
+ response = user_api_client.post_graphql(QUERY_COLLECTION, variables=variables)
+ content = get_graphql_content(response)
+ collection_data = content["data"]["collection"]
+ assert collection_data is not None
+ assert collection_data["name"] == collection.name
+
+
+def test_collection_query_by_slug(
+ user_api_client, collection,
+):
+ variables = {"slug": collection.slug}
+ response = user_api_client.post_graphql(QUERY_COLLECTION, variables=variables)
+ content = get_graphql_content(response)
+ collection_data = content["data"]["collection"]
+ assert collection_data is not None
+ assert collection_data["name"] == collection.name
+
+
+def test_collection_query_error_when_id_and_slug_provided(
+ user_api_client, collection, graphql_log_handler,
+):
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", collection.pk),
+ "slug": collection.slug,
+ }
+ response = user_api_client.post_graphql(QUERY_COLLECTION, variables=variables)
+ assert graphql_log_handler.messages == [
+ "saleor.graphql.errors.handled[ERROR].GraphQLError"
+ ]
+ content = get_graphql_content(response, ignore_errors=True)
+ assert len(content["errors"]) == 1
+
+
+def test_collection_query_error_when_no_param(
+ user_api_client, collection, graphql_log_handler,
+):
+ variables = {}
+ response = user_api_client.post_graphql(QUERY_COLLECTION, variables=variables)
+ assert graphql_log_handler.messages == [
+ "saleor.graphql.errors.handled[ERROR].GraphQLError"
+ ]
+ content = get_graphql_content(response, ignore_errors=True)
+ assert len(content["errors"]) == 1
def test_collections_query(
diff --git a/tests/api/test_menu.py b/tests/api/test_menu.py
index a6af126768..16dc091e06 100644
--- a/tests/api/test_menu.py
+++ b/tests/api/test_menu.py
@@ -9,7 +9,7 @@ from saleor.menu.models import Menu, MenuItem
from saleor.product.models import Category
from tests.api.utils import get_graphql_content
-from .utils import assert_no_permission, construct_query_input, menu_item_to_json
+from .utils import assert_no_permission, menu_item_to_json
def test_validate_menu_item_instance(category, page):
@@ -22,36 +22,67 @@ def test_validate_menu_item_instance(category, page):
_validate_menu_item_instance({"category": None}, "category", Category)
- "arguments, expected_error",
- ((["id"], False), (["name"], False), ([], True), (["id", "name"], True)),
-)
-def test_collection_query(
- arguments, expected_error, user_api_client, menu, graphql_log_handler
-):
- query_input = construct_query_input(arguments=arguments, obj=menu)
- query = f"""
- query {{
- menu{query_input} {{
+QUERY_MENU = """
+ query ($id: ID, $name: String){
+ menu(
+ id: $id,
+ name: $name,
+ ) {
id
name
- }}
- }}
+ }
+ }
"""
- if expected_error:
- response = user_api_client.post_graphql(query)
- assert graphql_log_handler.messages == [
- "saleor.graphql.errors.handled[ERROR].GraphQLError"
- ]
- content = get_graphql_content(response, ignore_errors=True)
- assert len(content["errors"]) == 1
- else:
- response = user_api_client.post_graphql(query)
- content = get_graphql_content(response)
- menu_data = content["data"]["menu"]
- assert menu_data is not None
- assert menu_data["name"] == menu.name
+
+def test_menu_query_by_id(
+ user_api_client, menu,
+):
+ variables = {"id": graphene.Node.to_global_id("Menu", menu.pk)}
+
+ response = user_api_client.post_graphql(QUERY_MENU, variables=variables)
+ content = get_graphql_content(response)
+ menu_data = content["data"]["menu"]
+ assert menu_data is not None
+ assert menu_data["name"] == menu.name
+
+
+def test_menu_query_by_name(
+ user_api_client, menu,
+):
+ variables = {"name": menu.name}
+ response = user_api_client.post_graphql(QUERY_MENU, variables=variables)
+ content = get_graphql_content(response)
+ menu_data = content["data"]["menu"]
+ assert menu_data is not None
+ assert menu_data["name"] == menu.name
+
+
+def test_menu_query_error_when_id_and_name_provided(
+ user_api_client, menu, graphql_log_handler,
+):
+ variables = {
+ "id": graphene.Node.to_global_id("Menu", menu.pk),
+ "name": menu.name,
+ }
+ response = user_api_client.post_graphql(QUERY_MENU, variables=variables)
+ assert graphql_log_handler.messages == [
+ "saleor.graphql.errors.handled[ERROR].GraphQLError"
+ ]
+ content = get_graphql_content(response, ignore_errors=True)
+ assert len(content["errors"]) == 1
+
+
+def test_menu_query_error_when_no_param(
+ user_api_client, menu, graphql_log_handler,
+):
+ variables = {}
+ response = user_api_client.post_graphql(QUERY_MENU, variables=variables)
+ assert graphql_log_handler.messages == [
+ "saleor.graphql.errors.handled[ERROR].GraphQLError"
+ ]
+ content = get_graphql_content(response, ignore_errors=True)
+ assert len(content["errors"]) == 1
def test_menu_query(user_api_client, menu):
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 3e4175c9ad..9589008add 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -34,11 +34,7 @@ from saleor.warehouse.models import Allocation, Stock, Warehouse
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,
- construct_query_input,
- get_multipart_request_body,
-)
+from .utils import assert_no_permission, get_multipart_request_body
@pytest.fixture
@@ -108,36 +104,67 @@ QUERY_FETCH_ALL_PRODUCTS = """
"""
- "arguments, expected_error",
- ((["id"], False), (["slug"], False), ([], True), (["id", "slug"], True)),
-)
-def test_collection_query(
- arguments, expected_error, user_api_client, product, graphql_log_handler
-):
- query_input = construct_query_input(arguments=arguments, obj=product)
- query = f"""
- query {{
- product{query_input} {{
+QUERY_PRODUCT = """
+ query ($id: ID, $slug: String){
+ product(
+ id: $id,
+ slug: $slug,
+ ) {
id
name
- }}
- }}
+ }
+ }
"""
- if expected_error:
- response = user_api_client.post_graphql(query)
- assert graphql_log_handler.messages == [
- "saleor.graphql.errors.handled[ERROR].GraphQLError"
- ]
- content = get_graphql_content(response, ignore_errors=True)
- assert len(content["errors"]) == 1
- else:
- response = user_api_client.post_graphql(query)
- content = get_graphql_content(response)
- product_data = content["data"]["product"]
- assert product_data is not None
- assert product_data["name"] == product.name
+
+def test_product_query_by_id(
+ user_api_client, product,
+):
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ response = user_api_client.post_graphql(QUERY_PRODUCT, variables=variables)
+ content = get_graphql_content(response)
+ collection_data = content["data"]["product"]
+ assert collection_data is not None
+ assert collection_data["name"] == product.name
+
+
+def test_product_query_by_slug(
+ user_api_client, product,
+):
+ variables = {"slug": product.slug}
+ response = user_api_client.post_graphql(QUERY_PRODUCT, variables=variables)
+ content = get_graphql_content(response)
+ collection_data = content["data"]["product"]
+ assert collection_data is not None
+ assert collection_data["name"] == product.name
+
+
+def test_product_query_error_when_id_and_slug_provided(
+ user_api_client, product, graphql_log_handler,
+):
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "slug": product.slug,
+ }
+ response = user_api_client.post_graphql(QUERY_PRODUCT, variables=variables)
+ assert graphql_log_handler.messages == [
+ "saleor.graphql.errors.handled[ERROR].GraphQLError"
+ ]
+ content = get_graphql_content(response, ignore_errors=True)
+ assert len(content["errors"]) == 1
+
+
+def test_product_query_error_when_no_param(
+ user_api_client, product, graphql_log_handler,
+):
+ variables = {}
+ response = user_api_client.post_graphql(QUERY_PRODUCT, variables=variables)
+ assert graphql_log_handler.messages == [
+ "saleor.graphql.errors.handled[ERROR].GraphQLError"
+ ]
+ content = get_graphql_content(response, ignore_errors=True)
+ assert len(content["errors"]) == 1
def test_fetch_all_products(user_api_client, product):
diff --git a/tests/api/utils.py b/tests/api/utils.py
index 40fe14547d..129dbe32e8 100644
--- a/tests/api/utils.py
+++ b/tests/api/utils.py
@@ -1,7 +1,5 @@
import json
-from typing import List
-import graphene
from django.core.serializers.json import DjangoJSONEncoder
from saleor.graphql.core.utils import snake_to_camel_case
@@ -64,22 +62,3 @@ def menu_item_to_json(menu_item):
item_json = get_menu_item_as_dict(menu_item)
item_json["child_items"] = []
return item_json
-
-
-def construct_query_input(arguments: List, obj: object) -> str:
- obj_pk = graphene.Node.to_global_id(obj.__class__.__name__, obj.pk)
- id_arg = ""
- slug_arg = ""
- name_arg = ""
- if "id" in arguments:
- id_arg = f'id: "{obj_pk}",'
- if "slug" in arguments:
- slug_arg = f'slug: "{obj.slug}",'
- if "name" in arguments:
- name_arg = f'name: "{obj.name}",'
-
- query_input = ""
- if arguments:
- query_input = f"({id_arg} {slug_arg} {name_arg})"
-
- return query_input