Commit 39811999 authored by M. de Verteuil's avatar M. de Verteuil
Browse files

Add test for getting serializer by user

parent 841edaad
import datetime
import functools
from mock import patch
from mock import Mock, patch
from distutils.version import StrictVersion
from django.core.urlresolvers import RegexURLResolver, RegexURLPattern
......@@ -618,6 +618,39 @@ class DocumentationGeneratorTest(TestCase):
self.assertEqual(1, len(serializer_set))
self.assertEqual(SerializerFoo, list(serializer_set)[0])
def test_get_serializer_class_for_user(self):
class SerializerForAnonymous(serializers.Serializer):
pass
class SerializerForAuthenticated(serializers.Serializer):
pass
class TestView(APIView):
def get_serializer_class(self):
if self.request.user.is_authenticated():
return SerializerForAuthenticated
else:
return SerializerForAnonymous
urlparser = UrlParser()
url_patterns = patterns('', url(r'^a/$', TestView.as_view()))
apis = urlparser.get_apis(url_patterns)
anonymous_user = AnonymousUser()
docs_for_anonymous = DocumentationGenerator(for_user=anonymous_user)
serializers_for_anonymous = docs_for_anonymous._get_serializer_set(apis)
self.assertEqual(1, len(serializers_for_anonymous))
self.assertEqual([SerializerForAnonymous], list(serializers_for_anonymous))
authenticated_user = Mock(spec=User)
authenticated_user.is_authenticated.return_value = True
docs_for_authenticated = DocumentationGenerator(for_user=authenticated_user)
serializers_for_authenticated = docs_for_authenticated._get_serializer_set(apis)
self.assertEqual(1, len(serializers_for_authenticated))
self.assertEqual([SerializerForAuthenticated], list(serializers_for_authenticated))
def test_old_parameter_description_syntax(self):
from rest_framework.views import Response
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment