django-rest-framework/djangorestframework/tests/views.py

136 lines
5.4 KiB
Python
Raw Normal View History

2012-02-25 22:45:17 +04:00
from django.core.urlresolvers import reverse
from django.conf.urls.defaults import patterns, url, include
2012-01-10 03:23:19 +04:00
from django.http import HttpResponse
from django.test import TestCase
from django import forms
from django.db import models
2012-02-25 22:45:17 +04:00
from django.utils import simplejson as json
from djangorestframework.resources import ModelResource
2012-02-25 22:45:17 +04:00
from djangorestframework.views import (
View,
ListOrCreateModelView,
InstanceModelView
)
class MockView(View):
"""This is a basic mock view"""
pass
2012-01-10 03:23:19 +04:00
class MockViewFinal(View):
"""View with final() override"""
def final(self, request, response, *args, **kwargs):
return HttpResponse('{"test": "passed"}', content_type="application/json")
2012-02-25 22:45:17 +04:00
class ResourceMockView(View):
"""This is a resource-based mock view"""
class MockForm(forms.Form):
foo = forms.BooleanField(required=False)
bar = forms.IntegerField(help_text='Must be an integer.')
baz = forms.CharField(max_length=32)
2011-12-29 17:31:12 +04:00
form = MockForm
2012-02-25 22:45:17 +04:00
class MockResource(ModelResource):
"""This is a mock model-based resource"""
2011-12-29 17:31:12 +04:00
class MockResourceModel(models.Model):
foo = models.BooleanField()
bar = models.IntegerField(help_text='Must be an integer.')
baz = models.CharField(max_length=32, help_text='Free text. Max length 32 chars.')
2011-12-29 17:31:12 +04:00
model = MockResourceModel
fields = ('foo', 'bar', 'baz')
2012-02-25 22:45:17 +04:00
urlpatterns = patterns('',
url(r'^mock/$', MockView.as_view()),
2012-01-10 03:23:19 +04:00
url(r'^mock/final/$', MockViewFinal.as_view()),
url(r'^resourcemock/$', ResourceMockView.as_view()),
url(r'^model/$', ListOrCreateModelView.as_view(resource=MockResource)),
url(r'^model/(?P<pk>[^/]+)/$', InstanceModelView.as_view(resource=MockResource)),
2012-02-25 22:45:17 +04:00
url(r'^restframework/', include('djangorestframework.urls', namespace='djangorestframework')),
)
2012-02-25 22:45:17 +04:00
class BaseViewTests(TestCase):
"""Test the base view class of djangorestframework"""
2011-12-29 17:31:12 +04:00
urls = 'djangorestframework.tests.views'
2012-01-10 03:23:19 +04:00
def test_view_call_final(self):
response = self.client.options('/mock/final/')
self.assertEqual(response['Content-Type'].split(';')[0], "application/json")
2012-02-25 22:45:17 +04:00
data = json.loads(response.content)
2012-01-10 03:23:19 +04:00
self.assertEqual(data['test'], 'passed')
def test_options_method_simple_view(self):
response = self.client.options('/mock/')
2011-12-29 17:31:12 +04:00
self._verify_options_response(response,
name='Mock',
description='This is a basic mock view')
2011-12-29 17:31:12 +04:00
def test_options_method_resource_view(self):
response = self.client.options('/resourcemock/')
2011-12-29 17:31:12 +04:00
self._verify_options_response(response,
name='Resource Mock',
description='This is a resource-based mock view',
2012-02-25 22:45:17 +04:00
fields={'foo': 'BooleanField',
'bar': 'IntegerField',
'baz': 'CharField',
})
2011-12-29 17:31:12 +04:00
def test_options_method_model_resource_list_view(self):
response = self.client.options('/model/')
2011-12-29 17:31:12 +04:00
self._verify_options_response(response,
name='Mock List',
description='This is a mock model-based resource',
2012-02-25 22:45:17 +04:00
fields={'foo': 'BooleanField',
'bar': 'IntegerField',
'baz': 'CharField',
})
def test_options_method_model_resource_detail_view(self):
response = self.client.options('/model/0/')
2011-12-29 17:31:12 +04:00
self._verify_options_response(response,
name='Mock Instance',
description='This is a mock model-based resource',
2012-02-25 22:45:17 +04:00
fields={'foo': 'BooleanField',
'bar': 'IntegerField',
'baz': 'CharField',
})
2011-12-29 17:31:12 +04:00
def _verify_options_response(self, response, name, description, fields=None, status=200,
mime_type='application/json'):
self.assertEqual(response.status_code, status)
self.assertEqual(response['Content-Type'].split(';')[0], mime_type)
2012-02-25 22:45:17 +04:00
data = json.loads(response.content)
2011-12-29 17:31:12 +04:00
self.assertTrue('application/json' in data['renders'])
self.assertEqual(name, data['name'])
self.assertEqual(description, data['description'])
if fields is None:
self.assertFalse(hasattr(data, 'fields'))
else:
2011-12-29 17:31:12 +04:00
self.assertEqual(data['fields'], fields)
class ExtraViewsTests(TestCase):
"""Test the extra views djangorestframework provides"""
2011-12-29 17:31:12 +04:00
urls = 'djangorestframework.tests.views'
def test_login_view(self):
"""Ensure the login view exists"""
2012-02-25 22:45:17 +04:00
response = self.client.get(reverse('djangorestframework:login'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'].split(';')[0], 'text/html')
def test_logout_view(self):
"""Ensure the logout view exists"""
2012-02-25 22:45:17 +04:00
response = self.client.get(reverse('djangorestframework:logout'))
self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'].split(';')[0], 'text/html')