Add failing test for request.version AttributeError in BrowsableAPI.

This commit is contained in:
Brandon Cazander 2015-01-24 01:37:23 -08:00
parent 8f25c0c53c
commit b09ef28959
3 changed files with 45 additions and 1 deletions

View File

@ -1,10 +1,17 @@
from __future__ import unicode_literals
from django.conf.urls import patterns, url, include
from rest_framework import routers
from .views import MockView
from .views import MockView, FooViewSet, BarViewSet
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')),
)

View File

@ -3,6 +3,7 @@ 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):
@ -16,6 +17,8 @@ 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()
@ -25,6 +28,13 @@ 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('/')

View File

@ -1,9 +1,14 @@
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):
@ -13,3 +18,25 @@ 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, )