Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Handle pagination with invalid cursor that is valid base64 (#9521)
Jakub3c12a1e95ab9
diff --git a/saleor/graphql/core/connection.py b/saleor/graphql/core/connection.pyindex 892adaf807..cda2ef5087 100644--- a/saleor/graphql/core/connection.py+++ b/saleor/graphql/core/connection.py@@ -235,7 +235,10 @@ def connection_from_queryset_slice(filter_kwargs = (_prepare_filter(cursor, sorting_fields, sorting_direction) if cursor else Q())- filtered_qs = qs.filter(filter_kwargs)+ try:+ filtered_qs = qs.filter(filter_kwargs)+ except ValueError:+ raise GraphQLError("Received cursor is invalid.")filtered_qs = filtered_qs[:end_margin]edges, page_info = _get_edges_for_connection(edge_type, filtered_qs, args, sorting_fieldsdiff --git a/saleor/graphql/core/tests/test_pagination.py b/saleor/graphql/core/tests/test_pagination.pyindex aba886c530..cc98e9e4ce 100644--- a/saleor/graphql/core/tests/test_pagination.py+++ b/saleor/graphql/core/tests/test_pagination.py@@ -1,3 +1,4 @@+import base64import mathimport graphene@@ -239,6 +240,17 @@ def test_pagination_invalid_cursor(books):assert str(result.errors[0]) == "Received cursor is invalid."+def test_pagination_invalid_cursor_and_valid_base64(books):+ """This cursor should have int value, in this test we pass string value."""+ cursor = base64.b64encode(str.encode(f"{['Test']}")).decode("utf-8")+ variables = {"first": 5, "after": cursor}++ result = schema.execute(QUERY_PAGINATION_TEST, variables=variables)++ assert len(result.errors) == 1+ assert str(result.errors[0]) == "Received cursor is invalid."++QUERY_PAGINATION_WITH_FRAGMENTS = """fragment BookFragment on BookType {name