Skip to content

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

Fix search bug: 'NoneType' object has no attribute 'strip' (#13937)

Krzysztof Waliczek8c8d9db54d63
diff --git a/saleor/graphql/order/schema.py b/saleor/graphql/order/schema.py
index ae749abf29..79454c74be 100644
--- a/saleor/graphql/order/schema.py
+++ b/saleor/graphql/order/schema.py
@@ -68,7 +68,8 @@ from .types import Order, OrderCountableConnection, OrderEventCountableConnectio
def search_string_in_kwargs(kwargs: dict) -> bool:
- return bool(kwargs.get("filter", {}).get("search", "").strip())
+ filter_search = kwargs.get("filter", {}).get("search", "") or ""
+ return bool(filter_search.strip())
def sort_field_from_kwargs(kwargs: dict) -> Optional[List[str]]:
diff --git a/saleor/graphql/order/tests/queries/test_order_with_sort.py b/saleor/graphql/order/tests/queries/test_order_with_sort.py
index b45f2669ab..b8e9663cfd 100644
--- a/saleor/graphql/order/tests/queries/test_order_with_sort.py
+++ b/saleor/graphql/order/tests/queries/test_order_with_sort.py
@@ -132,3 +132,26 @@ def test_sort_order_by_rank_without_search(
content["errors"][0]["message"]
== "Sorting by RANK is available only when using a search filter."
)
+
+
+def test_sort_order_by_rank_with_nonetype_search(
+ staff_api_client, permission_group_manage_orders
+):
+ # given
+ permission_group_manage_orders.user_set.add(staff_api_client.user)
+
+ variables = {
+ "sortBy": {"field": "RANK", "direction": "DESC"},
+ "search": None,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(SEARCH_ORDERS_QUERY, variables)
+
+ # then
+ content = get_graphql_content(response, ignore_errors=True)
+
+ errors = content["errors"]
+ expected_message = "Sorting by RANK is available only when using a search filter."
+ assert len(errors) == 1
+ assert errors[0]["message"] == expected_message
diff --git a/saleor/graphql/product/tests/queries/test_products_query.py b/saleor/graphql/product/tests/queries/test_products_query.py
index e7622c9c95..79e07015ef 100644
--- a/saleor/graphql/product/tests/queries/test_products_query.py
+++ b/saleor/graphql/product/tests/queries/test_products_query.py
@@ -869,6 +869,79 @@ def test_sort_product_by_rank_without_search(
assert content["errors"][0]["message"] == message
+def test_products_query_by_rank_returns_error_with_filter_nontype_search(
+ staff_api_client,
+ channel_USD,
+):
+ # given
+ variables = {
+ "filters": {"search": None},
+ "sortBy": {"field": "RANK", "direction": "DESC"},
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ SEARCH_PRODUCTS_QUERY,
+ variables=variables,
+ )
+
+ # then
+ content = get_graphql_content(response, ignore_errors=True)
+
+ assert "errors" in content
+ errors = content["errors"]
+ expected_message = (
+ "Sorting by RANK is available only when using a search filter "
+ "or search argument."
+ )
+ assert len(errors) == 1
+ assert errors[0]["message"] == expected_message
+
+
+def test_products_query_by_rank_returns_error_with_nontype_search(
+ staff_api_client,
+ channel_USD,
+):
+ # given
+ query = """
+ query($search: String, $sortBy: ProductOrder, $channel: String) {
+ products(first: 5, search: $search, sortBy: $sortBy, channel: $channel) {
+ edges {
+ node {
+ name
+ slug
+ }
+ }
+ }
+ }
+ """
+
+ variables = {
+ "search": None,
+ "sortBy": {"field": "RANK", "direction": "DESC"},
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query,
+ variables=variables,
+ )
+
+ # then
+ content = get_graphql_content(response, ignore_errors=True)
+
+ assert "errors" in content
+ errors = content["errors"]
+ expected_message = (
+ "Sorting by RANK is available only when using a search filter "
+ "or search argument."
+ )
+ assert len(errors) == 1
+ assert errors[0]["message"] == expected_message
+
+
def test_search_product_by_description_and_name_without_sort_by(
user_api_client, product_list, product, channel_USD
):
diff --git a/saleor/graphql/product/utils.py b/saleor/graphql/product/utils.py
index 6e106e77a5..5ff678ad54 100644
--- a/saleor/graphql/product/utils.py
+++ b/saleor/graphql/product/utils.py
@@ -146,9 +146,9 @@ def update_ordered_media(ordered_media):
def search_string_in_kwargs(kwargs: dict) -> bool:
- return bool(kwargs.get("filter", {}).get("search", "").strip()) or bool(
- kwargs.get("search", "").strip()
- )
+ filter_search = kwargs.get("filter", {}).get("search", "") or ""
+ search = kwargs.get("search", "") or ""
+ return bool(filter_search.strip()) or bool(search.strip())
def sort_field_from_kwargs(kwargs: dict) -> Optional[List[str]]: