aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/tests/generics.py
diff options
context:
space:
mode:
authorTom Christie2012-10-02 19:54:24 +0100
committerTom Christie2012-10-02 19:54:24 +0100
commite1518de68fa9ad4b2628894abf17924e386ccbda (patch)
treeb1d190f6bd08ce087cf0a2d3e8cd681b5e7e7d04 /rest_framework/tests/generics.py
parent31b06f1721f98730556dc56927b985e4032788c3 (diff)
parentd1b99f350aded62fe480f7dc4749cd63d52715d2 (diff)
downloaddjango-rest-framework-e1518de68fa9ad4b2628894abf17924e386ccbda.tar.bz2
Merge branch 'restframework2' of https://github.com/tomchristie/django-rest-framework into restframework2
Diffstat (limited to 'rest_framework/tests/generics.py')
-rw-r--r--rest_framework/tests/generics.py39
1 files changed, 35 insertions, 4 deletions
diff --git a/rest_framework/tests/generics.py b/rest_framework/tests/generics.py
index fee6e3a6..76662373 100644
--- a/rest_framework/tests/generics.py
+++ b/rest_framework/tests/generics.py
@@ -13,6 +13,7 @@ class RootView(generics.RootAPIView):
Example description for OPTIONS.
"""
model = BasicModel
+ paginate_by = None
class InstanceView(generics.InstanceAPIView):
@@ -51,7 +52,8 @@ class TestRootView(TestCase):
POST requests to RootAPIView should create a new object.
"""
content = {'text': 'foobar'}
- request = factory.post('/', json.dumps(content), content_type='application/json')
+ request = factory.post('/', json.dumps(content),
+ content_type='application/json')
response = self.view(request).render()
self.assertEquals(response.status_code, status.HTTP_201_CREATED)
self.assertEquals(response.data, {'id': 4, 'text': u'foobar'})
@@ -63,7 +65,8 @@ class TestRootView(TestCase):
PUT requests to RootAPIView should not be allowed
"""
content = {'text': 'foobar'}
- request = factory.put('/', json.dumps(content), content_type='application/json')
+ request = factory.put('/', json.dumps(content),
+ content_type='application/json')
response = self.view(request).render()
self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
self.assertEquals(response.data, {"detail": "Method 'PUT' not allowed."})
@@ -99,6 +102,19 @@ class TestRootView(TestCase):
self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals(response.data, expected)
+ def test_post_cannot_set_id(self):
+ """
+ POST requests to create a new object should not be able to set the id.
+ """
+ content = {'id': 999, 'text': 'foobar'}
+ request = factory.post('/', json.dumps(content),
+ content_type='application/json')
+ response = self.view(request).render()
+ self.assertEquals(response.status_code, status.HTTP_201_CREATED)
+ self.assertEquals(response.data, {'id': 4, 'text': u'foobar'})
+ created = self.objects.get(id=4)
+ self.assertEquals(created.text, 'foobar')
+
class TestInstanceView(TestCase):
def setUp(self):
@@ -129,7 +145,8 @@ class TestInstanceView(TestCase):
POST requests to InstanceAPIView should not be allowed
"""
content = {'text': 'foobar'}
- request = factory.post('/', json.dumps(content), content_type='application/json')
+ request = factory.post('/', json.dumps(content),
+ content_type='application/json')
response = self.view(request).render()
self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
self.assertEquals(response.data, {"detail": "Method 'POST' not allowed."})
@@ -139,7 +156,8 @@ class TestInstanceView(TestCase):
PUT requests to InstanceAPIView should update an object.
"""
content = {'text': 'foobar'}
- request = factory.put('/1', json.dumps(content), content_type='application/json')
+ request = factory.put('/1', json.dumps(content),
+ content_type='application/json')
response = self.view(request, pk=1).render()
self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals(response.data, {'id': 1, 'text': 'foobar'})
@@ -178,3 +196,16 @@ class TestInstanceView(TestCase):
}
self.assertEquals(response.status_code, status.HTTP_200_OK)
self.assertEquals(response.data, expected)
+
+ def test_put_cannot_set_id(self):
+ """
+ POST requests to create a new object should not be able to set the id.
+ """
+ content = {'id': 999, 'text': 'foobar'}
+ request = factory.put('/1', json.dumps(content),
+ content_type='application/json')
+ response = self.view(request, pk=1).render()
+ self.assertEquals(response.status_code, status.HTTP_200_OK)
+ self.assertEquals(response.data, {'id': 1, 'text': 'foobar'})
+ updated = self.objects.get(id=1)
+ self.assertEquals(updated.text, 'foobar')