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
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class DummyStorage:
|
2013-11-23 14:35:24 +00:00
|
|
|
"""
|
|
|
|
dummy message-store to test the api methods
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.store = []
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
def add(self, level, message, extra_tags=""):
|
2013-11-23 14:35:24 +00:00
|
|
|
self.store.append(message)
|
|
|
|
|
|
|
|
|
2017-01-05 14:23:39 +00:00
|
|
|
class ApiTests(SimpleTestCase):
|
2018-11-26 19:01:27 +00:00
|
|
|
rf = RequestFactory()
|
|
|
|
|
2013-11-23 14:35:24 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.request = self.rf.request()
|
|
|
|
self.storage = DummyStorage()
|
|
|
|
|
|
|
|
def test_ok(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "some message"
|
2013-11-23 14:35:24 +00:00
|
|
|
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):
|
2017-01-05 14:23:39 +00:00
|
|
|
msg = "add_message() argument must be an HttpRequest object, not 'NoneType'."
|
2013-11-23 14:35:24 +00:00
|
|
|
self.request._messages = self.storage
|
2017-01-05 14:23:39 +00:00
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
messages.add_message(None, messages.DEBUG, "some message")
|
2017-01-05 14:23:39 +00:00
|
|
|
self.assertEqual(self.storage.store, [])
|
2013-11-23 14:35:24 +00:00
|
|
|
|
|
|
|
def test_middleware_missing(self):
|
2022-02-04 07:08:27 +00:00
|
|
|
msg = (
|
|
|
|
"You cannot add messages without installing "
|
|
|
|
"django.contrib.messages.middleware.MessageMiddleware"
|
|
|
|
)
|
2017-01-05 14:23:39 +00:00
|
|
|
with self.assertRaisesMessage(messages.MessageFailure, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
messages.add_message(self.request, messages.DEBUG, "some message")
|
2017-01-05 14:23:39 +00:00
|
|
|
self.assertEqual(self.storage.store, [])
|
2013-11-23 14:35:24 +00:00
|
|
|
|
|
|
|
def test_middleware_missing_silently(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.DEBUG, "some message", fail_silently=True
|
|
|
|
)
|
2017-01-05 14:23:39 +00:00
|
|
|
self.assertEqual(self.storage.store, [])
|
2017-01-09 23:03:10 +00:00
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class CustomRequest:
|
2017-01-09 23:03:10 +00:00
|
|
|
def __init__(self, request):
|
|
|
|
self._request = request
|
|
|
|
|
|
|
|
def __getattribute__(self, attr):
|
|
|
|
try:
|
2017-01-21 13:13:44 +00:00
|
|
|
return super().__getattribute__(attr)
|
2017-01-09 23:03:10 +00:00
|
|
|
except AttributeError:
|
|
|
|
return getattr(self._request, attr)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomRequestApiTests(ApiTests):
|
|
|
|
"""
|
|
|
|
add_message() should use ducktyping to allow request wrappers such as the
|
|
|
|
one in Django REST framework.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2017-01-09 23:03:10 +00:00
|
|
|
def setUp(self):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUp()
|
2017-01-09 23:03:10 +00:00
|
|
|
self.request = CustomRequest(self.request)
|