parts/django/tests/regressiontests/context_processors/tests.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 """
       
     2 Tests for Django's bundled context processors.
       
     3 """
       
     4 
       
     5 from django.conf import settings
       
     6 from django.contrib.auth import authenticate
       
     7 from django.db.models import Q
       
     8 from django.test import TestCase
       
     9 from django.template import Template
       
    10 
       
    11 class RequestContextProcessorTests(TestCase):
       
    12     """
       
    13     Tests for the ``django.core.context_processors.request`` processor.
       
    14     """
       
    15 
       
    16     urls = 'regressiontests.context_processors.urls'
       
    17 
       
    18     def test_request_attributes(self):
       
    19         """
       
    20         Test that the request object is available in the template and that its
       
    21         attributes can't be overridden by GET and POST parameters (#3828).
       
    22         """
       
    23         url = '/request_attrs/'
       
    24         # We should have the request object in the template.
       
    25         response = self.client.get(url)
       
    26         self.assertContains(response, 'Have request')
       
    27         # Test is_secure.
       
    28         response = self.client.get(url)
       
    29         self.assertContains(response, 'Not secure')
       
    30         response = self.client.get(url, {'is_secure': 'blah'})
       
    31         self.assertContains(response, 'Not secure')
       
    32         response = self.client.post(url, {'is_secure': 'blah'})
       
    33         self.assertContains(response, 'Not secure')
       
    34         # Test path.
       
    35         response = self.client.get(url)
       
    36         self.assertContains(response, url)
       
    37         response = self.client.get(url, {'path': '/blah/'})
       
    38         self.assertContains(response, url)
       
    39         response = self.client.post(url, {'path': '/blah/'})
       
    40         self.assertContains(response, url)
       
    41 
       
    42 class AuthContextProcessorTests(TestCase):
       
    43     """
       
    44     Tests for the ``django.contrib.auth.context_processors.auth`` processor
       
    45     """
       
    46     urls = 'regressiontests.context_processors.urls'
       
    47     fixtures = ['context-processors-users.xml']
       
    48 
       
    49     def test_session_not_accessed(self):
       
    50         """
       
    51         Tests that the session is not accessed simply by including
       
    52         the auth context processor
       
    53         """
       
    54         response = self.client.get('/auth_processor_no_attr_access/')
       
    55         self.assertContains(response, "Session not accessed")
       
    56 
       
    57     def test_session_is_accessed(self):
       
    58         """
       
    59         Tests that the session is accessed if the auth context processor
       
    60         is used and relevant attributes accessed.
       
    61         """
       
    62         response = self.client.get('/auth_processor_attr_access/')
       
    63         self.assertContains(response, "Session accessed")
       
    64 
       
    65     def test_perms_attrs(self):
       
    66         self.client.login(username='super', password='secret')
       
    67         response = self.client.get('/auth_processor_perms/')
       
    68         self.assertContains(response, "Has auth permissions")
       
    69 
       
    70     def test_message_attrs(self):
       
    71         self.client.login(username='super', password='secret')
       
    72         response = self.client.get('/auth_processor_messages/')
       
    73         self.assertContains(response, "Message 1")
       
    74 
       
    75     def test_user_attrs(self):
       
    76         """
       
    77         Test that the lazy objects returned behave just like the wrapped objects.
       
    78         """
       
    79         # These are 'functional' level tests for common use cases.  Direct
       
    80         # testing of the implementation (SimpleLazyObject) is in the 'utils'
       
    81         # tests.
       
    82         self.client.login(username='super', password='secret')
       
    83         user = authenticate(username='super', password='secret')
       
    84         response = self.client.get('/auth_processor_user/')
       
    85         self.assertContains(response, "unicode: super")
       
    86         self.assertContains(response, "id: 100")
       
    87         self.assertContains(response, "username: super")
       
    88         # bug #12037 is tested by the {% url %} in the template:
       
    89         self.assertContains(response, "url: /userpage/super/")
       
    90 
       
    91         # See if this object can be used for queries where a Q() comparing
       
    92         # a user can be used with another Q() (in an AND or OR fashion).
       
    93         # This simulates what a template tag might do with the user from the
       
    94         # context. Note that we don't need to execute a query, just build it.
       
    95         #
       
    96         # The failure case (bug #12049) on Python 2.4 with a LazyObject-wrapped
       
    97         # User is a fatal TypeError: "function() takes at least 2 arguments
       
    98         # (0 given)" deep inside deepcopy().
       
    99         #
       
   100         # Python 2.5 and 2.6 succeeded, but logged internally caught exception
       
   101         # spew:
       
   102         #
       
   103         #    Exception RuntimeError: 'maximum recursion depth exceeded while
       
   104         #    calling a Python object' in <type 'exceptions.AttributeError'>
       
   105         #    ignored"
       
   106         query = Q(user=response.context['user']) & Q(someflag=True)
       
   107 
       
   108         # Tests for user equality.  This is hard because User defines
       
   109         # equality in a non-duck-typing way
       
   110         # See bug #12060
       
   111         self.assertEqual(response.context['user'], user)
       
   112         self.assertEqual(user, response.context['user'])