Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix json scalar (#12524)
Maciej Korycinski96f242cbc997
diff --git a/saleor/graphql/core/scalars.py b/saleor/graphql/core/scalars.pyindex 75dd2545c1..5ccaafdf26 100644--- a/saleor/graphql/core/scalars.py+++ b/saleor/graphql/core/scalars.py@@ -57,9 +57,22 @@ class PositiveDecimal(Decimal):class JSON(GenericScalar):@staticmethoddef parse_literal(node):- if not isinstance(node, ast.ObjectValue):- raise GraphQLError("JSON scalar needs to recieve a correct JSON structure.")- return node+ if isinstance(node, ast.ObjectValue):+ return {+ field.name.value: GenericScalar.parse_literal(field.value)+ for field in node.fields+ }+ elif isinstance(node, ast.ListValue):+ return [GenericScalar.parse_literal(value) for value in node.values]+ raise GraphQLError("JSON scalar needs to receive a correct JSON structure.")++ @staticmethod+ def parse_value(value):+ if isinstance(value, dict):+ return value+ if isinstance(value, list):+ return [GenericScalar.parse_value(v) for v in value]+ raise GraphQLError("JSON scalar needs to receive a correct JSON structure.")class WeightScalar(graphene.Scalar):diff --git a/saleor/graphql/core/tests/test_scalars.py b/saleor/graphql/core/tests/test_scalars.pyindex f9d06a78db..7a68b5d5fa 100644--- a/saleor/graphql/core/tests/test_scalars.py+++ b/saleor/graphql/core/tests/test_scalars.py@@ -1,7 +1,10 @@+import mockimport pytestfrom ....order.models import Order+from ....payment.interface import PaymentGatewayDatafrom ...tests.utils import get_graphql_content, get_graphql_content_from_response+from ..utils import to_global_id_or_noneQUERY_CHECKOUT = """query getCheckout($token: UUID!) {@@ -95,3 +98,176 @@ def test_order_query_with_filter_created_str_as_date_value(content = get_graphql_content_from_response(response)assert 'Variable "$filter" got invalid value' in content["errors"][0]["message"]+++PAYMENT_GATEWAY_INITIALIZE_WITH_VARIABLES = """+mutation PaymentGatewayInitialize(+ $id: ID!,+ $paymentGateways: [PaymentGatewayToInitialize!]+) {+ paymentGatewayInitialize(+ id: $id+ paymentGateways: $paymentGateways+ ) {+ gatewayConfigs {+ id+ }+ }+}+"""+++ "data",+ [+ {"input": "json"},+ None,+ {},+ [{"input": [{"json": "a"}]}, {}],+ [+ {+ "input": [+ {+ "json": [+ "a",+ ]+ }+ ]+ },+ {},+ ],+ [1, 2, 3],+ ],+)[email protected]("saleor.plugins.manager.PluginsManager.payment_gateway_initialize_session")+def test_json_scalar_as_correct_variable(+ mocked_initialize, data, user_api_client, checkout_with_prices, plugins_manager+):+ # given+ checkout = checkout_with_prices++ mocked_initialize.return_value = [+ PaymentGatewayData(app_identifier="app.id", data={"data": data})+ ]++ variables = {+ "id": to_global_id_or_none(checkout),+ "paymentGateways": [{"id": "app.id", "data": data}],+ }++ # when+ response = user_api_client.post_graphql(+ PAYMENT_GATEWAY_INITIALIZE_WITH_VARIABLES, variables+ )++ # then+ get_graphql_content(response)++[email protected]("data_value", (True, "string", 1.0, 1))[email protected]("saleor.plugins.manager.PluginsManager.payment_gateway_initialize_session")+def test_json_scalar_as_incorrect_variable(+ mocked_initialize,+ data_value,+ user_api_client,+ checkout_with_prices,+ plugins_manager,+):+ # given+ checkout = checkout_with_prices++ mocked_initialize.return_value = [+ PaymentGatewayData(app_identifier="app.id", data={"data": {"json": "data"}})+ ]++ variables = {+ "id": to_global_id_or_none(checkout),+ "paymentGateways": [{"id": "app.id", "data": data_value}],+ }++ # when+ response = user_api_client.post_graphql(+ PAYMENT_GATEWAY_INITIALIZE_WITH_VARIABLES, variables+ )++ # then+ content = get_graphql_content_from_response(response)+ assert "errors" in content+++PAYMENT_GATEWAY_INITIALIZE_WITHOUT_VARIABLES = """+mutation {+ paymentGatewayInitialize(+ id: "%s"+ paymentGateways: [{id: "app.id", data: %s}]+ ) {+ gatewayConfigs {+ id+ }+ }+}+"""++[email protected]("data_value", (True, "string", 1.0, 1))[email protected]("saleor.plugins.manager.PluginsManager.payment_gateway_initialize_session")+def test_json_scalar_as_incorrect_value(+ mocked_initialize,+ data_value,+ user_api_client,+ checkout_with_prices,+ plugins_manager,+):+ # given+ checkout = checkout_with_prices++ mocked_initialize.return_value = [+ PaymentGatewayData(app_identifier="app.id", data={"data": {"json": "data"}})+ ]++ query = PAYMENT_GATEWAY_INITIALIZE_WITHOUT_VARIABLES % (+ to_global_id_or_none(checkout),+ data_value,+ )++ # when+ response = user_api_client.post_graphql(+ query,+ )++ # then+ content = get_graphql_content_from_response(response)+ assert "errors" in content+++ "data",+ [+ '{input: "json"}',+ "{}",+ '[{input: "json"}]',+ '[{input: "json"}, {}]',+ '[{input: [{json: "aa"}]}, {input: {json: [{}]}}]',+ '[{input: [{json: [{json2: ["aa",]}]}]}, {}]',+ "[1,2,3]",+ ],+)[email protected]("saleor.plugins.manager.PluginsManager.payment_gateway_initialize_session")+def test_json_scalar_as_correct_value(+ mocked_initialize, data, user_api_client, checkout_with_prices, plugins_manager+):+ # given+ checkout = checkout_with_prices+ mocked_initialize.return_value = [+ PaymentGatewayData(app_identifier="app.id", data={"data": {"response": "data"}})+ ]+ query = PAYMENT_GATEWAY_INITIALIZE_WITHOUT_VARIABLES % (+ to_global_id_or_none(checkout),+ data,+ )++ # when+ response = user_api_client.post_graphql(query)++ # then+ get_graphql_content(response)