django-rest-framework/tests/test_validation.py

258 lines
8.1 KiB
Python
Raw Normal View History

2013-02-23 02:13:06 +04:00
from __future__ import unicode_literals
2015-06-25 23:55:51 +03:00
import re
from django.core.validators import MaxValueValidator, RegexValidator
2013-02-23 02:13:06 +04:00
from django.db import models
from django.test import TestCase
from django.utils import six
2015-06-25 23:55:51 +03:00
2014-10-17 16:23:14 +04:00
from rest_framework import generics, serializers, status
2013-06-28 20:17:39 +04:00
from rest_framework.test import APIRequestFactory
2013-02-23 02:13:06 +04:00
2013-06-28 20:17:39 +04:00
factory = APIRequestFactory()
2013-02-23 02:13:06 +04:00
# Regression for #666
class ValidationModel(models.Model):
blank_validated_field = models.CharField(max_length=255)
class ValidationModelSerializer(serializers.ModelSerializer):
class Meta:
model = ValidationModel
fields = ('blank_validated_field',)
read_only_fields = ('blank_validated_field',)
class UpdateValidationModel(generics.RetrieveUpdateDestroyAPIView):
queryset = ValidationModel.objects.all()
2013-02-23 02:13:06 +04:00
serializer_class = ValidationModelSerializer
# Regression for #653
class ShouldValidateModel(models.Model):
should_validate_field = models.CharField(max_length=255)
class ShouldValidateModelSerializer(serializers.ModelSerializer):
renamed = serializers.CharField(source='should_validate_field', required=False)
2014-09-05 19:29:46 +04:00
def validate_renamed(self, value):
if len(value) < 3:
2014-10-17 16:23:14 +04:00
raise serializers.ValidationError('Minimum 3 characters.')
2014-09-05 19:29:46 +04:00
return value
2013-02-23 02:13:06 +04:00
class Meta:
model = ShouldValidateModel
fields = ('renamed',)
class TestNestedValidationError(TestCase):
def test_nested_validation_error_detail(self):
"""
Ensure nested validation error detail is rendered correctly.
"""
e = serializers.ValidationError({
'nested': {
'field': ['error'],
}
})
2016-11-30 12:48:33 +03:00
assert serializers.as_serializer_error(e) == {
'nested': {
'field': ['error'],
}
2016-11-30 12:48:33 +03:00
}
2013-12-20 19:53:06 +04:00
class TestPreSaveValidationExclusionsSerializer(TestCase):
2013-02-23 02:13:06 +04:00
def test_renamed_fields_are_model_validated(self):
"""
Ensure fields with 'source' applied do get still get model validation.
"""
# We've set `required=False` on the serializer, but the model
# does not have `blank=True`, so this serializer should not validate.
serializer = ShouldValidateModelSerializer(data={'renamed': ''})
2016-11-30 12:48:33 +03:00
assert serializer.is_valid() is False
assert 'renamed' in serializer.errors
assert 'should_validate_field' not in serializer.errors
class TestCustomValidationMethods(TestCase):
def test_custom_validation_method_is_executed(self):
serializer = ShouldValidateModelSerializer(data={'renamed': 'fo'})
2016-11-30 12:48:33 +03:00
assert not serializer.is_valid()
assert 'renamed' in serializer.errors
def test_custom_validation_method_passing(self):
serializer = ShouldValidateModelSerializer(data={'renamed': 'foo'})
2016-11-30 12:48:33 +03:00
assert serializer.is_valid()
2013-05-30 11:07:30 +04:00
class ValidationSerializer(serializers.Serializer):
foo = serializers.CharField()
def validate_foo(self, attrs, source):
2014-10-17 16:23:14 +04:00
raise serializers.ValidationError("foo invalid")
2013-05-30 11:07:30 +04:00
def validate(self, attrs):
2014-10-17 16:23:14 +04:00
raise serializers.ValidationError("serializer invalid")
2013-05-30 11:07:30 +04:00
class TestAvoidValidation(TestCase):
"""
If serializer was initialized with invalid data (None or non dict-like), it
should avoid validation layer (validate_<field> and validate methods)
"""
def test_serializer_errors_has_only_invalid_data_error(self):
serializer = ValidationSerializer(data='invalid data')
2016-11-30 12:48:33 +03:00
assert not serializer.is_valid()
assert serializer.errors == {
'non_field_errors': [
'Invalid data. Expected a dictionary, but got %s.' % six.text_type.__name__
]
2016-11-30 12:48:33 +03:00
}
# regression tests for issue: 1493
class ValidationMaxValueValidatorModel(models.Model):
number_value = models.PositiveIntegerField(validators=[MaxValueValidator(100)])
class ValidationMaxValueValidatorModelSerializer(serializers.ModelSerializer):
class Meta:
model = ValidationMaxValueValidatorModel
fields = '__all__'
class UpdateMaxValueValidationModel(generics.RetrieveUpdateDestroyAPIView):
queryset = ValidationMaxValueValidatorModel.objects.all()
serializer_class = ValidationMaxValueValidatorModelSerializer
class TestMaxValueValidatorValidation(TestCase):
def test_max_value_validation_serializer_success(self):
serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 99})
2016-11-30 12:48:33 +03:00
assert serializer.is_valid()
def test_max_value_validation_serializer_fails(self):
serializer = ValidationMaxValueValidatorModelSerializer(data={'number_value': 101})
2016-11-30 12:48:33 +03:00
assert not serializer.is_valid()
assert serializer.errors == {
'number_value': [
'Ensure this value is less than or equal to 100.'
]
}
def test_max_value_validation_success(self):
obj = ValidationMaxValueValidatorModel.objects.create(number_value=100)
request = factory.patch('/{0}'.format(obj.pk), {'number_value': 98}, format='json')
view = UpdateMaxValueValidationModel().as_view()
response = view(request, pk=obj.pk).render()
2016-11-30 12:48:33 +03:00
assert response.status_code == status.HTTP_200_OK
def test_max_value_validation_fail(self):
obj = ValidationMaxValueValidatorModel.objects.create(number_value=100)
request = factory.patch('/{0}'.format(obj.pk), {'number_value': 101}, format='json')
view = UpdateMaxValueValidationModel().as_view()
response = view(request, pk=obj.pk).render()
2016-11-30 12:48:33 +03:00
assert response.content == b'{"number_value":["Ensure this value is less than or equal to 100."]}'
assert response.status_code == status.HTTP_400_BAD_REQUEST
# regression tests for issue: 1533
class TestChoiceFieldChoicesValidate(TestCase):
CHOICES = [
(0, 'Small'),
(1, 'Medium'),
(2, 'Large'),
]
SINGLE_CHOICES = [0, 1, 2]
CHOICES_NESTED = [
('Category', (
(1, 'First'),
(2, 'Second'),
(3, 'Third'),
)),
(4, 'Fourth'),
]
MIXED_CHOICES = [
('Category', (
(1, 'First'),
(2, 'Second'),
)),
3,
(4, 'Fourth'),
]
def test_choices(self):
"""
Make sure a value for choices works as expected.
"""
f = serializers.ChoiceField(choices=self.CHOICES)
value = self.CHOICES[0][0]
try:
f.to_internal_value(value)
2014-10-17 16:23:14 +04:00
except serializers.ValidationError:
self.fail("Value %s does not validate" % str(value))
def test_single_choices(self):
"""
Make sure a single value for choices works as expected.
"""
f = serializers.ChoiceField(choices=self.SINGLE_CHOICES)
value = self.SINGLE_CHOICES[0]
try:
f.to_internal_value(value)
except serializers.ValidationError:
self.fail("Value %s does not validate" % str(value))
def test_nested_choices(self):
"""
Make sure a nested value for choices works as expected.
"""
f = serializers.ChoiceField(choices=self.CHOICES_NESTED)
value = self.CHOICES_NESTED[0][1][0][0]
try:
f.to_internal_value(value)
except serializers.ValidationError:
self.fail("Value %s does not validate" % str(value))
def test_mixed_choices(self):
"""
Make sure mixed values for choices works as expected.
"""
f = serializers.ChoiceField(choices=self.MIXED_CHOICES)
value = self.MIXED_CHOICES[1]
try:
f.to_internal_value(value)
except serializers.ValidationError:
self.fail("Value %s does not validate" % str(value))
class RegexSerializer(serializers.Serializer):
pin = serializers.CharField(
validators=[RegexValidator(regex=re.compile('^[0-9]{4,6}$'),
message='A PIN is 4-6 digits')])
expected_repr = """
RegexSerializer():
pin = CharField(validators=[<django.core.validators.RegexValidator object>])
""".strip()
class TestRegexSerializer(TestCase):
def test_regex_repr(self):
serializer_repr = repr(RegexSerializer())
assert serializer_repr == expected_repr