Skip to content

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

Split meta module (#13168)

Iga Karbowiak337487429640
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index 99a7234e68..f064b6294d 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -25,7 +25,7 @@ from ...core.doc_category import DOC_CATEGORY_USERS
from ...core.enums import LanguageCodeEnum
from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ...core.types import AccountError, BaseInputObjectType, NonNullList
-from ...meta.mutations import MetadataInput
+from ...meta.inputs import MetadataInput
from ...plugins.dataloaders import get_plugin_manager_promise
from ...site.dataloaders import get_site_promise
from ..enums import AddressTypeEnum
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 9509e928ff..e94bf6d2ea 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -42,7 +42,7 @@ from ...core.mutations import (
validation_error_to_error_type,
)
from ...core.types import AccountError, BaseInputObjectType, NonNullList
-from ...meta.mutations import MetadataInput
+from ...meta.inputs import MetadataInput
from ...plugins.dataloaders import get_plugin_manager_promise
from .authentication import CreateToken
diff --git a/saleor/graphql/checkout/mutations/checkout_complete.py b/saleor/graphql/checkout/mutations/checkout_complete.py
index 68381d15d9..ff76ae4ab3 100644
--- a/saleor/graphql/checkout/mutations/checkout_complete.py
+++ b/saleor/graphql/checkout/mutations/checkout_complete.py
@@ -29,7 +29,7 @@ from ...core.mutations import BaseMutation
from ...core.scalars import UUID
from ...core.types import CheckoutError, NonNullList
from ...core.validators import validate_one_of_args_is_in_mutation
-from ...meta.mutations import MetadataInput
+from ...meta.inputs import MetadataInput
from ...order.types import Order
from ...plugins.dataloaders import get_plugin_manager_promise
from ...site.dataloaders import get_site_promise
diff --git a/saleor/graphql/checkout/mutations/checkout_create.py b/saleor/graphql/checkout/mutations/checkout_create.py
index bd8347b463..8d97307e57 100644
--- a/saleor/graphql/checkout/mutations/checkout_create.py
+++ b/saleor/graphql/checkout/mutations/checkout_create.py
@@ -44,7 +44,7 @@ if TYPE_CHECKING:
from ....account.models import Address
from .utils import CheckoutLineData
-from ...meta.mutations import MetadataInput
+from ...meta.inputs import MetadataInput
class CheckoutAddressValidationRules(BaseInputObjectType):
diff --git a/saleor/graphql/checkout/mutations/order_create_from_checkout.py b/saleor/graphql/checkout/mutations/order_create_from_checkout.py
index 12f56b5c28..e9c68696b1 100644
--- a/saleor/graphql/checkout/mutations/order_create_from_checkout.py
+++ b/saleor/graphql/checkout/mutations/order_create_from_checkout.py
@@ -13,7 +13,7 @@ from ...core.descriptions import ADDED_IN_32, ADDED_IN_38
from ...core.doc_category import DOC_CATEGORY_ORDERS
from ...core.mutations import BaseMutation
from ...core.types import Error, NonNullList
-from ...meta.mutations import MetadataInput
+from ...meta.inputs import MetadataInput
from ...order.types import Order
from ...plugins.dataloaders import get_plugin_manager_promise
from ..enums import OrderCreateFromCheckoutErrorCode
diff --git a/saleor/graphql/invoice/mutations.py b/saleor/graphql/invoice/mutations.py
index 7ee41dd226..deb1815662 100644
--- a/saleor/graphql/invoice/mutations.py
+++ b/saleor/graphql/invoice/mutations.py
@@ -16,7 +16,7 @@ from ..core.descriptions import ADDED_IN_314
from ..core.doc_category import DOC_CATEGORY_ORDERS
from ..core.mutations import ModelDeleteMutation, ModelMutation
from ..core.types import BaseInputObjectType, InvoiceError, NonNullList
-from ..meta.mutations import MetadataInput
+from ..meta.inputs import MetadataInput
from ..order.types import Order
from ..plugins.dataloaders import get_plugin_manager_promise
from .types import Invoice
diff --git a/saleor/graphql/meta/inputs.py b/saleor/graphql/meta/inputs.py
new file mode 100644
index 0000000000..b322c4381b
--- /dev/null
+++ b/saleor/graphql/meta/inputs.py
@@ -0,0 +1,6 @@
+import graphene
+
+
+class MetadataInput(graphene.InputObjectType):
+ key = graphene.String(required=True, description="Key of a metadata item.")
+ value = graphene.String(required=True, description="Value of a metadata item.")
diff --git a/saleor/graphql/meta/mutations/__init__.py b/saleor/graphql/meta/mutations/__init__.py
new file mode 100644
index 0000000000..a3d12ef981
--- /dev/null
+++ b/saleor/graphql/meta/mutations/__init__.py
@@ -0,0 +1,11 @@
+from .delete_metadata import DeleteMetadata
+from .delete_private_metadata import DeletePrivateMetadata
+from .update_metadata import UpdateMetadata
+from .update_private_metadata import UpdatePrivateMetadata
+
+__all__ = [
+ "DeleteMetadata",
+ "DeletePrivateMetadata",
+ "UpdateMetadata",
+ "UpdatePrivateMetadata",
+]
diff --git a/saleor/graphql/meta/mutations.py b/saleor/graphql/meta/mutations/base.py
similarity index 52%
rename from saleor/graphql/meta/mutations.py
rename to saleor/graphql/meta/mutations/base.py
index f02e43b42a..44b3c648dd 100644
--- a/saleor/graphql/meta/mutations.py
+++ b/saleor/graphql/meta/mutations/base.py
@@ -1,54 +1,27 @@
-import logging
-from typing import List, cast
+from typing import List
import graphene
-from django.core.exceptions import FieldDoesNotExist, ValidationError
-from django.db import DatabaseError
+from django.core.exceptions import ValidationError
from graphql.error.base import GraphQLError
-from ...checkout import models as checkout_models
-from ...checkout.models import Checkout
-from ...core import models
-from ...core.error_codes import MetadataErrorCode
-from ...core.exceptions import PermissionDenied
-from ...discount import models as discount_models
-from ...menu import models as menu_models
-from ...order import models as order_models
-from ...product import models as product_models
-from ...shipping import models as shipping_models
-from ..channel import ChannelContext
-from ..core import ResolveInfo
-from ..core.mutations import BaseMutation
-from ..core.types import MetadataError, NonNullList
-from ..core.utils import from_global_id_or_error
-from ..payment.utils import metadata_contains_empty_key
-from .extra_methods import MODEL_EXTRA_METHODS, MODEL_EXTRA_PREFETCH
-from .permissions import (
- PRIVATE_META_PERMISSION_MAP,
- PUBLIC_META_PERMISSION_MAP,
- AccountPermissions,
-)
-from .types import ObjectWithMetadata, get_valid_metadata_instance
-
-logger = logging.getLogger(__name__)
-
-
-def _save_instance(instance, metadata_field: str):
- fields = [metadata_field]
-
- try:
- if bool(instance._meta.get_field("updated_at")):
- fields.append("updated_at")
- except FieldDoesNotExist:
- pass
-
- try:
- instance.save(update_fields=fields)
- except DatabaseError:
- msg = "Cannot update metadata for instance. Updating not existing object."
- raise ValidationError(
- {"metadata": ValidationError(msg, code=MetadataErrorCode.NOT_FOUND.value)}
- )
+from ....checkout import models as checkout_models
+from ....checkout.models import Checkout
+from ....core import models
+from ....core.error_codes import MetadataErrorCode
+from ....core.exceptions import PermissionDenied
+from ....discount import models as discount_models
+from ....menu import models as menu_models
+from ....order import models as order_models
+from ....product import models as product_models
+from ....shipping import models as shipping_models
+from ...channel import ChannelContext
+from ...core import ResolveInfo
+from ...core.mutations import BaseMutation
+from ...core.utils import from_global_id_or_error
+from ...payment.utils import metadata_contains_empty_key
+from ..extra_methods import MODEL_EXTRA_METHODS, MODEL_EXTRA_PREFETCH
+from ..permissions import AccountPermissions
+from ..types import ObjectWithMetadata
class MetadataPermissionOptions(graphene.types.mutation.MutationOptions):
@@ -251,147 +224,3 @@ class BaseMetadataMutation(BaseMutation):
if use_channel_context:
instance = ChannelContext(node=instance, channel_slug=None)
return cls(**{"item": instance, "errors": []})
-
-
-class MetadataInput(graphene.InputObjectType):
- key = graphene.String(required=True, description="Key of a metadata item.")
- value = graphene.String(required=True, description="Value of a metadata item.")
-
-
-class UpdateMetadata(BaseMetadataMutation):
- class Meta:
- description = (
- "Updates metadata of an object. To use it, you need to have access to the "
- "modified object."
- )
- permission_map = PUBLIC_META_PERMISSION_MAP
- error_type_class = MetadataError
- error_type_field = "metadata_errors"
-
- class Arguments:
- id = graphene.ID(
- description="ID or token (for Order and Checkout) of an object to update.",
- required=True,
- )
- input = NonNullList(
- MetadataInput,
- description="Fields required to update the object's metadata.",
- required=True,
- )
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls, _root, info: ResolveInfo, /, *, id: str, input: List
- ):
- instance = cast(models.ModelWithMetadata, cls.get_instance(info, id=id))
- if instance:
- meta_instance = get_valid_metadata_instance(instance)
- cls.validate_metadata_keys(input)
- items = {data.key: data.value for data in input}
- meta_instance.store_value_in_metadata(items=items)
- _save_instance(meta_instance, "metadata")
-
- return cls.success_response(instance)
-
-
-class DeleteMetadata(BaseMetadataMutation):
- class Meta:
- description = (
- "Delete metadata of an object. To use it, you need to have access to the "
- "modified object."
- )
- permission_map = PUBLIC_META_PERMISSION_MAP
- error_type_class = MetadataError
- error_type_field = "metadata_errors"
-
- class Arguments:
- id = graphene.ID(
- description="ID or token (for Order and Checkout) of an object to update.",
- required=True,
- )
- keys = NonNullList(
- graphene.String,
- description="Metadata keys to delete.",
- required=True,
- )
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls, _root, info: ResolveInfo, /, *, id: str, keys: List[str]
- ):
- instance = cast(models.ModelWithMetadata, cls.get_instance(info, id=id))
- if instance:
- meta_instance = get_valid_metadata_instance(instance)
- for key in keys:
- meta_instance.delete_value_from_metadata(key)
- _save_instance(meta_instance, "metadata")
- return cls.success_response(instance)
-
-
-class UpdatePrivateMetadata(BaseMetadataMutation):
- class Meta:
- description = (
- "Updates private metadata of an object. To use it, you need to be an "
- "authenticated staff user or an app and have access to the modified object."
- )
- permission_map = PRIVATE_META_PERMISSION_MAP
- error_type_class = MetadataError
- error_type_field = "metadata_errors"
-
- class Arguments:
- id = graphene.ID(
- description="ID or token (for Order and Checkout) of an object to update.",
- required=True,
- )
- input = NonNullList(
- MetadataInput,
- description="Fields required to update the object's metadata.",
- required=True,
- )
-
- @classmethod
- def perform_mutation(cls, _root, info: ResolveInfo, /, **data):
- instance = cls.get_instance(info, **data)
- if instance:
- meta_instance = get_valid_metadata_instance(instance)
- metadata_list = data.pop("input")
- cls.validate_metadata_keys(metadata_list)
- items = {data.key: data.value for data in metadata_list}
- meta_instance.store_value_in_private_metadata(items=items)
- _save_instance(meta_instance, "private_metadata")
- return cls.success_response(instance)
-
-
-class DeletePrivateMetadata(BaseMetadataMutation):
- class Meta:
- description = (
- "Delete object's private metadata. To use it, you need to be an "
- "authenticated staff user or an app and have access to the modified object."
- )
- permission_map = PRIVATE_META_PERMISSION_MAP
- error_type_class = MetadataError
- error_type_field = "metadata_errors"
-
- class Arguments:
- id = graphene.ID(
- description="ID or token (for Order and Checkout) of an object to update.",
- required=True,
- )
- keys = NonNullList(
- graphene.String,
- description="Metadata keys to delete.",
- required=True,
- )
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls, _root, info: ResolveInfo, /, *, id: str, keys: List[str]
- ):
- instance = cls.get_instance(info, id=id)
-
- if instance:
- meta_instance = get_valid_metadata_instance(instance)
- for key in keys:
- meta_instance.delete_value_from_private_metadata(key)
- _save_instance(meta_instance, "private_metadata")
- return cls.success_response(instance)
diff --git a/saleor/graphql/meta/mutations/delete_metadata.py b/saleor/graphql/meta/mutations/delete_metadata.py
new file mode 100644
index 0000000000..d1e2995882
--- /dev/null
+++ b/saleor/graphql/meta/mutations/delete_metadata.py
@@ -0,0 +1,45 @@
+from typing import List, cast
+
+import graphene
+
+from ....core import models
+from ...core import ResolveInfo
+from ...core.types import MetadataError, NonNullList
+from ..permissions import PUBLIC_META_PERMISSION_MAP
+from ..types import get_valid_metadata_instance
+from .base import BaseMetadataMutation
+from .utils import save_instance
+
+
+class DeleteMetadata(BaseMetadataMutation):
+ class Meta:
+ description = (
+ "Delete metadata of an object. To use it, you need to have access to the "
+ "modified object."
+ )
+ permission_map = PUBLIC_META_PERMISSION_MAP
+ error_type_class = MetadataError
+ error_type_field = "metadata_errors"
+
+ class Arguments:
+ id = graphene.ID(
+ description="ID or token (for Order and Checkout) of an object to update.",
+ required=True,
+ )
+ keys = NonNullList(
+ graphene.String,
+ description="Metadata keys to delete.",
+ required=True,
+ )
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls, _root, info: ResolveInfo, /, *, id: str, keys: List[str]
+ ):
+ instance = cast(models.ModelWithMetadata, cls.get_instance(info, id=id))
+ if instance:
+ meta_instance = get_valid_metadata_instance(instance)
+ for key in keys:
+ meta_instance.delete_value_from_metadata(key)
+ save_instance(meta_instance, "metadata")
+ return cls.success_response(instance)
diff --git a/saleor/graphql/meta/mutations/delete_private_metadata.py b/saleor/graphql/meta/mutations/delete_private_metadata.py
new file mode 100644
index 0000000000..dae3ceee9d
--- /dev/null
+++ b/saleor/graphql/meta/mutations/delete_private_metadata.py
@@ -0,0 +1,45 @@
+from typing import List
+
+import graphene
+
+from ...core import ResolveInfo
+from ...core.types import MetadataError, NonNullList
+from ..permissions import PRIVATE_META_PERMISSION_MAP
+from ..types import get_valid_metadata_instance
+from .base import BaseMetadataMutation
+from .utils import save_instance
+
+
+class DeletePrivateMetadata(BaseMetadataMutation):
+ class Meta:
+ description = (
+ "Delete object's private metadata. To use it, you need to be an "
+ "authenticated staff user or an app and have access to the modified object."
+ )
+ permission_map = PRIVATE_META_PERMISSION_MAP
+ error_type_class = MetadataError
+ error_type_field = "metadata_errors"
+
+ class Arguments:
+ id = graphene.ID(
+ description="ID or token (for Order and Checkout) of an object to update.",
+ required=True,
+ )
+ keys = NonNullList(
+ graphene.String,
+ description="Metadata keys to delete.",
+ required=True,
+ )
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls, _root, info: ResolveInfo, /, *, id: str, keys: List[str]
+ ):
+ instance = cls.get_instance(info, id=id)
+
+ if instance:
+ meta_instance = get_valid_metadata_instance(instance)
+ for key in keys:
+ meta_instance.delete_value_from_private_metadata(key)
+ save_instance(meta_instance, "private_metadata")
+ return cls.success_response(instance)
diff --git a/saleor/graphql/meta/mutations/update_metadata.py b/saleor/graphql/meta/mutations/update_metadata.py
new file mode 100644
index 0000000000..e3a3fe8c90
--- /dev/null
+++ b/saleor/graphql/meta/mutations/update_metadata.py
@@ -0,0 +1,48 @@
+from typing import List, cast
+
+import graphene
+
+from ....core import models
+from ...core import ResolveInfo
+from ...core.types import MetadataError, NonNullList
+from ..inputs import MetadataInput
+from ..permissions import PUBLIC_META_PERMISSION_MAP
+from ..types import get_valid_metadata_instance
+from .base import BaseMetadataMutation
+from .utils import save_instance
+
+
+class UpdateMetadata(BaseMetadataMutation):
+ class Meta:
+ description = (
+ "Updates metadata of an object. To use it, you need to have access to the "
+ "modified object."
+ )
+ permission_map = PUBLIC_META_PERMISSION_MAP
+ error_type_class = MetadataError
+ error_type_field = "metadata_errors"
+
+ class Arguments:
+ id = graphene.ID(
+ description="ID or token (for Order and Checkout) of an object to update.",
+ required=True,
+ )
+ input = NonNullList(
+ MetadataInput,
+ description="Fields required to update the object's metadata.",
+ required=True,
+ )
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls, _root, info: ResolveInfo, /, *, id: str, input: List
+ ):
+ instance = cast(models.ModelWithMetadata, cls.get_instance(info, id=id))
+ if instance:
+ meta_instance = get_valid_metadata_instance(instance)
+ cls.validate_metadata_keys(input)
+ items = {data.key: data.value for data in input}
+ meta_instance.store_value_in_metadata(items=items)
+ save_instance(meta_instance, "metadata")
+
+ return cls.success_response(instance)
diff --git a/saleor/graphql/meta/mutations/update_private_metadata.py b/saleor/graphql/meta/mutations/update_private_metadata.py
new file mode 100644
index 0000000000..99fe83b18e
--- /dev/null
+++ b/saleor/graphql/meta/mutations/update_private_metadata.py
@@ -0,0 +1,43 @@
+import graphene
+
+from ...core import ResolveInfo
+from ...core.types import MetadataError, NonNullList
+from ..inputs import MetadataInput
+from ..permissions import PRIVATE_META_PERMISSION_MAP
+from ..types import get_valid_metadata_instance
+from .base import BaseMetadataMutation
+from .utils import save_instance
+
+
+class UpdatePrivateMetadata(BaseMetadataMutation):
+ class Meta:
+ description = (
+ "Updates private metadata of an object. To use it, you need to be an "
+ "authenticated staff user or an app and have access to the modified object."
+ )
+ permission_map = PRIVATE_META_PERMISSION_MAP
+ error_type_class = MetadataError
+ error_type_field = "metadata_errors"
+
+ class Arguments:
+ id = graphene.ID(
+ description="ID or token (for Order and Checkout) of an object to update.",
+ required=True,
+ )
+ input = NonNullList(
+ MetadataInput,
+ description="Fields required to update the object's metadata.",
+ required=True,
+ )
+
+ @classmethod
+ def perform_mutation(cls, _root, info: ResolveInfo, /, **data):
+ instance = cls.get_instance(info, **data)
+ if instance:
+ meta_instance = get_valid_metadata_instance(instance)
+ metadata_list = data.pop("input")
+ cls.validate_metadata_keys(metadata_list)
+ items = {data.key: data.value for data in metadata_list}
+ meta_instance.store_value_in_private_metadata(items=items)
+ save_instance(meta_instance, "private_metadata")
+ return cls.success_response(instance)
diff --git a/saleor/graphql/meta/mutations/utils.py b/saleor/graphql/meta/mutations/utils.py
new file mode 100644
index 0000000000..e5d4699aaf
--- /dev/null
+++ b/saleor/graphql/meta/mutations/utils.py
@@ -0,0 +1,22 @@
+from django.core.exceptions import FieldDoesNotExist, ValidationError
+from django.db import DatabaseError
+
+from ....core.error_codes import MetadataErrorCode
+
+
+def save_instance(instance, metadata_field: str):
+ fields = [metadata_field]
+
+ try:
+ if bool(instance._meta.get_field("updated_at")):
+ fields.append("updated_at")
+ except FieldDoesNotExist:
+ pass
+
+ try:
+ instance.save(update_fields=fields)
+ except DatabaseError:
+ msg = "Cannot update metadata for instance. Updating not existing object."
+ raise ValidationError(
+ {"metadata": ValidationError(msg, code=MetadataErrorCode.NOT_FOUND.value)}
+ )
diff --git a/saleor/graphql/meta/tests/mutations/__init__.py b/saleor/graphql/meta/tests/mutations/__init__.py
new file mode 100644
index 0000000000..80a89c130c
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/__init__.py
@@ -0,0 +1,7 @@
+PRIVATE_KEY = "private_key"
+PRIVATE_VALUE = "private_vale"
+
+PUBLIC_KEY = "key"
+PUBLIC_KEY2 = "key2"
+PUBLIC_VALUE = "value"
+PUBLIC_VALUE2 = "value2"
diff --git a/saleor/graphql/meta/tests/mutations/test_account.py b/saleor/graphql/meta/tests/mutations/test_account.py
new file mode 100644
index 0000000000..4ff606f29f
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_account.py
@@ -0,0 +1,1433 @@
+import graphene
+import pytest
+
+from .....account.error_codes import AccountErrorCode
+from .....account.models import User
+from .....core.error_codes import MetadataErrorCode
+from ....tests.utils import assert_no_permission
+from . import (
+ PRIVATE_KEY,
+ PRIVATE_VALUE,
+ PUBLIC_KEY,
+ PUBLIC_KEY2,
+ PUBLIC_VALUE,
+ PUBLIC_VALUE2,
+)
+from .test_delete_metadata import (
+ DELETE_PUBLIC_METADATA_MUTATION,
+ execute_clear_public_metadata_for_item,
+ execute_clear_public_metadata_for_multiple_items,
+ item_without_multiple_public_metadata,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ DELETE_PRIVATE_METADATA_MUTATION,
+ execute_clear_private_metadata_for_item,
+ execute_clear_private_metadata_for_multiple_items,
+ item_without_multiple_private_metadata,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ UPDATE_PUBLIC_METADATA_MUTATION,
+ execute_update_public_metadata_for_item,
+ execute_update_public_metadata_for_multiple_items,
+ item_contains_multiple_proper_public_metadata,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ UPDATE_PRIVATE_METADATA_MUTATION,
+ execute_update_private_metadata_for_item,
+ execute_update_private_metadata_for_multiple_items,
+ item_contains_multiple_proper_private_metadata,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_public_metadata_for_customer_address_as_staff(
+ staff_api_client, permission_manage_users, customer_user
+):
+ # given
+ address = customer_user.addresses.first()
+ address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ address.save(update_fields=["metadata"])
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_users, address_id, "Address"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], address, address_id
+ )
+
+
+def test_delete_public_metadata_for_customer_address_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ address = customer_user.addresses.first()
+ address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ address.save(update_fields=["metadata"])
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ app_api_client, permission_manage_users, address_id, "Address"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], address, address_id
+ )
+
+
+def test_delete_public_metadata_for_staff_address_as_another_staff(
+ staff_api_client, staff_users, address, permission_manage_staff
+):
+ # given
+ staff_user = staff_users[-1]
+ staff_user.addresses.add(address)
+ address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ address.save(update_fields=["metadata"])
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_staff, address_id, "Address"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], address, address_id
+ )
+
+
+def test_delete_public_metadata_for_staff_address_as_staff(staff_api_client, address):
+ # given
+ staff_user = staff_api_client.user
+ staff_user.addresses.add(address)
+ address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ address.save(update_fields=["metadata"])
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, None, address_id, "Address"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], address, address_id
+ )
+
+
+def test_delete_public_metadata_for_staff_address_as_app(
+ app_api_client, staff_user, address, permission_manage_staff
+):
+ # given
+ staff_user.addresses.add(address)
+ address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ address.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Address", address.pk),
+ "keys": [PUBLIC_KEY],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ DELETE_PUBLIC_METADATA_MUTATION % "Address",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_delete_public_metadata_for_myself_address(staff_api_client, address):
+ # given
+ staff = staff_api_client.user
+ staff.addresses.add(address)
+ address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ staff.save(update_fields=["metadata"])
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, None, address_id, "Address"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], address, address_id
+ )
+
+
+def test_delete_private_metadata_for_customer_as_staff(
+ staff_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ customer_user.save(update_fields=["private_metadata"])
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_delete_private_metadata_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ customer_user.save(update_fields=["private_metadata"])
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ app_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_delete_multiple_private_metadata_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_metadata(
+ {PUBLIC_KEY: PUBLIC_VALUE, PUBLIC_KEY2: PUBLIC_VALUE2}
+ )
+ customer_user.save(update_fields=["metadata"])
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_multiple_items(
+ app_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_without_multiple_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_delete_private_metadata_for_other_staff_as_staff(
+ staff_api_client, permission_manage_staff, admin_user
+):
+ # given
+ assert admin_user.pk != staff_api_client.user.pk
+ admin_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ admin_user.save(update_fields=["private_metadata"])
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_staff, admin_id, "User"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], admin_user, admin_id
+ )
+
+
+def test_delete_private_metadata_for_staff_as_app_no_permission(
+ app_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ admin_user.save(update_fields=["private_metadata"])
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+ variables = {
+ "id": admin_id,
+ "keys": [PRIVATE_KEY],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ DELETE_PRIVATE_METADATA_MUTATION % "User",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_delete_private_metadata_for_myself_as_customer_no_permission(user_api_client):
+ # given
+ customer = user_api_client.user
+ customer.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ customer.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("User", customer.pk),
+ "keys": [PRIVATE_KEY],
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ DELETE_PRIVATE_METADATA_MUTATION % "User", variables, permissions=[]
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_delete_private_metadata_for_myself_as_staff_no_permission(
+ staff_api_client, permission_manage_users
+):
+ # given
+ staff = staff_api_client.user
+ staff.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ staff.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("User", staff.pk),
+ "keys": [PRIVATE_KEY],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ DELETE_PRIVATE_METADATA_MUTATION % "User",
+ variables,
+ permissions=[permission_manage_users],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_delete_private_metadata_for_customer_address_as_staff(
+ staff_api_client, permission_manage_users, customer_user
+):
+ # given
+ address = customer_user.addresses.first()
+ address.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ address.save(update_fields=["private_metadata"])
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_users, address_id, "Address"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], address, address_id
+ )
+
+
+def test_delete_private_metadata_for_customer_address_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ address = customer_user.addresses.first()
+ address.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ address.save(update_fields=["private_metadata"])
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ app_api_client, permission_manage_users, address_id, "Address"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], address, address_id
+ )
+
+
+def test_delete_private_metadata_for_staff_address_as_staff(
+ staff_api_client, address, permission_manage_staff
+):
+ # given
+ staff_user = staff_api_client.user
+ staff_user.addresses.add(address)
+ address.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ address.save(update_fields=["private_metadata"])
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_staff, address_id, "Address"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], address, address_id
+ )
+
+
+def test_delete_private_metadata_for_staff_address_as_app(
+ app_api_client, staff_user, address, permission_manage_staff
+):
+ # given
+ staff_user.addresses.add(address)
+ address.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ address.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Address", address.pk),
+ "keys": [PRIVATE_KEY],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ DELETE_PRIVATE_METADATA_MUTATION % "Address",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_delete_private_metadata_for_myself_address_as_staff_no_permission(
+ staff_api_client, address, permission_manage_users
+):
+ # given
+ staff = staff_api_client.user
+ staff.addresses.add(address)
+ staff.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ staff.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Address", address.pk),
+ "keys": [PRIVATE_KEY],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ DELETE_PRIVATE_METADATA_MUTATION % "Address",
+ variables,
+ permissions=[permission_manage_users],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_delete_public_metadata_for_customer_as_staff(
+ staff_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ customer_user.save(update_fields=["metadata"])
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_delete_public_metadata_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ customer_user.save(update_fields=["metadata"])
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ app_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_delete_multiple_public_metadata_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_metadata(
+ {PUBLIC_KEY: PUBLIC_VALUE, PUBLIC_KEY2: PUBLIC_VALUE2}
+ )
+ customer_user.save(update_fields=["metadata"])
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_multiple_items(
+ app_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_without_multiple_public_metadata(
+ response["data"]["deleteMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_delete_public_metadata_for_other_staff_as_staff(
+ staff_api_client, permission_manage_staff, admin_user
+):
+ # given
+ assert admin_user.pk != staff_api_client.user.pk
+ admin_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ admin_user.save(update_fields=["metadata"])
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_staff, admin_id, "User"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], admin_user, admin_id
+ )
+
+
+def test_delete_public_metadata_for_staff_as_app_no_permission(
+ app_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ admin_user.save(update_fields=["metadata"])
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+ variables = {
+ "id": admin_id,
+ "keys": [PRIVATE_KEY],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ DELETE_PUBLIC_METADATA_MUTATION % "User",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_delete_public_metadata_for_myself_as_customer(user_api_client):
+ # given
+ customer = user_api_client.user
+ customer.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ customer.save(update_fields=["metadata"])
+ customer_id = graphene.Node.to_global_id("User", customer.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ user_api_client, None, customer_id, "User"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], customer, customer_id
+ )
+
+
+def test_delete_public_metadata_for_myself_as_staff(staff_api_client):
+ # given
+ staff = staff_api_client.user
+ staff.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ staff.save(update_fields=["metadata"])
+ staff_id = graphene.Node.to_global_id("User", staff.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, None, staff_id, "User"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], staff, staff_id
+ )
+
+
+def test_update_public_metadata_for_customer_address_by_logged_user(
+ user_api_client, address
+):
+ # given
+ user = user_api_client.user
+ user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ user_api_client, None, address_id, "Address", value="NewMetaValue"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ address,
+ address_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_public_metadata_for_customer_address_by_different_logged_user(
+ user2_api_client, customer_user
+):
+ # given
+ address = customer_user.addresses.first()
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = user2_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_public_metadata_for_customer_address_by_staff_with_perm(
+ staff_api_client, customer_user, permission_manage_users
+):
+ # given
+ address = customer_user.addresses.first()
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_users,
+ address_id,
+ "Address",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ address,
+ address_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_public_metadata_for_customer_address_by_app_with_perm(
+ app_api_client, customer_user, permission_manage_users
+):
+ # given
+ address = customer_user.addresses.first()
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ app_api_client,
+ permission_manage_users,
+ address_id,
+ "Address",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ address,
+ address_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_public_metadata_for_address_by_non_logged_user(
+ api_client, customer_user
+):
+ # given
+ address = customer_user.addresses.first()
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+ variables = {
+ "id": address_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_public_metadata_for_staff_address_by_staff_with_perm(
+ staff_api_client, address, staff_users, permission_manage_staff
+):
+ # given
+ user = staff_users[-1]
+ user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_staff,
+ address_id,
+ "Address",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ address,
+ address_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_public_metadata_for_staff_address_by_staff_without_perm(
+ staff_api_client, address, staff_users
+):
+ # given
+ user = staff_users[-1]
+ user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_public_metadata_for_staff_address_by_customer(
+ user_api_client, address, staff_users
+):
+ # given
+ user = staff_users[-1]
+ user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_public_metadata_for_staff_address_by_app_with_perm(
+ app_api_client, staff_user, address, permission_manage_staff
+):
+ # given
+ staff_user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Address",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_public_metadata_for_staff_address_by_app_without_perm(
+ app_api_client, staff_user, address
+):
+ # given
+ staff_user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_public_metadata_for_warehouse_address_by_staff(
+ staff_api_client, warehouse, permission_manage_staff
+):
+ # given
+ address = warehouse.address
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Address",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_public_metadata_for_site_settings_address_by_staff(
+ staff_api_client, site_settings, address, permission_manage_staff
+):
+ # given
+ site_settings.company_address = address
+ site_settings.save(update_fields=["company_address"])
+
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Address",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_add_public_metadata_for_customer_as_staff(
+ staff_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_add_public_metadata_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ app_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_change_metadata_for_non_existing_user(app_api_client, customer_user):
+ # given the non-existing user ID
+ last_id = User.objects.order_by("id").values_list("id", flat=True).last()
+ customer_id = graphene.Node.to_global_id("User", last_id + 100)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ app_api_client, [], customer_id, "User"
+ )
+
+ # then
+ assert (
+ response["data"]["updateMetadata"]["errors"][0]["code"]
+ == AccountErrorCode.NOT_FOUND.name
+ )
+
+
+def test_add_multiple_public_metadata_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_update_public_metadata_for_multiple_items(
+ app_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_contains_multiple_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_add_public_metadata_for_other_staff_as_staff(
+ staff_api_client, permission_manage_staff, admin_user
+):
+ # given
+ assert admin_user.pk != staff_api_client.user.pk
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_staff, admin_id, "User"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], admin_user, admin_id
+ )
+
+
+ "input",
+ [{"key": " ", "value": "test"}, {"key": " ", "value": ""}],
+)
+def test_staff_update_metadata_empty_key(
+ input, staff_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_staff,
+ admin_id,
+ "User",
+ input["key"],
+ input["value"],
+ )
+
+ # then
+ data = response["data"]["updateMetadata"]
+ errors = data["errors"]
+
+ assert not data["item"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == MetadataErrorCode.REQUIRED.name
+ assert errors[0]["field"] == "input"
+
+
+def test_add_public_metadata_for_myself_as_customer(user_api_client):
+ # given
+ customer = user_api_client.user
+ customer_id = graphene.Node.to_global_id("User", customer.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ user_api_client, None, customer_id, "User"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], customer, customer_id
+ )
+
+
+def test_add_public_metadata_for_myself_as_staff(staff_api_client):
+ # given
+ staff = staff_api_client.user
+ staff_id = graphene.Node.to_global_id("User", staff.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, None, staff_id, "User"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], staff, staff_id
+ )
+
+
+def test_add_private_metadata_for_customer_as_staff(
+ staff_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_add_private_metadata_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ app_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_add_multiple_private_metadata_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_id = graphene.Node.to_global_id("User", customer_user.pk)
+
+ # when
+ response = execute_update_private_metadata_for_multiple_items(
+ app_api_client, permission_manage_users, customer_id, "User"
+ )
+
+ # then
+ assert item_contains_multiple_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], customer_user, customer_id
+ )
+
+
+def test_add_private_metadata_for_other_staff_as_staff(
+ staff_api_client, permission_manage_staff, admin_user
+):
+ # given
+ assert admin_user.pk != staff_api_client.user.pk
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_staff, admin_id, "User"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], admin_user, admin_id
+ )
+
+
+def test_add_public_metadata_for_staff_as_app_no_permission(
+ app_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+ variables = {
+ "id": admin_id,
+ "input": [{"key": PUBLIC_KEY, "value": PUBLIC_VALUE}],
+ }
+
+ # when
+
+ response = app_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "User",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_add_private_metadata_for_staff_as_app_no_permission(
+ app_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+ variables = {
+ "id": admin_id,
+ "input": [{"key": PRIVATE_KEY, "value": PRIVATE_VALUE}],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "User",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_add_private_metadata_for_myself_as_customer_no_permission(user_api_client):
+ # given
+ customer = user_api_client.user
+ variables = {
+ "id": graphene.Node.to_global_id("User", customer.pk),
+ "input": [{"key": PRIVATE_KEY, "value": PRIVATE_VALUE}],
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "User",
+ variables,
+ permissions=[],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+ "input",
+ [{"key": " ", "value": "test"}, {"key": " ", "value": ""}],
+)
+def test_staff_update_private_metadata_empty_key(
+ input, staff_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_id = graphene.Node.to_global_id("User", admin_user.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_staff,
+ admin_id,
+ "User",
+ input["key"],
+ input["value"],
+ )
+
+ # then
+ data = response["data"]["updatePrivateMetadata"]
+ errors = data["errors"]
+
+ assert not data["item"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == MetadataErrorCode.REQUIRED.name
+ assert errors[0]["field"] == "input"
+
+
+def test_add_private_metadata_for_myself_as_staff(staff_api_client):
+ # given
+ staff = staff_api_client.user
+ variables = {
+ "id": graphene.Node.to_global_id("User", staff.pk),
+ "input": [{"key": PRIVATE_KEY, "value": PRIVATE_VALUE}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "User",
+ variables,
+ permissions=[],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_customer_address_by_logged_user(
+ user_api_client, address
+):
+ # given
+ user = user_api_client.user
+ user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_customer_address_by_different_logged_user(
+ user2_api_client, customer_user
+):
+ # given
+ address = customer_user.addresses.first()
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = user2_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_customer_address_by_staff_with_perm(
+ staff_api_client, customer_user, permission_manage_users
+):
+ # given
+ address = customer_user.addresses.first()
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_users,
+ address_id,
+ "Address",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ address,
+ address_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_private_metadata_for_customer_address_by_app_with_perm(
+ app_api_client, customer_user, permission_manage_users
+):
+ # given
+ address = customer_user.addresses.first()
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ app_api_client,
+ permission_manage_users,
+ address_id,
+ "Address",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ address,
+ address_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_private_metadata_for_address_by_non_logged_user(
+ api_client, customer_user
+):
+ # given
+ address = customer_user.addresses.first()
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+ variables = {
+ "id": address_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_staff_address_by_staff_with_perm(
+ staff_api_client, address, staff_users, permission_manage_staff
+):
+ # given
+ user = staff_users[-1]
+ user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_staff,
+ address_id,
+ "Address",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ address,
+ address_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_private_metadata_for_staff_address_by_staff_without_perm(
+ staff_api_client, address, staff_users
+):
+ # given
+ user = staff_users[-1]
+ user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_staff_address_by_customer(
+ user_api_client, address, staff_users
+):
+ # given
+ user = staff_users[-1]
+ user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_staff_address_by_app_with_perm(
+ app_api_client, staff_user, address, permission_manage_staff
+):
+ # given
+ staff_user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Address",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_staff_address_by_app_without_perm(
+ app_api_client, staff_user, address
+):
+ # given
+ staff_user.addresses.add(address)
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_warehouse_address_by_staff(
+ staff_api_client, warehouse, permission_manage_staff
+):
+ # given
+ address = warehouse.address
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Address",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_site_settings_address_by_staff(
+ staff_api_client, site_settings, address, permission_manage_staff
+):
+ # given
+ site_settings.company_address = address
+ site_settings.save(update_fields=["company_address"])
+
+ address_id = graphene.Node.to_global_id("Address", address.pk)
+
+ variables = {
+ "id": address_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Address",
+ variables,
+ permissions=[permission_manage_staff],
+ )
+
+ # then
+ assert_no_permission(response)
diff --git a/saleor/graphql/meta/tests/mutations/test_app.py b/saleor/graphql/meta/tests/mutations/test_app.py
new file mode 100644
index 0000000000..51b4e29400
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_app.py
@@ -0,0 +1,295 @@
+import graphene
+
+from .....app.models import App
+from .....core.jwt import create_access_token_for_app
+from ....tests.fixtures import ApiClient
+from ....tests.utils import assert_no_permission
+from . import PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+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_delete_private_metadata_for_app(staff_api_client, permission_manage_apps, app):
+ # given
+ app.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ app.save(update_fields=["private_metadata"])
+ app_id = graphene.Node.to_global_id("App", app.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_apps,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ app,
+ app_id,
+ )
+
+
+def test_delete_public_metadata_for_app(staff_api_client, permission_manage_apps, app):
+ # given
+ app.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ app.save(update_fields=["metadata"])
+ app_id = graphene.Node.to_global_id("App", app.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_apps,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], app, app_id
+ )
+
+
+def test_add_public_metadata_for_sale(
+ staff_api_client, permission_manage_discounts, sale
+):
+ # given
+ sale_id = graphene.Node.to_global_id("Sale", sale.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_discounts, sale_id, "Sale"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], sale, sale_id
+ )
+
+
+def test_add_public_metadata_for_app(staff_api_client, permission_manage_apps, app):
+ # given
+ app_id = graphene.Node.to_global_id("App", app.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_apps,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], app, app_id
+ )
+
+
+def test_add_public_metadata_for_app_by_different_app(
+ app_api_client, permission_manage_apps, app, payment_app
+):
+ # given
+ app_id = graphene.Node.to_global_id("App", payment_app.pk)
+ app_api_client.app = app
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ app_api_client,
+ permission_manage_apps,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], payment_app, app_id
+ )
+
+
+def test_add_public_metadata_for_app_that_is_owner(
+ app_api_client, permission_manage_apps, app
+):
+ # given
+ app_id = graphene.Node.to_global_id("App", app.pk)
+ app_api_client.app = app
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ app_api_client,
+ None,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], app, app_id
+ )
+
+
+def test_add_public_metadata_for_app_by_staff_without_permissions(
+ staff_api_client, app
+):
+ # given
+ app_id = graphene.Node.to_global_id("App", app.pk)
+ variables = {
+ "id": app_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "App", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_add_public_metadata_for_app_with_app_user_token(app, staff_user):
+ # given
+ token = create_access_token_for_app(app, staff_user)
+ api_client = ApiClient(user=staff_user)
+ api_client.token = token
+ app_id = graphene.Node.to_global_id("App", app.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client,
+ None,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], app, app_id
+ )
+
+
+def test_add_public_metadata_for_unrelated_app_by_app_user_token(staff_user, app):
+ # given
+ token = create_access_token_for_app(app, staff_user)
+ api_client = ApiClient(user=staff_user)
+ api_client.token = token
+ second_app = App.objects.create(name="Sample app", is_active=True)
+ app_id = graphene.Node.to_global_id("App", second_app.pk)
+
+ variables = {
+ "id": app_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "App", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_add_private_metadata_for_app_by_different_app(
+ app_api_client, permission_manage_apps, app, payment_app
+):
+ # given
+ app_id = graphene.Node.to_global_id("App", payment_app.pk)
+ app_api_client.app = app
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ app_api_client,
+ permission_manage_apps,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ payment_app,
+ app_id,
+ )
+
+
+def test_add_private_metadata_for_app_with_app_user_token(
+ staff_user, permission_manage_apps, app, payment_app
+):
+ # given
+ token = create_access_token_for_app(app, staff_user)
+ api_client = ApiClient(user=staff_user)
+ api_client.token = token
+ app_id = graphene.Node.to_global_id("App", app.pk)
+
+ variables = {
+ "id": app_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "App", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_add_private_metadata_by_app_that_is_owner(
+ app_api_client, permission_manage_apps, app
+):
+ # given
+ app_id = graphene.Node.to_global_id("App", app.pk)
+ app_api_client.app = app
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ app_api_client,
+ None,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ app,
+ app_id,
+ )
+
+
+def test_add_private_metadata_for_app(staff_api_client, permission_manage_apps, app):
+ # given
+ app_id = graphene.Node.to_global_id("App", app.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_apps,
+ app_id,
+ "App",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ app,
+ app_id,
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_attribute.py b/saleor/graphql/meta/tests/mutations/test_attribute.py
new file mode 100644
index 0000000000..0c3d76c680
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_attribute.py
@@ -0,0 +1,48 @@
+import graphene
+
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_add_private_metadata_for_product_attribute(
+ staff_api_client, permission_manage_product_types_and_attributes, color_attribute
+):
+ # given
+ attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ attribute_id,
+ "Attribute",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], color_attribute, attribute_id
+ )
+
+
+def test_add_private_metadata_for_page_attribute(
+ staff_api_client, permission_manage_page_types_and_attributes, size_page_attribute
+):
+ # given
+ attribute_id = graphene.Node.to_global_id("Attribute", size_page_attribute.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_page_types_and_attributes,
+ attribute_id,
+ "Attribute",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ size_page_attribute,
+ attribute_id,
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_checkout.py b/saleor/graphql/meta/tests/mutations/test_checkout.py
new file mode 100644
index 0000000000..c717e6dee4
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_checkout.py
@@ -0,0 +1,355 @@
+from unittest.mock import patch
+
+import graphene
+
+from . import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_public_metadata_for_checkout(api_client, checkout):
+ # given
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, checkout_id, "Checkout"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_delete_public_metadata_for_checkout_by_token(api_client, checkout):
+ # given
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, checkout.token, "Checkout"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_delete_public_metadata_for_checkout_line(api_client, checkout_line):
+ # given
+ checkout_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout_line.save(update_fields=["metadata"])
+ checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, checkout_line_id, "CheckoutLine"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], checkout_line, checkout_line_id
+ )
+
+
+def test_delete_private_metadata_for_checkout(
+ staff_api_client, checkout, permission_manage_checkouts
+):
+ # given
+ checkout.metadata_storage.store_value_in_private_metadata(
+ {PRIVATE_KEY: PRIVATE_VALUE}
+ )
+ checkout.metadata_storage.save(update_fields=["private_metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_checkouts, checkout_id, "Checkout"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_delete_private_metadata_for_checkout_by_token(
+ staff_api_client, checkout, permission_manage_checkouts
+):
+ # given
+ checkout.metadata_storage.store_value_in_private_metadata(
+ {PRIVATE_KEY: PRIVATE_VALUE}
+ )
+ checkout.metadata_storage.save(update_fields=["private_metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_checkouts, checkout.token, "Checkout"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_delete_private_metadata_for_checkout_line(
+ staff_api_client, checkout_line, permission_manage_checkouts
+):
+ # given
+ checkout_line.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ checkout_line.save(update_fields=["private_metadata"])
+ checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_checkouts, checkout_line_id, "CheckoutLine"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ checkout_line,
+ checkout_line_id,
+ )
+
+
+def test_add_public_metadata_for_checkout(api_client, checkout):
+ # given
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout_id, "Checkout"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_add_public_metadata_for_checkout_no_checkout_metadata_storage(
+ api_client, checkout
+):
+ # given
+ checkout.metadata_storage.delete()
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout_id, "Checkout"
+ )
+
+ # then
+ checkout.refresh_from_db()
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_add_public_metadata_for_checkout_line(api_client, checkout_line):
+ # given
+ checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout_line_id, "CheckoutLine"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], checkout_line, checkout_line_id
+ )
+
+
+def test_add_public_metadata_for_checkout_by_token(api_client, checkout):
+ # given
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout.token, "Checkout"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+@patch("saleor.plugins.manager.PluginsManager.checkout_updated")
+def test_add_metadata_for_checkout_triggers_checkout_updated_hook(
+ mock_checkout_updated, api_client, checkout
+):
+ # given
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout_id, "Checkout"
+ )
+
+ # then
+ assert response["data"]["updateMetadata"]["errors"] == []
+ mock_checkout_updated.assert_called_once_with(checkout)
+
+
+def test_add_private_metadata_for_checkout(
+ staff_api_client, checkout, permission_manage_checkouts
+):
+ # given
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_checkouts, checkout_id, "Checkout"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_add_private_metadata_for_checkout_no_checkout_metadata_storage(
+ staff_api_client, checkout, permission_manage_checkouts
+):
+ # given
+ checkout.metadata_storage.delete()
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_checkouts, checkout_id, "Checkout"
+ )
+
+ # then
+ checkout.refresh_from_db()
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_add_private_metadata_for_checkout_line(
+ staff_api_client, checkout_line, permission_manage_checkouts
+):
+ # given
+ checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_checkouts, checkout_line_id, "CheckoutLine"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ checkout_line,
+ checkout_line_id,
+ )
+
+
+def test_add_private_metadata_for_checkout_by_token(
+ staff_api_client, checkout, permission_manage_checkouts
+):
+ # given
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_checkouts, checkout.token, "Checkout"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_update_private_metadata_for_checkout_line(
+ staff_api_client, checkout_line, permission_manage_checkouts
+):
+ # given
+ checkout_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout_line.save(update_fields=["private_metadata"])
+ checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_checkouts,
+ checkout_line_id,
+ "CheckoutLine",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ checkout_line,
+ checkout_line_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_public_metadata_for_checkout_line(api_client, checkout_line):
+ # given
+ checkout_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout_line.save(update_fields=["metadata"])
+ checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout_line_id, "CheckoutLine", value="NewMetaValue"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ checkout_line,
+ checkout_line_id,
+ value="NewMetaValue",
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_delete_metadata.py b/saleor/graphql/meta/tests/mutations/test_delete_metadata.py
new file mode 100644
index 0000000000..76e3c0e45c
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_delete_metadata.py
@@ -0,0 +1,187 @@
+import base64
+
+import graphene
+
+from .....core.error_codes import MetadataErrorCode
+from .....core.models import ModelWithMetadata
+from ....tests.utils import get_graphql_content
+from . import PUBLIC_KEY, PUBLIC_KEY2, PUBLIC_VALUE, PUBLIC_VALUE2
+from .test_update_metadata import item_contains_proper_public_metadata
+
+DELETE_PUBLIC_METADATA_MUTATION = """
+mutation DeletePublicMetadata($id: ID!, $keys: [String!]!) {
+ deleteMetadata(
+ id: $id
+ keys: $keys
+ ) {
+ errors{
+ field
+ code
+ }
+ item {
+ metadata{
+ key
+ value
+ }
+ ...on %s{
+ id
+ }
+ }
+ }
+}
+"""
+
+
+def execute_clear_public_metadata_for_item(
+ client,
+ permissions,
+ item_id,
+ item_type,
+ key=PUBLIC_KEY,
+):
+ variables = {
+ "id": item_id,
+ "keys": [key],
+ }
+ response = client.post_graphql(
+ DELETE_PUBLIC_METADATA_MUTATION % item_type,
+ variables,
+ permissions=[permissions] if permissions else None,
+ )
+ response = get_graphql_content(response)
+ return response
+
+
+def execute_clear_public_metadata_for_multiple_items(
+ client, permissions, item_id, item_type, key=PUBLIC_KEY, key2=PUBLIC_KEY2
+):
+ variables = {
+ "id": item_id,
+ "keys": [key, key2],
+ }
+
+ response = client.post_graphql(
+ DELETE_PUBLIC_METADATA_MUTATION % item_type,
+ variables,
+ permissions=[permissions] if permissions else None,
+ )
+ response = get_graphql_content(response)
+ return response
+
+
+def item_without_public_metadata(
+ item_from_response,
+ item,
+ item_id,
+ key=PUBLIC_KEY,
+ value=PUBLIC_VALUE,
+):
+ if item_from_response["id"] != item_id:
+ return False
+ item.refresh_from_db()
+ return item.get_value_from_metadata(key) != value
+
+
+def item_without_multiple_public_metadata(
+ item_from_response,
+ item,
+ item_id,
+ key=PUBLIC_KEY,
+ value=PUBLIC_VALUE,
+ key2=PUBLIC_KEY2,
+ value2=PUBLIC_VALUE2,
+):
+ if item_from_response["id"] != item_id:
+ return False
+ item.refresh_from_db()
+ return all(
+ [
+ item.get_value_from_metadata(key) != value,
+ item.get_value_from_metadata(key2) != value2,
+ ]
+ )
+
+
+def test_delete_public_metadata_for_non_exist_item(
+ staff_api_client, permission_manage_payments
+):
+ # given
+ payment_id = "Payment: 0"
+ payment_id = base64.b64encode(str.encode(payment_id)).decode("utf-8")
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_payments, payment_id, "Checkout"
+ )
+
+ # then
+ errors = response["data"]["deleteMetadata"]["errors"]
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
+
+
+def test_delete_public_metadata_for_item_without_meta(
+ api_client, permission_group_manage_users
+):
+ # given
+ group = permission_group_manage_users
+ assert not issubclass(type(group), ModelWithMetadata)
+ group_id = graphene.Node.to_global_id("Group", group.pk)
+
+ # when
+ # We use "User" type inside mutation for valid graphql query with fragment
+ # without this we are not able to reuse DELETE_PUBLIC_METADATA_MUTATION
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, group_id, "User"
+ )
+
+ # then
+ errors = response["data"]["deleteMetadata"]["errors"]
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
+
+
+def test_delete_public_metadata_for_not_exist_key(api_client, checkout):
+ # given
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, checkout_id, "Checkout", key="Not-exits"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["deleteMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_delete_public_metadata_for_one_key(api_client, checkout):
+ # given
+ checkout.metadata_storage.store_value_in_metadata(
+ {PUBLIC_KEY: PUBLIC_VALUE, "to_clear": PUBLIC_VALUE},
+ )
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, checkout_id, "Checkout", key="to_clear"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["deleteMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ key="to_clear",
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_delete_private_metadata.py b/saleor/graphql/meta/tests/mutations/test_delete_private_metadata.py
new file mode 100644
index 0000000000..d3419463e9
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_delete_private_metadata.py
@@ -0,0 +1,209 @@
+import base64
+
+import graphene
+
+from .....core.error_codes import MetadataErrorCode
+from .....core.models import ModelWithMetadata
+from ....tests.utils import get_graphql_content
+from . import (
+ PRIVATE_KEY,
+ PRIVATE_VALUE,
+ PUBLIC_KEY,
+ PUBLIC_KEY2,
+ PUBLIC_VALUE,
+ PUBLIC_VALUE2,
+)
+from .test_update_private_metadata import item_contains_proper_private_metadata
+
+DELETE_PRIVATE_METADATA_MUTATION = """
+mutation DeletePrivateMetadata($id: ID!, $keys: [String!]!) {
+ deletePrivateMetadata(
+ id: $id
+ keys: $keys
+ ) {
+ errors{
+ field
+ code
+ }
+ item {
+ privateMetadata{
+ key
+ value
+ }
+ ...on %s{
+ id
+ }
+ }
+ }
+}
+"""
+
+
+def execute_clear_private_metadata_for_item(
+ client,
+ permissions,
+ item_id,
+ item_type,
+ key=PRIVATE_KEY,
+):
+ variables = {
+ "id": item_id,
+ "keys": [key],
+ }
+
+ response = client.post_graphql(
+ DELETE_PRIVATE_METADATA_MUTATION % item_type,
+ variables,
+ permissions=[permissions] if permissions else None,
+ )
+ response = get_graphql_content(response)
+ return response
+
+
+def execute_clear_private_metadata_for_multiple_items(
+ client, permissions, item_id, item_type, key=PUBLIC_KEY, key2=PUBLIC_KEY2
+):
+ variables = {
+ "id": item_id,
+ "keys": [key, key2],
+ }
+
+ response = client.post_graphql(
+ DELETE_PRIVATE_METADATA_MUTATION % item_type,
+ variables,
+ permissions=[permissions] if permissions else None,
+ )
+ response = get_graphql_content(response)
+ return response
+
+
+def item_without_private_metadata(
+ item_from_response,
+ item,
+ item_id,
+ key=PRIVATE_KEY,
+ value=PRIVATE_VALUE,
+):
+ if item_from_response["id"] != item_id:
+ return False
+ item.refresh_from_db()
+ return item.get_value_from_private_metadata(key) != value
+
+
+def item_without_multiple_private_metadata(
+ item_from_response,
+ item,
+ item_id,
+ key=PUBLIC_KEY,
+ value=PUBLIC_VALUE,
+ key2=PUBLIC_KEY2,
+ value2=PUBLIC_VALUE2,
+):
+ if item_from_response["id"] != item_id:
+ return False
+ item.refresh_from_db()
+ return all(
+ [
+ item.get_value_from_private_metadata(key) != value,
+ item.get_value_from_private_metadata(key2) != value2,
+ ]
+ )
+
+
+def test_delete_private_metadata_for_non_exist_item(
+ staff_api_client, permission_manage_payments
+):
+ # given
+ payment_id = "Payment: 0"
+ payment_id = base64.b64encode(str.encode(payment_id)).decode("utf-8")
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_payments, payment_id, "Payment"
+ )
+
+ # then
+ errors = response["data"]["deletePrivateMetadata"]["errors"]
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
+
+
+def test_delete_private_metadata_for_item_without_meta(
+ api_client, permission_group_manage_users
+):
+ # given
+ group = permission_group_manage_users
+ assert not issubclass(type(group), ModelWithMetadata)
+ group_id = graphene.Node.to_global_id("Group", group.pk)
+
+ # when
+ # We use "User" type inside mutation for valid graphql query with fragment
+ # without this we are not able to reuse DELETE_PRIVATE_METADATA_MUTATION
+ response = execute_clear_private_metadata_for_item(
+ api_client, None, group_id, "User"
+ )
+
+ # then
+ errors = response["data"]["deletePrivateMetadata"]["errors"]
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
+
+
+def test_delete_private_metadata_for_not_exist_key(
+ staff_api_client, checkout, permission_manage_checkouts
+):
+ # given
+ checkout.metadata_storage.store_value_in_private_metadata(
+ {PRIVATE_KEY: PRIVATE_VALUE}
+ )
+ checkout.metadata_storage.save(update_fields=["private_metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_checkouts,
+ checkout.token,
+ "Checkout",
+ key="Not-exits",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+
+
+def test_delete_private_metadata_for_one_key(
+ staff_api_client, checkout, permission_manage_checkouts
+):
+ # given
+ checkout.metadata_storage.store_value_in_private_metadata(
+ {PRIVATE_KEY: PRIVATE_VALUE, "to_clear": PRIVATE_VALUE},
+ )
+ checkout.metadata_storage.save(update_fields=["private_metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_checkouts,
+ checkout.token,
+ "Checkout",
+ key="to_clear",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ )
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ key="to_clear",
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_discount.py b/saleor/graphql/meta/tests/mutations/test_discount.py
new file mode 100644
index 0000000000..ec765d458c
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_discount.py
@@ -0,0 +1,146 @@
+import graphene
+
+from . import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_public_metadata_for_voucher(
+ staff_api_client, permission_manage_discounts, voucher
+):
+ # given
+ voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ voucher.save(update_fields=["metadata"])
+ voucher_id = graphene.Node.to_global_id("Voucher", voucher.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_discounts, voucher_id, "Voucher"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], voucher, voucher_id
+ )
+
+
+def test_delete_public_metadata_for_sale(
+ staff_api_client, permission_manage_discounts, sale
+):
+ # given
+ sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ sale.save(update_fields=["metadata"])
+ sale_id = graphene.Node.to_global_id("Sale", sale.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_discounts, sale_id, "Sale"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], sale, sale_id
+ )
+
+
+def test_delete_private_metadata_for_voucher(
+ staff_api_client, permission_manage_discounts, voucher
+):
+ # given
+ voucher.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ voucher.save(update_fields=["private_metadata"])
+ voucher_id = graphene.Node.to_global_id("Voucher", voucher.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_discounts, voucher_id, "Voucher"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], voucher, voucher_id
+ )
+
+
+def test_delete_private_metadata_for_sale(
+ staff_api_client, permission_manage_discounts, sale
+):
+ # given
+ sale.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ sale.save(update_fields=["private_metadata"])
+ sale_id = graphene.Node.to_global_id("Sale", sale.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_discounts, sale_id, "Sale"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], sale, sale_id
+ )
+
+
+def test_add_public_metadata_for_voucher(
+ staff_api_client, permission_manage_discounts, voucher
+):
+ # given
+ voucher_id = graphene.Node.to_global_id("Voucher", voucher.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_discounts, voucher_id, "Voucher"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], voucher, voucher_id
+ )
+
+
+def test_add_private_metadata_for_sale(
+ staff_api_client, permission_manage_discounts, sale
+):
+ # given
+ sale_id = graphene.Node.to_global_id("Sale", sale.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_discounts, sale_id, "Sale"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], sale, sale_id
+ )
+
+
+def test_add_private_metadata_for_voucher(
+ staff_api_client, permission_manage_discounts, voucher
+):
+ # given
+ voucher_id = graphene.Node.to_global_id("Voucher", voucher.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_discounts, voucher_id, "Voucher"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], voucher, voucher_id
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_gift_card.py b/saleor/graphql/meta/tests/mutations/test_gift_card.py
new file mode 100644
index 0000000000..8da4f240a2
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_gift_card.py
@@ -0,0 +1,91 @@
+import graphene
+
+from . import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_private_metadata_for_gift_card(
+ staff_api_client, permission_manage_gift_card, gift_card
+):
+ # given
+ gift_card.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ gift_card.save(update_fields=["private_metadata"])
+ gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_gift_card, gift_card_id, "GiftCard"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], gift_card, gift_card_id
+ )
+
+
+def test_delete_public_metadata_for_gift_card(
+ staff_api_client, permission_manage_gift_card, gift_card
+):
+ # given
+ gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ gift_card.save(update_fields=["metadata"])
+ gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_gift_card, gift_card_id, "GiftCard"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], gift_card, gift_card_id
+ )
+
+
+def test_add_public_metadata_for_gift_card(
+ staff_api_client, permission_manage_gift_card, gift_card
+):
+ # given
+ gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_gift_card, gift_card_id, "GiftCard"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], gift_card, gift_card_id
+ )
+
+
+def test_add_private_metadata_for_gift_card(
+ staff_api_client, permission_manage_gift_card, gift_card
+):
+ # given
+ gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_gift_card, gift_card_id, "GiftCard"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], gift_card, gift_card_id
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_invoice.py b/saleor/graphql/meta/tests/mutations/test_invoice.py
new file mode 100644
index 0000000000..7c90525227
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_invoice.py
@@ -0,0 +1,43 @@
+import graphene
+
+from .....invoice.models import Invoice
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_add_public_metadata_for_invoice(staff_api_client, permission_manage_orders):
+ # given
+ invoice = Invoice.objects.create(number="1/7/2020")
+ invoice_id = graphene.Node.to_global_id("Invoice", invoice.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_orders, invoice_id, "Invoice"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], invoice, invoice_id
+ )
+
+
+def test_add_private_metadata_for_invoice(staff_api_client, permission_manage_orders):
+ # given
+ invoice = Invoice.objects.create(number="1/7/2020")
+ invoice_id = graphene.Node.to_global_id("Invoice", invoice.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, invoice_id, "Invoice"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], invoice, invoice_id
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_menu.py b/saleor/graphql/meta/tests/mutations/test_menu.py
new file mode 100644
index 0000000000..3978b450f9
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_menu.py
@@ -0,0 +1,199 @@
+import graphene
+
+from . import PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_private_metadata_for_menu(
+ staff_api_client, permission_manage_menus, menu
+):
+ # given
+ menu.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ menu.save(update_fields=["metadata"])
+ menu_id = graphene.Node.to_global_id("Menu", menu.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_menus,
+ menu_id,
+ "Menu",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ menu,
+ menu_id,
+ )
+
+
+def test_delete_private_metadata_for_menu_item(
+ staff_api_client, permission_manage_menus, menu_item
+):
+ # given
+ menu_item.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ menu_item.save(update_fields=["metadata"])
+ menu_item_id = graphene.Node.to_global_id("MenuItem", menu_item.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_menus,
+ menu_item_id,
+ "MenuItem",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ menu_item,
+ menu_item_id,
+ )
+
+
+def test_delete_public_metadata_for_menu(
+ staff_api_client, permission_manage_menus, menu
+):
+ # given
+ menu.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ menu.save(update_fields=["metadata"])
+ menu_id = graphene.Node.to_global_id("Menu", menu.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_menus,
+ menu_id,
+ "Menu",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"],
+ menu,
+ menu_id,
+ )
+
+
+def test_delete_public_metadata_for_menu_item(
+ staff_api_client, permission_manage_menus, menu_item
+):
+ # given
+ menu_item.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ menu_item.save(update_fields=["metadata"])
+ menu_item_id = graphene.Node.to_global_id("MenuItem", menu_item.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_menus,
+ menu_item_id,
+ "MenuItem",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"],
+ menu_item,
+ menu_item_id,
+ )
+
+
+def test_add_public_metadata_for_menu(staff_api_client, permission_manage_menus, menu):
+ # given
+ menu_id = graphene.Node.to_global_id("Menu", menu.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_menus,
+ menu_id,
+ "Menu",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ menu,
+ menu_id,
+ )
+
+
+def test_add_public_metadata_for_menu_item(
+ staff_api_client, permission_manage_menus, menu_item
+):
+ # given
+ menu_item_id = graphene.Node.to_global_id("MenuItem", menu_item.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_menus,
+ menu_item_id,
+ "MenuItem",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ menu_item,
+ menu_item_id,
+ )
+
+
+def test_add_private_metadata_for_menu(staff_api_client, permission_manage_menus, menu):
+ # given
+ menu_id = graphene.Node.to_global_id("Menu", menu.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_menus,
+ menu_id,
+ "Menu",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ menu,
+ menu_id,
+ )
+
+
+def test_add_private_metadata_for_menu_item(
+ staff_api_client, permission_manage_menus, menu_item
+):
+ # given
+ menu_item_id = graphene.Node.to_global_id("MenuItem", menu_item.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_menus,
+ menu_item_id,
+ "MenuItem",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ menu_item,
+ menu_item_id,
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_order.py b/saleor/graphql/meta/tests/mutations/test_order.py
new file mode 100644
index 0000000000..08d08c57c8
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_order.py
@@ -0,0 +1,580 @@
+import graphene
+
+from .....payment.models import TransactionItem
+from . import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_public_metadata_for_order_by_id(api_client, order):
+ # given
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["metadata"])
+ order_id = graphene.Node.to_global_id("Order", order.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, order_id, "Order"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], order, order_id
+ )
+
+
+def test_delete_public_metadata_for_order_by_token(api_client, order):
+ # given
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["metadata"])
+ order_id = graphene.Node.to_global_id("Order", order.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, order.id, "Order"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], order, order_id
+ )
+
+
+def test_delete_public_metadata_for_draft_order_by_id(api_client, draft_order):
+ # given
+ draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ draft_order.save(update_fields=["metadata"])
+ draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, draft_order_id, "Order"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], draft_order, draft_order_id
+ )
+
+
+def test_delete_public_metadata_for_draft_order_by_token(api_client, draft_order):
+ # given
+ draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ draft_order.save(update_fields=["metadata"])
+ draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, draft_order.id, "Order"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], draft_order, draft_order_id
+ )
+
+
+def test_delete_public_metadata_for_order_line(api_client, order_line):
+ # given
+ order_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order_line.save(update_fields=["metadata"])
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ api_client, None, order_line_id, "OrderLine"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], order_line, order_line_id
+ )
+
+
+def test_delete_private_metadata_for_order_by_id(
+ staff_api_client, order, permission_manage_orders
+):
+ # given
+ order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ order.save(update_fields=["private_metadata"])
+ order_id = graphene.Node.to_global_id("Order", order.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, order_id, "Order"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], order, order_id
+ )
+
+
+def test_delete_private_metadata_for_order_by_token(
+ staff_api_client, order, permission_manage_orders
+):
+ # given
+ order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ order.save(update_fields=["private_metadata"])
+ order_id = graphene.Node.to_global_id("Order", order.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, order.id, "Order"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], order, order_id
+ )
+
+
+def test_delete_private_metadata_for_draft_order_by_id(
+ staff_api_client, draft_order, permission_manage_orders
+):
+ # given
+ draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ draft_order.save(update_fields=["private_metadata"])
+ draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, draft_order_id, "Order"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], draft_order, draft_order_id
+ )
+
+
+def test_delete_private_metadata_for_draft_order_by_token(
+ staff_api_client, draft_order, permission_manage_orders
+):
+ # given
+ draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ draft_order.save(update_fields=["private_metadata"])
+ draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, draft_order.id, "Order"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], draft_order, draft_order_id
+ )
+
+
+def test_delete_private_metadata_for_order_line(
+ staff_api_client, order_line, permission_manage_orders
+):
+ # given
+ order_line.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ order_line.save(update_fields=["private_metadata"])
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, order_line_id, "OrderLine"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], order_line, order_line_id
+ )
+
+
+def test_add_public_metadata_for_order_by_id(api_client, order):
+ # given
+ order_id = graphene.Node.to_global_id("Order", order.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, order_id, "Order"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], order, order_id
+ )
+
+
+def test_add_public_metadata_for_order_by_token(api_client, order):
+ # given
+ order_id = graphene.Node.to_global_id("Order", order.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, order.id, "Order"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], order, order_id
+ )
+
+
+def test_add_public_metadata_for_draft_order_by_id(api_client, draft_order):
+ # given
+ draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, draft_order_id, "Order"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], draft_order, draft_order_id
+ )
+
+
+def test_add_public_metadata_for_draft_order_by_token(api_client, draft_order):
+ # given
+ draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, draft_order.id, "Order"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], draft_order, draft_order_id
+ )
+
+
+def test_add_public_metadata_for_order_line(api_client, order_line):
+ # given
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, order_line_id, "OrderLine"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], order_line, order_line_id
+ )
+
+
+def test_update_public_metadata_for_order_line(api_client, order_line):
+ # given
+ order_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order_line.save(update_fields=["metadata"])
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, order_line_id, "OrderLine", value="NewMetaValue"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ order_line,
+ order_line_id,
+ value="NewMetaValue",
+ )
+
+
+def test_add_private_metadata_for_order_by_id(
+ staff_api_client, order, permission_manage_orders
+):
+ # given
+ order_id = graphene.Node.to_global_id("Order", order.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, order_id, "Order"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], order, order_id
+ )
+
+
+def test_add_private_metadata_for_order_by_token(
+ staff_api_client, order, permission_manage_orders
+):
+ # given
+ order_id = graphene.Node.to_global_id("Order", order.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, order.id, "Order"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], order, order_id
+ )
+
+
+def test_update_private_metadata_for_order_line(
+ staff_api_client, order_line, permission_manage_orders
+):
+ # given
+ order_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order_line.save(update_fields=["private_metadata"])
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_orders,
+ order_line_id,
+ "OrderLine",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ order_line,
+ order_line_id,
+ value="NewMetaValue",
+ )
+
+
+def test_add_private_metadata_for_draft_order_by_id(
+ staff_api_client, draft_order, permission_manage_orders
+):
+ # given
+ draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, draft_order_id, "Order"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], draft_order, draft_order_id
+ )
+
+
+def test_add_private_metadata_for_draft_order_by_token(
+ staff_api_client, draft_order, permission_manage_orders
+):
+ # given
+ draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, draft_order.id, "Order"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], draft_order, draft_order_id
+ )
+
+
+def test_add_private_metadata_for_order_line(
+ staff_api_client, order_line, permission_manage_orders
+):
+ # given
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, order_line_id, "OrderLine"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], order_line, order_line_id
+ )
+
+
+def test_delete_private_metadata_for_fulfillment(
+ staff_api_client, permission_manage_orders, fulfillment
+):
+ # given
+ fulfillment.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ fulfillment.save(update_fields=["private_metadata"])
+ fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, fulfillment_id, "Fulfillment"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], fulfillment, fulfillment_id
+ )
+
+
+def test_delete_public_metadata_for_fulfillment(
+ staff_api_client, permission_manage_orders, fulfillment
+):
+ # given
+ fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ fulfillment.save(update_fields=["metadata"])
+ fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_orders, fulfillment_id, "Fulfillment"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], fulfillment, fulfillment_id
+ )
+
+
+def test_delete_public_metadata_for_transaction_item(
+ staff_api_client, permission_manage_payments
+):
+ # given
+ transaction_item = TransactionItem.objects.create(
+ metadata={PUBLIC_KEY: PUBLIC_VALUE}
+ )
+ transaction_id = graphene.Node.to_global_id(
+ "TransactionItem", transaction_item.token
+ )
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_payments, transaction_id, "TransactionItem"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], transaction_item, transaction_id
+ )
+
+
+def test_add_public_metadata_for_transaction_item(
+ staff_api_client,
+ permission_manage_payments,
+):
+ # given
+ transaction_item = TransactionItem.objects.create()
+ transaction_id = graphene.Node.to_global_id(
+ "TransactionItem", transaction_item.token
+ )
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_payments, transaction_id, "TransactionItem"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], transaction_item, transaction_id
+ )
+
+
+def test_add_public_metadata_for_fulfillment(
+ staff_api_client, permission_manage_orders, fulfillment
+):
+ # given
+ fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_orders, fulfillment_id, "Fulfillment"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], fulfillment, fulfillment_id
+ )
+
+
+def test_add_private_metadata_for_category(
+ staff_api_client, permission_manage_products, category
+):
+ # given
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_products, category_id, "Category"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], category, category_id
+ )
+
+
+def test_add_private_metadata_for_collection(
+ staff_api_client, permission_manage_products, published_collection, channel_USD
+):
+ # given
+ collection = published_collection
+ collection_id = graphene.Node.to_global_id("Collection", collection.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_products, collection_id, "Collection"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], collection, collection_id
+ )
+
+
+def test_add_private_metadata_for_digital_content(
+ staff_api_client, permission_manage_products, digital_content
+):
+ # given
+ digital_content_id = graphene.Node.to_global_id(
+ "DigitalContent", digital_content.pk
+ )
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_products,
+ digital_content_id,
+ "DigitalContent",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ digital_content,
+ digital_content_id,
+ )
+
+
+def test_add_private_metadata_for_fulfillment(
+ staff_api_client, permission_manage_orders, fulfillment
+):
+ # given
+ fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_orders, fulfillment_id, "Fulfillment"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], fulfillment, fulfillment_id
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_page.py b/saleor/graphql/meta/tests/mutations/test_page.py
new file mode 100644
index 0000000000..7a9d149f62
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_page.py
@@ -0,0 +1,169 @@
+import graphene
+
+from . import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_private_metadata_for_page_attribute(
+ staff_api_client, permission_manage_page_types_and_attributes, size_page_attribute
+):
+ # given
+ size_page_attribute.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ size_page_attribute.save(update_fields=["private_metadata"])
+ attribute_id = graphene.Node.to_global_id("Attribute", size_page_attribute.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_page_types_and_attributes,
+ attribute_id,
+ "Attribute",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ size_page_attribute,
+ attribute_id,
+ )
+
+
+def test_delete_private_metadata_for_page(
+ staff_api_client, permission_manage_pages, page
+):
+ # given
+ page.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ page.save(update_fields=["private_metadata"])
+ page_id = graphene.Node.to_global_id("Page", page.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_pages,
+ page_id,
+ "Page",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ page,
+ page_id,
+ )
+
+
+def test_delete_public_metadata_for_page_attribute(
+ staff_api_client, permission_manage_page_types_and_attributes, size_page_attribute
+):
+ # given
+ size_page_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ size_page_attribute.save(update_fields=["metadata"])
+ attribute_id = graphene.Node.to_global_id("Attribute", size_page_attribute.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_page_types_and_attributes,
+ attribute_id,
+ "Attribute",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], size_page_attribute, attribute_id
+ )
+
+
+def test_delete_public_metadata_for_page(
+ staff_api_client, permission_manage_pages, page
+):
+ # given
+ page.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ page.save(update_fields=["metadata"])
+ page_id = graphene.Node.to_global_id("Page", page.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_pages,
+ page_id,
+ "Page",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], page, page_id
+ )
+
+
+def test_add_public_metadata_for_page_attribute(
+ staff_api_client, permission_manage_page_types_and_attributes, size_page_attribute
+):
+ # given
+ attribute_id = graphene.Node.to_global_id("Attribute", size_page_attribute.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_page_types_and_attributes,
+ attribute_id,
+ "Attribute",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], size_page_attribute, attribute_id
+ )
+
+
+def test_add_public_metadata_for_page(staff_api_client, permission_manage_pages, page):
+ # given
+ page_id = graphene.Node.to_global_id("Page", page.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_pages,
+ page_id,
+ "Page",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], page, page_id
+ )
+
+
+def test_add_private_metadata_for_page(staff_api_client, permission_manage_pages, page):
+ # given
+ page_id = graphene.Node.to_global_id("Page", page.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_pages,
+ page_id,
+ "Page",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ page,
+ page_id,
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_payment.py b/saleor/graphql/meta/tests/mutations/test_payment.py
new file mode 100644
index 0000000000..aeb3d9c0e0
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_payment.py
@@ -0,0 +1,217 @@
+import graphene
+
+from .....payment.models import TransactionItem
+from .....payment.utils import payment_owned_by_user
+from ....tests.utils import assert_no_permission
+from . import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+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_update_private_metadata_for_payment_by_staff(
+ staff_api_client, permission_manage_payments, payment_with_private_metadata
+):
+ # given
+ payment_id = graphene.Node.to_global_id("Payment", payment_with_private_metadata.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_payments,
+ payment_id,
+ "Payment",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ payment_with_private_metadata,
+ payment_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_private_metadata_for_payment_by_app(
+ app_api_client, permission_manage_payments, payment_with_private_metadata
+):
+ # given
+ payment_id = graphene.Node.to_global_id("Payment", payment_with_private_metadata.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ app_api_client,
+ permission_manage_payments,
+ payment_id,
+ "Payment",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ payment_with_private_metadata,
+ payment_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_private_metadata_for_payment_by_staff_without_permission(
+ staff_api_client, payment_with_private_metadata
+):
+ # given
+ payment_id = graphene.Node.to_global_id("Payment", payment_with_private_metadata.pk)
+ variables = {
+ "id": payment_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Payment", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_private_metadata_for_payment_by_app_without_permission(
+ app_api_client, payment
+):
+ # given
+ payment_id = graphene.Node.to_global_id("Payment", payment.pk)
+ variables = {
+ "id": payment_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Payment", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_delete_private_metadata_for_transaction_item(
+ staff_api_client, permission_manage_payments, voucher
+):
+ # given
+ transaction_item = TransactionItem.objects.create(
+ private_metadata={PRIVATE_KEY: PRIVATE_VALUE}
+ )
+ transaction_id = graphene.Node.to_global_id(
+ "TransactionItem", transaction_item.token
+ )
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_payments, transaction_id, "TransactionItem"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ transaction_item,
+ transaction_id,
+ )
+
+
+def test_update_public_metadata_for_payment_by_logged_user(
+ user_api_client, payment_with_public_metadata
+):
+ # given
+ payment_with_public_metadata.order.user = user_api_client.user
+ payment_with_public_metadata.order.save()
+ payment_id = graphene.Node.to_global_id("Payment", payment_with_public_metadata.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ user_api_client, None, payment_id, "Payment", value="NewMetaValue"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ payment_with_public_metadata,
+ payment_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_public_metadata_for_payment_by_different_logged_user(
+ user2_api_client, payment_with_public_metadata
+):
+ # given
+ assert not payment_owned_by_user(
+ payment_with_public_metadata.pk, user2_api_client.user
+ )
+ payment_id = graphene.Node.to_global_id("Payment", payment_with_public_metadata.pk)
+ variables = {
+ "id": payment_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = user2_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Payment", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_update_public_metadata_for_payment_by_non_logged_user(
+ api_client, payment_with_public_metadata
+):
+ # given
+ payment_id = graphene.Node.to_global_id("Payment", payment_with_public_metadata.pk)
+ variables = {
+ "id": payment_id,
+ "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Payment", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_add_private_metadata_for_transaction_item(
+ staff_api_client, permission_manage_payments
+):
+ # given
+ transaction_item = TransactionItem.objects.create(
+ private_metadata={PRIVATE_KEY: PRIVATE_VALUE}
+ )
+ transaction_id = graphene.Node.to_global_id(
+ "TransactionItem", transaction_item.token
+ )
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_payments, transaction_id, "TransactionItem"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ transaction_item,
+ transaction_id,
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_product.py b/saleor/graphql/meta/tests/mutations/test_product.py
new file mode 100644
index 0000000000..d39fc0ac34
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_product.py
@@ -0,0 +1,587 @@
+from unittest.mock import patch
+
+import graphene
+
+from . import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_private_metadata_for_product_attribute(
+ staff_api_client, permission_manage_product_types_and_attributes, color_attribute
+):
+ # given
+ color_attribute.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ color_attribute.save(update_fields=["private_metadata"])
+ attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ attribute_id,
+ "Attribute",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], color_attribute, attribute_id
+ )
+
+
+def test_delete_private_metadata_for_category(
+ staff_api_client, permission_manage_products, category
+):
+ # given
+ category.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ category.save(update_fields=["private_metadata"])
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_products, category_id, "Category"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], category, category_id
+ )
+
+
+def test_delete_private_metadata_for_collection(
+ staff_api_client, permission_manage_products, published_collection, channel_USD
+):
+ # given
+ collection = published_collection
+ collection.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ collection.save(update_fields=["private_metadata"])
+ collection_id = graphene.Node.to_global_id("Collection", collection.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_products, collection_id, "Collection"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], collection, collection_id
+ )
+
+
+def test_delete_private_metadata_for_digital_content(
+ staff_api_client, permission_manage_products, digital_content
+):
+ # given
+ digital_content.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ digital_content.save(update_fields=["private_metadata"])
+ digital_content_id = graphene.Node.to_global_id(
+ "DigitalContent", digital_content.pk
+ )
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_products,
+ digital_content_id,
+ "DigitalContent",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ digital_content,
+ digital_content_id,
+ )
+
+
+def test_delete_public_metadata_for_product_media(
+ staff_api_client, permission_manage_products, product_with_image
+):
+ # given
+ media = product_with_image.media.first()
+ media_id = graphene.Node.to_global_id("ProductMedia", media.pk)
+ media.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ media.save(update_fields=["metadata"])
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_products, media_id, "ProductMedia"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], media, media_id
+ )
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_delete_private_metadata_for_product(
+ updated_webhook_mock, staff_api_client, permission_manage_products, product
+):
+ # given
+ product.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ product.save(update_fields=["private_metadata"])
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_products, product_id, "Product"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], product, product_id
+ )
+ updated_webhook_mock.assert_called_once_with(product)
+
+
+def test_delete_private_metadata_for_product_type(
+ staff_api_client, permission_manage_product_types_and_attributes, product_type
+):
+ # given
+ product_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ product_type.save(update_fields=["private_metadata"])
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ product_type_id,
+ "ProductType",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], product_type, product_type_id
+ )
+
+
+def test_delete_private_metadata_for_product_variant(
+ staff_api_client, permission_manage_products, variant
+):
+ # given
+ variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ variant.save(update_fields=["private_metadata"])
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_products, variant_id, "ProductVariant"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], variant, variant_id
+ )
+
+
+def test_delete_private_metadata_for_product_media(
+ staff_api_client, permission_manage_products, product_with_image
+):
+ # given
+ media = product_with_image.media.first()
+ media_id = graphene.Node.to_global_id("ProductMedia", media.pk)
+ media.store_value_in_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ media.save(update_fields=["private_metadata"])
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_products, media_id, "ProductMedia"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], media, media_id
+ )
+
+
+def test_delete_public_metadata_for_product_attribute(
+ staff_api_client, permission_manage_product_types_and_attributes, color_attribute
+):
+ # given
+ color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ color_attribute.save(update_fields=["metadata"])
+ attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ attribute_id,
+ "Attribute",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], color_attribute, attribute_id
+ )
+
+
+def test_delete_public_metadata_for_category(
+ staff_api_client, permission_manage_products, category
+):
+ # given
+ category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ category.save(update_fields=["metadata"])
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_products, category_id, "Category"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], category, category_id
+ )
+
+
+def test_delete_public_metadata_for_collection(
+ staff_api_client, permission_manage_products, published_collection, channel_USD
+):
+ # given
+ collection = published_collection
+ collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ collection.save(update_fields=["metadata"])
+ collection_id = graphene.Node.to_global_id("Collection", collection.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_products, collection_id, "Collection"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], collection, collection_id
+ )
+
+
+def test_delete_public_metadata_for_digital_content(
+ staff_api_client, permission_manage_products, digital_content
+):
+ # given
+ digital_content.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ digital_content.save(update_fields=["metadata"])
+ digital_content_id = graphene.Node.to_global_id(
+ "DigitalContent", digital_content.pk
+ )
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_products,
+ digital_content_id,
+ "DigitalContent",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], digital_content, digital_content_id
+ )
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_delete_public_metadata_for_product(
+ updated_webhook_mock, staff_api_client, permission_manage_products, product
+):
+ # given
+ product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product.save(update_fields=["metadata"])
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_products, product_id, "Product"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], product, product_id
+ )
+ updated_webhook_mock.assert_called_once_with(product)
+
+
+def test_delete_public_metadata_for_product_type(
+ staff_api_client, permission_manage_product_types_and_attributes, product_type
+):
+ # given
+ product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product_type.save(update_fields=["metadata"])
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ product_type_id,
+ "ProductType",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], product_type, product_type_id
+ )
+
+
+def test_delete_public_metadata_for_product_variant(
+ staff_api_client, permission_manage_products, variant
+):
+ # given
+ variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ variant.save(update_fields=["metadata"])
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_products, variant_id, "ProductVariant"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], variant, variant_id
+ )
+
+
+def test_add_public_metadata_for_product_media(
+ staff_api_client, permission_manage_products, product_with_image
+):
+ # given
+ media = product_with_image.media.first()
+ media_id = graphene.Node.to_global_id("ProductMedia", media.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_products, media_id, "ProductMedia"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], media, media_id
+ )
+
+
+def test_add_public_metadata_for_product_attribute(
+ staff_api_client, permission_manage_product_types_and_attributes, color_attribute
+):
+ # given
+ attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ attribute_id,
+ "Attribute",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], color_attribute, attribute_id
+ )
+
+
+def test_add_public_metadata_for_category(
+ staff_api_client, permission_manage_products, category
+):
+ # given
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_products, category_id, "Category"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], category, category_id
+ )
+
+
+def test_add_public_metadata_for_collection(
+ staff_api_client, permission_manage_products, published_collection, channel_USD
+):
+ # given
+ collection_id = graphene.Node.to_global_id("Collection", published_collection.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_products, collection_id, "Collection"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], published_collection, collection_id
+ )
+
+
+def test_add_public_metadata_for_digital_content(
+ staff_api_client, permission_manage_products, digital_content
+):
+ # given
+ digital_content_id = graphene.Node.to_global_id(
+ "DigitalContent", digital_content.pk
+ )
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_products,
+ digital_content_id,
+ "DigitalContent",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], digital_content, digital_content_id
+ )
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_add_public_metadata_for_product(
+ updated_webhook_mock, staff_api_client, permission_manage_products, product
+):
+ # given
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_products, product_id, "Product"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], product, product_id
+ )
+ updated_webhook_mock.assert_called_once_with(product)
+
+
+def test_add_public_metadata_for_product_type(
+ staff_api_client, permission_manage_product_types_and_attributes, product_type
+):
+ # given
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ product_type_id,
+ "ProductType",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], product_type, product_type_id
+ )
+
+
+def test_add_public_metadata_for_product_variant(
+ staff_api_client, permission_manage_products, variant
+):
+ # given
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_products,
+ variant_id,
+ "ProductVariant",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], variant, variant_id
+ )
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_add_private_metadata_for_product(
+ updated_webhook_mock, staff_api_client, permission_manage_products, product
+):
+ # given
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_products, product_id, "Product"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], product, product_id
+ )
+ updated_webhook_mock.assert_called_once_with(product)
+
+
+def test_add_private_metadata_for_product_type(
+ staff_api_client, permission_manage_product_types_and_attributes, product_type
+):
+ # given
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_product_types_and_attributes,
+ product_type_id,
+ "ProductType",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], product_type, product_type_id
+ )
+
+
+def test_add_private_metadata_for_product_variant(
+ staff_api_client, permission_manage_products, variant
+):
+ # given
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_products,
+ variant_id,
+ "ProductVariant",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], variant, variant_id
+ )
+
+
+def test_add_private_metadata_for_product_media(
+ staff_api_client, permission_manage_products, product_with_image
+):
+ # given
+ media = product_with_image.media.first()
+ media_id = graphene.Node.to_global_id("ProductMedia", media.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_products, media_id, "ProductMedia"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], media, media_id
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_shipping.py b/saleor/graphql/meta/tests/mutations/test_shipping.py
new file mode 100644
index 0000000000..62e0d14a4c
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_shipping.py
@@ -0,0 +1,211 @@
+import graphene
+
+from . import PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_private_metadata_for_shipping_method(
+ staff_api_client, permission_manage_shipping, shipping_method
+):
+ # given
+ shipping_method.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ shipping_method.save(update_fields=["metadata"])
+ shipping_method_id = graphene.Node.to_global_id(
+ "ShippingMethodType", shipping_method.pk
+ )
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_shipping,
+ shipping_method_id,
+ "ShippingMethodType",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ shipping_method,
+ shipping_method_id,
+ )
+
+
+def test_delete_private_metadata_for_shipping_zone(
+ staff_api_client, permission_manage_shipping, shipping_zone
+):
+ # given
+ shipping_zone.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ shipping_zone.save(update_fields=["metadata"])
+ shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_shipping,
+ shipping_zone_id,
+ "ShippingZone",
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"],
+ shipping_zone,
+ shipping_zone_id,
+ )
+
+
+def test_delete_public_metadata_for_shipping_method(
+ staff_api_client, permission_manage_shipping, shipping_method
+):
+ # given
+ shipping_method.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ shipping_method.save(update_fields=["metadata"])
+ shipping_method_id = graphene.Node.to_global_id(
+ "ShippingMethodType", shipping_method.pk
+ )
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_shipping,
+ shipping_method_id,
+ "ShippingMethodType",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"],
+ shipping_method,
+ shipping_method_id,
+ )
+
+
+def test_delete_public_metadata_for_shipping_zone(
+ staff_api_client, permission_manage_shipping, shipping_zone
+):
+ # given
+ shipping_zone.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ shipping_zone.save(update_fields=["metadata"])
+ shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_shipping,
+ shipping_zone_id,
+ "ShippingZone",
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"],
+ shipping_zone,
+ shipping_zone_id,
+ )
+
+
+def test_add_public_metadata_for_shipping_method(
+ staff_api_client, permission_manage_shipping, shipping_method
+):
+ # given
+ shipping_method_id = graphene.Node.to_global_id(
+ "ShippingMethodType", shipping_method.pk
+ )
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_shipping,
+ shipping_method_id,
+ "ShippingMethodType",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ shipping_method,
+ shipping_method_id,
+ )
+
+
+def test_add_public_metadata_for_shipping_zone(
+ staff_api_client, permission_manage_shipping, shipping_zone
+):
+ # given
+ shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client,
+ permission_manage_shipping,
+ shipping_zone_id,
+ "ShippingZone",
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ shipping_zone,
+ shipping_zone_id,
+ )
+
+
+def test_add_private_metadata_for_shipping_method(
+ staff_api_client, permission_manage_shipping, shipping_method
+):
+ # given
+ shipping_method_id = graphene.Node.to_global_id(
+ "ShippingMethodType", shipping_method.pk
+ )
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_shipping,
+ shipping_method_id,
+ "ShippingMethodType",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ shipping_method,
+ shipping_method_id,
+ )
+
+
+def test_add_private_metadata_for_shipping_zone(
+ staff_api_client, permission_manage_shipping, shipping_zone
+):
+ # given
+ shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_shipping,
+ shipping_zone_id,
+ "ShippingZone",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ shipping_zone,
+ shipping_zone_id,
+ )
diff --git a/saleor/graphql/meta/tests/mutations/test_update_metadata.py b/saleor/graphql/meta/tests/mutations/test_update_metadata.py
new file mode 100644
index 0000000000..89d353d64d
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_update_metadata.py
@@ -0,0 +1,250 @@
+import base64
+from unittest.mock import patch
+
+import before_after
+import graphene
+import pytest
+from django.core.exceptions import ValidationError
+from django.db import transaction
+
+from .....checkout.models import Checkout
+from .....core.error_codes import MetadataErrorCode
+from .....core.models import ModelWithMetadata
+from ....tests.utils import get_graphql_content
+from . import PUBLIC_KEY, PUBLIC_KEY2, PUBLIC_VALUE, PUBLIC_VALUE2
+
+UPDATE_PUBLIC_METADATA_MUTATION = """
+mutation UpdatePublicMetadata($id: ID!, $input: [MetadataInput!]!) {
+ updateMetadata(
+ id: $id
+ input: $input
+ ) {
+ errors{
+ field
+ code
+ message
+ }
+ item {
+ metadata{
+ key
+ value
+ }
+ ...on %s{
+ id
+ }
+ }
+ }
+}
+"""
+
+
+def execute_update_public_metadata_for_item(
+ client,
+ permissions,
+ item_id,
+ item_type,
+ key=PUBLIC_KEY,
+ value=PUBLIC_VALUE,
+ ignore_errors=False,
+):
+ variables = {
+ "id": item_id,
+ "input": [{"key": key, "value": value}],
+ }
+
+ response = client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % item_type,
+ variables,
+ permissions=[permissions] if permissions else None,
+ )
+ response = get_graphql_content(response, ignore_errors=ignore_errors)
+ return response
+
+
+def execute_update_public_metadata_for_multiple_items(
+ client,
+ permissions,
+ item_id,
+ item_type,
+ key=PUBLIC_KEY,
+ value=PUBLIC_VALUE,
+ key2=PUBLIC_KEY2,
+ value2=PUBLIC_VALUE2,
+):
+ variables = {
+ "id": item_id,
+ "input": [{"key": key, "value": value}, {"key": key2, "value": value2}],
+ }
+
+ response = client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % item_type,
+ variables,
+ permissions=[permissions] if permissions else None,
+ )
+ response = get_graphql_content(response)
+ return response
+
+
+def item_contains_proper_public_metadata(
+ item_from_response,
+ item,
+ item_id,
+ key=PUBLIC_KEY,
+ value=PUBLIC_VALUE,
+):
+ if item_from_response["id"] != item_id:
+ return False
+ item.refresh_from_db()
+ return item.get_value_from_metadata(key) == value
+
+
+def item_contains_multiple_proper_public_metadata(
+ item_from_response,
+ item,
+ item_id,
+ key=PUBLIC_KEY,
+ value=PUBLIC_VALUE,
+ key2=PUBLIC_KEY2,
+ value2=PUBLIC_VALUE2,
+):
+ if item_from_response["id"] != item_id:
+ return False
+ item.refresh_from_db()
+ return all(
+ [
+ item.get_value_from_metadata(key) == value,
+ item.get_value_from_metadata(key2) == value2,
+ ]
+ )
+
+
+def test_meta_mutations_handle_validation_errors(staff_api_client):
+ # given
+ invalid_id = "6QjoLs5LIqb3At7hVKKcUlqXceKkFK"
+ variables = {
+ "id": invalid_id,
+ "input": [{"key": "year", "value": "of-saleor"}],
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ UPDATE_PUBLIC_METADATA_MUTATION % "Checkout", variables
+ )
+
+ # then
+ content = get_graphql_content(response)
+ errors = content["data"]["updateMetadata"]["errors"]
+ assert errors
+ assert errors[0]["code"] == MetadataErrorCode.INVALID.name
+
+
+@patch("saleor.plugins.manager.PluginsManager.checkout_updated")
+def test_base_metadata_mutation_handles_errors_from_extra_action(
+ mock_checkout_updated, api_client, checkout
+):
+ # given
+ error_field = "field"
+ error_msg = "boom"
+ mock_checkout_updated.side_effect = ValidationError({error_field: error_msg})
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout_id, "Checkout"
+ )
+
+ # then
+ errors = response["data"]["updateMetadata"]["errors"]
+ assert errors[0]["field"] == error_field
+ assert errors[0]["message"] == error_msg
+
+
+def test_update_public_metadata_for_item(api_client, checkout):
+ # given
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ api_client, None, checkout.token, "Checkout", value="NewMetaValue"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ value="NewMetaValue",
+ )
+
+
[email protected]_db(transaction=True)
+def test_update_public_metadata_for_item_on_deleted_instance(api_client, checkout):
+ # given
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.metadata_storage.save(update_fields=["metadata"])
+
+ def delete_checkout_object(*args, **kwargs):
+ with transaction.atomic():
+ Checkout.objects.filter(pk=checkout.pk).delete()
+
+ # when
+ with before_after.before(
+ "saleor.graphql.meta.mutations.update_metadata.save_instance",
+ delete_checkout_object,
+ ):
+ response = execute_update_public_metadata_for_item(
+ api_client,
+ None,
+ checkout.token,
+ "Checkout",
+ value="NewMetaValue",
+ ignore_errors=True,
+ )
+
+ # then
+ assert not Checkout.objects.filter(pk=checkout.pk).first()
+ assert (
+ response["data"]["updateMetadata"]["errors"][0]["code"]
+ == MetadataErrorCode.NOT_FOUND.name
+ )
+
+
+def test_update_public_metadata_for_non_exist_item(
+ staff_api_client, permission_manage_payments
+):
+ # given
+ payment_id = "Payment: 0"
+ payment_id = base64.b64encode(str.encode(payment_id)).decode("utf-8")
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_payments, payment_id, "Payment"
+ )
+
+ # then
+ errors = response["data"]["updateMetadata"]["errors"]
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
+
+
+def test_update_public_metadata_for_item_without_meta(
+ api_client, permission_group_manage_users
+):
+ # given
+ group = permission_group_manage_users
+ assert not issubclass(type(group), ModelWithMetadata)
+ group_id = graphene.Node.to_global_id("Group", group.pk)
+
+ # when
+ # We use "User" type inside mutation for valid graphql query with fragment
+ # without this we are not able to reuse UPDATE_PUBLIC_METADATA_MUTATION
+ response = execute_update_public_metadata_for_item(
+ api_client, None, group_id, "User"
+ )
+
+ # then
+ errors = response["data"]["updateMetadata"]["errors"]
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
diff --git a/saleor/graphql/meta/tests/mutations/test_update_private_metadata.py b/saleor/graphql/meta/tests/mutations/test_update_private_metadata.py
new file mode 100644
index 0000000000..6f8effb7e9
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_update_private_metadata.py
@@ -0,0 +1,201 @@
+import base64
+
+import graphene
+
+from .....core.error_codes import MetadataErrorCode
+from .....core.models import ModelWithMetadata
+from ....tests.utils import assert_no_permission, get_graphql_content
+from . import (
+ PRIVATE_KEY,
+ PRIVATE_VALUE,
+ PUBLIC_KEY,
+ PUBLIC_KEY2,
+ PUBLIC_VALUE,
+ PUBLIC_VALUE2,
+)
+
+UPDATE_PRIVATE_METADATA_MUTATION = """
+mutation UpdatePrivateMetadata($id: ID!, $input: [MetadataInput!]!) {
+ updatePrivateMetadata(
+ id: $id
+ input: $input
+ ) {
+ errors{
+ field
+ code
+ }
+ item {
+ privateMetadata{
+ key
+ value
+ }
+ ...on %s{
+ id
+ }
+ }
+ }
+}
+"""
+
+
+def execute_update_private_metadata_for_item(
+ client,
+ permissions,
+ item_id,
+ item_type,
+ key=PRIVATE_KEY,
+ value=PRIVATE_VALUE,
+):
+ variables = {
+ "id": item_id,
+ "input": [{"key": key, "value": value}],
+ }
+
+ response = client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % item_type,
+ variables,
+ permissions=[permissions] if permissions else None,
+ )
+ response = get_graphql_content(response)
+ return response
+
+
+def execute_update_private_metadata_for_multiple_items(
+ client,
+ permissions,
+ item_id,
+ item_type,
+ key=PUBLIC_KEY,
+ value=PUBLIC_VALUE,
+ key2=PUBLIC_KEY2,
+ value2=PUBLIC_VALUE2,
+):
+ variables = {
+ "id": item_id,
+ "input": [{"key": key, "value": value}, {"key": key2, "value": value2}],
+ }
+
+ response = client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % item_type,
+ variables,
+ permissions=[permissions] if permissions else None,
+ )
+ response = get_graphql_content(response)
+ return response
+
+
+def item_contains_proper_private_metadata(
+ item_from_response,
+ item,
+ item_id,
+ key=PRIVATE_KEY,
+ value=PRIVATE_VALUE,
+):
+ if item_from_response["id"] != item_id:
+ return False
+ item.refresh_from_db()
+ return item.get_value_from_private_metadata(key) == value
+
+
+def item_contains_multiple_proper_private_metadata(
+ item_from_response,
+ item,
+ item_id,
+ key=PUBLIC_KEY,
+ value=PUBLIC_VALUE,
+ key2=PUBLIC_KEY2,
+ value2=PUBLIC_VALUE2,
+):
+ if item_from_response["id"] != item_id:
+ return False
+ item.refresh_from_db()
+ return all(
+ [
+ item.get_value_from_private_metadata(key) == value,
+ item.get_value_from_private_metadata(key2) == value2,
+ ]
+ )
+
+
+def test_update_private_metadata_for_item(
+ staff_api_client, checkout, permission_manage_checkouts
+):
+ # given
+ checkout.metadata_storage.store_value_in_private_metadata(
+ {PRIVATE_KEY: PRIVATE_KEY}
+ )
+ checkout.metadata_storage.save(update_fields=["private_metadata"])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client,
+ permission_manage_checkouts,
+ checkout.token,
+ "Checkout",
+ value="NewMetaValue",
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"],
+ checkout.metadata_storage,
+ checkout_id,
+ value="NewMetaValue",
+ )
+
+
+def test_update_private_metadata_for_non_exist_item(
+ staff_api_client, permission_manage_payments
+):
+ # given
+ payment_id = "Payment: 0"
+ payment_id = base64.b64encode(str.encode(payment_id)).decode("utf-8")
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_payments, payment_id, "Payment"
+ )
+
+ # then
+ errors = response["data"]["updatePrivateMetadata"]["errors"]
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
+
+
+def test_update_private_metadata_for_item_without_meta(
+ api_client, permission_group_manage_users
+):
+ # given
+ group = permission_group_manage_users
+ assert not issubclass(type(group), ModelWithMetadata)
+ group_id = graphene.Node.to_global_id("Group", group.pk)
+
+ # when
+ # We use "User" type inside mutation for valid graphql query with fragment
+ # without this we are not able to reuse UPDATE_PRIVATE_METADATA_MUTATION
+ response = execute_update_private_metadata_for_item(
+ api_client, None, group_id, "User"
+ )
+
+ # then
+ errors = response["data"]["updatePrivateMetadata"]["errors"]
+ assert errors[0]["field"] == "id"
+ assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
+
+
+def test_update_private_metadata_by_customer(user_api_client, payment):
+ # given
+ payment_id = graphene.Node.to_global_id("Payment", payment.pk)
+ variables = {
+ "id": payment_id,
+ "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ UPDATE_PRIVATE_METADATA_MUTATION % "Payment", variables, permissions=None
+ )
+
+ # then
+ assert_no_permission(response)
diff --git a/saleor/graphql/meta/tests/mutations/test_warehouse.py b/saleor/graphql/meta/tests/mutations/test_warehouse.py
new file mode 100644
index 0000000000..0dde232ef2
--- /dev/null
+++ b/saleor/graphql/meta/tests/mutations/test_warehouse.py
@@ -0,0 +1,91 @@
+import graphene
+
+from . import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+from .test_delete_metadata import (
+ execute_clear_public_metadata_for_item,
+ item_without_public_metadata,
+)
+from .test_delete_private_metadata import (
+ execute_clear_private_metadata_for_item,
+ item_without_private_metadata,
+)
+from .test_update_metadata import (
+ execute_update_public_metadata_for_item,
+ item_contains_proper_public_metadata,
+)
+from .test_update_private_metadata import (
+ execute_update_private_metadata_for_item,
+ item_contains_proper_private_metadata,
+)
+
+
+def test_delete_private_metadata_for_warehouse(
+ staff_api_client, permission_manage_products, warehouse
+):
+ # given
+ warehouse.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ warehouse.save(update_fields=["private_metadata"])
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+
+ # when
+ response = execute_clear_private_metadata_for_item(
+ staff_api_client, permission_manage_products, warehouse_id, "Warehouse"
+ )
+
+ # then
+ assert item_without_private_metadata(
+ response["data"]["deletePrivateMetadata"]["item"], warehouse, warehouse_id
+ )
+
+
+def test_delete_public_metadata_for_warehouse(
+ staff_api_client, permission_manage_products, warehouse
+):
+ # given
+ warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ warehouse.save(update_fields=["metadata"])
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+
+ # when
+ response = execute_clear_public_metadata_for_item(
+ staff_api_client, permission_manage_products, warehouse_id, "Warehouse"
+ )
+
+ # then
+ assert item_without_public_metadata(
+ response["data"]["deleteMetadata"]["item"], warehouse, warehouse_id
+ )
+
+
+def test_add_public_metadata_for_warehouse(
+ staff_api_client, permission_manage_products, warehouse
+):
+ # given
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+
+ # when
+ response = execute_update_public_metadata_for_item(
+ staff_api_client, permission_manage_products, warehouse_id, "Warehouse"
+ )
+
+ # then
+ assert item_contains_proper_public_metadata(
+ response["data"]["updateMetadata"]["item"], warehouse, warehouse_id
+ )
+
+
+def test_add_private_metadata_for_warehouse(
+ staff_api_client, permission_manage_products, warehouse
+):
+ # given
+ warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
+
+ # when
+ response = execute_update_private_metadata_for_item(
+ staff_api_client, permission_manage_products, warehouse_id, "Warehouse"
+ )
+
+ # then
+ assert item_contains_proper_private_metadata(
+ response["data"]["updatePrivateMetadata"]["item"], warehouse, warehouse_id
+ )
diff --git a/saleor/graphql/meta/tests/queries/__init__.py b/saleor/graphql/meta/tests/queries/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/graphql/meta/tests/queries/test_account.py b/saleor/graphql/meta/tests/queries/test_account.py
new file mode 100644
index 0000000000..9583e42a28
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_account.py
@@ -0,0 +1,257 @@
+import graphene
+
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+
+QUERY_USER_PUBLIC_META = """
+ query userMeta($id: ID!){
+ user(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_customer_as_staff(
+ staff_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ customer_user.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_USER_PUBLIC_META, variables, [permission_manage_users]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["user"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ customer_user.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_USER_PUBLIC_META, variables, [permission_manage_users]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["user"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_staff_as_other_staff(
+ staff_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ admin_user.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("User", admin_user.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_USER_PUBLIC_META, variables, [permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["user"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_USER_PRIVATE_META = """
+ query userMeta($id: ID!){
+ user(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_customer_as_staff(
+ staff_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ customer_user.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_USER_PRIVATE_META, variables, [permission_manage_users]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["user"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_customer_as_app(
+ app_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ customer_user.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_USER_PRIVATE_META, variables, [permission_manage_users]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["user"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_staff_as_other_staff(
+ staff_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ admin_user.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("User", admin_user.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_USER_PRIVATE_META, variables, [permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["user"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_SELF_PUBLIC_META = """
+ {
+ me{
+ metadata{
+ key
+ value
+ }
+ metafields(keys: ["INVALID", "key"])
+ keyFieldValue: metafield(key: "key")
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_me_as_customer(user_api_client):
+ # given
+ me = user_api_client.user
+ me.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ me.save(update_fields=["metadata"])
+
+ # when
+ response = user_api_client.post_graphql(QUERY_SELF_PUBLIC_META)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["me"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+ metafields = content["data"]["me"]["metafields"]
+ assert metafields[PUBLIC_KEY] == PUBLIC_VALUE
+ field_value = content["data"]["me"]["keyFieldValue"]
+ assert field_value == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_me_as_staff(staff_api_client):
+ # given
+ me = staff_api_client.user
+ me.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ me.save(update_fields=["metadata"])
+
+ # when
+ response = staff_api_client.post_graphql(QUERY_SELF_PUBLIC_META)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["me"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+ metafields = content["data"]["me"]["metafields"]
+ assert metafields[PUBLIC_KEY] == PUBLIC_VALUE
+ field_value = content["data"]["me"]["keyFieldValue"]
+ assert field_value == PUBLIC_VALUE
+
+
+QUERY_SELF_PRIVATE_META = """
+ {
+ me{
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_me_as_customer(user_api_client):
+ # given
+
+ # when
+ response = user_api_client.post_graphql(QUERY_SELF_PRIVATE_META)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_me_as_staff_with_manage_customer(
+ staff_api_client, permission_manage_users
+):
+ # given
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_SELF_PRIVATE_META, None, [permission_manage_users]
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_me_as_staff_with_manage_staff(
+ staff_api_client, permission_manage_staff
+):
+ # given
+ me = staff_api_client.user
+ me.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ me.save(update_fields=["private_metadata"])
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_SELF_PRIVATE_META, None, [permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["me"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
diff --git a/saleor/graphql/meta/tests/queries/test_app.py b/saleor/graphql/meta/tests/queries/test_app.py
new file mode 100644
index 0000000000..d468064e11
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_app.py
@@ -0,0 +1,159 @@
+import graphene
+
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+
+QUERY_APP_PRIVATE_META = """
+ query appMeta($id: ID!){
+ app(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_app_as_anonymous_user(api_client, app):
+ # given
+ variables = {"id": graphene.Node.to_global_id("App", app.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_APP_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_app_as_customer(user_api_client, app):
+ # given
+ variables = {"id": graphene.Node.to_global_id("App", app.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_APP_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_app_as_staff(
+ staff_api_client, app, permission_manage_apps
+):
+ # given
+ app.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ app.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("App", app.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_APP_PRIVATE_META,
+ variables,
+ [permission_manage_apps],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["app"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_app_as_app(app_api_client, app, permission_manage_apps):
+ # given
+ app.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ app.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("App", app.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_APP_PRIVATE_META,
+ variables,
+ [permission_manage_apps],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["app"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_APP_PUBLIC_META = """
+ query appMeta($id: ID!){
+ app(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_app_as_anonymous_user(api_client, app):
+ # given
+ variables = {"id": graphene.Node.to_global_id("App", app.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_APP_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_app_as_customer(user_api_client, app):
+ # given
+ variables = {"id": graphene.Node.to_global_id("App", app.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_APP_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_app_as_staff(
+ staff_api_client, app, permission_manage_apps
+):
+ # given
+ app.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ app.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("App", app.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_APP_PUBLIC_META,
+ variables,
+ [permission_manage_apps],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["app"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_app_as_app(app_api_client, app, permission_manage_apps):
+ # given
+ app.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ app.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("App", app.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_APP_PUBLIC_META,
+ variables,
+ [permission_manage_apps],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["app"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
diff --git a/saleor/graphql/meta/tests/queries/test_attribute.py b/saleor/graphql/meta/tests/queries/test_attribute.py
new file mode 100644
index 0000000000..2bc7c637b4
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_attribute.py
@@ -0,0 +1,175 @@
+import graphene
+
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+
+QUERY_ATTRIBUTE_PUBLIC_META = """
+ query attributeMeta($id: ID!){
+ attribute(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_attribute_as_anonymous_user(api_client, color_attribute):
+ # given
+ color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ color_attribute.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_ATTRIBUTE_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["attribute"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_attribute_as_customer(user_api_client, color_attribute):
+ # given
+ color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ color_attribute.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_ATTRIBUTE_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["attribute"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_attribute_as_staff(
+ staff_api_client, color_attribute, permission_manage_products
+):
+ # given
+ color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ color_attribute.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_ATTRIBUTE_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["attribute"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_attribute_as_app(
+ app_api_client, color_attribute, permission_manage_products
+):
+ # given
+ color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ color_attribute.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_ATTRIBUTE_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["attribute"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_ATTRIBUTE_PRIVATE_META = """
+ query attributeMeta($id: ID!){
+ attribute(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_attribute_as_anonymous_user(
+ api_client, color_attribute
+):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_ATTRIBUTE_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_attribute_as_customer(user_api_client, color_attribute):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_ATTRIBUTE_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_attribute_as_staff(
+ staff_api_client, color_attribute, permission_manage_product_types_and_attributes
+):
+ # given
+ color_attribute.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ color_attribute.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_ATTRIBUTE_PRIVATE_META,
+ variables,
+ [permission_manage_product_types_and_attributes],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["attribute"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_attribute_as_app(
+ app_api_client, color_attribute, permission_manage_product_types_and_attributes
+):
+ # given
+ color_attribute.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ color_attribute.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_ATTRIBUTE_PRIVATE_META,
+ variables,
+ [permission_manage_product_types_and_attributes],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["attribute"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
diff --git a/saleor/graphql/meta/tests/queries/test_checkout.py b/saleor/graphql/meta/tests/queries/test_checkout.py
new file mode 100644
index 0000000000..52d06018ee
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_checkout.py
@@ -0,0 +1,221 @@
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+
+QUERY_CHECKOUT_PUBLIC_META = """
+ query checkoutMeta($token: UUID!){
+ checkout(token: $token){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_checkout_as_anonymous_user(api_client, checkout):
+ # given
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = api_client.post_graphql(QUERY_CHECKOUT_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["checkout"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_other_customer_checkout_as_anonymous_user(
+ api_client, checkout, customer_user
+):
+ # given
+ checkout.user = customer_user
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.save(update_fields=["user"])
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = api_client.post_graphql(QUERY_CHECKOUT_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ assert not content["data"]["checkout"]
+
+
+def test_query_public_meta_for_checkout_as_customer(user_api_client, checkout):
+ # given
+ checkout.user = user_api_client.user
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.save(update_fields=["user"])
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_CHECKOUT_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["checkout"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_checkout_as_staff(
+ staff_api_client, checkout, customer_user, permission_manage_checkouts
+):
+ # given
+ checkout.user = customer_user
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.save(update_fields=["user"])
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_CHECKOUT_PUBLIC_META,
+ variables,
+ [permission_manage_checkouts],
+ check_no_permissions=False, # Remove after fix #5245
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["checkout"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_checkout_as_app(
+ app_api_client, checkout, customer_user, permission_manage_checkouts
+):
+ # given
+ checkout.user = customer_user
+ checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.save(update_fields=["user"])
+ checkout.metadata_storage.save(update_fields=["metadata"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_CHECKOUT_PUBLIC_META,
+ variables,
+ [permission_manage_checkouts],
+ check_no_permissions=False, # Remove after fix #5245
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["checkout"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_CHECKOUT_PRIVATE_META = """
+ query checkoutMeta($token: UUID!){
+ checkout(token: $token){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_checkout_as_anonymous_user(api_client, checkout):
+ # given
+ variables = {"token": checkout.pk}
+
+ # when
+ response = api_client.post_graphql(QUERY_CHECKOUT_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_other_customer_checkout_as_anonymous_user(
+ api_client, checkout, customer_user
+):
+ # given
+ checkout.user = customer_user
+ checkout.save(update_fields=["user"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = api_client.post_graphql(QUERY_CHECKOUT_PRIVATE_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ assert not content["data"]["checkout"]
+
+
+def test_query_private_meta_for_checkout_as_customer(user_api_client, checkout):
+ # given
+ checkout.user = user_api_client.user
+ checkout.save(update_fields=["user"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_CHECKOUT_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_checkout_as_staff(
+ staff_api_client, checkout, customer_user, permission_manage_checkouts
+):
+ # given
+ checkout.user = customer_user
+ checkout.metadata_storage.store_value_in_private_metadata(
+ {PRIVATE_KEY: PRIVATE_VALUE}
+ )
+ checkout.save(update_fields=["user"])
+ checkout.metadata_storage.save(update_fields=["private_metadata"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_CHECKOUT_PRIVATE_META,
+ variables,
+ [permission_manage_checkouts],
+ check_no_permissions=False, # Remove after fix #5245
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["checkout"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_checkout_as_app(
+ app_api_client, checkout, customer_user, permission_manage_checkouts
+):
+ # given
+ checkout.user = customer_user
+ checkout.metadata_storage.store_value_in_private_metadata(
+ {PRIVATE_KEY: PRIVATE_VALUE}
+ )
+ checkout.save(update_fields=["user"])
+ checkout.metadata_storage.save(update_fields=["private_metadata"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_CHECKOUT_PRIVATE_META,
+ variables,
+ [permission_manage_checkouts],
+ check_no_permissions=False, # Remove after fix #5245
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["checkout"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
diff --git a/saleor/graphql/meta/tests/queries/test_discount.py b/saleor/graphql/meta/tests/queries/test_discount.py
new file mode 100644
index 0000000000..9054beac60
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_discount.py
@@ -0,0 +1,351 @@
+import graphene
+
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+
+QUERY_SALE_PUBLIC_META = """
+ query saleMeta($id: ID!){
+ sale(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_sale_as_anonymous_user(api_client, sale):
+ # given
+ sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ sale.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Sale", sale.pk),
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_SALE_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_sale_as_customer(user_api_client, sale):
+ # given
+ sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ sale.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Sale", sale.pk),
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_SALE_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_sale_as_staff(
+ staff_api_client, sale, permission_manage_discounts
+):
+ # given
+ sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ sale.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Sale", sale.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_SALE_PUBLIC_META,
+ variables,
+ [permission_manage_discounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["sale"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_sale_as_app(
+ app_api_client, sale, permission_manage_discounts
+):
+ # given
+ sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ sale.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Sale", sale.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_SALE_PUBLIC_META,
+ variables,
+ [permission_manage_discounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["sale"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_SALE_PRIVATE_META = """
+ query saleMeta($id: ID!){
+ sale(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_sale_as_anonymous_user(api_client, sale):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Sale", sale.pk),
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_SALE_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_sale_as_customer(user_api_client, sale):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Sale", sale.pk),
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_SALE_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_sale_as_staff(
+ staff_api_client, sale, permission_manage_discounts
+):
+ # given
+ sale.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ sale.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Sale", sale.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_SALE_PRIVATE_META,
+ variables,
+ [permission_manage_discounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["sale"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_sale_as_app(
+ app_api_client, sale, permission_manage_discounts
+):
+ # given
+ sale.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ sale.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Sale", sale.pk),
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_SALE_PRIVATE_META,
+ variables,
+ [permission_manage_discounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["sale"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_VOUCHER_PUBLIC_META = """
+ query voucherMeta($id: ID!){
+ voucher(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_voucher_as_anonymous_user(api_client, voucher):
+ # given
+ voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ voucher.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Voucher", voucher.pk),
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_VOUCHER_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_voucher_as_customer(user_api_client, voucher):
+ # given
+ voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ voucher.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Voucher", voucher.pk),
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_VOUCHER_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_voucher_as_staff(
+ staff_api_client, voucher, permission_manage_discounts
+):
+ # given
+ voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ voucher.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Voucher", voucher.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_VOUCHER_PUBLIC_META,
+ variables,
+ [permission_manage_discounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["voucher"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_voucher_as_app(
+ app_api_client, voucher, permission_manage_discounts
+):
+ # given
+ voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ voucher.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Voucher", voucher.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_VOUCHER_PUBLIC_META,
+ variables,
+ [permission_manage_discounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["voucher"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_VOUCHER_PRIVATE_META = """
+ query voucherMeta($id: ID!){
+ voucher(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_voucher_as_anonymous_user(api_client, voucher):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Voucher", voucher.pk),
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_VOUCHER_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_voucher_as_customer(user_api_client, voucher):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Voucher", voucher.pk),
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_VOUCHER_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_voucher_as_staff(
+ staff_api_client, voucher, permission_manage_discounts
+):
+ # given
+ voucher.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ voucher.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Voucher", voucher.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_VOUCHER_PRIVATE_META,
+ variables,
+ [permission_manage_discounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["voucher"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_voucher_as_app(
+ app_api_client, voucher, permission_manage_discounts
+):
+ # given
+ voucher.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ voucher.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Voucher", voucher.pk),
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_VOUCHER_PRIVATE_META,
+ variables,
+ [permission_manage_discounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["voucher"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
diff --git a/saleor/graphql/meta/tests/queries/test_gift_card.py b/saleor/graphql/meta/tests/queries/test_gift_card.py
new file mode 100644
index 0000000000..d96f238e46
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_gift_card.py
@@ -0,0 +1,173 @@
+import graphene
+
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+
+QUERY_GIFT_CARD_PRIVATE_META = """
+ query giftCardMeta($id: ID!){
+ giftCard(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_gift_card_as_anonymous_user(api_client, gift_card):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("GiftCard", gift_card.pk),
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_GIFT_CARD_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_gift_card_as_customer(user_api_client, gift_card):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("GiftCard", gift_card.pk),
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_GIFT_CARD_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_gift_card_as_staff(
+ staff_api_client, gift_card, permission_manage_gift_card
+):
+ # given
+ gift_card.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ gift_card.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_GIFT_CARD_PRIVATE_META,
+ variables,
+ [permission_manage_gift_card],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["giftCard"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_gift_card_as_app(
+ app_api_client, gift_card, permission_manage_gift_card
+):
+ # given
+ gift_card.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ gift_card.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("GiftCard", gift_card.pk),
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_GIFT_CARD_PRIVATE_META,
+ variables,
+ [permission_manage_gift_card],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["giftCard"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_GIFT_CARD_PUBLIC_META = """
+ query giftCardMeta($id: ID!){
+ giftCard(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_gift_card_as_anonymous_user(api_client, gift_card):
+ # given
+ gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ gift_card.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_GIFT_CARD_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_gift_card_as_customer(user_api_client, gift_card):
+ # given
+ gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ gift_card.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_GIFT_CARD_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_gift_card_as_staff(
+ staff_api_client, gift_card, permission_manage_gift_card
+):
+ # given
+ gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ gift_card.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_GIFT_CARD_PUBLIC_META,
+ variables,
+ [permission_manage_gift_card],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["giftCard"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_gift_card_as_app(
+ app_api_client, gift_card, permission_manage_gift_card
+):
+ # given
+ gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ gift_card.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_GIFT_CARD_PUBLIC_META,
+ variables,
+ [permission_manage_gift_card],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["giftCard"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
diff --git a/saleor/graphql/meta/tests/test_meta_filters.py b/saleor/graphql/meta/tests/queries/test_meta_filters.py
similarity index 96%
rename from saleor/graphql/meta/tests/test_meta_filters.py
rename to saleor/graphql/meta/tests/queries/test_meta_filters.py
index b8c462b0ef..a1edb174c8 100644
--- a/saleor/graphql/meta/tests/test_meta_filters.py
+++ b/saleor/graphql/meta/tests/queries/test_meta_filters.py
@@ -1,7 +1,7 @@
import pytest
-from ....product.models import Product
-from ...tests.utils import get_graphql_content
+from .....product.models import Product
+from ....tests.utils import get_graphql_content
FILTER_BY_META_QUERY = """
query filterProductsByMetadata ($filter:ProductFilterInput, $channel: String){
@@ -97,6 +97,7 @@ query filterProductsByMetadata ($filter:ProductFilterInput, $channel: String){
def test_filter_by_meta_total_returned_objects(
metadata, total_count, api_client, product_list, channel_USD
):
+ # given
product1, product2, product3 = product_list
variables = {
"channel": channel_USD.slug,
@@ -108,7 +109,10 @@ def test_filter_by_meta_total_returned_objects(
product2.store_value_in_metadata({"C": "3", "Z": "4"})
Product.objects.bulk_update([product1, product2], ["metadata"])
+ # when
response = api_client.post_graphql(FILTER_BY_META_QUERY, variables)
+
+ # then
content = get_graphql_content(response)
assert len(content["data"]["products"]["edges"]) == total_count
@@ -116,6 +120,7 @@ def test_filter_by_meta_total_returned_objects(
def test_filter_by_meta_expected_product_for_key_and_value(
api_client, product_list, channel_USD
):
+ # given
product = product_list[0]
variables = {
"channel": channel_USD.slug,
@@ -139,6 +144,7 @@ def test_filter_by_meta_expected_product_for_key_and_value(
def test_filter_by_meta_expected_product_for_only_key(
api_client, product_list, channel_USD
):
+ # given
product = product_list[0]
variables = {
"channel": channel_USD.slug,
diff --git a/saleor/graphql/meta/tests/queries/test_order.py b/saleor/graphql/meta/tests/queries/test_order.py
new file mode 100644
index 0000000000..63ee985b20
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_order.py
@@ -0,0 +1,971 @@
+from typing import List
+
+import graphene
+
+from .....order.models import Order
+from .....permission.models import Permission
+from ....tests.fixtures import ApiClient
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE, execute_query
+
+QUERY_ORDER_BY_TOKEN_PUBLIC_META = """
+ query orderMeta($token: UUID!){
+ orderByToken(token: $token){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_order_by_token_as_anonymous_user(api_client, order):
+ # given
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["metadata"])
+ variables = {"token": order.id}
+
+ # when
+ response = api_client.post_graphql(QUERY_ORDER_BY_TOKEN_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_order_by_token_as_customer(user_api_client, order):
+ # given
+ order.user = user_api_client.user
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "metadata"])
+ variables = {"token": order.id}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_ORDER_BY_TOKEN_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_order_by_token_as_staff(
+ staff_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "metadata"])
+ variables = {"token": order.id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_ORDER_BY_TOKEN_PUBLIC_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_order_by_token_as_app(
+ app_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "metadata"])
+ variables = {"token": order.id}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_ORDER_BY_TOKEN_PUBLIC_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_ORDER_PUBLIC_META = """
+ query orderMeta($id: ID!){
+ order(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_order_as_anonymous_user(api_client, order):
+ # given
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_ORDER_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_order_as_customer(user_api_client, order):
+ # given
+ order.user = user_api_client.user
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_ORDER_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_order_as_staff(
+ staff_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_ORDER_PUBLIC_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_order_as_app(
+ app_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_ORDER_PUBLIC_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_DRAFT_ORDER_PUBLIC_META = """
+ query draftOrderMeta($id: ID!){
+ order(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_draft_order_as_anonymous_user(api_client, draft_order):
+ # given
+ draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ draft_order.save(update_fields=["user", "metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_DRAFT_ORDER_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_draft_order_as_customer(user_api_client, draft_order):
+ # given
+ draft_order.user = user_api_client.user
+ draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ draft_order.save(update_fields=["user", "metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_DRAFT_ORDER_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_draft_order_as_staff(
+ staff_api_client, draft_order, customer_user, permission_manage_orders
+):
+ # given
+ draft_order.user = customer_user
+ draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ draft_order.save(update_fields=["user", "metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_DRAFT_ORDER_PUBLIC_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_draft_order_as_app(
+ app_api_client, draft_order, customer_user, permission_manage_orders
+):
+ # given
+ draft_order.user = customer_user
+ draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ draft_order.save(update_fields=["user", "metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_ORDER_PUBLIC_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_FULFILLMENT_PUBLIC_META = """
+ query fulfillmentMeta($token: UUID!){
+ orderByToken(token: $token){
+ fulfillments{
+ metadata{
+ key
+ value
+ }
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_fulfillment_as_anonymous_user(
+ api_client, fulfilled_order
+):
+ # given
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ fulfillment.save(update_fields=["metadata"])
+ variables = {"token": fulfilled_order.id}
+
+ # when
+ response = api_client.post_graphql(QUERY_FULFILLMENT_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["fulfillments"][0]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_fulfillment_as_customer(
+ user_api_client, fulfilled_order
+):
+ # given
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ fulfillment.save(update_fields=["metadata"])
+ fulfilled_order.user = user_api_client.user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.id}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_FULFILLMENT_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["fulfillments"][0]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_fulfillment_as_staff(
+ staff_api_client, fulfilled_order, customer_user, permission_manage_orders
+):
+ # given
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ fulfillment.save(update_fields=["metadata"])
+ fulfilled_order.user = customer_user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_FULFILLMENT_PUBLIC_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["fulfillments"][0]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_fulfillment_as_app(
+ app_api_client, fulfilled_order, customer_user, permission_manage_orders
+):
+ # given
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ fulfillment.save(update_fields=["metadata"])
+ fulfilled_order.user = customer_user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.id}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_FULFILLMENT_PUBLIC_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["fulfillments"][0]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_ORDER_BY_TOKEN_PRIVATE_META = """
+ query orderMeta($token: UUID!){
+ orderByToken(token: $token){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_order_by_token_as_anonymous_user(api_client, order):
+ # given
+ variables = {"token": order.id}
+
+ # when
+ response = api_client.post_graphql(QUERY_ORDER_BY_TOKEN_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_order_by_token_as_customer(user_api_client, order):
+ # given
+ order.user = user_api_client.user
+ order.save(update_fields=["user"])
+ variables = {"token": order.id}
+
+ # when
+ response = user_api_client.post_graphql(
+ QUERY_ORDER_BY_TOKEN_PRIVATE_META, variables
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_order_by_token_as_staff(
+ staff_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ order.save(update_fields=["user", "private_metadata"])
+ variables = {"token": order.id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_ORDER_BY_TOKEN_PRIVATE_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_order_by_token_as_app(
+ app_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ order.save(update_fields=["user", "private_metadata"])
+ variables = {"token": order.id}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_ORDER_BY_TOKEN_PRIVATE_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_ORDER_PRIVATE_META = """
+ query orderMeta($id: ID!){
+ order(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_order_as_anonymous_user(api_client, order):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_ORDER_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_order_as_customer(user_api_client, order):
+ # given
+ order.user = user_api_client.user
+ order.save(update_fields=["user"])
+ variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_ORDER_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_order_as_staff(
+ staff_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ order.save(update_fields=["user", "private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_ORDER_PRIVATE_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_order_as_app(
+ app_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ order.save(update_fields=["user", "private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_ORDER_PRIVATE_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_DRAFT_ORDER_PRIVATE_META = """
+ query draftOrderMeta($id: ID!){
+ order(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_draft_order_as_anonymous_user(api_client, draft_order):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_DRAFT_ORDER_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_draft_order_as_customer(user_api_client, draft_order):
+ # given
+ draft_order.user = user_api_client.user
+ draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ draft_order.save(update_fields=["user", "private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_DRAFT_ORDER_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_draft_order_as_staff(
+ staff_api_client, draft_order, customer_user, permission_manage_orders
+):
+ # given
+ draft_order.user = customer_user
+ draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ draft_order.save(update_fields=["user", "private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_DRAFT_ORDER_PRIVATE_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_draft_order_as_app(
+ app_api_client, draft_order, customer_user, permission_manage_orders
+):
+ # given
+ draft_order.user = customer_user
+ draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ draft_order.save(update_fields=["user", "private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_ORDER_PRIVATE_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["order"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_FULFILLMENT_PRIVATE_META = """
+ query fulfillmentMeta($token: UUID!){
+ orderByToken(token: $token){
+ fulfillments{
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_fulfillment_as_anonymous_user(
+ api_client, fulfilled_order
+):
+ # given
+ variables = {"token": fulfilled_order.id}
+
+ # when
+ response = api_client.post_graphql(QUERY_FULFILLMENT_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_fulfillment_as_customer(
+ user_api_client, fulfilled_order
+):
+ # given
+ fulfilled_order.user = user_api_client.user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.id}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_FULFILLMENT_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_fulfillment_as_staff(
+ staff_api_client, fulfilled_order, customer_user, permission_manage_orders
+):
+ # given
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ fulfillment.save(update_fields=["private_metadata"])
+ fulfilled_order.user = customer_user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.id}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_FULFILLMENT_PRIVATE_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["fulfillments"][0]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_fulfillment_as_app(
+ app_api_client, fulfilled_order, customer_user, permission_manage_orders
+):
+ # given
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ fulfillment.save(update_fields=["private_metadata"])
+ fulfilled_order.user = customer_user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.id}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_FULFILLMENT_PRIVATE_META,
+ variables,
+ [permission_manage_orders],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["orderByToken"]["fulfillments"][0]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_TRANSACTION_ITEM_PUBLIC_META = """
+query transactionItemMeta($id: ID!){
+ order(id: $id){
+ transactions{
+ metadata{
+ key
+ value
+ }
+ }
+ }
+}
+"""
+
+
+def execute_query_public_metadata_for_transaction_item(
+ client: ApiClient, order: Order, permissions: List[Permission] = None
+):
+ return execute_query(
+ QUERY_TRANSACTION_ITEM_PUBLIC_META, client, order, "Order", permissions
+ )
+
+
+def assert_transaction_item_contains_metadata(response):
+ content = get_graphql_content(response)
+ metadata = content["data"]["order"]["transactions"][0]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_transaction_item_as_customer(
+ user_api_client, order, permission_manage_orders
+):
+ # given
+ order.payment_transactions.create(metadata={PUBLIC_KEY: PUBLIC_VALUE})
+ order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["metadata"])
+
+ # when
+ response = execute_query_public_metadata_for_transaction_item(
+ user_api_client,
+ order,
+ permissions=[],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_transaction_item_as_staff_with_permission(
+ staff_api_client,
+ order_with_lines,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ # given
+ order_with_lines.payment_transactions.create(metadata={PUBLIC_KEY: PUBLIC_VALUE})
+
+ # when
+ response = execute_query_public_metadata_for_transaction_item(
+ staff_api_client,
+ order_with_lines,
+ permissions=[permission_manage_orders, permission_manage_payments],
+ )
+
+ # then
+ assert_transaction_item_contains_metadata(response)
+
+
+def test_query_public_meta_for_transaction_item_as_staff_without_permission(
+ staff_api_client, order
+):
+ # given
+ order.payment_transactions.create(metadata={PUBLIC_KEY: PUBLIC_VALUE})
+
+ # when
+ response = execute_query_public_metadata_for_transaction_item(
+ staff_api_client, order
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_transaction_item_as_app_with_permission(
+ app_api_client,
+ order,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ order.payment_transactions.create(metadata={PUBLIC_KEY: PUBLIC_VALUE})
+
+ # when
+ response = execute_query_public_metadata_for_transaction_item(
+ app_api_client,
+ order,
+ permissions=[permission_manage_payments, permission_manage_orders],
+ )
+
+ # then
+ assert_transaction_item_contains_metadata(response)
+
+
+def test_query_public_meta_for_transaction_item_as_app_without_permission(
+ app_api_client, order
+):
+ # when
+ response = execute_query_public_metadata_for_transaction_item(app_api_client, order)
+
+ # then
+ assert_no_permission(response)
+
+
+QUERY_TRANSACTION_ITEM_PRIVATE_META = """
+query transactionItemMeta($id: ID!){
+ order(id: $id){
+ transactions{
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+}
+"""
+
+
+def execute_query_private_metadata_for_transaction_item(
+ client: ApiClient, order: Order, permissions: List[Permission] = None
+):
+ return execute_query(
+ QUERY_TRANSACTION_ITEM_PRIVATE_META, client, order, "Order", permissions
+ )
+
+
+def assert_transaction_item_contains_private_metadata(response):
+ content = get_graphql_content(response)
+ metadata = content["data"]["order"]["transactions"][0]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_transaction_item_as_customer(
+ user_api_client, order, permission_manage_orders
+):
+ # given
+ order.payment_transactions.create(private_metadata={PRIVATE_KEY: PRIVATE_VALUE})
+
+ # when
+ response = execute_query_public_metadata_for_transaction_item(
+ user_api_client,
+ order,
+ permissions=[],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+#
+
+
+def test_query_private_meta_for_transaction_item_as_staff_with_permission(
+ staff_api_client,
+ order_with_lines,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ # given
+ order_with_lines.payment_transactions.create(
+ private_metadata={PRIVATE_KEY: PRIVATE_VALUE}
+ )
+
+ # when
+ response = execute_query_private_metadata_for_transaction_item(
+ staff_api_client,
+ order_with_lines,
+ permissions=[permission_manage_orders, permission_manage_payments],
+ )
+
+ # then
+ assert_transaction_item_contains_private_metadata(response)
+
+
+def test_query_private_meta_for_transaction_item_as_staff_without_permission(
+ staff_api_client, order
+):
+ # given
+ order.payment_transactions.create(private_metadata={PRIVATE_KEY: PRIVATE_VALUE})
+
+ # when
+ response = execute_query_private_metadata_for_transaction_item(
+ staff_api_client, order
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_transaction_item_as_app_with_permission(
+ app_api_client,
+ order,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ order.payment_transactions.create(private_metadata={PRIVATE_KEY: PRIVATE_VALUE})
+
+ # when
+ response = execute_query_private_metadata_for_transaction_item(
+ app_api_client,
+ order,
+ permissions=[permission_manage_payments, permission_manage_orders],
+ )
+
+ # then
+ assert_transaction_item_contains_private_metadata(response)
+
+
+def test_query_private_meta_for_transaction_item_as_app_without_permission(
+ app_api_client, order
+):
+ # when
+ response = execute_query_private_metadata_for_transaction_item(
+ app_api_client, order
+ )
+
+ # then
+ assert_no_permission(response)
diff --git a/saleor/graphql/meta/tests/queries/test_page.py b/saleor/graphql/meta/tests/queries/test_page.py
new file mode 100644
index 0000000000..484f9c1f80
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_page.py
@@ -0,0 +1,173 @@
+import graphene
+
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+
+QUERY_PAGE_TYPE_PUBLIC_META = """
+ query pageTypeMeta($id: ID!){
+ pageType(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_page_type_as_anonymous_user(api_client, page_type):
+ # given
+ page_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ page_type.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_PAGE_TYPE_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["pageType"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_page_type_as_customer(user_api_client, page_type):
+ # given
+ page_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ page_type.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_PAGE_TYPE_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["pageType"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_page_type_as_staff(
+ staff_api_client, page_type, permission_manage_products
+):
+ # given
+ page_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ page_type.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_PAGE_TYPE_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["pageType"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_page_type_as_app(
+ app_api_client, page_type, permission_manage_products
+):
+ # given
+ page_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ page_type.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_PAGE_TYPE_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["pageType"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_PAGE_TYPE_PRIVATE_META = """
+ query pageTypeMeta($id: ID!){
+ pageType(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_page_type_as_anonymous_user(api_client, page_type):
+ # given
+ variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_PAGE_TYPE_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_page_type_as_customer(user_api_client, page_type):
+ # given
+ variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_PAGE_TYPE_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_page_type_as_staff(
+ staff_api_client, page_type, permission_manage_page_types_and_attributes
+):
+ # given
+ page_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ page_type.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_PAGE_TYPE_PRIVATE_META,
+ variables,
+ [permission_manage_page_types_and_attributes],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["pageType"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_page_type_as_app(
+ app_api_client, page_type, permission_manage_page_types_and_attributes
+):
+ # given
+ page_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ page_type.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_PAGE_TYPE_PRIVATE_META,
+ variables,
+ [permission_manage_page_types_and_attributes],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["pageType"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
diff --git a/saleor/graphql/meta/tests/queries/test_payment.py b/saleor/graphql/meta/tests/queries/test_payment.py
new file mode 100644
index 0000000000..bd89e59145
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_payment.py
@@ -0,0 +1,212 @@
+from typing import List
+
+from .....payment.models import Payment
+from .....payment.utils import payment_owned_by_user
+from .....permission.models import Permission
+from ....tests.fixtures import ApiClient
+from ....tests.utils import assert_no_permission
+from .utils import (
+ assert_model_contains_metadata,
+ assert_model_contains_private_metadata,
+ execute_query,
+)
+
+QUERY_PAYMENT_PRIVATE_META = """
+ query paymentsMeta($id: ID!){
+ payment(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def execute_query_private_metadata_for_payment(
+ client: ApiClient, payment: Payment, permissions: List[Permission] = None
+):
+ return execute_query(
+ QUERY_PAYMENT_PRIVATE_META, client, payment, "Payment", permissions
+ )
+
+
+def assert_payment_contains_private_metadata(response):
+ assert_model_contains_private_metadata(response, "payment")
+
+
+def test_query_private_meta_for_payment_as_staff_with_permission(
+ staff_api_client,
+ payment_with_private_metadata,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ # when
+ response = execute_query_private_metadata_for_payment(
+ staff_api_client,
+ payment_with_private_metadata,
+ permissions=[permission_manage_orders, permission_manage_payments],
+ )
+
+ # then
+ assert_payment_contains_private_metadata(response)
+
+
+def test_query_private_meta_for_payment_as_staff_without_permission(
+ staff_api_client, payment_with_private_metadata
+):
+ # when
+ response = execute_query_private_metadata_for_payment(
+ staff_api_client, payment_with_private_metadata
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_payment_as_app_with_permission(
+ app_api_client,
+ payment_with_private_metadata,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ # when
+ response = execute_query_private_metadata_for_payment(
+ app_api_client,
+ payment_with_private_metadata,
+ permissions=[permission_manage_orders, permission_manage_payments],
+ )
+
+ # then
+ assert_payment_contains_private_metadata(response)
+
+
+def test_query_private_meta_for_payment_as_app_without_permission(
+ app_api_client, payment_with_private_metadata
+):
+ # when
+ response = execute_query_private_metadata_for_payment(
+ app_api_client, payment_with_private_metadata
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+QUERY_PAYMENT_PUBLIC_META = """
+ query paymentsMeta($id: ID!){
+ payment(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def execute_query_public_metadata_for_payment(
+ client: ApiClient, payment: Payment, permissions: List[Permission] = None
+):
+ return execute_query(
+ QUERY_PAYMENT_PUBLIC_META, client, payment, "Payment", permissions
+ )
+
+
+def assert_payment_contains_metadata(response):
+ assert_model_contains_metadata(response, "payment")
+
+
+def test_query_public_meta_for_payment_as_customer(
+ user_api_client, payment_with_public_metadata, permission_manage_orders
+):
+ # given
+ assert payment_owned_by_user(payment_with_public_metadata.pk, user_api_client.user)
+
+ # when
+ response = execute_query_public_metadata_for_payment(
+ user_api_client,
+ payment_with_public_metadata,
+ permissions=[permission_manage_orders],
+ )
+
+ # then
+ assert_payment_contains_metadata(response)
+
+
+def test_query_public_meta_for_payment_as_another_customer(
+ user2_api_client,
+ payment_with_public_metadata,
+ permission_manage_orders,
+):
+ # given
+ assert not payment_owned_by_user(
+ payment_with_public_metadata.pk, user2_api_client.user
+ )
+ # when
+ response = execute_query_public_metadata_for_payment(
+ user2_api_client,
+ payment_with_public_metadata,
+ permissions=[permission_manage_orders],
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_payment_as_staff_with_permission(
+ staff_api_client,
+ payment_with_public_metadata,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ # when
+ response = execute_query_public_metadata_for_payment(
+ staff_api_client,
+ payment_with_public_metadata,
+ permissions=[permission_manage_orders, permission_manage_payments],
+ )
+
+ # then
+ assert_payment_contains_metadata(response)
+
+
+def test_query_public_meta_for_payment_as_staff_without_permission(
+ staff_api_client, payment_with_public_metadata
+):
+ # when
+ response = execute_query_public_metadata_for_payment(
+ staff_api_client, payment_with_public_metadata
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_payment_as_app_with_permission(
+ app_api_client,
+ payment_with_public_metadata,
+ permission_manage_orders,
+ permission_manage_payments,
+):
+ # when
+ response = execute_query_public_metadata_for_payment(
+ app_api_client,
+ payment_with_public_metadata,
+ permissions=[permission_manage_orders, permission_manage_payments],
+ )
+
+ # then
+ assert_payment_contains_metadata(response)
+
+
+def test_query_public_meta_for_payment_as_app_without_permission(
+ app_api_client, payment_with_public_metadata
+):
+ # when
+ response = execute_query_public_metadata_for_payment(
+ app_api_client, payment_with_public_metadata
+ )
+
+ # then
+ assert_no_permission(response)
diff --git a/saleor/graphql/meta/tests/queries/test_product.py b/saleor/graphql/meta/tests/queries/test_product.py
new file mode 100644
index 0000000000..5fe6b95b0a
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_product.py
@@ -0,0 +1,1267 @@
+import graphene
+
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+
+QUERY_CATEGORY_PUBLIC_META = """
+ query categoryMeta($id: ID!){
+ category(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_category_as_anonymous_user(api_client, category):
+ # given
+ category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ category.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_CATEGORY_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["category"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_category_as_customer(user_api_client, category):
+ # given
+ category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ category.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_CATEGORY_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["category"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_category_as_staff(
+ staff_api_client, category, permission_manage_products
+):
+ # given
+ category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ category.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_CATEGORY_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["category"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_category_as_app(
+ app_api_client, category, permission_manage_products
+):
+ # given
+ category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ category.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_CATEGORY_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["category"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_COLLECTION_PUBLIC_META = """
+ query collectionMeta($id: ID!, $channel: String) {
+ collection(id: $id, channel: $channel) {
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_collection_as_anonymous_user(
+ api_client, published_collection, channel_USD
+):
+ # given
+ collection = published_collection
+ collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ collection.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", collection.pk),
+ "channel": channel_USD.slug,
+ }
+ # when
+ response = api_client.post_graphql(QUERY_COLLECTION_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["collection"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_collection_as_customer(
+ user_api_client, published_collection, channel_USD
+):
+ # given
+ collection = published_collection
+ collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ collection.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", collection.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_COLLECTION_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["collection"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_collection_as_staff(
+ staff_api_client, published_collection, permission_manage_products, channel_USD
+):
+ # given
+ collection = published_collection
+ collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ collection.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", collection.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_COLLECTION_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["collection"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_collection_as_app(
+ app_api_client, published_collection, permission_manage_products, channel_USD
+):
+ # given
+ collection = published_collection
+ collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ collection.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", collection.pk),
+ "channel": channel_USD.slug,
+ }
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_COLLECTION_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["collection"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_DIGITAL_CONTENT_PUBLIC_META = """
+ query digitalContentMeta($id: ID!){
+ digitalContent(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_digital_content_as_anonymous_user(
+ api_client, digital_content
+):
+ # given
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_DIGITAL_CONTENT_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_digital_content_as_customer(
+ user_api_client, digital_content
+):
+ # given
+ digital_content.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ digital_content.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = user_api_client.post_graphql(
+ QUERY_DIGITAL_CONTENT_PUBLIC_META, variables
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_digital_content_as_staff(
+ staff_api_client, digital_content, permission_manage_products
+):
+ # given
+ digital_content.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ digital_content.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_DIGITAL_CONTENT_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["digitalContent"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_digital_content_as_app(
+ app_api_client, digital_content, permission_manage_products
+):
+ # given
+ digital_content.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ digital_content.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_DIGITAL_CONTENT_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["digitalContent"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_PRODUCT_PUBLIC_META = """
+ query productsMeta($id: ID!, $channel: String){
+ product(id: $id, channel: $channel){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_product_as_anonymous_user(
+ api_client, product, channel_USD
+):
+ # given
+ product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_PRODUCT_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["product"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_product_as_customer(
+ user_api_client, product, channel_USD
+):
+ # given
+ product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_PRODUCT_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["product"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_product_as_staff(
+ staff_api_client, product, permission_manage_products
+):
+ # given
+ product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_PRODUCT_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["product"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_product_as_app(
+ app_api_client, product, permission_manage_products
+):
+ # given
+ product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_PRODUCT_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["product"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_PRODUCT_TYPE_PUBLIC_META = """
+ query productTypeMeta($id: ID!){
+ productType(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_product_type_as_anonymous_user(api_client, product_type):
+ # given
+ product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product_type.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_PRODUCT_TYPE_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productType"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_product_type_as_customer(user_api_client, product_type):
+ # given
+ product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product_type.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_PRODUCT_TYPE_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productType"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_product_type_as_staff(
+ staff_api_client, product_type, permission_manage_products
+):
+ # given
+ product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product_type.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_PRODUCT_TYPE_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productType"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_product_type_as_app(
+ app_api_client, product_type, permission_manage_products
+):
+ # given
+ product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ product_type.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_PRODUCT_TYPE_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productType"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_PRODUCT_VARIANT_PUBLIC_META = """
+ query productVariantMeta($id: ID!, $channel: String){
+ productVariant(id: $id, channel: $channel){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_product_variant_as_anonymous_user(
+ api_client, variant, channel_USD
+):
+ # given
+ variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ variant.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_PRODUCT_VARIANT_PUBLIC_META, variables)
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productVariant"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_product_variant_as_customer(
+ user_api_client, variant, channel_USD
+):
+ # given
+ variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ variant.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ QUERY_PRODUCT_VARIANT_PUBLIC_META, variables
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productVariant"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_product_variant_as_staff(
+ staff_api_client, variant, permission_manage_products
+):
+ # given
+ variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ variant.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_PRODUCT_VARIANT_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productVariant"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_product_variant_as_app(
+ app_api_client, variant, permission_manage_products
+):
+ # given
+ variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ variant.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_PRODUCT_VARIANT_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productVariant"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_CATEGORY_PRIVATE_META = """
+ query categoryMeta($id: ID!){
+ category(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_category_as_anonymous_user(api_client, category):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_CATEGORY_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_category_as_customer(user_api_client, category):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_CATEGORY_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_category_as_staff(
+ staff_api_client, category, permission_manage_products
+):
+ # given
+ category.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ category.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_CATEGORY_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["category"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_category_as_app(
+ app_api_client, category, permission_manage_products
+):
+ # given
+ category.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ category.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_CATEGORY_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["category"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_COLLECTION_PRIVATE_META = """
+ query collectionMeta($id: ID!, $channel: String){
+ collection(id: $id, channel: $channel){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_collection_as_anonymous_user(
+ api_client, published_collection, channel_USD
+):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", published_collection.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_COLLECTION_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_collection_as_customer(
+ user_api_client, published_collection, channel_USD
+):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", published_collection.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_COLLECTION_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_collection_as_staff(
+ staff_api_client, published_collection, permission_manage_products, channel_USD
+):
+ # given
+ collection = published_collection
+ collection.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ collection.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", published_collection.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_COLLECTION_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["collection"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_collection_as_app(
+ app_api_client, published_collection, permission_manage_products, channel_USD
+):
+ # given
+ collection = published_collection
+ collection.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ collection.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Collection", collection.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_COLLECTION_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["collection"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_DIGITAL_CONTENT_PRIVATE_META = """
+ query digitalContentMeta($id: ID!){
+ digitalContent(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_digital_content_as_anonymous_user(
+ api_client, digital_content
+):
+ # given
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_DIGITAL_CONTENT_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_digital_content_as_customer(
+ user_api_client, digital_content
+):
+ # given
+ digital_content.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ digital_content.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = user_api_client.post_graphql(
+ QUERY_DIGITAL_CONTENT_PRIVATE_META, variables
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_digital_content_as_staff(
+ staff_api_client, digital_content, permission_manage_products
+):
+ # given
+ digital_content.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ digital_content.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_DIGITAL_CONTENT_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["digitalContent"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_digital_content_as_app(
+ app_api_client, digital_content, permission_manage_products
+):
+ # given
+ digital_content.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ digital_content.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_DIGITAL_CONTENT_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["digitalContent"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_PRODUCT_PRIVATE_META = """
+ query productsMeta($id: ID!, $channel: String){
+ product(id: $id, channel: $channel){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_product_as_anonymous_user(
+ api_client, product, channel_USD
+):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_PRODUCT_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_product_as_customer(
+ user_api_client, product, channel_USD
+):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_PRODUCT_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_product_as_staff(
+ staff_api_client, product, permission_manage_products
+):
+ # given
+ product.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ product.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_PRODUCT_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["product"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_product_as_app(
+ app_api_client, product, permission_manage_products
+):
+ # given
+ product.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ product.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Product", product.pk),
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_PRODUCT_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["product"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_PRODUCT_TYPE_PRIVATE_META = """
+ query productTypeMeta($id: ID!){
+ productType(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_product_type_as_anonymous_user(
+ api_client, product_type
+):
+ # given
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_PRODUCT_TYPE_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_product_type_as_customer(user_api_client, product_type):
+ # given
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = user_api_client.post_graphql(QUERY_PRODUCT_TYPE_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_product_type_as_staff(
+ staff_api_client, product_type, permission_manage_product_types_and_attributes
+):
+ # given
+ product_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ product_type.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_PRODUCT_TYPE_PRIVATE_META,
+ variables,
+ [permission_manage_product_types_and_attributes],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productType"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_product_type_as_app(
+ app_api_client, product_type, permission_manage_product_types_and_attributes
+):
+ # given
+ product_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ product_type.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_PRODUCT_TYPE_PRIVATE_META,
+ variables,
+ [permission_manage_product_types_and_attributes],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productType"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_PRODUCT_VARIANT_PRIVATE_META = """
+ query productVariantMeta($id: ID!, $channel: String){
+ productVariant(id: $id, channel: $channel){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_product_variant_as_anonymous_user(
+ api_client, variant, channel_USD
+):
+ # given
+ variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ variant.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_PRODUCT_VARIANT_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_product_variant_as_customer(
+ user_api_client, variant, channel_USD
+):
+ # given
+ variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ variant.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(
+ QUERY_PRODUCT_VARIANT_PRIVATE_META, variables
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_product_variant_as_staff(
+ staff_api_client, variant, permission_manage_products
+):
+ # given
+ variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ variant.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_PRODUCT_VARIANT_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productVariant"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_product_variant_as_app(
+ app_api_client, variant, permission_manage_products
+):
+ # given
+ variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ variant.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_PRODUCT_VARIANT_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["productVariant"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+QUERY_PRODUCT_MEDIA_METADATA = """
+ query productMediaById(
+ $mediaId: ID!,
+ $productId: ID!,
+ $channel: String,
+ ) {
+ product(id: $productId, channel: $channel) {
+ mediaById(id: $mediaId) {
+ metadata {
+ key
+ value
+ }
+ privateMetadata {
+ key
+ value
+ }
+ }
+ }
+ }
+"""
+
+
+def test_query_metadata_for_product_media_as_staff(
+ staff_api_client, product_with_image, channel_USD, permission_manage_products
+):
+ # given
+ query = QUERY_PRODUCT_MEDIA_METADATA
+ media = product_with_image.media.first()
+
+ metadata = {"label": "image-name"}
+ private_metadata = {"private-label": "private-name"}
+ media.store_value_in_metadata(metadata)
+ media.store_value_in_private_metadata(private_metadata)
+ media.save(update_fields=["metadata", "private_metadata"])
+
+ variables = {
+ "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
+ "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["product"]["mediaById"]
+ assert data["metadata"][0]["key"] in metadata.keys()
+ assert data["metadata"][0]["value"] in metadata.values()
+ assert data["privateMetadata"][0]["key"] in private_metadata.keys()
+ assert data["privateMetadata"][0]["value"] in private_metadata.values()
+
+
+def test_query_metadata_for_product_media_as_app(
+ app_api_client, product_with_image, channel_USD, permission_manage_products
+):
+ # given
+ query = QUERY_PRODUCT_MEDIA_METADATA
+ media = product_with_image.media.first()
+
+ metadata = {"label": "image-name"}
+ private_metadata = {"private-label": "private-name"}
+ media.store_value_in_metadata(metadata)
+ media.store_value_in_private_metadata(private_metadata)
+ media.save(update_fields=["metadata", "private_metadata"])
+
+ variables = {
+ "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
+ "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+
+ # then
+ data = content["data"]["product"]["mediaById"]
+ assert data["metadata"][0]["key"] in metadata.keys()
+ assert data["metadata"][0]["value"] in metadata.values()
+ assert data["privateMetadata"][0]["key"] in private_metadata.keys()
+ assert data["privateMetadata"][0]["value"] in private_metadata.values()
+
+
+def test_query_metadata_for_product_media_as_anonymous_user(
+ api_client, product_with_image, channel_USD
+):
+ # given
+ query = QUERY_PRODUCT_MEDIA_METADATA
+ media = product_with_image.media.first()
+ variables = {
+ "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
+ "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = api_client.post_graphql(query, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_metadata_for_product_media_as_customer_user(
+ user_api_client, product_with_image, channel_USD
+):
+ # given
+ query = QUERY_PRODUCT_MEDIA_METADATA
+ media = product_with_image.media.first()
+ variables = {
+ "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
+ "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = user_api_client.post_graphql(query, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_metadata_for_product_media_as_staff_missing_permissions(
+ staff_api_client, product_with_image, channel_USD
+):
+ # given
+ query = QUERY_PRODUCT_MEDIA_METADATA
+ media = product_with_image.media.first()
+
+ variables = {
+ "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
+ "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
+ "channel": channel_USD.slug,
+ }
+
+ # when
+ response = staff_api_client.post_graphql(query, variables)
+
+ # then
+ assert_no_permission(response)
diff --git a/saleor/graphql/meta/tests/queries/test_warehouse.py b/saleor/graphql/meta/tests/queries/test_warehouse.py
new file mode 100644
index 0000000000..0c0548bfb7
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/test_warehouse.py
@@ -0,0 +1,177 @@
+import graphene
+
+from ....tests.utils import assert_no_permission, get_graphql_content
+from .utils import PRIVATE_KEY, PRIVATE_VALUE, PUBLIC_KEY, PUBLIC_VALUE
+
+QUERY_WAREHOUSE_PUBLIC_META = """
+ query warehouseMeta($id: ID!){
+ warehouse(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_warehouse_as_anonymous_user(api_client, warehouse):
+ # given
+ warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ warehouse.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_WAREHOUSE_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_warehouse_as_customer(user_api_client, warehouse):
+ # given
+ warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ warehouse.save(update_fields=["metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_WAREHOUSE_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_warehouse_as_staff(
+ staff_api_client, warehouse, permission_manage_products
+):
+ # given
+ warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ warehouse.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Warehouse", warehouse.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_WAREHOUSE_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["warehouse"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_warehouse_as_app(
+ app_api_client, warehouse, permission_manage_products
+):
+ # given
+ warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
+ warehouse.save(update_fields=["metadata"])
+ variables = {"id": graphene.Node.to_global_id("Warehouse", warehouse.pk)}
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_WAREHOUSE_PUBLIC_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["warehouse"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+QUERY_WAREHOUSE_PRIVATE_META = """
+ query warehouseMeta($id: ID!){
+ warehouse(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_warehouse_as_anonymous_user(api_client, warehouse):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
+ }
+
+ # when
+ response = api_client.post_graphql(QUERY_WAREHOUSE_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_warehouse_as_customer(user_api_client, warehouse):
+ # given
+ variables = {
+ "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
+ }
+
+ # when
+ response = user_api_client.post_graphql(QUERY_WAREHOUSE_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_warehouse_as_staff(
+ staff_api_client, warehouse, permission_manage_products
+):
+ # given
+ warehouse.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ warehouse.save(update_fields=["private_metadata"])
+ variables = {"id": graphene.Node.to_global_id("Warehouse", warehouse.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_WAREHOUSE_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["warehouse"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_warehouse_as_app(
+ app_api_client, warehouse, permission_manage_products
+):
+ # given
+ warehouse.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
+ warehouse.save(update_fields=["private_metadata"])
+ variables = {
+ "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
+ }
+
+ # when
+ response = app_api_client.post_graphql(
+ QUERY_WAREHOUSE_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["warehouse"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
diff --git a/saleor/graphql/meta/tests/queries/utils.py b/saleor/graphql/meta/tests/queries/utils.py
new file mode 100644
index 0000000000..f9a0fa664a
--- /dev/null
+++ b/saleor/graphql/meta/tests/queries/utils.py
@@ -0,0 +1,46 @@
+from typing import List
+
+import graphene
+from django.http import HttpResponse
+
+from .....core.models import ModelWithMetadata
+from .....permission.models import Permission
+from ....tests.fixtures import ApiClient
+from ....tests.utils import get_graphql_content
+
+PRIVATE_KEY = "private_key"
+PRIVATE_VALUE = "private_vale"
+
+PUBLIC_KEY = "key"
+PUBLIC_KEY2 = "key2"
+PUBLIC_VALUE = "value"
+PUBLIC_VALUE2 = "value2"
+
+
+def execute_query(
+ query_str: str,
+ client: ApiClient,
+ model: ModelWithMetadata,
+ model_name: str,
+ permissions: List[Permission] = None,
+):
+ return client.post_graphql(
+ query_str,
+ variables={"id": graphene.Node.to_global_id(model_name, model.pk)},
+ permissions=[] if permissions is None else permissions,
+ check_no_permissions=False,
+ )
+
+
+def assert_model_contains_private_metadata(response: HttpResponse, model_name: str):
+ content = get_graphql_content(response)
+ metadata = content["data"][model_name]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def assert_model_contains_metadata(response: HttpResponse, model_name: str):
+ content = get_graphql_content(response)
+ metadata = content["data"][model_name]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
diff --git a/saleor/graphql/meta/tests/test_meta_mutations.py b/saleor/graphql/meta/tests/test_meta_mutations.py
deleted file mode 100644
index 98fb2ed35f..0000000000
--- a/saleor/graphql/meta/tests/test_meta_mutations.py
+++ /dev/null
@@ -1,5001 +0,0 @@
-import base64
-from unittest.mock import patch
-
-import before_after
-import graphene
-import pytest
-from django.core.exceptions import ValidationError
-from django.db import transaction
-
-from ....account.error_codes import AccountErrorCode
-from ....account.models import User
-from ....app.models import App
-from ....checkout.models import Checkout
-from ....core.error_codes import MetadataErrorCode
-from ....core.jwt import create_access_token_for_app
-from ....core.models import ModelWithMetadata
-from ....invoice.models import Invoice
-from ....payment.models import TransactionItem
-from ....payment.utils import payment_owned_by_user
-from ...tests.fixtures import ApiClient
-from ...tests.utils import assert_no_permission, get_graphql_content
-
-PRIVATE_KEY = "private_key"
-PRIVATE_VALUE = "private_vale"
-
-PUBLIC_KEY = "key"
-PUBLIC_KEY2 = "key2"
-PUBLIC_VALUE = "value"
-PUBLIC_VALUE2 = "value2"
-
-
-UPDATE_PUBLIC_METADATA_MUTATION = """
-mutation UpdatePublicMetadata($id: ID!, $input: [MetadataInput!]!) {
- updateMetadata(
- id: $id
- input: $input
- ) {
- errors{
- field
- code
- message
- }
- item {
- metadata{
- key
- value
- }
- ...on %s{
- id
- }
- }
- }
-}
-"""
-
-
-def execute_update_public_metadata_for_item(
- client,
- permissions,
- item_id,
- item_type,
- key=PUBLIC_KEY,
- value=PUBLIC_VALUE,
- ignore_errors=False,
-):
- variables = {
- "id": item_id,
- "input": [{"key": key, "value": value}],
- }
-
- response = client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % item_type,
- variables,
- permissions=[permissions] if permissions else None,
- )
- response = get_graphql_content(response, ignore_errors=ignore_errors)
- return response
-
-
-def execute_update_public_metadata_for_multiple_items(
- client,
- permissions,
- item_id,
- item_type,
- key=PUBLIC_KEY,
- value=PUBLIC_VALUE,
- key2=PUBLIC_KEY2,
- value2=PUBLIC_VALUE2,
-):
- variables = {
- "id": item_id,
- "input": [{"key": key, "value": value}, {"key": key2, "value": value2}],
- }
-
- response = client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % item_type,
- variables,
- permissions=[permissions] if permissions else None,
- )
- response = get_graphql_content(response)
- return response
-
-
-def item_contains_proper_public_metadata(
- item_from_response,
- item,
- item_id,
- key=PUBLIC_KEY,
- value=PUBLIC_VALUE,
-):
- if item_from_response["id"] != item_id:
- return False
- item.refresh_from_db()
- return item.get_value_from_metadata(key) == value
-
-
-def item_contains_multiple_proper_public_metadata(
- item_from_response,
- item,
- item_id,
- key=PUBLIC_KEY,
- value=PUBLIC_VALUE,
- key2=PUBLIC_KEY2,
- value2=PUBLIC_VALUE2,
-):
- if item_from_response["id"] != item_id:
- return False
- item.refresh_from_db()
- return all(
- [
- item.get_value_from_metadata(key) == value,
- item.get_value_from_metadata(key2) == value2,
- ]
- )
-
-
-def test_meta_mutations_handle_validation_errors(staff_api_client):
- invalid_id = "6QjoLs5LIqb3At7hVKKcUlqXceKkFK"
- variables = {
- "id": invalid_id,
- "input": [{"key": "year", "value": "of-saleor"}],
- }
- response = staff_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Checkout", variables
- )
- content = get_graphql_content(response)
- errors = content["data"]["updateMetadata"]["errors"]
- assert errors
- assert errors[0]["code"] == MetadataErrorCode.INVALID.name
-
-
-@patch("saleor.plugins.manager.PluginsManager.checkout_updated")
-def test_base_metadata_mutation_handles_errors_from_extra_action(
- mock_checkout_updated, api_client, checkout
-):
- # given
- error_field = "field"
- error_msg = "boom"
- mock_checkout_updated.side_effect = ValidationError({error_field: error_msg})
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, checkout_id, "Checkout"
- )
-
- # then
- errors = response["data"]["updateMetadata"]["errors"]
- assert errors[0]["field"] == error_field
- assert errors[0]["message"] == error_msg
-
-
-def test_add_public_metadata_for_customer_as_staff(
- staff_api_client, permission_manage_users, customer_user
-):
- # given
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_add_public_metadata_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- app_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_change_metadata_for_non_existing_user(app_api_client, customer_user):
- # given the non-existing user ID
- last_id = User.objects.order_by("id").values_list("id", flat=True).last()
- customer_id = graphene.Node.to_global_id("User", last_id + 100)
-
- # when
- response = execute_update_public_metadata_for_item(
- app_api_client, [], customer_id, "User"
- )
-
- # then
- assert (
- response["data"]["updateMetadata"]["errors"][0]["code"]
- == AccountErrorCode.NOT_FOUND.name
- )
-
-
-def test_add_multiple_public_metadata_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_update_public_metadata_for_multiple_items(
- app_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_contains_multiple_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_add_public_metadata_for_other_staff_as_staff(
- staff_api_client, permission_manage_staff, admin_user
-):
- # given
- assert admin_user.pk != staff_api_client.user.pk
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_staff, admin_id, "User"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], admin_user, admin_id
- )
-
-
-def test_add_public_metadata_for_staff_as_app_no_permission(
- app_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
- variables = {
- "id": admin_id,
- "input": [{"key": PUBLIC_KEY, "value": PUBLIC_VALUE}],
- }
-
- # when
-
- response = app_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "User",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
- "input",
- [{"key": " ", "value": "test"}, {"key": " ", "value": ""}],
-)
-def test_staff_update_metadata_empty_key(
- input, staff_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_staff,
- admin_id,
- "User",
- input["key"],
- input["value"],
- )
-
- # then
- data = response["data"]["updateMetadata"]
- errors = data["errors"]
-
- assert not data["item"]
- assert len(errors) == 1
- assert errors[0]["code"] == MetadataErrorCode.REQUIRED.name
- assert errors[0]["field"] == "input"
-
-
-def test_add_public_metadata_for_myself_as_customer(user_api_client):
- # given
- customer = user_api_client.user
- customer_id = graphene.Node.to_global_id("User", customer.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- user_api_client, None, customer_id, "User"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], customer, customer_id
- )
-
-
-def test_add_private_metadata_for_invoice(staff_api_client, permission_manage_orders):
- # given
- invoice = Invoice.objects.create(number="1/7/2020")
- invoice_id = graphene.Node.to_global_id("Invoice", invoice.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_orders, invoice_id, "Invoice"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], invoice, invoice_id
- )
-
-
-def test_add_public_metadata_for_invoice(staff_api_client, permission_manage_orders):
- # given
- invoice = Invoice.objects.create(number="1/7/2020")
- invoice_id = graphene.Node.to_global_id("Invoice", invoice.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_orders, invoice_id, "Invoice"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], invoice, invoice_id
- )
-
-
-def test_add_public_metadata_for_myself_as_staff(staff_api_client):
- # given
- staff = staff_api_client.user
- staff_id = graphene.Node.to_global_id("User", staff.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, None, staff_id, "User"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], staff, staff_id
- )
-
-
-def test_add_public_metadata_for_checkout(api_client, checkout):
- # given
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, checkout_id, "Checkout"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_add_public_metadata_for_checkout_no_checkout_metadata_storage(
- api_client, checkout
-):
- # given
- checkout.metadata_storage.delete()
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, checkout_id, "Checkout"
- )
-
- # then
- checkout.refresh_from_db()
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_add_public_metadata_for_checkout_line(api_client, checkout_line):
- # given
- checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, checkout_line_id, "CheckoutLine"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], checkout_line, checkout_line_id
- )
-
-
-def test_add_public_metadata_for_checkout_by_token(api_client, checkout):
- # given
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, checkout.token, "Checkout"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-@patch("saleor.plugins.manager.PluginsManager.checkout_updated")
-def test_add_metadata_for_checkout_triggers_checkout_updated_hook(
- mock_checkout_updated, api_client, checkout
-):
- # given
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, checkout_id, "Checkout"
- )
-
- # then
- assert response["data"]["updateMetadata"]["errors"] == []
- mock_checkout_updated.assert_called_once_with(checkout)
-
-
-def test_add_public_metadata_for_order_by_id(api_client, order):
- # given
- order_id = graphene.Node.to_global_id("Order", order.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, order_id, "Order"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], order, order_id
- )
-
-
-def test_add_public_metadata_for_order_by_token(api_client, order):
- # given
- order_id = graphene.Node.to_global_id("Order", order.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, order.id, "Order"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], order, order_id
- )
-
-
-def test_add_public_metadata_for_draft_order_by_id(api_client, draft_order):
- # given
- draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, draft_order_id, "Order"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], draft_order, draft_order_id
- )
-
-
-def test_add_public_metadata_for_draft_order_by_token(api_client, draft_order):
- # given
- draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, draft_order.id, "Order"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], draft_order, draft_order_id
- )
-
-
-def test_add_public_metadata_for_order_line(api_client, order_line):
- # given
- order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, order_line_id, "OrderLine"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], order_line, order_line_id
- )
-
-
-def test_add_public_metadata_for_product_attribute(
- staff_api_client, permission_manage_product_types_and_attributes, color_attribute
-):
- # given
- attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_product_types_and_attributes,
- attribute_id,
- "Attribute",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], color_attribute, attribute_id
- )
-
-
-def test_add_public_metadata_for_page_attribute(
- staff_api_client, permission_manage_page_types_and_attributes, size_page_attribute
-):
- # given
- attribute_id = graphene.Node.to_global_id("Attribute", size_page_attribute.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_page_types_and_attributes,
- attribute_id,
- "Attribute",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], size_page_attribute, attribute_id
- )
-
-
-def test_add_public_metadata_for_category(
- staff_api_client, permission_manage_products, category
-):
- # given
- category_id = graphene.Node.to_global_id("Category", category.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_products, category_id, "Category"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], category, category_id
- )
-
-
-def test_add_public_metadata_for_collection(
- staff_api_client, permission_manage_products, published_collection, channel_USD
-):
- # given
- collection_id = graphene.Node.to_global_id("Collection", published_collection.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_products, collection_id, "Collection"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], published_collection, collection_id
- )
-
-
-def test_add_public_metadata_for_digital_content(
- staff_api_client, permission_manage_products, digital_content
-):
- # given
- digital_content_id = graphene.Node.to_global_id(
- "DigitalContent", digital_content.pk
- )
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_products,
- digital_content_id,
- "DigitalContent",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], digital_content, digital_content_id
- )
-
-
-def test_add_public_metadata_for_fulfillment(
- staff_api_client, permission_manage_orders, fulfillment
-):
- # given
- fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_orders, fulfillment_id, "Fulfillment"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], fulfillment, fulfillment_id
- )
-
-
-@patch("saleor.plugins.manager.PluginsManager.product_updated")
-def test_add_public_metadata_for_product(
- updated_webhook_mock, staff_api_client, permission_manage_products, product
-):
- # given
- product_id = graphene.Node.to_global_id("Product", product.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_products, product_id, "Product"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], product, product_id
- )
- updated_webhook_mock.assert_called_once_with(product)
-
-
-def test_add_public_metadata_for_product_type(
- staff_api_client, permission_manage_product_types_and_attributes, product_type
-):
- # given
- product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_product_types_and_attributes,
- product_type_id,
- "ProductType",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], product_type, product_type_id
- )
-
-
-def test_add_public_metadata_for_product_variant(
- staff_api_client, permission_manage_products, variant
-):
- # given
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_products,
- variant_id,
- "ProductVariant",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], variant, variant_id
- )
-
-
-def test_add_public_metadata_for_app(staff_api_client, permission_manage_apps, app):
- # given
- app_id = graphene.Node.to_global_id("App", app.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_apps,
- app_id,
- "App",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], app, app_id
- )
-
-
-def test_add_public_metadata_for_app_by_different_app(
- app_api_client, permission_manage_apps, app, payment_app
-):
- # given
- app_id = graphene.Node.to_global_id("App", payment_app.pk)
- app_api_client.app = app
-
- # when
- response = execute_update_public_metadata_for_item(
- app_api_client,
- permission_manage_apps,
- app_id,
- "App",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], payment_app, app_id
- )
-
-
-def test_add_public_metadata_for_app_that_is_owner(
- app_api_client, permission_manage_apps, app
-):
- # given
- app_id = graphene.Node.to_global_id("App", app.pk)
- app_api_client.app = app
-
- # when
- response = execute_update_public_metadata_for_item(
- app_api_client,
- None,
- app_id,
- "App",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], app, app_id
- )
-
-
-def test_add_public_metadata_for_app_by_staff_without_permissions(
- staff_api_client, app
-):
- # given
- app_id = graphene.Node.to_global_id("App", app.pk)
- variables = {
- "id": app_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = staff_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "App", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_add_public_metadata_for_app_with_app_user_token(app, staff_user):
- # given
- token = create_access_token_for_app(app, staff_user)
- api_client = ApiClient(user=staff_user)
- api_client.token = token
- app_id = graphene.Node.to_global_id("App", app.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client,
- None,
- app_id,
- "App",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], app, app_id
- )
-
-
-def test_add_public_metadata_for_unrelated_app_by_app_user_token(staff_user, app):
- # given
- token = create_access_token_for_app(app, staff_user)
- api_client = ApiClient(user=staff_user)
- api_client.token = token
- second_app = App.objects.create(name="Sample app", is_active=True)
- app_id = graphene.Node.to_global_id("App", second_app.pk)
-
- variables = {
- "id": app_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "App", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_add_public_metadata_for_page(staff_api_client, permission_manage_pages, page):
- # given
- page_id = graphene.Node.to_global_id("Page", page.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_pages,
- page_id,
- "Page",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], page, page_id
- )
-
-
-def test_add_public_metadata_for_shipping_method(
- staff_api_client, permission_manage_shipping, shipping_method
-):
- # given
- shipping_method_id = graphene.Node.to_global_id(
- "ShippingMethodType", shipping_method.pk
- )
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_shipping,
- shipping_method_id,
- "ShippingMethodType",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- shipping_method,
- shipping_method_id,
- )
-
-
-def test_add_public_metadata_for_shipping_zone(
- staff_api_client, permission_manage_shipping, shipping_zone
-):
- # given
- shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_shipping,
- shipping_zone_id,
- "ShippingZone",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- shipping_zone,
- shipping_zone_id,
- )
-
-
-def test_add_public_metadata_for_menu(staff_api_client, permission_manage_menus, menu):
- # given
- menu_id = graphene.Node.to_global_id("Menu", menu.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_menus,
- menu_id,
- "Menu",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- menu,
- menu_id,
- )
-
-
-def test_add_public_metadata_for_menu_item(
- staff_api_client, permission_manage_menus, menu_item
-):
- # given
- menu_item_id = graphene.Node.to_global_id("MenuItem", menu_item.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_menus,
- menu_item_id,
- "MenuItem",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- menu_item,
- menu_item_id,
- )
-
-
-def test_update_public_metadata_for_item(api_client, checkout):
- # given
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.metadata_storage.save(update_fields=["metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, checkout.token, "Checkout", value="NewMetaValue"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- value="NewMetaValue",
- )
-
-
-def test_update_public_metadata_for_checkout_line(api_client, checkout_line):
- # given
- checkout_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout_line.save(update_fields=["metadata"])
- checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, checkout_line_id, "CheckoutLine", value="NewMetaValue"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- checkout_line,
- checkout_line_id,
- value="NewMetaValue",
- )
-
-
-def test_update_public_metadata_for_order_line(api_client, order_line):
- # given
- order_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order_line.save(update_fields=["metadata"])
- order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- api_client, None, order_line_id, "OrderLine", value="NewMetaValue"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- order_line,
- order_line_id,
- value="NewMetaValue",
- )
-
-
[email protected]_db(transaction=True)
-def test_update_public_metadata_for_item_on_deleted_instance(api_client, checkout):
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.metadata_storage.save(update_fields=["metadata"])
-
- def delete_checkout_object(*args, **kwargs):
- with transaction.atomic():
- Checkout.objects.filter(pk=checkout.pk).delete()
-
- with before_after.before(
- "saleor.graphql.meta.mutations._save_instance", delete_checkout_object
- ):
- response = execute_update_public_metadata_for_item(
- api_client,
- None,
- checkout.token,
- "Checkout",
- value="NewMetaValue",
- ignore_errors=True,
- )
-
- assert not Checkout.objects.filter(pk=checkout.pk).first()
- assert (
- response["data"]["updateMetadata"]["errors"][0]["code"]
- == MetadataErrorCode.NOT_FOUND.name
- )
-
-
-def test_update_public_metadata_for_non_exist_item(
- staff_api_client, permission_manage_payments
-):
- # given
- payment_id = "Payment: 0"
- payment_id = base64.b64encode(str.encode(payment_id)).decode("utf-8")
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_payments, payment_id, "Payment"
- )
-
- # then
- errors = response["data"]["updateMetadata"]["errors"]
- assert errors[0]["field"] == "id"
- assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
-
-
-def test_update_public_metadata_for_item_without_meta(
- api_client, permission_group_manage_users
-):
- # given
- group = permission_group_manage_users
- assert not issubclass(type(group), ModelWithMetadata)
- group_id = graphene.Node.to_global_id("Group", group.pk)
-
- # when
- # We use "User" type inside mutation for valid graphql query with fragment
- # without this we are not able to reuse UPDATE_PUBLIC_METADATA_MUTATION
- response = execute_update_public_metadata_for_item(
- api_client, None, group_id, "User"
- )
-
- # then
- errors = response["data"]["updateMetadata"]["errors"]
- assert errors[0]["field"] == "id"
- assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
-
-
-def test_update_public_metadata_for_payment_by_logged_user(
- user_api_client, payment_with_public_metadata
-):
- # given
- payment_with_public_metadata.order.user = user_api_client.user
- payment_with_public_metadata.order.save()
- payment_id = graphene.Node.to_global_id("Payment", payment_with_public_metadata.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- user_api_client, None, payment_id, "Payment", value="NewMetaValue"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- payment_with_public_metadata,
- payment_id,
- value="NewMetaValue",
- )
-
-
-def test_update_public_metadata_for_payment_by_different_logged_user(
- user2_api_client, payment_with_public_metadata
-):
- # given
- assert not payment_owned_by_user(
- payment_with_public_metadata.pk, user2_api_client.user
- )
- payment_id = graphene.Node.to_global_id("Payment", payment_with_public_metadata.pk)
- variables = {
- "id": payment_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = user2_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Payment", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_public_metadata_for_payment_by_non_logged_user(
- api_client, payment_with_public_metadata
-):
- # given
- payment_id = graphene.Node.to_global_id("Payment", payment_with_public_metadata.pk)
- variables = {
- "id": payment_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Payment", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_add_public_metadata_for_warehouse(
- staff_api_client, permission_manage_products, warehouse
-):
- # given
- warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_products, warehouse_id, "Warehouse"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], warehouse, warehouse_id
- )
-
-
-def test_add_public_metadata_for_gift_card(
- staff_api_client, permission_manage_gift_card, gift_card
-):
- # given
- gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_gift_card, gift_card_id, "GiftCard"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], gift_card, gift_card_id
- )
-
-
-def test_update_public_metadata_for_customer_address_by_logged_user(
- user_api_client, address
-):
- # given
- user = user_api_client.user
- user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- user_api_client, None, address_id, "Address", value="NewMetaValue"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- address,
- address_id,
- value="NewMetaValue",
- )
-
-
-def test_update_public_metadata_for_customer_address_by_different_logged_user(
- user2_api_client, customer_user
-):
- # given
- address = customer_user.addresses.first()
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = user2_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_public_metadata_for_customer_address_by_staff_with_perm(
- staff_api_client, customer_user, permission_manage_users
-):
- # given
- address = customer_user.addresses.first()
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_users,
- address_id,
- "Address",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- address,
- address_id,
- value="NewMetaValue",
- )
-
-
-def test_update_public_metadata_for_customer_address_by_app_with_perm(
- app_api_client, customer_user, permission_manage_users
-):
- # given
- address = customer_user.addresses.first()
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- app_api_client,
- permission_manage_users,
- address_id,
- "Address",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- address,
- address_id,
- value="NewMetaValue",
- )
-
-
-def test_update_public_metadata_for_address_by_non_logged_user(
- api_client, customer_user
-):
- # given
- address = customer_user.addresses.first()
- address_id = graphene.Node.to_global_id("Address", address.pk)
- variables = {
- "id": address_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_public_metadata_for_staff_address_by_staff_with_perm(
- staff_api_client, address, staff_users, permission_manage_staff
-):
- # given
- user = staff_users[-1]
- user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client,
- permission_manage_staff,
- address_id,
- "Address",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"],
- address,
- address_id,
- value="NewMetaValue",
- )
-
-
-def test_update_public_metadata_for_staff_address_by_staff_without_perm(
- staff_api_client, address, staff_users
-):
- # given
- user = staff_users[-1]
- user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = staff_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_public_metadata_for_staff_address_by_customer(
- user_api_client, address, staff_users
-):
- # given
- user = staff_users[-1]
- user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = user_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_public_metadata_for_staff_address_by_app_with_perm(
- app_api_client, staff_user, address, permission_manage_staff
-):
- # given
- staff_user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = app_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Address",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_public_metadata_for_staff_address_by_app_without_perm(
- app_api_client, staff_user, address
-):
- # given
- staff_user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = app_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Address", variables
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_public_metadata_for_warehouse_address_by_staff(
- staff_api_client, warehouse, permission_manage_staff
-):
- # given
- address = warehouse.address
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = staff_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Address",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_public_metadata_for_site_settings_address_by_staff(
- staff_api_client, site_settings, address, permission_manage_staff
-):
- # given
- site_settings.company_address = address
- site_settings.save(update_fields=["company_address"])
-
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = staff_api_client.post_graphql(
- UPDATE_PUBLIC_METADATA_MUTATION % "Address",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-DELETE_PUBLIC_METADATA_MUTATION = """
-mutation DeletePublicMetadata($id: ID!, $keys: [String!]!) {
- deleteMetadata(
- id: $id
- keys: $keys
- ) {
- errors{
- field
- code
- }
- item {
- metadata{
- key
- value
- }
- ...on %s{
- id
- }
- }
- }
-}
-"""
-
-
-def execute_clear_public_metadata_for_item(
- client,
- permissions,
- item_id,
- item_type,
- key=PUBLIC_KEY,
-):
- variables = {
- "id": item_id,
- "keys": [key],
- }
- response = client.post_graphql(
- DELETE_PUBLIC_METADATA_MUTATION % item_type,
- variables,
- permissions=[permissions] if permissions else None,
- )
- response = get_graphql_content(response)
- return response
-
-
-def execute_clear_public_metadata_for_multiple_items(
- client, permissions, item_id, item_type, key=PUBLIC_KEY, key2=PUBLIC_KEY2
-):
- variables = {
- "id": item_id,
- "keys": [key, key2],
- }
-
- response = client.post_graphql(
- DELETE_PUBLIC_METADATA_MUTATION % item_type,
- variables,
- permissions=[permissions] if permissions else None,
- )
- response = get_graphql_content(response)
- return response
-
-
-def item_without_public_metadata(
- item_from_response,
- item,
- item_id,
- key=PUBLIC_KEY,
- value=PUBLIC_VALUE,
-):
- if item_from_response["id"] != item_id:
- return False
- item.refresh_from_db()
- return item.get_value_from_metadata(key) != value
-
-
-def item_without_multiple_public_metadata(
- item_from_response,
- item,
- item_id,
- key=PUBLIC_KEY,
- value=PUBLIC_VALUE,
- key2=PUBLIC_KEY2,
- value2=PUBLIC_VALUE2,
-):
- if item_from_response["id"] != item_id:
- return False
- item.refresh_from_db()
- return all(
- [
- item.get_value_from_metadata(key) != value,
- item.get_value_from_metadata(key2) != value2,
- ]
- )
-
-
-def test_delete_public_metadata_for_customer_as_staff(
- staff_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- customer_user.save(update_fields=["metadata"])
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_delete_public_metadata_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- customer_user.save(update_fields=["metadata"])
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- app_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_delete_multiple_public_metadata_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_metadata(
- {PUBLIC_KEY: PUBLIC_VALUE, PUBLIC_KEY2: PUBLIC_VALUE2}
- )
- customer_user.save(update_fields=["metadata"])
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_clear_public_metadata_for_multiple_items(
- app_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_without_multiple_public_metadata(
- response["data"]["deleteMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_delete_public_metadata_for_other_staff_as_staff(
- staff_api_client, permission_manage_staff, admin_user
-):
- # given
- assert admin_user.pk != staff_api_client.user.pk
- admin_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- admin_user.save(update_fields=["metadata"])
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_staff, admin_id, "User"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], admin_user, admin_id
- )
-
-
-def test_delete_public_metadata_for_staff_as_app_no_permission(
- app_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- admin_user.save(update_fields=["metadata"])
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
- variables = {
- "id": admin_id,
- "keys": [PRIVATE_KEY],
- }
-
- # when
- response = app_api_client.post_graphql(
- DELETE_PRIVATE_METADATA_MUTATION % "User",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_delete_public_metadata_for_myself_as_customer(user_api_client):
- # given
- customer = user_api_client.user
- customer.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- customer.save(update_fields=["metadata"])
- customer_id = graphene.Node.to_global_id("User", customer.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- user_api_client, None, customer_id, "User"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], customer, customer_id
- )
-
-
-def test_delete_public_metadata_for_myself_as_staff(staff_api_client):
- # given
- staff = staff_api_client.user
- staff.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- staff.save(update_fields=["metadata"])
- staff_id = graphene.Node.to_global_id("User", staff.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, None, staff_id, "User"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], staff, staff_id
- )
-
-
-def test_delete_public_metadata_for_checkout(api_client, checkout):
- # given
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.metadata_storage.save(update_fields=["metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, checkout_id, "Checkout"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_delete_public_metadata_for_checkout_by_token(api_client, checkout):
- # given
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.metadata_storage.save(update_fields=["metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, checkout.token, "Checkout"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_delete_public_metadata_for_checkout_line(api_client, checkout_line):
- # given
- checkout_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout_line.save(update_fields=["metadata"])
- checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, checkout_line_id, "CheckoutLine"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], checkout_line, checkout_line_id
- )
-
-
-def test_delete_public_metadata_for_order_by_id(api_client, order):
- # given
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["metadata"])
- order_id = graphene.Node.to_global_id("Order", order.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, order_id, "Order"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], order, order_id
- )
-
-
-def test_delete_public_metadata_for_order_by_token(api_client, order):
- # given
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["metadata"])
- order_id = graphene.Node.to_global_id("Order", order.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, order.id, "Order"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], order, order_id
- )
-
-
-def test_delete_public_metadata_for_draft_order_by_id(api_client, draft_order):
- # given
- draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- draft_order.save(update_fields=["metadata"])
- draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, draft_order_id, "Order"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], draft_order, draft_order_id
- )
-
-
-def test_delete_public_metadata_for_draft_order_by_token(api_client, draft_order):
- # given
- draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- draft_order.save(update_fields=["metadata"])
- draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, draft_order.id, "Order"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], draft_order, draft_order_id
- )
-
-
-def test_delete_public_metadata_for_order_line(api_client, order_line):
- # given
- order_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order_line.save(update_fields=["metadata"])
- order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, order_line_id, "OrderLine"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], order_line, order_line_id
- )
-
-
-def test_delete_public_metadata_for_product_attribute(
- staff_api_client, permission_manage_product_types_and_attributes, color_attribute
-):
- # given
- color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- color_attribute.save(update_fields=["metadata"])
- attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_product_types_and_attributes,
- attribute_id,
- "Attribute",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], color_attribute, attribute_id
- )
-
-
-def test_delete_public_metadata_for_page_attribute(
- staff_api_client, permission_manage_page_types_and_attributes, size_page_attribute
-):
- # given
- size_page_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- size_page_attribute.save(update_fields=["metadata"])
- attribute_id = graphene.Node.to_global_id("Attribute", size_page_attribute.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_page_types_and_attributes,
- attribute_id,
- "Attribute",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], size_page_attribute, attribute_id
- )
-
-
-def test_delete_public_metadata_for_category(
- staff_api_client, permission_manage_products, category
-):
- # given
- category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- category.save(update_fields=["metadata"])
- category_id = graphene.Node.to_global_id("Category", category.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_products, category_id, "Category"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], category, category_id
- )
-
-
-def test_delete_public_metadata_for_collection(
- staff_api_client, permission_manage_products, published_collection, channel_USD
-):
- # given
- collection = published_collection
- collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- collection.save(update_fields=["metadata"])
- collection_id = graphene.Node.to_global_id("Collection", collection.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_products, collection_id, "Collection"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], collection, collection_id
- )
-
-
-def test_delete_public_metadata_for_digital_content(
- staff_api_client, permission_manage_products, digital_content
-):
- # given
- digital_content.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- digital_content.save(update_fields=["metadata"])
- digital_content_id = graphene.Node.to_global_id(
- "DigitalContent", digital_content.pk
- )
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_products,
- digital_content_id,
- "DigitalContent",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], digital_content, digital_content_id
- )
-
-
-def test_delete_public_metadata_for_fulfillment(
- staff_api_client, permission_manage_orders, fulfillment
-):
- # given
- fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- fulfillment.save(update_fields=["metadata"])
- fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_orders, fulfillment_id, "Fulfillment"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], fulfillment, fulfillment_id
- )
-
-
-@patch("saleor.plugins.manager.PluginsManager.product_updated")
-def test_delete_public_metadata_for_product(
- updated_webhook_mock, staff_api_client, permission_manage_products, product
-):
- # given
- product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product.save(update_fields=["metadata"])
- product_id = graphene.Node.to_global_id("Product", product.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_products, product_id, "Product"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], product, product_id
- )
- updated_webhook_mock.assert_called_once_with(product)
-
-
-def test_delete_public_metadata_for_product_type(
- staff_api_client, permission_manage_product_types_and_attributes, product_type
-):
- # given
- product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product_type.save(update_fields=["metadata"])
- product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_product_types_and_attributes,
- product_type_id,
- "ProductType",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], product_type, product_type_id
- )
-
-
-def test_delete_public_metadata_for_product_variant(
- staff_api_client, permission_manage_products, variant
-):
- # given
- variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- variant.save(update_fields=["metadata"])
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_products, variant_id, "ProductVariant"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], variant, variant_id
- )
-
-
-def test_delete_public_metadata_for_app(staff_api_client, permission_manage_apps, app):
- # given
- app.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- app.save(update_fields=["metadata"])
- app_id = graphene.Node.to_global_id("App", app.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_apps,
- app_id,
- "App",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], app, app_id
- )
-
-
-def test_delete_public_metadata_for_page(
- staff_api_client, permission_manage_pages, page
-):
- # given
- page.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- page.save(update_fields=["metadata"])
- page_id = graphene.Node.to_global_id("Page", page.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_pages,
- page_id,
- "Page",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], page, page_id
- )
-
-
-def test_delete_public_metadata_for_shipping_method(
- staff_api_client, permission_manage_shipping, shipping_method
-):
- # given
- shipping_method.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- shipping_method.save(update_fields=["metadata"])
- shipping_method_id = graphene.Node.to_global_id(
- "ShippingMethodType", shipping_method.pk
- )
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_shipping,
- shipping_method_id,
- "ShippingMethodType",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"],
- shipping_method,
- shipping_method_id,
- )
-
-
-def test_delete_public_metadata_for_shipping_zone(
- staff_api_client, permission_manage_shipping, shipping_zone
-):
- # given
- shipping_zone.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- shipping_zone.save(update_fields=["metadata"])
- shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_shipping,
- shipping_zone_id,
- "ShippingZone",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"],
- shipping_zone,
- shipping_zone_id,
- )
-
-
-def test_delete_public_metadata_for_menu(
- staff_api_client, permission_manage_menus, menu
-):
- # given
- menu.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- menu.save(update_fields=["metadata"])
- menu_id = graphene.Node.to_global_id("Menu", menu.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_menus,
- menu_id,
- "Menu",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"],
- menu,
- menu_id,
- )
-
-
-def test_delete_public_metadata_for_menu_item(
- staff_api_client, permission_manage_menus, menu_item
-):
- # given
- menu_item.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- menu_item.save(update_fields=["metadata"])
- menu_item_id = graphene.Node.to_global_id("MenuItem", menu_item.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client,
- permission_manage_menus,
- menu_item_id,
- "MenuItem",
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"],
- menu_item,
- menu_item_id,
- )
-
-
-def test_delete_public_metadata_for_non_exist_item(
- staff_api_client, permission_manage_payments
-):
- # given
- payment_id = "Payment: 0"
- payment_id = base64.b64encode(str.encode(payment_id)).decode("utf-8")
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_payments, payment_id, "Checkout"
- )
-
- # then
- errors = response["data"]["deleteMetadata"]["errors"]
- assert errors[0]["field"] == "id"
- assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
-
-
-def test_delete_public_metadata_for_item_without_meta(
- api_client, permission_group_manage_users
-):
- # given
- group = permission_group_manage_users
- assert not issubclass(type(group), ModelWithMetadata)
- group_id = graphene.Node.to_global_id("Group", group.pk)
-
- # when
- # We use "User" type inside mutation for valid graphql query with fragment
- # without this we are not able to reuse DELETE_PUBLIC_METADATA_MUTATION
- response = execute_clear_public_metadata_for_item(
- api_client, None, group_id, "User"
- )
-
- # then
- errors = response["data"]["deleteMetadata"]["errors"]
- assert errors[0]["field"] == "id"
- assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
-
-
-def test_delete_public_metadata_for_not_exist_key(api_client, checkout):
- # given
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.metadata_storage.save(update_fields=["metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, checkout_id, "Checkout", key="Not-exits"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["deleteMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_delete_public_metadata_for_one_key(api_client, checkout):
- # given
- checkout.metadata_storage.store_value_in_metadata(
- {PUBLIC_KEY: PUBLIC_VALUE, "to_clear": PUBLIC_VALUE},
- )
- checkout.metadata_storage.save(update_fields=["metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- api_client, None, checkout_id, "Checkout", key="to_clear"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["deleteMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- key="to_clear",
- )
-
-
-def test_delete_public_metadata_for_warehouse(
- staff_api_client, permission_manage_products, warehouse
-):
- # given
- warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- warehouse.save(update_fields=["metadata"])
- warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_products, warehouse_id, "Warehouse"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], warehouse, warehouse_id
- )
-
-
-def test_delete_public_metadata_for_gift_card(
- staff_api_client, permission_manage_gift_card, gift_card
-):
- # given
- gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- gift_card.save(update_fields=["metadata"])
- gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_gift_card, gift_card_id, "GiftCard"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], gift_card, gift_card_id
- )
-
-
-UPDATE_PRIVATE_METADATA_MUTATION = """
-mutation UpdatePrivateMetadata($id: ID!, $input: [MetadataInput!]!) {
- updatePrivateMetadata(
- id: $id
- input: $input
- ) {
- errors{
- field
- code
- }
- item {
- privateMetadata{
- key
- value
- }
- ...on %s{
- id
- }
- }
- }
-}
-"""
-
-
-def execute_update_private_metadata_for_item(
- client,
- permissions,
- item_id,
- item_type,
- key=PRIVATE_KEY,
- value=PRIVATE_VALUE,
-):
- variables = {
- "id": item_id,
- "input": [{"key": key, "value": value}],
- }
-
- response = client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % item_type,
- variables,
- permissions=[permissions] if permissions else None,
- )
- response = get_graphql_content(response)
- return response
-
-
-def execute_update_private_metadata_for_multiple_items(
- client,
- permissions,
- item_id,
- item_type,
- key=PUBLIC_KEY,
- value=PUBLIC_VALUE,
- key2=PUBLIC_KEY2,
- value2=PUBLIC_VALUE2,
-):
- variables = {
- "id": item_id,
- "input": [{"key": key, "value": value}, {"key": key2, "value": value2}],
- }
-
- response = client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % item_type,
- variables,
- permissions=[permissions] if permissions else None,
- )
- response = get_graphql_content(response)
- return response
-
-
-def item_contains_proper_private_metadata(
- item_from_response,
- item,
- item_id,
- key=PRIVATE_KEY,
- value=PRIVATE_VALUE,
-):
- if item_from_response["id"] != item_id:
- return False
- item.refresh_from_db()
- return item.get_value_from_private_metadata(key) == value
-
-
-def item_contains_multiple_proper_private_metadata(
- item_from_response,
- item,
- item_id,
- key=PUBLIC_KEY,
- value=PUBLIC_VALUE,
- key2=PUBLIC_KEY2,
- value2=PUBLIC_VALUE2,
-):
- if item_from_response["id"] != item_id:
- return False
- item.refresh_from_db()
- return all(
- [
- item.get_value_from_private_metadata(key) == value,
- item.get_value_from_private_metadata(key2) == value2,
- ]
- )
-
-
-def test_add_private_metadata_for_customer_as_staff(
- staff_api_client, permission_manage_users, customer_user
-):
- # given
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_add_private_metadata_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- app_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_add_multiple_private_metadata_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_update_private_metadata_for_multiple_items(
- app_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_contains_multiple_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_add_private_metadata_for_other_staff_as_staff(
- staff_api_client, permission_manage_staff, admin_user
-):
- # given
- assert admin_user.pk != staff_api_client.user.pk
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_staff, admin_id, "User"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], admin_user, admin_id
- )
-
-
-def test_add_private_metadata_for_staff_as_app_no_permission(
- app_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
- variables = {
- "id": admin_id,
- "input": [{"key": PRIVATE_KEY, "value": PRIVATE_VALUE}],
- }
-
- # when
- response = app_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "User",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_add_private_metadata_for_myself_as_customer_no_permission(user_api_client):
- # given
- customer = user_api_client.user
- variables = {
- "id": graphene.Node.to_global_id("User", customer.pk),
- "input": [{"key": PRIVATE_KEY, "value": PRIVATE_VALUE}],
- }
-
- # when
- response = user_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "User",
- variables,
- permissions=[],
- )
-
- # then
- assert_no_permission(response)
-
-
- "input",
- [{"key": " ", "value": "test"}, {"key": " ", "value": ""}],
-)
-def test_staff_update_private_metadata_empty_key(
- input, staff_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_staff,
- admin_id,
- "User",
- input["key"],
- input["value"],
- )
-
- # then
- data = response["data"]["updatePrivateMetadata"]
- errors = data["errors"]
-
- assert not data["item"]
- assert len(errors) == 1
- assert errors[0]["code"] == MetadataErrorCode.REQUIRED.name
- assert errors[0]["field"] == "input"
-
-
-def test_add_private_metadata_for_myself_as_staff(staff_api_client):
- # given
- staff = staff_api_client.user
- variables = {
- "id": graphene.Node.to_global_id("User", staff.pk),
- "input": [{"key": PRIVATE_KEY, "value": PRIVATE_VALUE}],
- }
-
- # when
- response = staff_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "User",
- variables,
- permissions=[],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_add_private_metadata_for_checkout(
- staff_api_client, checkout, permission_manage_checkouts
-):
- # given
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_checkouts, checkout_id, "Checkout"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_add_private_metadata_for_checkout_no_checkout_metadata_storage(
- staff_api_client, checkout, permission_manage_checkouts
-):
- # given
- checkout.metadata_storage.delete()
-
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_checkouts, checkout_id, "Checkout"
- )
-
- # then
- checkout.refresh_from_db()
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_add_private_metadata_for_checkout_line(
- staff_api_client, checkout_line, permission_manage_checkouts
-):
- # given
- checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_checkouts, checkout_line_id, "CheckoutLine"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- checkout_line,
- checkout_line_id,
- )
-
-
-def test_add_private_metadata_for_checkout_by_token(
- staff_api_client, checkout, permission_manage_checkouts
-):
- # given
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_checkouts, checkout.token, "Checkout"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_add_private_metadata_for_order_by_id(
- staff_api_client, order, permission_manage_orders
-):
- # given
- order_id = graphene.Node.to_global_id("Order", order.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_orders, order_id, "Order"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], order, order_id
- )
-
-
-def test_add_private_metadata_for_order_by_token(
- staff_api_client, order, permission_manage_orders
-):
- # given
- order_id = graphene.Node.to_global_id("Order", order.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_orders, order.id, "Order"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], order, order_id
- )
-
-
-def test_add_private_metadata_for_draft_order_by_id(
- staff_api_client, draft_order, permission_manage_orders
-):
- # given
- draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_orders, draft_order_id, "Order"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], draft_order, draft_order_id
- )
-
-
-def test_add_private_metadata_for_draft_order_by_token(
- staff_api_client, draft_order, permission_manage_orders
-):
- # given
- draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_orders, draft_order.id, "Order"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], draft_order, draft_order_id
- )
-
-
-def test_add_private_metadata_for_order_line(
- staff_api_client, order_line, permission_manage_orders
-):
- # given
- order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_orders, order_line_id, "OrderLine"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], order_line, order_line_id
- )
-
-
-def test_add_private_metadata_for_product_attribute(
- staff_api_client, permission_manage_product_types_and_attributes, color_attribute
-):
- # given
- attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_product_types_and_attributes,
- attribute_id,
- "Attribute",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], color_attribute, attribute_id
- )
-
-
-def test_add_private_metadata_for_page_attribute(
- staff_api_client, permission_manage_page_types_and_attributes, size_page_attribute
-):
- # given
- attribute_id = graphene.Node.to_global_id("Attribute", size_page_attribute.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_page_types_and_attributes,
- attribute_id,
- "Attribute",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- size_page_attribute,
- attribute_id,
- )
-
-
-def test_add_private_metadata_for_category(
- staff_api_client, permission_manage_products, category
-):
- # given
- category_id = graphene.Node.to_global_id("Category", category.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_products, category_id, "Category"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], category, category_id
- )
-
-
-def test_add_private_metadata_for_collection(
- staff_api_client, permission_manage_products, published_collection, channel_USD
-):
- # given
- collection = published_collection
- collection_id = graphene.Node.to_global_id("Collection", collection.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_products, collection_id, "Collection"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], collection, collection_id
- )
-
-
-def test_add_private_metadata_for_digital_content(
- staff_api_client, permission_manage_products, digital_content
-):
- # given
- digital_content_id = graphene.Node.to_global_id(
- "DigitalContent", digital_content.pk
- )
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_products,
- digital_content_id,
- "DigitalContent",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- digital_content,
- digital_content_id,
- )
-
-
-def test_add_private_metadata_for_fulfillment(
- staff_api_client, permission_manage_orders, fulfillment
-):
- # given
- fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_orders, fulfillment_id, "Fulfillment"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], fulfillment, fulfillment_id
- )
-
-
-@patch("saleor.plugins.manager.PluginsManager.product_updated")
-def test_add_private_metadata_for_product(
- updated_webhook_mock, staff_api_client, permission_manage_products, product
-):
- # given
- product_id = graphene.Node.to_global_id("Product", product.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_products, product_id, "Product"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], product, product_id
- )
- updated_webhook_mock.assert_called_once_with(product)
-
-
-def test_add_private_metadata_for_product_type(
- staff_api_client, permission_manage_product_types_and_attributes, product_type
-):
- # given
- product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_product_types_and_attributes,
- product_type_id,
- "ProductType",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], product_type, product_type_id
- )
-
-
-def test_add_private_metadata_for_product_variant(
- staff_api_client, permission_manage_products, variant
-):
- # given
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_products,
- variant_id,
- "ProductVariant",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], variant, variant_id
- )
-
-
-def test_add_private_metadata_for_app(staff_api_client, permission_manage_apps, app):
- # given
- app_id = graphene.Node.to_global_id("App", app.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_apps,
- app_id,
- "App",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- app,
- app_id,
- )
-
-
-def test_add_private_metadata_for_app_by_different_app(
- app_api_client, permission_manage_apps, app, payment_app
-):
- # given
- app_id = graphene.Node.to_global_id("App", payment_app.pk)
- app_api_client.app = app
-
- # when
- response = execute_update_private_metadata_for_item(
- app_api_client,
- permission_manage_apps,
- app_id,
- "App",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- payment_app,
- app_id,
- )
-
-
-def test_add_private_metadata_for_app_with_app_user_token(
- staff_user, permission_manage_apps, app, payment_app
-):
- # given
- token = create_access_token_for_app(app, staff_user)
- api_client = ApiClient(user=staff_user)
- api_client.token = token
- app_id = graphene.Node.to_global_id("App", app.pk)
-
- variables = {
- "id": app_id,
- "input": [{"key": PUBLIC_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "App", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_add_private_metadata_by_app_that_is_owner(
- app_api_client, permission_manage_apps, app
-):
- # given
- app_id = graphene.Node.to_global_id("App", app.pk)
- app_api_client.app = app
-
- # when
- response = execute_update_private_metadata_for_item(
- app_api_client,
- None,
- app_id,
- "App",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- app,
- app_id,
- )
-
-
-def test_add_private_metadata_for_page(staff_api_client, permission_manage_pages, page):
- # given
- page_id = graphene.Node.to_global_id("Page", page.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_pages,
- page_id,
- "Page",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- page,
- page_id,
- )
-
-
-def test_add_private_metadata_for_shipping_method(
- staff_api_client, permission_manage_shipping, shipping_method
-):
- # given
- shipping_method_id = graphene.Node.to_global_id(
- "ShippingMethodType", shipping_method.pk
- )
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_shipping,
- shipping_method_id,
- "ShippingMethodType",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- shipping_method,
- shipping_method_id,
- )
-
-
-def test_add_private_metadata_for_shipping_zone(
- staff_api_client, permission_manage_shipping, shipping_zone
-):
- # given
- shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_shipping,
- shipping_zone_id,
- "ShippingZone",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- shipping_zone,
- shipping_zone_id,
- )
-
-
-def test_add_private_metadata_for_menu(staff_api_client, permission_manage_menus, menu):
- # given
- menu_id = graphene.Node.to_global_id("Menu", menu.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_menus,
- menu_id,
- "Menu",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- menu,
- menu_id,
- )
-
-
-def test_add_private_metadata_for_menu_item(
- staff_api_client, permission_manage_menus, menu_item
-):
- # given
- menu_item_id = graphene.Node.to_global_id("MenuItem", menu_item.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_menus,
- menu_item_id,
- "MenuItem",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- menu_item,
- menu_item_id,
- )
-
-
-def test_update_private_metadata_for_item(
- staff_api_client, checkout, permission_manage_checkouts
-):
- # given
- checkout.metadata_storage.store_value_in_private_metadata(
- {PRIVATE_KEY: PRIVATE_KEY}
- )
- checkout.metadata_storage.save(update_fields=["private_metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_checkouts,
- checkout.token,
- "Checkout",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- value="NewMetaValue",
- )
-
-
-def test_update_private_metadata_for_non_exist_item(
- staff_api_client, permission_manage_payments
-):
- # given
- payment_id = "Payment: 0"
- payment_id = base64.b64encode(str.encode(payment_id)).decode("utf-8")
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_payments, payment_id, "Payment"
- )
-
- # then
- errors = response["data"]["updatePrivateMetadata"]["errors"]
- assert errors[0]["field"] == "id"
- assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
-
-
-def test_update_private_metadata_for_item_without_meta(
- api_client, permission_group_manage_users
-):
- # given
- group = permission_group_manage_users
- assert not issubclass(type(group), ModelWithMetadata)
- group_id = graphene.Node.to_global_id("Group", group.pk)
-
- # when
- # We use "User" type inside mutation for valid graphql query with fragment
- # without this we are not able to reuse UPDATE_PRIVATE_METADATA_MUTATION
- response = execute_update_private_metadata_for_item(
- api_client, None, group_id, "User"
- )
-
- # then
- errors = response["data"]["updatePrivateMetadata"]["errors"]
- assert errors[0]["field"] == "id"
- assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
-
-
-def test_update_private_metadata_for_payment_by_staff(
- staff_api_client, permission_manage_payments, payment_with_private_metadata
-):
- # given
- payment_id = graphene.Node.to_global_id("Payment", payment_with_private_metadata.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_payments,
- payment_id,
- "Payment",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- payment_with_private_metadata,
- payment_id,
- value="NewMetaValue",
- )
-
-
-def test_update_private_metadata_for_payment_by_app(
- app_api_client, permission_manage_payments, payment_with_private_metadata
-):
- # given
- payment_id = graphene.Node.to_global_id("Payment", payment_with_private_metadata.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- app_api_client,
- permission_manage_payments,
- payment_id,
- "Payment",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- payment_with_private_metadata,
- payment_id,
- value="NewMetaValue",
- )
-
-
-def test_update_private_metadata_for_payment_by_staff_without_permission(
- staff_api_client, payment_with_private_metadata
-):
- # given
- payment_id = graphene.Node.to_global_id("Payment", payment_with_private_metadata.pk)
- variables = {
- "id": payment_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = staff_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Payment", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_payment_by_app_without_permission(
- app_api_client, payment
-):
- # given
- payment_id = graphene.Node.to_global_id("Payment", payment.pk)
- variables = {
- "id": payment_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = app_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Payment", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_by_customer(user_api_client, payment):
- # given
- payment_id = graphene.Node.to_global_id("Payment", payment.pk)
- variables = {
- "id": payment_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = user_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Payment", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_checkout_line(
- staff_api_client, checkout_line, permission_manage_checkouts
-):
- # given
- checkout_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout_line.save(update_fields=["private_metadata"])
- checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_checkouts,
- checkout_line_id,
- "CheckoutLine",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- checkout_line,
- checkout_line_id,
- value="NewMetaValue",
- )
-
-
-def test_update_private_metadata_for_order_line(
- staff_api_client, order_line, permission_manage_orders
-):
- # given
- order_line.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order_line.save(update_fields=["private_metadata"])
- order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_orders,
- order_line_id,
- "OrderLine",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- order_line,
- order_line_id,
- value="NewMetaValue",
- )
-
-
-def test_add_private_metadata_for_warehouse(
- staff_api_client, permission_manage_products, warehouse
-):
- # given
- warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_products, warehouse_id, "Warehouse"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], warehouse, warehouse_id
- )
-
-
-def test_add_private_metadata_for_gift_card(
- staff_api_client, permission_manage_gift_card, gift_card
-):
- # given
- gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_gift_card, gift_card_id, "GiftCard"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], gift_card, gift_card_id
- )
-
-
-def test_update_private_metadata_for_customer_address_by_logged_user(
- user_api_client, address
-):
- # given
- user = user_api_client.user
- user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = user_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_customer_address_by_different_logged_user(
- user2_api_client, customer_user
-):
- # given
- address = customer_user.addresses.first()
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = user2_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_customer_address_by_staff_with_perm(
- staff_api_client, customer_user, permission_manage_users
-):
- # given
- address = customer_user.addresses.first()
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_users,
- address_id,
- "Address",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- address,
- address_id,
- value="NewMetaValue",
- )
-
-
-def test_update_private_metadata_for_customer_address_by_app_with_perm(
- app_api_client, customer_user, permission_manage_users
-):
- # given
- address = customer_user.addresses.first()
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- app_api_client,
- permission_manage_users,
- address_id,
- "Address",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- address,
- address_id,
- value="NewMetaValue",
- )
-
-
-def test_update_private_metadata_for_address_by_non_logged_user(
- api_client, customer_user
-):
- # given
- address = customer_user.addresses.first()
- address_id = graphene.Node.to_global_id("Address", address.pk)
- variables = {
- "id": address_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_staff_address_by_staff_with_perm(
- staff_api_client, address, staff_users, permission_manage_staff
-):
- # given
- user = staff_users[-1]
- user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client,
- permission_manage_staff,
- address_id,
- "Address",
- value="NewMetaValue",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- address,
- address_id,
- value="NewMetaValue",
- )
-
-
-def test_update_private_metadata_for_staff_address_by_staff_without_perm(
- staff_api_client, address, staff_users
-):
- # given
- user = staff_users[-1]
- user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = staff_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_staff_address_by_customer(
- user_api_client, address, staff_users
-):
- # given
- user = staff_users[-1]
- user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = user_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables, permissions=None
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_staff_address_by_app_with_perm(
- app_api_client, staff_user, address, permission_manage_staff
-):
- # given
- staff_user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = app_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Address",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_staff_address_by_app_without_perm(
- app_api_client, staff_user, address
-):
- # given
- staff_user.addresses.add(address)
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = app_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Address", variables
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_warehouse_address_by_staff(
- staff_api_client, warehouse, permission_manage_staff
-):
- # given
- address = warehouse.address
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = staff_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Address",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_update_private_metadata_for_site_settings_address_by_staff(
- staff_api_client, site_settings, address, permission_manage_staff
-):
- # given
- site_settings.company_address = address
- site_settings.save(update_fields=["company_address"])
-
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- variables = {
- "id": address_id,
- "input": [{"key": PRIVATE_KEY, "value": "NewMetaValue"}],
- }
-
- # when
- response = staff_api_client.post_graphql(
- UPDATE_PRIVATE_METADATA_MUTATION % "Address",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-DELETE_PRIVATE_METADATA_MUTATION = """
-mutation DeletePrivateMetadata($id: ID!, $keys: [String!]!) {
- deletePrivateMetadata(
- id: $id
- keys: $keys
- ) {
- errors{
- field
- code
- }
- item {
- privateMetadata{
- key
- value
- }
- ...on %s{
- id
- }
- }
- }
-}
-"""
-
-
-def execute_clear_private_metadata_for_item(
- client,
- permissions,
- item_id,
- item_type,
- key=PRIVATE_KEY,
-):
- variables = {
- "id": item_id,
- "keys": [key],
- }
-
- response = client.post_graphql(
- DELETE_PRIVATE_METADATA_MUTATION % item_type,
- variables,
- permissions=[permissions] if permissions else None,
- )
- response = get_graphql_content(response)
- return response
-
-
-def execute_clear_private_metadata_for_multiple_items(
- client, permissions, item_id, item_type, key=PUBLIC_KEY, key2=PUBLIC_KEY2
-):
- variables = {
- "id": item_id,
- "keys": [key, key2],
- }
-
- response = client.post_graphql(
- DELETE_PUBLIC_METADATA_MUTATION % item_type,
- variables,
- permissions=[permissions] if permissions else None,
- )
- response = get_graphql_content(response)
- return response
-
-
-def item_without_private_metadata(
- item_from_response,
- item,
- item_id,
- key=PRIVATE_KEY,
- value=PRIVATE_VALUE,
-):
- if item_from_response["id"] != item_id:
- return False
- item.refresh_from_db()
- return item.get_value_from_private_metadata(key) != value
-
-
-def item_without_multiple_private_metadata(
- item_from_response,
- item,
- item_id,
- key=PUBLIC_KEY,
- value=PUBLIC_VALUE,
- key2=PUBLIC_KEY2,
- value2=PUBLIC_VALUE2,
-):
- if item_from_response["id"] != item_id:
- return False
- item.refresh_from_db()
- return all(
- [
- item.get_value_from_private_metadata(key) != value,
- item.get_value_from_private_metadata(key2) != value2,
- ]
- )
-
-
-def test_delete_private_metadata_for_customer_as_staff(
- staff_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- customer_user.save(update_fields=["private_metadata"])
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_delete_private_metadata_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- customer_user.save(update_fields=["private_metadata"])
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- app_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_delete_multiple_private_metadata_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_metadata(
- {PUBLIC_KEY: PUBLIC_VALUE, PUBLIC_KEY2: PUBLIC_VALUE2}
- )
- customer_user.save(update_fields=["metadata"])
- customer_id = graphene.Node.to_global_id("User", customer_user.pk)
-
- # when
- response = execute_clear_private_metadata_for_multiple_items(
- app_api_client, permission_manage_users, customer_id, "User"
- )
-
- # then
- assert item_without_multiple_private_metadata(
- response["data"]["deleteMetadata"]["item"], customer_user, customer_id
- )
-
-
-def test_delete_private_metadata_for_other_staff_as_staff(
- staff_api_client, permission_manage_staff, admin_user
-):
- # given
- assert admin_user.pk != staff_api_client.user.pk
- admin_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- admin_user.save(update_fields=["private_metadata"])
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_staff, admin_id, "User"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], admin_user, admin_id
- )
-
-
-def test_delete_private_metadata_for_staff_as_app_no_permission(
- app_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- admin_user.save(update_fields=["private_metadata"])
- admin_id = graphene.Node.to_global_id("User", admin_user.pk)
- variables = {
- "id": admin_id,
- "keys": [PRIVATE_KEY],
- }
-
- # when
- response = app_api_client.post_graphql(
- DELETE_PRIVATE_METADATA_MUTATION % "User",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_delete_private_metadata_for_myself_as_customer_no_permission(user_api_client):
- # given
- customer = user_api_client.user
- customer.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- customer.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("User", customer.pk),
- "keys": [PRIVATE_KEY],
- }
-
- # when
- response = user_api_client.post_graphql(
- DELETE_PRIVATE_METADATA_MUTATION % "User", variables, permissions=[]
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_delete_private_metadata_for_myself_as_staff_no_permission(
- staff_api_client, permission_manage_users
-):
- # given
- staff = staff_api_client.user
- staff.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- staff.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("User", staff.pk),
- "keys": [PRIVATE_KEY],
- }
-
- # when
- response = staff_api_client.post_graphql(
- DELETE_PRIVATE_METADATA_MUTATION % "User",
- variables,
- permissions=[permission_manage_users],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_delete_private_metadata_for_checkout(
- staff_api_client, checkout, permission_manage_checkouts
-):
- # given
- checkout.metadata_storage.store_value_in_private_metadata(
- {PRIVATE_KEY: PRIVATE_VALUE}
- )
- checkout.metadata_storage.save(update_fields=["private_metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_checkouts, checkout_id, "Checkout"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_delete_private_metadata_for_checkout_by_token(
- staff_api_client, checkout, permission_manage_checkouts
-):
- # given
- checkout.metadata_storage.store_value_in_private_metadata(
- {PRIVATE_KEY: PRIVATE_VALUE}
- )
- checkout.metadata_storage.save(update_fields=["private_metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_checkouts, checkout.token, "Checkout"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_delete_private_metadata_for_checkout_line(
- staff_api_client, checkout_line, permission_manage_checkouts
-):
- # given
- checkout_line.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- checkout_line.save(update_fields=["private_metadata"])
- checkout_line_id = graphene.Node.to_global_id("CheckoutLine", checkout_line.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_checkouts, checkout_line_id, "CheckoutLine"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- checkout_line,
- checkout_line_id,
- )
-
-
-def test_delete_private_metadata_for_order_by_id(
- staff_api_client, order, permission_manage_orders
-):
- # given
- order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- order.save(update_fields=["private_metadata"])
- order_id = graphene.Node.to_global_id("Order", order.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_orders, order_id, "Order"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], order, order_id
- )
-
-
-def test_delete_private_metadata_for_order_by_token(
- staff_api_client, order, permission_manage_orders
-):
- # given
- order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- order.save(update_fields=["private_metadata"])
- order_id = graphene.Node.to_global_id("Order", order.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_orders, order.id, "Order"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], order, order_id
- )
-
-
-def test_delete_private_metadata_for_draft_order_by_id(
- staff_api_client, draft_order, permission_manage_orders
-):
- # given
- draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- draft_order.save(update_fields=["private_metadata"])
- draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_orders, draft_order_id, "Order"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], draft_order, draft_order_id
- )
-
-
-def test_delete_private_metadata_for_draft_order_by_token(
- staff_api_client, draft_order, permission_manage_orders
-):
- # given
- draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- draft_order.save(update_fields=["private_metadata"])
- draft_order_id = graphene.Node.to_global_id("Order", draft_order.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_orders, draft_order.id, "Order"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], draft_order, draft_order_id
- )
-
-
-def test_delete_private_metadata_for_order_line(
- staff_api_client, order_line, permission_manage_orders
-):
- # given
- order_line.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- order_line.save(update_fields=["private_metadata"])
- order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_orders, order_line_id, "OrderLine"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], order_line, order_line_id
- )
-
-
-def test_delete_private_metadata_for_product_attribute(
- staff_api_client, permission_manage_product_types_and_attributes, color_attribute
-):
- # given
- color_attribute.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- color_attribute.save(update_fields=["private_metadata"])
- attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_product_types_and_attributes,
- attribute_id,
- "Attribute",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], color_attribute, attribute_id
- )
-
-
-def test_delete_private_metadata_for_page_attribute(
- staff_api_client, permission_manage_page_types_and_attributes, size_page_attribute
-):
- # given
- size_page_attribute.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- size_page_attribute.save(update_fields=["private_metadata"])
- attribute_id = graphene.Node.to_global_id("Attribute", size_page_attribute.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_page_types_and_attributes,
- attribute_id,
- "Attribute",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- size_page_attribute,
- attribute_id,
- )
-
-
-def test_delete_private_metadata_for_category(
- staff_api_client, permission_manage_products, category
-):
- # given
- category.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- category.save(update_fields=["private_metadata"])
- category_id = graphene.Node.to_global_id("Category", category.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_products, category_id, "Category"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], category, category_id
- )
-
-
-def test_delete_private_metadata_for_collection(
- staff_api_client, permission_manage_products, published_collection, channel_USD
-):
- # given
- collection = published_collection
- collection.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- collection.save(update_fields=["private_metadata"])
- collection_id = graphene.Node.to_global_id("Collection", collection.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_products, collection_id, "Collection"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], collection, collection_id
- )
-
-
-def test_delete_private_metadata_for_digital_content(
- staff_api_client, permission_manage_products, digital_content
-):
- # given
- digital_content.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- digital_content.save(update_fields=["private_metadata"])
- digital_content_id = graphene.Node.to_global_id(
- "DigitalContent", digital_content.pk
- )
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_products,
- digital_content_id,
- "DigitalContent",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- digital_content,
- digital_content_id,
- )
-
-
-def test_delete_private_metadata_for_fulfillment(
- staff_api_client, permission_manage_orders, fulfillment
-):
- # given
- fulfillment.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- fulfillment.save(update_fields=["private_metadata"])
- fulfillment_id = graphene.Node.to_global_id("Fulfillment", fulfillment.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_orders, fulfillment_id, "Fulfillment"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], fulfillment, fulfillment_id
- )
-
-
-@patch("saleor.plugins.manager.PluginsManager.product_updated")
-def test_delete_private_metadata_for_product(
- updated_webhook_mock, staff_api_client, permission_manage_products, product
-):
- # given
- product.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- product.save(update_fields=["private_metadata"])
- product_id = graphene.Node.to_global_id("Product", product.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_products, product_id, "Product"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], product, product_id
- )
- updated_webhook_mock.assert_called_once_with(product)
-
-
-def test_delete_private_metadata_for_product_type(
- staff_api_client, permission_manage_product_types_and_attributes, product_type
-):
- # given
- product_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- product_type.save(update_fields=["private_metadata"])
- product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_product_types_and_attributes,
- product_type_id,
- "ProductType",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], product_type, product_type_id
- )
-
-
-def test_delete_private_metadata_for_product_variant(
- staff_api_client, permission_manage_products, variant
-):
- # given
- variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- variant.save(update_fields=["private_metadata"])
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_products, variant_id, "ProductVariant"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], variant, variant_id
- )
-
-
-def test_delete_private_metadata_for_app(staff_api_client, permission_manage_apps, app):
- # given
- app.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- app.save(update_fields=["private_metadata"])
- app_id = graphene.Node.to_global_id("App", app.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_apps,
- app_id,
- "App",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- app,
- app_id,
- )
-
-
-def test_delete_private_metadata_for_page(
- staff_api_client, permission_manage_pages, page
-):
- # given
- page.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- page.save(update_fields=["private_metadata"])
- page_id = graphene.Node.to_global_id("Page", page.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_pages,
- page_id,
- "Page",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- page,
- page_id,
- )
-
-
-def test_delete_private_metadata_for_shipping_method(
- staff_api_client, permission_manage_shipping, shipping_method
-):
- # given
- shipping_method.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- shipping_method.save(update_fields=["metadata"])
- shipping_method_id = graphene.Node.to_global_id(
- "ShippingMethodType", shipping_method.pk
- )
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_shipping,
- shipping_method_id,
- "ShippingMethodType",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- shipping_method,
- shipping_method_id,
- )
-
-
-def test_delete_private_metadata_for_shipping_zone(
- staff_api_client, permission_manage_shipping, shipping_zone
-):
- # given
- shipping_zone.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- shipping_zone.save(update_fields=["metadata"])
- shipping_zone_id = graphene.Node.to_global_id("ShippingZone", shipping_zone.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_shipping,
- shipping_zone_id,
- "ShippingZone",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- shipping_zone,
- shipping_zone_id,
- )
-
-
-def test_delete_private_metadata_for_menu(
- staff_api_client, permission_manage_menus, menu
-):
- # given
- menu.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- menu.save(update_fields=["metadata"])
- menu_id = graphene.Node.to_global_id("Menu", menu.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_menus,
- menu_id,
- "Menu",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- menu,
- menu_id,
- )
-
-
-def test_delete_private_metadata_for_menu_item(
- staff_api_client, permission_manage_menus, menu_item
-):
- # given
- menu_item.store_value_in_private_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- menu_item.save(update_fields=["metadata"])
- menu_item_id = graphene.Node.to_global_id("MenuItem", menu_item.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_menus,
- menu_item_id,
- "MenuItem",
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- menu_item,
- menu_item_id,
- )
-
-
-def test_delete_private_metadata_for_non_exist_item(
- staff_api_client, permission_manage_payments
-):
- # given
- payment_id = "Payment: 0"
- payment_id = base64.b64encode(str.encode(payment_id)).decode("utf-8")
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_payments, payment_id, "Payment"
- )
-
- # then
- errors = response["data"]["deletePrivateMetadata"]["errors"]
- assert errors[0]["field"] == "id"
- assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
-
-
-def test_delete_private_metadata_for_item_without_meta(
- api_client, permission_group_manage_users
-):
- # given
- group = permission_group_manage_users
- assert not issubclass(type(group), ModelWithMetadata)
- group_id = graphene.Node.to_global_id("Group", group.pk)
-
- # when
- # We use "User" type inside mutation for valid graphql query with fragment
- # without this we are not able to reuse DELETE_PRIVATE_METADATA_MUTATION
- response = execute_clear_private_metadata_for_item(
- api_client, None, group_id, "User"
- )
-
- # then
- errors = response["data"]["deletePrivateMetadata"]["errors"]
- assert errors[0]["field"] == "id"
- assert errors[0]["code"] == MetadataErrorCode.NOT_FOUND.name
-
-
-def test_delete_private_metadata_for_not_exist_key(
- staff_api_client, checkout, permission_manage_checkouts
-):
- # given
- checkout.metadata_storage.store_value_in_private_metadata(
- {PRIVATE_KEY: PRIVATE_VALUE}
- )
- checkout.metadata_storage.save(update_fields=["private_metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_checkouts,
- checkout.token,
- "Checkout",
- key="Not-exits",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
-
-
-def test_delete_private_metadata_for_one_key(
- staff_api_client, checkout, permission_manage_checkouts
-):
- # given
- checkout.metadata_storage.store_value_in_private_metadata(
- {PRIVATE_KEY: PRIVATE_VALUE, "to_clear": PRIVATE_VALUE},
- )
- checkout.metadata_storage.save(update_fields=["private_metadata"])
- checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client,
- permission_manage_checkouts,
- checkout.token,
- "Checkout",
- key="to_clear",
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- )
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- checkout.metadata_storage,
- checkout_id,
- key="to_clear",
- )
-
-
-def test_delete_private_metadata_for_warehouse(
- staff_api_client, permission_manage_products, warehouse
-):
- # given
- warehouse.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- warehouse.save(update_fields=["private_metadata"])
- warehouse_id = graphene.Node.to_global_id("Warehouse", warehouse.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_products, warehouse_id, "Warehouse"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], warehouse, warehouse_id
- )
-
-
-def test_delete_private_metadata_for_gift_card(
- staff_api_client, permission_manage_gift_card, gift_card
-):
- # given
- gift_card.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- gift_card.save(update_fields=["private_metadata"])
- gift_card_id = graphene.Node.to_global_id("GiftCard", gift_card.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_gift_card, gift_card_id, "GiftCard"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], gift_card, gift_card_id
- )
-
-
-def test_add_public_metadata_for_sale(
- staff_api_client, permission_manage_discounts, sale
-):
- # given
- sale_id = graphene.Node.to_global_id("Sale", sale.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_discounts, sale_id, "Sale"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], sale, sale_id
- )
-
-
-def test_delete_public_metadata_for_sale(
- staff_api_client, permission_manage_discounts, sale
-):
- # given
- sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- sale.save(update_fields=["metadata"])
- sale_id = graphene.Node.to_global_id("Sale", sale.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_discounts, sale_id, "Sale"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], sale, sale_id
- )
-
-
-def test_add_private_metadata_for_sale(
- staff_api_client, permission_manage_discounts, sale
-):
- # given
- sale_id = graphene.Node.to_global_id("Sale", sale.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_discounts, sale_id, "Sale"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], sale, sale_id
- )
-
-
-def test_delete_private_metadata_for_sale(
- staff_api_client, permission_manage_discounts, sale
-):
- # given
- sale.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- sale.save(update_fields=["private_metadata"])
- sale_id = graphene.Node.to_global_id("Sale", sale.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_discounts, sale_id, "Sale"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], sale, sale_id
- )
-
-
-def test_delete_private_metadata_for_customer_address_as_staff(
- staff_api_client, permission_manage_users, customer_user
-):
- # given
- address = customer_user.addresses.first()
- address.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- address.save(update_fields=["private_metadata"])
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_users, address_id, "Address"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], address, address_id
- )
-
-
-def test_delete_private_metadata_for_customer_address_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- address = customer_user.addresses.first()
- address.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- address.save(update_fields=["private_metadata"])
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- app_api_client, permission_manage_users, address_id, "Address"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], address, address_id
- )
-
-
-def test_delete_private_metadata_for_staff_address_as_staff(
- staff_api_client, address, permission_manage_staff
-):
- # given
- staff_user = staff_api_client.user
- staff_user.addresses.add(address)
- address.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- address.save(update_fields=["private_metadata"])
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_staff, address_id, "Address"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], address, address_id
- )
-
-
-def test_delete_private_metadata_for_staff_address_as_app(
- app_api_client, staff_user, address, permission_manage_staff
-):
- # given
- staff_user.addresses.add(address)
- address.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- address.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Address", address.pk),
- "keys": [PRIVATE_KEY],
- }
-
- # when
- response = app_api_client.post_graphql(
- DELETE_PRIVATE_METADATA_MUTATION % "Address",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_delete_private_metadata_for_myself_address_as_staff_no_permission(
- staff_api_client, address, permission_manage_users
-):
- # given
- staff = staff_api_client.user
- staff.addresses.add(address)
- staff.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- staff.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Address", address.pk),
- "keys": [PRIVATE_KEY],
- }
-
- # when
- response = staff_api_client.post_graphql(
- DELETE_PRIVATE_METADATA_MUTATION % "Address",
- variables,
- permissions=[permission_manage_users],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_delete_public_metadata_for_customer_address_as_staff(
- staff_api_client, permission_manage_users, customer_user
-):
- # given
- address = customer_user.addresses.first()
- address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- address.save(update_fields=["metadata"])
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_users, address_id, "Address"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], address, address_id
- )
-
-
-def test_delete_public_metadata_for_customer_address_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- address = customer_user.addresses.first()
- address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- address.save(update_fields=["metadata"])
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- app_api_client, permission_manage_users, address_id, "Address"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], address, address_id
- )
-
-
-def test_delete_public_metadata_for_staff_address_as_another_staff(
- staff_api_client, staff_users, address, permission_manage_staff
-):
- # given
- staff_user = staff_users[-1]
- staff_user.addresses.add(address)
- address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- address.save(update_fields=["metadata"])
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_staff, address_id, "Address"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], address, address_id
- )
-
-
-def test_delete_public_metadata_for_staff_address_as_staff(staff_api_client, address):
- # given
- staff_user = staff_api_client.user
- staff_user.addresses.add(address)
- address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- address.save(update_fields=["metadata"])
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, None, address_id, "Address"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], address, address_id
- )
-
-
-def test_delete_public_metadata_for_staff_address_as_app(
- app_api_client, staff_user, address, permission_manage_staff
-):
- # given
- staff_user.addresses.add(address)
- address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- address.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Address", address.pk),
- "keys": [PUBLIC_KEY],
- }
-
- # when
- response = app_api_client.post_graphql(
- DELETE_PUBLIC_METADATA_MUTATION % "Address",
- variables,
- permissions=[permission_manage_staff],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_delete_public_metadata_for_myself_address(staff_api_client, address):
- # given
- staff = staff_api_client.user
- staff.addresses.add(address)
- address.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- staff.save(update_fields=["metadata"])
- address_id = graphene.Node.to_global_id("Address", address.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, None, address_id, "Address"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], address, address_id
- )
-
-
-def test_add_public_metadata_for_voucher(
- staff_api_client, permission_manage_discounts, voucher
-):
- # given
- voucher_id = graphene.Node.to_global_id("Voucher", voucher.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_discounts, voucher_id, "Voucher"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], voucher, voucher_id
- )
-
-
-def test_delete_public_metadata_for_voucher(
- staff_api_client, permission_manage_discounts, voucher
-):
- # given
- voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- voucher.save(update_fields=["metadata"])
- voucher_id = graphene.Node.to_global_id("Voucher", voucher.pk)
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_discounts, voucher_id, "Voucher"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], voucher, voucher_id
- )
-
-
-def test_add_private_metadata_for_voucher(
- staff_api_client, permission_manage_discounts, voucher
-):
- # given
- voucher_id = graphene.Node.to_global_id("Voucher", voucher.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_discounts, voucher_id, "Voucher"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], voucher, voucher_id
- )
-
-
-def test_delete_private_metadata_for_voucher(
- staff_api_client, permission_manage_discounts, voucher
-):
- # given
- voucher.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- voucher.save(update_fields=["private_metadata"])
- voucher_id = graphene.Node.to_global_id("Voucher", voucher.pk)
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_discounts, voucher_id, "Voucher"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], voucher, voucher_id
- )
-
-
-def test_add_public_metadata_for_transaction_item(
- staff_api_client,
- permission_manage_payments,
-):
- # given
- transaction_item = TransactionItem.objects.create()
- transaction_id = graphene.Node.to_global_id(
- "TransactionItem", transaction_item.token
- )
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_payments, transaction_id, "TransactionItem"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], transaction_item, transaction_id
- )
-
-
-def test_delete_public_metadata_for_transaction_item(
- staff_api_client, permission_manage_payments
-):
- # given
- transaction_item = TransactionItem.objects.create(
- metadata={PUBLIC_KEY: PUBLIC_VALUE}
- )
- transaction_id = graphene.Node.to_global_id(
- "TransactionItem", transaction_item.token
- )
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_payments, transaction_id, "TransactionItem"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], transaction_item, transaction_id
- )
-
-
-def test_add_private_metadata_for_transaction_item(
- staff_api_client, permission_manage_payments
-):
- # given
- transaction_item = TransactionItem.objects.create(
- private_metadata={PRIVATE_KEY: PRIVATE_VALUE}
- )
- transaction_id = graphene.Node.to_global_id(
- "TransactionItem", transaction_item.token
- )
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_payments, transaction_id, "TransactionItem"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"],
- transaction_item,
- transaction_id,
- )
-
-
-def test_delete_private_metadata_for_transaction_item(
- staff_api_client, permission_manage_payments, voucher
-):
- # given
- transaction_item = TransactionItem.objects.create(
- private_metadata={PRIVATE_KEY: PRIVATE_VALUE}
- )
- transaction_id = graphene.Node.to_global_id(
- "TransactionItem", transaction_item.token
- )
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_payments, transaction_id, "TransactionItem"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"],
- transaction_item,
- transaction_id,
- )
-
-
-def test_add_public_metadata_for_product_media(
- staff_api_client, permission_manage_products, product_with_image
-):
- # given
- media = product_with_image.media.first()
- media_id = graphene.Node.to_global_id("ProductMedia", media.pk)
-
- # when
- response = execute_update_public_metadata_for_item(
- staff_api_client, permission_manage_products, media_id, "ProductMedia"
- )
-
- # then
- assert item_contains_proper_public_metadata(
- response["data"]["updateMetadata"]["item"], media, media_id
- )
-
-
-def test_delete_public_metadata_for_product_media(
- staff_api_client, permission_manage_products, product_with_image
-):
- # given
- media = product_with_image.media.first()
- media_id = graphene.Node.to_global_id("ProductMedia", media.pk)
- media.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- media.save(update_fields=["metadata"])
-
- # when
- response = execute_clear_public_metadata_for_item(
- staff_api_client, permission_manage_products, media_id, "ProductMedia"
- )
-
- # then
- assert item_without_public_metadata(
- response["data"]["deleteMetadata"]["item"], media, media_id
- )
-
-
-def test_add_private_metadata_for_product_media(
- staff_api_client, permission_manage_products, product_with_image
-):
- # given
- media = product_with_image.media.first()
- media_id = graphene.Node.to_global_id("ProductMedia", media.pk)
-
- # when
- response = execute_update_private_metadata_for_item(
- staff_api_client, permission_manage_products, media_id, "ProductMedia"
- )
-
- # then
- assert item_contains_proper_private_metadata(
- response["data"]["updatePrivateMetadata"]["item"], media, media_id
- )
-
-
-def test_delete_private_metadata_for_product_media(
- staff_api_client, permission_manage_products, product_with_image
-):
- # given
- media = product_with_image.media.first()
- media_id = graphene.Node.to_global_id("ProductMedia", media.pk)
- media.store_value_in_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- media.save(update_fields=["private_metadata"])
-
- # when
- response = execute_clear_private_metadata_for_item(
- staff_api_client, permission_manage_products, media_id, "ProductMedia"
- )
-
- # then
- assert item_without_private_metadata(
- response["data"]["deletePrivateMetadata"]["item"], media, media_id
- )
diff --git a/saleor/graphql/meta/tests/test_meta_queries.py b/saleor/graphql/meta/tests/test_meta_queries.py
deleted file mode 100644
index 755943f3b9..0000000000
--- a/saleor/graphql/meta/tests/test_meta_queries.py
+++ /dev/null
@@ -1,4139 +0,0 @@
-from typing import List
-
-import graphene
-from django.http import HttpResponse
-
-from ....core.models import ModelWithMetadata
-from ....order.models import Order
-from ....payment.models import Payment
-from ....payment.utils import payment_owned_by_user
-from ....permission.models import Permission
-from ...tests.fixtures import ApiClient
-from ...tests.utils import assert_no_permission, get_graphql_content
-
-PRIVATE_KEY = "private_key"
-PRIVATE_VALUE = "private_vale"
-
-PUBLIC_KEY = "key"
-PUBLIC_VALUE = "value"
-
-
-def execute_query(
- query_str: str,
- client: ApiClient,
- model: ModelWithMetadata,
- model_name: str,
- permissions: List[Permission] = None,
-):
- return client.post_graphql(
- query_str,
- variables={"id": graphene.Node.to_global_id(model_name, model.pk)},
- permissions=[] if permissions is None else permissions,
- check_no_permissions=False,
- )
-
-
-def assert_model_contains_metadata(response: HttpResponse, model_name: str):
- content = get_graphql_content(response)
- metadata = content["data"][model_name]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def assert_model_contains_private_metadata(response: HttpResponse, model_name: str):
- content = get_graphql_content(response)
- metadata = content["data"][model_name]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_SELF_PUBLIC_META = """
- {
- me{
- metadata{
- key
- value
- }
- metafields(keys: ["INVALID", "key"])
- keyFieldValue: metafield(key: "key")
- }
- }
-"""
-
-
-def test_query_public_meta_for_me_as_customer(user_api_client):
- # given
- me = user_api_client.user
- me.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- me.save(update_fields=["metadata"])
-
- # when
- response = user_api_client.post_graphql(QUERY_SELF_PUBLIC_META)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["me"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
- metafields = content["data"]["me"]["metafields"]
- assert metafields[PUBLIC_KEY] == PUBLIC_VALUE
- field_value = content["data"]["me"]["keyFieldValue"]
- assert field_value == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_me_as_staff(staff_api_client):
- # given
- me = staff_api_client.user
- me.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- me.save(update_fields=["metadata"])
-
- # when
- response = staff_api_client.post_graphql(QUERY_SELF_PUBLIC_META)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["me"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
- metafields = content["data"]["me"]["metafields"]
- assert metafields[PUBLIC_KEY] == PUBLIC_VALUE
- field_value = content["data"]["me"]["keyFieldValue"]
- assert field_value == PUBLIC_VALUE
-
-
-QUERY_USER_PUBLIC_META = """
- query userMeta($id: ID!){
- user(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_customer_as_staff(
- staff_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- customer_user.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_USER_PUBLIC_META, variables, [permission_manage_users]
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["user"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- customer_user.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_USER_PUBLIC_META, variables, [permission_manage_users]
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["user"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_staff_as_other_staff(
- staff_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- admin_user.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("User", admin_user.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_USER_PUBLIC_META, variables, [permission_manage_staff]
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["user"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_CHECKOUT_PUBLIC_META = """
- query checkoutMeta($token: UUID!){
- checkout(token: $token){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_checkout_as_anonymous_user(api_client, checkout):
- # given
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.metadata_storage.save(update_fields=["metadata"])
- variables = {"token": checkout.pk}
-
- # when
- response = api_client.post_graphql(QUERY_CHECKOUT_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["checkout"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_other_customer_checkout_as_anonymous_user(
- api_client, checkout, customer_user
-):
- # given
- checkout.user = customer_user
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.save(update_fields=["user"])
- checkout.metadata_storage.save(update_fields=["metadata"])
- variables = {"token": checkout.pk}
-
- # when
- response = api_client.post_graphql(QUERY_CHECKOUT_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- assert not content["data"]["checkout"]
-
-
-def test_query_public_meta_for_checkout_as_customer(user_api_client, checkout):
- # given
- checkout.user = user_api_client.user
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.save(update_fields=["user"])
- checkout.metadata_storage.save(update_fields=["metadata"])
- variables = {"token": checkout.pk}
-
- # when
- response = user_api_client.post_graphql(QUERY_CHECKOUT_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["checkout"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_checkout_as_staff(
- staff_api_client, checkout, customer_user, permission_manage_checkouts
-):
- # given
- checkout.user = customer_user
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.save(update_fields=["user"])
- checkout.metadata_storage.save(update_fields=["metadata"])
- variables = {"token": checkout.pk}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_CHECKOUT_PUBLIC_META,
- variables,
- [permission_manage_checkouts],
- check_no_permissions=False, # Remove after fix #5245
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["checkout"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_checkout_as_app(
- app_api_client, checkout, customer_user, permission_manage_checkouts
-):
- # given
- checkout.user = customer_user
- checkout.metadata_storage.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- checkout.save(update_fields=["user"])
- checkout.metadata_storage.save(update_fields=["metadata"])
- variables = {"token": checkout.pk}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_CHECKOUT_PUBLIC_META,
- variables,
- [permission_manage_checkouts],
- check_no_permissions=False, # Remove after fix #5245
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["checkout"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_ORDER_BY_TOKEN_PUBLIC_META = """
- query orderMeta($token: UUID!){
- orderByToken(token: $token){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_order_by_token_as_anonymous_user(api_client, order):
- # given
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["metadata"])
- variables = {"token": order.id}
-
- # when
- response = api_client.post_graphql(QUERY_ORDER_BY_TOKEN_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_order_by_token_as_customer(user_api_client, order):
- # given
- order.user = user_api_client.user
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["user", "metadata"])
- variables = {"token": order.id}
-
- # when
- response = user_api_client.post_graphql(QUERY_ORDER_BY_TOKEN_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_order_by_token_as_staff(
- staff_api_client, order, customer_user, permission_manage_orders
-):
- # given
- order.user = customer_user
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["user", "metadata"])
- variables = {"token": order.id}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_ORDER_BY_TOKEN_PUBLIC_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_order_by_token_as_app(
- app_api_client, order, customer_user, permission_manage_orders
-):
- # given
- order.user = customer_user
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["user", "metadata"])
- variables = {"token": order.id}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_ORDER_BY_TOKEN_PUBLIC_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_ORDER_PUBLIC_META = """
- query orderMeta($id: ID!){
- order(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_order_as_anonymous_user(api_client, order):
- # given
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["user", "metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_ORDER_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_order_as_customer(user_api_client, order):
- # given
- order.user = user_api_client.user
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["user", "metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_ORDER_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_order_as_staff(
- staff_api_client, order, customer_user, permission_manage_orders
-):
- # given
- order.user = customer_user
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["user", "metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_ORDER_PUBLIC_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_order_as_app(
- app_api_client, order, customer_user, permission_manage_orders
-):
- # given
- order.user = customer_user
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["user", "metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_ORDER_PUBLIC_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_DRAFT_ORDER_PUBLIC_META = """
- query draftOrderMeta($id: ID!){
- order(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_draft_order_as_anonymous_user(api_client, draft_order):
- # given
- draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- draft_order.save(update_fields=["user", "metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_DRAFT_ORDER_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_draft_order_as_customer(user_api_client, draft_order):
- # given
- draft_order.user = user_api_client.user
- draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- draft_order.save(update_fields=["user", "metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_DRAFT_ORDER_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_draft_order_as_staff(
- staff_api_client, draft_order, customer_user, permission_manage_orders
-):
- # given
- draft_order.user = customer_user
- draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- draft_order.save(update_fields=["user", "metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_DRAFT_ORDER_PUBLIC_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_draft_order_as_app(
- app_api_client, draft_order, customer_user, permission_manage_orders
-):
- # given
- draft_order.user = customer_user
- draft_order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- draft_order.save(update_fields=["user", "metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_ORDER_PUBLIC_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_FULFILLMENT_PUBLIC_META = """
- query fulfillmentMeta($token: UUID!){
- orderByToken(token: $token){
- fulfillments{
- metadata{
- key
- value
- }
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_fulfillment_as_anonymous_user(
- api_client, fulfilled_order
-):
- # given
- fulfillment = fulfilled_order.fulfillments.first()
- fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- fulfillment.save(update_fields=["metadata"])
- variables = {"token": fulfilled_order.id}
-
- # when
- response = api_client.post_graphql(QUERY_FULFILLMENT_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["fulfillments"][0]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_fulfillment_as_customer(
- user_api_client, fulfilled_order
-):
- # given
- fulfillment = fulfilled_order.fulfillments.first()
- fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- fulfillment.save(update_fields=["metadata"])
- fulfilled_order.user = user_api_client.user
- fulfilled_order.save(update_fields=["user"])
- variables = {"token": fulfilled_order.id}
-
- # when
- response = user_api_client.post_graphql(QUERY_FULFILLMENT_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["fulfillments"][0]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_fulfillment_as_staff(
- staff_api_client, fulfilled_order, customer_user, permission_manage_orders
-):
- # given
- fulfillment = fulfilled_order.fulfillments.first()
- fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- fulfillment.save(update_fields=["metadata"])
- fulfilled_order.user = customer_user
- fulfilled_order.save(update_fields=["user"])
- variables = {"token": fulfilled_order.id}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_FULFILLMENT_PUBLIC_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["fulfillments"][0]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_fulfillment_as_app(
- app_api_client, fulfilled_order, customer_user, permission_manage_orders
-):
- # given
- fulfillment = fulfilled_order.fulfillments.first()
- fulfillment.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- fulfillment.save(update_fields=["metadata"])
- fulfilled_order.user = customer_user
- fulfilled_order.save(update_fields=["user"])
- variables = {"token": fulfilled_order.id}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_FULFILLMENT_PUBLIC_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["fulfillments"][0]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_ATTRIBUTE_PUBLIC_META = """
- query attributeMeta($id: ID!){
- attribute(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_attribute_as_anonymous_user(api_client, color_attribute):
- # given
- color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- color_attribute.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_ATTRIBUTE_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["attribute"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_attribute_as_customer(user_api_client, color_attribute):
- # given
- color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- color_attribute.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_ATTRIBUTE_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["attribute"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_attribute_as_staff(
- staff_api_client, color_attribute, permission_manage_products
-):
- # given
- color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- color_attribute.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_ATTRIBUTE_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["attribute"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_attribute_as_app(
- app_api_client, color_attribute, permission_manage_products
-):
- # given
- color_attribute.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- color_attribute.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_ATTRIBUTE_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["attribute"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_CATEGORY_PUBLIC_META = """
- query categoryMeta($id: ID!){
- category(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_category_as_anonymous_user(api_client, category):
- # given
- category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- category.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_CATEGORY_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["category"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_category_as_customer(user_api_client, category):
- # given
- category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- category.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_CATEGORY_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["category"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_category_as_staff(
- staff_api_client, category, permission_manage_products
-):
- # given
- category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- category.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_CATEGORY_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["category"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_category_as_app(
- app_api_client, category, permission_manage_products
-):
- # given
- category.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- category.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_CATEGORY_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["category"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_COLLECTION_PUBLIC_META = """
- query collectionMeta($id: ID!, $channel: String) {
- collection(id: $id, channel: $channel) {
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_collection_as_anonymous_user(
- api_client, published_collection, channel_USD
-):
- # given
- collection = published_collection
- collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- collection.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Collection", collection.pk),
- "channel": channel_USD.slug,
- }
- # when
- response = api_client.post_graphql(QUERY_COLLECTION_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["collection"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_collection_as_customer(
- user_api_client, published_collection, channel_USD
-):
- # given
- collection = published_collection
- collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- collection.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Collection", collection.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_COLLECTION_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["collection"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_collection_as_staff(
- staff_api_client, published_collection, permission_manage_products, channel_USD
-):
- # given
- collection = published_collection
- collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- collection.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Collection", collection.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_COLLECTION_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["collection"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_collection_as_app(
- app_api_client, published_collection, permission_manage_products, channel_USD
-):
- # given
- collection = published_collection
- collection.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- collection.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Collection", collection.pk),
- "channel": channel_USD.slug,
- }
- # when
- response = app_api_client.post_graphql(
- QUERY_COLLECTION_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["collection"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_DIGITAL_CONTENT_PUBLIC_META = """
- query digitalContentMeta($id: ID!){
- digitalContent(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_digital_content_as_anonymous_user(
- api_client, digital_content
-):
- # given
- variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_DIGITAL_CONTENT_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_digital_content_as_customer(
- user_api_client, digital_content
-):
- # given
- digital_content.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- digital_content.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
-
- # when
- response = user_api_client.post_graphql(
- QUERY_DIGITAL_CONTENT_PUBLIC_META, variables
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_digital_content_as_staff(
- staff_api_client, digital_content, permission_manage_products
-):
- # given
- digital_content.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- digital_content.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_DIGITAL_CONTENT_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["digitalContent"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_digital_content_as_app(
- app_api_client, digital_content, permission_manage_products
-):
- # given
- digital_content.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- digital_content.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_DIGITAL_CONTENT_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["digitalContent"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_TRANSACTION_ITEM_PUBLIC_META = """
-query transactionItemMeta($id: ID!){
- order(id: $id){
- transactions{
- metadata{
- key
- value
- }
- }
- }
-}
-"""
-
-
-def execute_query_public_metadata_for_transaction_item(
- client: ApiClient, order: Order, permissions: List[Permission] = None
-):
- return execute_query(
- QUERY_TRANSACTION_ITEM_PUBLIC_META, client, order, "Order", permissions
- )
-
-
-def assert_transaction_item_contains_metadata(response):
- content = get_graphql_content(response)
- metadata = content["data"]["order"]["transactions"][0]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_transaction_item_as_customer(
- user_api_client, order, permission_manage_orders
-):
- # given
- order.payment_transactions.create(metadata={PUBLIC_KEY: PUBLIC_VALUE})
- order.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- order.save(update_fields=["metadata"])
-
- # when
- response = execute_query_public_metadata_for_transaction_item(
- user_api_client,
- order,
- permissions=[],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_transaction_item_as_staff_with_permission(
- staff_api_client,
- order_with_lines,
- permission_manage_orders,
- permission_manage_payments,
-):
- # given
- order_with_lines.payment_transactions.create(metadata={PUBLIC_KEY: PUBLIC_VALUE})
-
- # when
- response = execute_query_public_metadata_for_transaction_item(
- staff_api_client,
- order_with_lines,
- permissions=[permission_manage_orders, permission_manage_payments],
- )
-
- # then
- assert_transaction_item_contains_metadata(response)
-
-
-def test_query_public_meta_for_transaction_item_as_staff_without_permission(
- staff_api_client, order
-):
- # given
- order.payment_transactions.create(metadata={PUBLIC_KEY: PUBLIC_VALUE})
-
- # when
- response = execute_query_public_metadata_for_transaction_item(
- staff_api_client, order
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_transaction_item_as_app_with_permission(
- app_api_client,
- order,
- permission_manage_orders,
- permission_manage_payments,
-):
- order.payment_transactions.create(metadata={PUBLIC_KEY: PUBLIC_VALUE})
-
- # when
- response = execute_query_public_metadata_for_transaction_item(
- app_api_client,
- order,
- permissions=[permission_manage_payments, permission_manage_orders],
- )
-
- # then
- assert_transaction_item_contains_metadata(response)
-
-
-def test_query_public_meta_for_transaction_item_as_app_without_permission(
- app_api_client, order
-):
- # when
- response = execute_query_public_metadata_for_transaction_item(app_api_client, order)
-
- # then
- assert_no_permission(response)
-
-
-QUERY_PAYMENT_PUBLIC_META = """
- query paymentsMeta($id: ID!){
- payment(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def execute_query_public_metadata_for_payment(
- client: ApiClient, payment: Payment, permissions: List[Permission] = None
-):
- return execute_query(
- QUERY_PAYMENT_PUBLIC_META, client, payment, "Payment", permissions
- )
-
-
-def assert_payment_contains_metadata(response):
- assert_model_contains_metadata(response, "payment")
-
-
-def test_query_public_meta_for_payment_as_customer(
- user_api_client, payment_with_public_metadata, permission_manage_orders
-):
- # given
- assert payment_owned_by_user(payment_with_public_metadata.pk, user_api_client.user)
-
- # when
- response = execute_query_public_metadata_for_payment(
- user_api_client,
- payment_with_public_metadata,
- permissions=[permission_manage_orders],
- )
-
- # then
- assert_payment_contains_metadata(response)
-
-
-def test_query_public_meta_for_payment_as_another_customer(
- user2_api_client,
- payment_with_public_metadata,
- permission_manage_orders,
-):
- # given
- assert not payment_owned_by_user(
- payment_with_public_metadata.pk, user2_api_client.user
- )
- # when
- response = execute_query_public_metadata_for_payment(
- user2_api_client,
- payment_with_public_metadata,
- permissions=[permission_manage_orders],
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_payment_as_staff_with_permission(
- staff_api_client,
- payment_with_public_metadata,
- permission_manage_orders,
- permission_manage_payments,
-):
- # when
- response = execute_query_public_metadata_for_payment(
- staff_api_client,
- payment_with_public_metadata,
- permissions=[permission_manage_orders, permission_manage_payments],
- )
-
- # then
- assert_payment_contains_metadata(response)
-
-
-def test_query_public_meta_for_payment_as_staff_without_permission(
- staff_api_client, payment_with_public_metadata
-):
- # when
- response = execute_query_public_metadata_for_payment(
- staff_api_client, payment_with_public_metadata
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_payment_as_app_with_permission(
- app_api_client,
- payment_with_public_metadata,
- permission_manage_orders,
- permission_manage_payments,
-):
- # when
- response = execute_query_public_metadata_for_payment(
- app_api_client,
- payment_with_public_metadata,
- permissions=[permission_manage_orders, permission_manage_payments],
- )
-
- # then
- assert_payment_contains_metadata(response)
-
-
-def test_query_public_meta_for_payment_as_app_without_permission(
- app_api_client, payment_with_public_metadata
-):
- # when
- response = execute_query_public_metadata_for_payment(
- app_api_client, payment_with_public_metadata
- )
-
- # then
- assert_no_permission(response)
-
-
-QUERY_PRODUCT_PUBLIC_META = """
- query productsMeta($id: ID!, $channel: String){
- product(id: $id, channel: $channel){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_product_as_anonymous_user(
- api_client, product, channel_USD
-):
- # given
- product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Product", product.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = api_client.post_graphql(QUERY_PRODUCT_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["product"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_product_as_customer(
- user_api_client, product, channel_USD
-):
- # given
- product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Product", product.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_PRODUCT_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["product"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_product_as_staff(
- staff_api_client, product, permission_manage_products
-):
- # given
- product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_PRODUCT_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["product"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_product_as_app(
- app_api_client, product, permission_manage_products
-):
- # given
- product.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_PRODUCT_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["product"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_PRODUCT_TYPE_PUBLIC_META = """
- query productTypeMeta($id: ID!){
- productType(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_product_type_as_anonymous_user(api_client, product_type):
- # given
- product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product_type.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_PRODUCT_TYPE_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productType"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_product_type_as_customer(user_api_client, product_type):
- # given
- product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product_type.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_PRODUCT_TYPE_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productType"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_product_type_as_staff(
- staff_api_client, product_type, permission_manage_products
-):
- # given
- product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product_type.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_PRODUCT_TYPE_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productType"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_product_type_as_app(
- app_api_client, product_type, permission_manage_products
-):
- # given
- product_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- product_type.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_PRODUCT_TYPE_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productType"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_PRODUCT_VARIANT_PUBLIC_META = """
- query productVariantMeta($id: ID!, $channel: String){
- productVariant(id: $id, channel: $channel){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_product_variant_as_anonymous_user(
- api_client, variant, channel_USD
-):
- # given
- variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- variant.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = api_client.post_graphql(QUERY_PRODUCT_VARIANT_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productVariant"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_product_variant_as_customer(
- user_api_client, variant, channel_USD
-):
- # given
- variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- variant.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = user_api_client.post_graphql(
- QUERY_PRODUCT_VARIANT_PUBLIC_META, variables
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productVariant"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_product_variant_as_staff(
- staff_api_client, variant, permission_manage_products
-):
- # given
- variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- variant.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_PRODUCT_VARIANT_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productVariant"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_product_variant_as_app(
- app_api_client, variant, permission_manage_products
-):
- # given
- variant.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- variant.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
- }
-
- # when
- response = app_api_client.post_graphql(
- QUERY_PRODUCT_VARIANT_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productVariant"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_APP_PUBLIC_META = """
- query appMeta($id: ID!){
- app(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_app_as_anonymous_user(api_client, app):
- # given
- variables = {"id": graphene.Node.to_global_id("App", app.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_APP_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_app_as_customer(user_api_client, app):
- # given
- variables = {"id": graphene.Node.to_global_id("App", app.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_APP_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_app_as_staff(
- staff_api_client, app, permission_manage_apps
-):
- # given
- app.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- app.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("App", app.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_APP_PUBLIC_META,
- variables,
- [permission_manage_apps],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["app"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_app_as_app(app_api_client, app, permission_manage_apps):
- # given
- app.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- app.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("App", app.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_APP_PUBLIC_META,
- variables,
- [permission_manage_apps],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["app"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_PAGE_TYPE_PUBLIC_META = """
- query pageTypeMeta($id: ID!){
- pageType(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_page_type_as_anonymous_user(api_client, page_type):
- # given
- page_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- page_type.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_PAGE_TYPE_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["pageType"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_page_type_as_customer(user_api_client, page_type):
- # given
- page_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- page_type.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_PAGE_TYPE_PUBLIC_META, variables)
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["pageType"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_page_type_as_staff(
- staff_api_client, page_type, permission_manage_products
-):
- # given
- page_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- page_type.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_PAGE_TYPE_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["pageType"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_page_type_as_app(
- app_api_client, page_type, permission_manage_products
-):
- # given
- page_type.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- page_type.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_PAGE_TYPE_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["pageType"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_GIFT_CARD_PUBLIC_META = """
- query giftCardMeta($id: ID!){
- giftCard(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_gift_card_as_anonymous_user(api_client, gift_card):
- # given
- gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- gift_card.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_GIFT_CARD_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_gift_card_as_customer(user_api_client, gift_card):
- # given
- gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- gift_card.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_GIFT_CARD_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_gift_card_as_staff(
- staff_api_client, gift_card, permission_manage_gift_card
-):
- # given
- gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- gift_card.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_GIFT_CARD_PUBLIC_META,
- variables,
- [permission_manage_gift_card],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["giftCard"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_gift_card_as_app(
- app_api_client, gift_card, permission_manage_gift_card
-):
- # given
- gift_card.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- gift_card.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_GIFT_CARD_PUBLIC_META,
- variables,
- [permission_manage_gift_card],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["giftCard"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_SELF_PRIVATE_META = """
- {
- me{
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_me_as_customer(user_api_client):
- # given
-
- # when
- response = user_api_client.post_graphql(QUERY_SELF_PRIVATE_META)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_me_as_staff_with_manage_customer(
- staff_api_client, permission_manage_users
-):
- # given
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_SELF_PRIVATE_META, None, [permission_manage_users]
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_me_as_staff_with_manage_staff(
- staff_api_client, permission_manage_staff
-):
- # given
- me = staff_api_client.user
- me.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- me.save(update_fields=["private_metadata"])
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_SELF_PRIVATE_META, None, [permission_manage_staff]
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["me"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_USER_PRIVATE_META = """
- query userMeta($id: ID!){
- user(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_customer_as_staff(
- staff_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- customer_user.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_USER_PRIVATE_META, variables, [permission_manage_users]
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["user"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_customer_as_app(
- app_api_client, permission_manage_users, customer_user
-):
- # given
- customer_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- customer_user.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_USER_PRIVATE_META, variables, [permission_manage_users]
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["user"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_staff_as_other_staff(
- staff_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- admin_user.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("User", admin_user.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_USER_PRIVATE_META, variables, [permission_manage_staff]
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["user"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_CHECKOUT_PRIVATE_META = """
- query checkoutMeta($token: UUID!){
- checkout(token: $token){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_checkout_as_anonymous_user(api_client, checkout):
- # given
- variables = {"token": checkout.pk}
-
- # when
- response = api_client.post_graphql(QUERY_CHECKOUT_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_other_customer_checkout_as_anonymous_user(
- api_client, checkout, customer_user
-):
- # given
- checkout.user = customer_user
- checkout.save(update_fields=["user"])
- variables = {"token": checkout.pk}
-
- # when
- response = api_client.post_graphql(QUERY_CHECKOUT_PRIVATE_META, variables)
- content = get_graphql_content(response)
-
- # then
- assert not content["data"]["checkout"]
-
-
-def test_query_private_meta_for_checkout_as_customer(user_api_client, checkout):
- # given
- checkout.user = user_api_client.user
- checkout.save(update_fields=["user"])
- variables = {"token": checkout.pk}
-
- # when
- response = user_api_client.post_graphql(QUERY_CHECKOUT_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_checkout_as_staff(
- staff_api_client, checkout, customer_user, permission_manage_checkouts
-):
- # given
- checkout.user = customer_user
- checkout.metadata_storage.store_value_in_private_metadata(
- {PRIVATE_KEY: PRIVATE_VALUE}
- )
- checkout.save(update_fields=["user"])
- checkout.metadata_storage.save(update_fields=["private_metadata"])
- variables = {"token": checkout.pk}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_CHECKOUT_PRIVATE_META,
- variables,
- [permission_manage_checkouts],
- check_no_permissions=False, # Remove after fix #5245
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["checkout"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_checkout_as_app(
- app_api_client, checkout, customer_user, permission_manage_checkouts
-):
- # given
- checkout.user = customer_user
- checkout.metadata_storage.store_value_in_private_metadata(
- {PRIVATE_KEY: PRIVATE_VALUE}
- )
- checkout.save(update_fields=["user"])
- checkout.metadata_storage.save(update_fields=["private_metadata"])
- variables = {"token": checkout.pk}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_CHECKOUT_PRIVATE_META,
- variables,
- [permission_manage_checkouts],
- check_no_permissions=False, # Remove after fix #5245
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["checkout"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_ORDER_BY_TOKEN_PRIVATE_META = """
- query orderMeta($token: UUID!){
- orderByToken(token: $token){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_order_by_token_as_anonymous_user(api_client, order):
- # given
- variables = {"token": order.id}
-
- # when
- response = api_client.post_graphql(QUERY_ORDER_BY_TOKEN_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_order_by_token_as_customer(user_api_client, order):
- # given
- order.user = user_api_client.user
- order.save(update_fields=["user"])
- variables = {"token": order.id}
-
- # when
- response = user_api_client.post_graphql(
- QUERY_ORDER_BY_TOKEN_PRIVATE_META, variables
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_order_by_token_as_staff(
- staff_api_client, order, customer_user, permission_manage_orders
-):
- # given
- order.user = customer_user
- order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- order.save(update_fields=["user", "private_metadata"])
- variables = {"token": order.id}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_ORDER_BY_TOKEN_PRIVATE_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_order_by_token_as_app(
- app_api_client, order, customer_user, permission_manage_orders
-):
- # given
- order.user = customer_user
- order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- order.save(update_fields=["user", "private_metadata"])
- variables = {"token": order.id}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_ORDER_BY_TOKEN_PRIVATE_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_ORDER_PRIVATE_META = """
- query orderMeta($id: ID!){
- order(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_order_as_anonymous_user(api_client, order):
- # given
- variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_ORDER_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_order_as_customer(user_api_client, order):
- # given
- order.user = user_api_client.user
- order.save(update_fields=["user"])
- variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_ORDER_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_order_as_staff(
- staff_api_client, order, customer_user, permission_manage_orders
-):
- # given
- order.user = customer_user
- order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- order.save(update_fields=["user", "private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_ORDER_PRIVATE_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_order_as_app(
- app_api_client, order, customer_user, permission_manage_orders
-):
- # given
- order.user = customer_user
- order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- order.save(update_fields=["user", "private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_ORDER_PRIVATE_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_DRAFT_ORDER_PRIVATE_META = """
- query draftOrderMeta($id: ID!){
- order(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_draft_order_as_anonymous_user(api_client, draft_order):
- # given
- variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_DRAFT_ORDER_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_draft_order_as_customer(user_api_client, draft_order):
- # given
- draft_order.user = user_api_client.user
- draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- draft_order.save(update_fields=["user", "private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_DRAFT_ORDER_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_draft_order_as_staff(
- staff_api_client, draft_order, customer_user, permission_manage_orders
-):
- # given
- draft_order.user = customer_user
- draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- draft_order.save(update_fields=["user", "private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_DRAFT_ORDER_PRIVATE_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_draft_order_as_app(
- app_api_client, draft_order, customer_user, permission_manage_orders
-):
- # given
- draft_order.user = customer_user
- draft_order.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- draft_order.save(update_fields=["user", "private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_ORDER_PRIVATE_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["order"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_FULFILLMENT_PRIVATE_META = """
- query fulfillmentMeta($token: UUID!){
- orderByToken(token: $token){
- fulfillments{
- privateMetadata{
- key
- value
- }
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_fulfillment_as_anonymous_user(
- api_client, fulfilled_order
-):
- # given
- variables = {"token": fulfilled_order.id}
-
- # when
- response = api_client.post_graphql(QUERY_FULFILLMENT_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_fulfillment_as_customer(
- user_api_client, fulfilled_order
-):
- # given
- fulfilled_order.user = user_api_client.user
- fulfilled_order.save(update_fields=["user"])
- variables = {"token": fulfilled_order.id}
-
- # when
- response = user_api_client.post_graphql(QUERY_FULFILLMENT_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_fulfillment_as_staff(
- staff_api_client, fulfilled_order, customer_user, permission_manage_orders
-):
- # given
- fulfillment = fulfilled_order.fulfillments.first()
- fulfillment.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- fulfillment.save(update_fields=["private_metadata"])
- fulfilled_order.user = customer_user
- fulfilled_order.save(update_fields=["user"])
- variables = {"token": fulfilled_order.id}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_FULFILLMENT_PRIVATE_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["fulfillments"][0]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_fulfillment_as_app(
- app_api_client, fulfilled_order, customer_user, permission_manage_orders
-):
- # given
- fulfillment = fulfilled_order.fulfillments.first()
- fulfillment.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- fulfillment.save(update_fields=["private_metadata"])
- fulfilled_order.user = customer_user
- fulfilled_order.save(update_fields=["user"])
- variables = {"token": fulfilled_order.id}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_FULFILLMENT_PRIVATE_META,
- variables,
- [permission_manage_orders],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["orderByToken"]["fulfillments"][0]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_ATTRIBUTE_PRIVATE_META = """
- query attributeMeta($id: ID!){
- attribute(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_attribute_as_anonymous_user(
- api_client, color_attribute
-):
- # given
- variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_ATTRIBUTE_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_attribute_as_customer(user_api_client, color_attribute):
- # given
- variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_ATTRIBUTE_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_attribute_as_staff(
- staff_api_client, color_attribute, permission_manage_product_types_and_attributes
-):
- # given
- color_attribute.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- color_attribute.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_ATTRIBUTE_PRIVATE_META,
- variables,
- [permission_manage_product_types_and_attributes],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["attribute"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_attribute_as_app(
- app_api_client, color_attribute, permission_manage_product_types_and_attributes
-):
- # given
- color_attribute.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- color_attribute.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_ATTRIBUTE_PRIVATE_META,
- variables,
- [permission_manage_product_types_and_attributes],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["attribute"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_CATEGORY_PRIVATE_META = """
- query categoryMeta($id: ID!){
- category(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_category_as_anonymous_user(api_client, category):
- # given
- variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_CATEGORY_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_category_as_customer(user_api_client, category):
- # given
- variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_CATEGORY_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_category_as_staff(
- staff_api_client, category, permission_manage_products
-):
- # given
- category.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- category.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_CATEGORY_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["category"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_category_as_app(
- app_api_client, category, permission_manage_products
-):
- # given
- category.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- category.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_CATEGORY_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["category"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_COLLECTION_PRIVATE_META = """
- query collectionMeta($id: ID!, $channel: String){
- collection(id: $id, channel: $channel){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_collection_as_anonymous_user(
- api_client, published_collection, channel_USD
-):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Collection", published_collection.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = api_client.post_graphql(QUERY_COLLECTION_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_collection_as_customer(
- user_api_client, published_collection, channel_USD
-):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Collection", published_collection.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_COLLECTION_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_collection_as_staff(
- staff_api_client, published_collection, permission_manage_products, channel_USD
-):
- # given
- collection = published_collection
- collection.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- collection.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Collection", published_collection.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_COLLECTION_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["collection"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_collection_as_app(
- app_api_client, published_collection, permission_manage_products, channel_USD
-):
- # given
- collection = published_collection
- collection.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- collection.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Collection", collection.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = app_api_client.post_graphql(
- QUERY_COLLECTION_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["collection"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_DIGITAL_CONTENT_PRIVATE_META = """
- query digitalContentMeta($id: ID!){
- digitalContent(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_digital_content_as_anonymous_user(
- api_client, digital_content
-):
- # given
- variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_DIGITAL_CONTENT_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_digital_content_as_customer(
- user_api_client, digital_content
-):
- # given
- digital_content.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- digital_content.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
-
- # when
- response = user_api_client.post_graphql(
- QUERY_DIGITAL_CONTENT_PRIVATE_META, variables
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_digital_content_as_staff(
- staff_api_client, digital_content, permission_manage_products
-):
- # given
- digital_content.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- digital_content.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_DIGITAL_CONTENT_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["digitalContent"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_digital_content_as_app(
- app_api_client, digital_content, permission_manage_products
-):
- # given
- digital_content.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- digital_content.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_DIGITAL_CONTENT_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["digitalContent"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_TRANSACTION_ITEM_PRIVATE_META = """
-query transactionItemMeta($id: ID!){
- order(id: $id){
- transactions{
- privateMetadata{
- key
- value
- }
- }
- }
-}
-"""
-
-
-def execute_query_private_metadata_for_transaction_item(
- client: ApiClient, order: Order, permissions: List[Permission] = None
-):
- return execute_query(
- QUERY_TRANSACTION_ITEM_PRIVATE_META, client, order, "Order", permissions
- )
-
-
-def assert_transaction_item_contains_private_metadata(response):
- content = get_graphql_content(response)
- metadata = content["data"]["order"]["transactions"][0]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_transaction_item_as_customer(
- user_api_client, order, permission_manage_orders
-):
- # given
- order.payment_transactions.create(private_metadata={PRIVATE_KEY: PRIVATE_VALUE})
-
- # when
- response = execute_query_public_metadata_for_transaction_item(
- user_api_client,
- order,
- permissions=[],
- )
-
- # then
- assert_no_permission(response)
-
-
-#
-
-
-def test_query_private_meta_for_transaction_item_as_staff_with_permission(
- staff_api_client,
- order_with_lines,
- permission_manage_orders,
- permission_manage_payments,
-):
- # given
- order_with_lines.payment_transactions.create(
- private_metadata={PRIVATE_KEY: PRIVATE_VALUE}
- )
-
- # when
- response = execute_query_private_metadata_for_transaction_item(
- staff_api_client,
- order_with_lines,
- permissions=[permission_manage_orders, permission_manage_payments],
- )
-
- # then
- assert_transaction_item_contains_private_metadata(response)
-
-
-def test_query_private_meta_for_transaction_item_as_staff_without_permission(
- staff_api_client, order
-):
- # given
- order.payment_transactions.create(private_metadata={PRIVATE_KEY: PRIVATE_VALUE})
-
- # when
- response = execute_query_private_metadata_for_transaction_item(
- staff_api_client, order
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_transaction_item_as_app_with_permission(
- app_api_client,
- order,
- permission_manage_orders,
- permission_manage_payments,
-):
- order.payment_transactions.create(private_metadata={PRIVATE_KEY: PRIVATE_VALUE})
-
- # when
- response = execute_query_private_metadata_for_transaction_item(
- app_api_client,
- order,
- permissions=[permission_manage_payments, permission_manage_orders],
- )
-
- # then
- assert_transaction_item_contains_private_metadata(response)
-
-
-def test_query_private_meta_for_transaction_item_as_app_without_permission(
- app_api_client, order
-):
- # when
- response = execute_query_private_metadata_for_transaction_item(
- app_api_client, order
- )
-
- # then
- assert_no_permission(response)
-
-
-QUERY_PAYMENT_PRIVATE_META = """
- query paymentsMeta($id: ID!){
- payment(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def execute_query_private_metadata_for_payment(
- client: ApiClient, payment: Payment, permissions: List[Permission] = None
-):
- return execute_query(
- QUERY_PAYMENT_PRIVATE_META, client, payment, "Payment", permissions
- )
-
-
-def assert_payment_contains_private_metadata(response):
- assert_model_contains_private_metadata(response, "payment")
-
-
-def test_query_private_meta_for_payment_as_staff_with_permission(
- staff_api_client,
- payment_with_private_metadata,
- permission_manage_orders,
- permission_manage_payments,
-):
- # when
- response = execute_query_private_metadata_for_payment(
- staff_api_client,
- payment_with_private_metadata,
- permissions=[permission_manage_orders, permission_manage_payments],
- )
-
- # then
- assert_payment_contains_private_metadata(response)
-
-
-def test_query_private_meta_for_payment_as_staff_without_permission(
- staff_api_client, payment_with_private_metadata
-):
- # when
- response = execute_query_private_metadata_for_payment(
- staff_api_client, payment_with_private_metadata
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_payment_as_app_with_permission(
- app_api_client,
- payment_with_private_metadata,
- permission_manage_orders,
- permission_manage_payments,
-):
- # when
- response = execute_query_private_metadata_for_payment(
- app_api_client,
- payment_with_private_metadata,
- permissions=[permission_manage_orders, permission_manage_payments],
- )
-
- # then
- assert_payment_contains_private_metadata(response)
-
-
-def test_query_private_meta_for_payment_as_app_without_permission(
- app_api_client, payment_with_private_metadata
-):
- # when
- response = execute_query_private_metadata_for_payment(
- app_api_client, payment_with_private_metadata
- )
-
- # then
- assert_no_permission(response)
-
-
-QUERY_PRODUCT_PRIVATE_META = """
- query productsMeta($id: ID!, $channel: String){
- product(id: $id, channel: $channel){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_product_as_anonymous_user(
- api_client, product, channel_USD
-):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Product", product.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = api_client.post_graphql(QUERY_PRODUCT_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_product_as_customer(
- user_api_client, product, channel_USD
-):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Product", product.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_PRODUCT_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_product_as_staff(
- staff_api_client, product, permission_manage_products
-):
- # given
- product.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- product.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_PRODUCT_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["product"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_product_as_app(
- app_api_client, product, permission_manage_products
-):
- # given
- product.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- product.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Product", product.pk),
- }
-
- # when
- response = app_api_client.post_graphql(
- QUERY_PRODUCT_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["product"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_PRODUCT_TYPE_PRIVATE_META = """
- query productTypeMeta($id: ID!){
- productType(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_product_type_as_anonymous_user(
- api_client, product_type
-):
- # given
- variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_PRODUCT_TYPE_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_product_type_as_customer(user_api_client, product_type):
- # given
- variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_PRODUCT_TYPE_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_product_type_as_staff(
- staff_api_client, product_type, permission_manage_product_types_and_attributes
-):
- # given
- product_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- product_type.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_PRODUCT_TYPE_PRIVATE_META,
- variables,
- [permission_manage_product_types_and_attributes],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productType"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_product_type_as_app(
- app_api_client, product_type, permission_manage_product_types_and_attributes
-):
- # given
- product_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- product_type.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_PRODUCT_TYPE_PRIVATE_META,
- variables,
- [permission_manage_product_types_and_attributes],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productType"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_PRODUCT_VARIANT_PRIVATE_META = """
- query productVariantMeta($id: ID!, $channel: String){
- productVariant(id: $id, channel: $channel){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_product_variant_as_anonymous_user(
- api_client, variant, channel_USD
-):
- # given
- variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- variant.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = api_client.post_graphql(QUERY_PRODUCT_VARIANT_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_product_variant_as_customer(
- user_api_client, variant, channel_USD
-):
- # given
- variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- variant.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = user_api_client.post_graphql(
- QUERY_PRODUCT_VARIANT_PRIVATE_META, variables
- )
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_product_variant_as_staff(
- staff_api_client, variant, permission_manage_products
-):
- # given
- variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- variant.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_PRODUCT_VARIANT_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productVariant"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_product_variant_as_app(
- app_api_client, variant, permission_manage_products
-):
- # given
- variant.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- variant.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("ProductVariant", variant.pk),
- }
-
- # when
- response = app_api_client.post_graphql(
- QUERY_PRODUCT_VARIANT_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["productVariant"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_APP_PRIVATE_META = """
- query appMeta($id: ID!){
- app(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_app_as_anonymous_user(api_client, app):
- # given
- variables = {"id": graphene.Node.to_global_id("App", app.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_APP_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_app_as_customer(user_api_client, app):
- # given
- variables = {"id": graphene.Node.to_global_id("App", app.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_APP_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_app_as_staff(
- staff_api_client, app, permission_manage_apps
-):
- # given
- app.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- app.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("App", app.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_APP_PRIVATE_META,
- variables,
- [permission_manage_apps],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["app"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_app_as_app(app_api_client, app, permission_manage_apps):
- # given
- app.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- app.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("App", app.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_APP_PRIVATE_META,
- variables,
- [permission_manage_apps],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["app"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_PAGE_TYPE_PRIVATE_META = """
- query pageTypeMeta($id: ID!){
- pageType(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_page_type_as_anonymous_user(api_client, page_type):
- # given
- variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
-
- # when
- response = api_client.post_graphql(QUERY_PAGE_TYPE_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_page_type_as_customer(user_api_client, page_type):
- # given
- variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
-
- # when
- response = user_api_client.post_graphql(QUERY_PAGE_TYPE_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_page_type_as_staff(
- staff_api_client, page_type, permission_manage_page_types_and_attributes
-):
- # given
- page_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- page_type.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_PAGE_TYPE_PRIVATE_META,
- variables,
- [permission_manage_page_types_and_attributes],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["pageType"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_page_type_as_app(
- app_api_client, page_type, permission_manage_page_types_and_attributes
-):
- # given
- page_type.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- page_type.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("PageType", page_type.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_PAGE_TYPE_PRIVATE_META,
- variables,
- [permission_manage_page_types_and_attributes],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["pageType"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_WAREHOUSE_PUBLIC_META = """
- query warehouseMeta($id: ID!){
- warehouse(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_warehouse_as_anonymous_user(api_client, warehouse):
- # given
- warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- warehouse.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
- }
-
- # when
- response = api_client.post_graphql(QUERY_WAREHOUSE_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_warehouse_as_customer(user_api_client, warehouse):
- # given
- warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- warehouse.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_WAREHOUSE_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_warehouse_as_staff(
- staff_api_client, warehouse, permission_manage_products
-):
- # given
- warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- warehouse.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Warehouse", warehouse.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_WAREHOUSE_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["warehouse"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_warehouse_as_app(
- app_api_client, warehouse, permission_manage_products
-):
- # given
- warehouse.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- warehouse.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Warehouse", warehouse.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_WAREHOUSE_PUBLIC_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["warehouse"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_WAREHOUSE_PRIVATE_META = """
- query warehouseMeta($id: ID!){
- warehouse(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_warehouse_as_anonymous_user(api_client, warehouse):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
- }
-
- # when
- response = api_client.post_graphql(QUERY_WAREHOUSE_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_warehouse_as_customer(user_api_client, warehouse):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_WAREHOUSE_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_warehouse_as_staff(
- staff_api_client, warehouse, permission_manage_products
-):
- # given
- warehouse.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- warehouse.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Warehouse", warehouse.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_WAREHOUSE_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["warehouse"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_warehouse_as_app(
- app_api_client, warehouse, permission_manage_products
-):
- # given
- warehouse.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- warehouse.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Warehouse", warehouse.pk),
- }
-
- # when
- response = app_api_client.post_graphql(
- QUERY_WAREHOUSE_PRIVATE_META,
- variables,
- [permission_manage_products],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["warehouse"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_VOUCHER_PUBLIC_META = """
- query voucherMeta($id: ID!){
- voucher(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_voucher_as_anonymous_user(api_client, voucher):
- # given
- voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- voucher.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Voucher", voucher.pk),
- }
-
- # when
- response = api_client.post_graphql(QUERY_VOUCHER_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_voucher_as_customer(user_api_client, voucher):
- # given
- voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- voucher.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Voucher", voucher.pk),
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_VOUCHER_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_voucher_as_staff(
- staff_api_client, voucher, permission_manage_discounts
-):
- # given
- voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- voucher.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Voucher", voucher.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_VOUCHER_PUBLIC_META,
- variables,
- [permission_manage_discounts],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["voucher"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_voucher_as_app(
- app_api_client, voucher, permission_manage_discounts
-):
- # given
- voucher.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- voucher.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Voucher", voucher.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_VOUCHER_PUBLIC_META,
- variables,
- [permission_manage_discounts],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["voucher"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_VOUCHER_PRIVATE_META = """
- query voucherMeta($id: ID!){
- voucher(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_voucher_as_anonymous_user(api_client, voucher):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Voucher", voucher.pk),
- }
-
- # when
- response = api_client.post_graphql(QUERY_VOUCHER_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_voucher_as_customer(user_api_client, voucher):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Voucher", voucher.pk),
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_VOUCHER_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_voucher_as_staff(
- staff_api_client, voucher, permission_manage_discounts
-):
- # given
- voucher.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- voucher.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Voucher", voucher.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_VOUCHER_PRIVATE_META,
- variables,
- [permission_manage_discounts],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["voucher"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_voucher_as_app(
- app_api_client, voucher, permission_manage_discounts
-):
- # given
- voucher.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- voucher.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Voucher", voucher.pk),
- }
-
- # when
- response = app_api_client.post_graphql(
- QUERY_VOUCHER_PRIVATE_META,
- variables,
- [permission_manage_discounts],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["voucher"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_SALE_PUBLIC_META = """
- query saleMeta($id: ID!){
- sale(id: $id){
- metadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_public_meta_for_sale_as_anonymous_user(api_client, sale):
- # given
- sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- sale.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Sale", sale.pk),
- }
-
- # when
- response = api_client.post_graphql(QUERY_SALE_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_sale_as_customer(user_api_client, sale):
- # given
- sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- sale.save(update_fields=["metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Sale", sale.pk),
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_SALE_PUBLIC_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_public_meta_for_sale_as_staff(
- staff_api_client, sale, permission_manage_discounts
-):
- # given
- sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- sale.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Sale", sale.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_SALE_PUBLIC_META,
- variables,
- [permission_manage_discounts],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["sale"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-def test_query_public_meta_for_sale_as_app(
- app_api_client, sale, permission_manage_discounts
-):
- # given
- sale.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- sale.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("Sale", sale.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_SALE_PUBLIC_META,
- variables,
- [permission_manage_discounts],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["sale"]["metadata"][0]
- assert metadata["key"] == PUBLIC_KEY
- assert metadata["value"] == PUBLIC_VALUE
-
-
-QUERY_SALE_PRIVATE_META = """
- query saleMeta($id: ID!){
- sale(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_sale_as_anonymous_user(api_client, sale):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Sale", sale.pk),
- }
-
- # when
- response = api_client.post_graphql(QUERY_SALE_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_sale_as_customer(user_api_client, sale):
- # given
- variables = {
- "id": graphene.Node.to_global_id("Sale", sale.pk),
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_SALE_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_sale_as_staff(
- staff_api_client, sale, permission_manage_discounts
-):
- # given
- sale.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- sale.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("Sale", sale.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_SALE_PRIVATE_META,
- variables,
- [permission_manage_discounts],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["sale"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_sale_as_app(
- app_api_client, sale, permission_manage_discounts
-):
- # given
- sale.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- sale.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("Sale", sale.pk),
- }
-
- # when
- response = app_api_client.post_graphql(
- QUERY_SALE_PRIVATE_META,
- variables,
- [permission_manage_discounts],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["sale"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_GIFT_CARD_PRIVATE_META = """
- query giftCardMeta($id: ID!){
- giftCard(id: $id){
- privateMetadata{
- key
- value
- }
- }
- }
-"""
-
-
-def test_query_private_meta_for_gift_card_as_anonymous_user(api_client, gift_card):
- # given
- variables = {
- "id": graphene.Node.to_global_id("GiftCard", gift_card.pk),
- }
-
- # when
- response = api_client.post_graphql(QUERY_GIFT_CARD_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_gift_card_as_customer(user_api_client, gift_card):
- # given
- variables = {
- "id": graphene.Node.to_global_id("GiftCard", gift_card.pk),
- }
-
- # when
- response = user_api_client.post_graphql(QUERY_GIFT_CARD_PRIVATE_META, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_private_meta_for_gift_card_as_staff(
- staff_api_client, gift_card, permission_manage_gift_card
-):
- # given
- gift_card.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- gift_card.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("GiftCard", gift_card.pk)}
-
- # when
- response = staff_api_client.post_graphql(
- QUERY_GIFT_CARD_PRIVATE_META,
- variables,
- [permission_manage_gift_card],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["giftCard"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-def test_query_private_meta_for_gift_card_as_app(
- app_api_client, gift_card, permission_manage_gift_card
-):
- # given
- gift_card.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- gift_card.save(update_fields=["private_metadata"])
- variables = {
- "id": graphene.Node.to_global_id("GiftCard", gift_card.pk),
- }
-
- # when
- response = app_api_client.post_graphql(
- QUERY_GIFT_CARD_PRIVATE_META,
- variables,
- [permission_manage_gift_card],
- check_no_permissions=False,
- )
- content = get_graphql_content(response)
-
- # then
- metadata = content["data"]["giftCard"]["privateMetadata"][0]
- assert metadata["key"] == PRIVATE_KEY
- assert metadata["value"] == PRIVATE_VALUE
-
-
-QUERY_PRODUCT_MEDIA_METADATA = """
- query productMediaById(
- $mediaId: ID!,
- $productId: ID!,
- $channel: String,
- ) {
- product(id: $productId, channel: $channel) {
- mediaById(id: $mediaId) {
- metadata {
- key
- value
- }
- privateMetadata {
- key
- value
- }
- }
- }
- }
-"""
-
-
-def test_query_metadata_for_product_media_as_staff(
- staff_api_client, product_with_image, channel_USD, permission_manage_products
-):
- # given
- query = QUERY_PRODUCT_MEDIA_METADATA
- media = product_with_image.media.first()
-
- metadata = {"label": "image-name"}
- private_metadata = {"private-label": "private-name"}
- media.store_value_in_metadata(metadata)
- media.store_value_in_private_metadata(private_metadata)
- media.save(update_fields=["metadata", "private_metadata"])
-
- variables = {
- "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
- "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
- content = get_graphql_content(response)
-
- # then
- data = content["data"]["product"]["mediaById"]
- assert data["metadata"][0]["key"] in metadata.keys()
- assert data["metadata"][0]["value"] in metadata.values()
- assert data["privateMetadata"][0]["key"] in private_metadata.keys()
- assert data["privateMetadata"][0]["value"] in private_metadata.values()
-
-
-def test_query_metadata_for_product_media_as_app(
- app_api_client, product_with_image, channel_USD, permission_manage_products
-):
- # given
- query = QUERY_PRODUCT_MEDIA_METADATA
- media = product_with_image.media.first()
-
- metadata = {"label": "image-name"}
- private_metadata = {"private-label": "private-name"}
- media.store_value_in_metadata(metadata)
- media.store_value_in_private_metadata(private_metadata)
- media.save(update_fields=["metadata", "private_metadata"])
-
- variables = {
- "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
- "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = app_api_client.post_graphql(
- query, variables, permissions=[permission_manage_products]
- )
- content = get_graphql_content(response)
-
- # then
- data = content["data"]["product"]["mediaById"]
- assert data["metadata"][0]["key"] in metadata.keys()
- assert data["metadata"][0]["value"] in metadata.values()
- assert data["privateMetadata"][0]["key"] in private_metadata.keys()
- assert data["privateMetadata"][0]["value"] in private_metadata.values()
-
-
-def test_query_metadata_for_product_media_as_anonymous_user(
- api_client, product_with_image, channel_USD
-):
- # given
- query = QUERY_PRODUCT_MEDIA_METADATA
- media = product_with_image.media.first()
- variables = {
- "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
- "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = api_client.post_graphql(query, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_metadata_for_product_media_as_customer_user(
- user_api_client, product_with_image, channel_USD
-):
- # given
- query = QUERY_PRODUCT_MEDIA_METADATA
- media = product_with_image.media.first()
- variables = {
- "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
- "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = user_api_client.post_graphql(query, variables)
-
- # then
- assert_no_permission(response)
-
-
-def test_query_metadata_for_product_media_as_staff_missing_permissions(
- staff_api_client, product_with_image, channel_USD
-):
- # given
- query = QUERY_PRODUCT_MEDIA_METADATA
- media = product_with_image.media.first()
-
- variables = {
- "productId": graphene.Node.to_global_id("Product", product_with_image.pk),
- "mediaId": graphene.Node.to_global_id("ProductMedia", media.pk),
- "channel": channel_USD.slug,
- }
-
- # when
- response = staff_api_client.post_graphql(query, variables)
-
- # then
- assert_no_permission(response)
diff --git a/saleor/graphql/order/bulk_mutations/order_bulk_create.py b/saleor/graphql/order/bulk_mutations/order_bulk_create.py
index f7a37f2a55..d6260deea0 100644
--- a/saleor/graphql/order/bulk_mutations/order_bulk_create.py
+++ b/saleor/graphql/order/bulk_mutations/order_bulk_create.py
@@ -56,7 +56,7 @@ from ...core.scalars import PositiveDecimal, WeightScalar
from ...core.types import BaseInputObjectType, BaseObjectType, NonNullList
from ...core.types.common import OrderBulkCreateError
from ...core.utils import from_global_id_or_error
-from ...meta.mutations import MetadataInput
+from ...meta.inputs import MetadataInput
from ...payment.mutations.transaction.transaction_create import (
TransactionCreate,
TransactionCreateInput,
diff --git a/saleor/graphql/payment/mutations/payment/checkout_payment_create.py b/saleor/graphql/payment/mutations/payment/checkout_payment_create.py
index 2ed67c41a4..86bed93ed7 100644
--- a/saleor/graphql/payment/mutations/payment/checkout_payment_create.py
+++ b/saleor/graphql/payment/mutations/payment/checkout_payment_create.py
@@ -28,7 +28,7 @@ from ....core.mutations import BaseMutation
from ....core.scalars import UUID, PositiveDecimal
from ....core.types import BaseInputObjectType
from ....core.types import common as common_types
-from ....meta.mutations import MetadataInput
+from ....meta.inputs import MetadataInput
from ....plugins.dataloaders import get_plugin_manager_promise
from ...enums import StorePaymentMethodEnum
from ...types import Payment
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_create.py b/saleor/graphql/payment/mutations/transaction/transaction_create.py
index e9a19aff19..e45d134057 100644
--- a/saleor/graphql/payment/mutations/transaction/transaction_create.py
+++ b/saleor/graphql/payment/mutations/transaction/transaction_create.py
@@ -34,7 +34,7 @@ from ....core.doc_category import DOC_CATEGORY_PAYMENTS
from ....core.mutations import BaseMutation
from ....core.types import BaseInputObjectType
from ....core.types import common as common_types
-from ....meta.mutations import MetadataInput
+from ....meta.inputs import MetadataInput
from ....plugins.dataloaders import get_plugin_manager_promise
from ...enums import TransactionActionEnum, TransactionEventStatusEnum
from ...types import TransactionItem
diff --git a/saleor/graphql/product/bulk_mutations/product_bulk_create.py b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
index 3fef676606..77f6a068af 100644
--- a/saleor/graphql/product/bulk_mutations/product_bulk_create.py
+++ b/saleor/graphql/product/bulk_mutations/product_bulk_create.py
@@ -41,7 +41,7 @@ from ...core.types import (
from ...core.utils import get_duplicated_values
from ...core.validators import clean_seo_fields
from ...core.validators.file import clean_image_file, is_image_url, validate_image_url
-from ...meta.mutations import MetadataInput
+from ...meta.inputs import MetadataInput
from ...plugins.dataloaders import get_plugin_manager_promise
from ..mutations.product.product_create import ProductCreateInput
from ..types import Product
diff --git a/saleor/graphql/product/mutations/category/category_create.py b/saleor/graphql/product/mutations/category/category_create.py
index c485a17920..c223d9e9ef 100644
--- a/saleor/graphql/product/mutations/category/category_create.py
+++ b/saleor/graphql/product/mutations/category/category_create.py
@@ -19,7 +19,7 @@ from ....core.types import (
)
from ....core.validators import clean_seo_fields, validate_slug_and_generate_if_needed
from ....core.validators.file import clean_image_file
-from ....meta.mutations import MetadataInput
+from ....meta.inputs import MetadataInput
from ....plugins.dataloaders import get_plugin_manager_promise
from ...types import Category
diff --git a/saleor/graphql/product/mutations/collection/collection_create.py b/saleor/graphql/product/mutations/collection/collection_create.py
index d6dcc4563c..b40998462d 100644
--- a/saleor/graphql/product/mutations/collection/collection_create.py
+++ b/saleor/graphql/product/mutations/collection/collection_create.py
@@ -24,7 +24,7 @@ from ....core.types import (
)
from ....core.validators import clean_seo_fields, validate_slug_and_generate_if_needed
from ....core.validators.file import clean_image_file
-from ....meta.mutations import MetadataInput
+from ....meta.inputs import MetadataInput
from ....plugins.dataloaders import get_plugin_manager_promise
from ...types import Collection
diff --git a/saleor/graphql/product/mutations/digital_contents.py b/saleor/graphql/product/mutations/digital_contents.py
index 8cbfba0a7d..f4e1414579 100644
--- a/saleor/graphql/product/mutations/digital_contents.py
+++ b/saleor/graphql/product/mutations/digital_contents.py
@@ -12,7 +12,7 @@ from ...core.descriptions import ADDED_IN_38
from ...core.doc_category import DOC_CATEGORY_PRODUCTS
from ...core.mutations import BaseMutation, ModelMutation
from ...core.types import BaseInputObjectType, NonNullList, ProductError, Upload
-from ...meta.mutations import MetadataInput
+from ...meta.inputs import MetadataInput
from ...plugins.dataloaders import get_plugin_manager_promise
from ..types import DigitalContent, DigitalContentUrl, ProductVariant
diff --git a/saleor/graphql/product/mutations/product/product_create.py b/saleor/graphql/product/mutations/product/product_create.py
index 92ec0b435f..a857a38fb9 100644
--- a/saleor/graphql/product/mutations/product/product_create.py
+++ b/saleor/graphql/product/mutations/product/product_create.py
@@ -27,7 +27,7 @@ from ....core.mutations import ModelMutation
from ....core.scalars import WeightScalar
from ....core.types import BaseInputObjectType, NonNullList, ProductError, SeoInput
from ....core.validators import clean_seo_fields, validate_slug_and_generate_if_needed
-from ....meta.mutations import MetadataInput
+from ....meta.inputs import MetadataInput
from ....plugins.dataloaders import get_plugin_manager_promise
from ...types import Product
from ..utils import clean_tax_code
diff --git a/saleor/graphql/product/mutations/product_variant/product_variant_create.py b/saleor/graphql/product/mutations/product_variant/product_variant_create.py
index 7b264c4d73..689fabbfe0 100644
--- a/saleor/graphql/product/mutations/product_variant/product_variant_create.py
+++ b/saleor/graphql/product/mutations/product_variant/product_variant_create.py
@@ -24,7 +24,7 @@ from ....core.mutations import ModelMutation
from ....core.scalars import WeightScalar
from ....core.types import BaseInputObjectType, NonNullList, ProductError
from ....core.utils import get_duplicated_values
-from ....meta.mutations import MetadataInput
+from ....meta.inputs import MetadataInput
from ....plugins.dataloaders import get_plugin_manager_promise
from ....warehouse.types import Warehouse
from ...types import ProductVariant