Skip to content

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

Adjust error message for checking permissions for customer price (#18305)

Iga Karbowiak45331bfa801d
diff --git a/saleor/graphql/checkout/mutations/utils.py b/saleor/graphql/checkout/mutations/utils.py
index 9695c2d7f4..d7dcf5ea5b 100644
--- a/saleor/graphql/checkout/mutations/utils.py
+++ b/saleor/graphql/checkout/mutations/utils.py
@@ -490,7 +490,10 @@ def check_permissions_for_custom_prices(app, lines):
if any("price" in line for line in lines) and (
not app or not app.has_perm(CheckoutPermissions.HANDLE_CHECKOUTS)
):
- raise PermissionDenied(permissions=[CheckoutPermissions.HANDLE_CHECKOUTS])
+ raise PermissionDenied(
+ message="Setting the custom price is allowed only for apps with `MANAGE_CHECKOUTS` permission.",
+ permissions=[CheckoutPermissions.HANDLE_CHECKOUTS],
+ )
def find_line_id_when_variant_parameter_used(
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_create.py b/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
index 9b56884753..c5a20646d0 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_create.py
@@ -19,7 +19,11 @@ from .....core.models import EventDelivery
from .....product.models import ProductChannelListing, ProductVariantChannelListing
from .....warehouse.models import Reservation, Stock
from .....webhook.event_types import WebhookEventAsyncType, WebhookEventSyncType
-from ....tests.utils import assert_no_permission, get_graphql_content
+from ....tests.utils import (
+ assert_no_permission,
+ get_graphql_content,
+ get_graphql_content_from_response,
+)
MUTATION_CHECKOUT_CREATE = """
mutation createCheckout($checkoutInput: CheckoutCreateInput!) {
@@ -888,6 +892,7 @@ def test_checkout_create_with_custom_price_by_app_no_perm(
app_api_client, stock, graphql_address_data, channel_USD
):
"""Ensure that app without handle checkouts permission cannot set custom price."""
+ # given
variant = stock.product_variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
test_email = "[email protected]"
@@ -902,8 +907,17 @@ def test_checkout_create_with_custom_price_by_app_no_perm(
}
}
assert not Checkout.objects.exists()
+
+ # when
response = app_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ # then
assert_no_permission(response)
+ content = get_graphql_content_from_response(response)
+ assert (
+ "Setting the custom price is allowed only for apps with `MANAGE_CHECKOUTS` permission."
+ == content["errors"][0]["message"]
+ )
def test_checkout_create_with_custom_price_by_staff_with_handle_checkouts(
@@ -914,6 +928,7 @@ def test_checkout_create_with_custom_price_by_staff_with_handle_checkouts(
permission_handle_checkouts,
):
"""Ensure that staff with handle checkouts permission cannot set custom price."""
+ # given
staff_api_client.user.user_permissions.add(permission_handle_checkouts)
variant = stock.product_variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
@@ -929,8 +944,17 @@ def test_checkout_create_with_custom_price_by_staff_with_handle_checkouts(
}
}
assert not Checkout.objects.exists()
+
+ # when
response = staff_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)
+
+ # then
assert_no_permission(response)
+ content = get_graphql_content_from_response(response)
+ assert (
+ "Setting the custom price is allowed only for apps with `MANAGE_CHECKOUTS` permission."
+ == content["errors"][0]["message"]
+ )
def test_checkout_create_no_email(api_client, stock, graphql_address_data, channel_USD):
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
index 20f0768c9f..66e615841f 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_lines_add.py
@@ -37,6 +37,7 @@ from ....core.utils import to_global_id_or_none
from ....tests.utils import (
assert_no_permission,
get_graphql_content,
+ get_graphql_content_from_response,
)
from ...mutations.utils import update_checkout_shipping_method_if_invalid
@@ -1525,6 +1526,7 @@ def test_checkout_lines_add_custom_price_and_order_percentage_discount(
def test_checkout_lines_add_custom_price_app_no_perm(app_api_client, checkout, stock):
+ # given
variant = stock.product_variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
price = Decimal("13.11")
@@ -1534,13 +1536,23 @@ def test_checkout_lines_add_custom_price_app_no_perm(app_api_client, checkout, s
"lines": [{"variantId": variant_id, "quantity": 1, "price": price}],
"channelSlug": checkout.channel.slug,
}
+
+ # when
response = app_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)
+
+ # then
assert_no_permission(response)
+ content = get_graphql_content_from_response(response)
+ assert (
+ "Setting the custom price is allowed only for apps with `MANAGE_CHECKOUTS` permission."
+ == content["errors"][0]["message"]
+ )
def test_checkout_lines_add_custom_price_permission_denied_for_staff_user(
staff_api_client, checkout, stock, permission_handle_checkouts
):
+ # given
variant = stock.product_variant
variant_id = graphene.Node.to_global_id("ProductVariant", variant.pk)
price = Decimal("13.11")
@@ -1550,12 +1562,21 @@ def test_checkout_lines_add_custom_price_permission_denied_for_staff_user(
"lines": [{"variantId": variant_id, "quantity": 1, "price": price}],
"channelSlug": checkout.channel.slug,
}
+
+ # when
response = staff_api_client.post_graphql(
MUTATION_CHECKOUT_LINES_ADD,
variables,
permissions=[permission_handle_checkouts],
)
+
+ # then
assert_no_permission(response)
+ content = get_graphql_content_from_response(response)
+ assert (
+ "Setting the custom price is allowed only for apps with `MANAGE_CHECKOUTS` permission."
+ == content["errors"][0]["message"]
+ )
def test_checkout_lines_add_existing_variant_over_allowed_stock(