add validation to ItemCategorySerializer

This commit is contained in:
Mira Weller
2024-07-26 20:05:03 +02:00
parent 537a0993b0
commit badbb64f4f
2 changed files with 50 additions and 0 deletions

View File

@@ -447,6 +447,18 @@ class ItemCategorySerializer(I18nAwareModelSerializer):
'cross_selling_condition', 'cross_selling_match_products' 'cross_selling_condition', 'cross_selling_match_products'
) )
def validate(self, data):
data = super().validate(data)
full_data = self.to_internal_value(self.to_representation(self.instance)) if self.instance else {}
full_data.update(data)
if full_data.get('is_addon') and full_data.get('cross_selling_mode'):
raise ValidationError('is_addon and cross_selling_mode are mutually exclusive')
return data
class QuestionOptionSerializer(I18nAwareModelSerializer): class QuestionOptionSerializer(I18nAwareModelSerializer):
identifier = serializers.CharField(allow_null=True) identifier = serializers.CharField(allow_null=True)

View File

@@ -214,6 +214,44 @@ def test_category_update(token_client, organizer, event, team, category):
assert ItemCategory.objects.get(pk=category.pk).name == {"en": "Test"} assert ItemCategory.objects.get(pk=category.pk).name == {"en": "Test"}
@pytest.mark.django_db
def test_category_update_cross_selling_options(token_client, organizer, event, team, category):
resp = token_client.patch(
'/api/v1/organizers/{}/events/{}/categories/{}/'.format(organizer.slug, event.slug, category.pk),
{
"cross_selling_mode": "both",
},
format='json'
)
assert resp.status_code == 200
with scopes_disabled():
assert ItemCategory.objects.get(pk=category.pk).cross_selling_mode == 'both'
resp = token_client.patch(
'/api/v1/organizers/{}/events/{}/categories/{}/'.format(organizer.slug, event.slug, category.pk),
{
"cross_selling_mode": "something",
},
format='json'
)
assert resp.status_code == 400
with scopes_disabled():
assert ItemCategory.objects.get(pk=category.pk).cross_selling_mode == 'both'
resp = token_client.patch(
'/api/v1/organizers/{}/events/{}/categories/{}/'.format(organizer.slug, event.slug, category.pk),
{
"is_addon": True,
},
format='json'
)
assert resp.status_code == 400
assert 'mutually exclusive' in str(resp.data)
with scopes_disabled():
assert ItemCategory.objects.get(pk=category.pk).cross_selling_mode == 'both'
assert ItemCategory.objects.get(pk=category.pk).is_addon is False
@pytest.mark.django_db @pytest.mark.django_db
def test_category_update_wrong_event(token_client, organizer, event2, category): def test_category_update_wrong_event(token_client, organizer, event2, category):
resp = token_client.patch( resp = token_client.patch(