2007-05-10 21:46:15 +08:00
"""
Regression tests for the Test Client , especially the customized assertions .
"""
from django . test import Client , TestCase
2008-03-20 14:50:54 +08:00
from django . core . urlresolvers import reverse
2008-06-07 14:25:59 +08:00
from django . core . exceptions import SuspiciousOperation
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
import os
2008-07-01 23:10:51 +08:00
import sha
2007-05-10 21:46:15 +08:00
2007-07-20 22:32:20 +08:00
class AssertContainsTests ( TestCase ) :
def test_contains ( self ) :
2007-07-20 23:40:54 +08:00
" Responses can be inspected for content, including counting repeated substrings "
2007-07-20 22:32:20 +08:00
response = self . client . get ( ' /test_client_regress/no_template_view/ ' )
2007-07-21 13:15:19 +08:00
2008-06-06 21:50:02 +08:00
self . assertNotContains ( response , ' never ' )
2007-07-21 13:15:19 +08:00
self . assertContains ( response , ' never ' , 0 )
2007-07-20 22:32:20 +08:00
self . assertContains ( response , ' once ' )
self . assertContains ( response , ' once ' , 1 )
self . assertContains ( response , ' twice ' )
self . assertContains ( response , ' twice ' , 2 )
2008-06-06 21:50:02 +08:00
try :
self . assertNotContains ( response , ' once ' )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Response should not contain ' once ' " )
2007-07-21 13:15:19 +08:00
try :
self . assertContains ( response , ' never ' , 1 )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Found 0 instances of ' never ' in response (expected 1) " )
try :
self . assertContains ( response , ' once ' , 0 )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Found 1 instances of ' once ' in response (expected 0) " )
2007-07-20 22:32:20 +08:00
try :
self . assertContains ( response , ' once ' , 2 )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Found 1 instances of ' once ' in response (expected 2) " )
2007-11-11 11:55:44 +08:00
2007-07-20 22:32:20 +08:00
try :
self . assertContains ( response , ' twice ' , 1 )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Found 2 instances of ' twice ' in response (expected 1) " )
2007-11-11 11:55:44 +08:00
2007-07-20 22:32:20 +08:00
try :
self . assertContains ( response , ' thrice ' )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Couldn ' t find ' thrice ' in response " )
try :
self . assertContains ( response , ' thrice ' , 3 )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Found 0 instances of ' thrice ' in response (expected 3) " )
2007-11-11 11:55:44 +08:00
2007-05-10 21:46:15 +08:00
class AssertTemplateUsedTests ( TestCase ) :
fixtures = [ ' testdata.json ' ]
2007-11-11 11:55:44 +08:00
2007-05-10 21:46:15 +08:00
def test_no_context ( self ) :
" Template usage assertions work then templates aren ' t in use "
response = self . client . get ( ' /test_client_regress/no_template_view/ ' )
# Check that the no template case doesn't mess with the template assertions
self . assertTemplateNotUsed ( response , ' GET Template ' )
2007-11-11 11:55:44 +08:00
2007-05-10 21:46:15 +08:00
try :
self . assertTemplateUsed ( response , ' GET Template ' )
except AssertionError , e :
self . assertEquals ( str ( e ) , " No templates used to render the response " )
2007-11-11 11:55:44 +08:00
def test_single_context ( self ) :
2007-05-10 21:46:15 +08:00
" Template assertions work when there is a single context "
response = self . client . get ( ' /test_client/post_view/ ' , { } )
2007-11-11 11:55:44 +08:00
#
2007-05-10 21:46:15 +08:00
try :
self . assertTemplateNotUsed ( response , ' Empty GET Template ' )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Template ' Empty GET Template ' was used unexpectedly in rendering the response " )
2007-11-11 11:55:44 +08:00
2007-05-10 21:46:15 +08:00
try :
2007-11-11 11:55:44 +08:00
self . assertTemplateUsed ( response , ' Empty POST Template ' )
2007-05-10 21:46:15 +08:00
except AssertionError , e :
2007-09-04 07:14:51 +08:00
self . assertEquals ( str ( e ) , " Template ' Empty POST Template ' was not a template used to render the response. Actual template(s) used: Empty GET Template " )
2007-11-11 11:55:44 +08:00
2007-05-10 21:46:15 +08:00
def test_multiple_context ( self ) :
" Template assertions work when there are multiple contexts "
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 ' )
try :
self . assertTemplateNotUsed ( response , " form_view.html " )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Template ' form_view.html ' was used unexpectedly in rendering the response " )
try :
self . assertTemplateNotUsed ( response , ' base.html ' )
except AssertionError , e :
self . assertEquals ( str ( e ) , " Template ' base.html ' was used unexpectedly in rendering the response " )
try :
2007-11-11 11:55:44 +08:00
self . assertTemplateUsed ( response , " Valid POST Template " )
2007-05-10 21:46:15 +08:00
except AssertionError , e :
2007-09-04 07:14:51 +08:00
self . assertEquals ( str ( e ) , " Template ' Valid POST Template ' was not a template used to render the response. Actual template(s) used: form_view.html, base.html " )
2007-05-14 19:07:14 +08:00
class AssertRedirectsTests ( TestCase ) :
def test_redirect_page ( self ) :
2007-11-11 11:55:44 +08:00
" An assertion is raised if the original page couldn ' t be retrieved as expected "
2007-05-14 19:07:14 +08:00
# This page will redirect with code 301, not 302
2007-11-11 11:55:44 +08:00
response = self . client . get ( ' /test_client/permanent_redirect_view/ ' )
2007-05-14 19:07:14 +08:00
try :
self . assertRedirects ( response , ' /test_client/get_view/ ' )
except AssertionError , e :
2007-07-06 12:04:42 +08:00
self . assertEquals ( str ( e ) , " Response didn ' t redirect as expected: Response code was 301 (expected 302) " )
2007-11-11 11:55:44 +08:00
2007-08-31 19:37:28 +08:00
def test_lost_query ( self ) :
" An assertion is raised if the redirect location doesn ' t preserve GET parameters "
response = self . client . get ( ' /test_client/redirect_view/ ' , { ' var ' : ' value ' } )
try :
self . assertRedirects ( response , ' /test_client/get_view/ ' )
except AssertionError , e :
2007-11-11 11:55:44 +08:00
self . assertEquals ( str ( e ) , " Response redirected to ' http://testserver/test_client/get_view/?var=value ' , expected ' http://testserver/test_client/get_view/ ' " )
2007-05-14 19:07:14 +08:00
def test_incorrect_target ( self ) :
" An assertion is raised if the response redirects to another target "
2007-11-11 11:55:44 +08:00
response = self . client . get ( ' /test_client/permanent_redirect_view/ ' )
2007-05-14 19:07:14 +08:00
try :
# Should redirect to get_view
self . assertRedirects ( response , ' /test_client/some_view/ ' )
except AssertionError , e :
2007-07-06 12:04:42 +08:00
self . assertEquals ( str ( e ) , " Response didn ' t redirect as expected: Response code was 301 (expected 302) " )
2007-11-11 11:55:44 +08:00
2007-05-14 19:07:14 +08:00
def test_target_page ( self ) :
2007-07-06 12:04:42 +08:00
" An assertion is raised if the response redirect target cannot be retrieved as expected "
2007-05-14 19:07:14 +08:00
response = self . client . get ( ' /test_client/double_redirect_view/ ' )
try :
# The redirect target responds with a 301 code, not 200
2007-09-14 14:05:54 +08:00
self . assertRedirects ( response , ' http://testserver/test_client/permanent_redirect_view/ ' )
2007-05-14 19:07:14 +08:00
except AssertionError , e :
self . assertEquals ( str ( e ) , " Couldn ' t retrieve redirection page ' /test_client/permanent_redirect_view/ ' : response code was 301 (expected 200) " )
2007-11-11 11:55:44 +08:00
2007-05-10 21:46:15 +08:00
class AssertFormErrorTests ( TestCase ) :
def test_unknown_form ( self ) :
" An assertion is raised if the form name is unknown "
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 )
self . assertTemplateUsed ( response , " Invalid POST Template " )
try :
self . assertFormError ( response , ' wrong_form ' , ' some_field ' , ' Some error. ' )
except AssertionError , e :
self . assertEqual ( str ( e ) , " The form ' wrong_form ' was not used to render the response " )
2007-11-11 11:55:44 +08:00
2007-05-10 21:46:15 +08:00
def test_unknown_field ( self ) :
" An assertion is raised if the field name is unknown "
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 )
self . assertTemplateUsed ( response , " Invalid POST Template " )
try :
self . assertFormError ( response , ' form ' , ' some_field ' , ' Some error. ' )
except AssertionError , e :
self . assertEqual ( str ( e ) , " The form ' form ' in context 0 does not contain the field ' some_field ' " )
2007-11-11 11:55:44 +08:00
2007-05-10 21:46:15 +08:00
def test_noerror_field ( self ) :
" An assertion is raised if the field doesn ' t have any errors "
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 )
self . assertTemplateUsed ( response , " Invalid POST Template " )
try :
self . assertFormError ( response , ' form ' , ' value ' , ' Some error. ' )
except AssertionError , e :
self . assertEqual ( str ( e ) , " The field ' value ' on form ' form ' in context 0 contains no errors " )
2007-11-11 11:55:44 +08:00
2007-05-10 21:46:15 +08:00
def test_unknown_error ( self ) :
" An assertion is raised if the field doesn ' t contain the provided error "
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 )
self . assertTemplateUsed ( response , " Invalid POST Template " )
try :
self . assertFormError ( response , ' form ' , ' email ' , ' Some error. ' )
except AssertionError , e :
self . assertEqual ( str ( e ) , " The field ' email ' on form ' form ' in context 0 does not contain the error ' Some error. ' (actual errors: [u ' Enter a valid e-mail address. ' ]) " )
2007-11-11 11:55:44 +08:00
2007-09-04 08:48:19 +08:00
def test_unknown_nonfield_error ( self ) :
"""
Checks that an assertion is raised if the form ' s non field errors
doesn ' t contain the provided error.
"""
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 )
self . assertTemplateUsed ( response , " Invalid POST Template " )
try :
self . assertFormError ( response , ' form ' , None , ' Some error. ' )
except AssertionError , e :
2007-11-11 11:55:44 +08:00
self . assertEqual ( str ( e ) , " The form ' form ' in context 0 does not contain the non-field error ' Some error. ' (actual errors: ) " )
2007-05-10 21:46:15 +08:00
2007-09-03 19:21:40 +08:00
class LoginTests ( TestCase ) :
fixtures = [ ' testdata ' ]
def test_login_different_client ( self ) :
" Check that using a different test client doesn ' t violate authentication "
# Create a second client, and log in.
c = Client ( )
login = c . login ( username = ' testclient ' , password = ' password ' )
2007-09-14 17:55:17 +08:00
self . failUnless ( login , ' Could not log in ' )
2007-09-03 19:21:40 +08:00
# Get a redirection page with the second client.
response = c . get ( " /test_client_regress/login_protected_redirect_view/ " )
2007-11-11 11:55:44 +08:00
# At this points, the self.client isn't logged in.
# Check that assertRedirects uses the original client, not the
2007-09-03 19:21:40 +08:00
# default client.
2007-09-14 14:05:54 +08:00
self . assertRedirects ( response , " http://testserver/test_client_regress/get_view/ " )
2008-03-20 14:50:54 +08:00
class URLEscapingTests ( TestCase ) :
def test_simple_argument_get ( self ) :
" Get a view that has a simple string argument "
response = self . client . get ( reverse ( ' arg_view ' , args = [ ' Slartibartfast ' ] ) )
self . assertEqual ( response . status_code , 200 )
self . assertEqual ( response . content , ' Howdy, Slartibartfast ' )
def test_argument_with_space_get ( self ) :
" Get a view that has a string argument that requires escaping "
response = self . client . get ( reverse ( ' arg_view ' , args = [ ' Arthur Dent ' ] ) )
self . assertEqual ( response . status_code , 200 )
self . assertEqual ( response . content , ' Hi, Arthur ' )
def test_simple_argument_post ( self ) :
" Post for a view that has a simple string argument "
response = self . client . post ( reverse ( ' arg_view ' , args = [ ' Slartibartfast ' ] ) )
self . assertEqual ( response . status_code , 200 )
self . assertEqual ( response . content , ' Howdy, Slartibartfast ' )
def test_argument_with_space_post ( self ) :
" Post for a view that has a string argument that requires escaping "
response = self . client . post ( reverse ( ' arg_view ' , args = [ ' Arthur Dent ' ] ) )
self . assertEqual ( response . status_code , 200 )
self . assertEqual ( response . content , ' Hi, Arthur ' )
2008-06-07 14:25:59 +08:00
class ExceptionTests ( TestCase ) :
fixtures = [ ' testdata.json ' ]
def test_exception_cleared ( self ) :
" #5836 - A stale user exception isn ' t re-raised by the test client. "
2008-03-20 14:50:54 +08:00
2008-06-07 14:25:59 +08:00
login = self . client . login ( username = ' testclient ' , password = ' password ' )
self . failUnless ( login , ' Could not log in ' )
try :
response = self . client . get ( " /test_client_regress/staff_only/ " )
self . fail ( " General users should not be able to visit this page " )
except SuspiciousOperation :
pass
# At this point, an exception has been raised, and should be cleared.
# This next operation should be successful; if it isn't we have a problem.
login = self . client . login ( username = ' staff ' , password = ' password ' )
self . failUnless ( login , ' Could not log in ' )
try :
self . client . get ( " /test_client_regress/staff_only/ " )
except SuspiciousOperation :
self . fail ( " Staff should be able to visit this page " )
2008-06-30 20:34:29 +08:00
# We need two different tests to check URLconf subsitution - one to check
# it was changed, and another one (without self.urls) to check it was reverted on
# teardown. This pair of tests relies upon the alphabetical ordering of test execution.
class UrlconfSubstitutionTests ( TestCase ) :
urls = ' regressiontests.test_client_regress.urls '
def test_urlconf_was_changed ( self ) :
" TestCase can enforce a custom URLConf on a per-test basis "
url = reverse ( ' arg_view ' , args = [ ' somename ' ] )
self . assertEquals ( url , ' /arg_view/somename/ ' )
# This test needs to run *after* UrlconfSubstitutionTests; the zz prefix in the
# name is to ensure alphabetical ordering.
class zzUrlconfSubstitutionTests ( TestCase ) :
def test_urlconf_was_reverted ( self ) :
" URLconf is reverted to original value after modification in a TestCase "
url = reverse ( ' arg_view ' , args = [ ' somename ' ] )
self . assertEquals ( url , ' /test_client_regress/arg_view/somename/ ' )