mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 21:10:13 +03:00
non_native_fields: test additional field shown in browsable API
This commit is contained in:
parent
d31c12c21d
commit
7637847fa2
30
rest_framework/tests/test_non_native_fields.py
Normal file → Executable file
30
rest_framework/tests/test_non_native_fields.py
Normal file → Executable file
|
@ -1,6 +1,9 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from rest_framework import generics
|
||||||
|
from rest_framework.compat import patterns, url
|
||||||
|
|
||||||
|
|
||||||
class ExampleModel(models.Model):
|
class ExampleModel(models.Model):
|
||||||
|
@ -10,6 +13,7 @@ class ExampleModel(models.Model):
|
||||||
|
|
||||||
class ExampleSerializer(serializers.ModelSerializer):
|
class ExampleSerializer(serializers.ModelSerializer):
|
||||||
password_confirmation = serializers.CharField()
|
password_confirmation = serializers.CharField()
|
||||||
|
|
||||||
def validate_password_confirmation(self, attrs, source):
|
def validate_password_confirmation(self, attrs, source):
|
||||||
password_confirmation = attrs[source]
|
password_confirmation = attrs[source]
|
||||||
password = attrs['password']
|
password = attrs['password']
|
||||||
|
@ -17,6 +21,7 @@ class ExampleSerializer(serializers.ModelSerializer):
|
||||||
raise serializers.ValidationError('Password confirmation mismatch')
|
raise serializers.ValidationError('Password confirmation mismatch')
|
||||||
attrs.pop(source)
|
attrs.pop(source)
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ExampleModel
|
model = ExampleModel
|
||||||
fields = ('email', 'password', 'password_confirmation',)
|
fields = ('email', 'password', 'password_confirmation',)
|
||||||
|
@ -24,7 +29,24 @@ class ExampleSerializer(serializers.ModelSerializer):
|
||||||
non_native_fields = ('password_confirmation',)
|
non_native_fields = ('password_confirmation',)
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleView(generics.ListCreateAPIView):
|
||||||
|
"""
|
||||||
|
ExampleView
|
||||||
|
"""
|
||||||
|
model = ExampleModel
|
||||||
|
serializer_class = ExampleSerializer
|
||||||
|
|
||||||
|
example_view = ExampleView.as_view()
|
||||||
|
|
||||||
|
|
||||||
|
urlpatterns = patterns('',
|
||||||
|
url(r'^example$', example_view),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class NonNativeFieldTests(TestCase):
|
class NonNativeFieldTests(TestCase):
|
||||||
|
urls = 'rest_framework.tests.test_non_native_fields'
|
||||||
|
|
||||||
def test_non_native_fields(self):
|
def test_non_native_fields(self):
|
||||||
data = {
|
data = {
|
||||||
'email': 'foo@example.com',
|
'email': 'foo@example.com',
|
||||||
|
@ -49,3 +71,11 @@ class NonNativeFieldTests(TestCase):
|
||||||
self.assertEquals(len(serializer.errors), 1)
|
self.assertEquals(len(serializer.errors), 1)
|
||||||
self.assertEquals(serializer.errors['password_confirmation'],
|
self.assertEquals(serializer.errors['password_confirmation'],
|
||||||
['Password confirmation mismatch'])
|
['Password confirmation mismatch'])
|
||||||
|
|
||||||
|
def test_non_native_fields_displayed_in_html_version(self):
|
||||||
|
"""
|
||||||
|
Ensure password_confirmation field is shown in the browsable API form
|
||||||
|
"""
|
||||||
|
response = self.client.get('/example', HTTP_ACCEPT='text/html')
|
||||||
|
self.assertContains(response, 'for="password"')
|
||||||
|
self.assertContains(response, 'for="password_confirmation"')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user