~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to tests/regressiontests/context_processors/tests.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
"""
4
4
 
5
5
from django.conf import settings
 
6
from django.contrib.auth import authenticate
 
7
from django.db.models import Q
6
8
from django.test import TestCase
7
 
 
 
9
from django.template import Template
8
10
 
9
11
class RequestContextProcessorTests(TestCase):
10
12
    """
36
38
        self.assertContains(response, url)
37
39
        response = self.client.post(url, {'path': '/blah/'})
38
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'])