~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/contrib/messages/tests/test_fallback.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.contrib.messages import constants
 
2
from django.contrib.messages.storage.fallback import (FallbackStorage,
 
3
    CookieStorage)
 
4
from django.contrib.messages.tests.base import BaseTests
 
5
from django.contrib.messages.tests.test_cookie import (set_cookie_data,
 
6
    stored_cookie_messages_count)
 
7
from django.contrib.messages.tests.test_session import (set_session_data,
 
8
    stored_session_messages_count)
 
9
from django.test import TestCase
 
10
 
 
11
 
 
12
class FallbackTest(BaseTests, TestCase):
 
13
    storage_class = FallbackStorage
 
14
 
 
15
    def get_request(self):
 
16
        self.session = {}
 
17
        request = super(FallbackTest, self).get_request()
 
18
        request.session = self.session
 
19
        return request
 
20
 
 
21
    def get_cookie_storage(self, storage):
 
22
        return storage.storages[-2]
 
23
 
 
24
    def get_session_storage(self, storage):
 
25
        return storage.storages[-1]
 
26
 
 
27
    def stored_cookie_messages_count(self, storage, response):
 
28
        return stored_cookie_messages_count(self.get_cookie_storage(storage),
 
29
                                            response)
 
30
 
 
31
    def stored_session_messages_count(self, storage, response):
 
32
        return stored_session_messages_count(self.get_session_storage(storage))
 
33
 
 
34
    def stored_messages_count(self, storage, response):
 
35
        """
 
36
        Return the storage totals from both cookie and session backends.
 
37
        """
 
38
        total = (self.stored_cookie_messages_count(storage, response) +
 
39
                 self.stored_session_messages_count(storage, response))
 
40
        return total
 
41
 
 
42
    def test_get(self):
 
43
        request = self.get_request()
 
44
        storage = self.storage_class(request)
 
45
        cookie_storage = self.get_cookie_storage(storage)
 
46
 
 
47
        # Set initial cookie data.
 
48
        example_messages = [str(i) for i in range(5)]
 
49
        set_cookie_data(cookie_storage, example_messages)
 
50
 
 
51
        # Overwrite the _get method of the fallback storage to prove it is not
 
52
        # used (it would cause a TypeError: 'NoneType' object is not callable).
 
53
        self.get_session_storage(storage)._get = None
 
54
 
 
55
        # Test that the message actually contains what we expect.
 
56
        self.assertEqual(list(storage), example_messages)
 
57
 
 
58
    def test_get_empty(self):
 
59
        request = self.get_request()
 
60
        storage = self.storage_class(request)
 
61
 
 
62
        # Overwrite the _get method of the fallback storage to prove it is not
 
63
        # used (it would cause a TypeError: 'NoneType' object is not callable).
 
64
        self.get_session_storage(storage)._get = None
 
65
 
 
66
        # Test that the message actually contains what we expect.
 
67
        self.assertEqual(list(storage), [])
 
68
 
 
69
    def test_get_fallback(self):
 
70
        request = self.get_request()
 
71
        storage = self.storage_class(request)
 
72
        cookie_storage = self.get_cookie_storage(storage)
 
73
        session_storage = self.get_session_storage(storage)
 
74
 
 
75
        # Set initial cookie and session data.
 
76
        example_messages = [str(i) for i in range(5)]
 
77
        set_cookie_data(cookie_storage, example_messages[:4] +
 
78
                        [CookieStorage.not_finished])
 
79
        set_session_data(session_storage, example_messages[4:])
 
80
 
 
81
        # Test that the message actually contains what we expect.
 
82
        self.assertEqual(list(storage), example_messages)
 
83
 
 
84
    def test_get_fallback_only(self):
 
85
        request = self.get_request()
 
86
        storage = self.storage_class(request)
 
87
        cookie_storage = self.get_cookie_storage(storage)
 
88
        session_storage = self.get_session_storage(storage)
 
89
 
 
90
        # Set initial cookie and session data.
 
