diff options
Diffstat (limited to 'tests/modeltests/test_client')
| -rw-r--r-- | tests/modeltests/test_client/__init__.py | 0 | ||||
| -rw-r--r-- | tests/modeltests/test_client/management.py | 10 | ||||
| -rw-r--r-- | tests/modeltests/test_client/models.py | 101 | ||||
| -rw-r--r-- | tests/modeltests/test_client/urls.py | 9 | ||||
| -rw-r--r-- | tests/modeltests/test_client/views.py | 35 |
5 files changed, 155 insertions, 0 deletions
diff --git a/tests/modeltests/test_client/__init__.py b/tests/modeltests/test_client/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/modeltests/test_client/__init__.py diff --git a/tests/modeltests/test_client/management.py b/tests/modeltests/test_client/management.py new file mode 100644 index 0000000000..9b5a5c498e --- /dev/null +++ b/tests/modeltests/test_client/management.py @@ -0,0 +1,10 @@ +from django.dispatch import dispatcher +from django.db.models import signals +import models as test_client_app +from django.contrib.auth.models import User + +def setup_test(app, created_models, verbosity): + # Create a user account for the login-based tests + User.objects.create_user('testclient','testclient@example.com', 'password') + +dispatcher.connect(setup_test, sender=test_client_app, signal=signals.post_syncdb) diff --git a/tests/modeltests/test_client/models.py b/tests/modeltests/test_client/models.py new file mode 100644 index 0000000000..c5b1a241ca --- /dev/null +++ b/tests/modeltests/test_client/models.py @@ -0,0 +1,101 @@ +""" +39. Testing using the Test Client + +The test client is a class that can act like a simple +browser for testing purposes. + +It allows the user to compose GET and POST requests, and +obtain the response that the server gave to those requests. +The server Response objects are annotated with the details +of the contexts and templates that were rendered during the +process of serving the request. + +Client objects are stateful - they will retain cookie (and +thus session) details for the lifetime of the Client instance. + +This is not intended as a replacement for Twill,Selenium, or +other browser automation frameworks - it is here to allow +testing against the contexts and templates produced by a view, +rather than the HTML rendered to the end-user. + +""" +from django.test.client import Client +import unittest + +class ClientTest(unittest.TestCase): + def setUp(self): + "Set up test environment" + self.client = Client() + + def test_get_view(self): + "GET a view" + response = self.client.get('/test_client/get_view/') + + # Check some response details + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['var'], 42) + self.assertEqual(response.template.name, 'GET Template') + self.failUnless('This is a test.' in response.content) + + def test_get_post_view(self): + "GET a view that normally expects POSTs" + response = self.client.get('/test_client/post_view/', {}) + + # Check some response details + self.assertEqual(response.status_code, 200) + self.assertEqual(response.template.name, 'Empty POST Template') + + def test_empty_post(self): + "POST an empty dictionary to a view" + response = self.client.post('/test_client/post_view/', {}) + + # Check some response details + self.assertEqual(response.status_code, 200) + self.assertEqual(response.template.name, 'Empty POST Template') + + def test_post_view(self): + "POST some data to a view" + post_data = { + 'value': 37 + } + response = self.client.post('/test_client/post_view/', post_data) + + # Check some response details + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['data'], '37') + self.assertEqual(response.template.name, 'POST Template') + self.failUnless('Data received' in response.content) + + def test_redirect(self): + "GET a URL that redirects elsewhere" + response = self.client.get('/test_client/redirect_view/') + + # Check that the response was a 302 (redirect) + self.assertEqual(response.status_code, 302) + + def test_unknown_page(self): + "GET an invalid URL" + response = self.client.get('/test_client/unknown_view/') + + # Check that the response was a 404 + self.assertEqual(response.status_code, 404) + + def test_view_with_login(self): + "Request a page that is protected with @login_required" + + # Get the page without logging in. Should result in 302. + response = self.client.get('/test_client/login_protected_view/') + self.assertEqual(response.status_code, 302) + + # Request a page that requires a login + response = self.client.login('/test_client/login_protected_view/', 'testclient', 'password') + self.assertTrue(response) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.context['user'].username, 'testclient') + self.assertEqual(response.template.name, 'Login Template') + + def test_view_with_bad_login(self): + "Request a page that is protected with @login, but use bad credentials" + + response = self.client.login('/test_client/login_protected_view/', 'otheruser', 'nopassword') + self.assertFalse(response) diff --git a/tests/modeltests/test_client/urls.py b/tests/modeltests/test_client/urls.py new file mode 100644 index 0000000000..09bba5c007 --- /dev/null +++ b/tests/modeltests/test_client/urls.py @@ -0,0 +1,9 @@ +from django.conf.urls.defaults import * +import views + +urlpatterns = patterns('', + (r'^get_view/$', views.get_view), + (r'^post_view/$', views.post_view), + (r'^redirect_view/$', views.redirect_view), + (r'^login_protected_view/$', views.login_protected_view), +) diff --git a/tests/modeltests/test_client/views.py b/tests/modeltests/test_client/views.py new file mode 100644 index 0000000000..bf131032eb --- /dev/null +++ b/tests/modeltests/test_client/views.py @@ -0,0 +1,35 @@ +from django.template import Context, Template +from django.http import HttpResponse, HttpResponseRedirect +from django.contrib.auth.decorators import login_required + +def get_view(request): + "A simple view that expects a GET request, and returns a rendered template" + t = Template('This is a test. {{ var }} is the value.', name='GET Template') + c = Context({'var': 42}) + + return HttpResponse(t.render(c)) + +def post_view(request): + """A view that expects a POST, and returns a different template depending + on whether any POST data is available + """ + if request.POST: + t = Template('Data received: {{ data }} is the value.', name='POST Template') + c = Context({'data': request.POST['value']}) + else: + t = Template('Viewing POST page.', name='Empty POST Template') + c = Context() + + return HttpResponse(t.render(c)) + +def redirect_view(request): + "A view that redirects all requests to the GET view" + return HttpResponseRedirect('/test_client/get_view/') + +@login_required +def login_protected_view(request): + "A simple view that is login protected." + t = Template('This is a login protected test. Username is {{ user.username }}.', name='Login Template') + c = Context({'user': request.user}) + + return HttpResponse(t.render(c)) |
