2013-11-23 14:35:24 +00:00
|
|
|
from django.contrib import messages
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import RequestFactory, SimpleTestCase
|
2013-11-23 14:35:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DummyStorage(object):
|
|
|
|
"""
|
|
|
|
dummy message-store to test the api methods
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.store = []
|
|
|
|
|
|
|
|
def add(self, level, message, extra_tags=''):
|
|
|
|
self.store.append(message)
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class ApiTest(SimpleTestCase):
|
2013-11-23 14:35:24 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.rf = RequestFactory()
|
|
|
|
self.request = self.rf.request()
|
|
|
|
self.storage = DummyStorage()
|
|
|
|
|
|
|
|
def test_ok(self):
|
|
|
|
msg = 'some message'
|
|
|
|
|
|
|
|
self.request._messages = self.storage
|
|
|
|
messages.add_message(self.request, messages.DEBUG, msg)
|
|
|
|
self.assertIn(msg, self.storage.store)
|
|
|
|
|
|
|
|
def test_request_is_none(self):
|
|
|
|
msg = 'some message'
|
|
|
|
|
|
|
|
self.request._messages = self.storage
|
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
messages.add_message(None, messages.DEBUG, msg)
|
|
|
|
|
|
|
|
self.assertEqual([], self.storage.store)
|
|
|
|
|
|
|
|
def test_middleware_missing(self):
|
|
|
|
msg = 'some message'
|
|
|
|
|
|
|
|
with self.assertRaises(messages.MessageFailure):
|
|
|
|
messages.add_message(self.request, messages.DEBUG, msg)
|
|
|
|
|
|
|
|
self.assertEqual([], self.storage.store)
|
|
|
|
|
|
|
|
def test_middleware_missing_silently(self):
|
|
|
|
msg = 'some message'
|
|
|
|
|
|
|
|
messages.add_message(self.request, messages.DEBUG, msg,
|
|
|
|
fail_silently=True)
|
|
|
|
|
|
|
|
self.assertEqual([], self.storage.store)
|