Skip to content

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

Raise an error when the user is trying to sort products by rank without search (#7013)

Iga Karbowiak5349bb58027f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a01c3846fa..373767fc1e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,7 +21,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add optional support for reporting resource limits via a stub field in `shop` - #6967 by @NyanKiyoshi
- Allow to use `Bearer` as an authorization prefix - #6996 by @korycins
- Update checkout quantity when checkout lines are deleted - #7002 by @IKarbowiak
-
+- Raise an error when the user is trying to sort products by rank without search - #7013 by @IKarbowiak
### Breaking
- Multichannel MVP: Multicurrency - #6242 by @fowczarek @d-wysocki
diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.py
index 685fba3dc0..0cf87aa3d4 100644
--- a/saleor/graphql/product/schema.py
+++ b/saleor/graphql/product/schema.py
@@ -1,4 +1,5 @@
import graphene
+from graphql.error import GraphQLError
from ...account.utils import requestor_is_staff_member_or_app
from ...core.permissions import ProductPermissions
@@ -105,6 +106,7 @@ from .sorters import (
CategorySortingInput,
CollectionSortingInput,
ProductOrder,
+ ProductOrderField,
ProductTypeSortingInput,
)
from .types import (
@@ -311,6 +313,17 @@ class ProductQueries(graphene.ObjectType):
return ChannelContext(node=product, channel_slug=channel) if product else None
def resolve_products(self, info, channel=None, **kwargs):
+ # sort by RANK can be used only with search filter
+ if "sort_by" in kwargs and ProductOrderField.RANK == kwargs["sort_by"].get(
+ "field"
+ ):
+ if (
+ "filter" not in kwargs
+ or kwargs["filter"].get("search") is None
+ or not kwargs["filter"]["search"].strip()
+ ):
+ raise GraphQLError("Sorting by Rank is available only with searching.")
+
requestor = get_user_or_app_from_context(info.context)
if channel is None and not requestor_is_staff_member_or_app(requestor):
channel = get_default_channel_slug_or_graphql_error()
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 0c1da5e288..1402999380 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -2440,26 +2440,31 @@ def test_create_product_description_plaintext(
assert product.description_plaintext == description
-def test_search_product_by_description(user_api_client, product_list, channel_USD):
- search_query = """
- query Products($filters: ProductFilterInput, $channel: String) {
- products(first: 5, filter: $filters, channel: $channel) {
- edges {
- node {
- id
- name
- }
+SEARCH_PRODUCTS_QUERY = """
+ query Products(
+ $filters: ProductFilterInput, $sortBy: ProductOrder, $channel: String
+ ) {
+ products(first: 5, filter: $filters, sortBy: $sortBy, channel: $channel) {
+ edges {
+ node {
+ id
+ name
+ }
+ }
}
- }
}
- """
+"""
+
+
+def test_search_product_by_description(user_api_client, product_list, channel_USD):
+
variables = {"filters": {"search": "big"}, "channel": channel_USD.slug}
- response = user_api_client.post_graphql(search_query, variables)
+ response = user_api_client.post_graphql(SEARCH_PRODUCTS_QUERY, variables)
content = get_graphql_content(response)
assert len(content["data"]["products"]["edges"]) == 2
variables = {"filters": {"search": "small"}, "channel": channel_USD.slug}
- response = user_api_client.post_graphql(search_query, variables)
+ response = user_api_client.post_graphql(SEARCH_PRODUCTS_QUERY, variables)
content = get_graphql_content(response)
assert len(content["data"]["products"]["edges"]) == 1
@@ -2468,21 +2473,6 @@ def test_search_product_by_description(user_api_client, product_list, channel_US
def test_search_product_by_description_and_name(
user_api_client, product_list, product, channel_USD, category, product_type
):
- search_query = """
- query Products(
- $filters: ProductFilterInput, $sortBy: ProductOrder,$channel: String
- ) {
- products(first: 10, filter: $filters, sortBy: $sortBy,channel: $channel) {
- edges {
- node {
- id
- name
- slug
- }
- }
- }
- }
- """
product.description_plaintext = "red big red product"
product.save()
@@ -2500,7 +2490,7 @@ def test_search_product_by_description_and_name(
"sortBy": {"field": "RANK", "direction": "DESC"},
"channel": channel_USD.slug,
}
- response = user_api_client.post_graphql(search_query, variables)
+ response = user_api_client.post_graphql(SEARCH_PRODUCTS_QUERY, variables)
content = get_graphql_content(response)
data = content["data"]["products"]["edges"]
@@ -2509,24 +2499,73 @@ def test_search_product_by_description_and_name(
assert data[2]["node"]["name"] == product_1.name
-def test_search_product_by_description_and_name_without_sort_by(
+def test_sort_product_by_rank_without_search(
user_api_client, product_list, product, channel_USD, category, product_type
):
- search_query = """
- query Products(
- $filters: ProductFilterInput, $sortBy: ProductOrder,$channel: String
- ) {
- products(first: 10, filter: $filters, sortBy: $sortBy,channel: $channel) {
- edges {
- node {
- id
- name
- slug
- }
- }
- }
+ product.description_plaintext = "red big red product"
+ product.save()
+
+ product_2 = product_list[1]
+ product_2.name = "red product"
+ product_2.save()
+ product_1 = product_list[0]
+ product_1.description_plaintext = "some red product"
+ product_1.save()
+
+ variables = {
+ "sortBy": {"field": "RANK", "direction": "DESC"},
+ "channel": channel_USD.slug,
}
- """
+ response = user_api_client.post_graphql(SEARCH_PRODUCTS_QUERY, variables)
+ with pytest.raises(AssertionError) as e:
+ get_graphql_content(response)
+
+ assert (
+ e._excinfo[1].args[0][0]["message"]
+ == "Sorting by Rank is available only with searching."
+ )
+
+
[email protected]("search_value", ["", " ", None])
+def test_sort_product_by_rank_with_empty_search_value(
+ search_value,
+ user_api_client,
+ product_list,
+ product,
+ channel_USD,
+ category,
+ product_type,
+):
+ product.description_plaintext = "red big red product"
+ product.save()
+
+ product_2 = product_list[1]
+ product_2.name = "red product"
+ product_2.save()
+ product_1 = product_list[0]
+ product_1.description_plaintext = "some red product"
+ product_1.save()
+
+ variables = {
+ "filters": {
+ "search": search_value,
+ },
+ "sortBy": {"field": "RANK", "direction": "DESC"},
+ "channel": channel_USD.slug,
+ }
+ response = user_api_client.post_graphql(SEARCH_PRODUCTS_QUERY, variables)
+ with pytest.raises(AssertionError) as e:
+ get_graphql_content(response)
+
+ assert (
+ e._excinfo[1].args[0][0]["message"]
+ == "Sorting by Rank is available only with searching."
+ )
+
+
+def test_search_product_by_description_and_name_without_sort_by(
+ user_api_client, product_list, product, channel_USD, category, product_type
+):
product.description_plaintext = "red big red product"
product.save()
@@ -2543,7 +2582,7 @@ def test_search_product_by_description_and_name_without_sort_by(
},
"channel": channel_USD.slug,
}
- response = user_api_client.post_graphql(search_query, variables)
+ response = user_api_client.post_graphql(SEARCH_PRODUCTS_QUERY, variables)
content = get_graphql_content(response)
data = content["data"]["products"]["edges"]