Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add missing code for handling CustomerType metadata (#19683)
Marcin Wcisłoc2065d52cff1
diff --git a/saleor/graphql/meta/mutations/utils.py b/saleor/graphql/meta/mutations/utils.pyindex f52490f677..81d5cc99e7 100644--- a/saleor/graphql/meta/mutations/utils.py+++ b/saleor/graphql/meta/mutations/utils.py@@ -23,6 +23,7 @@ def get_valid_metadata_instance(instance) -> ModelWithMetadata:# In case there is no such field, set value as NoneTYPE_UPDATED_FIELD = {"Address": None,+ "CustomerType": None,"User": "updated_at","App": None,"Attribute": None,diff --git a/saleor/graphql/meta/resolvers.py b/saleor/graphql/meta/resolvers.pyindex a7f97a7e3e..608d58e828 100644--- a/saleor/graphql/meta/resolvers.py+++ b/saleor/graphql/meta/resolvers.py@@ -51,6 +51,7 @@ def resolve_object_with_metadata_type(instance):MODEL_TO_TYPE_MAP: dict[type, type[models.Model]] = {account_models.Address: account_types.Address,+ account_models.CustomerType: account_types.CustomerType,account_models.User: account_types.User,app_models.App: app_types.App,attribute_models.Attribute: attribute_types.Attribute,@@ -90,8 +91,6 @@ def resolve_object_with_metadata_type(instance):instance, "old_sale_id", False):return discount_types.Sale, instance.pk- if instance.__class__ not in MODEL_TO_TYPE_MAP:- raise ValueError(f"Unknown type: {instance.__class__}")return MODEL_TO_TYPE_MAP[instance.__class__], instance.pkif dataclasses.is_dataclass(instance):diff --git a/saleor/graphql/meta/tests/mutations/test_customer_type.py b/saleor/graphql/meta/tests/mutations/test_customer_type.pynew file mode 100644index 0000000000..694e19d640--- /dev/null+++ b/saleor/graphql/meta/tests/mutations/test_customer_type.py@@ -0,0 +1,138 @@+import graphene++from ....tests.utils import assert_no_permission+from . import PRIVATE_KEY, PUBLIC_KEY+from .test_update_metadata import (+ UPDATE_PUBLIC_METADATA_MUTATION,+ execute_update_public_metadata_for_item,+ item_contains_proper_public_metadata,+)+from .test_update_private_metadata import (+ UPDATE_PRIVATE_METADATA_MUTATION,+ execute_update_private_metadata_for_item,+ item_contains_proper_private_metadata,+)+++def test_add_public_metadata_for_customer_type_as_staff(+ staff_api_client, permission_manage_customer_types_and_attributes, customer_type+):+ # given+ customer_type_id = graphene.Node.to_global_id("CustomerType", customer_type.pk)++ # when+ response = execute_update_public_metadata_for_item(+ staff_api_client,+ permission_manage_customer_types_and_attributes,+ customer_type_id,+ "CustomerType",+ )++ # then+ assert item_contains_proper_public_metadata(+ response["data"]["updateMetadata"]["item"], customer_type, customer_type_id+ )+++def test_add_public_metadata_for_customer_type_as_app(+ app_api_client, permission_manage_customer_types_and_attributes, customer_type+):+ # given+ customer_type_id = graphene.Node.to_global_id("CustomerType", customer_type.pk)++ # when+ response = execute_update_public_metadata_for_item(+ app_api_client,+ permission_manage_customer_types_and_attributes,+ customer_type_id,+ "CustomerType",+ )++ # then+ assert item_contains_proper_public_metadata(+ response["data"]["updateMetadata"]["item"], customer_type, customer_type_id+ )+++def test_add_public_metadata_for_customer_type_without_permission(+ staff_api_client, customer_type+):+ # given+ customer_type_id = graphene.Node.to_global_id("CustomerType", customer_type.pk)+ variables = {+ "id": customer_type_id,+ "input": [{"key": PUBLIC_KEY, "value": "value"}],+ }+ # when+ response = staff_api_client.post_graphql(+ UPDATE_PUBLIC_METADATA_MUTATION % "CustomerType", variables+ )++ # then+ assert_no_permission(response)+ customer_type.refresh_from_db(fields=["metadata"])+ assert customer_type.metadata == {}+++def test_add_private_metadata_for_customer_type_as_staff(+ staff_api_client, permission_manage_customer_types_and_attributes, customer_type+):+ # given+ customer_type_id = graphene.Node.to_global_id("CustomerType", customer_type.pk)++ # when+ response = execute_update_private_metadata_for_item(+ staff_api_client,+ permission_manage_customer_types_and_attributes,+ customer_type_id,+ "CustomerType",+ )++ # then+ assert item_contains_proper_private_metadata(+ response["data"]["updatePrivateMetadata"]["item"],+ customer_type,+ customer_type_id,+ )+++def test_add_private_metadata_for_customer_type_as_app(+ app_api_client, permission_manage_customer_types_and_attributes, customer_type+):+ # given+ customer_type_id = graphene.Node.to_global_id("CustomerType", customer_type.pk)++ # when+ response = execute_update_private_metadata_for_item(+ app_api_client,+ permission_manage_customer_types_and_attributes,+ customer_type_id,+ "CustomerType",+ )++ # then+ assert item_contains_proper_private_metadata(+ response["data"]["updatePrivateMetadata"]["item"],+ customer_type,+ customer_type_id,+ )+++def test_add_private_metadata_for_customer_type_without_permission(+ staff_api_client, customer_type+):+ # given+ customer_type_id = graphene.Node.to_global_id("CustomerType", customer_type.pk)+ variables = {+ "id": customer_type_id,+ "input": [{"key": PRIVATE_KEY, "value": "value"}],+ }+ # when+ response = staff_api_client.post_graphql(+ UPDATE_PRIVATE_METADATA_MUTATION % "CustomerType", variables+ )++ # then+ assert_no_permission(response)+ customer_type.refresh_from_db(fields=["private_metadata"])+ assert customer_type.private_metadata == {}diff --git a/saleor/graphql/meta/tests/queries/test_customer_type.py b/saleor/graphql/meta/tests/queries/test_customer_type.pynew file mode 100644index 0000000000..cfa089e055--- /dev/null+++ b/saleor/graphql/meta/tests/queries/test_customer_type.py@@ -0,0 +1,137 @@+import graphene++from ....tests.utils import assert_no_permission, get_graphql_content+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE++QUERY_CUSTOMER_TYPE_PUBLIC_META = """+ query customerTypeMeta($id: ID!){+ customerType(id: $id){+ metadata{+ key+ value+ }+ }+ }+"""+++def test_query_public_meta_for_customer_type_as_staff(staff_api_client, customer_type):+ # given+ customer_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})+ customer_type.save(update_fields=["metadata"])+ variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}++ # when+ response = staff_api_client.post_graphql(QUERY_CUSTOMER_TYPE_PUBLIC_META, variables)+ content = get_graphql_content(response)++ # then+ metadata = content["data"]["customerType"]["metadata"]+ assert len(metadata) == 1+ assert metadata[0]["key"] == PUBLIC_KEY+ assert metadata[0]["value"] == PUBLIC_VALUE+++def test_query_public_meta_for_customer_type_as_app(app_api_client, customer_type):+ # given+ customer_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})+ customer_type.save(update_fields=["metadata"])+ variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}++ # when+ response = app_api_client.post_graphql(QUERY_CUSTOMER_TYPE_PUBLIC_META, variables)+ content = get_graphql_content(response)++ # then+ metadata = content["data"]["customerType"]["metadata"]+ assert len(metadata) == 1+ assert metadata[0]["key"] == PUBLIC_KEY+ assert metadata[0]["value"] == PUBLIC_VALUE+++QUERY_CUSTOMER_TYPE_PRIVATE_META = """+ query customerTypeMeta($id: ID!){+ customerType(id: $id){+ privateMetadata{+ key+ value+ }+ }+ }+"""+++def test_query_private_meta_for_customer_type_as_staff(+ staff_api_client, customer_type, permission_manage_customer_types_and_attributes+):+ # given+ customer_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})+ customer_type.save(update_fields=["private_metadata"])+ variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}++ # when+ response = staff_api_client.post_graphql(+ QUERY_CUSTOMER_TYPE_PRIVATE_META,+ variables,+ [permission_manage_customer_types_and_attributes],+ check_no_permissions=False,+ )+ content = get_graphql_content(response)++ # then+ metadata = content["data"]["customerType"]["privateMetadata"]+ assert len(metadata) == 1+ assert metadata[0]["key"] == PRIVATE_KEY+ assert metadata[0]["value"] == PRIVATE_VALUE+++def test_query_private_meta_for_customer_type_as_app(+ app_api_client, customer_type, permission_manage_customer_types_and_attributes+):+ # given+ customer_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})+ customer_type.save(update_fields=["private_metadata"])+ variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}++ # when+ response = app_api_client.post_graphql(+ QUERY_CUSTOMER_TYPE_PRIVATE_META,+ variables,+ [permission_manage_customer_types_and_attributes],+ check_no_permissions=False,+ )+ content = get_graphql_content(response)++ # then+ metadata = content["data"]["customerType"]["privateMetadata"]+ assert len(metadata) == 1+ assert metadata[0]["key"] == PRIVATE_KEY+ assert metadata[0]["value"] == PRIVATE_VALUE+++def test_query_private_meta_for_customer_type_as_staff_without_permission(+ staff_api_client, customer_type+):+ # given+ variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}++ # when+ response = staff_api_client.post_graphql(+ QUERY_CUSTOMER_TYPE_PRIVATE_META, variables+ )++ # then+ assert_no_permission(response)+++def test_query_private_meta_for_customer_type_as_app_without_permission(+ app_api_client, customer_type+):+ # given+ variables = {"id": graphene.Node.to_global_id("CustomerType", customer_type.pk)}++ # when+ response = app_api_client.post_graphql(QUERY_CUSTOMER_TYPE_PRIVATE_META, variables)++ # then+ assert_no_permission(response)