Django

Code

Changeset 6661

Show
Ignore:
Timestamp:
11/10/07 21:54:21 (10 months ago)
Author:
mtredinnick
Message:

When using assertRedirect(), allow the caller to specify relative URLs and
automatically fill in the hostname and scheme (host can be passed in, if
different from the default).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/test/testcases.py

    r6625 r6661  
    11import re 
    22import unittest 
    3 from urlparse import urlsplit 
     3from urlparse import urlsplit, urlunsplit 
    44 
    55from django.http import QueryDict 
     
    7575 
    7676    def assertRedirects(self, response, expected_url, status_code=302, 
    77                         target_status_code=200): 
     77                        target_status_code=200, host=None): 
    7878        """Asserts that a response redirected to a specific URL, and that the 
    7979        redirect URL can be loaded. 
     
    8787        url = response['Location'] 
    8888        scheme, netloc, path, query, fragment = urlsplit(url) 
     89        e_scheme, e_netloc, e_path, e_query, e_fragment = urlsplit(expected_url) 
     90        if not (e_scheme or e_netloc): 
     91            expected_url = urlunsplit(('http', host or 'testserver', e_path, 
     92                    e_query, e_fragment)) 
    8993        self.assertEqual(url, expected_url, 
    9094            "Response redirected to '%s', expected '%s'" % (url, expected_url)) 
  • django/trunk/tests/modeltests/test_client/models.py

    r6658 r6661  
    55The test client is a class that can act like a simple 
    66browser for testing purposes. 
    7    
     7 
    88It allows the user to compose GET and POST requests, and 
    99obtain the response that the server gave to those requests. 
     
    1616 
    1717This is not intended as a replacement for Twill,Selenium, or 
    18 other browser automation frameworks - it is here to allow  
    19 testing against the contexts and templates produced by a view,  
     18other browser automation frameworks - it is here to allow 
     19testing against the contexts and templates produced by a view, 
    2020rather than the HTML rendered to the end-user. 
    2121 
     
    2626class ClientTest(TestCase): 
    2727    fixtures = ['testdata.json'] 
    28      
     28 
    2929    def test_get_view(self): 
    3030        "GET a view" 
     
    3333        data = {'var': u'\xf2'} 
    3434        response = self.client.get('/test_client/get_view/', data) 
    35          
     35 
    3636        # Check some response details 
    3737        self.assertContains(response, 'This is a test') 
     
    4242        "GET a view that normally expects POSTs" 
    4343        response = self.client.get('/test_client/post_view/', {}) 
    44          
     44 
    4545        # Check some response details 
    4646        self.assertEqual(response.status_code, 200) 
     
    4848        self.assertTemplateUsed(response, 'Empty GET Template') 
    4949        self.assertTemplateNotUsed(response, 'Empty POST Template') 
    50          
     50 
    5151    def test_empty_post(self): 
    5252        "POST an empty dictionary to a view" 
    5353        response = self.client.post('/test_client/post_view/', {}) 
    54          
     54 
    5555        # Check some response details 
    5656        self.assertEqual(response.status_code, 200) 
     
    5858        self.assertTemplateNotUsed(response, 'Empty GET Template') 
    5959        self.assertTemplateUsed(response, 'Empty POST Template') 
    60          
     60 
    6161    def test_post(self): 
    6262        "POST some data to a view" 
     
    6565        } 
    6666        response = self.client.post('/test_client/post_view/', post_data) 
    67          
     67 
    6868        # Check some response details 
    6969        self.assertEqual(response.status_code, 200) 
     
    7171        self.assertEqual(response.template.name, 'POST Template') 
    7272        self.failUnless('Data received' in response.content) 
    73          
     73 
    7474    def test_raw_post(self): 
    7575        "POST raw data (with a content type) to a view" 
     
    8484        "GET a URL that redirects elsewhere" 
    8585        response = self.client.get('/test_client/redirect_view/') 
    86         # Check that the response was a 302 (redirect) 
    87         self.assertRedirects(response, 'http://testserver/test_client/get_view/') 
    88          
    89         client_providing_host = Client(HTTP_HOST='django.testserver') 
     86        # Check that the response was a 302 (redirect) and that 
     87        # assertRedirect() understands to put an implicit http://testserver/ in 
     88        # front of non-absolute URLs. 
     89        self.assertRedirects(response, '/test_client/get_view/') 
     90 
     91        host = 'django.testserver' 
     92        client_providing_host = Client(HTTP_HOST=host) 
    9093        response = client_providing_host.get('/test_client/redirect_view/') 
    9194        # Check that the response was a 302 (redirect) with absolute URI 
    92         self.assertRedirects(response, 'http://django.testserver/test_client/get_view/'
    93      
     95        self.assertRedirects(response, '/test_client/get_view/', host=host
     96 
    9497    def test_redirect_with_query(self): 
    9598        "GET a URL that redirects with given GET parameters" 
    9699        response = self.client.get('/test_client/redirect_view/', {'var': 'value'}) 
    97          
     100 
    98101        # Check if parameters are intact 
    99102        self.assertRedirects(response, 'http://testserver/test_client/get_view/?var=value') 
     
    113116        "GET a URL that redirects to a non-200 page" 
    114117        response = self.client.get('/test_client/double_redirect_view/') 
    115          
     118 
    116119        # Check that the response was a 302, and that 
    117120        # the attempt to get the redirection location returned 301 when retrieved 
     
    121124        "GET a URL that responds as '404:Not Found'" 
    122125        response = self.client.get('/test_client/bad_view/') 
    123          
     126 
    124127        # Check that the response was a 404, and that the content contains MAGIC 
    125128        self.assertContains(response, 'MAGIC', status_code=404) 
     
    149152        # Check that the multi-value data has been rolled out ok 
    150153        self.assertContains(response, 'Select a valid choice.', 0) 
    151          
     154 
    152155    def test_incomplete_data_form(self): 
    153156        "POST incomplete data to a form" 
    154157        post_data = { 
    155158            'text': 'Hello World', 
    156             'value': 37             
     159            'value': 37 
    157160        } 
    158161        response = self.client.post('/test_client/form_view/', post_data) 
     
    199202        post_data = { 
    200203            'text': 'Hello World', 
    201             'value': 37             
     204            'value': 37 
    202205        } 
    203206        response = self.client.post('/test_client/form_view_with_template/', post_data) 
     
    227230 
    228231        self.assertFormError(response, 'form', 'email', 'Enter a valid e-mail address.') 
    229          
     232 
    230233    def test_unknown_page(self): 
    231234        "GET an invalid URL" 
    232235        response = self.client.get('/test_client/unknown_view/') 
    233          
     236 
    234237        # Check that the response was a 404 
    235238        self.assertEqual(response.status_code, 404) 
    236          
     239 
    237240    def test_view_with_login(self): 
    238241        "Request a page that is protected with @login_required" 
    239          
     242 
    240243        # Get the page without logging in. Should result in 302. 
    241244        response = self.client.get('/test_client/login_protected_view/') 
    242245        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_view/') 
    243          
     246 
    244247        # Log in 
    245248        login = self.client.login(username='testclient', password='password') 
     
    253256    def test_view_with_method_login(self): 
    254257        "Request a page that is protected with a @login_required method" 
    255          
     258 
    256259        # Get the page without logging in. Should result in 302. 
    257260        response = self.client.get('/test_client/login_protected_method_view/') 
    258261        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_method_view/') 
    259          
     262 
    260263        # Log in 
    261264        login = self.client.login(username='testclient', password='password') 
     
    269272    def test_view_with_login_and_custom_redirect(self): 
    270273        "Request a page that is protected with @login_required(redirect_field_name='redirect_to')" 
    271          
     274 
    272275        # Get the page without logging in. Should result in 302. 
    273276        response = self.client.get('/test_client/login_protected_view_custom_redirect/') 
     
    314317    def test_view_with_permissions(self): 
    315318        "Request a page that is protected with @permission_required" 
    316          
     319 
    317320        # Get the page without logging in. Should result in 302. 
    318321        response = self.client.get('/test_client/permission_protected_view/') 
    319322        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_view/') 
    320          
     323 
    321324        # Log in 
    322325        login = self.client.login(username='testclient', password='password') 
     
    331334    def test_view_with_method_permissions(self): 
    332335        "Request a page that is protected with a @permission_required method" 
    333          
     336 
    334337        # Get the page without logging in. Should result in 302. 
    335338        response = self.client.get('/test_client/permission_protected_method_view/') 
    336339        self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_method_view/') 
    337          
     340 
    338341        # Log in 
    339342        login = self.client.login(username='testclient', password='password') 
     
    354357        except KeyError: 
    355358            pass 
    356          
     359 
    357360        from django.contrib.sessions.models import Session 
    358361        response = self.client.post('/test_client/session_view/') 
    359          
     362 
    360363        # Check that the session was modified 
    361364        self.assertEquals(self.client.session['tobacconist'], 'hovercraft') 
     
    364367        "Request a page that is known to throw an error" 
    365368        self.assertRaises(KeyError, self.client.get, "/test_client/broken_view/") 
    366          
     369 
    367370        #Try the same assertion, a different way 
    368371        try: 
     
    371374        except KeyError: 
    372375            pass 
    373      
     376 
    374377    def test_mail_sending(self): 
    375378        "Test that mail is redirected to a dummy outbox during test setup" 
    376          
     379 
    377380        response = self.client.get('/test_client/mail_sending_view/') 
    378381        self.assertEqual(response.status_code, 200) 
    379          
     382 
    380383        self.assertEqual(len(mail.outbox), 1) 
    381384        self.assertEqual(mail.outbox[0].subject, 'Test message') 
    382385        self.assertEqual(mail.outbox[0].body, 'This is a test email') 
    383         self.assertEqual(mail.outbox[0].from_email, 'from@example.com')  
     386        self.assertEqual(mail.outbox[0].from_email, 'from@example.com') 
    384387        self.assertEqual(mail.outbox[0].to[0], 'first@example.com') 
    385388        self.assertEqual(mail.outbox[0].to[1], 'second@example.com') 
     
    387390    def test_mass_mail_sending(self): 
    388391        "Test that mass mail is redirected to a dummy outbox during test setup" 
    389          
     392 
    390393        response = self.client.get('/test_client/mass_mail_sending_view/') 
    391394        self.assertEqual(response.status_code, 200) 
    392          
     395 
    393396        self.assertEqual(len(mail.outbox), 2) 
    394397        self.assertEqual(mail.outbox[0].subject, 'First Test message') 
    395398        self.assertEqual(mail.outbox[0].body, 'This is the first test email') 
    396         self.assertEqual(mail.outbox[0].from_email, 'from@example.com')  
     399        self.assertEqual(mail.outbox[0].from_email, 'from@example.com') 
    397400        self.assertEqual(mail.outbox[0].to[0], 'first@example.com') 
    398401        self.assertEqual(mail.outbox[0].to[1], 'second@example.com') 
     
    400403        self.assertEqual(mail.outbox[1].subject, 'Second Test message') 
    401404        self.assertEqual(mail.outbox[1].body, 'This is the second test email') 
    402         self.assertEqual(mail.outbox[1].from_email, 'from@example.com')  
     405        self.assertEqual(mail.outbox[1].from_email, 'from@example.com') 
    403406        self.assertEqual(mail.outbox[1].to[0], 'second@example.com') 
    404407        self.assertEqual(mail.outbox[1].to[1], 'third@example.com') 
    405          
     408