mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 08:29:59 +03:00
Merge a655910dbe
into 221e77d357
This commit is contained in:
commit
4a8a296fe7
|
@ -79,6 +79,11 @@ To run the tests.
|
||||||
|
|
||||||
./rest_framework/runtests/runtests.py
|
./rest_framework/runtests/runtests.py
|
||||||
|
|
||||||
|
To run the tests with code coverage.
|
||||||
|
|
||||||
|
pip install -r development.txt
|
||||||
|
./rest_framework/runtests/runcoverage.py
|
||||||
|
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
### 2.1.17
|
### 2.1.17
|
||||||
|
|
1
development.txt
Normal file
1
development.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
coverage
|
|
@ -2,7 +2,7 @@ from django.contrib.auth.models import User
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.test import Client, TestCase
|
from django.test import Client, TestCase
|
||||||
|
|
||||||
from rest_framework import permissions
|
from rest_framework import permissions, status
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
from rest_framework.authentication import TokenAuthentication, BasicAuthentication, SessionAuthentication
|
from rest_framework.authentication import TokenAuthentication, BasicAuthentication, SessionAuthentication
|
||||||
from rest_framework.compat import patterns
|
from rest_framework.compat import patterns
|
||||||
|
@ -44,23 +44,24 @@ class BasicAuthTests(TestCase):
|
||||||
"""Ensure POSTing json over basic auth with correct credentials passes and does not require CSRF"""
|
"""Ensure POSTing json over basic auth with correct credentials passes and does not require CSRF"""
|
||||||
auth = 'Basic %s' % base64.encodestring('%s:%s' % (self.username, self.password)).strip()
|
auth = 'Basic %s' % base64.encodestring('%s:%s' % (self.username, self.password)).strip()
|
||||||
response = self.csrf_client.post('/basic/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
|
response = self.csrf_client.post('/basic/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
def test_post_json_passing_basic_auth(self):
|
def test_post_json_passing_basic_auth(self):
|
||||||
"""Ensure POSTing form over basic auth with correct credentials passes and does not require CSRF"""
|
"""Ensure POSTing form over basic auth with correct credentials passes and does not require CSRF"""
|
||||||
auth = 'Basic %s' % base64.encodestring('%s:%s' % (self.username, self.password)).strip()
|
auth = 'Basic %s' % base64.encodestring('%s:%s' % (self.username, self.password)).strip()
|
||||||
response = self.csrf_client.post('/basic/', json.dumps({'example': 'example'}), 'application/json', HTTP_AUTHORIZATION=auth)
|
response = self.csrf_client.post('/basic/', json.dumps({'example': 'example'}), 'application/json',
|
||||||
self.assertEqual(response.status_code, 200)
|
HTTP_AUTHORIZATION=auth)
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
def test_post_form_failing_basic_auth(self):
|
def test_post_form_failing_basic_auth(self):
|
||||||
"""Ensure POSTing form over basic auth without correct credentials fails"""
|
"""Ensure POSTing form over basic auth without correct credentials fails"""
|
||||||
response = self.csrf_client.post('/basic/', {'example': 'example'})
|
response = self.csrf_client.post('/basic/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
def test_post_json_failing_basic_auth(self):
|
def test_post_json_failing_basic_auth(self):
|
||||||
"""Ensure POSTing json over basic auth without correct credentials fails"""
|
"""Ensure POSTing json over basic auth without correct credentials fails"""
|
||||||
response = self.csrf_client.post('/basic/', json.dumps({'example': 'example'}), 'application/json')
|
response = self.csrf_client.post('/basic/', json.dumps({'example': 'example'}), 'application/json')
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||||
self.assertEqual(response['WWW-Authenticate'], 'Basic realm="api"')
|
self.assertEqual(response['WWW-Authenticate'], 'Basic realm="api"')
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,7 +86,7 @@ class SessionAuthTests(TestCase):
|
||||||
"""
|
"""
|
||||||
self.csrf_client.login(username=self.username, password=self.password)
|
self.csrf_client.login(username=self.username, password=self.password)
|
||||||
response = self.csrf_client.post('/session/', {'example': 'example'})
|
response = self.csrf_client.post('/session/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, 403)
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
def test_post_form_session_auth_passing(self):
|
def test_post_form_session_auth_passing(self):
|
||||||
"""
|
"""
|
||||||
|
@ -93,7 +94,7 @@ class SessionAuthTests(TestCase):
|
||||||
"""
|
"""
|
||||||
self.non_csrf_client.login(username=self.username, password=self.password)
|
self.non_csrf_client.login(username=self.username, password=self.password)
|
||||||
response = self.non_csrf_client.post('/session/', {'example': 'example'})
|
response = self.non_csrf_client.post('/session/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
def test_put_form_session_auth_passing(self):
|
def test_put_form_session_auth_passing(self):
|
||||||
"""
|
"""
|
||||||
|
@ -101,14 +102,14 @@ class SessionAuthTests(TestCase):
|
||||||
"""
|
"""
|
||||||
self.non_csrf_client.login(username=self.username, password=self.password)
|
self.non_csrf_client.login(username=self.username, password=self.password)
|
||||||
response = self.non_csrf_client.put('/session/', {'example': 'example'})
|
response = self.non_csrf_client.put('/session/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
def test_post_form_session_auth_failing(self):
|
def test_post_form_session_auth_failing(self):
|
||||||
"""
|
"""
|
||||||
Ensure POSTing form over session authentication without logged in user fails.
|
Ensure POSTing form over session authentication without logged in user fails.
|
||||||
"""
|
"""
|
||||||
response = self.csrf_client.post('/session/', {'example': 'example'})
|
response = self.csrf_client.post('/session/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, 403)
|
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
|
||||||
class TokenAuthTests(TestCase):
|
class TokenAuthTests(TestCase):
|
||||||
|
@ -129,23 +130,24 @@ class TokenAuthTests(TestCase):
|
||||||
"""Ensure POSTing json over token auth with correct credentials passes and does not require CSRF"""
|
"""Ensure POSTing json over token auth with correct credentials passes and does not require CSRF"""
|
||||||
auth = "Token " + self.key
|
auth = "Token " + self.key
|
||||||
response = self.csrf_client.post('/token/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
|
response = self.csrf_client.post('/token/', {'example': 'example'}, HTTP_AUTHORIZATION=auth)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
def test_post_json_passing_token_auth(self):
|
def test_post_json_passing_token_auth(self):
|
||||||
"""Ensure POSTing form over token auth with correct credentials passes and does not require CSRF"""
|
"""Ensure POSTing form over token auth with correct credentials passes and does not require CSRF"""
|
||||||
auth = "Token " + self.key
|
auth = "Token " + self.key
|
||||||
response = self.csrf_client.post('/token/', json.dumps({'example': 'example'}), 'application/json', HTTP_AUTHORIZATION=auth)
|
response = self.csrf_client.post('/token/', json.dumps({'example': 'example'}), 'application/json',
|
||||||
self.assertEqual(response.status_code, 200)
|
HTTP_AUTHORIZATION=auth)
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
def test_post_form_failing_token_auth(self):
|
def test_post_form_failing_token_auth(self):
|
||||||
"""Ensure POSTing form over token auth without correct credentials fails"""
|
"""Ensure POSTing form over token auth without correct credentials fails"""
|
||||||
response = self.csrf_client.post('/token/', {'example': 'example'})
|
response = self.csrf_client.post('/token/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
def test_post_json_failing_token_auth(self):
|
def test_post_json_failing_token_auth(self):
|
||||||
"""Ensure POSTing json over token auth without correct credentials fails"""
|
"""Ensure POSTing json over token auth without correct credentials fails"""
|
||||||
response = self.csrf_client.post('/token/', json.dumps({'example': 'example'}), 'application/json')
|
response = self.csrf_client.post('/token/', json.dumps({'example': 'example'}), 'application/json')
|
||||||
self.assertEqual(response.status_code, 401)
|
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
def test_token_has_auto_assigned_key_if_none_provided(self):
|
def test_token_has_auto_assigned_key_if_none_provided(self):
|
||||||
"""Ensure creating a token with no key will auto-assign a key"""
|
"""Ensure creating a token with no key will auto-assign a key"""
|
||||||
|
@ -158,7 +160,7 @@ class TokenAuthTests(TestCase):
|
||||||
client = Client(enforce_csrf_checks=True)
|
client = Client(enforce_csrf_checks=True)
|
||||||
response = client.post('/auth-token/',
|
response = client.post('/auth-token/',
|
||||||
json.dumps({'username': self.username, 'password': self.password}), 'application/json')
|
json.dumps({'username': self.username, 'password': self.password}), 'application/json')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(json.loads(response.content)['token'], self.key)
|
self.assertEqual(json.loads(response.content)['token'], self.key)
|
||||||
|
|
||||||
def test_token_login_json_bad_creds(self):
|
def test_token_login_json_bad_creds(self):
|
||||||
|
@ -166,19 +168,19 @@ class TokenAuthTests(TestCase):
|
||||||
client = Client(enforce_csrf_checks=True)
|
client = Client(enforce_csrf_checks=True)
|
||||||
response = client.post('/auth-token/',
|
response = client.post('/auth-token/',
|
||||||
json.dumps({'username': self.username, 'password': "badpass"}), 'application/json')
|
json.dumps({'username': self.username, 'password': "badpass"}), 'application/json')
|
||||||
self.assertEqual(response.status_code, 400)
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
def test_token_login_json_missing_fields(self):
|
def test_token_login_json_missing_fields(self):
|
||||||
"""Ensure token login view using JSON POST fails if missing fields."""
|
"""Ensure token login view using JSON POST fails if missing fields."""
|
||||||
client = Client(enforce_csrf_checks=True)
|
client = Client(enforce_csrf_checks=True)
|
||||||
response = client.post('/auth-token/',
|
response = client.post('/auth-token/',
|
||||||
json.dumps({'username': self.username}), 'application/json')
|
json.dumps({'username': self.username}), 'application/json')
|
||||||
self.assertEqual(response.status_code, 400)
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
def test_token_login_form(self):
|
def test_token_login_form(self):
|
||||||
"""Ensure token login view using form POST works."""
|
"""Ensure token login view using form POST works."""
|
||||||
client = Client(enforce_csrf_checks=True)
|
client = Client(enforce_csrf_checks=True)
|
||||||
response = client.post('/auth-token/',
|
response = client.post('/auth-token/',
|
||||||
{'username': self.username, 'password': self.password})
|
{'username': self.username, 'password': self.password})
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(json.loads(response.content)['token'], self.key)
|
self.assertEqual(json.loads(response.content)['token'], self.key)
|
||||||
|
|
|
@ -14,13 +14,12 @@ from rest_framework.decorators import (
|
||||||
authentication_classes,
|
authentication_classes,
|
||||||
throttle_classes,
|
throttle_classes,
|
||||||
permission_classes,
|
permission_classes,
|
||||||
)
|
)
|
||||||
|
|
||||||
from rest_framework.tests.utils import RequestFactory
|
from rest_framework.tests.utils import RequestFactory
|
||||||
|
|
||||||
|
|
||||||
class DecoratorTestCase(TestCase):
|
class DecoratorTestCase(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
|
|
||||||
|
@ -51,49 +50,45 @@ class DecoratorTestCase(TestCase):
|
||||||
return Response()
|
return Response()
|
||||||
|
|
||||||
def test_calling_method(self):
|
def test_calling_method(self):
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def view(request):
|
def view(request):
|
||||||
return Response({})
|
return Response({})
|
||||||
|
|
||||||
request = self.factory.get('/')
|
request = self.factory.get('/')
|
||||||
response = view(request)
|
response = view(request)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
request = self.factory.post('/')
|
request = self.factory.post('/')
|
||||||
response = view(request)
|
response = view(request)
|
||||||
self.assertEqual(response.status_code, 405)
|
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||||
|
|
||||||
def test_calling_put_method(self):
|
def test_calling_put_method(self):
|
||||||
|
|
||||||
@api_view(['GET', 'PUT'])
|
@api_view(['GET', 'PUT'])
|
||||||
def view(request):
|
def view(request):
|
||||||
return Response({})
|
return Response({})
|
||||||
|
|
||||||
request = self.factory.put('/')
|
request = self.factory.put('/')
|
||||||
response = view(request)
|
response = view(request)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
request = self.factory.post('/')
|
request = self.factory.post('/')
|
||||||
response = view(request)
|
response = view(request)
|
||||||
self.assertEqual(response.status_code, 405)
|
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||||
|
|
||||||
def test_calling_patch_method(self):
|
def test_calling_patch_method(self):
|
||||||
|
|
||||||
@api_view(['GET', 'PATCH'])
|
@api_view(['GET', 'PATCH'])
|
||||||
def view(request):
|
def view(request):
|
||||||
return Response({})
|
return Response({})
|
||||||
|
|
||||||
request = self.factory.patch('/')
|
request = self.factory.patch('/')
|
||||||
response = view(request)
|
response = view(request)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
request = self.factory.post('/')
|
request = self.factory.post('/')
|
||||||
response = view(request)
|
response = view(request)
|
||||||
self.assertEqual(response.status_code, 405)
|
self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
|
||||||
|
|
||||||
def test_renderer_classes(self):
|
def test_renderer_classes(self):
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
@renderer_classes([JSONRenderer])
|
@renderer_classes([JSONRenderer])
|
||||||
def view(request):
|
def view(request):
|
||||||
|
@ -104,7 +99,6 @@ class DecoratorTestCase(TestCase):
|
||||||
self.assertTrue(isinstance(response.accepted_renderer, JSONRenderer))
|
self.assertTrue(isinstance(response.accepted_renderer, JSONRenderer))
|
||||||
|
|
||||||
def test_parser_classes(self):
|
def test_parser_classes(self):
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
@parser_classes([JSONParser])
|
@parser_classes([JSONParser])
|
||||||
def view(request):
|
def view(request):
|
||||||
|
@ -117,7 +111,6 @@ class DecoratorTestCase(TestCase):
|
||||||
view(request)
|
view(request)
|
||||||
|
|
||||||
def test_authentication_classes(self):
|
def test_authentication_classes(self):
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
@authentication_classes([BasicAuthentication])
|
@authentication_classes([BasicAuthentication])
|
||||||
def view(request):
|
def view(request):
|
||||||
|
@ -130,7 +123,6 @@ class DecoratorTestCase(TestCase):
|
||||||
view(request)
|
view(request)
|
||||||
|
|
||||||
def test_permission_classes(self):
|
def test_permission_classes(self):
|
||||||
|
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
@permission_classes([IsAuthenticated])
|
@permission_classes([IsAuthenticated])
|
||||||
def view(request):
|
def view(request):
|
||||||
|
|
|
@ -42,7 +42,7 @@ class SlugBasedInstanceView(InstanceView):
|
||||||
class TestRootView(TestCase):
|
class TestRootView(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""
|
"""
|
||||||
Create 3 BasicModel intances.
|
Create 3 BasicModel instances.
|
||||||
"""
|
"""
|
||||||
items = ['foo', 'bar', 'baz']
|
items = ['foo', 'bar', 'baz']
|
||||||
for item in items:
|
for item in items:
|
||||||
|
@ -343,7 +343,7 @@ class ExampleView(generics.ListCreateAPIView):
|
||||||
class TestM2MBrowseableAPI(TestCase):
|
class TestM2MBrowseableAPI(TestCase):
|
||||||
def test_m2m_in_browseable_api(self):
|
def test_m2m_in_browseable_api(self):
|
||||||
"""
|
"""
|
||||||
Test for particularly ugly reression with m2m in browseable API
|
Test for particularly ugly regression with m2m in browseable API
|
||||||
"""
|
"""
|
||||||
request = factory.get('/', HTTP_ACCEPT='text/html')
|
request = factory.get('/', HTTP_ACCEPT='text/html')
|
||||||
view = ExampleView().as_view()
|
view = ExampleView().as_view()
|
||||||
|
|
|
@ -3,6 +3,7 @@ from django.http import Http404
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.template import TemplateDoesNotExist, Template
|
from django.template import TemplateDoesNotExist, Template
|
||||||
import django.template.loader
|
import django.template.loader
|
||||||
|
from rest_framework import status
|
||||||
from rest_framework.compat import patterns, url
|
from rest_framework.compat import patterns, url
|
||||||
from rest_framework.decorators import api_view, renderer_classes
|
from rest_framework.decorators import api_view, renderer_classes
|
||||||
from rest_framework.renderers import TemplateHTMLRenderer
|
from rest_framework.renderers import TemplateHTMLRenderer
|
||||||
|
@ -67,13 +68,13 @@ class TemplateHTMLRendererTests(TestCase):
|
||||||
|
|
||||||
def test_not_found_html_view(self):
|
def test_not_found_html_view(self):
|
||||||
response = self.client.get('/not_found')
|
response = self.client.get('/not_found')
|
||||||
self.assertEquals(response.status_code, 404)
|
self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||||
self.assertEquals(response.content, "404 Not Found")
|
self.assertEquals(response.content, "404 Not Found")
|
||||||
self.assertEquals(response['Content-Type'], 'text/html')
|
self.assertEquals(response['Content-Type'], 'text/html')
|
||||||
|
|
||||||
def test_permission_denied_html_view(self):
|
def test_permission_denied_html_view(self):
|
||||||
response = self.client.get('/permission_denied')
|
response = self.client.get('/permission_denied')
|
||||||
self.assertEquals(response.status_code, 403)
|
self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||||
self.assertEquals(response.content, "403 Forbidden")
|
self.assertEquals(response.content, "403 Forbidden")
|
||||||
self.assertEquals(response['Content-Type'], 'text/html')
|
self.assertEquals(response['Content-Type'], 'text/html')
|
||||||
|
|
||||||
|
@ -104,12 +105,12 @@ class TemplateHTMLRendererExceptionTests(TestCase):
|
||||||
|
|
||||||
def test_not_found_html_view_with_template(self):
|
def test_not_found_html_view_with_template(self):
|
||||||
response = self.client.get('/not_found')
|
response = self.client.get('/not_found')
|
||||||
self.assertEquals(response.status_code, 404)
|
self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||||
self.assertEquals(response.content, "404: Not found")
|
self.assertEquals(response.content, "404: Not found")
|
||||||
self.assertEquals(response['Content-Type'], 'text/html')
|
self.assertEquals(response['Content-Type'], 'text/html')
|
||||||
|
|
||||||
def test_permission_denied_html_view_with_template(self):
|
def test_permission_denied_html_view_with_template(self):
|
||||||
response = self.client.get('/permission_denied')
|
response = self.client.get('/permission_denied')
|
||||||
self.assertEquals(response.status_code, 403)
|
self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN)
|
||||||
self.assertEquals(response.content, "403: Permission denied")
|
self.assertEquals(response.content, "403: Permission denied")
|
||||||
self.assertEquals(response['Content-Type'], 'text/html')
|
self.assertEquals(response['Content-Type'], 'text/html')
|
||||||
|
|
|
@ -20,7 +20,8 @@ class BlogPostCommentSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class PhotoSerializer(serializers.Serializer):
|
class PhotoSerializer(serializers.Serializer):
|
||||||
description = serializers.CharField()
|
description = serializers.CharField()
|
||||||
album_url = serializers.HyperlinkedRelatedField(source='album', view_name='album-detail', queryset=Album.objects.all(), slug_field='title', slug_url_kwarg='title')
|
album_url = serializers.HyperlinkedRelatedField(source='album', view_name='album-detail',
|
||||||
|
queryset=Album.objects.all(), slug_field='title', slug_url_kwarg='title')
|
||||||
|
|
||||||
def restore_object(self, attrs, instance=None):
|
def restore_object(self, attrs, instance=None):
|
||||||
return Photo(**attrs)
|
return Photo(**attrs)
|
||||||
|
@ -99,7 +100,7 @@ class TestBasicHyperlinkedView(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""
|
"""
|
||||||
Create 3 BasicModel intances.
|
Create 3 BasicModel instances.
|
||||||
"""
|
"""
|
||||||
items = ['foo', 'bar', 'baz']
|
items = ['foo', 'bar', 'baz']
|
||||||
for item in items:
|
for item in items:
|
||||||
|
@ -136,7 +137,7 @@ class TestManyToManyHyperlinkedView(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""
|
"""
|
||||||
Create 3 BasicModel intances.
|
Create 3 BasicModel instances.
|
||||||
"""
|
"""
|
||||||
items = ['foo', 'bar', 'baz']
|
items = ['foo', 'bar', 'baz']
|
||||||
anchors = []
|
anchors = []
|
||||||
|
@ -190,7 +191,6 @@ class TestCreateWithForeignKeys(TestCase):
|
||||||
self.create_view = BlogPostCommentListCreate.as_view()
|
self.create_view = BlogPostCommentListCreate.as_view()
|
||||||
|
|
||||||
def test_create_comment(self):
|
def test_create_comment(self):
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'text': 'A test comment',
|
'text': 'A test comment',
|
||||||
'blog_post_url': 'http://testserver/posts/1/'
|
'blog_post_url': 'http://testserver/posts/1/'
|
||||||
|
@ -215,7 +215,6 @@ class TestCreateWithForeignKeysAndCustomSlug(TestCase):
|
||||||
self.list_create_view = PhotoListCreate.as_view()
|
self.list_create_view = PhotoListCreate.as_view()
|
||||||
|
|
||||||
def test_create_photo(self):
|
def test_create_photo(self):
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'description': 'A test photo',
|
'description': 'A test photo',
|
||||||
'album_url': 'http://testserver/albums/test-album/'
|
'album_url': 'http://testserver/albums/test-album/'
|
||||||
|
@ -224,7 +223,8 @@ class TestCreateWithForeignKeysAndCustomSlug(TestCase):
|
||||||
request = factory.post('/photos/', data=data)
|
request = factory.post('/photos/', data=data)
|
||||||
response = self.list_create_view(request)
|
response = self.list_create_view(request)
|
||||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||||
self.assertNotIn('Location', response, msg='Location should only be included if there is a "url" field on the serializer')
|
self.assertNotIn('Location', response,
|
||||||
|
msg='Location should only be included if there is a "url" field on the serializer')
|
||||||
self.assertEqual(self.post.photo_set.count(), 1)
|
self.assertEqual(self.post.photo_set.count(), 1)
|
||||||
self.assertEqual(self.post.photo_set.all()[0].description, 'A test photo')
|
self.assertEqual(self.post.photo_set.all()[0].description, 'A test photo')
|
||||||
|
|
||||||
|
@ -234,7 +234,7 @@ class TestOptionalRelationHyperlinkedView(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""
|
"""
|
||||||
Create 1 OptionalRelationModel intances.
|
Create 1 OptionalRelationModel instances.
|
||||||
"""
|
"""
|
||||||
OptionalRelationModel().save()
|
OptionalRelationModel().save()
|
||||||
self.objects = OptionalRelationModel.objects
|
self.objects = OptionalRelationModel.objects
|
||||||
|
|
|
@ -24,7 +24,7 @@ class NullableForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||||
model = NullableForeignKeySource
|
model = NullableForeignKeySource
|
||||||
|
|
||||||
|
|
||||||
# TODO: M2M Tests, FKTests (Non-nulable), One2One
|
# TODO: M2M Tests, FKTests (Non-nullable), One2One
|
||||||
class PKForeignKeyTests(TestCase):
|
class PKForeignKeyTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
target = ForeignKeyTarget(name='target-1')
|
target = ForeignKeyTarget(name='target-1')
|
||||||
|
|
|
@ -9,7 +9,7 @@ from rest_framework import status, permissions
|
||||||
from rest_framework.compat import yaml, patterns, url, include
|
from rest_framework.compat import yaml, patterns, url, include
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.renderers import BaseRenderer, JSONRenderer, YAMLRenderer, \
|
from rest_framework.renderers import BaseRenderer, JSONRenderer, YAMLRenderer,\
|
||||||
XMLRenderer, JSONPRenderer, BrowsableAPIRenderer
|
XMLRenderer, JSONPRenderer, BrowsableAPIRenderer
|
||||||
from rest_framework.parsers import YAMLParser, XMLParser
|
from rest_framework.parsers import YAMLParser, XMLParser
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
|
@ -25,7 +25,6 @@ DUMMYCONTENT = 'dummycontent'
|
||||||
RENDERER_A_SERIALIZER = lambda x: 'Renderer A: %s' % x
|
RENDERER_A_SERIALIZER = lambda x: 'Renderer A: %s' % x
|
||||||
RENDERER_B_SERIALIZER = lambda x: 'Renderer B: %s' % x
|
RENDERER_B_SERIALIZER = lambda x: 'Renderer B: %s' % x
|
||||||
|
|
||||||
|
|
||||||
expected_results = [
|
expected_results = [
|
||||||
((elem for elem in [1, 2, 3]), JSONRenderer, '[1, 2, 3]') # Generator
|
((elem for elem in [1, 2, 3]), JSONRenderer, '[1, 2, 3]') # Generator
|
||||||
]
|
]
|
||||||
|
@ -63,7 +62,6 @@ class MockView(APIView):
|
||||||
|
|
||||||
|
|
||||||
class MockGETView(APIView):
|
class MockGETView(APIView):
|
||||||
|
|
||||||
def get(self, request, **kwargs):
|
def get(self, request, **kwargs):
|
||||||
return Response({'foo': ['bar', 'baz']})
|
return Response({'foo': ['bar', 'baz']})
|
||||||
|
|
||||||
|
@ -265,7 +263,7 @@ class JSONPRendererTests(TestCase):
|
||||||
"""
|
"""
|
||||||
resp = self.client.get('/jsonp/jsonrenderer',
|
resp = self.client.get('/jsonp/jsonrenderer',
|
||||||
HTTP_ACCEPT='application/javascript')
|
HTTP_ACCEPT='application/javascript')
|
||||||
self.assertEquals(resp.status_code, 200)
|
self.assertEquals(resp.status_code, status.HTTP_200_OK)
|
||||||
self.assertEquals(resp['Content-Type'], 'application/javascript')
|
self.assertEquals(resp['Content-Type'], 'application/javascript')
|
||||||
self.assertEquals(resp.content, 'callback(%s);' % _flat_repr)
|
self.assertEquals(resp.content, 'callback(%s);' % _flat_repr)
|
||||||
|
|
||||||
|
@ -275,7 +273,7 @@ class JSONPRendererTests(TestCase):
|
||||||
"""
|
"""
|
||||||
resp = self.client.get('/jsonp/nojsonrenderer',
|
resp = self.client.get('/jsonp/nojsonrenderer',
|
||||||
HTTP_ACCEPT='application/javascript')
|
HTTP_ACCEPT='application/javascript')
|
||||||
self.assertEquals(resp.status_code, 200)
|
self.assertEquals(resp.status_code, status.HTTP_200_OK)
|
||||||
self.assertEquals(resp['Content-Type'], 'application/javascript')
|
self.assertEquals(resp['Content-Type'], 'application/javascript')
|
||||||
self.assertEquals(resp.content, 'callback(%s);' % _flat_repr)
|
self.assertEquals(resp.content, 'callback(%s);' % _flat_repr)
|
||||||
|
|
||||||
|
@ -286,7 +284,7 @@ class JSONPRendererTests(TestCase):
|
||||||
callback_func = 'myjsonpcallback'
|
callback_func = 'myjsonpcallback'
|
||||||
resp = self.client.get('/jsonp/nojsonrenderer?callback=' + callback_func,
|
resp = self.client.get('/jsonp/nojsonrenderer?callback=' + callback_func,
|
||||||
HTTP_ACCEPT='application/javascript')
|
HTTP_ACCEPT='application/javascript')
|
||||||
self.assertEquals(resp.status_code, 200)
|
self.assertEquals(resp.status_code, status.HTTP_200_OK)
|
||||||
self.assertEquals(resp['Content-Type'], 'application/javascript')
|
self.assertEquals(resp['Content-Type'], 'application/javascript')
|
||||||
self.assertEquals(resp.content, '%s(%s);' % (callback_func, _flat_repr))
|
self.assertEquals(resp.content, '%s(%s);' % (callback_func, _flat_repr))
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ urlpatterns = patterns('',
|
||||||
|
|
||||||
class ReverseTests(TestCase):
|
class ReverseTests(TestCase):
|
||||||
"""
|
"""
|
||||||
Tests for fully qualifed URLs when using `reverse`.
|
Tests for fully qualified URLs when using `reverse`.
|
||||||
"""
|
"""
|
||||||
urls = 'rest_framework.tests.reverse'
|
urls = 'rest_framework.tests.reverse'
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,184 @@ from rest_framework import status
|
||||||
|
|
||||||
|
|
||||||
class TestStatus(TestCase):
|
class TestStatus(TestCase):
|
||||||
"""Simple sanity test to check the status module"""
|
"""Simple sanity tests to check the status module"""
|
||||||
|
|
||||||
def test_status(self):
|
def test_status_HTTP_100_CONTINUE(self):
|
||||||
"""Ensure the status module is present and correct."""
|
"""Ensure that HTTP_100_CONTINUE equals 100."""
|
||||||
self.assertEquals(200, status.HTTP_200_OK)
|
self.assertEquals(status.HTTP_100_CONTINUE, 100)
|
||||||
self.assertEquals(404, status.HTTP_404_NOT_FOUND)
|
|
||||||
|
def test_status_HTTP_101_SWITCHING_PROTOCOLS(self):
|
||||||
|
"""Ensure that HTTP_101_SWITCHING_PROTOCOLS equals 101."""
|
||||||
|
self.assertEquals(status.HTTP_101_SWITCHING_PROTOCOLS, 101)
|
||||||
|
|
||||||
|
def test_status_HTTP_200_OK(self):
|
||||||
|
"""Ensure that HTTP_200_OK equals 200."""
|
||||||
|
self.assertEquals(status.HTTP_200_OK, 200)
|
||||||
|
|
||||||
|
def test_status_HTTP_201_CREATED(self):
|
||||||
|
"""Ensure that HTTP_201_CREATED equals 201."""
|
||||||
|
self.assertEquals(status.HTTP_201_CREATED, 201)
|
||||||
|
|
||||||
|
def test_status_HTTP_202_ACCEPTED(self):
|
||||||
|
"""Ensure that HTTP_202_ACCEPTED equals 202."""
|
||||||
|
self.assertEquals(status.HTTP_202_ACCEPTED, 202)
|
||||||
|
|
||||||
|
def test_status_HTTP_203_NON_AUTHORITATIVE_INFORMATION(self):
|
||||||
|
"""Ensure that HTTP_203_NON_AUTHORITATIVE_INFORMATION equals 203."""
|
||||||
|
self.assertEquals(status.HTTP_203_NON_AUTHORITATIVE_INFORMATION, 203)
|
||||||
|
|
||||||
|
def test_status_HTTP_204_NO_CONTENT(self):
|
||||||
|
"""Ensure that HTTP_204_NO_CONTENT equals 204."""
|
||||||
|
self.assertEquals(status.HTTP_204_NO_CONTENT, 204)
|
||||||
|
|
||||||
|
def test_status_HTTP_205_RESET_CONTENT(self):
|
||||||
|
"""Ensure that HTTP_205_RESET_CONTENT equals 205."""
|
||||||
|
self.assertEquals(status.HTTP_205_RESET_CONTENT, 205)
|
||||||
|
|
||||||
|
def test_status_HTTP_206_PARTIAL_CONTENT(self):
|
||||||
|
"""Ensure that HTTP_206_PARTIAL_CONTENT equals 206."""
|
||||||
|
self.assertEquals(status.HTTP_206_PARTIAL_CONTENT, 206)
|
||||||
|
|
||||||
|
def test_status_HTTP_300_MULTIPLE_CHOICES(self):
|
||||||
|
"""Ensure that HTTP_300_MULTIPLE_CHOICES equals 300."""
|
||||||
|
self.assertEquals(status.HTTP_300_MULTIPLE_CHOICES, 300)
|
||||||
|
|
||||||
|
def test_status_HTTP_301_MOVED_PERMANENTLY(self):
|
||||||
|
"""Ensure that HTTP_301_MOVED_PERMANENTLY equals 301."""
|
||||||
|
self.assertEquals(status.HTTP_301_MOVED_PERMANENTLY, 301)
|
||||||
|
|
||||||
|
def test_status_HTTP_302_FOUND(self):
|
||||||
|
"""Ensure that HTTP_302_FOUND equals 302."""
|
||||||
|
self.assertEquals(status.HTTP_302_FOUND, 302)
|
||||||
|
|
||||||
|
def test_status_HTTP_303_SEE_OTHER(self):
|
||||||
|
"""Ensure that HTTP_303_SEE_OTHER equals 303."""
|
||||||
|
self.assertEquals(status.HTTP_303_SEE_OTHER, 303)
|
||||||
|
|
||||||
|
def test_status_HTTP_304_NOT_MODIFIED(self):
|
||||||
|
"""Ensure that HTTP_304_NOT_MODIFIED equals 304."""
|
||||||
|
self.assertEquals(status.HTTP_304_NOT_MODIFIED, 304)
|
||||||
|
|
||||||
|
def test_status_HTTP_305_USE_PROXY(self):
|
||||||
|
"""Ensure that HTTP_305_USE_PROXY equals 305."""
|
||||||
|
self.assertEquals(status.HTTP_305_USE_PROXY, 305)
|
||||||
|
|
||||||
|
def test_status_HTTP_306_RESERVED(self):
|
||||||
|
"""Ensure that HTTP_306_RESERVED equals 306."""
|
||||||
|
self.assertEquals(status.HTTP_306_RESERVED, 306)
|
||||||
|
|
||||||
|
def test_status_HTTP_307_TEMPORARY_REDIRECT(self):
|
||||||
|
"""Ensure that HTTP_307_TEMPORARY_REDIRECT equals 307."""
|
||||||
|
self.assertEquals(status.HTTP_307_TEMPORARY_REDIRECT, 307)
|
||||||
|
|
||||||
|
def test_status_HTTP_400_BAD_REQUEST(self):
|
||||||
|
"""Ensure that HTTP_400_BAD_REQUEST equals 400."""
|
||||||
|
self.assertEquals(status.HTTP_400_BAD_REQUEST, 400)
|
||||||
|
|
||||||
|
def test_status_HTTP_401_UNAUTHORIZED(self):
|
||||||
|
"""Ensure that HTTP_401_UNAUTHORIZED equals 401."""
|
||||||
|
self.assertEquals(status.HTTP_401_UNAUTHORIZED, 401)
|
||||||
|
|
||||||
|
def test_status_HTTP_402_PAYMENT_REQUIRED(self):
|
||||||
|
"""Ensure that HTTP_402_PAYMENT_REQUIRED equals 402."""
|
||||||
|
self.assertEquals(status.HTTP_402_PAYMENT_REQUIRED, 402)
|
||||||
|
|
||||||
|
def test_status_HTTP_403_FORBIDDEN(self):
|
||||||
|
"""Ensure that HTTP_403_FORBIDDEN equals 403."""
|
||||||
|
self.assertEquals(status.HTTP_403_FORBIDDEN, 403)
|
||||||
|
|
||||||
|
def test_status_HTTP_404_NOT_FOUND(self):
|
||||||
|
"""Ensure that HTTP_404_NOT_FOUND equals 404."""
|
||||||
|
self.assertEquals(status.HTTP_404_NOT_FOUND, 404)
|
||||||
|
|
||||||
|
def test_status_HTTP_405_METHOD_NOT_ALLOWED(self):
|
||||||
|
"""Ensure that HTTP_405_METHOD_NOT_ALLOWED equals 405."""
|
||||||
|
self.assertEquals(status.HTTP_405_METHOD_NOT_ALLOWED, 405)
|
||||||
|
|
||||||
|
def test_status_HTTP_406_NOT_ACCEPTABLE(self):
|
||||||
|
"""Ensure that HTTP_406_NOT_ACCEPTABLE equals 406."""
|
||||||
|
self.assertEquals(status.HTTP_406_NOT_ACCEPTABLE, 406)
|
||||||
|
|
||||||
|
def test_status_HTTP_407_PROXY_AUTHENTICATION_REQUIRED(self):
|
||||||
|
"""Ensure that HTTP_407_PROXY_AUTHENTICATION_REQUIRED equals 407."""
|
||||||
|
self.assertEquals(status.HTTP_407_PROXY_AUTHENTICATION_REQUIRED, 407)
|
||||||
|
|
||||||
|
def test_status_HTTP_408_REQUEST_TIMEOUT(self):
|
||||||
|
"""Ensure that HTTP_408_REQUEST_TIMEOUT equals 408."""
|
||||||
|
self.assertEquals(status.HTTP_408_REQUEST_TIMEOUT, 408)
|
||||||
|
|
||||||
|
def test_status_HTTP_409_CONFLICT(self):
|
||||||
|
"""Ensure that HTTP_409_CONFLICT equals 409."""
|
||||||
|
self.assertEquals(status.HTTP_409_CONFLICT, 409)
|
||||||
|
|
||||||
|
def test_status_HTTP_410_GONE(self):
|
||||||
|
"""Ensure that HTTP_410_GONE equals 410."""
|
||||||
|
self.assertEquals(status.HTTP_410_GONE, 410)
|
||||||
|
|
||||||
|
def test_status_HTTP_411_LENGTH_REQUIRED(self):
|
||||||
|
"""Ensure that HTTP_411_LENGTH_REQUIRED equals 411."""
|
||||||
|
self.assertEquals(status.HTTP_411_LENGTH_REQUIRED, 411)
|
||||||
|
|
||||||
|
def test_status_HTTP_412_PRECONDITION_FAILED(self):
|
||||||
|
"""Ensure that HTTP_412_PRECONDITION_FAILED equals 412."""
|
||||||
|
self.assertEquals(status.HTTP_412_PRECONDITION_FAILED, 412)
|
||||||
|
|
||||||
|
def test_status_HTTP_413_REQUEST_ENTITY_TOO_LARGE(self):
|
||||||
|
"""Ensure that HTTP_413_REQUEST_ENTITY_TOO_LARGE equals 413."""
|
||||||
|
self.assertEquals(status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, 413)
|
||||||
|
|
||||||
|
def test_status_HTTP_414_REQUEST_URI_TOO_LONG(self):
|
||||||
|
"""Ensure that HTTP_414_REQUEST_URI_TOO_LONG equals 414."""
|
||||||
|
self.assertEquals(status.HTTP_414_REQUEST_URI_TOO_LONG, 414)
|
||||||
|
|
||||||
|
def test_status_HTTP_415_UNSUPPORTED_MEDIA_TYPE(self):
|
||||||
|
"""Ensure that HTTP_415_UNSUPPORTED_MEDIA_TYPE equals 415."""
|
||||||
|
self.assertEquals(status.HTTP_415_UNSUPPORTED_MEDIA_TYPE, 415)
|
||||||
|
|
||||||
|
def test_status_HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE(self):
|
||||||
|
"""Ensure that HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE equals 416."""
|
||||||
|
self.assertEquals(status.HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE, 416)
|
||||||
|
|
||||||
|
def test_status_HTTP_417_EXPECTATION_FAILED(self):
|
||||||
|
"""Ensure that HTTP_417_EXPECTATION_FAILED equals 417."""
|
||||||
|
self.assertEquals(status.HTTP_417_EXPECTATION_FAILED, 417)
|
||||||
|
|
||||||
|
def test_status_HTTP_428_PRECONDITION_REQUIRED(self):
|
||||||
|
"""Ensure that HTTP_428_PRECONDITION_REQUIRED equals 428."""
|
||||||
|
self.assertEquals(status.HTTP_428_PRECONDITION_REQUIRED, 428)
|
||||||
|
|
||||||
|
def test_status_HTTP_429_TOO_MANY_REQUESTS(self):
|
||||||
|
"""Ensure that HTTP_429_TOO_MANY_REQUESTS equals 428."""
|
||||||
|
self.assertEquals(status.HTTP_429_TOO_MANY_REQUESTS, 429)
|
||||||
|
|
||||||
|
def test_status_HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE(self):
|
||||||
|
"""Ensure that HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE equals 431."""
|
||||||
|
self.assertEquals(status.HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE, 431)
|
||||||
|
|
||||||
|
def test_status_HTTP_500_INTERNAL_SERVER_ERROR(self):
|
||||||
|
"""Ensure that HTTP_500_INTERNAL_SERVER_ERROR equals 500."""
|
||||||
|
self.assertEquals(status.HTTP_500_INTERNAL_SERVER_ERROR, 500)
|
||||||
|
|
||||||
|
def HTTP_501_NOT_IMPLEMENTED(self):
|
||||||
|
"""Ensure that HTTP_501_NOT_IMPLEMENTED equals 501."""
|
||||||
|
self.assertEquals(status.HTTP_501_NOT_IMPLEMENTED, 501)
|
||||||
|
|
||||||
|
def test_status_HTTP_502_BAD_GATEWAY(self):
|
||||||
|
"""Ensure that HTTP_502_BAD_GATEWAY equals 502."""
|
||||||
|
self.assertEquals(status.HTTP_502_BAD_GATEWAY, 502)
|
||||||
|
|
||||||
|
def test_status_HTTP_503_SERVICE_UNAVAILABLE(self):
|
||||||
|
"""Ensure that HTTP_503_SERVICE_UNAVAILABLE equals 503."""
|
||||||
|
self.assertEquals(status.HTTP_503_SERVICE_UNAVAILABLE, 503)
|
||||||
|
|
||||||
|
def test_status_HTTP_504_GATEWAY_TIMEOUT(self):
|
||||||
|
"""Ensure that HTTP_504_GATEWAY_TIMEOUT equals 504."""
|
||||||
|
self.assertEquals(status.HTTP_504_GATEWAY_TIMEOUT, 504)
|
||||||
|
|
||||||
|
def test_status_HTTP_505_HTTP_VERSION_NOT_SUPPORTED(self):
|
||||||
|
"""Ensure that HTTP_505_HTTP_VERSION_NOT_SUPPORTED equals 505."""
|
||||||
|
self.assertEquals(status.HTTP_505_HTTP_VERSION_NOT_SUPPORTED, 505)
|
||||||
|
|
||||||
|
def test_status_HTTP_511_NETWORK_AUTHENTICATION_REQUIRED(self):
|
||||||
|
"""Ensure that HTTP_511_NETWORK_AUTHENTICATION_REQUIRED equals 511."""
|
||||||
|
self.assertEquals(status.HTTP_511_NETWORK_AUTHENTICATION_REQUIRED, 511)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user