thirdparty/google_appengine/lib/django/tests/modeltests/test_client/models.py
changeset 2866 a04b1e4126c4
parent 2864 2e0b0af889be
child 2868 9f7f269383f7
equal deleted inserted replaced
2864:2e0b0af889be 2866:a04b1e4126c4
     1 """
       
     2 38. Testing using the Test Client
       
     3 
       
     4 The test client is a class that can act like a simple
       
     5 browser for testing purposes.
       
     6   
       
     7 It allows the user to compose GET and POST requests, and
       
     8 obtain the response that the server gave to those requests.
       
     9 The server Response objects are annotated with the details
       
    10 of the contexts and templates that were rendered during the
       
    11 process of serving the request.
       
    12 
       
    13 Client objects are stateful - they will retain cookie (and
       
    14 thus session) details for the lifetime of the Client instance.
       
    15 
       
    16 This is not intended as a replacement for Twill,Selenium, or
       
    17 other browser automation frameworks - it is here to allow 
       
    18 testing against the contexts and templates produced by a view, 
       
    19 rather than the HTML rendered to the end-user.
       
    20 
       
    21 """
       
    22 from django.test import Client, TestCase
       
    23 
       
    24 class ClientTest(TestCase):
       
    25     fixtures = ['testdata.json']
       
    26     
       
    27     def setUp(self):
       
    28         "Set up test environment"
       
    29         self.client = Client()
       
    30         
       
    31     def test_get_view(self):
       
    32         "GET a view"
       
    33         response = self.client.get('/test_client/get_view/')
       
    34         
       
    35         # Check some response details
       
    36         self.assertEqual(response.status_code, 200)
       
    37         self.assertEqual(response.context['var'], 42)
       
    38         self.assertEqual(response.template.name, 'GET Template')
       
    39         self.failUnless('This is a test.' in response.content)
       
    40 
       
    41     def test_get_post_view(self):
       
    42         "GET a view that normally expects POSTs"
       
    43         response = self.client.get('/test_client/post_view/', {})
       
    44         
       
    45         # Check some response details
       
    46         self.assertEqual(response.status_code, 200)
       
    47         self.assertEqual(response.template.name, 'Empty GET Template')
       
    48         
       
    49     def test_empty_post(self):
       
    50         "POST an empty dictionary to a view"
       
    51         response = self.client.post('/test_client/post_view/', {})
       
    52         
       
    53         # Check some response details
       
    54         self.assertEqual(response.status_code, 200)
       
    55         self.assertEqual(response.template.name, 'Empty POST Template')
       
    56         
       
    57     def test_post(self):
       
    58         "POST some data to a view"
       
    59         post_data = {
       
    60             'value': 37
       
    61         }
       
    62         response = self.client.post('/test_client/post_view/', post_data)
       
    63         
       
    64         # Check some response details
       
    65         self.assertEqual(response.status_code, 200)
       
    66         self.assertEqual(response.context['data'], '37')
       
    67         self.assertEqual(response.template.name, 'POST Template')
       
    68         self.failUnless('Data received' in response.content)
       
    69         
       
    70     def test_raw_post(self):
       
    71         test_doc = """<?xml version="1.0" encoding="utf-8"?><library><book><title>Blink</title><author>Malcolm Gladwell</author></book></library>"""
       
    72         response = self.client.post("/test_client/raw_post_view/", test_doc,
       
    73                                     content_type="text/xml")
       
    74         self.assertEqual(response.status_code, 200)
       
    75         self.assertEqual(response.template.name, "Book template")
       
    76         self.assertEqual(response.content, "Blink - Malcolm Gladwell")
       
    77 
       
    78     def test_redirect(self):
       
    79         "GET a URL that redirects elsewhere"
       
    80         response = self.client.get('/test_client/redirect_view/')
       
    81         
       
    82         # Check that the response was a 302 (redirect)
       
    83         self.assertEqual(response.status_code, 302)
       
    84 
       
    85     def test_valid_form(self):
       
    86         "POST valid data to a form"
       
    87         post_data = {
       
    88             'text': 'Hello World',
       
    89             'email': 'foo@example.com',
       
    90             'value': 37,
       
    91             'single': 'b',
       
    92             'multi': ('b','c','e')
       
    93         }
       
    94         response = self.client.post('/test_client/form_view/', post_data)
       
    95         self.assertEqual(response.status_code, 200)
       
    96         self.assertEqual(response.template.name, "Valid POST Template")
       
    97 
       
    98     def test_incomplete_data_form(self):
       
    99         "POST incomplete data to a form"
       
   100         post_data = {
       
   101             'text': 'Hello World',
       
   102             'value': 37            
       
   103         }
       
   104         response = self.client.post('/test_client/form_view/', post_data)
       
   105         self.assertEqual(response.status_code, 200)
       
   106         self.assertEqual(response.template.name, "Invalid POST Template")
       
   107 
       
   108     def test_form_error(self):
       
   109         "POST erroneous data to a form"
       
   110         post_data = {
       
   111             'text': 'Hello World',
       
   112             'email': 'not an email address',
       
   113             'value': 37,
       
   114             'single': 'b',
       
   115             'multi': ('b','c','e')
       
   116         }
       
   117         response = self.client.post('/test_client/form_view/', post_data)
       
   118         self.assertEqual(response.status_code, 200)
       
   119         self.assertEqual(response.template.name, "Invalid POST Template")
       
   120         
       
   121     def test_unknown_page(self):
       
   122         "GET an invalid URL"
       
   123         response = self.client.get('/test_client/unknown_view/')
       
   124         
       
   125         # Check that the response was a 404
       
   126         self.assertEqual(response.status_code, 404)
       
   127         
       
   128     def test_view_with_login(self):
       
   129         "Request a page that is protected with @login_required"
       
   130         
       
   131         # Get the page without logging in. Should result in 302.
       
   132         response = self.client.get('/test_client/login_protected_view/')
       
   133         self.assertEqual(response.status_code, 302)
       
   134         
       
   135         # Request a page that requires a login
       
   136         response = self.client.login('/test_client/login_protected_view/', 'testclient', 'password')
       
   137         self.failUnless(response)
       
   138         self.assertEqual(response.status_code, 200)
       
   139         self.assertEqual(response.context['user'].username, 'testclient')
       
   140         self.assertEqual(response.template.name, 'Login Template')
       
   141 
       
   142     def test_view_with_bad_login(self):
       
   143         "Request a page that is protected with @login, but use bad credentials"
       
   144 
       
   145         response = self.client.login('/test_client/login_protected_view/', 'otheruser', 'nopassword')
       
   146         self.failIf(response)
       
   147 
       
   148     def test_session_modifying_view(self):
       
   149         "Request a page that modifies the session"
       
   150         # Session value isn't set initially
       
   151         try:
       
   152             self.client.session['tobacconist']
       
   153             self.fail("Shouldn't have a session value")
       
   154         except KeyError:
       
   155             pass
       
   156         
       
   157         from django.contrib.sessions.models import Session
       
   158         response = self.client.post('/test_client/session_view/')
       
   159         
       
   160         # Check that the session was modified
       
   161         self.assertEquals(self.client.session['tobacconist'], 'hovercraft')
       
   162 
       
   163     def test_view_with_exception(self):
       
   164         "Request a page that is known to throw an error"
       
   165         self.assertRaises(KeyError, self.client.get, "/test_client/broken_view/")
       
   166         
       
   167         #Try the same assertion, a different way
       
   168         try:
       
   169             self.client.get('/test_client/broken_view/')
       
   170             self.fail('Should raise an error')
       
   171         except KeyError:
       
   172             pass