91
        example_messages = [str(i) for i in range(5)]
 
92
        set_cookie_data(cookie_storage, [CookieStorage.not_finished],
 
93
                        encode_empty=True)
 
94
        set_session_data(session_storage, example_messages)
 
95
 
 
96
        # Test that the message actually contains what we expect.
 
97
        self.assertEqual(list(storage), example_messages)
 
98
 
 
99
    def test_flush_used_backends(self):
 
100
        request = self.get_request()
 
101
        storage = self.storage_class(request)
 
102
        cookie_storage = self.get_cookie_storage(storage)
 
103
        session_storage = self.get_session_storage(storage)
 
104
 
 
105
        # Set initial cookie and session data.
 
106
        set_cookie_data(cookie_storage, ['cookie', CookieStorage.not_finished])
 
107
        set_session_data(session_storage, ['session'])
 
108
 
 
109
        # When updating, previously used but no longer needed backends are
 
110
        # flushed.
 
111
        response = self.get_response()
 
112
        list(storage)
 
113
        storage.update(response)
 
114
        session_storing = self.stored_session_messages_count(storage, response)
 
115
        self.assertEqual(session_storing, 0)
 
116
 
 
117
    def test_no_fallback(self):
 
118
        """
 
119
        Confirms that:
 
120
 
 
121
        (1) A short number of messages whose data size doesn't exceed what is
 
122
        allowed in a cookie will all be stored in the CookieBackend.
 
123
 
 
124
        (2) If the CookieBackend can store all messages, the SessionBackend
 
125
        won't be written to at all.
 
126
        """
 
127
        storage = self.get_storage()
 
128
        response = self.get_response()
 
129
 
 
130
        # Overwrite the _store method of the fallback storage to prove it isn't
 
131
        # used (it would cause a TypeError: 'NoneType' object is not callable).
 
132
        self.get_session_storage(storage)._store = None
 
133
 
 
134
        for i in range(5):
 
135
            storage.add(constants.INFO, str(i) * 100)
 
136
        storage.update(response)
 
137
 
 
138
        cookie_storing = self.stored_cookie_messages_count(storage, response)
 
139
        self.assertEqual(cookie_storing, 5)
 
140
        session_storing = self.stored_session_messages_count(storage, response)
 
141
        self.assertEqual(session_storing, 0)
 
142
 
 
143
    def test_session_fallback(self):
 
144
        """
 
145
        Confirms that, if the data exceeds what is allowed in a cookie,
 
146
        messages which did not fit are stored in the SessionBackend.
 
147
        """
 
148
        storage = self.get_storage()
 
149
        response = self.get_response()
 
150
 
 
151
        # see comment in CookieText.test_cookie_max_length
 
152
        msg_size = int((CookieStorage.max_cookie_size - 54) / 4.5 - 37)
 
153
        for i in range(5):
 
154
            storage.add(constants.INFO, str(i) * msg_size)
 
155
        storage.update(response)
 
156
 
 
157
        cookie_storing = self.stored_cookie_messages_count(storage, response)
 
158
        self.assertEqual(cookie_storing, 4)
 
159
        session_storing = self.stored_session_messages_count(storage, response)
 
160
        self.assertEqual(session_storing, 1)
 
161
 
 
162
    def test_session_fallback_only(self):
 
163
        """
 
164
        Confirms that large messages, none of which fit in a cookie, are stored
 
165
        in the SessionBackend (and nothing is stored in the CookieBackend).
 
166
        """
 
167
        storage = self.get_storage()
 
168
        response = self.get_response()
 
169
 
 
170
        storage.add(constants.INFO, 'x' * 5000)
 
171
        storage.update(response)
 
172
 
 
173
        cookie_storing = self.stored_cookie_messages_count(storage, response)
 
174
        self.assertEqual(cookie_storing, 0)
 
175
        session_storing = self.stored_session_messages_count(storage, response)
 
176
        self.assertEqual(session_storing, 1)