Skip to content

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

Add test for query public and private metadata

fowczarek21879f897c11
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.py
index f32d02149a..2e96e47adc 100644
--- a/saleor/checkout/models.py
+++ b/saleor/checkout/models.py
@@ -110,9 +110,6 @@ class Checkout(ModelWithMetadata):
def __iter__(self):
return iter(self.lines.all())
- # def __len__(self):
- # return self.lines.count()
-
def get_customer_email(self) -> str:
return self.user.email if self.user else self.email
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 041670f5f8..6dc7cb4048 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -628,7 +628,7 @@ def create_line_for_order(checkout_line: "CheckoutLine", discounts) -> OrderLine
if not isinstance(unit_price, Decimal)
else Decimal("0.0")
)
- line = OrderLine( # type: ignore
+ line = OrderLine(
product_name=product_name,
variant_name=variant_name,
translated_product_name=translated_product_name,
diff --git a/saleor/graphql/meta/types.py b/saleor/graphql/meta/types.py
index 70f6683032..f2c7f4b1b0 100644
--- a/saleor/graphql/meta/types.py
+++ b/saleor/graphql/meta/types.py
@@ -26,7 +26,6 @@ class ObjectWithMetadata(graphene.Interface):
)
# Deprecated we should remove it in #5221
-
private_meta = graphene.List(
"saleor.graphql.meta.deprecated.types.MetaStore",
required=True,
@@ -39,7 +38,7 @@ class ObjectWithMetadata(graphene.Interface):
required=True,
description="List of publicly stored metadata namespaces.",
deprecation_reason="DEPRECATED: Will be removed in Saleor 2.11. "
- "use the `privetaMetadata` field instead. ",
+ "use the `metadata` field instead. ",
)
@staticmethod
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 3e6775aa46..537928885d 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -197,7 +197,7 @@ type Attribute implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
inputType: AttributeInputTypeEnum
name: String
slug: String
@@ -512,7 +512,7 @@ type Category implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
ancestors(before: String, after: String, first: Int, last: Int): CategoryCountableConnection
products(before: String, after: String, first: Int, last: Int): ProductCountableConnection
url: String
@@ -651,7 +651,7 @@ type Checkout implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
availableShippingMethods: [ShippingMethod]!
availablePaymentGateways: [PaymentGateway]!
email: String!
@@ -860,7 +860,7 @@ type Collection implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
products(before: String, after: String, first: Int, last: Int): ProductCountableConnection
backgroundImage(size: Int): Image
translation(languageCode: LanguageCodeEnum!): CollectionTranslation
@@ -1434,7 +1434,7 @@ type DigitalContent implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
}
type DigitalContentCountableConnection {
@@ -1628,7 +1628,7 @@ type Fulfillment implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
lines: [FulfillmentLine]
statusDisplay: String
}
@@ -2420,7 +2420,7 @@ interface ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
}
type Order implements Node, ObjectWithMetadata {
@@ -2447,7 +2447,7 @@ type Order implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
fulfillments: [Fulfillment]!
lines: [OrderLine]!
actions: [OrderAction]!
@@ -3135,7 +3135,7 @@ type Product implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
url: String!
thumbnail(size: Int): Image
pricing: ProductPricingInfo
@@ -3387,7 +3387,7 @@ type ProductType implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
products(before: String, after: String, first: Int, last: Int): ProductCountableConnection
taxRate: TaxRateType
taxType: TaxType
@@ -3529,7 +3529,7 @@ type ProductVariant implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
quantity: Int! @deprecated(reason: "This field will be removed in Saleor 2.11. Use the stock field instead.")
quantityAllocated: Int @deprecated(reason: "This field will be removed in Saleor 2.11. Use the stock field instead.")
stockQuantity: Int! @deprecated(reason: "This field will be removed in Saleor 2.11. Use the stock field instead.")
@@ -3889,7 +3889,7 @@ type ServiceAccount implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
permissions: [PermissionDisplay]
name: String
}
@@ -4541,7 +4541,7 @@ type User implements Node, ObjectWithMetadata {
privateMetadata: [MetaItem]!
metadata: [MetaItem]!
privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
- meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ")
+ meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ")
addresses: [Address]
checkout: Checkout
giftCards(before: String, after: String, first: Int, last: Int): GiftCardCountableConnection
diff --git a/tests/api/test_meta.py b/tests/api/test_meta.py
index 4483e28c89..9b7ae4f758 100644
--- a/tests/api/test_meta.py
+++ b/tests/api/test_meta.py
@@ -13,6 +13,2606 @@ PUBLIC_KEY = "key"
PUBLIC_VALUE = "value"
+QUERY_SELF_PUBLIC_META = """
+ {
+ me{
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_me_as_customer(user_api_client):
+ # given
+ me = user_api_client.user
+ me.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ me.save(update_fields=["meta"])
+
+ # 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
+
+
+def test_query_public_meta_for_me_as_staff(staff_api_client):
+ # given
+ me = staff_api_client.user
+ me.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ me.save(update_fields=["meta"])
+
+ # 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
+
+
+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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ customer_user.save(update_fields=["meta"])
+ 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_service_account(
+ service_account_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ customer_user.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
+
+ # when
+ response = service_account_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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ admin_user.save(update_fields=["meta"])
+ 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
+
+
+def test_query_public_meta_for_staff_as_service_account(
+ service_account_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_user.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ admin_user.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("User", admin_user.pk)}
+
+ # when
+ response = service_account_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.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.save(update_fields=["meta"])
+ 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
+
+
+# TODO: Restore after #5245
+# def test_query_public_meta_for_other_customer_checkout_as_anonymous_user(
+# api_client, checkout, customer_user
+# ):
+# # given
+# checkout.user = customer_user
+# checkout.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+# checkout.save(update_fields=["user", "meta"])
+# variables = {"token": checkout.pk}
+
+# # when
+# response = api_client.post_graphql(QUERY_CHECKOUT_PUBLIC_META, variables)
+
+# # then
+# assert_no_permission(response)
+
+
+def test_query_public_meta_for_checkout_as_customer(user_api_client, checkout):
+ # given
+ checkout.user = user_api_client.user
+ checkout.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.save(update_fields=["user", "meta"])
+ 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.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.save(update_fields=["user", "meta"])
+ 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_service_account(
+ service_account_api_client, checkout, customer_user, permission_manage_checkouts
+):
+ # given
+ checkout.user = customer_user
+ checkout.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ checkout.save(update_fields=["user", "meta"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = service_account_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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["meta"])
+ variables = {"token": order.token}
+
+ # 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "meta"])
+ variables = {"token": order.token}
+
+ # 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "meta"])
+ variables = {"token": order.token}
+
+ # 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_service_account(
+ service_account_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "meta"])
+ variables = {"token": order.token}
+
+ # when
+ response = service_account_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
+ variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_ORDER_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_order_as_customer(user_api_client, order):
+ # given
+ order.user = user_api_client.user
+ order.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "meta"])
+ 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ order.save(update_fields=["user", "meta"])
+ 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
+
+
+# TODO: Restore after #5251
+# def test_query_public_meta_for_order_as_service_account(
+# service_account_api_client, order, customer_user, permission_manage_orders
+# ):
+# # given
+# order.user = customer_user
+# order.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+# order.save(update_fields=["user", "meta"])
+# variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+# # when
+# response = service_account_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
+ variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_DRAFT_ORDER_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+# TODO: Restore after #5252
+# 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+# draft_order.save(update_fields=["user", "meta"])
+# variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+# # when
+# response = user_api_client.post_graphql(QUERY_DRAFT_ORDER_PUBLIC_META, variables)
+
+# # then
+# assert_no_permission(response)
+
+
+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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ draft_order.save(update_fields=["user", "meta"])
+ 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
+
+
+# TODO: Restore after #5251
+# def test_query_public_meta_for_draft_order_as_service_account(
+# service_account_api_client, draft_order, customer_user, permission_manage_orders
+# ):
+# # given
+# draft_order.user = customer_user
+# draft_order.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+# draft_order.save(update_fields=["user", "meta"])
+# variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+# # when
+# response = service_account_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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ fulfillment.save(update_fields=["meta"])
+ variables = {"token": fulfilled_order.token}
+
+ # 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ fulfillment.save(update_fields=["meta"])
+ fulfilled_order.user = user_api_client.user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.token}
+
+ # 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ fulfillment.save(update_fields=["meta"])
+ fulfilled_order.user = customer_user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.token}
+
+ # 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_service_account(
+ service_account_api_client, fulfilled_order, customer_user, permission_manage_orders
+):
+ # given
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ fulfillment.save(update_fields=["meta"])
+ fulfilled_order.user = customer_user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.token}
+
+ # when
+ response = service_account_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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ color_attribute.save(update_fields=["meta"])
+ 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ color_attribute.save(update_fields=["meta"])
+ 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ color_attribute.save(update_fields=["meta"])
+ 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_service_account(
+ service_account_api_client, color_attribute, permission_manage_products
+):
+ # given
+ color_attribute.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ color_attribute.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = service_account_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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ category.save(update_fields=["meta"])
+ 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ category.save(update_fields=["meta"])
+ 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ category.save(update_fields=["meta"])
+ 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_service_account(
+ service_account_api_client, category, permission_manage_products
+):
+ # given
+ category.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ category.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = service_account_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!){
+ collection(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_collection_as_anonymous_user(api_client, collection):
+ # given
+ collection.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ collection.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.pk)}
+
+ # 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, collection):
+ # given
+ collection.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ collection.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.pk)}
+
+ # 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, collection, permission_manage_products
+):
+ # given
+ collection.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ collection.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.pk)}
+
+ # 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_service_account(
+ service_account_api_client, collection, permission_manage_products
+):
+ # given
+ collection.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ collection.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.pk)}
+
+ # when
+ response = service_account_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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ digital_content.save(update_fields=["meta"])
+ 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ digital_content.save(update_fields=["meta"])
+ 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_service_account(
+ service_account_api_client, digital_content, permission_manage_products
+):
+ # given
+ digital_content.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ digital_content.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = service_account_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!){
+ product(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_product_as_anonymous_user(api_client, product):
+ # given
+ product.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ product.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ # 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):
+ # given
+ product.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ product.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ # 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ product.save(update_fields=["meta"])
+ 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_service_account(
+ service_account_api_client, product, permission_manage_products
+):
+ # given
+ product.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ product.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ # when
+ response = service_account_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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ product_type.save(update_fields=["meta"])
+ 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ product_type.save(update_fields=["meta"])
+ 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ product_type.save(update_fields=["meta"])
+ 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_service_account(
+ service_account_api_client, product_type, permission_manage_products
+):
+ # given
+ product_type.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ product_type.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = service_account_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!){
+ productVariant(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_product_variant_as_anonymous_user(api_client, variant):
+ # given
+ variant.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ variant.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+
+ # 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):
+ # given
+ variant.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ variant.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+
+ # 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_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ variant.save(update_fields=["meta"])
+ 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_service_account(
+ service_account_api_client, variant, permission_manage_products
+):
+ # given
+ variant.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ variant.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+
+ # when
+ response = service_account_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_SERVICE_ACCOUNT_PUBLIC_META = """
+ query serviceAccountMeta($id: ID!){
+ serviceAccount(id: $id){
+ metadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_public_meta_for_service_account_as_anonymous_user(
+ api_client, service_account
+):
+ # given
+ variables = {"id": graphene.Node.to_global_id("ServiceAccount", service_account.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_SERVICE_ACCOUNT_PUBLIC_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_service_account_as_customer(
+ user_api_client, service_account
+):
+ # given
+ variables = {"id": graphene.Node.to_global_id("ServiceAccount", service_account.pk)}
+
+ # when
+ response = user_api_client.post_graphql(
+ QUERY_SERVICE_ACCOUNT_PUBLIC_META, variables
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_public_meta_for_service_account_as_staff(
+ staff_api_client, service_account, permission_manage_service_accounts
+):
+ # given
+ service_account.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ service_account.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("ServiceAccount", service_account.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_SERVICE_ACCOUNT_PUBLIC_META,
+ variables,
+ [permission_manage_service_accounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["serviceAccount"]["metadata"][0]
+ assert metadata["key"] == PUBLIC_KEY
+ assert metadata["value"] == PUBLIC_VALUE
+
+
+def test_query_public_meta_for_service_account_as_service_account(
+ service_account_api_client, service_account, permission_manage_service_accounts
+):
+ # given
+ service_account.store_meta({PUBLIC_KEY: PUBLIC_VALUE})
+ service_account.save(update_fields=["meta"])
+ variables = {"id": graphene.Node.to_global_id("ServiceAccount", service_account.pk)}
+
+ # when
+ response = service_account_api_client.post_graphql(
+ QUERY_SERVICE_ACCOUNT_PUBLIC_META,
+ variables,
+ [permission_manage_service_accounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["serviceAccount"]["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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ me.save(update_fields=["private_meta"])
+
+ # 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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ customer_user.save(update_fields=["private_meta"])
+ 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_service_account(
+ service_account_api_client, permission_manage_users, customer_user
+):
+ # given
+ customer_user.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ customer_user.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("User", customer_user.pk)}
+
+ # when
+ response = service_account_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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ admin_user.save(update_fields=["private_meta"])
+ 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
+
+
+def test_query_private_meta_for_staff_as_service_account(
+ service_account_api_client, permission_manage_staff, admin_user
+):
+ # given
+ admin_user.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ admin_user.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("User", admin_user.pk)}
+
+ # when
+ response = service_account_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)
+
+
+# TODO: Restore after #5245
+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)
+
+ # then
+ assert_no_permission(response)
+
+
+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.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ checkout.save(update_fields=["user", "private_meta"])
+ 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_service_account(
+ service_account_api_client, checkout, customer_user, permission_manage_checkouts
+):
+ # given
+ checkout.user = customer_user
+ checkout.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ checkout.save(update_fields=["user", "private_meta"])
+ variables = {"token": checkout.pk}
+
+ # when
+ response = service_account_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.token}
+
+ # 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.token}
+
+ # 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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ order.save(update_fields=["user", "private_meta"])
+ variables = {"token": order.token}
+
+ # 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_service_account(
+ service_account_api_client, order, customer_user, permission_manage_orders
+):
+ # given
+ order.user = customer_user
+ order.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ order.save(update_fields=["user", "private_meta"])
+ variables = {"token": order.token}
+
+ # when
+ response = service_account_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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ order.save(update_fields=["user", "private_meta"])
+ 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
+
+
+# TODO: Restore after #5251
+# def test_query_private_meta_for_order_as_service_account(
+# service_account_api_client, order, customer_user, permission_manage_orders
+# ):
+# # given
+# order.user = customer_user
+# order.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+# order.save(update_fields=["user", "private_meta"])
+# variables = {"id": graphene.Node.to_global_id("Order", order.pk)}
+
+# # when
+# response = service_account_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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ draft_order.save(update_fields=["user", "private_meta"])
+ 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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ draft_order.save(update_fields=["user", "private_meta"])
+ 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
+
+
+# TODO: Restore after #5251
+# def test_query_private_meta_for_draft_order_as_service_account(
+# service_account_api_client, draft_order, customer_user, permission_manage_orders
+# ):
+# # given
+# draft_order.user = customer_user
+# draft_order.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+# draft_order.save(update_fields=["user", "private_meta"])
+# variables = {"id": graphene.Node.to_global_id("Order", draft_order.pk)}
+
+# # when
+# response = service_account_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.token}
+
+ # 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.token}
+
+ # 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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ fulfillment.save(update_fields=["private_meta"])
+ fulfilled_order.user = customer_user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.token}
+
+ # 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_service_account(
+ service_account_api_client, fulfilled_order, customer_user, permission_manage_orders
+):
+ # given
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ fulfillment.save(update_fields=["private_meta"])
+ fulfilled_order.user = customer_user
+ fulfilled_order.save(update_fields=["user"])
+ variables = {"token": fulfilled_order.token}
+
+ # when
+ response = service_account_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_products
+):
+ # given
+ color_attribute.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ color_attribute.save(update_fields=["private_meta"])
+ 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_products],
+ 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_service_account(
+ service_account_api_client, color_attribute, permission_manage_products
+):
+ # given
+ color_attribute.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ color_attribute.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("Attribute", color_attribute.pk)}
+
+ # when
+ response = service_account_api_client.post_graphql(
+ QUERY_ATTRIBUTE_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ 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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ category.save(update_fields=["private_meta"])
+ 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_service_account(
+ service_account_api_client, category, permission_manage_products
+):
+ # given
+ category.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ category.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("Category", category.pk)}
+
+ # when
+ response = service_account_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!){
+ collection(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_collection_as_anonymous_user(api_client, collection):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.pk)}
+
+ # 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, collection):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.pk)}
+
+ # 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, collection, permission_manage_products
+):
+ # given
+ collection.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ collection.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.pk)}
+
+ # 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_service_account(
+ service_account_api_client, collection, permission_manage_products
+):
+ # given
+ collection.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ collection.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("Collection", collection.pk)}
+
+ # when
+ response = service_account_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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ digital_content.save(update_fields=["private_meta"])
+ 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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ digital_content.save(update_fields=["private_meta"])
+ 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_service_account(
+ service_account_api_client, digital_content, permission_manage_products
+):
+ # given
+ digital_content.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ digital_content.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("DigitalContent", digital_content.pk)}
+
+ # when
+ response = service_account_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!){
+ product(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_product_as_anonymous_user(api_client, product):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ # 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):
+ # given
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ # 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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ product.save(update_fields=["private_meta"])
+ 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_service_account(
+ service_account_api_client, product, permission_manage_products
+):
+ # given
+ product.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ product.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}
+
+ # when
+ response = service_account_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_products
+):
+ # given
+ product_type.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ product_type.save(update_fields=["private_meta"])
+ 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_products],
+ 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_service_account(
+ service_account_api_client, product_type, permission_manage_products
+):
+ # given
+ product_type.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ product_type.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("ProductType", product_type.pk)}
+
+ # when
+ response = service_account_api_client.post_graphql(
+ QUERY_PRODUCT_TYPE_PRIVATE_META,
+ variables,
+ [permission_manage_products],
+ 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!){
+ productVariant(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_product_variant_as_anonymous_user(api_client, variant):
+ # given
+ variant.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ variant.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+
+ # 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):
+ # given
+ variant.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ variant.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+
+ # 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_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ variant.save(update_fields=["private_meta"])
+ 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_service_account(
+ service_account_api_client, variant, permission_manage_products
+):
+ # given
+ variant.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ variant.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}
+
+ # when
+ response = service_account_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_SERVICE_ACCOUNT_PRIVATE_META = """
+ query serviceAccountMeta($id: ID!){
+ serviceAccount(id: $id){
+ privateMetadata{
+ key
+ value
+ }
+ }
+ }
+"""
+
+
+def test_query_private_meta_for_service_account_as_anonymous_user(
+ api_client, service_account
+):
+ # given
+ variables = {"id": graphene.Node.to_global_id("ServiceAccount", service_account.pk)}
+
+ # when
+ response = api_client.post_graphql(QUERY_SERVICE_ACCOUNT_PRIVATE_META, variables)
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_service_account_as_customer(
+ user_api_client, service_account
+):
+ # given
+ variables = {"id": graphene.Node.to_global_id("ServiceAccount", service_account.pk)}
+
+ # when
+ response = user_api_client.post_graphql(
+ QUERY_SERVICE_ACCOUNT_PRIVATE_META, variables
+ )
+
+ # then
+ assert_no_permission(response)
+
+
+def test_query_private_meta_for_service_account_as_staff(
+ staff_api_client, service_account, permission_manage_service_accounts
+):
+ # given
+ service_account.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ service_account.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("ServiceAccount", service_account.pk)}
+
+ # when
+ response = staff_api_client.post_graphql(
+ QUERY_SERVICE_ACCOUNT_PRIVATE_META,
+ variables,
+ [permission_manage_service_accounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["serviceAccount"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
+def test_query_private_meta_for_service_account_as_service_account(
+ service_account_api_client, service_account, permission_manage_service_accounts
+):
+ # given
+ service_account.store_private_meta({PRIVATE_KEY: PRIVATE_VALUE})
+ service_account.save(update_fields=["private_meta"])
+ variables = {"id": graphene.Node.to_global_id("ServiceAccount", service_account.pk)}
+
+ # when
+ response = service_account_api_client.post_graphql(
+ QUERY_SERVICE_ACCOUNT_PRIVATE_META,
+ variables,
+ [permission_manage_service_accounts],
+ check_no_permissions=False,
+ )
+ content = get_graphql_content(response)
+
+ # then
+ metadata = content["data"]["serviceAccount"]["privateMetadata"][0]
+ assert metadata["key"] == PRIVATE_KEY
+ assert metadata["value"] == PRIVATE_VALUE
+
+
UPDATE_PUBLIC_METADATA_MUTATION = """
mutation UpdatePublicMetadata($id: ID!, $input: MetaItemInput!) {
updateMeta(