Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Restricted costPrice and stockQuantity fields in productVariant
NyanKiyoshi41bdb9388bd1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c4c4fe981e..f89d49dd18 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -90,6 +90,7 @@ All notable, unreleased changes to this project will be documented in this file.- Change `unique_together` in `AttributeValue` - #4805 by @fowczarek- Change max length of SKU in order/product variant to 255 - #4811 by @lex111- Replace Pipenv with Poetry - #3894 by @michaljelonek+- `productVariant` nodes now require `manage_products` permission to query `costPrice` and `stockQuantity` fields - #4753 by @NyanKiyoshi## 2.8.0diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex aed744fef3..57d91d553b 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -310,6 +310,7 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):return getattr(root, "digital_content", None)@staticmethod+ @permission_required("product.manage_products")def resolve_stock_quantity(root: models.ProductVariant, *_args):return root.quantity_available@@ -325,6 +326,11 @@ class ProductVariant(CountableDjangoObjectType, MetadataObjectType):def resolve_margin(root: models.ProductVariant, *_args):return get_margin_for_variant(root)+ @staticmethod+ @permission_required("product.manage_products")+ def resolve_cost_price(root: models.ProductVariant, *_args):+ return root.cost_price+@staticmethoddef resolve_price(root: models.ProductVariant, *_args):return (diff --git a/tests/api/benchmark/test_checkout.py b/tests/api/benchmark/test_checkout.pyindex ef0b6e5c8a..93f2e698c7 100644--- a/tests/api/benchmark/test_checkout.py+++ b/tests/api/benchmark/test_checkout.py@@ -100,7 +100,6 @@ def test_create_checkout(api_client, graphql_address_data, variant, count_querie...Price}variant {- stockQuantity...ProductVariant}quantity@@ -240,7 +239,6 @@ def test_add_shipping_to_checkout(...Price}variant {- stockQuantity...ProductVariant}quantity@@ -373,7 +371,6 @@ def test_add_billing_address_to_checkout(...Price}variant {- stockQuantity...ProductVariant}quantitydiff --git a/tests/api/benchmark/test_product.py b/tests/api/benchmark/test_product.pyindex 57ff1d17c9..bfff3ee047 100644--- a/tests/api/benchmark/test_product.py+++ b/tests/api/benchmark/test_product.py@@ -24,7 +24,6 @@ def test_product_details(product, api_client, count_queries):idskuname- stockQuantityisAvailableprice {currencydiff --git a/tests/api/benchmark/test_variant.py b/tests/api/benchmark/test_variant.pyindex 6e2e6110da..c7fb8c93cd 100644--- a/tests/api/benchmark/test_variant.py+++ b/tests/api/benchmark/test_variant.py@@ -24,7 +24,6 @@ def test_retrieve_variant_list(product_variant_list, api_client, count_queries):idskuname- stockQuantityisAvailableprice {currency@@ -49,7 +48,6 @@ def test_retrieve_variant_list(product_variant_list, api_client, count_queries):edges {node {...ProductVariantFields- stockQuantityproduct {...BasicProductFields}diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex 8b36bfa2dd..c9b5211df7 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -147,7 +147,6 @@ def test_product_query(staff_api_client, product, permission_manage_products):}variants {name- stockQuantity}isAvailablepricing {@@ -2306,98 +2305,82 @@ def test_report_product_sales(assert Decimal(amount) == line_b.quantity * line_b.unit_price_gross_amount-def test_variant_revenue_permissions(- staff_api_client, permission_manage_products, permission_manage_orders, product+ "field, is_nested",+ (+ ("basePrice", True),+ ("purchaseCost", True),+ ("margin", True),+ ("privateMeta", True),+ ),+)+def test_product_restricted_fields_permissions(+ staff_api_client,+ permission_manage_products,+ permission_manage_orders,+ product,+ field,+ is_nested,):- query = """- query VariantRevenue($id: ID!) {- productVariant(id: $id) {- revenue(period: TODAY) {- gross {- localized- }- }- }- }+ """Ensure non-public (restricted) fields are correctly requiring+ the 'manage_products' permission."""- variant = product.variants.first()- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}- permissions = [permission_manage_orders, permission_manage_products]- response = staff_api_client.post_graphql(query, variables, permissions)- content = get_graphql_content(response)- assert content["data"]["productVariant"]["revenue"]---def test_variant_quantity_permissions(- staff_api_client, permission_manage_products, product-):query = """- query Quantity($id: ID!) {- productVariant(id: $id) {- quantity+ query Product($id: ID!) {+ product(id: $id) {+ %(field)s}}- """- variant = product.variants.first()- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}- permissions = [permission_manage_products]- response = staff_api_client.post_graphql(query, variables, permissions)- content = get_graphql_content(response)- assert "quantity" in content["data"]["productVariant"]---def test_variant_quantity_ordered_permissions(- staff_api_client, permission_manage_products, permission_manage_orders, product-):- query = """- query QuantityOrdered($id: ID!) {- productVariant(id: $id) {- quantityOrdered- }+ """ % {+ "field": field if not is_nested else "%s { __typename }" % field}- """- variant = product.variants.first()- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}+ variables = {"id": graphene.Node.to_global_id("Product", product.pk)}permissions = [permission_manage_orders, permission_manage_products]response = staff_api_client.post_graphql(query, variables, permissions)content = get_graphql_content(response)- assert "quantityOrdered" in content["data"]["productVariant"]+ assert field in content["data"]["product"]-def test_variant_quantity_allocated_permissions(- staff_api_client, permission_manage_products, permission_manage_orders, product+ "field, is_nested",+ (+ ("digitalContent", True),+ ("stockQuantity", False),+ ("margin", False),+ ("costPrice", True),+ ("priceOverride", True),+ ("quantity", False),+ ("quantityOrdered", False),+ ("quantityAllocated", False),+ ("privateMeta", True),+ ),+)+def test_variant_restricted_fields_permissions(+ staff_api_client,+ permission_manage_products,+ permission_manage_orders,+ product,+ field,+ is_nested,):- query = """- query QuantityAllocated($id: ID!) {- productVariant(id: $id) {- quantityAllocated- }- }+ """Ensure non-public (restricted) fields are correctly requiring+ the 'manage_products' permission."""- variant = product.variants.first()- variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}- permissions = [permission_manage_orders, permission_manage_products]- response = staff_api_client.post_graphql(query, variables, permissions)- content = get_graphql_content(response)- assert "quantityAllocated" in content["data"]["productVariant"]---def test_variant_margin_permissions(- staff_api_client, permission_manage_products, permission_manage_orders, product-):query = """- query Margin($id: ID!) {+ query ProductVariant($id: ID!) {productVariant(id: $id) {- margin+ %(field)s}}- """+ """ % {+ "field": field if not is_nested else "%s { __typename }" % field+ }variant = product.variants.first()variables = {"id": graphene.Node.to_global_id("ProductVariant", variant.pk)}permissions = [permission_manage_orders, permission_manage_products]response = staff_api_client.post_graphql(query, variables, permissions)content = get_graphql_content(response)- assert "margin" in content["data"]["productVariant"]+ assert field in content["data"]["productVariant"]def test_variant_digital_content(