Skip to content

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

Validate type of id passed to transaction query

Ania Warzecha79547572b234
diff --git a/saleor/graphql/payment/schema.py b/saleor/graphql/payment/schema.py
index 36479f749a..21e91c6400 100644
--- a/saleor/graphql/payment/schema.py
+++ b/saleor/graphql/payment/schema.py
@@ -100,7 +100,9 @@ class PaymentQueries(graphene.ObjectType):
# If token is provided we ignore the id input.
if token:
return resolve_transaction(info, str(token))
- _, id = from_global_id_or_error(id, TransactionItem) # type: ignore[arg-type]
+ _, id = from_global_id_or_error(
+ global_id=id, only_type=TransactionItem, raise_error=True
+ ) # type: ignore[arg-type]
return resolve_transaction(info, id)
diff --git a/saleor/graphql/payment/tests/queries/test_transaction.py b/saleor/graphql/payment/tests/queries/test_transaction.py
index cd433a3ddd..f3a8dc6d1a 100644
--- a/saleor/graphql/payment/tests/queries/test_transaction.py
+++ b/saleor/graphql/payment/tests/queries/test_transaction.py
@@ -8,7 +8,11 @@ from freezegun import freeze_time
from .....payment import TransactionEventType
from .....payment.models import TransactionEvent
from ....core.utils import to_global_id_or_none
-from ....tests.utils import assert_no_permission, get_graphql_content
+from ....tests.utils import (
+ assert_no_permission,
+ get_graphql_content,
+ get_graphql_content_from_response,
+)
TEST_SERVER_DOMAIN = "testserver.com"
@@ -483,6 +487,28 @@ def test_transaction_create_by_user_query_no_permission(
assert_no_permission(response)
+def test_query_transaction_by_invalid_id(staff_api_client, permission_manage_payments):
+ # given
+ id = graphene.Node.to_global_id("Order", "e6cad766-c9df-4970-b77b-b8eb0e303fb6")
+ variables = {"id": id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ TRANSACTION_QUERY,
+ variables,
+ permissions=[permission_manage_payments],
+ )
+
+ # then
+ content = get_graphql_content_from_response(response)
+ assert len(content["errors"]) == 1
+ assert (
+ content["errors"][0]["message"]
+ == f"Invalid ID: {id}. Expected: TransactionItem, received: Order."
+ )
+ assert content["data"]["transaction"] is None
+
+
@pytest.mark.parametrize(
("db_field", "api_field"),
[