Skip to content

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

Fix gifts validation (#15393)

Iga Karbowiake7cf6e0f5d3b
diff --git a/saleor/graphql/discount/mutations/promotion/validators.py b/saleor/graphql/discount/mutations/promotion/validators.py
index 78523a4fb6..4649bb8a95 100644
--- a/saleor/graphql/discount/mutations/promotion/validators.py
+++ b/saleor/graphql/discount/mutations/promotion/validators.py
@@ -68,7 +68,8 @@ def _get_gift_ids(cleaned_input, instance):
return
if "gifts" in cleaned_input:
- return {gift.id for gift in cleaned_input["gifts"]}
+ gifts = cleaned_input["gifts"] or []
+ return {gift.id for gift in gifts}
else:
# this part is only for PromotionRuleUpdate mutation
# so the gifts will be fetched once
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 9115ff76df..b58afbb7b9 100644
--- a/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py
+++ b/saleor/graphql/discount/tests/mutations/test_promotion_rule_create.py
@@ -116,6 +116,7 @@ def test_promotion_rule_create_by_staff_user(
"rewardValueType": reward_value_type,
"rewardValue": reward_value,
"cataloguePredicate": catalogue_predicate,
+ "gifts": None,
}
}
@@ -1693,6 +1694,51 @@ def test_promotion_rule_create_gift_promotion_wrong_gift_instance(
assert promotion.rules.count() == rules_count
+def test_promotion_rule_create_gift_promotion_no_gifts(
+ staff_api_client,
+ permission_group_manage_discounts,
+ channel_USD,
+ order_promotion_without_rules,
+ product_list,
+):
+ # given
+ promotion = order_promotion_without_rules
+ permission_group_manage_discounts.user_set.add(staff_api_client.user)
+ rules_count = promotion.rules.count()
+ channel_id = graphene.Node.to_global_id("Channel", channel_USD.pk)
+ name = "test promotion rule"
+ reward_type = RewardTypeEnum.GIFT.name
+ promotion_id = graphene.Node.to_global_id("Promotion", promotion.id)
+ order_predicate = {
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": "100"}}}
+ }
+
+ variables = {
+ "input": {
+ "name": name,
+ "promotion": promotion_id,
+ "channels": [channel_id],
+ "rewardType": reward_type,
+ "orderPredicate": order_predicate,
+ "gifts": None,
+ }
+ }
+
+ # when
+ response = staff_api_client.post_graphql(PROMOTION_RULE_CREATE_MUTATION, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["promotionRuleCreate"]
+ errors = data["errors"]
+
+ assert not data["promotionRule"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == PromotionRuleCreateErrorCode.REQUIRED.name
+ assert errors[0]["field"] == "gifts"
+ assert promotion.rules.count() == rules_count
+
+
def test_promotion_rule_create_gift_promotion_with_reward_value(
staff_api_client,
permission_group_manage_discounts,