mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Added wo tests. One for PUTing on a non-existing id-based url. And another for PUTing on a non-existing slug-based url.
Fix doctoring for 'test_put_cannot_set_id'.
This commit is contained in:
parent
1eb56ebdd9
commit
bc99142c7d
|
@ -2,7 +2,7 @@ from django.test import TestCase
|
||||||
from django.test.client import RequestFactory
|
from django.test.client import RequestFactory
|
||||||
from django.utils import simplejson as json
|
from django.utils import simplejson as json
|
||||||
from rest_framework import generics, serializers, status
|
from rest_framework import generics, serializers, status
|
||||||
from rest_framework.tests.models import BasicModel, Comment
|
from rest_framework.tests.models import BasicModel, Comment, SlugBasedModel
|
||||||
|
|
||||||
|
|
||||||
factory = RequestFactory()
|
factory = RequestFactory()
|
||||||
|
@ -22,6 +22,13 @@ class InstanceView(generics.RetrieveUpdateDestroyAPIView):
|
||||||
model = BasicModel
|
model = BasicModel
|
||||||
|
|
||||||
|
|
||||||
|
class SlugBasedInstanceView(InstanceView):
|
||||||
|
"""
|
||||||
|
A model with a slug-field.
|
||||||
|
"""
|
||||||
|
model = SlugBasedModel
|
||||||
|
|
||||||
|
|
||||||
class TestRootView(TestCase):
|
class TestRootView(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""
|
"""
|
||||||
|
@ -129,6 +136,7 @@ class TestInstanceView(TestCase):
|
||||||
for obj in self.objects.all()
|
for obj in self.objects.all()
|
||||||
]
|
]
|
||||||
self.view = InstanceView.as_view()
|
self.view = InstanceView.as_view()
|
||||||
|
self.slug_based_view = SlugBasedInstanceView.as_view()
|
||||||
|
|
||||||
def test_get_instance_view(self):
|
def test_get_instance_view(self):
|
||||||
"""
|
"""
|
||||||
|
@ -198,7 +206,7 @@ class TestInstanceView(TestCase):
|
||||||
|
|
||||||
def test_put_cannot_set_id(self):
|
def test_put_cannot_set_id(self):
|
||||||
"""
|
"""
|
||||||
POST requests to create a new object should not be able to set the id.
|
PUT requests to create a new object should not be able to set the id.
|
||||||
"""
|
"""
|
||||||
content = {'id': 999, 'text': 'foobar'}
|
content = {'id': 999, 'text': 'foobar'}
|
||||||
request = factory.put('/1', json.dumps(content),
|
request = factory.put('/1', json.dumps(content),
|
||||||
|
@ -224,6 +232,38 @@ class TestInstanceView(TestCase):
|
||||||
updated = self.objects.get(id=1)
|
updated = self.objects.get(id=1)
|
||||||
self.assertEquals(updated.text, 'foobar')
|
self.assertEquals(updated.text, 'foobar')
|
||||||
|
|
||||||
|
def test_put_as_create_on_id_based_url(self):
|
||||||
|
"""
|
||||||
|
PUT requests to RetrieveUpdateDestroyAPIView should create an object
|
||||||
|
at the requested url if it doesn't exist, if creation is not possible,
|
||||||
|
e.g. the pk for an id-field is determined by the database,
|
||||||
|
a HTTP_403_FORBIDDEN error-response must be returned.
|
||||||
|
"""
|
||||||
|
content = {'text': 'foobar'}
|
||||||
|
# pk fields can not be created on demand, only the database can set th pk for a new object
|
||||||
|
request = factory.put('/5', json.dumps(content),
|
||||||
|
content_type='application/json')
|
||||||
|
response = self.view(request, pk=5).render()
|
||||||
|
expected = {
|
||||||
|
'detail': u'A resource could not be created at the requested URI'
|
||||||
|
}
|
||||||
|
self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||||
|
self.assertEquals(response.data, expected)
|
||||||
|
|
||||||
|
def test_put_as_create_on_slug_based_url(self):
|
||||||
|
"""
|
||||||
|
PUT requests to RetrieveUpdateDestroyAPIView should create an object
|
||||||
|
at the requested url if possible, else return HTTP_403_FORBIDDEN error-response.
|
||||||
|
"""
|
||||||
|
content = {'text': 'foobar'}
|
||||||
|
request = factory.put('/test_slug', json.dumps(content),
|
||||||
|
content_type='application/json')
|
||||||
|
response = self.slug_based_view(request, pk='test_slug').render()
|
||||||
|
self.assertEquals(response.status_code, status.HTTP_201_CREATED)
|
||||||
|
self.assertEquals(response.data, {'slug': 'test_slug', 'text': 'foobar'})
|
||||||
|
updated = self.objects.get(slug='test_slug')
|
||||||
|
self.assertEquals(updated.text, 'foobar')
|
||||||
|
|
||||||
|
|
||||||
# Regression test for #285
|
# Regression test for #285
|
||||||
|
|
||||||
|
|
|
@ -52,6 +52,11 @@ class BasicModel(RESTFrameworkModel):
|
||||||
text = models.CharField(max_length=100)
|
text = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
|
class SlugBasedModel(RESTFrameworkModel):
|
||||||
|
text = models.CharField(max_length=100)
|
||||||
|
slug = models.SlugField(max_length=32)
|
||||||
|
|
||||||
|
|
||||||
class DefaultValueModel(RESTFrameworkModel):
|
class DefaultValueModel(RESTFrameworkModel):
|
||||||
text = models.CharField(default='foobar', max_length=100)
|
text = models.CharField(default='foobar', max_length=100)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user