2011-02-05 00:52:21 +03:00
|
|
|
from django import forms
|
2011-02-19 13:26:27 +03:00
|
|
|
from django.db import models
|
2011-02-05 00:52:21 +03:00
|
|
|
from django.test import TestCase
|
2011-02-19 13:26:27 +03:00
|
|
|
from djangorestframework.compat import RequestFactory
|
2011-05-16 19:52:39 +04:00
|
|
|
from djangorestframework.resources import Resource, FormResource, ModelResource
|
2011-04-11 19:38:00 +04:00
|
|
|
from djangorestframework.response import ErrorResponse
|
2011-05-04 12:21:17 +04:00
|
|
|
from djangorestframework.views import BaseView
|
2011-05-12 18:11:14 +04:00
|
|
|
from djangorestframework.resources import Resource
|
2011-02-05 00:52:21 +03:00
|
|
|
|
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
class TestDisabledValidations(TestCase):
|
2011-04-11 18:03:49 +04:00
|
|
|
"""Tests on FormValidator with validation disabled by setting form to None"""
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_disabled_form_validator_returns_content_unchanged(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
"""If the view's form attribute is None then FormValidator(view).validate_request(content, None)
|
2011-04-11 18:03:49 +04:00
|
|
|
should just return the content unmodified."""
|
2011-05-16 19:52:39 +04:00
|
|
|
class DisabledFormResource(FormResource):
|
2011-02-19 13:26:27 +03:00
|
|
|
form = None
|
|
|
|
|
2011-05-16 19:52:39 +04:00
|
|
|
class MockView(BaseView):
|
|
|
|
resource = DisabledFormResource
|
|
|
|
|
|
|
|
view = MockView()
|
2011-02-05 00:52:21 +03:00
|
|
|
content = {'qwerty':'uiop'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertEqual(FormResource(view).validate_request(content, None), content)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_disabled_form_validator_get_bound_form_returns_none(self):
|
2011-04-11 18:03:49 +04:00
|
|
|
"""If the view's form attribute is None on then
|
|
|
|
FormValidator(view).get_bound_form(content) should just return None."""
|
2011-05-16 19:52:39 +04:00
|
|
|
class DisabledFormResource(FormResource):
|
2011-02-19 13:26:27 +03:00
|
|
|
form = None
|
2011-02-05 00:52:21 +03:00
|
|
|
|
2011-05-16 19:52:39 +04:00
|
|
|
class MockView(BaseView):
|
|
|
|
resource = DisabledFormResource
|
|
|
|
|
|
|
|
view = MockView()
|
2011-04-11 18:03:49 +04:00
|
|
|
content = {'qwerty':'uiop'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertEqual(FormResource(view).get_bound_form(content), None)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
2011-04-11 18:03:49 +04:00
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
def test_disabled_model_form_validator_returns_content_unchanged(self):
|
2011-05-04 12:21:17 +04:00
|
|
|
"""If the view's form is None and does not have a Resource with a model set then
|
2011-05-16 19:52:39 +04:00
|
|
|
ModelFormValidator(view).validate_request(content, None) should just return the content unmodified."""
|
|
|
|
|
2011-05-04 12:21:17 +04:00
|
|
|
class DisabledModelFormView(BaseView):
|
2011-05-16 19:52:39 +04:00
|
|
|
resource = ModelResource
|
2011-02-19 13:26:27 +03:00
|
|
|
|
2011-04-11 18:03:49 +04:00
|
|
|
view = DisabledModelFormView()
|
2011-02-19 13:26:27 +03:00
|
|
|
content = {'qwerty':'uiop'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertEqual(ModelResource(view).get_bound_form(content), None)#
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_disabled_model_form_validator_get_bound_form_returns_none(self):
|
|
|
|
"""If the form attribute is None on FormValidatorMixin then get_bound_form(content) should just return None."""
|
2011-05-04 12:21:17 +04:00
|
|
|
class DisabledModelFormView(BaseView):
|
2011-05-16 19:52:39 +04:00
|
|
|
resource = ModelResource
|
2011-04-11 18:03:49 +04:00
|
|
|
|
|
|
|
view = DisabledModelFormView()
|
|
|
|
content = {'qwerty':'uiop'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertEqual(ModelResource(view).get_bound_form(content), None)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
class TestNonFieldErrors(TestCase):
|
|
|
|
"""Tests against form validation errors caused by non-field errors. (eg as might be caused by some custom form validation)"""
|
|
|
|
|
|
|
|
def test_validate_failed_due_to_non_field_error_returns_appropriate_message(self):
|
|
|
|
"""If validation fails with a non-field error, ensure the response a non-field error"""
|
|
|
|
class MockForm(forms.Form):
|
|
|
|
field1 = forms.CharField(required=False)
|
|
|
|
field2 = forms.CharField(required=False)
|
|
|
|
ERROR_TEXT = 'You may not supply both field1 and field2'
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if 'field1' in self.cleaned_data and 'field2' in self.cleaned_data:
|
|
|
|
raise forms.ValidationError(self.ERROR_TEXT)
|
|
|
|
return self.cleaned_data #pragma: no cover
|
|
|
|
|
2011-05-16 19:52:39 +04:00
|
|
|
class MockResource(FormResource):
|
2011-02-19 13:26:27 +03:00
|
|
|
form = MockForm
|
|
|
|
|
2011-05-16 19:52:39 +04:00
|
|
|
class MockView(BaseView):
|
|
|
|
pass
|
|
|
|
|
2011-04-11 18:03:49 +04:00
|
|
|
view = MockView()
|
2011-02-19 13:26:27 +03:00
|
|
|
content = {'field1': 'example1', 'field2': 'example2'}
|
|
|
|
try:
|
2011-05-16 19:52:39 +04:00
|
|
|
MockResource(view).validate_request(content, None)
|
2011-04-11 19:38:00 +04:00
|
|
|
except ErrorResponse, exc:
|
2011-02-19 13:26:27 +03:00
|
|
|
self.assertEqual(exc.response.raw_content, {'errors': [MockForm.ERROR_TEXT]})
|
|
|
|
else:
|
2011-05-04 12:21:17 +04:00
|
|
|
self.fail('ErrorResponse was not raised') #pragma: no cover
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestFormValidation(TestCase):
|
|
|
|
"""Tests which check basic form validation.
|
|
|
|
Also includes the same set of tests with a ModelFormValidator for which the form has been explicitly set.
|
2011-04-11 18:03:49 +04:00
|
|
|
(ModelFormValidator should behave as FormValidator if a form is set rather than relying on the default ModelForm)"""
|
2011-02-19 13:26:27 +03:00
|
|
|
def setUp(self):
|
|
|
|
class MockForm(forms.Form):
|
|
|
|
qwerty = forms.CharField(required=True)
|
2011-02-05 00:52:21 +03:00
|
|
|
|
2011-05-16 19:52:39 +04:00
|
|
|
class MockFormResource(FormResource):
|
|
|
|
form = MockForm
|
|
|
|
|
|
|
|
class MockModelResource(ModelResource):
|
2011-02-19 13:26:27 +03:00
|
|
|
form = MockForm
|
2011-05-16 19:52:39 +04:00
|
|
|
|
|
|
|
class MockFormView(BaseView):
|
|
|
|
resource = MockFormResource
|
2011-04-11 18:03:49 +04:00
|
|
|
|
2011-05-04 12:21:17 +04:00
|
|
|
class MockModelFormView(BaseView):
|
2011-05-16 19:52:39 +04:00
|
|
|
resource = MockModelResource
|
|
|
|
|
|
|
|
self.MockFormResource = MockFormResource
|
|
|
|
self.MockModelResource = MockModelResource
|
2011-04-11 18:03:49 +04:00
|
|
|
self.MockFormView = MockFormView
|
|
|
|
self.MockModelFormView = MockModelFormView
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
def validation_returns_content_unchanged_if_already_valid_and_clean(self, validator):
|
2011-02-05 00:52:21 +03:00
|
|
|
"""If the content is already valid and clean then validate(content) should just return the content unmodified."""
|
|
|
|
content = {'qwerty':'uiop'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertEqual(validator.validate_request(content, None), content)
|
2011-02-05 00:52:21 +03:00
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
def validation_failure_raises_response_exception(self, validator):
|
2011-02-05 00:52:21 +03:00
|
|
|
"""If form validation fails a ResourceException 400 (Bad Request) should be raised."""
|
2011-05-16 19:52:39 +04:00
|
|
|
content = {}
|
|
|
|
self.assertRaises(ErrorResponse, validator.validate_request, content, None)
|
2011-02-05 00:52:21 +03:00
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
def validation_does_not_allow_extra_fields_by_default(self, validator):
|
2011-02-05 00:52:21 +03:00
|
|
|
"""If some (otherwise valid) content includes fields that are not in the form then validation should fail.
|
|
|
|
It might be okay on normal form submission, but for Web APIs we oughta get strict, as it'll help show up
|
|
|
|
broken clients more easily (eg submitting content with a misnamed field)"""
|
|
|
|
content = {'qwerty': 'uiop', 'extra': 'extra'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertRaises(ErrorResponse, validator.validate_request, content, None)
|
2011-02-05 00:52:21 +03:00
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
def validation_allows_extra_fields_if_explicitly_set(self, validator):
|
|
|
|
"""If we include an allowed_extra_fields paramater on _validate, then allow fields with those names."""
|
2011-02-05 00:52:21 +03:00
|
|
|
content = {'qwerty': 'uiop', 'extra': 'extra'}
|
2011-05-16 19:52:39 +04:00
|
|
|
validator._validate(content, None, allowed_extra_fields=('extra',))
|
2011-02-05 00:52:21 +03:00
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
def validation_does_not_require_extra_fields_if_explicitly_set(self, validator):
|
|
|
|
"""If we include an allowed_extra_fields paramater on _validate, then do not fail if we do not have fields with those names."""
|
2011-02-05 00:52:21 +03:00
|
|
|
content = {'qwerty': 'uiop'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertEqual(validator._validate(content, None, allowed_extra_fields=('extra',)), content)
|
2011-02-05 00:52:21 +03:00
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
def validation_failed_due_to_no_content_returns_appropriate_message(self, validator):
|
2011-02-05 00:52:21 +03:00
|
|
|
"""If validation fails due to no content, ensure the response contains a single non-field error"""
|
|
|
|
content = {}
|
|
|
|
try:
|
2011-05-16 19:52:39 +04:00
|
|
|
validator.validate_request(content, None)
|
2011-04-11 19:38:00 +04:00
|
|
|
except ErrorResponse, exc:
|
2011-04-02 19:32:37 +04:00
|
|
|
self.assertEqual(exc.response.raw_content, {'field-errors': {'qwerty': ['This field is required.']}})
|
2011-02-05 00:52:21 +03:00
|
|
|
else:
|
|
|
|
self.fail('ResourceException was not raised') #pragma: no cover
|
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
def validation_failed_due_to_field_error_returns_appropriate_message(self, validator):
|
2011-02-05 00:52:21 +03:00
|
|
|
"""If validation fails due to a field error, ensure the response contains a single field error"""
|
|
|
|
content = {'qwerty': ''}
|
|
|
|
try:
|
2011-05-16 19:52:39 +04:00
|
|
|
validator.validate_request(content, None)
|
2011-04-11 19:38:00 +04:00
|
|
|
except ErrorResponse, exc:
|
2011-02-05 00:52:21 +03:00
|
|
|
self.assertEqual(exc.response.raw_content, {'field-errors': {'qwerty': ['This field is required.']}})
|
|
|
|
else:
|
|
|
|
self.fail('ResourceException was not raised') #pragma: no cover
|
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
def validation_failed_due_to_invalid_field_returns_appropriate_message(self, validator):
|
2011-02-05 00:52:21 +03:00
|
|
|
"""If validation fails due to an invalid field, ensure the response contains a single field error"""
|
|
|
|
content = {'qwerty': 'uiop', 'extra': 'extra'}
|
|
|
|
try:
|
2011-05-16 19:52:39 +04:00
|
|
|
validator.validate_request(content, None)
|
2011-04-11 19:38:00 +04:00
|
|
|
except ErrorResponse, exc:
|
2011-02-05 00:52:21 +03:00
|
|
|
self.assertEqual(exc.response.raw_content, {'field-errors': {'extra': ['This field does not exist.']}})
|
|
|
|
else:
|
|
|
|
self.fail('ResourceException was not raised') #pragma: no cover
|
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
def validation_failed_due_to_multiple_errors_returns_appropriate_message(self, validator):
|
2011-02-05 00:52:21 +03:00
|
|
|
"""If validation for multiple reasons, ensure the response contains each error"""
|
|
|
|
content = {'qwerty': '', 'extra': 'extra'}
|
|
|
|
try:
|
2011-05-16 19:52:39 +04:00
|
|
|
validator.validate_request(content, None)
|
2011-04-11 19:38:00 +04:00
|
|
|
except ErrorResponse, exc:
|
2011-02-05 00:52:21 +03:00
|
|
|
self.assertEqual(exc.response.raw_content, {'field-errors': {'qwerty': ['This field is required.'],
|
|
|
|
'extra': ['This field does not exist.']}})
|
|
|
|
else:
|
|
|
|
self.fail('ResourceException was not raised') #pragma: no cover
|
2011-02-19 13:26:27 +03:00
|
|
|
|
2011-05-16 19:52:39 +04:00
|
|
|
# Tests on FormResource
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_form_validation_returns_content_unchanged_if_already_valid_and_clean(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockFormResource(self.MockFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_returns_content_unchanged_if_already_valid_and_clean(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_form_validation_failure_raises_response_exception(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockFormResource(self.MockFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failure_raises_response_exception(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_validation_does_not_allow_extra_fields_by_default(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockFormResource(self.MockFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_does_not_allow_extra_fields_by_default(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_validation_allows_extra_fields_if_explicitly_set(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockFormResource(self.MockFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_allows_extra_fields_if_explicitly_set(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_validation_does_not_require_extra_fields_if_explicitly_set(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockFormResource(self.MockFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_does_not_require_extra_fields_if_explicitly_set(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_validation_failed_due_to_no_content_returns_appropriate_message(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockFormResource(self.MockFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failed_due_to_no_content_returns_appropriate_message(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_validation_failed_due_to_field_error_returns_appropriate_message(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockFormResource(self.MockFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failed_due_to_field_error_returns_appropriate_message(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_validation_failed_due_to_invalid_field_returns_appropriate_message(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockFormResource(self.MockFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failed_due_to_invalid_field_returns_appropriate_message(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_validation_failed_due_to_multiple_errors_returns_appropriate_message(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockFormResource(self.MockFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failed_due_to_multiple_errors_returns_appropriate_message(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
2011-05-16 19:52:39 +04:00
|
|
|
# Same tests on ModelResource
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_modelform_validation_returns_content_unchanged_if_already_valid_and_clean(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockModelResource(self.MockModelFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_returns_content_unchanged_if_already_valid_and_clean(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_modelform_validation_failure_raises_response_exception(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockModelResource(self.MockModelFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failure_raises_response_exception(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_modelform_validation_does_not_allow_extra_fields_by_default(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockModelResource(self.MockModelFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_does_not_allow_extra_fields_by_default(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_modelform_validation_allows_extra_fields_if_explicitly_set(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockModelResource(self.MockModelFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_allows_extra_fields_if_explicitly_set(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_modelform_validation_does_not_require_extra_fields_if_explicitly_set(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockModelResource(self.MockModelFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_does_not_require_extra_fields_if_explicitly_set(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_modelform_validation_failed_due_to_no_content_returns_appropriate_message(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockModelResource(self.MockModelFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failed_due_to_no_content_returns_appropriate_message(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_modelform_validation_failed_due_to_field_error_returns_appropriate_message(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockModelResource(self.MockModelFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failed_due_to_field_error_returns_appropriate_message(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_modelform_validation_failed_due_to_invalid_field_returns_appropriate_message(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockModelResource(self.MockModelFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failed_due_to_invalid_field_returns_appropriate_message(validator)
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
def test_modelform_validation_failed_due_to_multiple_errors_returns_appropriate_message(self):
|
2011-05-16 19:52:39 +04:00
|
|
|
validator = self.MockModelResource(self.MockModelFormView())
|
2011-04-11 18:03:49 +04:00
|
|
|
self.validation_failed_due_to_multiple_errors_returns_appropriate_message(validator)
|
|
|
|
|
|
|
|
|
2011-04-11 18:06:29 +04:00
|
|
|
class TestModelFormValidator(TestCase):
|
|
|
|
"""Tests specific to ModelFormValidatorMixin"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Create a validator for a model with two fields and a property."""
|
|
|
|
class MockModel(models.Model):
|
|
|
|
qwerty = models.CharField(max_length=256)
|
|
|
|
uiop = models.CharField(max_length=256, blank=True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def readonly(self):
|
|
|
|
return 'read only'
|
2011-05-04 12:21:17 +04:00
|
|
|
|
2011-05-16 19:52:39 +04:00
|
|
|
class MockResource(ModelResource):
|
2011-04-11 18:06:29 +04:00
|
|
|
model = MockModel
|
2011-05-04 12:21:17 +04:00
|
|
|
|
|
|
|
class MockView(BaseView):
|
|
|
|
resource = MockResource
|
2011-04-11 18:06:29 +04:00
|
|
|
|
2011-05-16 19:52:39 +04:00
|
|
|
self.validator = MockResource(MockView)
|
2011-04-11 18:06:29 +04:00
|
|
|
|
|
|
|
|
|
|
|
def test_property_fields_are_allowed_on_model_forms(self):
|
|
|
|
"""Validation on ModelForms may include property fields that exist on the Model to be included in the input."""
|
|
|
|
content = {'qwerty':'example', 'uiop': 'example', 'readonly': 'read only'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertEqual(self.validator.validate_request(content, None), content)
|
2011-04-11 18:06:29 +04:00
|
|
|
|
|
|
|
def test_property_fields_are_not_required_on_model_forms(self):
|
|
|
|
"""Validation on ModelForms does not require property fields that exist on the Model to be included in the input."""
|
|
|
|
content = {'qwerty':'example', 'uiop': 'example'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertEqual(self.validator.validate_request(content, None), content)
|
2011-04-11 18:06:29 +04:00
|
|
|
|
|
|
|
def test_extra_fields_not_allowed_on_model_forms(self):
|
|
|
|
"""If some (otherwise valid) content includes fields that are not in the form then validation should fail.
|
|
|
|
It might be okay on normal form submission, but for Web APIs we oughta get strict, as it'll help show up
|
|
|
|
broken clients more easily (eg submitting content with a misnamed field)"""
|
2011-05-16 19:52:39 +04:00
|
|
|
content = {'qwerty': 'example', 'uiop':'example', 'readonly': 'read only', 'extra': 'extra'}
|
|
|
|
self.assertRaises(ErrorResponse, self.validator.validate_request, content, None)
|
2011-04-11 18:06:29 +04:00
|
|
|
|
|
|
|
def test_validate_requires_fields_on_model_forms(self):
|
|
|
|
"""If some (otherwise valid) content includes fields that are not in the form then validation should fail.
|
|
|
|
It might be okay on normal form submission, but for Web APIs we oughta get strict, as it'll help show up
|
|
|
|
broken clients more easily (eg submitting content with a misnamed field)"""
|
|
|
|
content = {'readonly': 'read only'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.assertRaises(ErrorResponse, self.validator.validate_request, content, None)
|
2011-04-11 18:06:29 +04:00
|
|
|
|
|
|
|
def test_validate_does_not_require_blankable_fields_on_model_forms(self):
|
|
|
|
"""Test standard ModelForm validation behaviour - fields with blank=True are not required."""
|
|
|
|
content = {'qwerty':'example', 'readonly': 'read only'}
|
2011-05-16 19:52:39 +04:00
|
|
|
self.validator.validate_request(content, None)
|
2011-04-11 18:06:29 +04:00
|
|
|
|
|
|
|
def test_model_form_validator_uses_model_forms(self):
|
|
|
|
self.assertTrue(isinstance(self.validator.get_bound_form(), forms.ModelForm))
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
|