Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
# coding: utf-8
|
2006-09-02 17:34:40 +08:00
|
|
|
"""
|
2007-12-02 05:18:45 +08:00
|
|
|
39. Testing using the Test Client
|
2006-09-02 17:34:40 +08:00
|
|
|
|
|
|
|
The test client is a class that can act like a simple
|
|
|
|
browser for testing purposes.
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
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.
|
|
|
|
|
2008-08-12 22:15:38 +08:00
|
|
|
``Client`` objects are stateful - they will retain cookie (and
|
|
|
|
thus session) details for the lifetime of the ``Client`` instance.
|
2006-09-02 17:34:40 +08:00
|
|
|
|
2008-08-12 22:15:38 +08:00
|
|
|
This is not intended as a replacement for Twill, Selenium, or
|
2007-11-11 11:54:21 +08:00
|
|
|
other browser automation frameworks - it is here to allow
|
|
|
|
testing against the contexts and templates produced by a view,
|
2006-09-02 17:34:40 +08:00
|
|
|
rather than the HTML rendered to the end-user.
|
|
|
|
|
|
|
|
"""
|
2010-08-27 21:54:13 +08:00
|
|
|
from django.conf import settings
|
2007-05-08 19:19:34 +08:00
|
|
|
from django.core import mail
|
2010-10-13 07:37:47 +08:00
|
|
|
from django.test import Client, TestCase, RequestFactory
|
|
|
|
|
|
|
|
from views import get_view
|
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
|
2007-03-01 21:11:08 +08:00
|
|
|
class ClientTest(TestCase):
|
|
|
|
fixtures = ['testdata.json']
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
def test_get_view(self):
|
|
|
|
"GET a view"
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
# The data is ignored, but let's check it doesn't crash the system
|
|
|
|
# anyway.
|
|
|
|
data = {'var': u'\xf2'}
|
|
|
|
response = self.client.get('/test_client/get_view/', data)
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
# Check some response details
|
2007-05-05 11:03:33 +08:00
|
|
|
self.assertContains(response, 'This is a test')
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
self.assertEqual(response.context['var'], u'\xf2')
|
2010-10-10 10:16:33 +08:00
|
|
|
self.assertEqual(response.templates[0].name, 'GET Template')
|
2006-09-02 17:34:40 +08:00
|
|
|
|
|
|
|
def test_get_post_view(self):
|
|
|
|
"GET a view that normally expects POSTs"
|
|
|
|
response = self.client.get('/test_client/post_view/', {})
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
# Check some response details
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2010-10-10 10:16:33 +08:00
|
|
|
self.assertEqual(response.templates[0].name, 'Empty GET Template')
|
2007-05-07 20:34:18 +08:00
|
|
|
self.assertTemplateUsed(response, 'Empty GET Template')
|
|
|
|
self.assertTemplateNotUsed(response, 'Empty POST Template')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
def test_empty_post(self):
|
|
|
|
"POST an empty dictionary to a view"
|
|
|
|
response = self.client.post('/test_client/post_view/', {})
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
# Check some response details
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2010-10-10 10:16:33 +08:00
|
|
|
self.assertEqual(response.templates[0].name, 'Empty POST Template')
|
2007-05-07 20:34:18 +08:00
|
|
|
self.assertTemplateNotUsed(response, 'Empty GET Template')
|
|
|
|
self.assertTemplateUsed(response, 'Empty POST Template')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-02-17 08:23:09 +08:00
|
|
|
def test_post(self):
|
2006-09-02 17:34:40 +08:00
|
|
|
"POST some data to a view"
|
|
|
|
post_data = {
|
|
|
|
'value': 37
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_client/post_view/', post_data)
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
# Check some response details
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.context['data'], '37')
|
2010-10-10 10:16:33 +08:00
|
|
|
self.assertEqual(response.templates[0].name, 'POST Template')
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue('Data received' in response.content)
|
2009-02-27 21:14:59 +08:00
|
|
|
|
2008-07-12 14:16:42 +08:00
|
|
|
def test_response_headers(self):
|
|
|
|
"Check the value of HTTP headers returned in a response"
|
|
|
|
response = self.client.get("/test_client/header_view/")
|
2009-02-27 21:14:59 +08:00
|
|
|
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response['X-DJANGO-TEST'], 'Slartibartfast')
|
2009-02-27 21:14:59 +08:00
|
|
|
|
2007-02-17 08:23:09 +08:00
|
|
|
def test_raw_post(self):
|
2007-05-10 19:27:59 +08:00
|
|
|
"POST raw data (with a content type) to a view"
|
2007-02-17 08:23:09 +08:00
|
|
|
test_doc = """<?xml version="1.0" encoding="utf-8"?><library><book><title>Blink</title><author>Malcolm Gladwell</author></book></library>"""
|
|
|
|
response = self.client.post("/test_client/raw_post_view/", test_doc,
|
|
|
|
content_type="text/xml")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2010-10-10 10:16:33 +08:00
|
|
|
self.assertEqual(response.templates[0].name, "Book template")
|
2007-02-17 08:23:09 +08:00
|
|
|
self.assertEqual(response.content, "Blink - Malcolm Gladwell")
|
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
def test_redirect(self):
|
|
|
|
"GET a URL that redirects elsewhere"
|
|
|
|
response = self.client.get('/test_client/redirect_view/')
|
2007-11-11 11:54:21 +08:00
|
|
|
# Check that the response was a 302 (redirect) and that
|
|
|
|
# assertRedirect() understands to put an implicit http://testserver/ in
|
|
|
|
# front of non-absolute URLs.
|
|
|
|
self.assertRedirects(response, '/test_client/get_view/')
|
|
|
|
|
|
|
|
host = 'django.testserver'
|
|
|
|
client_providing_host = Client(HTTP_HOST=host)
|
2007-09-14 13:28:00 +08:00
|
|
|
response = client_providing_host.get('/test_client/redirect_view/')
|
|
|
|
# Check that the response was a 302 (redirect) with absolute URI
|
2007-11-11 11:54:21 +08:00
|
|
|
self.assertRedirects(response, '/test_client/get_view/', host=host)
|
|
|
|
|
2007-08-31 19:37:28 +08:00
|
|
|
def test_redirect_with_query(self):
|
|
|
|
"GET a URL that redirects with given GET parameters"
|
|
|
|
response = self.client.get('/test_client/redirect_view/', {'var': 'value'})
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-08-31 19:37:28 +08:00
|
|
|
# Check if parameters are intact
|
2007-09-14 14:05:54 +08:00
|
|
|
self.assertRedirects(response, 'http://testserver/test_client/get_view/?var=value')
|
2007-03-22 18:23:52 +08:00
|
|
|
|
2007-05-10 19:27:59 +08:00
|
|
|
def test_permanent_redirect(self):
|
|
|
|
"GET a URL that redirects permanently elsewhere"
|
|
|
|
response = self.client.get('/test_client/permanent_redirect_view/')
|
|
|
|
# Check that the response was a 301 (permanent redirect)
|
2007-09-14 14:05:54 +08:00
|
|
|
self.assertRedirects(response, 'http://testserver/test_client/get_view/', status_code=301)
|
2007-05-10 19:27:59 +08:00
|
|
|
|
2007-09-14 13:28:00 +08:00
|
|
|
client_providing_host = Client(HTTP_HOST='django.testserver')
|
|
|
|
response = client_providing_host.get('/test_client/permanent_redirect_view/')
|
|
|
|
# Check that the response was a 301 (permanent redirect) with absolute URI
|
|
|
|
self.assertRedirects(response, 'http://django.testserver/test_client/get_view/', status_code=301)
|
|
|
|
|
2008-12-08 12:53:34 +08:00
|
|
|
def test_temporary_redirect(self):
|
|
|
|
"GET a URL that does a non-permanent redirect"
|
|
|
|
response = self.client.get('/test_client/temporary_redirect_view/')
|
|
|
|
# Check that the response was a 302 (non-permanent redirect)
|
|
|
|
self.assertRedirects(response, 'http://testserver/test_client/get_view/', status_code=302)
|
|
|
|
|
2007-05-10 19:27:59 +08:00
|
|
|
def test_redirect_to_strange_location(self):
|
|
|
|
"GET a URL that redirects to a non-200 page"
|
|
|
|
response = self.client.get('/test_client/double_redirect_view/')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-05-10 19:27:59 +08:00
|
|
|
# Check that the response was a 302, and that
|
|
|
|
# the attempt to get the redirection location returned 301 when retrieved
|
2007-09-14 14:05:54 +08:00
|
|
|
self.assertRedirects(response, 'http://testserver/test_client/permanent_redirect_view/', target_status_code=301)
|
2007-05-10 19:27:59 +08:00
|
|
|
|
2009-02-27 21:14:59 +08:00
|
|
|
def test_follow_redirect(self):
|
|
|
|
"A URL that redirects can be followed to termination."
|
|
|
|
response = self.client.get('/test_client/double_redirect_view/', follow=True)
|
|
|
|
self.assertRedirects(response, 'http://testserver/test_client/get_view/', status_code=302, target_status_code=200)
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(len(response.redirect_chain), 2)
|
2009-02-27 21:14:59 +08:00
|
|
|
|
2010-02-13 20:01:14 +08:00
|
|
|
def test_redirect_http(self):
|
|
|
|
"GET a URL that redirects to an http URI"
|
|
|
|
response = self.client.get('/test_client/http_redirect_view/',follow=True)
|
|
|
|
self.assertFalse(response.test_was_secure_request)
|
|
|
|
|
|
|
|
def test_redirect_https(self):
|
|
|
|
"GET a URL that redirects to an https URI"
|
|
|
|
response = self.client.get('/test_client/https_redirect_view/',follow=True)
|
|
|
|
self.assertTrue(response.test_was_secure_request)
|
|
|
|
|
2007-05-10 19:27:59 +08:00
|
|
|
def test_notfound_response(self):
|
|
|
|
"GET a URL that responds as '404:Not Found'"
|
|
|
|
response = self.client.get('/test_client/bad_view/')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-05-10 19:27:59 +08:00
|
|
|
# Check that the response was a 404, and that the content contains MAGIC
|
|
|
|
self.assertContains(response, 'MAGIC', status_code=404)
|
|
|
|
|
2007-03-22 18:23:52 +08:00
|
|
|
def test_valid_form(self):
|
|
|
|
"POST valid data to a form"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'foo@example.com',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
|
|
|
'multi': ('b','c','e')
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_client/form_view/', post_data)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2007-05-07 20:34:18 +08:00
|
|
|
self.assertTemplateUsed(response, "Valid POST Template")
|
2007-03-22 18:23:52 +08:00
|
|
|
|
2007-07-21 13:17:20 +08:00
|
|
|
def test_valid_form_with_hints(self):
|
|
|
|
"GET a form, providing hints in the GET data"
|
|
|
|
hints = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'multi': ('b','c','e')
|
|
|
|
}
|
|
|
|
response = self.client.get('/test_client/form_view/', data=hints)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertTemplateUsed(response, "Form GET Template")
|
|
|
|
# Check that the multi-value data has been rolled out ok
|
|
|
|
self.assertContains(response, 'Select a valid choice.', 0)
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-03-22 18:23:52 +08:00
|
|
|
def test_incomplete_data_form(self):
|
|
|
|
"POST incomplete data to a form"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
2007-11-11 11:54:21 +08:00
|
|
|
'value': 37
|
2007-03-22 18:23:52 +08:00
|
|
|
}
|
|
|
|
response = self.client.post('/test_client/form_view/', post_data)
|
2007-05-07 20:34:18 +08:00
|
|
|
self.assertContains(response, 'This field is required.', 3)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertTemplateUsed(response, "Invalid POST Template")
|
|
|
|
|
|
|
|
self.assertFormError(response, 'form', 'email', 'This field is required.')
|
|
|
|
self.assertFormError(response, 'form', 'single', 'This field is required.')
|
|
|
|
self.assertFormError(response, 'form', 'multi', 'This field is required.')
|
2007-03-22 18:23:52 +08:00
|
|
|
|
|
|
|
def test_form_error(self):
|
|
|
|
"POST erroneous data to a form"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'not an email address',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
|
|
|
'multi': ('b','c','e')
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_client/form_view/', post_data)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2007-05-07 20:34:18 +08:00
|
|
|
self.assertTemplateUsed(response, "Invalid POST Template")
|
|
|
|
|
|
|
|
self.assertFormError(response, 'form', 'email', 'Enter a valid e-mail address.')
|
|
|
|
|
|
|
|
def test_valid_form_with_template(self):
|
|
|
|
"POST valid data to a form using multiple templates"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'foo@example.com',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
|
|
|
'multi': ('b','c','e')
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_client/form_view_with_template/', post_data)
|
|
|
|
self.assertContains(response, 'POST data OK')
|
|
|
|
self.assertTemplateUsed(response, "form_view.html")
|
|
|
|
self.assertTemplateUsed(response, 'base.html')
|
|
|
|
self.assertTemplateNotUsed(response, "Valid POST Template")
|
|
|
|
|
|
|
|
def test_incomplete_data_form_with_template(self):
|
|
|
|
"POST incomplete data to a form using multiple templates"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
2007-11-11 11:54:21 +08:00
|
|
|
'value': 37
|
2007-05-07 20:34:18 +08:00
|
|
|
}
|
|
|
|
response = self.client.post('/test_client/form_view_with_template/', post_data)
|
|
|
|
self.assertContains(response, 'POST data has errors')
|
|
|
|
self.assertTemplateUsed(response, 'form_view.html')
|
|
|
|
self.assertTemplateUsed(response, 'base.html')
|
|
|
|
self.assertTemplateNotUsed(response, "Invalid POST Template")
|
|
|
|
|
|
|
|
self.assertFormError(response, 'form', 'email', 'This field is required.')
|
|
|
|
self.assertFormError(response, 'form', 'single', 'This field is required.')
|
|
|
|
self.assertFormError(response, 'form', 'multi', 'This field is required.')
|
|
|
|
|
|
|
|
def test_form_error_with_template(self):
|
|
|
|
"POST erroneous data to a form using multiple templates"
|
|
|
|
post_data = {
|
|
|
|
'text': 'Hello World',
|
|
|
|
'email': 'not an email address',
|
|
|
|
'value': 37,
|
|
|
|
'single': 'b',
|
|
|
|
'multi': ('b','c','e')
|
|
|
|
}
|
|
|
|
response = self.client.post('/test_client/form_view_with_template/', post_data)
|
|
|
|
self.assertContains(response, 'POST data has errors')
|
|
|
|
self.assertTemplateUsed(response, "form_view.html")
|
|
|
|
self.assertTemplateUsed(response, 'base.html')
|
|
|
|
self.assertTemplateNotUsed(response, "Invalid POST Template")
|
|
|
|
|
|
|
|
self.assertFormError(response, 'form', 'email', 'Enter a valid e-mail address.')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
def test_unknown_page(self):
|
|
|
|
"GET an invalid URL"
|
|
|
|
response = self.client.get('/test_client/unknown_view/')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
# Check that the response was a 404
|
|
|
|
self.assertEqual(response.status_code, 404)
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2010-12-23 07:55:13 +08:00
|
|
|
def test_url_parameters(self):
|
|
|
|
"Make sure that URL ;-parameters are not stripped."
|
|
|
|
response = self.client.get('/test_client/unknown_view/;some-parameter')
|
|
|
|
|
|
|
|
# Check that the path in the response includes it (ignore that it's a 404)
|
|
|
|
self.assertEqual(response.request['PATH_INFO'], '/test_client/unknown_view/;some-parameter')
|
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
def test_view_with_login(self):
|
|
|
|
"Request a page that is protected with @login_required"
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
# Get the page without logging in. Should result in 302.
|
|
|
|
response = self.client.get('/test_client/login_protected_view/')
|
2007-09-14 14:05:54 +08:00
|
|
|
self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_view/')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-05-05 23:16:15 +08:00
|
|
|
# Log in
|
2007-09-03 19:21:40 +08:00
|
|
|
login = self.client.login(username='testclient', password='password')
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(login, 'Could not log in')
|
2007-05-05 23:16:15 +08:00
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
# Request a page that requires a login
|
2007-05-05 23:16:15 +08:00
|
|
|
response = self.client.get('/test_client/login_protected_view/')
|
2006-09-02 17:34:40 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.context['user'].username, 'testclient')
|
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
def test_view_with_method_login(self):
|
|
|
|
"Request a page that is protected with a @login_required method"
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
# Get the page without logging in. Should result in 302.
|
|
|
|
response = self.client.get('/test_client/login_protected_method_view/')
|
|
|
|
self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_method_view/')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
# Log in
|
|
|
|
login = self.client.login(username='testclient', password='password')
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(login, 'Could not log in')
|
2007-11-08 06:45:07 +08:00
|
|
|
|
|
|
|
# Request a page that requires a login
|
|
|
|
response = self.client.get('/test_client/login_protected_method_view/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.context['user'].username, 'testclient')
|
|
|
|
|
2007-09-15 03:25:37 +08:00
|
|
|
def test_view_with_login_and_custom_redirect(self):
|
|
|
|
"Request a page that is protected with @login_required(redirect_field_name='redirect_to')"
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-09-15 03:25:37 +08:00
|
|
|
# Get the page without logging in. Should result in 302.
|
|
|
|
response = self.client.get('/test_client/login_protected_view_custom_redirect/')
|
|
|
|
self.assertRedirects(response, 'http://testserver/accounts/login/?redirect_to=/test_client/login_protected_view_custom_redirect/')
|
|
|
|
|
|
|
|
# Log in
|
|
|
|
login = self.client.login(username='testclient', password='password')
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(login, 'Could not log in')
|
2007-09-15 03:25:37 +08:00
|
|
|
|
|
|
|
# Request a page that requires a login
|
|
|
|
response = self.client.get('/test_client/login_protected_view_custom_redirect/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.context['user'].username, 'testclient')
|
|
|
|
|
2006-09-02 17:34:40 +08:00
|
|
|
def test_view_with_bad_login(self):
|
|
|
|
"Request a page that is protected with @login, but use bad credentials"
|
|
|
|
|
2007-05-05 23:16:15 +08:00
|
|
|
login = self.client.login(username='otheruser', password='nopassword')
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(login)
|
2007-02-09 21:47:36 +08:00
|
|
|
|
2007-07-12 23:26:37 +08:00
|
|
|
def test_view_with_inactive_login(self):
|
|
|
|
"Request a page that is protected with @login, but use an inactive login"
|
|
|
|
|
|
|
|
login = self.client.login(username='inactive', password='password')
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(login)
|
2007-07-12 23:26:37 +08:00
|
|
|
|
2007-08-17 22:20:25 +08:00
|
|
|
def test_logout(self):
|
2007-08-17 22:42:25 +08:00
|
|
|
"Request a logout after logging in"
|
2007-08-17 22:20:25 +08:00
|
|
|
# Log in
|
|
|
|
self.client.login(username='testclient', password='password')
|
|
|
|
|
|
|
|
# Request a page that requires a login
|
|
|
|
response = self.client.get('/test_client/login_protected_view/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.context['user'].username, 'testclient')
|
|
|
|
|
|
|
|
# Log out
|
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
# Request a page that requires a login
|
|
|
|
response = self.client.get('/test_client/login_protected_view/')
|
2007-09-14 14:05:54 +08:00
|
|
|
self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_view/')
|
2007-08-17 22:20:25 +08:00
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
def test_view_with_permissions(self):
|
|
|
|
"Request a page that is protected with @permission_required"
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
# Get the page without logging in. Should result in 302.
|
|
|
|
response = self.client.get('/test_client/permission_protected_view/')
|
|
|
|
self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_view/')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
# Log in
|
|
|
|
login = self.client.login(username='testclient', password='password')
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(login, 'Could not log in')
|
2007-11-08 06:45:07 +08:00
|
|
|
|
|
|
|
# Log in with wrong permissions. Should result in 302.
|
|
|
|
response = self.client.get('/test_client/permission_protected_view/')
|
|
|
|
self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_view/')
|
|
|
|
|
|
|
|
# TODO: Log in with right permissions and request the page again
|
|
|
|
|
2011-08-12 22:15:41 +08:00
|
|
|
def test_view_with_permissions_exception(self):
|
|
|
|
"Request a page that is protected with @permission_required but raises a exception"
|
|
|
|
|
|
|
|
# Get the page without logging in. Should result in 403.
|
|
|
|
response = self.client.get('/test_client/permission_protected_view_exception/')
|
|
|
|
self.assertEquals(response.status_code, 403)
|
|
|
|
|
|
|
|
# Log in
|
|
|
|
login = self.client.login(username='testclient', password='password')
|
|
|
|
self.assertTrue(login, 'Could not log in')
|
|
|
|
|
|
|
|
# Log in with wrong permissions. Should result in 403.
|
|
|
|
response = self.client.get('/test_client/permission_protected_view_exception/')
|
|
|
|
self.assertEquals(response.status_code, 403)
|
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
def test_view_with_method_permissions(self):
|
|
|
|
"Request a page that is protected with a @permission_required method"
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
# Get the page without logging in. Should result in 302.
|
|
|
|
response = self.client.get('/test_client/permission_protected_method_view/')
|
|
|
|
self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_method_view/')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
# Log in
|
|
|
|
login = self.client.login(username='testclient', password='password')
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(login, 'Could not log in')
|
2007-11-08 06:45:07 +08:00
|
|
|
|
|
|
|
# Log in with wrong permissions. Should result in 302.
|
|
|
|
response = self.client.get('/test_client/permission_protected_method_view/')
|
|
|
|
self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_method_view/')
|
|
|
|
|
|
|
|
# TODO: Log in with right permissions and request the page again
|
|
|
|
|
2007-02-09 21:47:36 +08:00
|
|
|
def test_session_modifying_view(self):
|
|
|
|
"Request a page that modifies the session"
|
|
|
|
# Session value isn't set initially
|
|
|
|
try:
|
|
|
|
self.client.session['tobacconist']
|
|
|
|
self.fail("Shouldn't have a session value")
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-02-09 21:47:36 +08:00
|
|
|
from django.contrib.sessions.models import Session
|
|
|
|
response = self.client.post('/test_client/session_view/')
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-02-09 21:47:36 +08:00
|
|
|
# Check that the session was modified
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(self.client.session['tobacconist'], 'hovercraft')
|
2007-02-11 08:23:31 +08:00
|
|
|
|
|
|
|
def test_view_with_exception(self):
|
|
|
|
"Request a page that is known to throw an error"
|
|
|
|
self.assertRaises(KeyError, self.client.get, "/test_client/broken_view/")
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-02-11 08:23:31 +08:00
|
|
|
#Try the same assertion, a different way
|
|
|
|
try:
|
|
|
|
self.client.get('/test_client/broken_view/')
|
|
|
|
self.fail('Should raise an error')
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
def test_mail_sending(self):
|
|
|
|
"Test that mail is redirected to a dummy outbox during test setup"
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
response = self.client.get('/test_client/mail_sending_view/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 1)
|
|
|
|
self.assertEqual(mail.outbox[0].subject, 'Test message')
|
|
|
|
self.assertEqual(mail.outbox[0].body, 'This is a test email')
|
2007-11-11 11:54:21 +08:00
|
|
|
self.assertEqual(mail.outbox[0].from_email, 'from@example.com')
|
2007-05-08 19:19:34 +08:00
|
|
|
self.assertEqual(mail.outbox[0].to[0], 'first@example.com')
|
|
|
|
self.assertEqual(mail.outbox[0].to[1], 'second@example.com')
|
|
|
|
|
|
|
|
def test_mass_mail_sending(self):
|
|
|
|
"Test that mass mail is redirected to a dummy outbox during test setup"
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
response = self.client.get('/test_client/mass_mail_sending_view/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2007-11-11 11:54:21 +08:00
|
|
|
|
2007-05-08 19:19:34 +08:00
|
|
|
self.assertEqual(len(mail.outbox), 2)
|
|
|
|
self.assertEqual(mail.outbox[0].subject, 'First Test message')
|
|
|
|
self.assertEqual(mail.outbox[0].body, 'This is the first test email')
|
2007-11-11 11:54:21 +08:00
|
|
|
self.assertEqual(mail.outbox[0].from_email, 'from@example.com')
|
2007-05-08 19:19:34 +08:00
|
|
|
self.assertEqual(mail.outbox[0].to[0], 'first@example.com')
|
|
|
|
self.assertEqual(mail.outbox[0].to[1], 'second@example.com')
|
|
|
|
|
|
|
|
self.assertEqual(mail.outbox[1].subject, 'Second Test message')
|
|
|
|
self.assertEqual(mail.outbox[1].body, 'This is the second test email')
|
2007-11-11 11:54:21 +08:00
|
|
|
self.assertEqual(mail.outbox[1].from_email, 'from@example.com')
|
2007-05-08 19:19:34 +08:00
|
|
|
self.assertEqual(mail.outbox[1].to[0], 'second@example.com')
|
|
|
|
self.assertEqual(mail.outbox[1].to[1], 'third@example.com')
|
2010-08-27 21:54:13 +08:00
|
|
|
|
|
|
|
class CSRFEnabledClientTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# Enable the CSRF middleware for this test
|
|
|
|
self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
|
|
|
|
csrf_middleware_class = 'django.middleware.csrf.CsrfViewMiddleware'
|
|
|
|
if csrf_middleware_class not in settings.MIDDLEWARE_CLASSES:
|
|
|
|
settings.MIDDLEWARE_CLASSES += (csrf_middleware_class,)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
|
|
|
|
|
|
|
|
def test_csrf_enabled_client(self):
|
|
|
|
"A client can be instantiated with CSRF checks enabled"
|
|
|
|
csrf_client = Client(enforce_csrf_checks=True)
|
|
|
|
|
|
|
|
# The normal client allows the post
|
|
|
|
response = self.client.post('/test_client/post_view/', {})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
# The CSRF-enabled client rejects it
|
|
|
|
response = csrf_client.post('/test_client/post_view/', {})
|
|
|
|
self.assertEqual(response.status_code, 403)
|
2010-10-09 12:50:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomTestClient(Client):
|
|
|
|
i_am_customized = "Yes"
|
|
|
|
|
|
|
|
class CustomTestClientTest(TestCase):
|
|
|
|
client_class = CustomTestClient
|
|
|
|
|
|
|
|
def test_custom_test_client(self):
|
|
|
|
"""A test case can specify a custom class for self.client."""
|
|
|
|
self.assertEqual(hasattr(self.client, "i_am_customized"), True)
|
|
|
|
|
2010-10-13 07:37:47 +08:00
|
|
|
|
|
|
|
class RequestFactoryTest(TestCase):
|
|
|
|
def test_request_factory(self):
|
|
|
|
factory = RequestFactory()
|
|
|
|
request = factory.get('/somewhere/')
|
|
|
|
response = get_view(request)
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertContains(response, 'This is a test')
|