PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/context_processors/tests.py

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