mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Streamline test for #2455
This commit is contained in:
parent
ed04725822
commit
6c083b12a1
|
@ -1,17 +1,11 @@
|
|||
from __future__ import unicode_literals
|
||||
from django.conf.urls import patterns, url, include
|
||||
from rest_framework import routers
|
||||
|
||||
from .views import MockView, FooViewSet, BarViewSet
|
||||
from .views import MockView
|
||||
|
||||
router = routers.SimpleRouter()
|
||||
router.register(r'foo', FooViewSet)
|
||||
router.register(r'bar', BarViewSet)
|
||||
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
(r'^$', MockView.as_view()),
|
||||
url(r'^', include(router.urls)),
|
||||
url(r'^bar/(?P<pk>\d+)/$', BarViewSet, name='bar-list'),
|
||||
url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')),
|
||||
)
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
from django.db import models
|
||||
|
||||
|
||||
class Foo(models.Model):
|
||||
name = models.CharField(max_length=30)
|
||||
|
||||
|
||||
class Bar(models.Model):
|
||||
foo = models.ForeignKey("Foo", editable=False)
|
|
@ -1,14 +0,0 @@
|
|||
from .models import Foo, Bar
|
||||
from rest_framework.serializers import HyperlinkedModelSerializer, HyperlinkedIdentityField
|
||||
|
||||
|
||||
class FooSerializer(HyperlinkedModelSerializer):
|
||||
bar = HyperlinkedIdentityField(view_name='bar-list')
|
||||
|
||||
class Meta:
|
||||
model = Foo
|
||||
|
||||
|
||||
class BarSerializer(HyperlinkedModelSerializer):
|
||||
class Meta:
|
||||
model = Bar
|
|
@ -3,7 +3,6 @@ from django.contrib.auth.models import User
|
|||
from django.test import TestCase
|
||||
|
||||
from rest_framework.test import APIClient
|
||||
from .models import Foo, Bar
|
||||
|
||||
|
||||
class DropdownWithAuthTests(TestCase):
|
||||
|
@ -17,8 +16,6 @@ class DropdownWithAuthTests(TestCase):
|
|||
self.email = 'lennon@thebeatles.com'
|
||||
self.password = 'password'
|
||||
self.user = User.objects.create_user(self.username, self.email, self.password)
|
||||
foo = Foo.objects.create(name='Foo')
|
||||
Bar.objects.create(foo=foo)
|
||||
|
||||
def tearDown(self):
|
||||
self.client.logout()
|
||||
|
@ -28,13 +25,6 @@ class DropdownWithAuthTests(TestCase):
|
|||
response = self.client.get('/')
|
||||
self.assertContains(response, 'john')
|
||||
|
||||
def test_bug_2455_clone_request(self):
|
||||
self.client.login(username=self.username, password=self.password)
|
||||
json_response = self.client.get('/foo/1/?format=json')
|
||||
self.assertEqual(json_response.status_code, 200)
|
||||
browsable_api_response = self.client.get('/foo/1/')
|
||||
self.assertEqual(browsable_api_response.status_code, 200)
|
||||
|
||||
def test_logout_shown_when_logged_in(self):
|
||||
self.client.login(username=self.username, password=self.password)
|
||||
response = self.client.get('/')
|
||||
|
|
|
@ -1,14 +1,9 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from rest_framework import authentication
|
||||
from rest_framework import renderers
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.renderers import BrowsableAPIRenderer, JSONRenderer
|
||||
from rest_framework.versioning import NamespaceVersioning
|
||||
from .models import Foo, Bar
|
||||
from .serializers import FooSerializer, BarSerializer
|
||||
|
||||
|
||||
class MockView(APIView):
|
||||
|
@ -18,25 +13,3 @@ class MockView(APIView):
|
|||
|
||||
def get(self, request):
|
||||
return Response({'a': 1, 'b': 2, 'c': 3})
|
||||
|
||||
|
||||
class SerializerClassMixin(object):
|
||||
def get_serializer_class(self):
|
||||
# Get base name of serializer
|
||||
self.request.version
|
||||
return self.serializer_class
|
||||
|
||||
|
||||
class FooViewSet(SerializerClassMixin, ModelViewSet):
|
||||
versioning_class = NamespaceVersioning
|
||||
model = Foo
|
||||
queryset = Foo.objects.all()
|
||||
serializer_class = FooSerializer
|
||||
renderer_classes = (BrowsableAPIRenderer, JSONRenderer)
|
||||
|
||||
|
||||
class BarViewSet(SerializerClassMixin, ModelViewSet):
|
||||
model = Bar
|
||||
queryset = Bar.objects.all()
|
||||
serializer_class = BarSerializer
|
||||
renderer_classes = (BrowsableAPIRenderer, )
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from __future__ import unicode_literals
|
||||
from rest_framework import exceptions, serializers, status, views
|
||||
from rest_framework.request import Request
|
||||
from rest_framework.renderers import BrowsableAPIRenderer
|
||||
from rest_framework.test import APIRequestFactory
|
||||
|
||||
request = Request(APIRequestFactory().options('/'))
|
||||
|
@ -168,3 +169,17 @@ class TestMetadata:
|
|||
response = view(request=request)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
assert list(response.data['actions'].keys()) == ['POST']
|
||||
|
||||
def test_bug_2455_clone_request(self):
|
||||
class ExampleView(views.APIView):
|
||||
renderer_classes = (BrowsableAPIRenderer,)
|
||||
|
||||
def post(self, request):
|
||||
pass
|
||||
|
||||
def get_serializer(self):
|
||||
assert hasattr(self.request, 'version')
|
||||
return serializers.Serializer()
|
||||
|
||||
view = ExampleView.as_view()
|
||||
view(request=request)
|
||||
|
|
Loading…
Reference in New Issue
Block a user