Skip to content

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

Fix error for invalid promotion ID in promotionRuleCreate (#15498)

Iga Karbowiak952a07b7b231
diff --git a/saleor/graphql/discount/mutations/promotion/promotion_rule_create.py b/saleor/graphql/discount/mutations/promotion/promotion_rule_create.py
index e856212db5..38e95e022e 100644
--- a/saleor/graphql/discount/mutations/promotion/promotion_rule_create.py
+++ b/saleor/graphql/discount/mutations/promotion/promotion_rule_create.py
@@ -25,7 +25,8 @@ from .validators import clean_promotion_rule
class PromotionRuleCreateInput(PromotionRuleInput):
promotion = graphene.ID(
- description="The ID of the promotion that rule belongs to.", required=True
+ description="The ID of the promotion that rule belongs to.",
+ required=True,
)
@@ -69,12 +70,15 @@ class PromotionRuleCreate(ModelMutation):
def clean_input(
cls, info: ResolveInfo, instance: models.PromotionRule, data: dict, **kwargs
):
+ promotion_value = data.pop("promotion")
cleaned_input = super().clean_input(info, instance, data, **kwargs)
- errors: defaultdict[str, list[ValidationError]] = defaultdict(list)
-
- promotion = cleaned_input["promotion"]
- promotion_type = promotion.type
+ promotion = cls.get_node_or_error(
+ info, promotion_value, field="promotion", only_type="Promotion"
+ )
+ cleaned_input["promotion"] = promotion
+ promotion_type = promotion.type # type: ignore[union-attr]
+ errors: defaultdict[str, list[ValidationError]] = defaultdict(list)
clean_promotion_rule(
cleaned_input,
promotion_type,
diff --git a/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py b/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py
index b58afbb7b9..4490ff3a46 100644
--- a/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py
+++ b/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py
@@ -1883,3 +1883,52 @@ def test_promotion_rule_create_gift_promotion_missing_gifts(
assert errors[0]["code"] == PromotionRuleCreateErrorCode.REQUIRED.name
assert errors[0]["field"] == "gifts"
assert promotion.rules.count() == rules_count
+
+
+def test_promotion_rule_create_invalid_promotion_id(
+ staff_api_client,
+ permission_group_manage_discounts,
+ channel_USD,
+ variant,
+ catalogue_promotion,
+ promotion_rule,
+):
+ # given
+ permission_group_manage_discounts.user_set.add(staff_api_client.user)
+
+ channel_ids = [graphene.Node.to_global_id("Channel", channel_USD.pk)]
+ catalogue_predicate = {
+ "variantPredicate": {
+ "ids": [graphene.Node.to_global_id("ProductVariant", variant.id)]
+ }
+ }
+ name = "test promotion rule"
+ reward_value = Decimal("10")
+ reward_value_type = RewardValueTypeEnum.PERCENTAGE.name
+ invalid_promotion_id = graphene.Node.to_global_id(
+ "PromotionRule", promotion_rule.id
+ )
+
+ variables = {
+ "input": {
+ "name": name,
+ "promotion": invalid_promotion_id,
+ "channels": channel_ids,
+ "rewardValueType": reward_value_type,
+ "rewardValue": reward_value,
+ "cataloguePredicate": catalogue_predicate,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(PROMOTION_RULE_CREATE_MUTATION, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["promotionRuleCreate"]
+ rule_data = data["promotionRule"]
+
+ assert not rule_data
+ assert len(data["errors"]) == 1
+ assert data["errors"][0]["field"] == "promotion"
+ assert data["errors"][0]["code"] == PromotionRuleCreateErrorCode.GRAPHQL_ERROR.name