2012-12-21 17:01:03 +08:00
|
|
|
from django.contrib.messages import constants
|
2013-04-03 11:42:00 +08:00
|
|
|
from django.contrib.messages.tests.base import BaseTests
|
2012-12-21 17:01:03 +08:00
|
|
|
from django.contrib.messages.storage.base import Message
|
2009-12-10 00:57:23 +08:00
|
|
|
from django.contrib.messages.storage.session import SessionStorage
|
2012-12-21 17:01:03 +08:00
|
|
|
from django.utils.safestring import SafeData, mark_safe
|
2013-04-03 11:42:00 +08:00
|
|
|
from django.test import TestCase
|
2009-12-10 00:57:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
def set_session_data(storage, messages):
|
|
|
|
"""
|
|
|
|
Sets the messages into the backend request's session and remove the
|
|
|
|
backend's loaded data cache.
|
|
|
|
"""
|
2013-08-22 08:12:19 +08:00
|
|
|
storage.request.session[storage.session_key] = storage.serialize_messages(messages)
|
2009-12-10 00:57:23 +08:00
|
|
|
if hasattr(storage, '_loaded_data'):
|
|
|
|
del storage._loaded_data
|
|
|
|
|
|
|
|
|
|
|
|
def stored_session_messages_count(storage):
|
2013-08-22 08:12:19 +08:00
|
|
|
data = storage.deserialize_messages(storage.request.session.get(storage.session_key, []))
|
2009-12-10 00:57:23 +08:00
|
|
|
return len(data)
|
|
|
|
|
|
|
|
|
2013-04-03 11:42:00 +08:00
|
|
|
class SessionTest(BaseTests, TestCase):
|
2009-12-10 00:57:23 +08:00
|
|
|
storage_class = SessionStorage
|
|
|
|
|
|
|
|
def get_request(self):
|
|
|
|
self.session = {}
|
|
|
|
request = super(SessionTest, self).get_request()
|
|
|
|
request.session = self.session
|
|
|
|
return request
|
|
|
|
|
|
|
|
def stored_messages_count(self, storage, response):
|
|
|
|
return stored_session_messages_count(storage)
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
storage = self.storage_class(self.get_request())
|
|
|
|
# Set initial data.
|
|
|
|
example_messages = ['test', 'me']
|
|
|
|
set_session_data(storage, example_messages)
|
|
|
|
# Test that the message actually contains what we expect.
|
|
|
|
self.assertEqual(list(storage), example_messages)
|
2012-12-21 17:01:03 +08:00
|
|
|
|
|
|
|
def test_safedata(self):
|
|
|
|
"""
|
|
|
|
Tests that a message containing SafeData is keeping its safe status when
|
|
|
|
retrieved from the message storage.
|
|
|
|
"""
|
|
|
|
storage = self.get_storage()
|
|
|
|
|
|
|
|
message = Message(constants.DEBUG, mark_safe("<b>Hello Django!</b>"))
|
|
|
|
set_session_data(storage, [message])
|
|
|
|
self.assertIsInstance(list(storage)[0].message, SafeData)
|